my $lint = HTML::Lint->new; $lint->only_types( HTML::Lint::Error::STRUCTURE ); # Parse lines of data. $lint->newfile( $filename ); while ( my $line = <> ) { $lint->parse( $line ); } $lint->eof(); # Or, parse an entire file at once. $lint->parse_file( $filename ); # Fetch the errors that the linter found. my $error_count = $lint->errors; foreach my $error ( $lint->errors ) { print $error->as_string, "\n"; }
HTML::Lint also comes with a wrapper program called weblint that handles linting from the command line:
$ weblint http://www.cnn.com/ http://www.cnn.com/ (395:83) <IMG SRC="spacer.gif"> tag has no HEIGHT and WIDTH attributes. http://www.cnn.com/ (395:83) <IMG SRC="goofus.gif"> does not have ALT text defined http://www.cnn.com/ (396:217) Unknown element <nobr> http://www.cnn.com/ (396:241) </nobr> with no opening <nobr> http://www.cnn.com/ (842:7) target attribute in <a> is repeated
And finally, you can also get Apache::HTML::Lint that passes any mod_perl-generated code through HTML::Lint and get it dumped into your Apache error_log.
[Mon Jun 3 14:03:31 2002] [warn] /foo.pl (1:45) </p> with no opening <p> [Mon Jun 3 14:03:31 2002] [warn] /foo.pl (1:49) Unknown element <gronk> [Mon Jun 3 14:03:31 2002] [warn] /foo.pl (1:56) Unknown attribute "x" for tag <table>
my $lint = HTML::Lint->new( only_types => HTML::Lint::Error::STRUCTURE );
If you want more than one, you must pass an arrayref:
my $lint = HTML::Lint->new( only_types => [HTML::Lint::Error::STRUCTURE, HTML::Lint::Error::FLUFF] );
Any parameters (and there shouldn't be any) are passed through to HTML::Parser's eof() method.
In scalar context, it returns the number of errors found.
$lint->only_types( HTML::Lint::Error::STRUCTURE );
Calling this without parameters makes the object return all possible errors.
The error types are "STRUCTURE", "HELPER" and "FLUFF". See HTML::Lint::Error for details on these types.
For example:
$lint->gripe( 'attr-repeated', tag => $tag, attr => $attr );
Usually, the user of the object won't call this directly, but just in case, here you go.
Note that $filename does NOT need to match what's put into "parse()" or "parse_file()". It can be a description, a URL, or whatever.
You should call "newfile()" even if you are only validating one file. If you do not call "newfile()" then your errors will not have a filename attached to them.
Say you have an image where for whatever reason you can't get dimensions for the image. This HTML snippet:
<img src="logo.png" height="120" width="50" alt="Company logo"> <img src="that.png">
causes this error:
foo.html (14:20) <img src="that.png"> tag has no HEIGHT and WIDTH attributes
But if for some reason you can't get those dimensions when you build the page, you can at least stop HTML::Lint complaining about it.
<img src="this.png" height="120" width="50" alt="Company logo"> <!-- html-lint elem-img-sizes-missing: off, elem-img-alt-missing: off --> <img src="that.png"> <!-- html-lint elem-img-sizes-missing: on, elem-img-alt-missing: off -->
If you want to turn off all HTML::Lint warnings for a block of code, use
<!-- html-lint all: off -->
And turn them back on with
<!-- html-lint all: on -->
You don't have to use ``on'' and ``off''. For ``on'', you can use ``true'' or ``1''. For ``off'', you can use ``0'' or ``false''.
For a list of possible errors and their codes, see HTML::Lint::Error, or run perldoc HTML::Lint::Error.
https://github.com/petdance/html-lint/issues
DO NOT send bug reports to http://rt.cpan.org/ or http://code.google.com/
<b><li></b><b>Hello Reader - Spanish Level 1 (K-3)</b>
This program is free software; you can redistribute it and/or modify it under the terms of the Artistic License v2.0.
http://www.opensource.org/licenses/Artistic-2.0
Please note that these modules are not products of or supported by the employers of the various contributors to the code.