Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I validate CSS within a script? [closed]

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);
   }
like image 253
JAL Avatar asked Jul 25 '10 18:07

JAL


People also ask

Is there a CSS validator?

The W3C CSS Validation Service can be used to check the correctness (validity) of W3. CSS. The Validation Service was created by the W3C to help Web developers validate CSS.

Can CSS style sheets be validated?

A CSS validator checks your Cascading Style Sheets to make sure that they comply with the CSS standards set by the W3 Consortium. There are a few validators which will also tell you which CSS features are supported by which browsers (since not all browsers are equal in their CSS implementation).

How do I validate HTML and CSS code?

You can validate both HTML and CSS files. You can use any commercial software package or free online application, such as the following World Wide Web Consortium (W3C) validators: W3C CSS Validator at http://jigsaw.w3.org/css-validator/ W3C HTML Validator at http://validator.w3.org/


2 Answers

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.

like image 152
quantumSoup Avatar answered Sep 17 '22 03:09

quantumSoup


Python library:

http://cthedot.de/cssutils/

like image 40
gruszczy Avatar answered Sep 21 '22 03:09

gruszczy