Mojo/DOM.pm

filter: Statementstmt 100% 231 / 231 Branchbran 97.7% 131 / 134 Conditioncond 89.8% 71 / 79 MC/DCmcdc 77.7% 35 / 45 Subroutinesub 100% 75 / 75 Podpod 100% 43 / 43 totaltotal 96.5% 586 / 607 CC 101 SCAR 16.0
CC
101
Cov
98%
val
12.0
_siblings
11.0
_text
10.0
CRAP 5.0
« Mojolicious/Types.pm ↑ Summary Mojo/Asset/File.pm »
1 package Mojo::DOM;
2 31 ▶use Mojo::Base -strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 use overload
4 4 ▶ '@{}' => sub { shift->child_nodes },
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
5 98 ▶ '%{}' => sub { shift->attr },
Subroutine __ANON__: called CC=1 SCAR=0.0
6 84 ▶ bool => sub {1},
Subroutine __ANON__: called CC=1 SCAR=0.0
7 161 ▶ '""' => sub { shift->to_string },
Subroutine __ANON__: called CC=1 SCAR=0.0
8 31 fallback => 1;
9  
10 # "Fry: This snow is beautiful. I'm glad global warming never happened.
11 # Leela: Actually, it did. But thank God nuclear winter canceled it out."
12 31 ▶use Mojo::Collection;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 31 ▶use Mojo::DOM::CSS;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 31 ▶use Mojo::DOM::HTML;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 31 ▶use Scalar::Util qw(blessed weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 31 ▶use Storable qw(dclone);
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 29 ▶sub all_text { _text(_nodes($_[0]->tree), $_[0]->xml, 1) }
Subroutine all_text: called CC=1 SCAR=0.0
Pod documented
19  
20 15 ▶sub ancestors { _select($_[0]->_collect([_ancestors($_[0]->tree)]), $_[1]) }
Subroutine ancestors: called CC=1 SCAR=0.0
Pod documented
21  
22 9 ▶sub append { shift->_add(1, @_) }
Subroutine append: called CC=1 SCAR=0.0
Pod documented
23 16 ▶sub append_content { shift->_content(1, 0, @_) }
Subroutine append_content: called CC=1 SCAR=0.0
Pod documented
24  
25 738 ▶sub at {
Subroutine at: called CC=2 SCAR=6.9
Pod documented
26 738 my $self = shift;
27 738 ▶ return undef unless my $result = $self->_css->select_one(@_);
Branch: 1 branch
BranchTrueFalseTotal
unless my $result = $self->_css->select_one(@_) 67 670 737
28 670 return $self->_build($result, $self->xml);
29 }
30  
31 178 ▶sub attr {
Subroutine attr: called CC=6 SCAR=17.9
Pod documented
32 178 my $self = shift;
33  
34 # Hash
35 178 my $tree = $self->tree;
36 178 ▶ my $attrs = $tree->[0] ne 'tag' ? {} : $tree->[2];
Branch: 1 branch
BranchTrueFalseTotal
$tree->[0] ne 'tag' ? : 6 172 178
37 178 ▶ return $attrs unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 131 47 178
38  
39 # Get
40 47 ▶ return $attrs->{$_[0]} unless @_ > 1 || ref $_[0];
Branch: 1 branch
BranchTrueFalseTotal
unless @_ > 1 or ref $_[0] 43 4 47
Condition100%3/3
@_ > 1 or ref $_[0]
l!l&&r!l&&!r
3143
MC/DC100%2/2
@_ > 1 or ref $_[0]
@_ > 1 ref $_[0]
Truth table100%3/3
A or B
A= @_ > 1B= ref $_[0]
ABresult
1X1
011
000
41  
42 # Set
43 4 ▶ my $values = ref $_[0] ? $_[0] : {@_};
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 1 3 4
44 4 @$attrs{keys %$values} = values %$values;
45  
46 4 return $self;
47 }
48  
49 72 ▶sub child_nodes { $_[0]->_collect(_nodes($_[0]->tree)) }
Subroutine child_nodes: called CC=1 SCAR=0.0
Pod documented
50  
51 14 ▶sub children { _select($_[0]->_collect(_nodes($_[0]->tree, 1)), $_[1]) }
Subroutine children: called CC=1 SCAR=0.0
Pod documented
52  
53 65 ▶sub content {
Subroutine content: called CC=5 SCAR=16.1
Pod documented
54 65 my $self = shift;
55  
56 65 my $type = $self->type;
57 65 ▶ if ($type eq 'root' || $type eq 'tag') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "root" or $type eq "tag") 28 37 65
Condition100%3/3
$type eq "root" or $type eq "tag"
l!l&&r!l&&!r
62237
MC/DC100%2/2
$type eq "root" or $type eq "tag"
$type eq "root" $type eq "tag"
Truth table100%3/3
A or B
A= $type eq "root"B= $type eq "tag"
ABresult
1X1
011
000
58 28 ▶ return $self->_content(0, 1, @_) if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 21 7 28
59 7 my $html = Mojo::DOM::HTML->new(xml => $self->xml);
60 7 return join '', map { $html->tree($_)->render } @{_nodes($self->tree)};
61 }
62  
63 37 ▶ return $self->tree->[1] unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 34 3 37
64 3 $self->tree->[1] = shift;
65 3 return $self;
66 }
67  
68 13 ▶sub descendant_nodes { $_[0]->_collect(_all(_nodes($_[0]->tree))) }
Subroutine descendant_nodes: called CC=1 SCAR=0.0
Pod documented
69  
70 455 ▶sub find {
Subroutine find: called CC=1 SCAR=0.0
Pod documented
71 455 my $self = shift;
72 455 return $self->_collect($self->_css->select(@_));
73 }
74  
75 8 ▶sub following { _select($_[0]->_collect(_siblings($_[0]->tree, 1, 1)), $_[1]) }
Subroutine following: called CC=1 SCAR=0.0
Pod documented
76 7 ▶sub following_nodes { $_[0]->_collect(_siblings($_[0]->tree, 0, 1)) }
Subroutine following_nodes: called CC=1 SCAR=0.0
Pod documented
77  
78 51 ▶sub matches { shift->_css->matches(@_) }
Subroutine matches: called CC=1 SCAR=0.0
Pod documented
79  
80 18 ▶sub namespace {
Subroutine namespace: called CC=8 SCAR=20.8
Pod documented
81 18 my $self = shift;
82  
83 18 ▶ return undef if (my $tree = $self->tree)->[0] ne 'tag';
Branch: 1 branch
BranchTrueFalseTotal
if +(my $tree = $self->tree)->[0] ne "tag" 2 16 18
84  
85 # Extract namespace prefix and search parents
86 16 ▶ my $ns = $tree->[1] =~ /^(.*?):/ ? "xmlns:$1" : undef;
Branch: 1 branch
BranchTrueFalseTotal
$tree->[1] =~ /^(.*?):/u ? : 5 11 16
87 16 for my $node ($tree, _ancestors($tree)) {
88  
89 # Namespace for prefix
90 35 my $attrs = $node->[2];
91 35 ▶ if ($ns) { $_ eq $ns and return $attrs->{$_} for keys %$attrs }
Branch: 3 branches
BranchTrueFalseTotal
if ($ns) { } 13 22 35
elsif (defined $attrs->{'xmlns'}) { } 10 12 22
$_ eq $ns and return $attrs->{$_} 5 2 7
92  
93 # Namespace attribute
94 10 elsif (defined $attrs->{xmlns}) { return $attrs->{xmlns} }
95 }
96  
97 1 return undef;
98 }
99  
100 2397 ▶sub new {
Subroutine new: called CC=3 SCAR=11.0
Pod documented
101 2397 my $class = shift;
102 2397 ▶ my $self = bless \Mojo::DOM::HTML->new, ref $class || $class;
Condition66%2/3
ref $class || $class
l!l&&r!l&&!r
21462510
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table66%2/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
103 2397 ▶ return @_ ? $self->parse(@_) : $self;
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 208 2189 2397
104 }
105  
106 12 ▶sub new_tag {
Subroutine new_tag: called CC=2 SCAR=6.9
Pod documented
107 12 my $self = shift;
108 12 my $new = $self->new;
109 12 $$new->tag(@_);
110 12 ▶ $$new->xml($$self->xml) if ref $self;
Branch: 1 branch
BranchTrueFalseTotal
if ref $self 4 8 12
111 12 return $new;
112 }
113  
114 13 ▶sub next { $_[0]->_maybe(_siblings($_[0]->tree, 1, 1, 0)) }
Subroutine next: called CC=1 SCAR=0.0
Pod documented
115 5 ▶sub next_node { $_[0]->_maybe(_siblings($_[0]->tree, 0, 1, 0)) }
Subroutine next_node: called CC=1 SCAR=0.0
Pod documented
116  
117 54 ▶sub parent {
Subroutine parent: called CC=2 SCAR=7.1
Pod documented
118 54 my $self = shift;
119 54 ▶ return undef if (my $tree = $self->tree)->[0] eq 'root';
Branch: 1 branch
BranchTrueFalseTotal
if +(my $tree = $self->tree)->[0] eq "root" 0 54 54
120 54 return $self->_build(_parent($tree), $self->xml);
121 }
122  
123 247 ▶sub parse { ${$_[0]}->parse($_[1]) and return $_[0] }
Subroutine parse: called CC=2 SCAR=7.2
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
${$_[0];}->parse($_[1]) and return $_[0] 247 0 247
124  
125 5 ▶sub preceding { _select($_[0]->_collect(_siblings($_[0]->tree, 1, 0)), $_[1]) }
Subroutine preceding: called CC=1 SCAR=0.0
Pod documented
126 7 ▶sub preceding_nodes { $_[0]->_collect(_siblings($_[0]->tree, 0)) }
Subroutine preceding_nodes: called CC=1 SCAR=0.0
Pod documented
127  
128 11 ▶sub prepend { shift->_add(0, @_) }
Subroutine prepend: called CC=1 SCAR=0.0
Pod documented
129 6 ▶sub prepend_content { shift->_content(0, 0, @_) }
Subroutine prepend_content: called CC=1 SCAR=0.0
Pod documented
130  
131 7 ▶sub previous { $_[0]->_maybe(_siblings($_[0]->tree, 1, 0, -1)) }
Subroutine previous: called CC=1 SCAR=0.0
Pod documented
132 5 ▶sub previous_node { $_[0]->_maybe(_siblings($_[0]->tree, 0, 0, -1)) }
Subroutine previous_node: called CC=1 SCAR=0.0
Pod documented
133  
134 11 ▶sub remove { shift->replace('') }
Subroutine remove: called CC=1 SCAR=0.0
Pod documented
135  
136 29 ▶sub replace {
Subroutine replace: called CC=2 SCAR=6.9
Pod documented
137 29 my ($self, $new) = @_;
138 29 ▶ return $self->parse($new) if (my $tree = $self->tree)->[0] eq 'root';
Branch: 1 branch
BranchTrueFalseTotal
if +(my $tree = $self->tree)->[0] eq "root" 8 21 29
139 21 return $self->_replace(_parent($tree), $tree, _nodes($self->_parse($new)));
140 }
141  
142 14 ▶sub root {
Subroutine root: called CC=2 SCAR=6.9
Pod documented
143 14 my $self = shift;
144 14 ▶ return $self unless my $tree = _ancestors($self->tree, 1);
Branch: 1 branch
BranchTrueFalseTotal
unless my $tree = _ancestors($self->tree, 1) 3 11 14
145 11 return $self->_build($tree, $self->xml);
146 }
147  
148 13 ▶sub selector {
Subroutine selector: called CC=2 SCAR=6.9
Pod documented
149 13 ▶ return undef unless (my $tree = shift->tree)->[0] eq 'tag';
Branch: 1 branch
BranchTrueFalseTotal
unless +(my $tree = (shift())->tree)->[0] eq "tag" 2 11 13
150 11 return join ' > ', reverse map { $_->[1] . ':nth-child(' . (@{_siblings($_, 1)} + 1) . ')' } $tree, _ancestors($tree);
151 }
152  
153 10 ▶sub strip {
Subroutine strip: called CC=2 SCAR=6.9
Pod documented
154 10 my $self = shift;
155 10 ▶ return $self if (my $tree = $self->tree)->[0] ne 'tag';
Branch: 1 branch
BranchTrueFalseTotal
if +(my $tree = $self->tree)->[0] ne "tag" 2 8 10
156 8 return $self->_replace($tree->[3], $tree, _nodes($tree));
157 }
158  
159 97 ▶sub tag {
Subroutine tag: called CC=3 SCAR=11.0
Pod documented
160 97 my ($self, $tag) = @_;
161 97 ▶ return undef if (my $tree = $self->tree)->[0] ne 'tag';
Branch: 1 branch
BranchTrueFalseTotal
if +(my $tree = $self->tree)->[0] ne "tag" 2 95 97
162 95 ▶ return $tree->[1] unless $tag;
Branch: 1 branch
BranchTrueFalseTotal
unless $tag 94 1 95
163 1 $tree->[1] = $tag;
164 1 return $self;
165 }
166  
167 1 ▶sub tap { shift->Mojo::Base::tap(@_) }
Subroutine tap: called CC=1 SCAR=0.0
Pod documented
168  
169 816 ▶sub text { _text(_nodes(shift->tree), 0, 0) }
Subroutine text: called CC=1 SCAR=0.0
Pod documented
170  
171 171 ▶sub to_string { ${shift()}->render }
Subroutine to_string: called CC=1 SCAR=0.0
Pod documented
172  
173 5112 ▶sub tree { @_ > 1 ? (${$_[0]}->tree($_[1]) and return $_[0]) : ${$_[0]}->tree }
Subroutine tree: called CC=3 SCAR=11.1
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 ? : 2142 2970 5112
Condition50%1/2
${$_[0];}->tree($_[1]) and return $_[0]
!ll
02142
MC/DC0%0/1
${$_[0];}->tree($_[1]) and return $_[0]
${$_[0];}->tree($_[1])
Truth table50%1/2
A
A= ${$_[0];}->tree($_[1])
Aresult
00
11
174  
175 93 ▶sub type { shift->tree->[0] }
Subroutine type: called CC=1 SCAR=0.0
Pod documented
176  
177 27 ▶sub val {
Subroutine val: called CC=12 SCAR=24.9
Pod documented
178 27 my $self = shift;
179  
180 # "option"
181 27 ▶ return $self->{value} // $self->text if (my $tag = $self->tag) eq 'option';
Branch: 1 branch
BranchTrueFalseTotal
if (my $tag = $self->tag) eq "option" 10 17 27
Condition66%2/3
$self->{"value"} // $self->text
l!l&&r!l&&!r
280
MC/DC0%0/2
$self->{"value"} // $self->text
$self->{"value"} $self->text
Truth table66%2/3
A // B
A= $self->{"value"}B= $self->text
ABresult
1X1
011
000
182  
183 # "input" ("type=checkbox" and "type=radio")
184 17 ▶ my $type = $self->{type} // '';
Condition100%2/2
$self->{"type"} // ""
l!l
89
MC/DC100%1/1
$self->{"type"} // ""
$self->{"type"}
Truth table100%2/2
A
A= $self->{"type"}
Aresult
11
00
185 17 ▶ return $self->{value} // 'on' if $tag eq 'input' && ($type eq 'radio' || $type eq 'checkbox');
Branch: 1 branch
BranchTrueFalseTotal
if $tag eq "input" and $type eq "radio" || $type eq "checkbox" 5 12 17
Condition100%8/8
$tag eq "input" and $type eq "radio" || $type eq "checkbox"
!ll&&!rl&&r
845
$type eq "radio" or $type eq "checkbox"
l!l&&r!l&&!r
234
$self->{"value"} // "on"
l!l
32
MC/DC100%3/3
$tag eq "input" and $type eq "radio" || $type eq "checkbox"
$tag eq "input" $type eq "radio" $type eq "checkbox"
MC/DC100%1/1
$self->{"value"} // "on"
$self->{"value"}
Truth table100%4/4
A and B or C
A= $tag eq "input"B= $type eq "radio"C= $type eq "checkbox"
ABCresult
0XX0
1000
11X1
1011
Truth table100%2/2
A
A= $self->{"value"}
Aresult
11
00
186  
187 # "textarea", "input" or "button"
188 12 ▶ return $tag eq 'textarea' ? $self->text : $self->{value} if $tag ne 'select';
Branch: 2 branches
BranchTrueFalseTotal
if $tag ne "select" 7 5 12
$tag eq 'textarea' ? : 1 6 7
189  
190 # "select"
191 6 ▶ my $v = $self->find('option:checked:not([disabled])')->grep(sub { !$_->ancestors('optgroup[disabled]')->size })
Subroutine __ANON__: called CC=1 SCAR=0.0
192 5 ->map('val');
193 5 ▶ return exists $self->{multiple} ? $v->size ? $v->to_array : undef : $v->last;
Branch: 2 branches
BranchTrueFalseTotal
exists $self->{'multiple'} ? : 2 3 5
$v->size ? : 1 1 2
194 }
195  
196 1 ▶sub with_roles { shift->Mojo::Base::with_roles(@_) }
Subroutine with_roles: called CC=1 SCAR=0.0
Pod documented
197  
198 9 ▶sub wrap { shift->_wrap(0, @_) }
Subroutine wrap: called CC=1 SCAR=0.0
Pod documented
199 7 ▶sub wrap_content { shift->_wrap(1, @_) }
Subroutine wrap_content: called CC=1 SCAR=0.0
Pod documented
200  
201 3650 ▶sub xml { @_ > 1 ? (${$_[0]}->xml($_[1]) and return $_[0]) : ${$_[0]}->xml }
Subroutine xml: called CC=3 SCAR=11.1
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 ? : 2164 1486 3650
Condition50%1/2
${$_[0];}->xml($_[1]) and return $_[0]
!ll
02164
MC/DC0%0/1
${$_[0];}->xml($_[1]) and return $_[0]
${$_[0];}->xml($_[1])
Truth table50%1/2
A
A= ${$_[0];}->xml($_[1])
Aresult
00
11
202  
203 20 ▶sub _add {
Subroutine _add: called CC=2 SCAR=6.9
204 20 my ($self, $offset, $new) = @_;
205  
206 20 ▶ return $self if (my $tree = $self->tree)->[0] eq 'root';
Branch: 1 branch
BranchTrueFalseTotal
if +(my $tree = $self->tree)->[0] eq "root" 4 16 20
207  
208 16 my $parent = _parent($tree);
209 16 splice @$parent, _offset($parent, $tree) + $offset, 0, @{_link($parent, _nodes($self->_parse($new)))};
210  
211 16 return $self;
212 }
213  
214 21 ▶sub _all {
Subroutine _all: called CC=2 SCAR=6.9
215 21 my $nodes = shift;
216 21 ▶ @$nodes = map { $_->[0] eq 'tag' ? ($_, @{_all(_nodes($_))}) : ($_) } @$nodes;
Branch: 1 branch
BranchTrueFalseTotal
$_->[0] eq 'tag' ? : 8 52 60
217 21 return $nodes;
218 }
219  
220 56 ▶sub _ancestors {
Subroutine _ancestors: called CC=5 SCAR=16.1
221 56 my ($tree, $root) = @_;
222  
223 56 ▶ return () unless $tree = _parent($tree);
Branch: 1 branch
BranchTrueFalseTotal
unless $tree = _parent($tree) 3 53 56
224 53 my @ancestors;
225 53 ▶ do { push @ancestors, $tree } while ($tree->[0] eq 'tag') && ($tree = $tree->[3]);
Branch: 1 branch
BranchTrueFalseTotal
$tree->[0] eq "tag" and $tree = $tree->[3] and do { do { push @ancestors, $tree } } 88 53 141
Condition66%2/3
$tree->[0] eq "tag" and $tree = $tree->[3]
!ll&&!rl&&r
53088
MC/DC50%1/2
$tree->[0] eq "tag" and $tree = $tree->[3]
$tree->[0] eq "tag" $tree = $tree->[3]
Truth table66%2/3
A and B
A= $tree->[0] eq "tag"B= $tree = $tree->[3]
ABresult
0X0
100
111
226 53 ▶ return $root ? $ancestors[-1] : @ancestors[0 .. $#ancestors - 1];
Branch: 1 branch
BranchTrueFalseTotal
$root ? : 11 42 53
227 }
228  
229 2142 ▶sub _build { shift->new->tree(shift)->xml(shift) }
Subroutine _build: called CC=1 SCAR=0.0
230  
231 595 ▶sub _collect {
Subroutine _collect: called CC=2 SCAR=7.0
232 595 ▶ my ($self, $nodes) = (shift, shift // []);
Condition50%1/2
shift() // []
l!l
5950
MC/DC0%0/1
shift() // []
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
233 595 my $xml = $self->xml;
234 595 return Mojo::Collection->new(map { $self->_build($_, $xml) } @$nodes);
235 }
236  
237 43 ▶sub _content {
Subroutine _content: called CC=6 SCAR=17.9
238 43 my ($self, $start, $offset, $new) = @_;
239  
240 43 my $tree = $self->tree;
241 43 ▶ unless ($tree->[0] eq 'root' || $tree->[0] eq 'tag') {
Branch: 1 branch
BranchTrueFalseTotal
unless ($tree->[0] eq "root" or $tree->[0] eq "tag") 2 41 43
Condition100%3/3
$tree->[0] eq "root" or $tree->[0] eq "tag"
l!l&&r!l&&!r
7342
MC/DC100%2/2
$tree->[0] eq "root" or $tree->[0] eq "tag"
$tree->[0] eq "root" $tree->[0] eq "tag"
Truth table100%3/3
A or B
A= $tree->[0] eq "root"B= $tree->[0] eq "tag"
ABresult
1X1
011
000
242 2 my $old = $self->content;
243 2 ▶ return $self->content($start ? $old . $new : $new . $old);
Branch: 1 branch
BranchTrueFalseTotal
$start ? : 1 1 2
244 }
245  
246 41 ▶ $start = $start ? ($#$tree + 1) : _start($tree);
Branch: 1 branch
BranchTrueFalseTotal
$start ? : 15 26 41
247 41 ▶ $offset = $offset ? $#$tree : 0;
Branch: 1 branch
BranchTrueFalseTotal
$offset ? : 21 20 41
248 41 splice @$tree, $start, $offset, @{_link($tree, _nodes($self->_parse($new)))};
249  
250 41 return $self;
251 }
252  
253 1244 ▶sub _css { Mojo::DOM::CSS->new(tree => shift->tree) }
Subroutine _css: called CC=1 SCAR=0.0
254  
255 3 ▶sub _fragment { _link(my $r = ['root', @_], [@_], 1); $r }
Subroutine _fragment: called CC=1 SCAR=0.0
256  
257 113 ▶sub _link {
Subroutine _link: called CC=8 SCAR=20.8
258 113 my ($parent, $children, $fragment) = @_;
259  
260 # Link parent to children (raw text is only allowed in script and style elements)
261 113 ▶ my $raw = $fragment || $parent->[0] eq 'tag' && ($parent->[1] eq 'script' || $parent->[1] eq 'style');
Condition100%9/9
$fragment || $parent->[0] eq 'tag' && ($parent->[1] eq 'script' || $parent->[1] eq 'style')
l!l&&r!l&&!r
36104
$parent->[0] eq "tag" and $parent->[1] eq "script" || $parent->[1] eq "style"
!ll&&!rl&&r
34706
$parent->[1] eq "script" or $parent->[1] eq "style"
l!l&&r!l&&!r
5170
MC/DC100%4/4
$fragment || $parent->[0] eq 'tag' && ($parent->[1] eq 'script' || $parent->[1] eq 'style')
$fragment $parent->[0] eq "tag" $parent->[1] eq "script" $parent->[1] eq "style"
Truth table100%5/5
A || B and C or D
A= $fragmentB= $parent->[0] eq "tag"C= $parent->[1] eq "script"D= $parent->[1] eq "style"
ABCDresult
1XXX1
011X1
01011
00XX0
01000
262 113 for my $node (@$children) {
263 112 ▶ $node->[0] = 'text' if $node->[0] eq 'raw' && !$raw;
Branch: 1 branch
BranchTrueFalseTotal
if $node->[0] eq "raw" and not $raw 2 110 112
Condition100%3/3
$node->[0] eq "raw" and not $raw
!ll&&!rl&&r
10732
MC/DC100%2/2
$node->[0] eq "raw" and not $raw
$node->[0] eq "raw" not $raw
Truth table100%3/3
A and not(B)
A= $node->[0] eq "raw"B= not $raw
ABresult
0X0
100
111
264 112 ▶ my $offset = $node->[0] eq 'tag' ? 3 : 2;
Branch: 1 branch
BranchTrueFalseTotal
$node->[0] eq 'tag' ? : 61 51 112
265 112 $node->[$offset] = $parent;
266 112 weaken $node->[$offset];
267 }
268  
269 113 return $children;
270 }
271  
272 30 ▶sub _maybe { $_[1] ? $_[0]->_build($_[1], $_[0]->xml) : undef }
Subroutine _maybe: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_[1] ? : 20 10 30
273  
274 1321 ▶sub _nodes {
Subroutine _nodes: called CC=3 SCAR=11.0
275 1321 ▶ return () unless my $tree = shift;
Branch: 1 branch
BranchTrueFalseTotal
unless my $tree = shift() 0 1321 1321
276 1321 my @nodes = @$tree[_start($tree) .. $#$tree];
277 1321 ▶ return shift() ? [grep { $_->[0] eq 'tag' } @nodes] : \@nodes;
Branch: 1 branch
BranchTrueFalseTotal
shift() ? : 46 1275 1321
278 }
279  
280 52 ▶sub _offset {
Subroutine _offset: called CC=3 SCAR=11.0
281 52 my ($parent, $child) = @_;
282 52 my $i = _start($parent);
283 52 ▶ $_ eq $child ? last : $i++ for @$parent[$i .. $#$parent];
Branch: 1 branch
BranchTrueFalseTotal
$_ eq $child ? : 52 30 82
284 52 return $i;
285 }
286  
287 236 ▶sub _parent { $_[0]->[$_[0][0] eq 'tag' ? 3 : 2] }
Subroutine _parent: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_[0][0] eq 'tag' ? : 195 41 236
288  
289 92 ▶sub _parse {
Subroutine _parse: called CC=4 SCAR=13.9
290 92 my ($self, $input) = @_;
291 92 ▶ return Mojo::DOM::HTML->new(xml => $self->xml)->parse($input)->tree unless blessed $input && $input->isa('Mojo::DOM');
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($input) and $input->isa("Mojo::DOM") 68 24 92
Condition66%2/3
builtin::blessed($input) and $input->isa("Mojo::DOM")
!ll&&!rl&&r
68024
MC/DC50%1/2
builtin::blessed($input) and $input->isa("Mojo::DOM")
builtin::blessed($input) $input->isa("Mojo::DOM")
Truth table66%2/3
A and B
A= builtin::blessed($input)B= $input->isa("Mojo::DOM")
ABresult
0X0
100
111
292 24 my $tree = dclone $input->tree;
293 24 ▶ return $tree->[0] eq 'root' ? $tree : _fragment($tree);
Branch: 1 branch
BranchTrueFalseTotal
$tree->[0] eq 'root' ? : 21 3 24
294 }
295  
296 36 ▶sub _replace {
Subroutine _replace: called CC=1 SCAR=0.0
297 36 my ($self, $parent, $child, $nodes) = @_;
298 36 splice @$parent, _offset($parent, $child), 1, @{_link($parent, $nodes)};
299 36 return $self->parent;
300 }
301  
302 42 ▶sub _select { $_[1] ? $_[0]->grep(matches => $_[1]) : $_[0] }
Subroutine _select: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_[1] ? : 21 21 42
303  
304 88 ▶sub _siblings {
Subroutine _siblings: called CC=11 SCAR=24.0
305 88 my ($tree, $tags, $tail, $i) = @_;
306  
307 88 ▶ return defined $i ? undef : [] if $tree->[0] eq 'root';
Branch: 2 branches
BranchTrueFalseTotal
if $tree->[0] eq "root" 6 82 88
defined $i ? : 2 4 6
308  
309 82 my $nodes = _nodes(_parent($tree));
310 82 my $match = -1;
311 82 ▶ defined($match++) and $_ eq $tree and last for @$nodes;
Branch: 1 branch
BranchTrueFalseTotal
defined $match++ and $_ eq $tree and last 82 143 225
Condition66%2/3
defined $match++ and $_ eq $tree
!ll&&!rl&&r
014382
MC/DC50%1/2
defined $match++ and $_ eq $tree
defined $match++ $_ eq $tree
Truth table66%2/3
A and B
A= defined $match++B= $_ eq $tree
ABresult
0X0
100
111
312  
313 82 ▶ if ($tail) { splice @$nodes, 0, $match + 1 }
Branch: 1 branch
BranchTrueFalseTotal
if ($tail) { } 30 52 82
314 52 else { splice @$nodes, $match, ($#$nodes + 1) - $match }
315  
316 82 ▶ @$nodes = grep { $_->[0] eq 'tag' } @$nodes if $tags;
Branch: 1 branch
BranchTrueFalseTotal
if $tags 60 22 82
317  
318 82 ▶ return defined $i ? $i == -1 && !@$nodes ? undef : $nodes->[$i] : $nodes;
Branch: 2 branches
BranchTrueFalseTotal
defined $i ? : 28 54 82
$i == -1 && !@$nodes ? : 4 24 28
Condition100%3/3
$i == -1 and not @$nodes
!ll&&!rl&&r
1774
MC/DC100%2/2
$i == -1 and not @$nodes
$i == -1 not @$nodes
Truth table100%3/3
A and not(B)
A= $i == -1B= not @$nodes
ABresult
0X0
100
111
319 }
320  
321 1404 ▶sub _start { $_[0][0] eq 'root' ? 1 : 4 }
Subroutine _start: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_[0][0] eq 'root' ? : 222 1182 1404
322  
323 845 ▶sub _text {
Subroutine _text: called CC=10 SCAR=23.0
324 845 my ($nodes, $xml, $all) = @_;
325  
326 845 my $text = '';
327 845 ▶ while (my $node = shift @$nodes) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $node = shift @$nodes) 1217 845 2062
328 1217 my $type = $node->[0];
329  
330 # Text
331 1217 ▶ if ($type eq 'text' || $type eq 'cdata' || $type eq 'raw') { $text .= $node->[1] }
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'text' or $type eq 'cdata' or $type eq 'raw') { } 996 221 1217
elsif ($type eq 'tag' and $all) { } 157 66 223
Condition100%6/6
$type eq "text" or $type eq "cdata" or $type eq "raw"
l!l&&r!l&&!r
97719221
$type eq "text" or $type eq "cdata"
l!l&&r!l&&!r
96611240
MC/DC100%3/3
$type eq "text" or $type eq "cdata" or $type eq "raw"
$type eq "text" $type eq "cdata" $type eq "raw"
Truth table100%4/4
A or B or C
A= $type eq "text"B= $type eq "cdata"C= $type eq "raw"
ABCresult
1XX1
01X1
0011
0000
332  
333 # Nested tag
334 ▶ elsif ($type eq 'tag' && $all) {
Condition100%3/3
$type eq "tag" and $all
!ll&&!rl&&r
262157
MC/DC100%2/2
$type eq "tag" and $all
$type eq "tag" $all
Truth table100%3/3
A and B
A= $type eq "tag"B= $all
ABresult
0X0
100
111
335 157 ▶ unshift @$nodes, @{_nodes($node)} if $xml || ($node->[1] ne 'script' && $node->[1] ne 'style');
Branch: 1 branch
BranchTrueFalseTotal
if $xml or $node->[1] ne "script" and $node->[1] ne "style" 145 12 157
Condition100%6/6
$xml or $node->[1] ne "script" and $node->[1] ne "style"
l!l&&r!l&&!r
314212
$node->[1] ne "script" and $node->[1] ne "style"
!ll&&!rl&&r
66142
MC/DC100%3/3
$xml or $node->[1] ne "script" and $node->[1] ne "style"
$xml $node->[1] ne "script" $node->[1] ne "style"
Truth table100%4/4
A or B and C
A= $xmlB= $node->[1] ne "script"C= $node->[1] ne "style"
ABCresult
1XX1
0111
00X0
0100
336 }
337 }
338  
339 845 return $text;
340 }
341  
342 16 ▶sub _wrap {
Subroutine _wrap: called CC=9 SCAR=22.0
343 16 my ($self, $content, $new) = @_;
344  
345 16 ▶ return $self if (my $tree = $self->tree)->[0] eq 'root' && !$content;
Branch: 1 branch
BranchTrueFalseTotal
if +(my $tree = $self->tree)->[0] eq "root" and not $content 1 15 16
Condition100%3/3
+(my $tree = $self->tree)->[0] eq "root" and not $content
!ll&&!rl&&r
1411
MC/DC100%2/2
+(my $tree = $self->tree)->[0] eq "root" and not $content
+(my $tree = $self->tree)->[0] eq "root" not $content
Truth table100%3/3
A and not(B)
A= +(my $tree = $self->tree)->[0] eq "root"B= not $content
ABresult
0X0
100
111
346 15 ▶ return $self if $tree->[0] ne 'root' && $tree->[0] ne 'tag' && $content;
Branch: 1 branch
BranchTrueFalseTotal
if $tree->[0] ne "root" and $tree->[0] ne "tag" and $content 1 14 15
Condition100%6/6
$tree->[0] ne "root" and $tree->[0] ne "tag" and $content
!ll&&!rl&&r
1221
$tree->[0] ne "root" and $tree->[0] ne "tag"
!ll&&!rl&&r
1113
MC/DC100%3/3
$tree->[0] ne "root" and $tree->[0] ne "tag" and $content
$tree->[0] ne "root" $tree->[0] ne "tag" $content
Truth table100%4/4
A and B and C
A= $tree->[0] ne "root"B= $tree->[0] ne "tag"C= $content
ABCresult
0XX0
10X0
1100
1111
347  
348 # Find innermost tag
349 14 my $current;
350 14 my $first = $new = $self->_parse($new);
351 14 ▶ $current = $first while $first = _nodes($first, 1)->[0];
Branch: 1 branch
BranchTrueFalseTotal
$first = _nodes($first, 1)->[0] and do { $current = $first } 18 14 32
352 14 ▶ return $self unless $current;
Branch: 1 branch
BranchTrueFalseTotal
unless $current 2 12 14
353  
354 # Wrap content
355 12 ▶ if ($content) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content) 5 7 12
356 5 push @$current, @{_link($current, _nodes($tree))};
357 5 splice @$tree, _start($tree), $#$tree, @{_link($tree, _nodes($new))};
358 5 return $self;
359 }
360  
361 # Wrap element
362 7 $self->_replace(_parent($tree), $tree, _nodes($new));
363 7 push @$current, @{_link($current, [$tree])};
364 7 return $self;
365 }
366  
367 31 1;
368  
369 =encoding utf8
370  
371 =head1 NAME
372  
373 Mojo::DOM - Minimalistic HTML/XML DOM parser with CSS selectors
374  
375 =head1 SYNOPSIS
376  
377 use Mojo::DOM;
378  
379 # Parse
380 my $dom = Mojo::DOM->new('<div><p id="a">Test</p><p id="b">123</p></div>');
381  
382 # Find
383 say $dom->at('#b')->text;
384 say $dom->find('p')->map('text')->join("\n");
385 say $dom->find('[id]')->map(attr => 'id')->join("\n");
386  
387 # Iterate
388 $dom->find('p[id]')->reverse->each(sub { say $_->{id} });
389  
390 # Loop
391 for my $e ($dom->find('p[id]')->each) {
392 say $e->{id}, ':', $e->text;
393 }
394  
395 # Modify
396 $dom->find('div p')->last->append('<p id="c">456</p>');
397 $dom->at('#c')->prepend($dom->new_tag('p', id => 'd', '789'));
398 $dom->find(':not(p)')->map('strip');
399  
400 # Render
401 say "$dom";
402  
403 =head1 DESCRIPTION
404  
405 L<Mojo::DOM> is a minimalistic and relaxed HTML/XML DOM parser with CSS selector support. It will even try to interpret
406 broken HTML and XML, so you should not use it for validation or sanitization. Its interpretation of broken markup can
407 differ from that of a web browser.
408  
409 =head1 NODES AND ELEMENTS
410  
411 When we parse an HTML/XML fragment, it gets turned into a tree of nodes.
412  
413 <!DOCTYPE html>
414 <html>
415 <head><title>Hello</title></head>
416 <body>World!</body>
417 </html>
418  
419 There are currently eight different kinds of nodes, C<cdata>, C<comment>, C<doctype>, C<pi>, C<raw>, C<root>, C<tag>
420 and C<text>. Elements are nodes of the type C<tag>.
421  
422 root
423 |- doctype (html)
424 +- tag (html)
425 |- tag (head)
426 | +- tag (title)
427 | +- raw (Hello)
428 +- tag (body)
429 +- text (World!)
430  
431 While all node types are represented as L<Mojo::DOM> objects, some methods like L</"attr"> and L</"namespace"> only
432 apply to elements.
433  
434 =head1 HTML AND XML
435  
436 L<Mojo::DOM> defaults to HTML semantics, that means all tags and attribute names are lowercased and selectors need to
437 be lowercase as well.
438  
439 # HTML semantics
440 my $dom = Mojo::DOM->new('<P ID="greeting">Hi!</P>');
441 say $dom->at('p[id]')->text;
442  
443 If the document starts with an XML declaration, the parser will automatically switch into XML mode and everything
444 becomes case-sensitive.
445  
446 # XML semantics
447 my $dom = Mojo::DOM->new('<?xml version="1.0"?><P ID="greeting">Hi!</P>');
448 say $dom->at('P[ID]')->text;
449  
450 HTML or XML semantics can also be forced with the L</"xml"> method.
451  
452 # Force HTML semantics
453 my $dom = Mojo::DOM->new->xml(0)->parse('<P ID="greeting">Hi!</P>');
454 say $dom->at('p[id]')->text;
455  
456 # Force XML semantics
457 my $dom = Mojo::DOM->new->xml(1)->parse('<P ID="greeting">Hi!</P>');
458 say $dom->at('P[ID]')->text;
459  
460 =head1 METHODS
461  
462 L<Mojo::DOM> implements the following methods.
463  
464 =head2 all_text
465  
466 my $text = $dom->all_text;
467  
468 Extract text content from all descendant nodes of this element. For HTML documents C<script> and C<style> elements are
469 excluded.
470  
471 # "foo\nbarbaz\n"
472 $dom->parse("<div>foo\n<p>bar</p>baz\n</div>")->at('div')->all_text;
473  
474 =head2 ancestors
475  
476 my $collection = $dom->ancestors;
477 my $collection = $dom->ancestors('div ~ p');
478  
479 Find all ancestor elements of this node matching the CSS selector and return a L<Mojo::Collection> object containing
480 these elements as L<Mojo::DOM> objects. All selectors from L<Mojo::DOM::CSS/"SELECTORS"> are supported.
481  
482 # List tag names of ancestor elements
483 say $dom->ancestors->map('tag')->join("\n");
484  
485 =head2 append
486  
487 $dom = $dom->append('<p>I ♥ Mojolicious!</p>');
488 $dom = $dom->append(Mojo::DOM->new);
489  
490 Append HTML/XML fragment to this node (for all node types other than C<root>).
491  
492 # "<div><h1>Test</h1><h2>123</h2></div>"
493 $dom->parse('<div><h1>Test</h1></div>')
494 ->at('h1')->append('<h2>123</h2>')->root;
495  
496 # "<p>Test 123</p>"
497 $dom->parse('<p>Test</p>')->at('p')
498 ->child_nodes->first->append(' 123')->root;
499  
500 =head2 append_content
501  
502 $dom = $dom->append_content('<p>I ♥ Mojolicious!</p>');
503 $dom = $dom->append_content(Mojo::DOM->new);
504  
505 Append HTML/XML fragment (for C<root> and C<tag> nodes) or raw content to this node's content.
506  
507 # "<div><h1>Test123</h1></div>"
508 $dom->parse('<div><h1>Test</h1></div>')
509 ->at('h1')->append_content('123')->root;
510  
511 # "<!-- Test 123 --><br>"
512 $dom->parse('<!-- Test --><br>')
513 ->child_nodes->first->append_content('123 ')->root;
514  
515 # "<p>Test<i>123</i></p>"
516 $dom->parse('<p>Test</p>')->at('p')->append_content('<i>123</i>')->root;
517  
518 =head2 at
519  
520 my $result = $dom->at('div ~ p');
521 my $result = $dom->at('svg|line', svg => 'http://www.w3.org/2000/svg');
522  
523 Find first descendant element of this element matching the CSS selector and return it as a L<Mojo::DOM> object, or
524 C<undef> if none could be found. All selectors from L<Mojo::DOM::CSS/"SELECTORS"> are supported.
525  
526 # Find first element with "svg" namespace definition
527 my $namespace = $dom->at('[xmlns\:svg]')->{'xmlns:svg'};
528  
529 Trailing key/value pairs can be used to declare xml namespace aliases.
530  
531 # "<rect />"
532 $dom->parse('<svg xmlns="http://www.w3.org/2000/svg"><rect /></svg>')
533 ->at('svg|rect', svg => 'http://www.w3.org/2000/svg');
534  
535 =head2 attr
536  
537 my $hash = $dom->attr;
538 my $foo = $dom->attr('foo');
539 $dom = $dom->attr({foo => 'bar'});
540 $dom = $dom->attr(foo => 'bar');
541  
542 This element's attributes.
543  
544 # Remove an attribute
545 delete $dom->attr->{id};
546  
547 # Attribute without value
548 $dom->attr(selected => undef);
549  
550 # List id attributes
551 say $dom->find('*')->map(attr => 'id')->compact->join("\n");
552  
553 =head2 child_nodes
554  
555 my $collection = $dom->child_nodes;
556  
557 Return a L<Mojo::Collection> object containing all child nodes of this element as L<Mojo::DOM> objects.
558  
559 # "<p><b>123</b></p>"
560 $dom->parse('<p>Test<b>123</b></p>')->at('p')->child_nodes->first->remove;
561  
562 # "<!DOCTYPE html>"
563 $dom->parse('<!DOCTYPE html><b>123</b>')->child_nodes->first;
564  
565 # " Test "
566 $dom->parse('<b>123</b><!-- Test -->')->child_nodes->last->content;
567  
568 =head2 children
569  
570 my $collection = $dom->children;
571 my $collection = $dom->children('div ~ p');
572  
573 Find all child elements of this element matching the CSS selector and return a L<Mojo::Collection> object containing
574 these elements as L<Mojo::DOM> objects. All selectors from L<Mojo::DOM::CSS/"SELECTORS"> are supported.
575  
576 # Show tag name of random child element
577 say $dom->children->shuffle->first->tag;
578  
579 =head2 content
580  
581 my $str = $dom->content;
582 $dom = $dom->content('<p>I ♥ Mojolicious!</p>');
583 $dom = $dom->content(Mojo::DOM->new);
584  
585 Return this node's content or replace it with HTML/XML fragment (for C<root> and C<tag> nodes) or raw content.
586  
587 # "<b>Test</b>"
588 $dom->parse('<div><b>Test</b></div>')->at('div')->content;
589  
590 # "<div><h1>123</h1></div>"
591 $dom->parse('<div><h1>Test</h1></div>')->at('h1')->content('123')->root;
592  
593 # "<p><i>123</i></p>"
594 $dom->parse('<p>Test</p>')->at('p')->content('<i>123</i>')->root;
595  
596 # "<div><h1></h1></div>"
597 $dom->parse('<div><h1>Test</h1></div>')->at('h1')->content('')->root;
598  
599 # " Test "
600 $dom->parse('<!-- Test --><br>')->child_nodes->first->content;
601  
602 # "<div><!-- 123 -->456</div>"
603 $dom->parse('<div><!-- Test -->456</div>')
604 ->at('div')->child_nodes->first->content(' 123 ')->root;
605  
606 =head2 descendant_nodes
607  
608 my $collection = $dom->descendant_nodes;
609  
610 Return a L<Mojo::Collection> object containing all descendant nodes of this element as L<Mojo::DOM> objects.
611  
612 # "<p><b>123</b></p>"
613 $dom->parse('<p><!-- Test --><b>123<!-- 456 --></b></p>')
614 ->descendant_nodes->grep(sub { $_->type eq 'comment' })
615 ->map('remove')->first;
616  
617 # "<p><b>test</b>test</p>"
618 $dom->parse('<p><b>123</b>456</p>')
619 ->at('p')->descendant_nodes->grep(sub { $_->type eq 'text' })
620 ->map(content => 'test')->first->root;
621  
622 =head2 find
623  
624 my $collection = $dom->find('div ~ p');
625 my $collection = $dom->find('svg|line', svg => 'http://www.w3.org/2000/svg');
626  
627 Find all descendant elements of this element matching the CSS selector and return a L<Mojo::Collection> object
628 containing these elements as L<Mojo::DOM> objects. All selectors from L<Mojo::DOM::CSS/"SELECTORS"> are supported.
629  
630 # Find a specific element and extract information
631 my $id = $dom->find('div')->[23]{id};
632  
633 # Extract information from multiple elements
634 my @headers = $dom->find('h1, h2, h3')->map('text')->each;
635  
636 # Count all the different tags
637 my $hash = $dom->find('*')->reduce(sub { $a->{$b->tag}++; $a }, {});
638  
639 # Find elements with a class that contains dots
640 my @divs = $dom->find('div.foo\.bar')->each;
641  
642 Trailing key/value pairs can be used to declare xml namespace aliases.
643  
644 # "<rect />"
645 $dom->parse('<svg xmlns="http://www.w3.org/2000/svg"><rect /></svg>')
646 ->find('svg|rect', svg => 'http://www.w3.org/2000/svg')->first;
647  
648 =head2 following
649  
650 my $collection = $dom->following;
651 my $collection = $dom->following('div ~ p');
652  
653 Find all sibling elements after this node matching the CSS selector and return a L<Mojo::Collection> object containing
654 these elements as L<Mojo::DOM> objects. All selectors from L<Mojo::DOM::CSS/"SELECTORS"> are supported.
655  
656 # List tags of sibling elements after this node
657 say $dom->following->map('tag')->join("\n");
658  
659 =head2 following_nodes
660  
661 my $collection = $dom->following_nodes;
662  
663 Return a L<Mojo::Collection> object containing all sibling nodes after this node as L<Mojo::DOM> objects.
664  
665 # "C"
666 $dom->parse('<p>A</p><!-- B -->C')->at('p')->following_nodes->last->content;
667  
668 =head2 matches
669  
670 my $bool = $dom->matches('div ~ p');
671 my $bool = $dom->matches('svg|line', svg => 'http://www.w3.org/2000/svg');
672  
673 Check if this element matches the CSS selector. All selectors from L<Mojo::DOM::CSS/"SELECTORS"> are supported.
674  
675 # True
676 $dom->parse('<p class="a">A</p>')->at('p')->matches('.a');
677 $dom->parse('<p class="a">A</p>')->at('p')->matches('p[class]');
678  
679 # False
680 $dom->parse('<p class="a">A</p>')->at('p')->matches('.b');
681 $dom->parse('<p class="a">A</p>')->at('p')->matches('p[id]');
682  
683 Trailing key/value pairs can be used to declare xml namespace aliases.
684  
685 # True
686 $dom->parse('<svg xmlns="http://www.w3.org/2000/svg"><rect /></svg>')
687 ->matches('svg|rect', svg => 'http://www.w3.org/2000/svg');
688  
689 =head2 namespace
690  
691 my $namespace = $dom->namespace;
692  
693 Find this element's namespace, or return C<undef> if none could be found.
694  
695 # "http://www.w3.org/2000/svg"
696 Mojo::DOM->new('<svg xmlns:svg="http://www.w3.org/2000/svg"><svg:circle>3.14</svg:circle></svg>')->at('svg\:circle')->namespace;
697  
698 # Find namespace for an element with namespace prefix
699 my $namespace = $dom->at('svg > svg\:circle')->namespace;
700  
701 # Find namespace for an element that may or may not have a namespace prefix
702 my $namespace = $dom->at('svg > circle')->namespace;
703  
704 =head2 new
705  
706 my $dom = Mojo::DOM->new;
707 my $dom = Mojo::DOM->new('<foo bar="baz">I ♥ Mojolicious!</foo>');
708  
709 Construct a new scalar-based L<Mojo::DOM> object and L</"parse"> HTML/XML fragment if necessary.
710  
711 =head2 new_tag
712  
713 my $tag = Mojo::DOM->new_tag('div');
714 my $tag = $dom->new_tag('div');
715 my $tag = $dom->new_tag('div', id => 'foo', hidden => undef);
716 my $tag = $dom->new_tag('div', 'safe content');
717 my $tag = $dom->new_tag('div', id => 'foo', 'safe content');
718 my $tag = $dom->new_tag('div', data => {mojo => 'rocks'}, 'safe content');
719 my $tag = $dom->new_tag('div', id => 'foo', sub { 'unsafe content' });
720  
721 Construct a new L<Mojo::DOM> object for an HTML/XML tag with or without attributes and content. The C<data> attribute
722 may contain a hash reference with key/value pairs to generate attributes from.
723  
724 # "<br>"
725 $dom->new_tag('br');
726  
727 # "<div></div>"
728 $dom->new_tag('div');
729  
730 # "<div id="foo" hidden></div>"
731 $dom->new_tag('div', id => 'foo', hidden => undef);
732  
733 # "<div>test &amp; 123</div>"
734 $dom->new_tag('div', 'test & 123');
735  
736 # "<div id="foo">test &amp; 123</div>"
737 $dom->new_tag('div', id => 'foo', 'test & 123');
738  
739 # "<div data-foo="1" data-bar="test">test &amp; 123</div>""
740 $dom->new_tag('div', data => {foo => 1, Bar => 'test'}, 'test & 123');
741  
742 # "<div id="foo">test & 123</div>"
743 $dom->new_tag('div', id => 'foo', sub { 'test & 123' });
744  
745 # "<div>Hello<b>Mojo!</b></div>"
746 $dom->parse('<div>Hello</div>')->at('div')
747 ->append_content($dom->new_tag('b', 'Mojo!'))->root;
748  
749 =head2 next
750  
751 my $sibling = $dom->next;
752  
753 Return L<Mojo::DOM> object for next sibling element, or C<undef> if there are no more siblings.
754  
755 # "<h2>123</h2>"
756 $dom->parse('<div><h1>Test</h1><h2>123</h2></div>')->at('h1')->next;
757  
758 =head2 next_node
759  
760 my $sibling = $dom->next_node;
761  
762 Return L<Mojo::DOM> object for next sibling node, or C<undef> if there are no more siblings.
763  
764 # "456"
765 $dom->parse('<p><b>123</b><!-- Test -->456</p>')
766 ->at('b')->next_node->next_node;
767  
768 # " Test "
769 $dom->parse('<p><b>123</b><!-- Test -->456</p>')
770 ->at('b')->next_node->content;
771  
772 =head2 parent
773  
774 my $parent = $dom->parent;
775  
776 Return L<Mojo::DOM> object for parent of this node, or C<undef> if this node has no parent.
777  
778 # "<b><i>Test</i></b>"
779 $dom->parse('<p><b><i>Test</i></b></p>')->at('i')->parent;
780  
781 =head2 parse
782  
783 $dom = $dom->parse('<foo bar="baz">I ♥ Mojolicious!</foo>');
784  
785 Parse HTML/XML fragment with L<Mojo::DOM::HTML>.
786  
787 # Parse XML
788 my $dom = Mojo::DOM->new->xml(1)->parse('<foo>I ♥ Mojolicious!</foo>');
789  
790 =head2 preceding
791  
792 my $collection = $dom->preceding;
793 my $collection = $dom->preceding('div ~ p');
794  
795 Find all sibling elements before this node matching the CSS selector and return a L<Mojo::Collection> object containing
796 these elements as L<Mojo::DOM> objects. All selectors from L<Mojo::DOM::CSS/"SELECTORS"> are supported.
797  
798 # List tags of sibling elements before this node
799 say $dom->preceding->map('tag')->join("\n");
800  
801 =head2 preceding_nodes
802  
803 my $collection = $dom->preceding_nodes;
804  
805 Return a L<Mojo::Collection> object containing all sibling nodes before this node as L<Mojo::DOM> objects.
806  
807 # "A"
808 $dom->parse('A<!-- B --><p>C</p>')->at('p')->preceding_nodes->first->content;
809  
810 =head2 prepend
811  
812 $dom = $dom->prepend('<p>I ♥ Mojolicious!</p>');
813 $dom = $dom->prepend(Mojo::DOM->new);
814  
815 Prepend HTML/XML fragment to this node (for all node types other than C<root>).
816  
817 # "<div><h1>Test</h1><h2>123</h2></div>"
818 $dom->parse('<div><h2>123</h2></div>')
819 ->at('h2')->prepend('<h1>Test</h1>')->root;
820  
821 # "<p>Test 123</p>"
822 $dom->parse('<p>123</p>')
823 ->at('p')->child_nodes->first->prepend('Test ')->root;
824  
825 =head2 prepend_content
826  
827 $dom = $dom->prepend_content('<p>I ♥ Mojolicious!</p>');
828 $dom = $dom->prepend_content(Mojo::DOM->new);
829  
830 Prepend HTML/XML fragment (for C<root> and C<tag> nodes) or raw content to this node's content.
831  
832 # "<div><h2>Test123</h2></div>"
833 $dom->parse('<div><h2>123</h2></div>')
834 ->at('h2')->prepend_content('Test')->root;
835  
836 # "<!-- Test 123 --><br>"
837 $dom->parse('<!-- 123 --><br>')
838 ->child_nodes->first->prepend_content(' Test')->root;
839  
840 # "<p><i>123</i>Test</p>"
841 $dom->parse('<p>Test</p>')->at('p')->prepend_content('<i>123</i>')->root;
842  
843 =head2 previous
844  
845 my $sibling = $dom->previous;
846  
847 Return L<Mojo::DOM> object for previous sibling element, or C<undef> if there are no more siblings.
848  
849 # "<h1>Test</h1>"
850 $dom->parse('<div><h1>Test</h1><h2>123</h2></div>')->at('h2')->previous;
851  
852 =head2 previous_node
853  
854 my $sibling = $dom->previous_node;
855  
856 Return L<Mojo::DOM> object for previous sibling node, or C<undef> if there are no more siblings.
857  
858 # "123"
859 $dom->parse('<p>123<!-- Test --><b>456</b></p>')
860 ->at('b')->previous_node->previous_node;
861  
862 # " Test "
863 $dom->parse('<p>123<!-- Test --><b>456</b></p>')
864 ->at('b')->previous_node->content;
865  
866 =head2 remove
867  
868 my $parent = $dom->remove;
869  
870 Remove this node and return L</"root"> (for C<root> nodes) or L</"parent">.
871  
872 # "<div></div>"
873 $dom->parse('<div><h1>Test</h1></div>')->at('h1')->remove;
874  
875 # "<p><b>456</b></p>"
876 $dom->parse('<p>123<b>456</b></p>')
877 ->at('p')->child_nodes->first->remove->root;
878  
879 =head2 replace
880  
881 my $parent = $dom->replace('<div>I ♥ Mojolicious!</div>');
882 my $parent = $dom->replace(Mojo::DOM->new);
883  
884 Replace this node with HTML/XML fragment and return L</"root"> (for C<root> nodes) or L</"parent">.
885  
886 # "<div><h2>123</h2></div>"
887 $dom->parse('<div><h1>Test</h1></div>')->at('h1')->replace('<h2>123</h2>');
888  
889 # "<p><b>123</b></p>"
890 $dom->parse('<p>Test</p>')
891 ->at('p')->child_nodes->[0]->replace('<b>123</b>')->root;
892  
893 =head2 root
894  
895 my $root = $dom->root;
896  
897 Return L<Mojo::DOM> object for C<root> node.
898  
899 =head2 selector
900  
901 my $selector = $dom->selector;
902  
903 Get a unique CSS selector for this element.
904  
905 # "ul:nth-child(1) > li:nth-child(2)"
906 $dom->parse('<ul><li>Test</li><li>123</li></ul>')->find('li')->last->selector;
907  
908 # "p:nth-child(1) > b:nth-child(1) > i:nth-child(1)"
909 $dom->parse('<p><b><i>Test</i></b></p>')->at('i')->selector;
910  
911 =head2 strip
912  
913 my $parent = $dom->strip;
914  
915 Remove this element while preserving its content and return L</"parent">.
916  
917 # "<div>Test</div>"
918 $dom->parse('<div><h1>Test</h1></div>')->at('h1')->strip;
919  
920 =head2 tag
921  
922 my $tag = $dom->tag;
923 $dom = $dom->tag('div');
924  
925 This element's tag name.
926  
927 # List tag names of child elements
928 say $dom->children->map('tag')->join("\n");
929  
930 =head2 tap
931  
932 $dom = $dom->tap(sub {...});
933  
934 Alias for L<Mojo::Base/"tap">.
935  
936 =head2 text
937  
938 my $text = $dom->text;
939  
940 Extract text content from this element only (not including child elements).
941  
942 # "bar"
943 $dom->parse("<div>foo<p>bar</p>baz</div>")->at('p')->text;
944  
945 # "foo\nbaz\n"
946 $dom->parse("<div>foo\n<p>bar</p>baz\n</div>")->at('div')->text;
947  
948 To extract text content from all descendant nodes see L</"all_text">.
949  
950 =head2 to_string
951  
952 my $str = $dom->to_string;
953  
954 Render this node and its content to HTML/XML.
955  
956 # "<b>Test</b>"
957 $dom->parse('<div><b>Test</b></div>')->at('div b')->to_string;
958  
959 =head2 tree
960  
961 my $tree = $dom->tree;
962 $dom = $dom->tree(['root']);
963  
964 Document Object Model. Note that this structure should only be used very carefully since it is very dynamic.
965  
966 =head2 type
967  
968 my $type = $dom->type;
969  
970 This node's type, usually C<cdata>, C<comment>, C<doctype>, C<pi>, C<raw>, C<root>, C<tag> or C<text>.
971  
972 # "cdata"
973 $dom->parse('<![CDATA[Test]]>')->child_nodes->first->type;
974  
975 # "comment"
976 $dom->parse('<!-- Test -->')->child_nodes->first->type;
977  
978 # "doctype"
979 $dom->parse('<!DOCTYPE html>')->child_nodes->first->type;
980  
981 # "pi"
982 $dom->parse('<?xml version="1.0"?>')->child_nodes->first->type;
983  
984 # "raw"
985 $dom->parse('<title>Test</title>')->at('title')->child_nodes->first->type;
986  
987 # "root"
988 $dom->parse('<p>Test</p>')->type;
989  
990 # "tag"
991 $dom->parse('<p>Test</p>')->at('p')->type;
992  
993 # "text"
994 $dom->parse('<p>Test</p>')->at('p')->child_nodes->first->type;
995  
996 =head2 val
997  
998 my $value = $dom->val;
999  
1000 Extract value from form element (such as C<button>, C<input>, C<option>, C<select> and C<textarea>), or return C<undef>
1001 if this element has no value. In the case of C<select> with C<multiple> attribute, find C<option> elements with
1002 C<selected> attribute and return an array reference with all values, or C<undef> if none could be found.
1003  
1004 # "a"
1005 $dom->parse('<input name=test value=a>')->at('input')->val;
1006  
1007 # "b"
1008 $dom->parse('<textarea>b</textarea>')->at('textarea')->val;
1009  
1010 # "c"
1011 $dom->parse('<option value="c">Test</option>')->at('option')->val;
1012  
1013 # "d"
1014 $dom->parse('<select><option selected>d</option></select>')
1015 ->at('select')->val;
1016  
1017 # "e"
1018 $dom->parse('<select multiple><option selected>e</option></select>')
1019 ->at('select')->val->[0];
1020  
1021 # "on"
1022 $dom->parse('<input name=test type=checkbox>')->at('input')->val;
1023  
1024 =head2 with_roles
1025  
1026 my $new_class = Mojo::DOM->with_roles('Mojo::DOM::Role::One');
1027 my $new_class = Mojo::DOM->with_roles('+One', '+Two');
1028 $dom = $dom->with_roles('+One', '+Two');
1029  
1030 Alias for L<Mojo::Base/"with_roles">.
1031  
1032 =head2 wrap
1033  
1034 $dom = $dom->wrap('<div></div>');
1035 $dom = $dom->wrap(Mojo::DOM->new);
1036  
1037 Wrap HTML/XML fragment around this node (for all node types other than C<root>), placing it as the last child of the
1038 first innermost element.
1039  
1040 # "<p>123<b>Test</b></p>"
1041 $dom->parse('<b>Test</b>')->at('b')->wrap('<p>123</p>')->root;
1042  
1043 # "<div><p><b>Test</b></p>123</div>"
1044 $dom->parse('<b>Test</b>')->at('b')->wrap('<div><p></p>123</div>')->root;
1045  
1046 # "<p><b>Test</b></p><p>123</p>"
1047 $dom->parse('<b>Test</b>')->at('b')->wrap('<p></p><p>123</p>')->root;
1048  
1049 # "<p><b>Test</b></p>"
1050 $dom->parse('<p>Test</p>')->at('p')->child_nodes->first->wrap('<b>')->root;
1051  
1052 =head2 wrap_content
1053  
1054 $dom = $dom->wrap_content('<div></div>');
1055 $dom = $dom->wrap_content(Mojo::DOM->new);
1056  
1057 Wrap HTML/XML fragment around this node's content (for C<root> and C<tag> nodes), placing it as the last children of
1058 the first innermost element.
1059  
1060 # "<p><b>123Test</b></p>"
1061 $dom->parse('<p>Test<p>')->at('p')->wrap_content('<b>123</b>')->root;
1062  
1063 # "<p><b>Test</b></p><p>123</p>"
1064 $dom->parse('<b>Test</b>')->wrap_content('<p></p><p>123</p>');
1065  
1066 =head2 xml
1067  
1068 my $bool = $dom->xml;
1069 $dom = $dom->xml($bool);
1070  
1071 Disable HTML semantics in parser and activate case-sensitivity, defaults to auto-detection based on XML declarations.
1072  
1073 =head1 OPERATORS
1074  
1075 L<Mojo::DOM> overloads the following operators.
1076  
1077 =head2 array
1078  
1079 my @nodes = @$dom;
1080  
1081 Alias for L</"child_nodes">.
1082  
1083 # "<!-- Test -->"
1084 $dom->parse('<!-- Test --><b>123</b>')->[0];
1085  
1086 =head2 bool
1087  
1088 my $bool = !!$dom;
1089  
1090 Always true.
1091  
1092 =head2 hash
1093  
1094 my %attrs = %$dom;
1095  
1096 Alias for L</"attr">.
1097  
1098 # "test"
1099 $dom->parse('<div id="test">Test</div>')->at('div')->{id};
1100  
1101 =head2 stringify
1102  
1103 my $str = "$dom";
1104  
1105 Alias for L</"to_string">.
1106  
1107 =head1 SEE ALSO
1108  
1109 L<Mojolicious>, L<Mojolicious::Guides>, L<https://mojolicious.org>.
1110  
1111 =cut
« Mojolicious/Types.pm ↑ Summary Mojo/Asset/File.pm »