WebSer...L::W3C(3pm)
๐ WebService::Validator::HTML::W3C โ the Perl module that talks to the W3C's online HTML validator ๐
okay bestie, quick vibe check: the manual name for this one is WebSer...L::W3C(3pm) โ yes that's the truncated header, it's giving cryptic but it's just WebService::Validator::HTML::W3C living its best life in section 3pm. Let's get into it. ๐ฆ
โจ SYNOPSIS โจ
use WebService::Validator::HTML::W3C;
my $v = WebService::Validator::HTML::W3C->new(
detailed => 1
);
if ( $v->validate("http://www.example.com/") ) {
if ( $v->is_valid ) {
printf ("%s is valid\n", $v->uri);
} else {
printf ("%s is not valid\n", $v->uri);
foreach my $error ( @{$v->errors} ) {
printf("%s at line %d\n", $error->msg,
$error->line);
}
}
} else {
printf ("Failed to validate the website: %s\n", $v->validator_error);
}
๐ธ DESCRIPTION ๐ธ
So WebService::Validator::HTML::W3C gives you access to the W3C's online Markup validator. ๐
And it's not just a yes/no girlie โ besides telling you whether a page is valid, it also hands you a detailed list of the errors and where in the validated document they occur. Iconic, honestly.
๐ METHODS ๐
๐ new ๐
my $v = WebService::Validator::HTML::W3C->new();
Returns a brand new instance of the WebService::Validator::HTML::W3C object. Fresh out the box, no cap. โจ
There are various options you can set when creating the Validator object, like so:
my $v = WebService::Validator::HTML::W3C->new( http_timeout => 20 );
Here's the full lineup, bestie:
validator_uriโ The URI of the validator you wanna use. By default this hits up the W3C's validator athttp://validator.w3.org/check. If you've got a local installation of the validator (recommended if you wish to do a lot of testing) or you wanna use a validator at another location, this option is your girl. โ ๏ธ Please note that you need to use the full path to the validator cgi.uaโ The user agent to use. Should be anLWP::UserAgentobject or something that provides the same interface. Heads up: if this argument is provided, the"http_timeout"and"proxy"arguments are ignored. ๐ซถhttp_timeoutโ How long (in seconds) to wait for the HTTP connection to timeout when contacting the validator. By default this is 30 seconds.detailedโ This fetches the XML response from the validator in order to provide information for theerrorsmethod. You should set this to true if you intend to use theerrorsmethod. ๐proxyโ An HTTP proxy to use when communicating with the validation service.outputโ Controls which output format is used. Can be eitherxmlorsoap12.
The default is soap12 because the XML format is deprecated and is likely to be removed in the future. ๐
The default will always work, so unless you're using a locally installed Validator you can safely ignore this.
๐ฆ validate ๐ฆ
$v->validate( 'http:://www.example.com/' );
Validate a URI. Returns 0 if the validation fails (e.g. if the validator cannot be reached), otherwise 1. Simple as that. โจ
๐ validate_file ๐
$v->validate_file( './file.html' );
Validate a file by uploading it to the W3C Validator. NB: This has only been tested on a Linux box so it may not work on non unix machines. ๐ง
๐ง validate_markup ๐ง
$v->validate_markup( $markup );
Validate a scalar containing HTML. Yes, a whole scalar. She contains multitudes. ๐
๐ซ Alternate interface ๐ซ
You can also pass a hash in to specify what you wish to validate. This is provided to ensure compatibility with the CSS validator module.
$v->validate( uri => 'http://example.com/' );
$v->validate( string => $markup );
$v->validate( file => './file.html' );
โญ is_valid โญ
$v->is_valid;
Returns true (1) if the URI validated, otherwise 0.
๐ uri ๐
$v->uri();
Returns the URI of the last page on which validation succeeded.
๐ฅ num_errors ๐ฅ
$num_errors = $v->num_errors();
Returns the number of errors that the validator encountered. ๐ญ
๐ธ errorcount ๐ธ
Synonym for num_errors. It's there to match the CSS Validator interface. Team player behaviour fr fr.
๐ warningcount ๐
$num_errors = $v->warningcount();
Returns the number of warnings that the validator encountered.
๐ฆ errors ๐ฆ
$errors = $v->errors();
foreach my $err ( @$errors ) {
printf("line: %s, col: %s\n\terror: %s\n",
$err->line, $err->col, $err->msg);
}
Returns an array ref of WebService::Validator::HTML::W3C::Error objects. These have line, col and msg methods that return a line number, a column in that line, and the error that occurred at that point.
Note that you needXML::XPathfor this to work and you must have initialisedWebService::Validator::HTML::W3Cwith thedetailedoption. If you have not set thedetailedoption a warning will be issued, thedetailedoption will be set and a second request made to the validator in order to fetch the required information. ๐ญ
If there was a problem processing the detailed information then this method will return 0.
๐ซถ warnings ๐ซถ
$warnings = $v->warnings();
Works exactly the same as errors, only it returns an array ref of WebService::Validator::HTML::W3C::Warning objects. In all other respects it's the same. Twin energy. ๐ฏ
๐ validator_error ๐
$error = $v->validator_error();
Returns a string indicating why validation may not have occurred. This is not the reason that a webpage was invalid. It is the reason that no meaningful information about the attempted validation could be obtained. This is most likely to be an HTTP error.
Possible values are:
You need to supply a URI to validateโ You didn't pass a URI to thevalidatemethod. ๐You need to supply a URI with a schemeโ The URI you passed tovalidatedidn't have a scheme on the front. The W3C validator can't handle URIs likewww.example.combut instead needs URIs of the formhttp://www.example.com/.Not a W3C Validator or Bad URIโ The URI did not return the headers thatWebService::Validator::HTML::W3Crelies on, so it is likely that there is not a W3C Validator at that URI. The other possibility is that it didn't like the URI you provided. Sadly the Validator doesn't give very useful feedback on this at the moment. ๐Could not contact validatorโWebService::Validator::HTML::W3Ccould not establish a connection to the URI.Did not get a sensible result from the validatorโ Should never happen and most likely indicates a problem somewhere, but on the off chance thatWebService::Validator::HTML::W3Cis unable to make sense of the response from the validator you'll get this error.Result format does not appear to be SOAP|XMLโ If you've asked for detailed results and the reponse from the validator isn't in the expected format then you'll get this error. Most likely to happen if you ask for SOAP output from a validator that doesn't support that format.You need to provide a uri, string or file to validateโ You've passed in a hash (or in fact more than one argument) tovalidatebut the hash does not contain one of the three expected keys.
๐ validator_uri ๐
$uri = $v->validator_uri();
$v->validator_uri('http://validator.w3.org/check');
Returns or sets the URI of the validator to use. โ ๏ธ Please note that you need to use the full path to the validator cgi.
๐ง http_timeout ๐ง
$timeout = $v->http_timeout();
$v->http_timeout(10);
Returns or sets the timeout for the HTTP request.
๐ OTHER MODULES ๐
Please note that there is also an official W3C module that is part of the W3C::LogValidator distribution. However, that module is not very useful outside the constraints of that package. WebService::Validator::HTML::W3C is meant as a more general way to access the W3C Validator. ๐
HTML::Validator uses nsgmls to validate against the W3Cs DTDs. You have to fetch the relevant DTDs and so on. ๐ฎ๐จ
There is also the HTML::Parser based HTML::Lint which mostly checks for known tags rather than XML/HTML validity.
WebService::Validator::CSS::W3C provides the same functionality as this module for the W3C's CSS validator. Sibling behaviour. ๐ซถ
๐ฅ IMPORTANT ๐ฅ
This module is not in any way associated with the W3C so please do not report any problems with this module to them. Also please remember that the online Validator is a shared resource so do not abuse it. This means sleeping between requests. If you want to do a lot of testing against it then please consider downloading and installing the Validator software which is available from the W3C. Debian testing users will also find that it is available via apt-get.
๐ญ BUGS ๐ญ
While the interface to the Validator is fairly stable it may be updated. I will endeavour to track any changes with this module so please check on CPAN for new versions if you find things break. Also note that this module is only guaranteed to work with the currently stable version of the validator. It will most likely work with any Beta versions but don't rely on it.
If in doubt please try and run the test suite before reporting bugs. Note that in order to run tests against the validator service you will need to have a connection to the internet and also set an environment variable called TEST_AUTHOR.
That said I'm very happy to hear about bugs. All the more so if they come with patches ;).
Please use <http://rt.cpan.org/> for filing bug reports, and indeed feature requests. The code can also be found on github <https://github.com/struan/webservice-validator-html-w3c>.
๐ซถ THANKS ๐ซถ
To the various people on the code review ladder mailing list who provided useful suggestions.
Carl Vincent provided a patch to allow for proxy support.
Chris Dolan provided a patch to allow for custom user agents.
Matt Ryder provided a patch for support of the explanations in the SOAP output.
๐ธ SUPPORT ๐ธ
author email or via <http://rt.cpan.org/>.
๐ AUTHOR ๐
Struan Donald <struan@cpan.org>
โญ COPYRIGHT โญ
Copyright (C) 2003-2008 Struan Donald. All rights reserved.
๐ LICENSE ๐
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
๐ฆ SEE ALSO ๐ฆ
okay that's the whole page, bestie, you ate that ๐
โจ and for the sign-off โ this little icon was built from perl v5.34.0 and dated 2022-06-28, and it lives under the manpage name WebSer...L::W3C(3pm). Slay, go validate something. ๐๐
๐ฆ where this page comes from
- debian bookworm: libwebservice-validator-html-w3c-perl 0.28-3 ยท Artistic-1.0-Perl OR GPL-1.0-or-later
- debian trixie: libwebservice-validator-html-w3c-perl 0.28-3 ยท Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: libwebservice-validator-html-w3c-perl 0.28-3 ยท Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libwebservice-validator-html-w3c-perl 0.28-3 ยท Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libwebservice-validator-html-w3c-perl 0.28-3 ยท Artistic-1.0-Perl OR GPL-1.0-or-later
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms