是否有 CSS 优化器可以发现选择器中相同的单个属性并将它们分组在一起?

发布于 2024-08-23 02:15:59 字数 536 浏览 13 评论 0原文

这是我期望的示例:

输入:

a {
    background: red;    
}

p {
    background: red;    
}

strong {
    background: red;
    color: green;
}

输出:

strong{color:green;}
a,p,strong{background:red;}

大多数优化器将输出如下内容:

strong{background:red;color:green;}
a,p{background:red;}

注意它如何没有识别出 strong 的事实,尽管它包含 color : green;,还包含 background: red; 因此它可以与其他组合在一起吗?

您能提供的任何帮助将不胜感激

一切顺利

祝伊恩

Here is an example of what I'd expect:

Input:

a {
    background: red;    
}

p {
    background: red;    
}

strong {
    background: red;
    color: green;
}

Output:

strong{color:green;}
a,p,strong{background:red;}

Most optimisers will output something like this:

strong{background:red;color:green;}
a,p{background:red;}

Notice how it hasn't picked up the fact that strong, although it contains color: green;, also contains background: red; thus it can be grouped with the others?

Any help you could provide would be greatly appreciated

All the best

Iain

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

雾里花 2024-08-30 02:15:59

也许 CSSTidy 可以在这里提供帮助。

如果你看一下这个CSS“之前”,你会看到这部分代码:

a:hover{
    color: #DD6900;
}

a.admin:hover,a.mod:hover{
    color: #DD6900;
}

CSS "after" 中,您会得到:

a:hover,a.admin:hover,a.mod:hover,a.topictitle:hover {
color:#DD6900
}

不确定它会处理所有可能的情况——但在某些情况下,它似乎正在按照你的要求做;-)

Maybe CSSTidy could help, here.

If you take a look at this CSS "before", you'll see this portion of code :

a:hover{
    color: #DD6900;
}

a.admin:hover,a.mod:hover{
    color: #DD6900;
}

In in the CSS "after", you'll get :

a:hover,a.admin:hover,a.mod:hover,a.topictitle:hover {
color:#DD6900
}

Not sure it'll deal with every possible case -- but in some situations, it seems it's doing what you're asking ;-)

如何视而不见 2024-08-30 02:15:59

好吧,看起来我想要的东西要么不存在,要么很难实现,所以我编写了一个脚本来解决我的特定问题。我将其粘贴在这里,希望有人有一天会发现它有用。

<?php

$css = file_get_contents('test.css');

//Strip comments and whitespace. Tabs to spaces
$css = preg_replace("/\s{2,}/e", ' ', $css);
$css = preg_replace("/\/\*.*?\*\//", '', $css);
$css = str_replace("\t", " ", $css);
$css = str_replace(": ", ":", $css);
$css = str_replace(" }", "}", $css);
$css = str_replace("{", "{", $css);
$css = str_replace(";}", "}", $css);

//Break each rule out onto a new line
$css = preg_replace("/}\s*/", "}\r\n", $css);

//Break @ rules out onto new lines
$css = preg_replace('/(@.*?;\s*)/', '\0'."\r\n", $css);


//Parse CSS Rules
$parsed = array();
$css = explode("\r\n", $css);
foreach($css as $line => $rule){
    if (preg_match('/(.*?)\{(.*?)\}/i', $rule, $regs)) {
        $clean_selectors =  preg_replace('/\s*,\s*/', ',', $regs[1]);
        $clean_selectors =  preg_replace('/,\s*$|\s$/', '', $clean_selectors);
        $parsed[$line]['selectors'] = explode(',', $clean_selectors);
        $parsed[$line]['properties'] = explode(';', $regs[2]);
    } elseif(trim($rule) != '') {
        $parsed[$line] = $rule;
    }   
}

//Group CSS by property
$groups =  array();
foreach($parsed as $line => $css){
    if(is_array($css)){
        foreach($css['properties'] as $pline => $property){
            if(isset($groups[$property])){
                $groups[$property] = array_merge($groups[$property], $css['selectors']);
            } else {
                $groups[$property] = $css['selectors'];
            }

        }
    } else {
        $groups[$line] = $css;  
    }
}

//Output CSS sorted by property
foreach($groups as $property => $selectors){
    if(is_array($selectors)){
        asort($selectors);
        echo implode(",\r\n", $selectors)." {\r\n\t".trim($property).";\r\n}\r\n\r\n";
    } else {
        echo $selectors."\r\n\r\n"; 
    }
}

?>

现在,有几点注意事项。

  1. 不,这不是世界上最漂亮的代码,它很快就解决了我曾经遇到的一个特定问题,并且它非常适合我使用的 CSS。也就是说,它应该足够通用,可以与您扔给它的大多数 CSS 一起使用。

  2. CSS 的本质是有时规则出现的顺序对于最终文档的呈现很重要。如果您只是通过此脚本运行所有 CSS,您的页面可能不会再按您的预期呈现。我只是使用此脚本对我无法控制布局的 Web 应用程序上特定于页面的 css 进行分组。由于每个规则都适用于特定页面上的特定元素,因此当我以这种方式分组时,我并不期望有大量的狡猾之处 - 它只会使 CSS 更易于维护。

Okay, so it seems like what I was after either doesn't exist or is very hard to come by, so I wrote a script that solves my particular problem. I'm pasting it here in the hope that someone sometime might find it useful.

<?php

$css = file_get_contents('test.css');

//Strip comments and whitespace. Tabs to spaces
$css = preg_replace("/\s{2,}/e", ' ', $css);
$css = preg_replace("/\/\*.*?\*\//", '', $css);
$css = str_replace("\t", " ", $css);
$css = str_replace(": ", ":", $css);
$css = str_replace(" }", "}", $css);
$css = str_replace("{", "{", $css);
$css = str_replace(";}", "}", $css);

//Break each rule out onto a new line
$css = preg_replace("/}\s*/", "}\r\n", $css);

//Break @ rules out onto new lines
$css = preg_replace('/(@.*?;\s*)/', '\0'."\r\n", $css);


//Parse CSS Rules
$parsed = array();
$css = explode("\r\n", $css);
foreach($css as $line => $rule){
    if (preg_match('/(.*?)\{(.*?)\}/i', $rule, $regs)) {
        $clean_selectors =  preg_replace('/\s*,\s*/', ',', $regs[1]);
        $clean_selectors =  preg_replace('/,\s*$|\s$/', '', $clean_selectors);
        $parsed[$line]['selectors'] = explode(',', $clean_selectors);
        $parsed[$line]['properties'] = explode(';', $regs[2]);
    } elseif(trim($rule) != '') {
        $parsed[$line] = $rule;
    }   
}

//Group CSS by property
$groups =  array();
foreach($parsed as $line => $css){
    if(is_array($css)){
        foreach($css['properties'] as $pline => $property){
            if(isset($groups[$property])){
                $groups[$property] = array_merge($groups[$property], $css['selectors']);
            } else {
                $groups[$property] = $css['selectors'];
            }

        }
    } else {
        $groups[$line] = $css;  
    }
}

//Output CSS sorted by property
foreach($groups as $property => $selectors){
    if(is_array($selectors)){
        asort($selectors);
        echo implode(",\r\n", $selectors)." {\r\n\t".trim($property).";\r\n}\r\n\r\n";
    } else {
        echo $selectors."\r\n\r\n"; 
    }
}

?>

Now, a couple of cavaets.

  1. No, this is not the most beautiful code in the world, it was done quickly to solve one particular problem I was having once and it's tailored pretty heavily to the CSS I've been given to work with. That said, it should be generic enough to work with most CSS you throw at it.

  2. It is the nature of CSS that sometimes the order in which a rule appears is important to the rendering of the final document. It is likely that if you just run all your CSS through this script that your page won't render as you expect anymore. I'm just using this script to group page-specific css on a web application that I have no layout control over. As each rule applies to a particular element on a particular page, I'm not expecting huge amounts of dodgyness when I group in this way - it's just going to make the CSS more maintainable.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文