Mojo/DOM/HTML.pm

filter: Statementstmt 98.9% 191 / 193 Branchbran 96.5% 139 / 144 Conditioncond 90.3% 75 / 83 MC/DCmcdc 79.5% 35 / 44 Subroutinesub 100% 17 / 17 Podpod 100% 4 / 4 totaltotal 95.0% 461 / 485 CC 102 SCAR 33.1
CC
102
Cov
96%
parse
51.1
_render
16.1
_script_content
12.0
CRAP 27.4
« Mojolicious/Plugin/HeaderCondition.pm ↑ Summary Mojolicious/Command/Author/inflate.pm »
1 package Mojo::DOM::HTML;
2 31 ▶use Mojo::Base -base;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 31 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 31 ▶use Mojo::Util qw(html_attr_unescape html_unescape xml_escape);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 31 ▶use Scalar::Util qw(weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 31 our @EXPORT_OK = ('tag_to_html');
9  
10 5 ▶has tree => sub { ['root'] };
Subroutine __ANON__: called CC=1 SCAR=0.0
11 31 has 'xml';
12  
13 31 my $ATTR_RE = qr/
14 ([^<>=\s\/0-9.\-][^<>=\s\/]*|\/) # Key
15 (?:
16 \s*=\s*
17 (?s:(["'])(.*?)\g{-2}|([^>\s]*)) # Value
18 )?
19 \s*
20 /x;
21 31 my %MARKUP = (
22 doctype => qr/
23 \G<!DOCTYPE(
24 \s+\w+ # Doctype
25 (?:(?:\s+\w+)?(?:\s+(?:"[^"]*"|'[^']*'))+)? # External ID
26 (?:\s+\[.+?\])? # Int Subset
27 \s*)>
28 /xis,
29 comment => qr/\G<!--(.*?)(?:--!?|(?<=<!--)-?(?=>))>/s,
30 cdata => qr/\G<!\[CDATA\[(.*?)\]\]>/is,
31 pi => qr/\G<\?(.*?)\?>/s
32 );
33 31 my $TAG_RE = qr/<(\/\s*)?([^<>\s\/0-9.\-][^<>\s\/]*)\s*/;
34  
35 # HTML elements that only contain raw text
36 31 my %RAW = map { $_ => 1 } qw(script style);
37  
38 # HTML elements that only contain raw text and entities
39 31 my %RCDATA = map { $_ => 1 } qw(title textarea);
40  
41 # HTML elements with optional end tags
42 31 my %END = (body => 'head', optgroup => 'optgroup', option => 'option');
43  
44 # HTML elements that break paragraphs
45 31 map { $END{$_} = 'p' } (
46 qw(address article aside blockquote details dialog div dl fieldset figcaption figure footer form h1 h2 h3 h4 h5 h6),
47 qw(header hgroup hr main menu nav ol p pre section table ul)
48 );
49  
50 # Container HTML elements that create their own scope
51 31 my %SCOPE = map { $_ => 1 } qw(math svg);
52  
53 # SVG and MathML elements that are HTML integration points
54 31 my %INTEGRATION = map { $_ => 1 } qw(desc foreignobject mi mn mo ms mtext title);
55  
56 # HTML table elements with optional end tags
57 31 my %TABLE = map { $_ => 1 } qw(colgroup tbody td tfoot th thead tr);
58  
59 # HTML elements with optional end tags and scoping rules
60 31 my %CLOSE = (li => [{li => 1}, {ul => 1, ol => 1}], tr => [{tr => 1}, {table => 1}]);
61 31 $CLOSE{$_} = [\%TABLE, {table => 1}] for qw(colgroup tbody tfoot thead);
62 31 $CLOSE{$_} = [{dd => 1, dt => 1}, {dl => 1}] for qw(dd dt);
63 31 $CLOSE{$_} = [{rp => 1, rt => 1}, {ruby => 1}] for qw(rp rt);
64 31 $CLOSE{$_} = [{th => 1, td => 1}, {table => 1}] for qw(td th);
65  
66 # HTML parent elements that signal no more content when closed, but that are also phrasing content
67 31 my %NO_MORE_CONTENT = (ruby => [qw(rt rp)], select => [qw(option optgroup)]);
68  
69 # HTML elements without end tags
70 31 my %EMPTY = map { $_ => 1 } qw(area base br col embed hr img input keygen link menuitem meta param source track wbr);
71  
72 # HTML elements categorized as phrasing content (and obsolete inline elements)
73 31 my @PHRASING = (
74 qw(a abbr area audio b bdi bdo br button canvas cite code data datalist del dfn em embed i iframe img input ins kbd),
75 qw(keygen label link map mark math meta meter noscript object output picture progress q ruby s samp script select),
76 qw(slot small span strong sub sup svg template textarea time u var video wbr)
77 );
78 31 my @OBSOLETE = qw(acronym applet basefont big font strike tt);
79 31 my %PHRASING = map { $_ => 1 } @OBSOLETE, @PHRASING;
80  
81 # HTML elements that don't get their self-closing flag acknowledged
82 31 my %BLOCK = map { $_ => 1 } (
83 qw(a address applet article aside b big blockquote body button caption center code col colgroup dd details dialog),
84 qw(dir div dl dt em fieldset figcaption figure font footer form frameset h1 h2 h3 h4 h5 h6 head header hgroup html),
85 qw(i iframe li listing main marquee menu nav nobr noembed noframes noscript object ol optgroup option p plaintext),
86 qw(pre rp rt s script section select small strike strong style summary table tbody td template textarea tfoot th),
87 qw(thead title tr tt u ul xmp)
88 );
89  
90 315 ▶sub parse {
Subroutine parse: called CC=51 SCAR=39.3
Pod documented
91 315 my ($self, $html) = (shift, "$_[0]");
92  
93 # Detect XML from a leading declaration
94 315 ▶ $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?]/iu 15 300 315
Condition100%3/3
not exists $self->{"xml"} and $html =~ /^\x{feff}?\s*<\?xml[\s?]/iu
!ll&&!rl&&r
9021015
MC/DC100%2/2
not exists $self->{"xml"} and $html =~ /^\x{feff}?\s*<\?xml[\s?]/iu
not exists $self->{"xml"} $html =~ /^\x{feff}?\s*<\?xml[\s?]/iu
Truth table100%3/3
not(A) and B
A= not exists $self->{"xml"}B= $html =~ /^\x{feff}?\s*<\?xml[\s?]/iu
ABresult
0X0
100
111
95  
96 315 my $xml = $self->xml;
97 315 my $current = my $tree = ['root'];
98 315 my %unclosed;
99 315 ▶ while ((my $offset = pos($html) // 0) < length $html) {
Branch: 1 branch
BranchTrueFalseTotal
if ((my $offset = pos $html // 0) < length $html) 3433 315 3748
Condition100%2/2
pos $html // 0
l!l
3433315
MC/DC100%1/1
pos $html // 0
pos $html
Truth table100%2/2
A
A= pos $html
Aresult
11
00
100  
101 # Text
102 3433 ▶ if ($html =~ /\G([^<]+)/gc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($html =~ /\G([^<]+)/cgu) 1418 2015 3433
103 1418 _node($current, 'text', html_unescape $1);
104 1418 next;
105 }
106  
107 # DOCTYPE, comment, CDATA and processing instruction (only trying the regex for the opener keeps failures fast)
108 2015 my $next = substr $html, $offset + 1, 1;
109 2015 ▶ if ($next eq '!' || $next eq '?') {
Branch: 1 branch
BranchTrueFalseTotal
if ($next eq "!" or $next eq "?") 83 1932 2015
Condition100%3/3
$next eq "!" or $next eq "?"
l!l&&r!l&&!r
53301932
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
110 83 my $opener = uc substr $html, $offset, 9;
111 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
112 = substr($opener, 0, 4) eq '<!--' ? 'comment'
113 : $opener eq '<!DOCTYPE' ? 'doctype'
114 : $opener eq '<![CDATA[' ? 'cdata'
115 : $next eq '?' ? 'pi'
116 : undef;
117  
118 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
119 80 ▶ if ($html =~ /$MARKUP{$type}/gc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($html =~ /$MARKUP{$type}/cgu) 77 3 80
120 77 my $value = $1;
121  
122 # DOCTYPE, CDATA (outside of foreign content) and processing instructions end at the first ">" in HTML
123 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
124 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
125 5 pos($html) = $-[1] + $i + 1;
126 5 $value = substr $value, 0, $i;
127 }
128  
129 77 _node($current, $type, $value);
130 77 next;
131 }
132  
133 # Without an end for this one, there is none for later ones either (DOCTYPE can fail for other reasons)
134 3 ▶ $unclosed{$type} = 1 unless $type eq 'doctype';
Branch: 1 branch
BranchTrueFalseTotal
unless $type eq "doctype" 3 0 3
135 }
136 }
137  
138 # Tag
139 1938 ▶ if ($html =~ /\G$TAG_RE/gco) {
Branch: 1 branch
BranchTrueFalseTotal
if ($html =~ /\G$TAG_RE/cgou) 1913 25 1938
140 1913 ▶ my ($is_end, $tag) = (defined $1, $xml ? $2 : lc $2);
Branch: 1 branch
BranchTrueFalseTotal
$xml ? : 214 1699 1913
141  
142 # Stray "=" right after the tag name (like an empty attribute name)
143 1913 $html =~ /\G=\s*/gc;
144  
145 # Attributes
146 1913 my (%attrs, $closing);
147 1913 ▶ while ($html =~ /\G$ATTR_RE/gco) {
Branch: 1 branch
BranchTrueFalseTotal
if ($html =~ /\G$ATTR_RE/cgou) 33165 1913 35078
148 33165 ▶ my ($key, $value) = ($xml ? $1 : lc $1, $3 // $4);
Branch: 1 branch
BranchTrueFalseTotal
$xml ? : 52 33113 33165
Condition100%3/3
$3 // $4
l!l&&r!l&&!r
2373282999
MC/DC100%2/2
$3 // $4
$3 $4
Truth table100%3/3
A // B
A= $3B= $4
ABresult
1X1
011
000
149  
150 # Empty tag
151 33165 ▶ ++$closing and next if $key eq '/';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "/" 58 33107 33165
Condition60%3/5
$key eq '/' && (++$closing && (next))
!ll&&!rl&&r
33107580
++$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
152  
153 33107 ▶ $attrs{$key} = defined $value ? html_attr_unescape $value : $value;
Branch: 1 branch
BranchTrueFalseTotal
defined $value ? : 33069 38 33107
154 }
155  
156 # Incomplete tag
157 1913 ▶ if ($html !~ /\G>/gc) { pos($html) = $offset }
Branch: 2 branches
BranchTrueFalseTotal
if (not $html =~ /\G>/cgu) { } 9 1904 1913
elsif ($is_end) { } 794 1110 1904
158  
159 # End
160 elsif ($is_end) {
161  
162 # No more content
163 794 ▶ 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 783 794
Condition100%3/3
not $xml and my $tags = $NO_MORE_CONTENT{$tag}
!ll&&!rl&&r
9568811
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
164  
165 794 _end($tag, $xml, \$current);
166 794 next;
167 }
168  
169 # Start
170 else {
171  
172 # "image" is an alias for "img"
173 1110 ▶ $tag = 'img' if !$xml && $tag eq 'image';
Branch: 1 branch
BranchTrueFalseTotal
if not $xml and $tag eq "image" 1 1109 1110
Condition100%3/3
not $xml and $tag eq "image"
!ll&&!rl&&r
1189911
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
174 1110 _start($tag, \%attrs, $xml, \$current);
175  
176 # Element without end tag (self-closing)
177 1110 ▶ _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 1016 1110
Condition100%12/12
not $xml and $EMPTY{$tag} or $xml || !$BLOCK{$tag} and $closing
l!l&&r!l&&!r
71231016
not $xml and $EMPTY{$tag}
!ll&&!rl&&r
11892171
$xml || !$BLOCK{$tag} and $closing
!ll&&!rl&&r
86515123
$xml or not $BLOCK{$tag}
l!l&&r!l&&!r
11856865
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
178  
179 # Raw text elements (not in foreign content)
180 1110 ▶ 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]) 1044 66 1110
Condition100%9/9
$xml or not $RAW{$tag} || $RCDATA{$tag} or _foreign($current->[3])
l!l&&r!l&&!r
1039566
$xml or not $RAW{$tag} || $RCDATA{$tag}
l!l&&r!l&&!r
11892171
$RAW{$tag} || $RCDATA{$tag}
l!l&&r!l&&!r
4031921
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
181  
182 66 ▶ if ($tag eq 'script') {
Branch: 1 branch
BranchTrueFalseTotal
if ($tag eq "script") 26 40 66
183 26 my ($script, $found_end) = _script_content(\$html);
184 26 ▶ _node($current, 'raw', $script) if length $script;
Branch: 1 branch
BranchTrueFalseTotal
if length $script 20 6 26
185 26 ▶ _end($tag, 0, \$current) if $found_end;
Branch: 1 branch
BranchTrueFalseTotal
if $found_end 25 1 26
186 26 next;
187 }
188  
189 # Without an end tag for this element, there is none for later elements of the same type either
190 40 ▶ next if $unclosed{$tag};
Branch: 1 branch
BranchTrueFalseTotal
if $unclosed{$tag} 1 39 40
191 39 ▶ unless ($html =~ m!\G(.*?)</\Q$tag\E(?=[\s/>])[^>]*>?!gcsi) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($html =~ m[\G(.*?)</\Q$tag\E(?=[\s/>])[^>]*>?]cgisu) 1 38 39
192 1 $unclosed{$tag} = 1;
193 1 next;
194 }
195 38 ▶ _node($current, $RCDATA{$tag} ? ('text', html_unescape $1) : ('raw', $1));
Branch: 1 branch
BranchTrueFalseTotal
$RCDATA{$tag} ? : 28 10 38
196 38 _end($tag, 0, \$current);
197 38 next;
198 }
199 }
200  
201 # Runaway "<"
202 34 _node($current, 'text', '<');
203 34 pos($html) = $offset + 1;
204 }
205  
206 315 return $self->tree($tree);
207 }
208  
209 183 ▶sub render { _render($_[0]->tree, $_[0]->xml) }
Subroutine render: called CC=1 SCAR=0.0
Pod documented
210  
211 12 ▶sub tag { shift->tree(['root', _tag(@_)]) }
Subroutine tag: called CC=1 SCAR=0.0
Pod documented
212  
213 147 ▶sub tag_to_html { _render(_tag(@_), undef) }
Subroutine tag_to_html: called CC=1 SCAR=0.0
Pod documented
214  
215 1279 ▶sub _end {
Subroutine _end: called CC=9 SCAR=22.0
216 1279 my ($end, $xml, $current) = @_;
217  
218 # Search stack for start tag
219 1279 my $next = $$current;
220 1279 ▶ 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
221  
222 # Ignore useless end tag
223 6603 ▶ return if $next->[0] eq 'root';
Branch: 1 branch
BranchTrueFalseTotal
if $next->[0] eq "root" 264 6339 6603
224  
225 # Don’t traverse a container tag
226 6339 ▶ return if $SCOPE{$next->[1]} && $next->[1] ne $end;
Branch: 1 branch
BranchTrueFalseTotal
if $SCOPE{$next->[1]} and $next->[1] ne $end 3 6336 6339
Condition100%3/3
$SCOPE{$next->[1]} and $next->[1] ne $end
!ll&&!rl&&r
6326103
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
227  
228 # Right tag
229 6336 ▶ return $$current = $next->[3] if $next->[1] eq $end;
Branch: 1 branch
BranchTrueFalseTotal
if $next->[1] eq $end 1006 5330 6336
230  
231 # Phrasing content can only cross phrasing content
232 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
233  
234 } while $next = $next->[3];
235 }
236  
237 74 ▶sub _foreign {
Subroutine _foreign: called CC=7 SCAR=19.5
238 74 my $next = shift;
239  
240 # Search stack for SVG and MathML elements, but stop at HTML integration points
241 74 ▶ while ($next->[0] ne 'root') {
Branch: 1 branch
BranchTrueFalseTotal
if ($next->[0] ne "root") 80 64 144
242 80 ▶ return 1 if $SCOPE{$next->[1]};
Branch: 1 branch
BranchTrueFalseTotal
if $SCOPE{$next->[1]} 6 74 80
243 74 ▶ return 0 if $INTEGRATION{$next->[1]};
Branch: 1 branch
BranchTrueFalseTotal
if $INTEGRATION{$next->[1]} 3 71 74
244 return 0
245 71 ▶ if $next->[1] eq 'annotation-xml' && ($next->[2]{encoding} // '') =~ m!^(?:text/html|application/xhtml\+xml)$!i;
Branch: 1 branch
BranchTrueFalseTotal
if $next->[1] eq "annotation-xml" and ($next->[2]{"encoding"} // "") =~ m[^(?:text/html|application/xhtml\+xml)$]iu 1 70 71
Condition100%5/5
$next->[1] eq "annotation-xml" and ($next->[2]{"encoding"} // "") =~ m[^(?:text/html|application/xhtml\+xml)$]iu
!ll&&!rl&&r
6911
$next->[2]{"encoding"} // ""
l!l
11
MC/DC100%2/2
$next->[1] eq "annotation-xml" and ($next->[2]{"encoding"} // "") =~ m[^(?:text/html|application/xhtml\+xml)$]iu
$next->[1] eq "annotation-xml" $next->[2]{"encoding"}
Truth table100%3/3
A and B
A= $next->[1] eq "annotation-xml"B= $next->[2]{"encoding"}
ABresult
0X0
100
111
246 70 $next = $next->[3];
247 }
248  
249 64 return 0;
250 }
251  
252 1587 ▶sub _node {
Subroutine _node: called CC=1 SCAR=0.0
253 1587 my ($current, $type, $content) = @_;
254 1587 push @$current, my $new = [$type, $content, $current];
255 1587 weaken $new->[2];
256 }
257  
258 1309 ▶sub _render {
Subroutine _render: called CC=16 SCAR=27.8
259 1309 my ($tree, $xml) = @_;
260  
261 # Tag
262 1309 my $type = $tree->[0];
263 1309 ▶ if ($type eq 'tag') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "tag") 620 689 1309
264  
265 # Start tag
266 620 my $tag = $tree->[1];
267 620 my $result = "<$tag";
268  
269 # Attributes
270 620 for my $key (sort keys %{$tree->[2]}) {
271 346 my $value = $tree->[2]{$key};
272 346 ▶ $result .= $xml ? qq{ $key="$key"} : " $key" and next unless defined $value;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $value 13 333 346
$xml ? : 2 11 13
Condition60%3/5
defined $value || ($result .= ($xml ? qq[ $key="$key"] : " $key")) && (next)
l!l&&r!l&&!r
333130
$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
273 333 $result .= qq{ $key="} . xml_escape($value) . '"';
274 }
275  
276 # No children
277 620 ▶ return $xml ? "$result />" : $EMPTY{$tag} ? "$result>" : "$result></$tag>" unless $tree->[4];
Branch: 3 branches
BranchTrueFalseTotal
unless $tree->[4] 138 482 620
$xml ? : 5 133 138
$EMPTY{$tag} ? : 109 24 133
278  
279 # Children
280 31 ▶ no warnings 'recursion';
Subroutine BEGIN: called CC=1 SCAR=0.0
281 482 $result .= '>' . join '', map { _render($_, $xml) } @$tree[4 .. $#$tree];
282  
283 # End tag
284 482 return "$result</$tag>";
285 }
286  
287 # Text (escaped)
288 689 ▶ return xml_escape $tree->[1] if $type eq 'text';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "text" 453 236 689
289  
290 # Raw text
291 236 ▶ return $tree->[1] if $type eq 'raw';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "raw" 61 175 236
292  
293 # Root
294 175 ▶ return join '', map { _render($_, $xml) } @$tree[1 .. $#$tree] if $type eq 'root';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "root" 155 20 175
295  
296 # DOCTYPE
297 20 ▶ return '<!DOCTYPE' . $tree->[1] . '>' if $type eq 'doctype';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "doctype" 5 15 20
298  
299 # Comment
300 15 ▶ return '<!--' . $tree->[1] . '-->' if $type eq 'comment';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "comment" 4 11 15
301  
302 # CDATA
303 11 ▶ return '<![CDATA[' . $tree->[1] . ']]>' if $type eq 'cdata';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "cdata" 6 5 11
304  
305 # Processing instruction
306 5 ▶ return '<?' . $tree->[1] . '?>' if $type eq 'pi';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "pi" 5 0 5
307  
308 # Everything else
309 0 return '';
310 }
311  
312 26 ▶sub _script_content {
Subroutine _script_content: called CC=12 SCAR=24.9
313 26 my $html = shift;
314 26 my $start = pos $$html;
315  
316 26 my $state = 0;
317  
318 26 while (1) {
319 56 ▶ if ($state == 0) { $$html =~ /\G[^<]*/gcs }
Branch: 1 branch
BranchTrueFalseTotal
if ($state == 0) { } 43 13 56
320 13 else { $$html =~ /\G[^<\-]*/gcs }
321  
322 56 my $p = pos $$html;
323 56 ▶ return (substr($$html, $start), 0) if $p >= length $$html;
Branch: 1 branch
BranchTrueFalseTotal
if $p >= length $$html 1 55 56
324  
325 55 ▶ if ($state == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($state == 0) { } 42 13 55
elsif ($state == 1) { } 10 3 13
326 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/>])[^>]*>?]cgisu) { } 23 19 42
elsif ($$html =~ /\G<!--/cgsu) { } 6 13 19
327 6 elsif ($$html =~ /\G<!--/gcs) { $state = 1 }
328 13 else { pos($$html) = $p + 1 }
329 }
330 elsif ($state == 1) {
331 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/>])[^>]*>?]cgisu) { } 2 8 10
elsif ($$html =~ /\G-->/cgsu) { } 3 5 8
elsif ($$html =~ m[\G<script(?=[\s/>])]cgisu) { } 3 2 5
332 3 elsif ($$html =~ /\G-->/gcs) { $state = 0 }
333 3 elsif ($$html =~ m!\G<script(?=[\s/>])!gcsi) { $state = 2 }
334 2 else { pos($$html) = $p + 1 }
335 }
336 else {
337 3 ▶ if ($$html =~ m!\G</script(?=[\s/>])!gcsi) { $state = 1 }
Branch: 2 branches
BranchTrueFalseTotal
if ($$html =~ m[\G</script(?=[\s/>])]cgisu) { } 2 1 3
elsif ($$html =~ /\G-->/cgsu) { } 1 0 1
338 1 elsif ($$html =~ /\G-->/gcs) { $state = 0 }
339 0 else { pos($$html) = $p + 1 }
340 }
341 }
342 }
343  
344 1110 ▶sub _start {
Subroutine _start: called CC=8 SCAR=20.8
345 1110 my ($start, $attrs, $xml, $current) = @_;
346  
347 # Autoclose optional HTML elements
348 1110 ▶ if (!$xml && $$current->[0] ne 'root') {
Branch: 1 branch
BranchTrueFalseTotal
if (not $xml and $$current->[0] ne "root") 715 395 1110
Condition100%3/3
not $xml and $$current->[0] ne "root"
!ll&&!rl&&r
118277715
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
349 715 ▶ if (my $end = $END{$start}) { _end($end, 0, $current) }
Branch: 2 branches
BranchTrueFalseTotal
if (my $end = $END{$start}) { } 256 459 715
elsif (my $close = $CLOSE{$start}) { } 152 307 459
350  
351 elsif (my $close = $CLOSE{$start}) {
352 152 my ($allowed, $scope) = @$close;
353  
354 # Close allowed parent elements in scope
355 152 my $parent = $$current;
356 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
357 148 ▶ _end($parent->[1], 0, $current) if $allowed->{$parent->[1]};
Branch: 1 branch
BranchTrueFalseTotal
if $$allowed{$parent->[1]} 50 98 148
358 148 $parent = $parent->[3];
359 }
360 }
361 }
362  
363 # New tag
364 1110 push @$$current, my $new = ['tag', $start, $attrs, $$current];
365 1110 weaken $new->[3];
366 1110 $$current = $new;
367 }
368  
369 159 ▶sub _tag {
Subroutine _tag: called CC=5 SCAR=16.1
370 159 my $tree = ['tag', shift, undef, undef];
371  
372 # Content
373 159 ▶ push @$tree, ref $_[-1] eq 'CODE' ? ['raw', pop->()] : ['text', pop] if @_ % 2;
Branch: 2 branches
BranchTrueFalseTotal
if @_ % 2 62 97 159
ref $_[-1] eq 'CODE' ? : 56 6 62
374  
375 # Attributes
376 159 my $attrs = $tree->[2] = {@_};
377 159 ▶ 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" 158 1 159
Condition66%2/3
exists $attrs->{"data"} and ref $attrs->{"data"} eq "HASH"
!ll&&!rl&&r
15801
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
378 1 my $data = delete $attrs->{data};
379 1 @$attrs{map { y/_/-/; lc "data-$_" } keys %$data} = values %$data;
380 1 return $tree;
381 }
382  
383 31 1;
384  
385 =encoding utf8
386  
387 =head1 NAME
388  
389 Mojo::DOM::HTML - HTML/XML engine
390  
391 =head1 SYNOPSIS
392  
393 use Mojo::DOM::HTML;
394  
395 # Turn HTML into DOM tree
396 my $html = Mojo::DOM::HTML->new;
397 $html->parse('<div><p id="a">Test</p><p id="b">123</p></div>');
398 my $tree = $html->tree;
399  
400 =head1 DESCRIPTION
401  
402 L<Mojo::DOM::HTML> is the HTML/XML engine used by L<Mojo::DOM>, based on the L<HTML Living
403 Standard|https://html.spec.whatwg.org> and the L<Extensible Markup Language (XML) 1.0|https://www.w3.org/TR/xml/>.
404  
405 =head1 FUNCTIONS
406  
407 L<Mojo::DOM::HTML> implements the following functions, which can be imported individually.
408  
409 =head2 tag_to_html
410  
411 my $str = tag_to_html 'div', id => 'foo', 'safe content';
412  
413 Generate HTML/XML tag and render it right away. This is a significantly faster alternative to L</"tag"> for template
414 systems that have to generate a lot of tags.
415  
416 =head1 ATTRIBUTES
417  
418 L<Mojo::DOM::HTML> implements the following attributes.
419  
420 =head2 tree
421  
422 my $tree = $html->tree;
423 $html = $html->tree(['root']);
424  
425 Document Object Model. Note that this structure should only be used very carefully since it is very dynamic.
426  
427 =head2 xml
428  
429 my $bool = $html->xml;
430 $html = $html->xml($bool);
431  
432 Disable HTML semantics in parser and activate case-sensitivity, defaults to auto-detection based on a leading XML
433 declaration.
434  
435 =head1 METHODS
436  
437 L<Mojo::DOM::HTML> inherits all methods from L<Mojo::Base> and implements the following new ones.
438  
439 =head2 parse
440  
441 $html = $html->parse('<foo bar="baz">I ♥ Mojolicious!</foo>');
442  
443 Parse HTML/XML fragment.
444  
445 =head2 render
446  
447 my $str = $html->render;
448  
449 Render DOM to HTML/XML.
450  
451 =head2 tag
452  
453 $html = $html->tag('div', id => 'foo', 'safe content');
454  
455 Generate HTML/XML tag.
456  
457 =head1 SEE ALSO
458  
459 L<Mojolicious>, L<Mojolicious::Guides>, L<https://mojolicious.org>.
460  
461 =cut
« Mojolicious/Plugin/HeaderCondition.pm ↑ Summary Mojolicious/Command/Author/inflate.pm »