blib/lib/HTML/Anchor/Audit.pm

filter: Statementstmt 100% 169 / 169 Branchbran 95.0% 76 / 80 Conditioncond 79.6% 51 / 64 MC/DCmcdc 67.5% 25 / 37 Subroutinesub 100% 14 / 14 Podpod 100% 2 / 2 totaltotal 92.0% 337 / 366 CC 68 SCAR 30.6
CC
68
Cov
95%
audit_html
33.1
__ANON__
23.0
__ANON__
7.0
CRAP 21.2
↑ Summary script/html-anchor-audit »
1 package HTML::Anchor::Audit;
2  
3 12 ▶use 5.016;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 12 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 12 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 12 ▶use Encode qw(encode decode FB_CROAK FB_DEFAULT);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 12 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 12 ▶use HTML::Parser 3.72;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 12 ▶use Unicode::Normalize qw(NFC);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 12 our $VERSION = '0.001';
13 12 our @EXPORT_OK = qw(audit_html audit_utf8);
14  
15 9 ▶sub audit_utf8 {
Subroutine audit_utf8: called CC=5 SCAR=16.8
Pod documented
16 9 my ($bytes, @options) = @_;
17 9 ▶ croak 'audit_utf8 expects a scalar of UTF-8 bytes' if !defined($bytes) || ref($bytes);
Branch: 1 branch
BranchTrueFalseTotal
if not defined $bytes or ref $bytes 0 9 9
Condition33%1/3
not defined $bytes or ref $bytes
l!l&&r!l&&!r
009
MC/DC0%0/2
not defined $bytes or ref $bytes
not defined $bytes ref $bytes
Truth table33%1/3
not(A) or B
A= not defined $bytesB= ref $bytes
ABresult
1X1
011
000
18 9 ▶ croak 'audit_utf8 expects bytes, not a wide-character string' if $bytes =~ /[^\x00-\xFF]/;
Branch: 1 branch
BranchTrueFalseTotal
if $bytes =~ /[^\x00-\xFF]/u 0 9 9
19 9 my $text = eval { decode('UTF-8', $bytes, FB_CROAK) };
20 9 ▶ croak 'Input is not valid UTF-8' if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 2 7 9
21 7 return audit_html($text, @options);
22 }
23  
24 53 ▶sub audit_html {
Subroutine audit_html: called CC=33 SCAR=35.0
Pod documented
25 53 my ($html, @options) = @_;
26 53 ▶ croak 'audit_html expects a decoded character string' if !defined($html) || ref($html);
Branch: 1 branch
BranchTrueFalseTotal
if not defined $html or ref $html 2 51 53
Condition100%3/3
not defined $html or ref $html
l!l&&r!l&&!r
1151
MC/DC100%2/2
not defined $html or ref $html
not defined $html ref $html
Truth table100%3/3
not(A) or B
A= not defined $htmlB= ref $html
ABresult
1X1
011
000
27 51 ▶ croak 'Options must be key/value pairs' if @options % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @options % 2 1 50 51
28 50 my %opts = @options;
29 50 for my $key (keys %opts) {
30 15 ▶ croak "Unknown option: $key" if $key ne 'max_bytes' && $key ne 'max_issues';
Branch: 1 branch
BranchTrueFalseTotal
if $key ne "max_bytes" and $key ne "max_issues" 1 14 15
Condition100%3/3
$key ne "max_bytes" and $key ne "max_issues"
!ll&&!rl&&r
771
MC/DC100%2/2
$key ne "max_bytes" and $key ne "max_issues"
$key ne "max_bytes" $key ne "max_issues"
Truth table100%3/3
A and B
A= $key ne "max_bytes"B= $key ne "max_issues"
ABresult
0X0
100
111
31 croak "$key must be a positive integer" if !defined($opts{$key}) || ref($opts{$key})
32 14 ▶ || $opts{$key} !~ /\A[1-9][0-9]*\z/;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $opts{$key} or ref $opts{$key} or not $opts{$key} =~ /\A[1-9][0-9]*\z/u 2 12 14
Condition50%3/6
not defined $opts{$key} or ref $opts{$key} or not $opts{$key} =~ /\A[1-9][0-9]*\z/u
l!l&&r!l&&!r
0212
not defined $opts{$key} or ref $opts{$key}
l!l&&r!l&&!r
0014
MC/DC33%1/3
not defined $opts{$key} or ref $opts{$key} or not $opts{$key} =~ /\A[1-9][0-9]*\z/u
not defined $opts{$key} ref $opts{$key} not $opts{$key} =~ /\A[1-9][0-9]*\z/u
Truth table50%2/4
not(A) or B or not(C)
A= not defined $opts{$key}B= ref $opts{$key}C= not $opts{$key} =~ /\A[1-9][0-9]*\z/u
ABCresult
1XX1
01X1
0011
0000
33 }
34 47 ▶ my $max_bytes = $opts{max_bytes} // (2 * 1024 * 1024);
Condition100%2/2
$opts{"max_bytes"} // 2097152
l!l
641
MC/DC100%1/1
$opts{"max_bytes"} // 2097152
$opts{"max_bytes"}
Truth table100%2/2
A
A= $opts{"max_bytes"}
Aresult
11
00
35 47 ▶ my $max_issues = $opts{max_issues} // 500;
Condition100%2/2
$opts{"max_issues"} // 500
l!l
641
MC/DC100%1/1
$opts{"max_issues"} // 500
$opts{"max_issues"}
Truth table100%2/2
A
A= $opts{"max_issues"}
Aresult
11
00
36 47 ▶ croak "Input exceeds max_bytes ($max_bytes)" if length($html) > $max_bytes;
Branch: 1 branch
BranchTrueFalseTotal
if length $html > $max_bytes 0 47 47
37 47 my $bytes = eval { encode('UTF-8', $html, FB_CROAK) };
38 47 ▶ croak 'Input contains an invalid Unicode character' if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 47 47
39 47 ▶ croak "Input exceeds max_bytes ($max_bytes)" if length($bytes) > $max_bytes;
Branch: 1 branch
BranchTrueFalseTotal
if length $bytes > $max_bytes 1 46 47
40  
41 46 my @line_starts = (0);
42 46 ▶ while ($bytes =~ /\r\n|\r|\n/g) { push @line_starts, pos($bytes) }
Branch: 1 branch
BranchTrueFalseTotal
if ($bytes =~ /\r\n|\r|\n/gu) 15 46 61
43 46 my (@targets, @refs, @issues);
44 46 my (%ids, %names, %normalized);
45 46 my ($template_depth, $base_href, $out_of_scope, $total_issues) = (0, undef, 0, 0);
46 46 my ($cached_line, $cached_offset, $cached_column) = (-1, 0, 1);
47 10143 ▶ my $location = sub {
Subroutine __ANON__: called CC=7 SCAR=19.5
48 10143 my ($offset, $tag) = @_;
49 10143 my ($lo, $hi) = (0, $#line_starts);
50 10143 ▶ while ($lo < $hi) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lo < $hi) 33 10143 10176
51 33 my $mid = int(($lo + $hi + 1) / 2);
52 33 ▶ if ($line_starts[$mid] <= $offset) { $lo = $mid } else { $hi = $mid - 1 }
Branch: 1 branch
BranchTrueFalseTotal
if ($line_starts[$mid] <= $offset) { } 18 15 33
53 }
54 10143 ▶ my $start = $cached_line == $lo && $offset >= $cached_offset
Branch: 1 branch
BranchTrueFalseTotal
$cached_line == $lo && $offset >= $cached_offset ? : 10090 53 10143
Condition66%2/3
$cached_line == $lo and $offset >= $cached_offset
!ll&&!rl&&r
53010090
MC/DC50%1/2
$cached_line == $lo and $offset >= $cached_offset
$cached_line == $lo $offset >= $cached_offset
Truth table66%2/3
A and B
A= $cached_line == $loB= $offset >= $cached_offset
ABresult
0X0
100
111
55 ? $cached_offset : $line_starts[$lo];
56 10143 ▶ my $base_column = $start == $cached_offset && $cached_line == $lo ? $cached_column : 1;
Branch: 1 branch
BranchTrueFalseTotal
$start == $cached_offset && $cached_line == $lo ? : 10090 53 10143
Condition100%3/3
$start == $cached_offset and $cached_line == $lo
!ll&&!rl&&r
114210090
MC/DC100%2/2
$start == $cached_offset and $cached_line == $lo
$start == $cached_offset $cached_line == $lo
Truth table100%3/3
A and B
A= $start == $cached_offsetB= $cached_line == $lo
ABresult
0X0
100
111
57 10143 my $prefix = substr($bytes, $start, $offset - $start);
58 10143 my $column = $base_column + length(decode('UTF-8', $prefix, FB_CROAK));
59 10143 ($cached_line, $cached_offset, $cached_column) = ($lo, $offset, $column);
60 10143 return { tag => $tag, line => $lo + 1,
61 column => $column,
62 byte_column => $offset - $line_starts[$lo] + 1, byte_offset => $offset };
63 46 };
64 71 ▶ my $add_issue = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
65 71 my ($issue) = @_;
66 71 $total_issues++;
67 71 ▶ push @issues, $issue if @issues < $max_issues;
Branch: 1 branch
BranchTrueFalseTotal
if @issues < $max_issues 69 2 71
68 46 };
69 46 my $parser = HTML::Parser->new(api_version => 3);
70 46 $parser->utf8_mode(1);
71 10148 ▶ $parser->handler(start => sub {
Subroutine __ANON__: called CC=23 SCAR=31.4
72 10148 my ($tag, $attr, $offset) = @_;
73 10148 ▶ if ($tag eq 'template') {
Branch: 2 branches
BranchTrueFalseTotal
if ($tag eq 'template') { } 3 10145 10148
elsif ($template_depth) { } 4 10141 10145
74 # The template element belongs to the document; its content does not.
75 3 ▶ if ($template_depth) { $template_depth++; return }
Branch: 1 branch
BranchTrueFalseTotal
if ($template_depth) 1 2 3
76 4 } elsif ($template_depth) { return }
77 10143 my $loc = $location->($offset, $tag);
78 10143 my %values;
79 10143 for my $key (qw(id name href)) {
80 $values{$key} = decode('UTF-8', $attr->{$key}, FB_CROAK)
81 30429 ▶ if exists($attr->{$key}) && defined($attr->{$key});
Branch: 1 branch
BranchTrueFalseTotal
if exists $attr->{$key} and defined $attr->{$key} 10138 20291 30429
Condition66%2/3
exists $attr->{$key} and defined $attr->{$key}
!ll&&!rl&&r
20291010138
MC/DC50%1/2
exists $attr->{$key} and defined $attr->{$key}
exists $attr->{$key} defined $attr->{$key}
Truth table66%2/3
A and B
A= exists $attr->{$key}B= defined $attr->{$key}
ABresult
0X0
100
111
82 }
83 10143 ▶ if (defined($values{id}) && length($values{id})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $values{"id"} and length $values{"id"}) 5079 5064 10143
Condition66%2/3
defined $values{"id"} and length $values{"id"}
!ll&&!rl&&r
506405079
MC/DC50%1/2
defined $values{"id"} and length $values{"id"}
defined $values{"id"} length $values{"id"}
Truth table66%2/3
A and B
A= defined $values{"id"}B= length $values{"id"}
ABresult
0X0
100
111
84 5079 my $target = { kind => 'id', value => $values{id}, %$loc };
85 5079 ▶ my $existing = $ids{$values{id}} ||= [];
Condition100%2/2
$ids{$values{"id"}} ||= []
l!l
505029
MC/DC100%1/1
$ids{$values{"id"}} ||= []
$ids{$values{"id"}}
Truth table100%2/2
A
A= $ids{$values{"id"}}
Aresult
11
00
86 5079 ▶ if (@$existing) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$existing) 50 5029 5079
87 $add_issue->({ code => 'duplicate_id', value => $values{id}, %$loc,
88 50 related => [ { %{ $existing->[0] } } ] });
89 }
90 5079 push @$existing, $target;
91 5079 push @targets, $target;
92 5079 $normalized{NFC($values{id})}{$values{id}} = 1;
93 }
94 10143 ▶ if ($tag eq 'a' && defined($values{name}) && length($values{name})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tag eq "a" and defined $values{"name"} and length $values{"name"}) 5 10138 10143
Condition83%5/6
$tag eq "a" and defined $values{"name"} and length $values{"name"}
!ll&&!rl&&r
1013805
$tag eq "a" and defined $values{"name"}
!ll&&!rl&&r
508950495
MC/DC66%2/3
$tag eq "a" and defined $values{"name"} and length $values{"name"}
$tag eq "a" defined $values{"name"} length $values{"name"}
Truth table75%3/4
A and B and C
A= $tag eq "a"B= defined $values{"name"}C= length $values{"name"}
ABCresult
0XX0
10X0
1100
1111
95 5 my $target = { kind => 'name', value => $values{name}, %$loc };
96 5 ▶ push @{ $names{$values{name}} ||= [] }, $target;
Condition100%2/2
$names{$values{"name"}} ||= []
l!l
14
MC/DC100%1/1
$names{$values{"name"}} ||= []
$names{$values{"name"}}
Truth table100%2/2
A
A= $names{$values{"name"}}
Aresult
11
00
97 5 push @targets, $target;
98 5 $normalized{NFC($values{name})}{$values{name}} = 1;
99 }
100 10143 ▶ if ($tag eq 'base' && !defined($base_href) && defined($values{href})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tag eq "base" and not defined $base_href and defined $values{"href"}) 2 10141 10143
Condition83%5/6
$tag eq "base" and not defined $base_href and defined $values{"href"}
!ll&&!rl&&r
1014102
$tag eq "base" and not defined $base_href
!ll&&!rl&&r
1014012
MC/DC66%2/3
$tag eq "base" and not defined $base_href and defined $values{"href"}
$tag eq "base" not defined $base_href defined $values{"href"}
Truth table75%3/4
A and not(B) and C
A= $tag eq "base"B= not defined $base_hrefC= defined $values{"href"}
ABCresult
0XX0
10X0
1100
1111
101 2 my $href = $values{href};
102 2 $href =~ s/[\t\r\n]//g;
103 2 $href =~ s/\A[\x00-\x20]+|[\x00-\x20]+\z//g;
104 2 $base_href = $href;
105 }
106 10143 ▶ if (($tag eq 'a' || $tag eq 'area') && defined($values{href})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tag eq "a" || $tag eq "area" and defined $values{"href"}) 5050 5093 10143
Condition100%6/6
$tag eq "a" || $tag eq "area" and defined $values{"href"}
!ll&&!rl&&r
508855050
$tag eq "a" or $tag eq "area"
l!l&&r!l&&!r
505415088
MC/DC100%3/3
$tag eq "a" || $tag eq "area" and defined $values{"href"}
$tag eq "a" $tag eq "area" defined $values{"href"}
Truth table80%4/5
A or B and C
A= $tag eq "a"B= $tag eq "area"C= defined $values{"href"}
ABCresult
00X0
1X00
0100
1X11
0111
107 5050 my $href = $values{href};
108 5050 $href =~ s/[\t\r\n]//g;
109 5050 $href =~ s/\A[\x00-\x20]+|[\x00-\x20]+\z//g;
110 5050 ▶ if (substr($href, 0, 1) eq '#') {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($href, 0, 1) eq '#') { } 5047 3 5050
111 5047 push @refs, { href => $values{href}, fragment => substr($href, 1), %$loc };
112 3 } else { $out_of_scope++ }
113 }
114 10143 ▶ $template_depth = 1 if $tag eq 'template';
Branch: 1 branch
BranchTrueFalseTotal
if $tag eq "template" 2 10141 10143
115 46 }, 'tagname,attr,offset');
116 10143 ▶ $parser->handler(end => sub {
Subroutine __ANON__: called CC=3 SCAR=11.1
117 10143 my ($tag) = @_;
118 10143 ▶ $template_depth-- if $tag eq 'template' && $template_depth;
Branch: 1 branch
BranchTrueFalseTotal
if $tag eq "template" and $template_depth 3 10140 10143
Condition66%2/3
$tag eq "template" and $template_depth
!ll&&!rl&&r
1014003
MC/DC50%1/2
$tag eq "template" and $template_depth
$tag eq "template" $template_depth
Truth table66%2/3
A and B
A= $tag eq "template"B= $template_depth
ABresult
0X0
100
111
119 46 }, 'tagname');
120 46 $parser->parse($bytes);
121 46 $parser->eof;
122  
123 46 my ($checked, $ignored) = (0, 0);
124 46 for my $ref (@refs) {
125 5047 ▶ if (defined($base_href) && length($base_href)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $base_href and length $base_href) 1 5046 5047
Condition100%3/3
defined $base_href and length $base_href
!ll&&!rl&&r
504511
MC/DC100%2/2
defined $base_href and length $base_href
defined $base_href length $base_href
Truth table100%3/3
A and B
A= defined $base_hrefB= length $base_href
ABresult
0X0
100
111
126 1 $ref->{status} = 'ignored'; $ref->{reason} = 'base_href'; $ignored++; next;
127 }
128 5046 ▶ if (index($ref->{fragment}, ':~:') >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (index($ref->{"fragment"}, ":~:") != -1) 1 5045 5046
129 1 $ref->{status} = 'ignored'; $ref->{reason} = 'fragment_directive'; $ignored++; next;
130 }
131 5045 $checked++;
132 5045 ▶ if ($ref->{fragment} eq '') { $ref->{status} = 'top'; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($ref->{"fragment"} eq "") 1 5044 5045
133 5044 my $escaped = encode('UTF-8', $ref->{fragment});
134 5044 $escaped =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/eg;
135 5044 my $decoded = decode('UTF-8', $escaped, FB_DEFAULT);
136 5044 $ref->{decoded_fragment} = $decoded;
137 5044 my ($matches, $matched_value, $matched_kind);
138 5044 for my $value ($ref->{fragment}, $decoded) {
139 5068 ▶ if ($ids{$value}) { ($matches, $matched_value, $matched_kind) = ($ids{$value}, $value, 'id'); last }
Branch: 1 branch
BranchTrueFalseTotal
if ($ids{$value}) 5021 47 5068
140 47 ▶ if ($names{$value}) { ($matches, $matched_value, $matched_kind) = ($names{$value}, $value, 'name'); last }
Branch: 1 branch
BranchTrueFalseTotal
if ($names{$value}) 2 45 47
141 }
142 5044 ▶ if ($matches) {
Branch: 2 branches
BranchTrueFalseTotal
if ($matches) { } 5023 21 5044
elsif ($decoded =~ /\A[Tt][Oo][Pp]\z/u) { } 3 18 21
143 5023 ▶ my @related = map { { %$_ } } @$matches[0 .. ($#$matches < 9 ? $#$matches : 9)];
Branch: 1 branch
BranchTrueFalseTotal
$matches->$#* < 9 ? : 5022 1 5023
144 5023 ▶ $ref->{status} = @$matches > 1 ? 'ambiguous' : 'matched';
Branch: 1 branch
BranchTrueFalseTotal
@$matches > 1 ? : 3 5020 5023
145 5023 $ref->{target_value} = $matched_value;
146 5023 $ref->{target_kind} = $matched_kind;
147 5023 $ref->{target_count} = scalar(@$matches);
148 5023 $ref->{targets} = \@related;
149 5023 ▶ if (@$matches > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$matches > 1) 3 5020 5023
150 $add_issue->({ code => 'ambiguous_target', href => $ref->{href}, value => $matched_value,
151 3 (map { $_ => $ref->{$_} } qw(tag line column byte_column byte_offset)),
152 related_count => scalar(@$matches), related => \@related });
153 }
154 } elsif ($decoded =~ /\A[Tt][Oo][Pp]\z/) {
155 3 $ref->{status} = 'top';
156 } else {
157 18 $ref->{status} = 'missing';
158 18 ▶ my @suggestions = sort keys %{ $normalized{NFC($decoded)} || {} };
Condition100%2/2
$normalized{&NFC($decoded)} || {}
l!l
117
MC/DC100%1/1
$normalized{&NFC($decoded)} || {}
$normalized{&NFC($decoded)}
Truth table100%2/2
A
A= $normalized{&NFC($decoded)}
Aresult
11
00
159 my $issue = { code => 'missing_target', href => $ref->{href}, value => $decoded,
160 18 map { $_ => $ref->{$_} } qw(tag line column byte_column byte_offset) };
161 18 ▶ $issue->{normalization_candidates} = \@suggestions if @suggestions;
Branch: 1 branch
BranchTrueFalseTotal
if @suggestions 1 17 18
162 18 $add_issue->($issue);
163 }
164 }
165 46 ▶ @issues = sort { $a->{byte_offset} <=> $b->{byte_offset} || $a->{code} cmp $b->{code} } @issues;
Condition33%1/3
$HTML::Anchor::Audit::a->{'byte_offset'} <=> $HTML::Anchor::Audit::b->{'byte_offset'} || $HTML::Anchor::Audit::a->{'code'} cmp $HTML::Anchor::Audit::b->{'code'}
l!l&&r!l&&!r
5100
MC/DC0%0/2
$HTML::Anchor::Audit::a->{'byte_offset'} <=> $HTML::Anchor::Audit::b->{'byte_offset'} || $HTML::Anchor::Audit::a->{'code'} cmp $HTML::Anchor::Audit::b->{'code'}
$HTML::Anchor::Audit::a->{'byte_offset'} <=> $HTML::Anchor::Audit::b->{'byte_offset'} $HTML::Anchor::Audit::a->{'code'} cmp $HTML::Anchor::Audit::b->{'code'}
Truth table33%1/3
A || B
A= $HTML::Anchor::Audit::a->{'byte_offset'} <=> $HTML::Anchor::Audit::b->{'byte_offset'}B= $HTML::Anchor::Audit::a->{'code'} cmp $HTML::Anchor::Audit::b->{'code'}
ABresult
1X1
011
000
166 return {
167 46 ▶ schema_version => 1, targets => \@targets, references => \@refs, issues => \@issues,
Branch: 1 branch
BranchTrueFalseTotal
$total_issues > @issues ? : 2 44 46
168 statistics => { target_count => scalar(@targets), reference_count => scalar(@refs),
169 checked_reference_count => $checked, ignored_reference_count => $ignored,
170 out_of_scope_href_count => $out_of_scope, issue_count => $total_issues,
171 reported_issue_count => scalar(@issues), truncated => $total_issues > @issues ? 1 : 0 },
172 };
173 }
174  
175 12 1;
176  
177 __END__
↑ Summary script/html-anchor-audit »