将 CSS 规则减少到最小公分母

发布于 2024-12-10 13:05:17 字数 1841 浏览 0 评论 0原文

我有一个 PHP 模板引擎,它允许您在渲染之前为对象设置样式属性。

在开发期间,我希望这些属性是内联的,这样我就可以轻松找到它们并更改它们。但是,对于生产,我想提取它们并将它们缓存在 css 文件中。我还想从模板中的任何 html 元素中提取内联样式。

提取以及数组中的排序部分已完成。因此,我有一堆或数组,其结构如下:

array(
    'tag'  =>'div',
    'id'   =>'myDiv',
    'class'=>array('container','wrapper'),
    'style'=>array('background'=>'#CCCCCC',/*rules...*/)
);

请注意,不一定在所有元素上设置“id”和“class”键,但始终设置两者中的至少一个。

将此数组呈现为 css 文件的部分也已完成。

我需要做的是围绕数组的“样式”部分重新洗牌,以便将常见元素分组在一起。优化不是问题,因为该进程只会被调用一次然后被缓存。

但我尝试设计一些伪代码,但我被困住了。我猜测这个过程的某些部分至少必须做出“尽可能聪明的猜测”,但即使如此我也看不清楚。

最后的办法是创建完整的字符串,然后通过 css-minifier 运行字符串本身,但据我所知,没有人这样做“减少到最低公分母”的事情。

有什么建议吗?

[编辑]

作为对评论的答复,这里有更多说明:

如果我的数组是这样呈现的:

array(
    array(
        'tag'  =>'div',
        'id'   =>'myDiv',
        'style'=>array('background'=>'#CCCCCC','font-size'=>'24px','color'=>'#FF0000')
    ),
    array(
        'tag'  =>'div',
        'class'=>array('container','hasBorder'),
        'style'=>array('background'=>'#CCCCCC','border'=>'1px solid black')
    ),
    array(
        'tag'  =>'div',
        'class'=>array('wrapper','hasBorder'),
        'style'=>array('color'=>'#FF0000','border'=>'1px solid black')
    )
);

我的 CSS 应该这样呈现:

#myDiv{font-size:24px;}
#myDiv, .container{background:#CCCCCC;}
#myDiv, .wrapper{color:#FF0000;}
.hasBorder{border:1px solid black}

输出可能会有所不同;我不关心定义如何设置规则的确切逻辑是什么;但我想避免的是我目前所拥有的:

#myDiv{background:#CCCCCC;font-size:24px;color:#FF0000;}
.container{background:#CCCCCC;border:1px solid black;}
.wrapper{color:#FF0000';border:1px solid black;}

这是一个简单的例子,但我有很多重复的规则,这些规则毫无意义;生成的 CSS 不仅臃肿,而且不易于维护。我想要的是生成一个设计人员可以使用的基本 CSS。

I have a templating engine in PHP wich allows you to set style attributes to objects prior to rendering.

During development time, I want these attributes to be inline so I can find them and change them easily. However, for production, I want to extract them and cache them in a css file. I also want to extract inline styles from any html element in the template.

The extracting, as well as ordering in arrays part is done. So I have a bunch or arrays that are structured as follow

array(
    'tag'  =>'div',
    'id'   =>'myDiv',
    'class'=>array('container','wrapper'),
    'style'=>array('background'=>'#CCCCCC',/*rules...*/)
);

Note that 'id' and 'class' keys are not necessarily set on all elements, but at least one of both is always set.

The part that renders this array as a css file is also done.

What I need to do is re-shuffle around the 'style' part of the array so common elements are grouped together. Optimization is not an issue since this process will be called only once then cached.

But I tried to design some pseudo-code and I'm just stuck. I am guessing that some part of the process at least will have to make "as-smart-as-possible guesses", but even that I don't see clearly.

Last recourse would be to just create the full string, then run the string itself through a css-minifier, but as far as what I could read, none does this "reducing to the lowest common denominator" thing.

Any suggestion?

[edit]

In answer to comments, here is more clarification:

if my array is presented like so:

array(
    array(
        'tag'  =>'div',
        'id'   =>'myDiv',
        'style'=>array('background'=>'#CCCCCC','font-size'=>'24px','color'=>'#FF0000')
    ),
    array(
        'tag'  =>'div',
        'class'=>array('container','hasBorder'),
        'style'=>array('background'=>'#CCCCCC','border'=>'1px solid black')
    ),
    array(
        'tag'  =>'div',
        'class'=>array('wrapper','hasBorder'),
        'style'=>array('color'=>'#FF0000','border'=>'1px solid black')
    )
);

My CSS should be rendered as such:

#myDiv{font-size:24px;}
#myDiv, .container{background:#CCCCCC;}
#myDiv, .wrapper{color:#FF0000;}
.hasBorder{border:1px solid black}

The output could be different; I don't care what the exact logic for defining how rules are set is; But what I want to avoid is what I currently have:

#myDiv{background:#CCCCCC;font-size:24px;color:#FF0000;}
.container{background:#CCCCCC;border:1px solid black;}
.wrapper{color:#FF0000';border:1px solid black;}

This is a simple example, but I have lots of repeated rules all over that make no sense; the generated CSS is not only bloated, it is not humanely maintainable. What I want is to generate a base CSS that designers can use to work with.

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

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

发布评论

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

评论(1

旧竹 2024-12-17 13:05:17

css lint http://csslint.net/
小心,她会伤害你的感情

css lint http://csslint.net/
watch out, she'll hurt your feelings

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