This repository has been archived by the owner on Apr 14, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Lint.pm
686 lines (485 loc) · 16.1 KB
/
Lint.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
package HTML::Lint;
use warnings;
use strict;
use HTML::Lint::Error;
use HTML::Entities ();
=head1 NAME
HTML::Lint - check for HTML errors in a string or file
=head1 VERSION
Version 2.11_01
=cut
our $VERSION = '2.11_01';
=head1 SYNOPSIS
my $lint = HTML::Lint->new;
$lint->only_types( HTML::Lint::Error::STRUCTURE );
$lint->parse( $data );
$lint->parse_file( $filename );
my $error_count = $lint->errors;
foreach my $error ( $lint->errors ) {
print $error->as_string, "\n";
}
HTML::Lint also comes with a wrapper program called F<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 L<Apache::HTML::Lint> that passes any
mod_perl-generated code through HTML::Lint and get it dumped into your
Apache F<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>
=cut
=head1 METHODS
NOTE: Some of these methods mirror L<HTML::Parser>'s methods, but HTML::Lint
is not a subclass of HTML::Parser.
=head2 new()
Create an HTML::Lint object, which inherits from HTML::Parser.
You may pass the types of errors you want to check for in the
C<only_types> parm.
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] );
=cut
sub new {
my $class = shift;
my %args = @_;
my $self = {
_errors => [],
_types => [],
_flags => {},
};
bless $self, $class;
if ( my $only = $args{only_types} ) {
$self->only_types( ref $only eq 'ARRAY' ? @{$only} : $only );
delete $args{only_types};
}
warn "Unknown argument $_\n" for keys %args;
return $self;
}
=head2 $lint->parser()
Returns the parser object for this object, creating one if necessary.
=cut
sub parser {
my $self = shift;
if ( not $self->{_parser} ) {
$self->{_parser} = HTML::Lint::Parser->new( sub { $self->gripe( @_ ) } );
$self->{_parser}->ignore_elements( qw(script style) );
}
return $self->{_parser};
}
=head2 $lint->parse( $text )
=head2 $lint->parse( $code_ref )
Passes in a chunk of HTML to be linted, either as a piece of text,
or a code reference.
See L<HTML::Parser>'s C<parse_file> method for details.
=cut
sub parse {
my $self = shift;
return $self->parser->parse( @_ );
}
=head2 $lint->parse_file( $file )
Analyzes HTML directly from a file. The C<$file> argument can be a filename,
an open file handle, or a reference to an open file handle.
See L<HTML::Parser>'s C<parse_file> method for details.
=cut
sub parse_file {
my $self = shift;
return $self->parser->parse_file( @_ );
}
=head2 $lint->eof
Signals the end of a block of text getting passed in. This must be
called to make sure that all parsing is complete before looking at errors.
Any parameters (and there shouldn't be any) are passed through to
HTML::Parser's eof() method.
=cut
sub eof {
my $self = shift;
my $rc;
my $parser = $self->parser;
if ( $parser ) {
$rc = $self->parser->eof(@_);
delete $self->{_parser};
}
return $rc;
}
=head2 $lint->errors()
In list context, C<errors> returns all of the errors found in the
parsed text. Each error is an object of the type L<HTML::Lint::Error>.
In scalar context, it returns the number of errors found.
=cut
sub errors {
my $self = shift;
if ( wantarray ) {
return @{$self->{_errors}};
}
else {
return scalar @{$self->{_errors}};
}
}
=head2 $lint->clear_errors()
Clears the list of errors, in case you want to print and clear, print and clear.
=cut
sub clear_errors {
my $self = shift;
$self->{_errors} = [];
return;
}
=head2 $lint->set_flags( $which, $what )
=cut
sub set_flags {
my $self = shift;
my $which = shift;
my $what = shift;
return;
}
=head2 $lint->only_types( $type1[, $type2...] )
Specifies to only want errors of a certain type.
$lint->only_types( HTML::Lint::Error::STRUCTURE );
Calling this without parameters makes the object return all possible
errors.
The error types are C<STRUCTURE>, C<HELPER> and C<FLUFF>.
See L<HTML::Lint::Error> for details on these types.
=cut
sub only_types {
my $self = shift;
$self->{_types} = [@_];
return;
}
=head2 $lint->gripe( $errcode, [$key1=>$val1, ...] )
Adds an error message, in the form of an L<HTML::Lint::Error> object,
to the list of error messages for the current object. The file,
line and column are automatically passed to the L<HTML::Lint::Error>
constructor, as well as whatever other key value pairs are passed.
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.
=cut
sub gripe {
my $self = shift;
my $error = HTML::Lint::Error->new(
$self->{_file}, $self->parser->{_line}, $self->parser->{_column}, @_ );
my @keeps = @{$self->{_types}};
if ( !@keeps || $error->is_type(@keeps) ) {
push( @{$self->{_errors}}, $error );
}
return;
}
=head2 $lint->newfile( $filename )
Call C<newfile()> whenever you switch to another file in a batch
of linting. Otherwise, the object thinks everything is from the
same file. Note that the list of errors is NOT cleared.
Note that I<$filename> does NOT need to match what's put into parse()
or parse_file(). It can be a description, a URL, or whatever.
=cut
sub newfile {
my $self = shift;
my $file = shift;
delete $self->{_parser};
$self->{_file} = $file;
$self->{_line} = 0;
$self->{_column} = 0;
$self->{_first_seen} = {};
return $self->{_file};
} # newfile
=pod
HTML::Lint::Parser is a class only for this module.
=cut
package HTML::Lint::Parser;
use HTML::Parser 3.20;
use HTML::Tagset 3.03;
use HTML::Lint::HTML4 qw( %isKnownAttribute %isRequired %isNonrepeatable %isObsolete );
use HTML::Entities qw( %char2entity );
use base 'HTML::Parser';
sub new {
my $class = shift;
my $gripe = shift;
my $self =
HTML::Parser->new(
api_version => 3,
start_document_h => [ \&_start_document, 'self' ],
end_document_h => [ \&_end_document, 'self,line,column' ],
start_h => [ \&_start, 'self,tagname,line,column,@attr' ],
end_h => [ \&_end, 'self,tagname,line,column,tokenpos,@attr' ],
comment_h => [ \&_comment, 'self,tagname,line,column,text' ],
text_h => [ \&_text, 'self,text' ],
strict_names => 0,
empty_element_tags => 1,
);
bless $self, $class;
$self->{_gripe} = $gripe;
$self->{_stack} = [];
return $self;
}
sub gripe {
my $self = shift;
return $self->{_gripe}->( @_ );
}
sub _start_document {
}
sub _end_document {
my ($self,$line,$column) = @_;
for my $tag ( keys %isRequired ) {
if ( !$self->{_first_seen}->{$tag} ) {
$self->gripe( 'doc-tag-required', tag => $tag );
}
}
return;
}
sub _start {
my ($self,$tag,$line,$column,@attr) = @_;
$self->{_line} = $line;
$self->{_column} = $column;
my $validattr = $isKnownAttribute{ $tag };
if ( $validattr ) {
my %seen;
my $i = 0;
while ( $i < @attr ) {
my ($attr,$val) = @attr[$i++,$i++];
if ( $seen{$attr}++ ) {
$self->gripe( 'attr-repeated', tag => $tag, attr => $attr );
}
if ( $validattr && ( !$validattr->{$attr} ) ) {
$self->gripe( 'attr-unknown', tag => $tag, attr => $attr );
}
} # while attribs
}
else {
$self->gripe( 'elem-unknown', tag => $tag );
}
$self->_element_push( $tag ) unless $HTML::Tagset::emptyElement{ $tag };
if ( my $where = $self->{_first_seen}{$tag} ) {
if ( $isNonrepeatable{$tag} ) {
$self->gripe( 'elem-nonrepeatable',
tag => $tag,
where => HTML::Lint::Error::where( @{$where} )
);
}
}
else {
$self->{_first_seen}{$tag} = [$line,$column];
}
# Call any other overloaded func
my $tagfunc = "_start_$tag";
if ( $self->can($tagfunc) ) {
$self->$tagfunc( $tag, @attr );
}
return;
}
sub _text {
my ($self,$text) = @_;
while ( $text =~ /&(?![#0-9a-z])/ig ) {
$self->gripe( 'text-use-entity', char => '&', entity => '&' );
}
while ( $text =~ /([^\x09\x0A\x0D -~])/g ) {
my $bad = $1;
$self->gripe(
'text-use-entity',
char => sprintf( '\x%02lX', ord($bad) ),
entity => $char2entity{ $bad },
);
}
if ( not $self->{_unclosed_entities_regex} ) {
# Get Gisle's list
my @entities = sort keys %HTML::Entities::entity2char;
# Strip his semicolons
s/;$// for @entities;
# Build a regex
my $entities = join( '|', @entities );
$self->{_unclosed_entities_regex} = qr/&($entities)(?!;)/;
$self->{_entity_lookup} = { map { ($_,1) } @entities };
}
while ( $text =~ m/$self->{_unclosed_entities_regex}/g ) {
my $ent = $1;
$self->gripe( 'text-unclosed-entity', entity => "&$ent;" );
}
while ( $text =~ m/&([^;]+);/g ) {
my $ent = $1;
# Numeric entities are fine, if they're not too large.
if ( $ent =~ /^#(\d+)$/ ) {
if ( $1 > 65536 ) {
$self->gripe( 'text-invalid-entity', entity => "&$ent;" );
}
next;
}
# Hex entities are fine, if they're not too large.
if ( $ent =~ /^#x([\dA-F]+)$/i ) {
if ( length($1) > 4 ) {
$self->gripe( 'text-invalid-entity', entity => "&$ent;" );
}
next;
}
# If it's not a numeric entity, then check the lookup table.
if ( !exists $self->{_entity_lookup}{$ent} ) {
$self->gripe( 'text-unknown-entity', entity => "&$ent;" );
}
}
return;
}
sub _comment {
my ($self,$tagname,$line,$column,$text) = @_;
# Look for the html-lint directives
if ( $tagname =~ m/^\s*html-lint\s*(.+)\s*$/ ) {
my $text = $1;
my @commands = split( /\s*,\s*/, $text );
for my $command ( @commands ) {
print "command=[$command]\n";
my ($which,$what) = split( /\s*:\s*/, $command, 2 );
_trim($_) for ($which,$what);
print "which,what=[$which][$what]\n";
my ($val,$msg) = _translate_what( $what );
if ($msg) {
$self->gripe( 'config-unknown-setting', setting => $what,
where => HTML::Lint::Error::where($line,$column) );
next;
}
$self->{_flags}{$which} = $val;
}
}
return;
}
sub _translate_what {
my $what = shift;
print "[$what]\n";
$what = _trim( $what );
return (1,undef) if $what =~ /^(?:1|on|true)$/;
return (0,undef) if $what =~ /^(?:0|off|false)$/;
return (undef,qq{Unknown status "$what"});
}
sub _trim {
$_[0] =~ s/^\s+//;
$_[0] =~ s/\s+$//;
return $_[0];
}
sub _end {
my ($self,$tag,$line,$column,$tokenpos,@attr) = @_;
$self->{_line} = $line;
$self->{_column} = $column;
if ( !$tokenpos ) {
# This is a dummy end event for something like <img />.
# Do nothing.
}
elsif ( $HTML::Tagset::emptyElement{ $tag } ) {
$self->gripe( 'elem-empty-but-closed', tag => $tag );
}
else {
if ( $self->_in_context($tag) ) {
my @leftovers = $self->_element_pop_back_to($tag);
for ( @leftovers ) {
my ($tag,$line,$col) = @{$_};
$self->gripe( 'elem-unclosed', tag => $tag,
where => HTML::Lint::Error::where($line,$col) )
unless $HTML::Tagset::optionalEndTag{$tag};
} # for
}
else {
$self->gripe( 'elem-unopened', tag => $tag );
}
} # is empty element
# Call any other overloaded func
my $tagfunc = "_end_$tag";
if ( $self->can($tagfunc) ) {
$self->$tagfunc( $tag, $line );
}
return;
}
sub _element_push {
my $self = shift;
for ( @_ ) {
push( @{$self->{_stack}}, [$_,$self->{_line},$self->{_column}] );
} # while
return;
}
sub _find_tag_in_stack {
my $self = shift;
my $tag = shift;
my $stack = $self->{_stack};
my $offset = @{$stack} - 1;
while ( $offset >= 0 ) {
if ( $stack->[$offset][0] eq $tag ) {
return $offset;
}
--$offset;
} # while
return;
}
sub _element_pop_back_to {
my $self = shift;
my $tag = shift;
my $offset = $self->_find_tag_in_stack($tag) or return;
my @leftovers = splice( @{$self->{_stack}}, $offset + 1 );
pop @{$self->{_stack}};
return @leftovers;
}
sub _in_context {
my $self = shift;
my $tag = shift;
my $offset = $self->_find_tag_in_stack($tag);
return defined $offset;
}
# Overridden tag-specific stuff
sub _start_img {
my ($self,$tag,%attr) = @_;
my ($h,$w,$src) = @attr{qw( height width src )};
if ( defined $h && defined $w ) {
# Check sizes
}
else {
$self->gripe( 'elem-img-sizes-missing', src=>$src );
}
if ( not defined $attr{alt} ) {
$self->gripe( 'elem-img-alt-missing', src=>$src );
}
return;
}
=head1 MODIFYING HTML::LINT'S BEHAVIOR
Sometimes you'll have HTML that for some reason cannot conform to
HTML::Lint's expectations. For those instances, you can use HTML
comments to modify HTML::Lint's behavior.
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: off -->
=head1 BUGS, WISHES AND CORRESPONDENCE
All bugs and requests are now being handled through GitHub.
https://github.com/petdance/html-lint/issues
DO NOT send bug reports to http://rt.cpan.org/ or http://code.google.com/
=head1 TODO
=over 4
=item * Check for attributes that require values
=item * <TABLE>s that have no rows.
=item * Form fields that aren't in a FORM
=item * Check for valid entities, and that they end with semicolons
=item * DIVs with nothing in them.
=item * HEIGHT= that have percents in them.
=item * Check for goofy stuff like:
<b><li></b><b>Hello Reader - Spanish Level 1 (K-3)</b>
=back
=head1 COPYRIGHT & LICENSE
Copyright 2005-2012 Andy Lester.
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.
=head1 AUTHOR
Andy Lester, andy at petdance.com
=cut
1;