1HTML::Lint(3) User Contributed Perl Documentation HTML::Lint(3)
2
3
4
6 HTML::Lint - check for HTML errors in a string or file
7
9 Version 2.32
10
12 my $lint = HTML::Lint->new;
13 $lint->only_types( HTML::Lint::Error::STRUCTURE );
14
15 # Parse lines of data.
16 $lint->newfile( $filename );
17 while ( my $line = <> ) {
18 $lint->parse( $line );
19 }
20 $lint->eof();
21
22 # Or, parse an entire file at once.
23 $lint->parse_file( $filename );
24
25 # Fetch the errors that the linter found.
26 my $error_count = $lint->errors;
27
28 foreach my $error ( $lint->errors ) {
29 print $error->as_string, "\n";
30 }
31
32 HTML::Lint also comes with a wrapper program called weblint that
33 handles linting from the command line:
34
35 $ weblint http://www.cnn.com/
36 http://www.cnn.com/ (395:83) <IMG SRC="spacer.gif"> tag has no HEIGHT and WIDTH attributes.
37 http://www.cnn.com/ (395:83) <IMG SRC="goofus.gif"> does not have ALT text defined
38 http://www.cnn.com/ (396:217) Unknown element <nobr>
39 http://www.cnn.com/ (396:241) </nobr> with no opening <nobr>
40 http://www.cnn.com/ (842:7) target attribute in <a> is repeated
41
42 And finally, you can also get Apache::HTML::Lint that passes any
43 mod_perl-generated code through HTML::Lint and get it dumped into your
44 Apache error_log.
45
46 [Mon Jun 3 14:03:31 2002] [warn] /foo.pl (1:45) </p> with no opening <p>
47 [Mon Jun 3 14:03:31 2002] [warn] /foo.pl (1:49) Unknown element <gronk>
48 [Mon Jun 3 14:03:31 2002] [warn] /foo.pl (1:56) Unknown attribute "x" for tag <table>
49
51 NOTE: Some of these methods mirror HTML::Parser's methods, but
52 HTML::Lint is not a subclass of HTML::Parser.
53
54 new()
55 Create an HTML::Lint object, which inherits from HTML::Parser. You may
56 pass the types of errors you want to check for in the "only_types"
57 parm.
58
59 my $lint = HTML::Lint->new( only_types => HTML::Lint::Error::STRUCTURE );
60
61 If you want more than one, you must pass an arrayref:
62
63 my $lint = HTML::Lint->new(
64 only_types => [HTML::Lint::Error::STRUCTURE, HTML::Lint::Error::FLUFF] );
65
66 $lint->parser()
67 Returns the parser object for this object, creating one if necessary.
68
69 $lint->parse( $text )
70 $lint->parse( $code_ref )
71 Passes in a chunk of HTML to be linted, either as a piece of text, or a
72 code reference. See HTML::Parser's "parse_file" method for details.
73
74 $lint->parse_file( $file )
75 Analyzes HTML directly from a file. The $file argument can be a
76 filename, an open file handle, or a reference to an open file handle.
77 See HTML::Parser's "parse_file" method for details.
78
79 $lint->eof()
80 Signals the end of a block of text getting passed in. This must be
81 called to make sure that all parsing is complete before looking at
82 errors.
83
84 Any parameters (and there shouldn't be any) are passed through to
85 HTML::Parser's eof() method.
86
87 $lint->errors()
88 In list context, "errors" returns all of the errors found in the parsed
89 text. Each error is an object of the type HTML::Lint::Error.
90
91 In scalar context, it returns the number of errors found.
92
93 $lint->clear_errors()
94 Clears the list of errors, in case you want to print and clear, print
95 and clear.
96
97 $lint->only_types( $type1[, $type2...] )
98 Specifies to only want errors of a certain type.
99
100 $lint->only_types( HTML::Lint::Error::STRUCTURE );
101
102 Calling this without parameters makes the object return all possible
103 errors.
104
105 The error types are "STRUCTURE", "HELPER" and "FLUFF". See
106 HTML::Lint::Error for details on these types.
107
108 $lint->gripe( $errcode, [$key1=>$val1, ...] )
109 Adds an error message, in the form of an HTML::Lint::Error object, to
110 the list of error messages for the current object. The file, line and
111 column are automatically passed to the HTML::Lint::Error constructor,
112 as well as whatever other key value pairs are passed.
113
114 For example:
115
116 $lint->gripe( 'attr-repeated', tag => $tag, attr => $attr );
117
118 Usually, the user of the object won't call this directly, but just in
119 case, here you go.
120
121 $lint->newfile( $filename )
122 Call newfile() whenever you switch to another file in a batch of
123 linting. Otherwise, the object thinks everything is from the same
124 file. Note that the list of errors is NOT cleared.
125
126 Note that $filename does NOT need to match what's put into parse() or
127 parse_file(). It can be a description, a URL, or whatever.
128
129 You should call newfile() even if you are only validating one file. If
130 you do not call newfile() then your errors will not have a filename
131 attached to them.
132
134 Sometimes you'll have HTML that for some reason cannot conform to
135 HTML::Lint's expectations. For those instances, you can use HTML
136 comments to modify HTML::Lint's behavior.
137
138 Say you have an image where for whatever reason you can't get
139 dimensions for the image. This HTML snippet:
140
141 <img src="logo.png" height="120" width="50" alt="Company logo">
142 <img src="that.png">
143
144 causes this error:
145
146 foo.html (14:20) <img src="that.png"> tag has no HEIGHT and WIDTH attributes
147
148 But if for some reason you can't get those dimensions when you build
149 the page, you can at least stop HTML::Lint complaining about it.
150
151 <img src="this.png" height="120" width="50" alt="Company logo">
152 <!-- html-lint elem-img-sizes-missing: off, elem-img-alt-missing: off -->
153 <img src="that.png">
154 <!-- html-lint elem-img-sizes-missing: on, elem-img-alt-missing: off -->
155
156 If you want to turn off all HTML::Lint warnings for a block of code,
157 use
158
159 <!-- html-lint all: off -->
160
161 And turn them back on with
162
163 <!-- html-lint all: on -->
164
165 You don't have to use "on" and "off". For "on", you can use "true" or
166 "1". For "off", you can use "0" or "false".
167
168 For a list of possible errors and their codes, see HTML::Lint::Error,
169 or run perldoc HTML::Lint::Error.
170
172 All bugs and requests are now being handled through GitHub.
173
174 https://github.com/petdance/html-lint/issues
175
176 DO NOT send bug reports to http://rt.cpan.org/ or
177 http://code.google.com/
178
180 • Check for attributes that require values
181
182 • <TABLE>s that have no rows.
183
184 • Form fields that aren't in a FORM
185
186 • DIVs with nothing in them.
187
188 • HEIGHT= that have percents in them.
189
190 • Check for goofy stuff like:
191
192 <b><li></b><b>Hello Reader - Spanish Level 1 (K-3)</b>
193
195 Copyright 2005-2018 Andy Lester.
196
197 This program is free software; you can redistribute it and/or modify it
198 under the terms of the Artistic License v2.0.
199
200 http://www.opensource.org/licenses/Artistic-2.0
201
202 Please note that these modules are not products of or supported by the
203 employers of the various contributors to the code.
204
206 Andy Lester, andy at petdance.com
207
208
209
210perl v5.36.0 2023-01-20 HTML::Lint(3)