我想删除提供错误的css规则,就像这里的示例
一样.banneritem {
border: 1px solid #CED4E0;
border color: #CBCBCB;// is not valid cause it actually refers to border-color missing (-)
margin-bottom: 10px;
}
是否有任何框架或库可以从文件中省略这种css规则。
我在这里使用sabberworm css解析器我的示例代码
if ($loadedContents != ""){
preg_match_all('/display: none/is', $loadedContents, $matchvalue);
if (count($matchvalue[0]) > 0) {
$oCssParser = new Sabberworm\CSS\Parser($loadedContents);
$oDoc = $oCssParser -> parse();
foreach ($oDoc->getAllRuleSets() as $oRuleSet) {
if ($oRuleSet instanceof Sabberworm\CSS\RuleSet\AtRule) {
break;
}
$sSelector = $oRuleSet -> getSelectors();
$sSelector = $sSelector[0] -> getSelector();
$aDisplayRule = $oRuleSet -> getRules('display');
if (count($aDisplayRule) > 0) {
$aValues = $aDisplayRule['display'] -> getValues();
if ($aValues[0][0] == "none") {
$displayValue[] = "display:none;";
$displaySelector[] = $sSelector;
}
}
$bDisplayRule = $oRuleSet -> getRules('visibility');
if (count($bDisplayRule) > 0) {
$bValues = $bDisplayRule['visibility'] -> getValues();
if ($bValues[0][0] == "hidden") {
$visibilityValue[] = "visibility:hidden;";
$visibilitySelector[] = $sSelector;
}
}
}
}
}
我在这里工作所有的css规则并且发现display:none规则同时由于这个错误的规则我得到了致命的错误。
任何帮助都将非常感激。
答案 0 :(得分:0)
是的,我找到了解决方案,
首先,我将相同的代码放在上面的问题中,然后我创建了新的虚拟css并创建了新函数,见下文,
为此,我使用两个类名称 1)。 csstidy 2)。 Stylecow
require "../stylecow/autoload.php";
//for css rules omition
require('../csstidy/class.csstidy.php');
function makeDummyCssFile($html)
{
$cssTidy = new csstidy();
$newCss ="";
foreach ($html->find('link') as $e) {
$cssHrefs = $e -> href;
preg_match_all('~favicon~is', $cssHrefs, $feviMatch);
if (count($feviMatch[0]) == 0) {
preg_match_all('~(\bhttp|https\b)~is', $cssHrefs , $isThirdPartyCss);
if(count($isThirdPartyCss[0]) > 0)
{
$loadedHrefs = $cssHrefs;
}
else
{
preg_match_all('~' . SITE_NAME . '~is', $cssHrefs, $match);
if (count($match[0]) == 0) {
$loadedHrefs = SITE_NAME . $cssHrefs;
} else {
$loadedHrefs = $cssHrefs;
}
}
$loadedContents = file_get_contents($loadedHrefs);
$css = Stylecow\Parser::parseFile($loadedContents);
$newCss.= $css;
}
}
$result = $cssTidy->parse($newCss);
$newCss = $cssTidy->print->plain();
if($result)
{
foreach($cssTidy->log as $line => $array)
{
// echo "<pre>";print_r($array);
$array_size = count($array);
for($i = 0; $i < $array_size; ++$i)
{
if($array[$i]['t'] == "error")
{
$newCss = preg_replace("~(".$array[$i]['m']['selector']."[^\}]*\})~is", "", $newCss);#qtip-overlay div[^\}]*\}
}
}
}
}
$cssName = explode("http://", SITE_NAME);
$cssFileName = str_replace(".", "-", str_replace("/","",$cssName[1])) . "-" . date('Y-m-d') . ".css";
$str = "";
$myFile = DUMYPATH . $cssFileName;
if ($myFile != "") {
$fh = fopen($myFile, 'w+') or die("can't open file");
$stringData = $newCss;
fwrite($fh, $stringData);
fclose($fh);
}
return $cssFileName;
}
希望这会有所帮助:)
答案 1 :(得分:-1)
我的建议是使用基于LESS,SASS或SCSS的更强大的解析器。如果您使用CSS预处理框架Compass,它将帮助您编写更好的结构化代码并验证您的CSS。您可能会发现一些编译时命令很有用:
-q, --quiet Quiet mode.
--trace Show a full stacktrace on error
--force Allows compass to overwrite existing files.
--dry-run Dry Run. Tells you what it plans to do.
--boring Turn off colorized output.