我一直在尝试scssphp http://leafo.net/scssphp/,一个 PHP SCSS 编译器,特别是它的压缩的格式化程序。
这会输出类似的内容(注意闭花括号之前的额外空格和分号)
.navbar{margin-bottom :0px !important;margin-top :4px;}
所以我想实现这个小修改从生成的代码中删除一些位。
I know it makes little sense, in that I will save at most some percents in size; and I'm removing low-entropy information, which means that once I gzcompress it, the savings will be even smaller; while the modification will then have to be maintained. But it seemed easy enough, and harmless enough, and I... sort of did it for the lulz.
4225 public function format($block) {
4226 ob_start();
4227 this->block($block);
4228 $out = ob_get_clean();
4229--- return $out; // OLD
4229+++ return $this->postprocess($out); // NEW
4230 }
后处理函数在父格式化程序上是虚拟的,如上所示:
4232 protected function postprocess($text) {
4233 return $text;
4234 }
...而在scss_formatter_compressed
我执行一些替换:
+++ from line 4343:
protected function postprocess($text) {
$xlat = array(
'#\\s+(:\\d)#' => '\\1',
'#;}#' => '}',
'#\\s+(!important)#' => '\\1',
);
return preg_replace(
array_keys($xlat),
array_values($xlat),
$text);
}
现在输出是:
.navbar{margin-bottom:0px!important;margin-top:2px}
...对于简单的 SCSS 来说它是有效的。我已经在大型且复杂的 SCSS 层次结构上对其进行了测试,并且某些元素损坏了。
我有感觉它涉及到!important
条款。我做了一个并不广泛的研究,发现this https://github.com/yui/yuicompressor/issues/37在 YUI-compressor 网站上,一方面他们得出结论
@tml @namanyayg 你能提供任何删除空格的例子吗
在 '!important' 导致样式损坏之前?
我们收到了类似的请求https://github.com/GoalSmashers/clean-css https://github.com/GoalSmashers/clean-css但没有测试用例
无法验证您是否犯了错误或是否存在严重错误
空间必须存在的特定情况。
根据我们的测试摆脱它就可以了。何况还有这样的
代码已经在生产中使用了几年,没有任何
问题。
另一方面,OPhad一些问题,并且他们seemed与之前的空间相关!important
.
现在,如果这是一个 CSS 标准问题,我想修复我对 scssphp 的看法;如果是那个 SCSS 的问题,我当然希望修复我的 SCSS 文件。考虑到该文件夹的庞大规模和复杂性,我想在深入研究其未探知的深度之前先在这里问一下。