HTML/Lint.pm

filter: Statementstmt 100% 84 / 84 Branchbran 93.7% 15 / 16 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 100% 16 / 16 Podpod 100% 10 / 10 totaltotal 99.2% 130 / 131 SCAR 24.0
CC
11
Cov
99%
new
4.0
errors
4.0
gripe
3.0
CRAP 11.0
« Test/HTML/Lint.pm ↑ Summary HTML/Lint/HTML4.pm »
1 package HTML::Lint;
2  
3 40 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 40 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 40 ▶use HTML::Lint::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 40 ▶use HTML::Lint::Parser ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 40 ▶use HTML::Entities ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 =head1 NAME
12  
13 HTML::Lint - check for HTML errors in a string or file
14  
15 =head1 VERSION
16  
17 Version 2.30
18  
19 =cut
20  
21 40 our $VERSION = '2.30';
22  
23 =head1 SYNOPSIS
24  
25 my $lint = HTML::Lint->new;
26 $lint->only_types( HTML::Lint::Error::STRUCTURE );
27  
28 # Parse lines of data.
29 $lint->newfile( $filename );
30 while ( my $line = <> ) {
31 $lint->parse( $line );
32 }
33 $lint->eof();
34  
35 # Or, parse an entire file at once.
36 $lint->parse_file( $filename );
37  
38 # Fetch the errors that the linter found.
39 my $error_count = $lint->errors;
40  
41 foreach my $error ( $lint->errors ) {
42 print $error->as_string, "\n";
43 }
44  
45 HTML::Lint also comes with a wrapper program called F<weblint> that handles
46 linting from the command line:
47  
48 $ weblint http://www.cnn.com/
49 http://www.cnn.com/ (395:83) <IMG SRC="spacer.gif"> tag has no HEIGHT and WIDTH attributes.
50 http://www.cnn.com/ (395:83) <IMG SRC="goofus.gif"> does not have ALT text defined
51 http://www.cnn.com/ (396:217) Unknown element <nobr>
52 http://www.cnn.com/ (396:241) </nobr> with no opening <nobr>
53 http://www.cnn.com/ (842:7) target attribute in <a> is repeated
54  
55 And finally, you can also get L<Apache::HTML::Lint> that passes any
56 mod_perl-generated code through HTML::Lint and get it dumped into your
57 Apache F<error_log>.
58  
59 [Mon Jun 3 14:03:31 2002] [warn] /foo.pl (1:45) </p> with no opening <p>
60 [Mon Jun 3 14:03:31 2002] [warn] /foo.pl (1:49) Unknown element <gronk>
61 [Mon Jun 3 14:03:31 2002] [warn] /foo.pl (1:56) Unknown attribute "x" for tag <table>
62  
63 =cut
64  
65 =head1 METHODS
66  
67 NOTE: Some of these methods mirror L<HTML::Parser>'s methods, but HTML::Lint
68 is not a subclass of HTML::Parser.
69  
70 =head2 new()
71  
72 Create an HTML::Lint object, which inherits from HTML::Parser.
73 You may pass the types of errors you want to check for in the
74 C<only_types> parm.
75  
76 my $lint = HTML::Lint->new( only_types => HTML::Lint::Error::STRUCTURE );
77  
78 If you want more than one, you must pass an arrayref:
79  
80 my $lint = HTML::Lint->new(
81 only_types => [HTML::Lint::Error::STRUCTURE, HTML::Lint::Error::FLUFF] );
82  
83 =cut
84  
85 sub new {
86 43 ▶ my $class = shift;
Subroutine new: called CC=4 SCAR=13.9
Pod documented
87 43 my %args = @_;
88  
89 43 my $self = {
90 _errors => [],
91 _types => [],
92 };
93 43 bless $self, $class;
94  
95 43 ▶ if ( my $only = $args{only_types} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $only = $args{"only_types"}) 2 41 43
96 2 ▶ $self->only_types( ref $only eq 'ARRAY' ? @{$only} : $only );
Branch: 1 branch
BranchTrueFalseTotal
ref $only eq 'ARRAY' ? : 1 1 2
97 2 delete $args{only_types};
98 }
99  
100 43 warn "Unknown argument $_\n" for keys %args;
101  
102 43 return $self;
103 }
104  
105 =head2 $lint->parser()
106  
107 Returns the parser object for this object, creating one if necessary.
108  
109 =cut
110  
111 sub parser {
112 902 ▶ my $self = shift;
Subroutine parser: called CC=2 SCAR=6.9
Pod documented
113  
114 902 ▶ if ( not $self->{_parser} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"_parser"}) 47 855 902
115 47 ▶ $self->{_parser} = HTML::Lint::Parser->new( sub { $self->gripe( @_ ) } );
Subroutine __ANON__: called CC=1 SCAR=0.0
116 47 $self->{_parser}->ignore_elements( qw(script style) );
117 }
118  
119 902 return $self->{_parser};
120 }
121  
122 =head2 $lint->parse( $text )
123  
124 =head2 $lint->parse( $code_ref )
125  
126 Passes in a chunk of HTML to be linted, either as a piece of text,
127 or a code reference.
128 See L<HTML::Parser>'s C<parse_file> method for details.
129  
130 =cut
131  
132 sub parse {
133 668 ▶ my $self = shift;
Subroutine parse: called CC=1 SCAR=0.0
Pod documented
134  
135 668 my $rc = $self->parser->parse( @_ );
136  
137 668 $self->{_parse_called} = 1;
138  
139 668 return $rc;
140 }
141  
142 =head2 $lint->parse_file( $file )
143  
144 Analyzes HTML directly from a file. The C<$file> argument can be a filename,
145 an open file handle, or a reference to an open file handle.
146 See L<HTML::Parser>'s C<parse_file> method for details.
147  
148 =cut
149  
150 sub parse_file {
151 1 ▶ my $self = shift;
Subroutine parse_file: called CC=1 SCAR=0.0
Pod documented
152  
153 1 my $rc = $self->parser->parse_file( @_ );
154  
155 1 $self->{_parse_called} = 1;
156 1 $self->eof;
157  
158 1 return $rc;
159 }
160  
161 =head2 $lint->eof()
162  
163 Signals the end of a block of text getting passed in. This must be
164 called to make sure that all parsing is complete before looking at errors.
165  
166 Any parameters (and there shouldn't be any) are passed through to
167 HTML::Parser's eof() method.
168  
169 =cut
170  
171 sub eof { ## no critic ( Subroutines::ProhibitBuiltinHomonyms )
172 43 ▶ my $self = shift;
Subroutine eof: called CC=2 SCAR=7.0
Pod documented
173  
174 43 my $rc;
175 43 my $parser = $self->parser;
176 43 ▶ if ( $parser ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parser) 43 0 43
177 43 $rc = $parser->eof(@_);
178 43 delete $self->{_parser};
179 43 $self->{_eof_called} = 1;
180 }
181  
182 43 return $rc;
183 }
184  
185 =head2 $lint->errors()
186  
187 In list context, C<errors> returns all of the errors found in the
188 parsed text. Each error is an object of the type L<HTML::Lint::Error>.
189  
190 In scalar context, it returns the number of errors found.
191  
192 =cut
193  
194 sub errors {
195 46 ▶ my $self = shift;
Subroutine errors: called CC=4 SCAR=13.9
Pod documented
196  
197 46 ▶ if ( !$self->{_parse_called} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $self->{'_parse_called'}) { } 1 45 46
elsif (not $self->{'_eof_called'}) { } 3 42 45
198 1 $self->gripe( 'api-parse-not-called' );
199 }
200 elsif ( !$self->{_eof_called} ) {
201 3 $self->gripe( 'api-eof-not-called' );
202 }
203  
204 46 ▶ if ( wantarray ) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 38 8 46
205 38 return @{$self->{_errors}};
206 }
207 else {
208 8 return scalar @{$self->{_errors}};
209 }
210 }
211  
212 =head2 $lint->clear_errors()
213  
214 Clears the list of errors, in case you want to print and clear, print and clear.
215  
216 =cut
217  
218 sub clear_errors {
219 3 ▶ my $self = shift;
Subroutine clear_errors: called CC=1 SCAR=0.0
Pod documented
220  
221 3 $self->{_errors} = [];
222  
223 3 return;
224 }
225  
226 =head2 $lint->only_types( $type1[, $type2...] )
227  
228 Specifies to only want errors of a certain type.
229  
230 $lint->only_types( HTML::Lint::Error::STRUCTURE );
231  
232 Calling this without parameters makes the object return all possible
233 errors.
234  
235 The error types are C<STRUCTURE>, C<HELPER> and C<FLUFF>.
236 See L<HTML::Lint::Error> for details on these types.
237  
238 =cut
239  
240 sub only_types {
241 5 ▶ my $self = shift;
Subroutine only_types: called CC=1 SCAR=0.0
Pod documented
242  
243 5 $self->{_types} = [@_];
244  
245 5 return;
246 }
247  
248 =head2 $lint->gripe( $errcode, [$key1=>$val1, ...] )
249  
250 Adds an error message, in the form of an L<HTML::Lint::Error> object,
251 to the list of error messages for the current object. The file,
252 line and column are automatically passed to the L<HTML::Lint::Error>
253 constructor, as well as whatever other key value pairs are passed.
254  
255 For example:
256  
257 $lint->gripe( 'attr-repeated', tag => $tag, attr => $attr );
258  
259 Usually, the user of the object won't call this directly, but just
260 in case, here you go.
261  
262 =cut
263  
264 sub gripe {
265 95 ▶ my $self = shift;
Subroutine gripe: called CC=3 SCAR=11.0
Pod documented
266  
267 my $error = HTML::Lint::Error->new(
268 95 $self->{_file}, $self->parser->{_line}, $self->parser->{_column}, @_ );
269  
270 95 my @keeps = @{$self->{_types}};
271 95 ▶ if ( !@keeps || $error->is_type(@keeps) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not @keeps or $error->is_type(@keeps)) 87 8 95
Condition100%3/3
not @keeps or $error->is_type(@keeps)
l!l&&r!l&&!r
8528
MC/DC100%2/2
not @keeps or $error->is_type(@keeps)
not @keeps $error->is_type(@keeps)
Truth table100%3/3
not(A) or B
A= not @keepsB= $error->is_type(@keeps)
ABresult
1X1
011
000
272 87 push( @{$self->{_errors}}, $error );
273 }
274  
275 95 return;
276 }
277  
278  
279 =head2 $lint->newfile( $filename )
280  
281 Call C<newfile()> whenever you switch to another file in a batch
282 of linting. Otherwise, the object thinks everything is from the
283 same file. Note that the list of errors is NOT cleared.
284  
285 Note that I<$filename> does NOT need to match what's put into C<parse()>
286 or C<parse_file()>. It can be a description, a URL, or whatever.
287  
288 You should call C<newfile()> even if you are only validating one file. If
289 you do not call C<newfile()> then your errors will not have a filename
290 attached to them.
291  
292 =cut
293  
294 sub newfile {
295 36 ▶ my $self = shift;
Subroutine newfile: called CC=1 SCAR=0.0
Pod documented
296 36 my $file = shift;
297  
298 36 delete $self->{_parser};
299 36 delete $self->{_parse_called};
300 36 delete $self->{_eof_called};
301 36 $self->{_file} = $file;
302 36 $self->{_line} = 0;
303 36 $self->{_column} = 0;
304 36 $self->{_first_seen} = {};
305  
306 36 return $self->{_file};
307 } # newfile
308  
309 40 1;
310  
311 =head1 MODIFYING HTML::LINT'S BEHAVIOR
312  
313 Sometimes you'll have HTML that for some reason cannot conform to
314 HTML::Lint's expectations. For those instances, you can use HTML
315 comments to modify HTML::Lint's behavior.
316  
317 Say you have an image where for whatever reason you can't get
318 dimensions for the image. This HTML snippet:
319  
320 <img src="logo.png" height="120" width="50" alt="Company logo">
321 <img src="that.png">
322  
323 causes this error:
324  
325 foo.html (14:20) <img src="that.png"> tag has no HEIGHT and WIDTH attributes
326  
327 But if for some reason you can't get those dimensions when you build
328 the page, you can at least stop HTML::Lint complaining about it.
329  
330 <img src="this.png" height="120" width="50" alt="Company logo">
331 <!-- html-lint elem-img-sizes-missing: off, elem-img-alt-missing: off -->
332 <img src="that.png">
333 <!-- html-lint elem-img-sizes-missing: on, elem-img-alt-missing: off -->
334  
335 If you want to turn off all HTML::Lint warnings for a block of code, use
336  
337 <!-- html-lint all: off -->
338  
339 And turn them back on with
340  
341 <!-- html-lint all: on -->
342  
343 You don't have to use "on" and "off". For "on", you can use "true"
344 or "1". For "off", you can use "0" or "false".
345  
346 For a list of possible errors and their codes, see L<HTML::Lint::Error>,
347 or run F<perldoc HTML::Lint::Error>.
348  
349 =head1 BUGS, WISHES AND CORRESPONDENCE
350  
351 All bugs and requests are now being handled through GitHub.
352  
353 https://github.com/petdance/html-lint/issues
354  
355 DO NOT send bug reports to http://rt.cpan.org/ or http://code.google.com/
356  
357 =head1 TODO
358  
359 =over 4
360  
361 =item * Check for attributes that require values
362  
363 =item * <TABLE>s that have no rows.
364  
365 =item * Form fields that aren't in a FORM
366  
367 =item * DIVs with nothing in them.
368  
369 =item * HEIGHT= that have percents in them.
370  
371 =item * Check for goofy stuff like:
372  
373 <b><li></b><b>Hello Reader - Spanish Level 1 (K-3)</b>
374  
375 =back
376  
377 =head1 COPYRIGHT & LICENSE
378  
379 Copyright 2005-2018 Andy Lester.
380  
381 This program is free software; you can redistribute it and/or modify it
382 under the terms of the Artistic License v2.0.
383  
384 http://www.opensource.org/licenses/Artistic-2.0
385  
386 Please note that these modules are not products of or supported by the
387 employers of the various contributors to the code.
388  
389 =head1 AUTHOR
390  
391 Andy Lester, andy at petdance.com
392  
393 =cut
394  
395 40 1;
« Test/HTML/Lint.pm ↑ Summary HTML/Lint/HTML4.pm »