HTML/Lint/Parser.pm

filter: Statementstmt 99.4% 193 / 194 Branchbran 98.7% 79 / 80 Conditioncond 89.6% 26 / 29 MC/DCmcdc 81.2% 13 / 16 Subroutinesub 100% 27 / 27 Podpod 100% 2 / 2 totaltotal 97.7% 340 / 348 SCAR 40.6
CC
58
Cov
98%
_entity
14.0
_start
9.0
_comment
9.0
CRAP 58.0
↑ Summary HTML/Lint/Error.pm »
1 package HTML::Lint::Parser;
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::Parser 3.20;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 40 ▶use HTML::Tagset 3.03;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 40 ▶use HTML::Lint::Error ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 40 ▶use HTML::Lint::HTML4 qw( %isKnownAttribute %isRequired %isNonrepeatable %isObsolete );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 40 ▶use HTML::Entities qw( %char2entity %entity2char );
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 40 ▶use parent 'HTML::Parser';
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 =head1 NAME
16  
17 HTML::Lint::Parser - Parser for HTML::Lint. No user-serviceable parts inside.
18  
19 =head1 VERSION
20  
21 Version 2.30
22  
23 =cut
24  
25 40 our $VERSION = '2.30';
26  
27 =head1 SYNOPSIS
28  
29 See L<HTML::Lint> for all the gory details.
30  
31 =head1 METHODS
32  
33 =head2 new( $gripe )
34  
35 Constructor for the main parsing object. The I<$gripe> argument
36 is a coderef to a function that can handle errors from the parser.
37 It is only ever (so far) C<HTML::Lint::gripe()>.
38  
39 =cut
40  
41 sub new {
42 47 ▶ my $class = shift;
Subroutine new: called CC=1 SCAR=0.0
Pod documented
43 47 my $gripe = shift;
44  
45 47 my $self =
46 HTML::Parser->new(
47 api_version => 3,
48 start_document_h => [ \&_start_document, 'self' ],
49 end_document_h => [ \&_end_document, 'self,line,column' ],
50 start_h => [ \&_start, 'self,tagname,line,column,@attr' ],
51 end_h => [ \&_end, 'self,tagname,line,column,tokenpos,@attr' ],
52 comment_h => [ \&_comment, 'self,tagname,line,column,text' ],
53 text_h => [ \&_text, 'self,text' ],
54 strict_names => 0,
55 empty_element_tags => 1,
56 attr_encoded => 1,
57 );
58 47 bless $self, $class;
59  
60 47 $self->{_gripe} = $gripe;
61 47 $self->{_stack} = [];
62 47 $self->{_directives} = {};
63  
64 47 return $self;
65 }
66  
67 =head2 $parser->gripe( $errorcode, [ arg1=>val1, ...] )
68  
69 Calls the passed-in gripe function.
70  
71 If a given directive has been set to turn off a given message, then
72 the parent gripe never gets called.
73  
74 =cut
75  
76 sub gripe {
77 96 ▶ my $self = shift;
Subroutine gripe: called CC=2 SCAR=6.9
Pod documented
78 96 my $errorcode = shift;
79  
80 96 ▶ if ( $self->_displayable( $errorcode ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_displayable($errorcode)) 91 5 96
81 91 $self->{_gripe}->( $errorcode, @_ );
82 }
83  
84 96 return;
85 }
86  
87 sub _displayable {
88 96 ▶ my $self = shift;
Subroutine _displayable: called CC=2 SCAR=6.9
89 96 my $errorcode = shift;
90  
91 96 my $directives = $self->{_directives};
92 96 ▶ if ( not defined $directives->{$errorcode} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $directives->{$errorcode}) { } 87 9 96
93 87 return 1;
94 }
95 else {
96 9 return $directives->{$errorcode};
97 }
98 }
99  
100 sub _start_document {
101 47 ▶ return;
Subroutine _start_document: called CC=1 SCAR=0.0
102 }
103  
104 sub _end_document {
105 44 ▶ my ($self,$line,$column) = @_;
Subroutine _end_document: called CC=3 SCAR=11.0
106  
107 44 for my $tag ( sort keys %isRequired ) {
108 176 ▶ if ( !$self->{_first_seen}->{$tag} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"_first_seen"}{$tag}) 29 147 176
109 29 $self->gripe( 'doc-tag-required', tag => $tag );
110 }
111 }
112  
113 44 return;
114 }
115  
116 sub _start {
117 528 ▶ my ($self,$tag,$line,$column,@attr) = @_;
Subroutine _start: called CC=9 SCAR=22.0
118  
119 528 $self->{_line} = $line;
120 528 $self->{_column} = $column;
121  
122 528 my $validattr = $isKnownAttribute{ $tag };
123 528 ▶ if ( $validattr ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($validattr) { } 525 3 528
124 525 my %seen;
125 525 my $i = 0;
126 525 ▶ while ( $i < @attr ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @attr) 630 525 1155
127 630 my ($attr,$val) = @attr[$i++,$i++];
128 630 ▶ if ( $seen{$attr}++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($seen{$attr}++) 1 629 630
129 1 $self->gripe( 'attr-repeated', tag => $tag, attr => $attr );
130 }
131  
132 630 ▶ if ( !$validattr->{$attr} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($validattr->{$attr}) 4 626 630
133 4 $self->gripe( 'attr-unknown', tag => $tag, attr => $attr );
134 }
135  
136 630 $self->_entity($val, 'attr');
137 } # while attribs
138 }
139 else {
140 3 $self->gripe( 'elem-unknown', tag => $tag );
141 }
142 528 ▶ $self->_element_push( $tag ) unless $HTML::Tagset::emptyElement{ $tag };
Branch: 1 branch
BranchTrueFalseTotal
unless $HTML::Tagset::emptyElement{$tag} 401 127 528
143  
144 528 ▶ if ( my $where = $self->{_first_seen}{$tag} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $where = $self->{'_first_seen'}{$tag}) { } 291 237 528
145 291 ▶ if ( $isNonrepeatable{$tag} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($HTML::Lint::Parser::isNonrepeatable{$tag}) 1 290 291
146 $self->gripe( 'elem-nonrepeatable',
147 tag => $tag,
148 1 where => HTML::Lint::Error::where( @{$where} )
149 );
150 }
151 }
152 else {
153 237 $self->{_first_seen}{$tag} = [$line,$column];
154 }
155  
156 # Call any other overloaded func
157 528 my $tagfunc = "_start_$tag";
158 528 ▶ if ( $self->can($tagfunc) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->can($tagfunc)) 53 475 528
159 53 $self->$tagfunc( $tag, @attr );
160 }
161  
162 528 return;
163 }
164  
165 sub _text {
166 761 ▶ my ($self,$text) = @_;
Subroutine _text: called CC=1 SCAR=0.0
167  
168 761 $self->_entity($text, 'text');
169  
170 761 return;
171 }
172  
173 sub _entity {
174 1391 ▶ my ($self,$text,$type) = @_;
Subroutine _entity: called CC=14 SCAR=26.4
175  
176 1391 ▶ if ( not $self->{_entity_lookup} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"_entity_lookup"}) 44 1347 1391
177 44 my @entities = sort keys %HTML::Entities::entity2char;
178 # Strip his semicolons
179 44 s/;$// for @entities;
180 44 $self->{_entity_lookup} = { map { ($_,1) } @entities };
181 }
182  
183 1391 ▶ while ( $text =~ /([^\x09\x0A\x0D -~])/g ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ /([^\x09\x0A\x0D -~])/g) 4 1391 1395
184 4 my $bad = $1;
185 $self->gripe(
186 $type . '-use-entity',
187 char => sprintf( '\x%02lX', ord($bad) ),
188 4 ▶ entity => $char2entity{ $bad } || '&#' . ord($bad) . ';',
Condition100%2/2
$HTML::Lint::Parser::char2entity{$bad} || '&#' . ord($bad) . ';'
l!l
31
MC/DC100%1/1
$HTML::Lint::Parser::char2entity{$bad} || '&#' . ord($bad) . ';'
$HTML::Lint::Parser::char2entity{$bad}
Truth table100%2/2
A
A= $HTML::Lint::Parser::char2entity{$bad}
Aresult
11
00
189 );
190 }
191  
192 1391 ▶ while ( $text =~ /&([^ ;]*;?)/g ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ /&([^ ;]*;?)/g) 90 1391 1481
193 90 my $match = $1;
194  
195 90 ▶ if ( $match eq '' ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($match eq '') { } 4 86 90
elsif (not $match =~ /;$/) { } 6 80 86
elsif ($match =~ /^#(\d+);$/) { } 12 68 80
elsif ($match =~ /^#x([\dA-F]+);$/i) { } 3 65 68
196 4 $self->gripe( $type . '-use-entity', char => '&', entity => '&amp;' );
197 }
198 elsif ( $match !~ m/;$/ ) {
199 6 ▶ if ( exists $self->{_entity_lookup}->{$match}
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{'_entity_lookup'}{$match} or $match =~ /^#(\d+)$/ or $match =~ /^#x[\dA-F]+$/i) { } 5 1 6
Condition83%5/6
exists $self->{"_entity_lookup"}{$match} or $match =~ /^#(\d+)$/ or $match =~ /^#x[\dA-F]+$/i
l!l&&r!l&&!r
501
exists $self->{"_entity_lookup"}{$match} or $match =~ /^#(\d+)$/
l!l&&r!l&&!r
321
MC/DC66%2/3
exists $self->{"_entity_lookup"}{$match} or $match =~ /^#(\d+)$/ or $match =~ /^#x[\dA-F]+$/i
exists $self->{"_entity_lookup"}{$match} $match =~ /^#(\d+)$/ $match =~ /^#x[\dA-F]+$/i
Truth table75%3/4
A or B or C
A= exists $self->{"_entity_lookup"}{$match}B= $match =~ /^#(\d+)$/C= $match =~ /^#x[\dA-F]+$/i
ABCresult
1XX1
01X1
0011
0000
200 || $match =~ m/^#(\d+)$/ || $match =~ m/^#x[\dA-F]+$/i) {
201 5 $self->gripe( $type . '-unclosed-entity', entity => "&$match;" );
202 }
203 else {
204 1 $self->gripe( $type . '-unknown-entity', entity => "&$match" );
205 }
206 }
207 elsif ( $match =~ m/^#(\d+);$/ ) {
208 # All numeric entities are OK. We used to check that they were in a given range.
209 }
210 elsif ( $match =~ m/^#x([\dA-F]+);$/i ) {
211 # All hex entities OK. We used to check that they were in a given range.
212 }
213 else {
214 65 $match =~ s/;$//;
215 65 ▶ if ( !exists $self->{_entity_lookup}->{$match} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"_entity_lookup"}{$match}) 4 61 65
216 4 $self->gripe( $type . '-unknown-entity', entity => "&$match;" );
217 }
218 }
219 }
220  
221 1391 return;
222 }
223  
224 sub _comment {
225 29 ▶ my ($self,$tagname,$line,$column,$text) = @_;
Subroutine _comment: called CC=9 SCAR=22.0
226  
227 # Look for the html-lint directives
228 29 ▶ if ( $tagname =~ m/^\s*html-lint\s*(.+)\s*$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tagname =~ /^\s*html-lint\s*(.+)\s*$/) 14 15 29
229 14 my $text = $1;
230  
231 14 my @commands = split( /\s*,\s*/, $text );
232  
233 14 for my $command ( @commands ) {
234 15 my ($directive,$value) = split( /\s*:\s*/, $command, 2 );
235 15 _trim($_) for ($directive,$value);
236  
237 15 ▶ if ( ($directive ne 'all') &&
Branch: 1 branch
BranchTrueFalseTotal
if ($directive ne "all" and not exists $HTML::Lint::Error::errors{$directive}) 1 14 15
Condition100%3/3
$directive ne "all" and not exists $HTML::Lint::Error::errors{$directive}
!ll&&!rl&&r
3111
MC/DC100%2/2
$directive ne "all" and not exists $HTML::Lint::Error::errors{$directive}
$directive ne "all" not exists $HTML::Lint::Error::errors{$directive}
Truth table100%3/3
A and not(B)
A= $directive ne "all"B= not exists $HTML::Lint::Error::errors{$directive}
ABresult
0X0
100
111
238 ( not exists $HTML::Lint::Error::errors{ $directive } ) ) {
239 1 $self->gripe( 'config-unknown-directive',
240 directive => $directive,
241 where => HTML::Lint::Error::where($line,$column)
242 );
243 1 next;
244 }
245  
246 14 my $normalized_value = _normalize_value( $value );
247 14 ▶ if ( !defined($normalized_value) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $normalized_value) 1 13 14
248 1 $self->gripe( 'config-unknown-value',
249 directive => $directive,
250 value => $value,
251 where => HTML::Lint::Error::where($line,$column)
252 );
253 1 next;
254 }
255  
256 13 ▶ if ( $directive eq 'all' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($directive eq 'all') { } 3 10 13
257 3 for my $err ( keys %HTML::Lint::Error::errors ) {
258 63 $self->_set_directive( $err, $normalized_value );
259 }
260 }
261 else {
262 10 $self->_set_directive( $directive, $normalized_value );
263 }
264 }
265 }
266  
267 29 return;
268 }
269  
270 sub _set_directive {
271 73 ▶ my $self = shift;
Subroutine _set_directive: called CC=1 SCAR=0.0
272 73 my $which = shift;
273 73 my $what = shift;
274  
275 73 $self->{_directives}{$which} = $what;
276  
277 73 return;
278 }
279  
280  
281 sub _normalize_value {
282 14 ▶ my $what = shift;
Subroutine _normalize_value: called CC=7 SCAR=19.5
283  
284 14 $what = _trim( $what );
285 14 ▶ return 1 if $what eq '1' || $what eq 'on' || $what eq 'true';
Branch: 1 branch
BranchTrueFalseTotal
if $what eq 1 or $what eq "on" or $what eq "true" 4 9 13
Condition100%6/6
$what eq 1 or $what eq "on" or $what eq "true"
l!l&&r!l&&!r
419
$what eq 1 or $what eq "on"
l!l&&r!l&&!r
1310
MC/DC100%3/3
$what eq 1 or $what eq "on" or $what eq "true"
$what eq 1 $what eq "on" $what eq "true"
Truth table100%4/4
A or B or C
A= $what eq 1B= $what eq "on"C= $what eq "true"
ABCresult
1XX1
01X1
0011
0000
286 9 ▶ return 0 if $what eq '0' || $what eq 'off' || $what eq 'false';
Branch: 1 branch
BranchTrueFalseTotal
if $what eq 0 or $what eq "off" or $what eq "false" 7 1 8
Condition100%6/6
$what eq 0 or $what eq "off" or $what eq "false"
l!l&&r!l&&!r
711
$what eq 0 or $what eq "off"
l!l&&r!l&&!r
162
MC/DC100%3/3
$what eq 0 or $what eq "off" or $what eq "false"
$what eq 0 $what eq "off" $what eq "false"
Truth table100%4/4
A or B or C
A= $what eq 0B= $what eq "off"C= $what eq "false"
ABCresult
1XX1
01X1
0011
0000
287 1 return undef;
288 }
289  
290 sub _trim {
291 44 ▶ $_[0] =~ s/^\s+//;
Subroutine _trim: called CC=1 SCAR=0.0
292 44 $_[0] =~ s/\s+$//;
293  
294 44 return $_[0];
295 }
296  
297 sub _end { ## no critic ( Subroutines::ProhibitManyArgs ) I have no choice in what these args are.
298 391 ▶ my ($self,$tag,$line,$column,$tokenpos,@attr) = @_;
Subroutine _end: called CC=7 SCAR=19.5
299  
300 391 $self->{_line} = $line;
301 391 $self->{_column} = $column;
302  
303 391 ▶ if ( !$tokenpos ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $tokenpos) { } 12 379 391
elsif ($HTML::Tagset::emptyElement{$tag}) { } 1 378 379
304 # This is a dummy end event for something like <img />.
305 # Do nothing.
306 }
307 elsif ( $HTML::Tagset::emptyElement{ $tag } ) {
308 1 $self->gripe( 'elem-empty-but-closed', tag => $tag );
309 }
310 else {
311 378 ▶ if ( $self->_in_context($tag) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_in_context($tag)) { } 374 4 378
312 374 my @leftovers = $self->_element_pop_back_to($tag);
313 374 for ( @leftovers ) {
314 26 my ($tag,$line,$col) = @{$_};
315 $self->gripe( 'elem-unclosed', tag => $tag,
316 where => HTML::Lint::Error::where($line,$col) )
317 26 ▶ unless $HTML::Tagset::optionalEndTag{$tag};
Branch: 1 branch
BranchTrueFalseTotal
unless $HTML::Tagset::optionalEndTag{$tag} 12 14 26
318 } # for
319 }
320 else {
321 4 $self->gripe( 'elem-unopened', tag => $tag );
322 }
323 } # is empty element
324  
325 # Call any other overloaded func
326 391 my $tagfunc = "_end_$tag";
327 391 ▶ if ( $self->can($tagfunc) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->can($tagfunc)) 0 391 391
328 0 $self->$tagfunc( $tag, $line );
329 }
330  
331 391 return;
332 }
333  
334 sub _element_push {
335 401 ▶ my $self = shift;
Subroutine _element_push: called CC=2 SCAR=6.9
336 401 for ( @_ ) {
337 401 push( @{$self->{_stack}}, [$_,$self->{_line},$self->{_column}] );
338 } # while
339  
340 401 return;
341 }
342  
343 sub _find_tag_in_stack {
344 752 ▶ my $self = shift;
Subroutine _find_tag_in_stack: called CC=3 SCAR=11.0
345 752 my $tag = shift;
346 752 my $stack = $self->{_stack};
347  
348 752 my $offset = @{$stack} - 1;
349 752 ▶ while ( $offset >= 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($offset >= 0) 808 4 812
350 808 ▶ if ( $stack->[$offset][0] eq $tag ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stack->[$offset][0] eq $tag) 748 60 808
351 748 return $offset;
352 }
353 60 --$offset;
354 } # while
355  
356 4 return;
357 }
358  
359 sub _element_pop_back_to {
360 374 ▶ my $self = shift;
Subroutine _element_pop_back_to: called CC=2 SCAR=6.9
361 374 my $tag = shift;
362  
363 374 ▶ my $offset = $self->_find_tag_in_stack($tag) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $offset = $self->_find_tag_in_stack($tag) or return 44 330 374
364  
365 330 my @leftovers = splice( @{$self->{_stack}}, $offset + 1 );
366 330 pop @{$self->{_stack}};
367  
368 330 return @leftovers;
369 }
370  
371 sub _in_context {
372 378 ▶ my $self = shift;
Subroutine _in_context: called CC=1 SCAR=0.0
373 378 my $tag = shift;
374  
375 378 my $offset = $self->_find_tag_in_stack($tag);
376 378 return defined $offset;
377 }
378  
379 # Overridden tag-specific stuff
380 sub _start_img { ## no critic ( Subroutines::ProhibitUnusedPrivateSubroutines ) # Called by parser based on tag name.
381 43 ▶ my ($self,$tag,%attr) = @_;
Subroutine _start_img: called CC=4 SCAR=13.9
382  
383 43 my ($h,$w,$src) = @attr{qw( height width src )};
384 43 ▶ if ( defined $h && defined $w ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $h and defined $w) { } 36 7 43
Condition66%2/3
defined $h and defined $w
!ll&&!rl&&r
7036
MC/DC50%1/2
defined $h and defined $w
defined $h defined $w
Truth table66%2/3
A and B
A= defined $hB= defined $w
ABresult
0X0
100
111
385 # Check sizes
386 }
387 else {
388 7 $self->gripe( 'elem-img-sizes-missing', src=>$src );
389 }
390 43 ▶ if ( not defined $attr{alt} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $attr{"alt"}) 8 35 43
391 8 $self->gripe( 'elem-img-alt-missing', src=>$src );
392 }
393  
394 43 return;
395 }
396  
397 sub _start_input { ## no critic ( Subroutines::ProhibitUnusedPrivateSubroutines ) # Called by parser based on tag name.
398 10 ▶ my ($self,$tag,%attr) = @_;
Subroutine _start_input: called CC=6 SCAR=17.9
399  
400 10 my ($type,$alt) = @attr{qw( type alt )};
401 10 ▶ if ( defined($type) && (lc($type) eq 'image') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $type and lc $type eq "image") 9 1 10
Condition66%2/3
defined $type and lc $type eq "image"
!ll&&!rl&&r
019
MC/DC50%1/2
defined $type and lc $type eq "image"
defined $type lc $type eq "image"
Truth table66%2/3
A and B
A= defined $typeB= lc $type eq "image"
ABresult
0X0
100
111
402 9 my $ok = defined($alt);
403 9 ▶ if ( $ok ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ok) 5 4 9
404 5 $alt =~ s/^ +//;
405 5 $alt =~ s/ +$//;
406 5 $ok = ($alt ne '');
407 }
408 9 ▶ if ( !$ok ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 6 3 9
409 6 my $name = $attr{name};
410 6 ▶ $name = '' unless defined $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 3 3 6
411 6 $self->gripe( 'elem-input-alt-missing', name => $name );
412 }
413 }
414  
415 10 return;
416 }
417  
418 40 1;
↑ Summary HTML/Lint/Error.pm »