DOM58/_HTML.pm

filter: Statementstmt 98.5% 201 / 204 Branchbran 96.2% 152 / 158 Conditioncond 86.9% 73 / 84 MC/DCmcdc 73.3% 33 / 45 Subroutinesub 100% 20 / 20 Podpod 0.0% 0 / 7 totaltotal 92.4% 479 / 518 CC 109 SCAR 32.7
CC
109
Cov
96%
parse
52.1
_render
16.1
_script_content
12.0
CRAP 26.3
↑ Summary DOM58/_CSS.pm »
1 package Mojo::DOM58::_HTML;
2  
3 # This file is part of Mojo::DOM58 which is released under:
4 # The Artistic License 2.0 (GPL Compatible)
5 # See the documentation for Mojo::DOM58 for full license details.
6  
7 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Mojo::DOM58::Entities qw(html_attr_unescape html_escape html_unescape);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Scalar::Util 'weaken';
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 2 our $VERSION = '3.003';
14  
15 2 our @EXPORT_OK = 'tag_to_html';
16  
17 2 my $ATTR_RE = qr/
18 ([^<>=\s\/0-9.\-][^<>=\s\/]*|\/) # Key
19 (?:
20 \s*=\s*
21 (?s:(?:"(.*?)")|(?:'(.*?)')|([^>\s]*)) # Value
22 )?
23 \s*
24 /x;
25 2 my %MARKUP = (
26 doctype => qr/
27 \G<!DOCTYPE(
28 \s+\w+ # Doctype
29 (?:(?:\s+\w+)?(?:\s+(?:"[^"]*"|'[^']*'))+)? # External ID
30 (?:\s+\[.+?\])? # Int Subset
31 \s*)>
32 /xis,
33 comment => qr/\G<!--(.*?)(?:--!?|(?<=<!--)-?(?=>))>/s,
34 cdata => qr/\G<!\[CDATA\[(.*?)\]\]>/is,
35 pi => qr/\G<\?(.*?)\?>/s
36 );
37 2 my $TAG_RE = qr/<(\/\s*)?([^<>\s\/0-9.\-][^<>\s\/]*)\s*/;
38  
39 # HTML elements that only contain raw text
40 2 my %RAW = map { $_ => 1 } qw(script style);
41  
42 # HTML elements that only contain raw text and entities
43 2 my %RCDATA = map { $_ => 1 } qw(title textarea);
44  
45 # HTML elements with optional end tags
46 2 my %END = (body => 'head', optgroup => 'optgroup', option => 'option');
47  
48 # HTML elements that break paragraphs
49 2 $END{$_} = 'p' for
50 qw(address article aside blockquote details dialog div dl fieldset),
51 qw(figcaption figure footer form h1 h2 h3 h4 h5 h6 header hgroup hr main),
52 qw(menu nav ol p pre section table ul);
53  
54 # Container HTML elements that create their own scope
55 2 my %SCOPE = map { $_ => 1 } qw(math svg);
56  
57 # SVG and MathML elements that are HTML integration points
58 2 my %INTEGRATION = map { $_ => 1 } qw(desc foreignobject mi mn mo ms mtext title);
59  
60 # HTML table elements with optional end tags
61 2 my %TABLE = map { $_ => 1 } qw(colgroup tbody td tfoot th thead tr);
62  
63 # HTML elements with optional end tags and scoping rules
64 2 my %CLOSE
65 = (li => [{li => 1}, {ul => 1, ol => 1}], tr => [{tr => 1}, {table => 1}]);
66 2 $CLOSE{$_} = [\%TABLE, {table => 1}] for qw(colgroup tbody tfoot thead);
67 2 $CLOSE{$_} = [{dd => 1, dt => 1}, {dl => 1}] for qw(dd dt);
68 2 $CLOSE{$_} = [{rp => 1, rt => 1}, {ruby => 1}] for qw(rp rt);
69 2 $CLOSE{$_} = [{th => 1, td => 1}, {table => 1}] for qw(td th);
70  
71 # HTML parent elements that signal no more content when closed, but that are also phrasing content
72 2 my %NO_MORE_CONTENT = (ruby => [qw(rt rp)], select => [qw(option optgroup)]);
73  
74 # HTML elements without end tags
75 2 my %EMPTY = map { $_ => 1 } (
76 qw(area base br col embed hr img input keygen link menuitem meta param),
77 qw(source track wbr)
78 );
79  
80 # HTML elements categorized as phrasing content (and obsolete inline elements)
81 2 my @PHRASING = (
82 qw(a abbr area audio b bdi bdo br button canvas cite code data datalist),
83 qw(del dfn em embed i iframe img input ins kbd keygen label link map mark),
84 qw(math meta meter noscript object output picture progress q ruby s samp),
85 qw(script select slot small span strong sub sup svg template textarea time u),
86 qw(var video wbr)
87 );
88 2 my @OBSOLETE = qw(acronym applet basefont big font strike tt);
89 2 my %PHRASING = map { $_ => 1 } @OBSOLETE, @PHRASING;
90  
91 # HTML elements that don't get their self-closing flag acknowledged
92 2 my %BLOCK = map { $_ => 1 } (
93 qw(a address applet article aside b big blockquote body button caption),
94 qw(center code col colgroup dd details dialog dir div dl dt em fieldset),
95 qw(figcaption figure font footer form frameset h1 h2 h3 h4 h5 h6 head),
96 qw(header hgroup html i iframe li listing main marquee menu nav nobr),
97 qw(noembed noframes noscript object ol optgroup option p plaintext pre rp),
98 qw(rt s script section select small strike strong style summary table),
99 qw(tbody td template textarea tfoot th thead title tr tt u ul xmp)
100 );
101  
102 2419 ▶sub new {
Subroutine new: called CC=4 SCAR=16.1
Pod undocumented
103 2419 my $class = shift;
104 0 ▶ bless @_ ? @_ > 1 ? {@_} : {%{$_[0]}} : {}, ref $class || $class;
Branch: 2 branches
BranchTrueFalseTotal
@_ ? : 71 2348 2419
@_ > 1 ? : 71 0 71
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
024190
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
105 }
106  
107 12 ▶sub tag { shift->tree(['root', _tag(@_)]) }
Subroutine tag: called CC=1 SCAR=0.0
Pod undocumented
108  
109 2 ▶sub tag_to_html { _render(_tag(@_), undef) }
Subroutine tag_to_html: called CC=1 SCAR=0.0
Pod undocumented
110  
111 5571 ▶sub tree {
Subroutine tree: called CC=3 SCAR=11.0
Pod undocumented
112 5571 my $self = shift;
113 5571 ▶ return exists $self->{tree} ? $self->{tree} : ($self->{tree} = ['root']) unless @_;
Branch: 2 branches
BranchTrueFalseTotal
unless @_ 3143 2428 5571
exists $self->{'tree'} ? : 3138 5 3143
114 2428 $self->{tree} = shift;
115 2428 return $self;
116 }
117  
118 4081 ▶sub xml {
Subroutine xml: called CC=2 SCAR=6.9
Pod undocumented
119 4081 my $self = shift;
120 4081 ▶ return $self->{xml} unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 1941 2140 4081
121 2140 $self->{xml} = shift;
122 2140 return $self;
123 }
124  
125 305 ▶sub parse {
Subroutine parse: called CC=52 SCAR=39.5
Pod undocumented
126 305 my ($self, $html) = (shift, "$_[0]");
127  
128 # Detect XML from a leading declaration
129 305 ▶ $self->xml(1) if !exists $self->{xml} && $html =~ /^\x{feff}?\s*<\?xml[\s?]/i;
Branch: 1 branch
BranchTrueFalseTotal
if not exists $self->{"xml"} and $html =~ /^\x{feff}?\s*<\?xml[\s?]/i 15 290 305
Condition100%3/3
not exists $self->{"xml"} and $html =~ /^\x{feff}?\s*<\?xml[\s?]/i
!ll&&!rl&&r
8620415
MC/DC100%2/2
not exists $self->{"xml"} and $html =~ /^\x{feff}?\s*<\?xml[\s?]/i
not exists $self->{"xml"} $html =~ /^\x{feff}?\s*<\?xml[\s?]/i
Truth table100%3/3
not(A) and B
A= not exists $self->{"xml"}B= $html =~ /^\x{feff}?\s*<\?xml[\s?]/i
ABresult
0X0
100
111
130  
131 305 my $xml = $self->xml;
132 305 my $current = my $tree = ['root'];
133 305 my %unclosed;
134 305 ▶ while ((my $offset = pos($html) || 0) < length $html) {
Branch: 1 branch
BranchTrueFalseTotal
if ((my $offset = pos $html || 0) < length $html) 3398 305 3703
Condition100%2/2
pos $html || 0
l!l
3398305
MC/DC100%1/1
pos $html || 0
pos $html
Truth table100%2/2
A
A= pos $html
Aresult
11
00
135  
136 # Text
137 3398 ▶ if ($html =~ /\G([^<]+)/gc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($html =~ /\G([^<]+)/cg) 1401 1997 3398
138 1401 _node($current, 'text', html_unescape $1);
139 1401 next;
140 }
141  
142 # DOCTYPE, comment, CDATA and processing instruction (only trying the regex for the opener keeps failures flat)
143 1997 my $next = substr $html, $offset + 1, 1;
144 1997 ▶ if ($next eq '!' || $next eq '?') {
Branch: 1 branch
BranchTrueFalseTotal
if ($next eq "!" or $next eq "?") 83 1914 1997
Condition100%3/3
$next eq "!" or $next eq "?"
l!l&&r!l&&!r
53301914
MC/DC100%2/2
$next eq "!" or $next eq "?"
$next eq "!" $next eq "?"
Truth table100%3/3
A or B
A= $next eq "!"B= $next eq "?"
ABresult
1X1
011
000
145 83 my $opener = uc substr $html, $offset, 9;
146 83 ▶ my $type
Branch: 4 branches
BranchTrueFalseTotal
substr($opener, 0, 4) eq '<!--' ? : 20 63 83
$opener eq '<!DOCTYPE' ? : 19 44 63
$opener eq '<![CDATA[' ? : 14 30 44
$next eq '?' ? : 30 0 30
147 = substr($opener, 0, 4) eq '<!--' ? 'comment'
148 : $opener eq '<!DOCTYPE' ? 'doctype'
149 : $opener eq '<![CDATA[' ? 'cdata'
150 : $next eq '?' ? 'pi'
151 : undef;
152  
153 83 ▶ if ($type && !$unclosed{$type}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type and not $unclosed{$type}) 80 3 83
Condition66%2/3
$type and not $unclosed{$type}
!ll&&!rl&&r
0380
MC/DC50%1/2
$type and not $unclosed{$type}
$type not $unclosed{$type}
Truth table66%2/3
A and not(B)
A= $typeB= not $unclosed{$type}
ABresult
0X0
100
111
154 80 ▶ if ($html =~ /$MARKUP{$type}/gc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($html =~ /$MARKUP{$type}/cg) 77 3 80
155 77 my $value = $1;
156  
157 # DOCTYPE, CDATA (outside of foreign content) and processing instructions end at the first ">" in HTML
158 77 ▶ my $i = $xml || $type eq 'comment' ? -1 : index $value, '>';
Branch: 1 branch
BranchTrueFalseTotal
$xml || $type eq 'comment' ? : 42 35 77
Condition100%3/3
$xml or $type eq "comment"
l!l&&r!l&&!r
251735
MC/DC100%2/2
$xml or $type eq "comment"
$xml $type eq "comment"
Truth table100%3/3
A or B
A= $xmlB= $type eq "comment"
ABresult
1X1
011
000
159 77 ▶ if ($i >= 0 && !($type eq 'cdata' && _foreign($current))) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i >= 0 and not $type eq "cdata" && _foreign($current)) 5 72 77
Condition100%6/6
$i >= 0 and not $type eq "cdata" && _foreign($current)
!ll&&!rl&&r
7115
$type eq 'cdata' && _foreign($current)
!ll&&!rl&&r
321
MC/DC100%3/3
$i >= 0 and not $type eq "cdata" && _foreign($current)
$i >= 0 $type eq 'cdata' _foreign($current)
Truth table100%4/4
A and not(B && C)
A= $i >= 0B= $type eq 'cdata'C= _foreign($current)
ABCresult
0XX0
1110
10X1
1101
160 5 pos($html) = $-[1] + $i + 1;
161 5 $value = substr $value, 0, $i;
162 }
163  
164 77 _node($current, $type, $value);
165 77 next;
166 }
167  
168 # Without an end for this one, there is none for later ones either (DOCTYPE can fail for other reasons)
169 3 ▶ $unclosed{$type} = 1 unless $type eq 'doctype';
Branch: 1 branch
BranchTrueFalseTotal
unless $type eq "doctype" 3 0 3
170 }
171 }
172  
173 # Tag
174 1920 ▶ if ($html =~ /\G$TAG_RE/gco) {
Branch: 1 branch
BranchTrueFalseTotal
if ($html =~ /\G$TAG_RE/cog) 1895 25 1920
175 1895 ▶ my ($is_end, $tag) = (defined $1, $xml ? $2 : lc $2);
Branch: 1 branch
BranchTrueFalseTotal
$xml ? : 214 1681 1895
176  
177 # Stray "=" right after the tag name (like an empty attribute name)
178 1895 $html =~ /\G=\s*/gc;
179  
180 # Attributes
181 1895 my (%attrs, $closing);
182 1895 ▶ while ($html =~ /\G$ATTR_RE/gco) {
Branch: 1 branch
BranchTrueFalseTotal
if ($html =~ /\G$ATTR_RE/cog) 33157 1895 35052
183 33157 ▶ my ($key, $value) = ($xml ? $1 : lc $1, defined $2 ? $2 : defined $3 ? $3 : $4);
Branch: 3 branches
BranchTrueFalseTotal
$xml ? : 52 33105 33157
defined $2 ? : 213 32944 33157
defined $3 ? : 16 32928 32944
184  
185 # Empty tag
186 33157 ▶ ++$closing and next if $key eq '/';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "/" 58 33099 33157
Condition60%3/5
$key eq '/' && (++$closing && (next))
!ll
33099580
++$closing and next
!ll
058
MC/DC0%0/2
$key eq '/' && (++$closing && (next))
$key eq '/' ++$closing
Truth table33%1/3
A && B
A= $key eq '/'B= ++$closing
ABresult
0X0
100
111
187  
188 33099 ▶ $attrs{$key} = defined $value ? html_attr_unescape $value : $value;
Branch: 1 branch
BranchTrueFalseTotal
defined $value ? : 33061 38 33099
189 }
190  
191 # Incomplete tag
192 1895 ▶ if ($html !~ /\G>/gc) { pos($html) = $offset }
Branch: 2 branches
BranchTrueFalseTotal
if (not $html =~ /\G>/cg) { } 9 1886 1895
elsif ($is_end) { } 785 1101 1886
193  
194 # End
195 elsif ($is_end) {
196  
197 # No more content
198 785 ▶ if (!$xml && (my $tags = $NO_MORE_CONTENT{$tag})) { _end($_, $xml, \$current) for @$tags }
Branch: 1 branch
BranchTrueFalseTotal
if (not $xml and my $tags = $NO_MORE_CONTENT{$tag}) 11 774 785
Condition100%3/3
not $xml and my $tags = $NO_MORE_CONTENT{$tag}
!ll&&!rl&&r
9567911
MC/DC100%2/2
not $xml and my $tags = $NO_MORE_CONTENT{$tag}
not $xml my $tags = $NO_MORE_CONTENT{$tag}
Truth table100%3/3
not(A) and B
A= not $xmlB= my $tags = $NO_MORE_CONTENT{$tag}
ABresult
0X0
100
111
199  
200 785 _end($tag, $xml, \$current);
201 785 next;
202 }
203  
204 # Start
205 else {
206  
207 # "image" is an alias for "img"
208 1101 ▶ $tag = 'img' if !$xml && $tag eq 'image';
Branch: 1 branch
BranchTrueFalseTotal
if not $xml and $tag eq "image" 1 1100 1101
Condition100%3/3
not $xml and $tag eq "image"
!ll&&!rl&&r
1189821
MC/DC100%2/2
not $xml and $tag eq "image"
not $xml $tag eq "image"
Truth table100%3/3
not(A) and B
A= not $xmlB= $tag eq "image"
ABresult
0X0
100
111
209 1101 _start($tag, \%attrs, $xml, \$current);
210  
211 # Element without end tag (self-closing)
212 1101 ▶ _end($tag, $xml, \$current) if !$xml && $EMPTY{$tag} || ($xml || !$BLOCK{$tag}) && $closing;
Branch: 1 branch
BranchTrueFalseTotal
if not $xml and $EMPTY{$tag} or $xml || !$BLOCK{$tag} and $closing 94 1007 1101
Condition100%12/12
not $xml and $EMPTY{$tag} or $xml || !$BLOCK{$tag} and $closing
l!l&&r!l&&!r
71231007
not $xml and $EMPTY{$tag}
!ll&&!rl&&r
11891271
$xml || !$BLOCK{$tag} and $closing
!ll&&!rl&&r
85615123
$xml or not $BLOCK{$tag}
l!l&&r!l&&!r
11856856
MC/DC80%4/5
not $xml and $EMPTY{$tag} or $xml || !$BLOCK{$tag} and $closing
not $xml $EMPTY{$tag} $xml not $BLOCK{$tag} $closing
Truth table54%6/11
not(A) and B or C or not(D) and E
A= not $xmlB= $EMPTY{$tag}C= $xmlD= not $BLOCK{$tag}E= $closing
ABCDEresult
11XXX1
0X1X11
0X0111
101X11
100111
0X00X0
0X1X00
0X0100
1000X0
101X00
100100
213  
214 # Raw text elements (not in foreign content)
215 1101 ▶ next if $xml || !$RAW{$tag} && !$RCDATA{$tag} || _foreign($current->[3]);
Branch: 1 branch
BranchTrueFalseTotal
if $xml or not $RAW{$tag} || $RCDATA{$tag} or _foreign($current->[3]) 1035 66 1101
Condition100%9/9
$xml or not $RAW{$tag} || $RCDATA{$tag} or _foreign($current->[3])
l!l&&r!l&&!r
1030566
$xml or not $RAW{$tag} || $RCDATA{$tag}
l!l&&r!l&&!r
11891271
$RAW{$tag} || $RCDATA{$tag}
l!l&&r!l&&!r
4031912
MC/DC100%4/4
$xml or not $RAW{$tag} || $RCDATA{$tag} or _foreign($current->[3])
$xml $RAW{$tag} $RCDATA{$tag} _foreign($current->[3])
Truth table100%6/6
A or not(B || C) or D
A= $xmlB= $RAW{$tag}C= $RCDATA{$tag}D= _foreign($current->[3])
ABCDresult
1XXX1
000X1
01X11
00111
01X00
00100
216  
217 66 ▶ if ($tag eq 'script') {
Branch: 1 branch
BranchTrueFalseTotal
if ($tag eq "script") 26 40 66
218 26 my ($script, $found_end) = _script_content(\$html);
219 26 ▶ _node($current, 'raw', $script) if length $script;
Branch: 1 branch
BranchTrueFalseTotal
if length $script 20 6 26
220 26 ▶ _end($tag, 0, \$current) if $found_end;
Branch: 1 branch
BranchTrueFalseTotal
if $found_end 25 1 26
221 26 next;
222 }
223  
224 # Without an end tag for this element, there is none for later elements of the same type either
225 40 ▶ next if $unclosed{$tag};
Branch: 1 branch
BranchTrueFalseTotal
if $unclosed{$tag} 1 39 40
226 39 ▶ unless ($html =~ m!\G(.*?)</\Q$tag\E(?=[\s/>])[^>]*>?!gcsi) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($html =~ m[\G(.*?)</\Q$tag\E(?=[\s/>])[^>]*>?]cgis) 1 38 39
227 1 $unclosed{$tag} = 1;
228 1 next;
229 }
230 38 ▶ _node($current, $RCDATA{$tag} ? ('text', html_unescape $1) : ('raw', $1));
Branch: 1 branch
BranchTrueFalseTotal
$RCDATA{$tag} ? : 28 10 38
231 38 _end($tag, 0, \$current);
232 38 next;
233 }
234 }
235  
236 # Runaway "<"
237 34 _node($current, 'text', '<');
238 34 pos($html) = $offset + 1;
239 }
240  
241 305 return $self->tree($tree);
242 }
243  
244 184 ▶sub render { _render($_[0]->tree, $_[0]->xml) }
Subroutine render: called CC=1 SCAR=0.0
Pod undocumented
245  
246 1270 ▶sub _end {
Subroutine _end: called CC=9 SCAR=22.0
247 1270 my ($end, $xml, $current) = @_;
248  
249 # Search stack for start tag
250 1270 my $next = $$current;
251 1270 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$next = $next->[3] and do { do { return if $next->[0] eq "root"; return if $SCOPE{$next->[1]} and $next->[1] ne $end; return $$current = $next->[3] if $next->[1] eq $end; return if not $xml and $PHRASING{$end} and not $PHRASING{$next->[1]} } } 5324 0 5324
252  
253 # Ignore useless end tag
254 6594 ▶ return if $next->[0] eq 'root';
Branch: 1 branch
BranchTrueFalseTotal
if $next->[0] eq "root" 264 6330 6594
255  
256 # Don't traverse a container tag
257 6330 ▶ return if $SCOPE{$next->[1]} && $next->[1] ne $end;
Branch: 1 branch
BranchTrueFalseTotal
if $SCOPE{$next->[1]} and $next->[1] ne $end 3 6327 6330
Condition100%3/3
$SCOPE{$next->[1]} and $next->[1] ne $end
!ll&&!rl&&r
6317103
MC/DC100%2/2
$SCOPE{$next->[1]} and $next->[1] ne $end
$SCOPE{$next->[1]} $next->[1] ne $end
Truth table100%3/3
A and B
A= $SCOPE{$next->[1]}B= $next->[1] ne $end
ABresult
0X0
100
111
258  
259 # Right tag
260 6327 ▶ return $$current = $next->[3] if $next->[1] eq $end;
Branch: 1 branch
BranchTrueFalseTotal
if $next->[1] eq $end 997 5330 6327
261  
262 # Phrasing content can only cross phrasing content
263 5330 ▶ return if !$xml && $PHRASING{$end} && !$PHRASING{$next->[1]};
Branch: 1 branch
BranchTrueFalseTotal
if not $xml and $PHRASING{$end} and not $PHRASING{$next->[1]} 6 5324 5330
Condition83%5/6
not $xml and $PHRASING{$end} and not $PHRASING{$next->[1]}
!ll&&!rl&&r
532406
not $xml and $PHRASING{$end}
!ll&&!rl&&r
1953056
MC/DC66%2/3
not $xml and $PHRASING{$end} and not $PHRASING{$next->[1]}
not $xml $PHRASING{$end} not $PHRASING{$next->[1]}
Truth table75%3/4
not(A) and B and not(C)
A= not $xmlB= $PHRASING{$end}C= not $PHRASING{$next->[1]}
ABCresult
0XX0
10X0
1100
1111
264  
265 } while $next = $next->[3];
266 }
267  
268 74 ▶sub _foreign {
Subroutine _foreign: called CC=7 SCAR=19.5
269 74 my $next = shift;
270  
271 # Search stack for SVG and MathML elements, but stop at HTML integration points
272 74 ▶ while ($next->[0] ne 'root') {
Branch: 1 branch
BranchTrueFalseTotal
if ($next->[0] ne "root") 80 64 144
273 80 ▶ return 1 if $SCOPE{$next->[1]};
Branch: 1 branch
BranchTrueFalseTotal
if $SCOPE{$next->[1]} 6 74 80
274 74 ▶ return 0 if $INTEGRATION{$next->[1]};
Branch: 1 branch
BranchTrueFalseTotal
if $INTEGRATION{$next->[1]} 3 71 74
275 return 0 if $next->[1] eq 'annotation-xml' && defined $next->[2]{encoding}
276 71 ▶ && $next->[2]{encoding} =~ m!^(?:text/html|application/xhtml\+xml)$!i;
Branch: 1 branch
BranchTrueFalseTotal
if $next->[1] eq "annotation-xml" and defined $next->[2]{"encoding"} and $next->[2]{"encoding"} =~ m[^(?:text/html|application/xhtml\+xml)$]i 1 70 71
Condition83%5/6
$next->[1] eq "annotation-xml" and defined $next->[2]{"encoding"} and $next->[2]{"encoding"} =~ m[^(?:text/html|application/xhtml\+xml)$]i
!ll&&!rl&&r
7001
$next->[1] eq "annotation-xml" and defined $next->[2]{"encoding"}
!ll&&!rl&&r
6911
MC/DC66%2/3
$next->[1] eq "annotation-xml" and defined $next->[2]{"encoding"} and $next->[2]{"encoding"} =~ m[^(?:text/html|application/xhtml\+xml)$]i
$next->[1] eq "annotation-xml" defined $next->[2]{"encoding"} $next->[2]{"encoding"} =~ m[^(?:text/html|application/xhtml\+xml)$]i
Truth table75%3/4
A and B and C
A= $next->[1] eq "annotation-xml"B= defined $next->[2]{"encoding"}C= $next->[2]{"encoding"} =~ m[^(?:text/html|application/xhtml\+xml)$]i
ABCresult
0XX0
10X0
1100
1111
277 70 $next = $next->[3];
278 }
279  
280 64 return 0;
281 }
282  
283 1570 ▶sub _node {
Subroutine _node: called CC=1 SCAR=0.0
284 1570 my ($current, $type, $content) = @_;
285 1570 push @$current, my $new = [$type, $content, $current];
286 1570 weaken $new->[2];
287 }
288  
289 1112 ▶sub _render {
Subroutine _render: called CC=16 SCAR=27.8
290 1112 my ($tree, $xml) = @_;
291  
292 # Tag
293 1112 my $type = $tree->[0];
294 1112 ▶ if ($type eq 'tag') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "tag") 476 636 1112
295  
296 # Start tag
297 476 my $tag = $tree->[1];
298 476 my $result = "<$tag";
299  
300 # Attributes
301 476 for my $key (sort keys %{$tree->[2]}) {
302 71 my $value = $tree->[2]{$key};
303 71 ▶ $result .= $xml ? qq{ $key="$key"} : " $key" and next
Branch: 2 branches
BranchTrueFalseTotal
unless defined $value 13 58 71
$xml ? : 2 11 13
Condition60%3/5
defined $value || ($result .= ($xml ? qq[ $key="$key"] : " $key")) && (next)
l!l
58130
$result .= ($xml ? qq[ $key="$key"] : " $key") and next
!ll
013
MC/DC0%0/2
defined $value || ($result .= ($xml ? qq[ $key="$key"] : " $key")) && (next)
defined $value $result .= ($xml ? qq[ $key="$key"] : " $key")
Truth table66%2/3
A || B
A= defined $valueB= $result .= ($xml ? qq[ $key="$key"] : " $key")
ABresult
1X1
011
000
304 unless defined $value;
305 58 $result .= qq{ $key="} . html_escape($value) . '"';
306 }
307  
308 # No children
309 476 ▶ return $xml ? "$result />" : $EMPTY{$tag} ? "$result>" : "$result></$tag>"
Branch: 3 branches
BranchTrueFalseTotal
unless $tree->[4] 48 428 476
$xml ? : 5 43 48
$EMPTY{$tag} ? : 18 25 43
310 unless $tree->[4];
311  
312 # Children
313 2 ▶ no warnings 'recursion';
Subroutine BEGIN: called CC=1 SCAR=0.0
314 428 $result .= '>' . join '', map { _render($_, $xml) } @$tree[4 .. $#$tree];
315  
316 # End tag
317 428 return "$result</$tag>";
318 }
319  
320 # Text (escaped)
321 636 ▶ return html_escape($tree->[1]) if $type eq 'text';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "text" 451 185 636
322  
323 # Raw text
324 185 ▶ return $tree->[1] if $type eq 'raw';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "raw" 9 176 185
325  
326 # Root
327 176 ▶ return join '', map { _render($_, $xml) } @$tree[1 .. $#$tree]
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "root" 156 20 176
328 if $type eq 'root';
329  
330 # DOCTYPE
331 20 ▶ return '<!DOCTYPE' . $tree->[1] . '>' if $type eq 'doctype';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "doctype" 5 15 20
332  
333 # Comment
334 15 ▶ return '<!--' . $tree->[1] . '-->' if $type eq 'comment';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "comment" 4 11 15
335  
336 # CDATA
337 11 ▶ return '<![CDATA[' . $tree->[1] . ']]>' if $type eq 'cdata';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "cdata" 6 5 11
338  
339 # Processing instruction
340 5 ▶ return '<?' . $tree->[1] . '?>' if $type eq 'pi';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "pi" 5 0 5
341  
342 # Everything else
343 0 return '';
344 }
345  
346 26 ▶sub _script_content {
Subroutine _script_content: called CC=12 SCAR=24.9
347 26 my $html = shift;
348 26 my $start = pos $$html;
349  
350 26 my $state = 0;
351  
352 26 while (1) {
353 56 ▶ if ($state == 0) { $$html =~ /\G[^<]*/gcs }
Branch: 1 branch
BranchTrueFalseTotal
if ($state == 0) { } 43 13 56
354 13 else { $$html =~ /\G[^<\-]*/gcs }
355  
356 56 my $p = pos $$html;
357 56 ▶ return (substr($$html, $start), 0) if $p >= length $$html;
Branch: 1 branch
BranchTrueFalseTotal
if $p >= length $$html 1 55 56
358  
359 55 ▶ if ($state == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($state == 0) { } 42 13 55
elsif ($state == 1) { } 10 3 13
360 42 ▶ if ($$html =~ m!\G</script(?=[\s/>])[^>]*>?!gcsi) { return (substr($$html, $start, $p - $start), 1) }
Branch: 2 branches
BranchTrueFalseTotal
if ($$html =~ m[\G</script(?=[\s/>])[^>]*>?]cgis) { } 23 19 42
elsif ($$html =~ /\G<!--/cgs) { } 6 13 19
361 6 elsif ($$html =~ /\G<!--/gcs) { $state = 1 }
362 13 else { pos($$html) = $p + 1 }
363 }
364 elsif ($state == 1) {
365 10 ▶ if ($$html =~ m!\G</script(?=[\s/>])[^>]*>?!gcsi) { return (substr($$html, $start, $p - $start), 1) }
Branch: 3 branches
BranchTrueFalseTotal
if ($$html =~ m[\G</script(?=[\s/>])[^>]*>?]cgis) { } 2 8 10
elsif ($$html =~ /\G-->/cgs) { } 3 5 8
elsif ($$html =~ m[\G<script(?=[\s/>])]cgis) { } 3 2 5
366 3 elsif ($$html =~ /\G-->/gcs) { $state = 0 }
367 3 elsif ($$html =~ m!\G<script(?=[\s/>])!gcsi) { $state = 2 }
368 2 else { pos($$html) = $p + 1 }
369 }
370 else {
371 3 ▶ if ($$html =~ m!\G</script(?=[\s/>])!gcsi) { $state = 1 }
Branch: 2 branches
BranchTrueFalseTotal
if ($$html =~ m[\G</script(?=[\s/>])]cgis) { } 2 1 3
elsif ($$html =~ /\G-->/cgs) { } 1 0 1
372 1 elsif ($$html =~ /\G-->/gcs) { $state = 0 }
373 0 else { pos($$html) = $p + 1 }
374 }
375 }
376 }
377  
378 1101 ▶sub _start {
Subroutine _start: called CC=8 SCAR=20.8
379 1101 my ($start, $attrs, $xml, $current) = @_;
380  
381 # Autoclose optional HTML elements
382 1101 ▶ if (!$xml && $$current->[0] ne 'root') {
Branch: 1 branch
BranchTrueFalseTotal
if (not $xml and $$current->[0] ne "root") 711 390 1101
Condition100%3/3
not $xml and $$current->[0] ne "root"
!ll&&!rl&&r
118272711
MC/DC100%2/2
not $xml and $$current->[0] ne "root"
not $xml $$current->[0] ne "root"
Truth table100%3/3
not(A) and B
A= not $xmlB= $$current->[0] ne "root"
ABresult
0X0
100
111
383 711 ▶ if (my $end = $END{$start}) { _end($end, 0, $current) }
Branch: 2 branches
BranchTrueFalseTotal
if (my $end = $END{$start}) { } 256 455 711
elsif (my $close = $CLOSE{$start}) { } 152 303 455
384  
385 elsif (my $close = $CLOSE{$start}) {
386 152 my ($allowed, $scope) = @$close;
387  
388 # Close allowed parent elements in scope
389 152 my $parent = $$current;
390 152 ▶ while ($parent->[0] ne 'root' && !$scope->{$parent->[1]}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parent->[0] ne "root" and not $$scope{$parent->[1]}) 148 152 300
Condition66%2/3
$parent->[0] ne "root" and not $$scope{$parent->[1]}
!ll&&!rl&&r
0152148
MC/DC50%1/2
$parent->[0] ne "root" and not $$scope{$parent->[1]}
$parent->[0] ne "root" not $$scope{$parent->[1]}
Truth table66%2/3
A and not(B)
A= $parent->[0] ne "root"B= not $$scope{$parent->[1]}
ABresult
0X0
100
111
391 148 ▶ _end($parent->[1], 0, $current) if $allowed->{$parent->[1]};
Branch: 1 branch
BranchTrueFalseTotal
if $$allowed{$parent->[1]} 50 98 148
392 148 $parent = $parent->[3];
393 }
394 }
395 }
396  
397 # New tag
398 1101 push @$$current, my $new = ['tag', $start, $attrs, $$current];
399 1101 weaken $new->[3];
400 1101 $$current = $new;
401 }
402  
403 14 ▶sub _tag {
Subroutine _tag: called CC=5 SCAR=16.1
404 14 my $tree = ['tag', shift, undef, undef];
405  
406 # Content
407 14 ▶ push @$tree, ref $_[-1] eq 'CODE' ? ['raw', pop->()] : ['text', pop]
Branch: 2 branches
BranchTrueFalseTotal
if @_ % 2 8 6 14
ref $_[-1] eq 'CODE' ? : 4 4 8
408 if @_ % 2;
409  
410 # Attributes
411 14 my $attrs = $tree->[2] = {@_};
412 14 ▶ return $tree unless exists $attrs->{data} && ref $attrs->{data} eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless exists $attrs->{"data"} and ref $attrs->{"data"} eq "HASH" 13 1 14
Condition66%2/3
exists $attrs->{"data"} and ref $attrs->{"data"} eq "HASH"
!ll&&!rl&&r
1301
MC/DC50%1/2
exists $attrs->{"data"} and ref $attrs->{"data"} eq "HASH"
exists $attrs->{"data"} ref $attrs->{"data"} eq "HASH"
Truth table66%2/3
A and B
A= exists $attrs->{"data"}B= ref $attrs->{"data"} eq "HASH"
ABresult
0X0
100
111
413 1 my $data = delete $attrs->{data};
414 1 @$attrs{map { y/_/-/; lc "data-$_" } keys %$data} = values %$data;
415  
416 1 return $tree;
417 }
418  
419 2 1;
420  
421 =for Pod::Coverage *EVERYTHING*
422  
423 =cut
↑ Summary DOM58/_CSS.pm »