The W3C CSS validator is an online service for checking a stylesheet for standards compliance. This service can be accessed in Perl via the WebService::Validator::CSS::W3C module, which is handy for automating validation. However, for checking a large number of stylesheets, it is better to run a local install of the validator so as not to abuse the public service. A local install also has a higher availability than an online service. Unfortunately, the perl module does not support a local install out of the box.
To deal with this, I did the following:
- Download and install the CSS validator locally. Note that the raw installation directory is quite large. However, only the css-validator.jar file, and the lib and org directories produced by the installation are needed to run it, which makes it much smaller.
- Install the WebService::Validator::CSS::W3C module.
- Override the WebService::Validator::CSS::W3C::validate() method provided by the module using the code below. This uses the local installation instead of the remote service for running the validation.
# Override default $v->validate() method to use local installation: sub WebService::Validator::CSS::W3C::validate { my $self = shift; my %opt = ( @_ ); my $file = "/tmp/check.$$.css"; my $validator = "/usr/local/bin/css-validator"; open ( my $CSS, ">$file" ); print $CSS $opt{string}; close ( $CSS ); chdir ( $validator ); my $data = `java -jar css-validator.jar --output=soap12 --profile=$opt{profile} file:$file`; unlink ( $file ); $data =~ s/^.+\n//m; my $u = URI->new ( "data:" ); $u->data ( $data ); my $req = HTTP::Request->new ( GET => $u ); # Copied from original: $self->{'success'} = 0; my $res = $ua->simple_request($req); $self->{'response'} = $res; return $self->_handle_response($res); }
Then the module may be used more or less as usual:
use WebService::Validator::CSS::W3C; my $v = WebService::Validator::CSS::W3C->new(); $v->validate ( string => $css, profile => "css3" );