Question

I want to remove css rules which are giving error like here is the example

.banneritem {
   border: 1px solid #CED4E0;
   border color: #CBCBCB;// is not valid cause it actually refers to border-color missing (-)
   margin-bottom: 10px;
}

is there any framework or library which will omit this kind of css rules from file.

I am using sabberworm css parser here my sample code

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

I am here working all css rules and finds display:none rule in meanwhile due to this faulty rule I am getting fatal error.

Any help will be highly appreciate.

Was it helpful?

Solution

Yes I found the solution,

First of all I put the same code as was in my above question and then I created new dummy css and created new function see below,

For that I use two classes name is 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;
}

Hope this helps :)

OTHER TIPS

It is my suggestion to use a more robust parser based on LESS, SASS, or SCSS. If you use Compass, a CSS pre-processing framework, it will help you to write better structured code and also validate your CSS. There are a few compile time commands which you may find useful:

-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.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top