我猜你会写你自己的CSS解析器和过滤器,所以这里我会考虑,虽然我从来没有做过这样的事情:
-
color
,
font-family
.
-
background
background-color
,
background-image
.
-
-
在解析过程中要非常严格,丢弃解析器不理解的所有内容,即使它是有效的CSS。换句话说,创建自己的CSS子集。
解析时,最困难的部分是对
complex CSS selectors
. 但是你也可以在这里强加你自己的子集。
下面是一些(伪)代码,也许它会对您有所帮助:
<?php
function tokenizeCSS() {
return array(
array(
'selector' => '#foo .bar',
'properties' => array(
'background-color' => 'transparent',
'color' => '#fff',
),
);
);
}
function colorValidator($color)
{}
/**
* This is basically the white list. Keys are accepted CSS properties
* and values are the validator callbacks.
*/
$propertyValidators = array(
'background-color' => 'colorValidator',
'color' => 'colorValidator',
);
$filteredRules = array();
foreach (tokenizeCSS() as $rule) {
if (! validSelector($rule['selector'])) {
continue;
}
foreach ($rule['properties'] as $property => $value) {
/**
* Check property is in white list
*/
if (! isset($propertyValidators[$property]) {
continue;
}
/**
* Check property is valid
*/
if (! $propertyValidators[$property]($value)) {
continue;
}
/**
* Valid rule
*/
$filteredRules[$rule['selector']][$property] = $value;
}
}