-1

我想删除给出错误的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 规则,由于这个错误的规则,我遇到了致命错误。

任何帮助将不胜感激。

4

2 回答 2

0

是的,我找到了解决方案,

首先,我输入了与上述问题相同的代码,然后我创建了新的虚拟 css 并创建了新函数,如下所示,

为此,我使用两个类名称是 1)。2). 风格牛

  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;
}

希望这可以帮助 :)

于 2012-12-01T09:42:57.013 回答
-1

我建议使用基于 LESS、SASS 或 SCSS 的更强大的解析器。如果您使用Compass,一个 CSS 预处理框架,它将帮助您编写更好的结构化代码并验证您的 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.
于 2012-10-05T11:31:32.033 回答