pythonphpcssvalidation

How can I validate CSS within a script?


Is there a library out there which will validate CSS?

The only tools I can find to do so are web sites. If one of these sites has an API, that would fit the bill, too.

I have a script that serves as a CSS compiler. It sets various variables according to settings for a theme, and generates and writes a CSS file. Before committing to writing the CSS file, I'd like to validate it to make sure there aren't any invalid entries.

PHP would be convenient, but Python, Perl, Ruby, Java, or anything executable from a shell would be fine.

Ideally, there's something out there that I can use as part of a sequence like:

 $css_file=theme_compile('theme-name');
 if(!validate_css($css_file)){
   echo "css file contained invalid entry 'width:px'";//just an example, of course
   }   
 else{
   file_put_contents('/path/css_file',$css_file);
   }

Solution

  • W3C has an API:

    http://jigsaw.w3.org/css-validator/api.html

    You can also download the validator and run it locally: http://jigsaw.w3.org/css-validator/DOWNLOAD.html

    You need to be able to run java from your script.