DOM/Node.pm

filter: Statementstmt 98.1% 207 / 211 Branchbran 90.0% 81 / 90 Conditioncond 77.1% 27 / 35 MC/DCmcdc 31.8% 7 / 22 Subroutinesub 100% 38 / 38 Podpod 80.7% 21 / 26 totaltotal 90.2% 381 / 422 CC 81 SCAR 20.6
CC
81
Cov
94%
insertBefore
16.1
replaceChild
16.1
appendChild
13.2
CRAP 7.9
« DOM/Element/Form.pm ↑ Summary DOM/Attr.pm »
1 package HTML::DOM::Node;
2  
3 28 our $VERSION = '0.058';
4  
5  
6 28 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 28 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 28 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
10 28 ELEMENT_NODE => 1,
11 ATTRIBUTE_NODE => 2,
12 TEXT_NODE => 3,
13 CDATA_SECTION_NODE => 4,
14 ENTITY_REFERENCE_NODE => 5,
15 ENTITY_NODE => 6,
16 PROCESSING_INSTRUCTION_NODE => 7,
17 COMMENT_NODE => 8,
18 DOCUMENT_NODE => 9,
19 DOCUMENT_TYPE_NODE => 10,
20 DOCUMENT_FRAGMENT_NODE => 11,
21 NOTATION_NODE => 12,
22 28 };
23  
24 28 ▶use Exporter 5.57 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
25 28 ▶use HTML::DOM::Event;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 28 ▶use HTML::DOM::Exception qw'NO_MODIFICATION_ALLOWED_ERR NOT_FOUND_ERR
Subroutine BEGIN: called CC=1 SCAR=0.0
27 HIERARCHY_REQUEST_ERR
28 28 UNSPECIFIED_EVENT_TYPE_ERR';
29 28 ▶use Scalar::Util qw'refaddr weaken blessed';
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 28 require HTML::DOM::EventTarget;
32 28 require HTML::DOM::Implementation;
33 28 require HTML::DOM::NodeList;
34 28 require HTML::DOM::_Element;
35  
36 28 our @ISA =('HTML::DOM::_Element', # No, a node isn't an HTML element,
37 'HTML::DOM::EventTarget'); # but HTML::DOM::_Element (forked from
38 # HTML::Element) has some nice tree-handling
39 # methods (and, after all, TreeBuilder's
40 # pseudo-elements aren't elements either).
41  
42 28 our @EXPORT_OK = qw'
43 ELEMENT_NODE
44 ATTRIBUTE_NODE
45 TEXT_NODE
46 CDATA_SECTION_NODE
47 ENTITY_REFERENCE_NODE
48 ENTITY_NODE
49 PROCESSING_INSTRUCTION_NODE
50 COMMENT_NODE
51 DOCUMENT_NODE
52 DOCUMENT_TYPE_NODE
53 DOCUMENT_FRAGMENT_NODE
54 NOTATION_NODE
55 ';
56 28 our %EXPORT_TAGS = (all => \@EXPORT_OK);
57  
58  
59  
60 =head1 NAME
61  
62 HTML::DOM::Node - A Perl class for representing the nodes of an HTML DOM tree
63  
64 =head1 VERSION
65  
66 Version 0.058
67  
68 =head1 SYNOPSIS
69  
70 use HTML::DOM::Node ':all'; # constants
71 use HTML::DOM;
72 $doc = HTML::DOM->new;
73 $doc->isa('HTML::DOM::Node'); # true
74 $doc->nodeType == DOCUMENT_NODE; # true
75  
76 $doc->firstChild;
77 $doc->childNodes;
78 # etc
79  
80 =head1 DESCRIPTION
81  
82 This is the base class for all nodes in an HTML::DOM tree. (See
83 L<HTML::DOM/CLASSES AND DOM INTERFACES>.) It implements the Node
84 interface, and, indirectly, the EventTarget interface (see
85 L<HTML::DOM::EventTarget>.
86  
87 =head1 METHODS
88  
89 =head2 Attributes
90  
91 The following DOM attributes are supported:
92  
93 =over 4
94  
95 =item nodeName
96  
97 =item nodeType
98  
99 These two are implemented not by HTML::DOM::Node itself, but by its
100 subclasses.
101  
102 =item nodeValue
103  
104 =item parentNode
105  
106 =item childNodes
107  
108 =item firstChild
109  
110 =item lastChild
111  
112 =item previousSibling
113  
114 =item nextSibling
115  
116 =item attributes
117  
118 =item ownerDocument
119  
120 =item namespaceURI
121  
122 =item prefix
123  
124 =item localName
125  
126 Those last three always return nothing.
127  
128 =back
129  
130 There is also a C<_set_ownerDocument> method, which you probably do not
131 need to know about.
132  
133 =cut
134  
135 # ----------- ATTRIBUTE METHODS ------------- #
136  
137 # sub nodeName {} # every subclass overrides this
138 # sub nodeType {} # likewise
139  
140 3 ▶sub nodeValue {
Subroutine nodeValue: called CC=2 SCAR=8.1
Pod documented
141 3 ▶ if(@_ > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) 0 3 3
142 0 die new HTML::DOM::Exception
143 NO_MODIFICATION_ALLOWED_ERR,
144 'Read-only node';# ~~~ only when the node is
145 # readonly
146 }
147 3 return; # empty list
148 }
149  
150 4840 ▶sub parentNode {
Subroutine parentNode: called CC=2 SCAR=6.9
Pod documented
151 4840 my $p = $_[0]->parent;
152 4840 ▶ defined $p ? $p :()
Branch: 1 branch
BranchTrueFalseTotal
defined $p ? : 2998 1842 4840
153 }
154  
155 63 ▶sub childNodes {
Subroutine childNodes: called CC=2 SCAR=6.9
Pod documented
156 63 ▶ wantarray ? $_[0]->content_list :
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 40 23 63
157 new HTML::DOM::NodeList $_[0]->content_array_ref;
158 }
159  
160 85 ▶sub firstChild {
Subroutine firstChild: called CC=1 SCAR=0.0
Pod documented
161 85 ($_[0]->content_list)[0];
162 }
163  
164 7 ▶sub lastChild {
Subroutine lastChild: called CC=1 SCAR=0.0
Pod documented
165 7 ($_[0]->content_list)[-1];
166 }
167  
168 2 ▶sub previousSibling {
Subroutine previousSibling: called CC=2 SCAR=6.9
Pod documented
169 2 my $sib = scalar $_[0]->left;
170 2 ▶ defined $sib ? $sib : ();
Branch: 1 branch
BranchTrueFalseTotal
defined $sib ? : 1 1 2
171 }
172  
173 14 ▶sub nextSibling {
Subroutine nextSibling: called CC=2 SCAR=6.9
Pod documented
174 14 my $sib = scalar $_[0]->right;
175 14 ▶ defined $sib ? $sib : ();
Branch: 1 branch
BranchTrueFalseTotal
defined $sib ? : 7 7 14
176 }
177  
178 8 ▶sub attributes {} # null for most nodes; overridden by Element
Subroutine attributes: called CC=1 SCAR=0.0
Pod documented
179  
180 9122 ▶sub ownerDocument {
Subroutine ownerDocument: called CC=3 SCAR=11.0
Pod documented
181 9122 my $self = shift;
182 9122 ▶ $$self{_HTML_DOM_Node_owner} || do {
Branch: 1 branch
BranchTrueFalseTotal
$self->{"_HTML_DOM_Node_owner"} or do { my $root = $self->root; $self->{"_HTML_DOM_Node_owner"} = $root->{"_HTML_DOM_Node_owner"} || $root; builtin::weaken($self->{"_HTML_DOM_Node_owner"}); $self->{"_HTML_DOM_Node_owner"} } 8697 425 9122
183 425 my $root = $self->root;
184 # ~~~ I’m not sure this logic is right. I need to revisit
185 # this. Do we ever have a case in which ->root returns
186 # the wrong value? If so, can we guarantee that the
187 # ‘root’ has its _HTML_DOM_Node_owner attribute set?
188 $$self{_HTML_DOM_Node_owner} =
189 425 ▶ $$root{_HTML_DOM_Node_owner} || $root;
Condition66%2/3
$root->{'_HTML_DOM_Node_owner'} || $root
l!l&&r!l&&!r
204050
MC/DC0%0/2
$root->{'_HTML_DOM_Node_owner'} || $root
$root->{'_HTML_DOM_Node_owner'} $root
Truth table66%2/3
A || B
A= $root->{'_HTML_DOM_Node_owner'}B= $root
ABresult
1X1
011
000
190 425 weaken $$self{_HTML_DOM_Node_owner};
191 $$self{_HTML_DOM_Node_owner}
192 425 };
193 }
194  
195 1152 ▶sub _set_ownerDocument {
Subroutine _set_ownerDocument: called CC=1 SCAR=0.0
196 1152 $_[0]{_HTML_DOM_Node_owner} = $_[1];
197 1152 weaken $_[0]{_HTML_DOM_Node_owner};
198 }
199  
200 28 *prefix = *localName = *namespaceURI = *attributes;
201  
202  
203 =head2 Other Methods
204  
205 See the DOM spec. for descriptions of most of these. The first four
206 automatically trigger mutation events. (See L<HTML::DOM::Event::Mutation>.)
207  
208 =over 4
209  
210 =item insertBefore
211  
212 =item replaceChild
213  
214 =item removeChild
215  
216 =item appendChild
217  
218 =item hasChildNodes
219  
220 =item cloneNode
221  
222 =item normalize
223  
224 =item hasAttributes
225  
226 =item isSupported
227  
228 =cut
229  
230 # ----------- METHOD METHODS ------------- #
231  
232 15 ▶sub insertBefore {
Subroutine insertBefore: called CC=16 SCAR=27.8
Pod documented
233 # ~~~ NO_MODIFICATION_ALLOWED_ERR is meant to be raised if the
234 # node is read-only.
235 # ~~~ HIERARCHY_REQUEST_ERR is also supposed to be raised if the
236 # node type does not allow children of $new_node's type.
237  
238 15 my($self,$new_node,$before) = @_;
239  
240 15 ▶ $self->is_inside($new_node) and
Branch: 1 branch
BranchTrueFalseTotal
$self->is_inside($new_node) and die "HTML::DOM::Exception"->new(3, "A node cannot be inserted into one of its descendants") 1 14 15
241 die new HTML::DOM::Exception HIERARCHY_REQUEST_ERR,
242 'A node cannot be inserted into one of its descendants';
243  
244 14 ▶ my $doc = $self->ownerDocument || $self;
Condition66%2/3
$self->ownerDocument || $self
l!l&&r!l&&!r
1310
MC/DC0%0/2
$self->ownerDocument || $self
$self->ownerDocument $self
Truth table66%2/3
A || B
A= $self->ownerDocumentB= $self
ABresult
1X1
011
000
245  
246 14 my $index;
247 14 my @kids = $self->content_list;
248 14 ▶ if($before) { FIND_INDEX: {
Branch: 1 branch
BranchTrueFalseTotal
if ($before) { } 7 7 14
249 7 for (0..$#kids) {
250 9 ▶ $kids[$_] == $before
Branch: 1 branch
BranchTrueFalseTotal
$kids[$_] == $before and $index = $_, last FIND_INDEX 6 3 9
251 and $index = $_, last FIND_INDEX;
252 }
253 1 die new HTML::DOM::Exception NOT_FOUND_ERR,
254 'insertBefore\'s 2nd argument is not a child of this node';
255 }}
256 else {
257 7 $index = @kids;
258 }
259  
260 #$new_node->can('parent') or warn JE::Code::add_line_number("cant parent");
261 13 my $old_parent = $new_node->parent;
262 13 ▶ $old_parent and $new_node->trigger_event('DOMNodeRemoved',
Branch: 1 branch
BranchTrueFalseTotal
$old_parent and $new_node->trigger_event("DOMNodeRemoved", "rel_node", $old_parent) 6 7 13
263 rel_node => $old_parent);
264 13 my $was_inside_doc = $new_node->is_inside($doc);
265 13 ▶ if($was_inside_doc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($was_inside_doc) 4 9 13
266 $_->trigger_event('DOMNodeRemovedFromDocument')
267 4 for $new_node, $new_node->descendants;
268 }
269  
270 13 ▶ $self->splice_content($index, 0, my @nodes =
Branch: 1 branch
BranchTrueFalseTotal
$new_node->isa('HTML::DOM::DocumentFragment') ? : 1 12 13
271 $new_node->isa('HTML::DOM::DocumentFragment')
272 ? $new_node->childNodes
273 : $new_node
274 );
275 13 $_->_set_ownerDocument($doc) for @nodes;
276  
277 13 $new_node->trigger_event('DOMNodeInserted', rel_node => $self);
278 13 ▶ if($self->is_inside($doc)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_inside($doc)) 6 7 13
279 6 for($new_node, $new_node->descendants) {
280 31 ▶ if(
Branch: 1 branch
BranchTrueFalseTotal
if (not $was_inside_doc and my $sub = $doc->elem_handler(lc $_->tag)) 0 31 31
Condition66%2/3
not $was_inside_doc and my $sub = $doc->elem_handler(lc $_->tag)
!ll&&!rl&&r
16150
MC/DC0%0/2
not $was_inside_doc and my $sub = $doc->elem_handler(lc $_->tag)
not $was_inside_doc my $sub = $doc->elem_handler(lc $_->tag)
Truth table66%2/3
not(A) and B
A= not $was_inside_docB= my $sub = $doc->elem_handler(lc $_->tag)
ABresult
0X0
100
111
281 !$was_inside_doc
282 and my $sub = $doc->elem_handler(lc $_->tag)
283 ) {
284 0 &$sub($doc,$_)
285 }
286 31 $_->trigger_event('DOMNodeInsertedIntoDocument')
287 }
288 }
289 $_->trigger_event('DOMSubtreeModified')
290 13 for _nearest_common_parent($old_parent, $self);
291  
292 13 $doc->_modified;
293  
294 13 $new_node;
295 }
296  
297 18 ▶sub replaceChild {
Subroutine replaceChild: called CC=16 SCAR=27.8
Pod documented
298 # ~~~ NO_MODIFICATION_ALLOWED_ERR is meant to be raised if the
299 # node is read-only.
300 # ~~~ HIERARCHY_REQUEST_ERR is also supposed to be raised if the
301 # node type does not allow children of $new_node's type.
302  
303 18 my($self,$new_node,$old_node) = @_;
304  
305 18 ▶ $self->is_inside($new_node) and
Branch: 1 branch
BranchTrueFalseTotal
$self->is_inside($new_node) and die "HTML::DOM::Exception"->new(3, "A node cannot be inserted into one of its descendants") 1 17 18
306 die new HTML::DOM::Exception HIERARCHY_REQUEST_ERR,
307 'A node cannot be inserted into one of its descendants';
308  
309 17 ▶ my $doc = $self->ownerDocument || $self;
Condition66%2/3
$self->ownerDocument || $self
l!l&&r!l&&!r
1610
MC/DC0%0/2
$self->ownerDocument || $self
$self->ownerDocument $self
Truth table66%2/3
A || B
A= $self->ownerDocumentB= $self
ABresult
1X1
011
000
310  
311 28 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
312 17 ▶ $self == $old_node->parent or
Branch: 1 branch
BranchTrueFalseTotal
$self == $old_node->parent or die "HTML::DOM::Exception"->new(8, "replaceChild's 2nd argument is not a child of this node") 16 1 17
313 die new HTML::DOM::Exception NOT_FOUND_ERR,
314 'replaceChild\'s 2nd argument is not a child of this node';
315  
316 16 $old_node->trigger_event('DOMNodeRemoved',
317 rel_node => $self);
318 16 my $in_doc = $self->is_inside($doc);
319 16 ▶ if($in_doc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($in_doc) 9 7 16
320 $_->trigger_event('DOMNodeRemovedFromDocument')
321 9 for $old_node, $old_node->descendants;
322 }
323 16 my $old_parent = $new_node->parent;
324 16 ▶ $old_parent and $new_node->trigger_event('DOMNodeRemoved',
Branch: 1 branch
BranchTrueFalseTotal
$old_parent and $new_node->trigger_event("DOMNodeRemoved", "rel_node", $old_parent) 6 10 16
325 rel_node => $old_parent);
326 16 ▶ if($new_node->is_inside($doc) && !$new_node->is_inside($old_node)){
Branch: 1 branch
BranchTrueFalseTotal
if ($new_node->is_inside($doc) and not $new_node->is_inside($old_node)) 3 13 16
Condition100%3/3
$new_node->is_inside($doc) and not $new_node->is_inside($old_node)
!ll&&!rl&&r
1213
MC/DC100%2/2
$new_node->is_inside($doc) and not $new_node->is_inside($old_node)
$new_node->is_inside($doc) not $new_node->is_inside($old_node)
Truth table100%3/3
A and not(B)
A= $new_node->is_inside($doc)B= not $new_node->is_inside($old_node)
ABresult
0X0
100
111
327 $_->trigger_event('DOMNodeRemovedFromDocument')
328 3 for $new_node, $new_node->descendants;
329 }
330  
331 # If the owner is not set explicitly inside the node, it will lose
332 # its owner. The ownerDocument method sets it if it is not
333 # already set.
334 16 $old_node->ownerDocument;
335  
336 16 ▶ my $ret = $old_node->replace_with(
Branch: 1 branch
BranchTrueFalseTotal
$new_node->isa('HTML::DOM::DocumentFragment') ? : 1 15 16
337 my @nodes
338 = $new_node->isa('HTML::DOM::DocumentFragment')
339 ? $new_node->childNodes
340 : $new_node
341 );
342 16 $_->_set_ownerDocument($doc) for @nodes;
343  
344 16 $new_node->trigger_event('DOMNodeInserted', rel_node => $self);
345 16 ▶ if($in_doc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($in_doc) 9 7 16
346 9 for($new_node, $new_node->descendants) {
347 65 ▶ if(my $sub = $doc->elem_handler(lc $_->tag)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $sub = $doc->elem_handler(lc $_->tag)) 0 65 65
348 0 &$sub($doc,$_)
349 }
350 65 $_->trigger_event('DOMNodeInsertedIntoDocument')
351 }
352 }
353 $_->trigger_event('DOMSubtreeModified')
354 16 for _nearest_common_parent($old_parent, $self);
355  
356 16 $doc->_modified;
357  
358 16 $ret;
359 }
360  
361 27 ▶sub removeChild {
Subroutine removeChild: called CC=5 SCAR=16.1
Pod documented
362 # ~~~ NO_MODIFICATION_ALLOWED_ERR is meant to be raised if the
363 # node is read-only.
364  
365 27 my($self,$child) = @_;
366  
367 28 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
368 27 ▶ $self == $child->parent or
Branch: 1 branch
BranchTrueFalseTotal
$self == $child->parent or die "HTML::DOM::Exception"->new(8, "removeChild's argument is not a child of this node") 26 1 27
369 die new HTML::DOM::Exception NOT_FOUND_ERR,
370 'removeChild\'s argument is not a child of this node';
371  
372 # If the owner is not set explicitly inside the node, it will lose
373 # its owner. The ownerDocument method sets it if it is not
374 # already set.
375 26 my $doc = $child->ownerDocument;
376  
377 26 $child->trigger_event('DOMNodeRemoved',
378 rel_node => $self);
379 26 ▶ if($child->is_inside($doc)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($child->is_inside($doc)) 21 5 26
380 $_->trigger_event('DOMNodeRemovedFromDocument')
381 21 for $child, $child->descendants;
382 }
383  
384 26 $child->detach;
385  
386 26 $self->trigger_event('DOMSubtreeModified');
387  
388 26 ▶ {($self->ownerDocument||next)->_modified;}
Condition100%2/2
$self->ownerDocument || (next)
l!l
224
MC/DC100%1/1
$self->ownerDocument || (next)
$self->ownerDocument
Truth table100%2/2
A
A= $self->ownerDocument
Aresult
11
00
389  
390 26 $child;
391 }
392  
393 134 ▶sub appendChild {
Subroutine appendChild: called CC=13 SCAR=25.8
Pod documented
394 # ~~~ NO_MODIFICATION_ALLOWED_ERR is meant to be raised if the
395 # node is read-only.
396 # ~~~ HIERARCHY_REQUEST_ERR is also supposed to be raised if the
397 # node type does not allow children of $new_node's type.
398  
399 134 my($self,$new_node) = @_;
400  
401 134 ▶ $self->is_inside($new_node) and
Branch: 1 branch
BranchTrueFalseTotal
$self->is_inside($new_node) and die "HTML::DOM::Exception"->new(3, "A node cannot be inserted into one of its descendants") 1 133 134
402 die new HTML::DOM::Exception HIERARCHY_REQUEST_ERR,
403 'A node cannot be inserted into one of its descendants';
404  
405 133 ▶ my $doc = $self->ownerDocument || $self;
Condition66%2/3
$self->ownerDocument || $self
l!l&&r!l&&!r
12850
MC/DC0%0/2
$self->ownerDocument || $self
$self->ownerDocument $self
Truth table66%2/3
A || B
A= $self->ownerDocumentB= $self
ABresult
1X1
011
000
406  
407 133 my $old_parent = $new_node->parent;
408 133 ▶ $old_parent and $new_node->trigger_event('DOMNodeRemoved',
Branch: 1 branch
BranchTrueFalseTotal
$old_parent and $new_node->trigger_event("DOMNodeRemoved", "rel_node", $old_parent) 7 126 133
409 rel_node => $old_parent);
410 133 my $was_inside_doc = $new_node->is_inside($doc);
411 133 ▶ if($was_inside_doc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($was_inside_doc) 5 128 133
412 $_->trigger_event('DOMNodeRemovedFromDocument')
413 5 for $new_node, $new_node->descendants;
414 }
415  
416 $self->push_content(
417 133 ▶ my @nodes = $new_node->isa('HTML::DOM::DocumentFragment')
Branch: 1 branch
BranchTrueFalseTotal
$new_node->isa('HTML::DOM::DocumentFragment') ? : 1 132 133
418 ? $new_node->childNodes
419 : $new_node
420 );
421 133 $_->_set_ownerDocument($doc) for @nodes;
422  
423 133 $new_node->trigger_event('DOMNodeInserted', rel_node => $self);
424 133 ▶ if($self->is_inside($doc)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_inside($doc)) 27 106 133
425 27 for($new_node, $new_node->descendants) {
426 56 ▶ if(
Branch: 1 branch
BranchTrueFalseTotal
if (not $was_inside_doc and my $sub = $doc->elem_handler(lc $_->tag)) 0 56 56
Condition66%2/3
not $was_inside_doc and my $sub = $doc->elem_handler(lc $_->tag)
!ll&&!rl&&r
16400
MC/DC0%0/2
not $was_inside_doc and my $sub = $doc->elem_handler(lc $_->tag)
not $was_inside_doc my $sub = $doc->elem_handler(lc $_->tag)
Truth table66%2/3
not(A) and B
A= not $was_inside_docB= my $sub = $doc->elem_handler(lc $_->tag)
ABresult
0X0
100
111
427 !$was_inside_doc
428 and my $sub = $doc->elem_handler(lc $_->tag)
429 ) {
430 0 &$sub($doc,$_)
431 }
432 56 $_->trigger_event('DOMNodeInsertedIntoDocument')
433 }
434 }
435 $_->trigger_event('DOMSubtreeModified')
436 133 for _nearest_common_parent($old_parent, $self);
437  
438 133 $doc->_modified;
439  
440 133 $new_node;
441 }
442  
443 # This is used to determine who gets a DOMSubtreeModified event. Despite
444 # its name, it may choose one of the two nodes passed to it if one is the
445 # parent of the other. If neither of the nodes is in the same tree, they
446 # are both returned. The first arg may be undef, in which case the 2nd
447 # is returned.
448 162 ▶sub _nearest_common_parent {
Subroutine _nearest_common_parent: called CC=7 SCAR=19.5
449 162 my ($node1,$node2)=@_;
450 162 ▶ !defined $node1 and return $node2;
Branch: 1 branch
BranchTrueFalseTotal
defined $node1 or return $node2 19 143 162
451 19 ▶ $node1->root != $node2->root and return $node1, $node2;
Branch: 1 branch
BranchTrueFalseTotal
$node1->root != $node2->root and return $node1, $node2 8 11 19
452 11 my $addr1 = $node1->address;
453 11 my $addr2 = $node2->address;
454 11 ▶ while(substr $addr1, 0, length $addr2, ne $addr2 and
Branch: 1 branch
BranchTrueFalseTotal
if (substr($addr1, 0, length $addr2) ne $addr2 and substr($addr2, 0, length $addr1) ne $addr1) 3 11 14
Condition100%3/3
substr($addr1, 0, length $addr2) ne $addr2 and substr($addr2, 0, length $addr1) ne $addr1
!ll&&!rl&&r
833
MC/DC100%2/2
substr($addr1, 0, length $addr2) ne $addr2 and substr($addr2, 0, length $addr1) ne $addr1
substr($addr1, 0, length $addr2) ne $addr2 substr($addr2, 0, length $addr1) ne $addr1
Truth table100%3/3
A and B
A= substr($addr1, 0, length $addr2) ne $addr2B= substr($addr2, 0, length $addr1) ne $addr1
ABresult
0X0
100
111
455 substr $addr2, 0, length $addr1, ne $addr1) {
456 3 s/\.[^.]*\z// for $addr1, $addr2;
457 }
458 $node2->address(
459 11 ▶ length $addr1 < length $addr2 ? $addr1 : $addr2
Branch: 1 branch
BranchTrueFalseTotal
length $addr1 < length $addr2 ? : 3 8 11
460 )
461 }
462  
463 3 ▶sub hasChildNodes {
Subroutine hasChildNodes: called CC=1 SCAR=0.0
Pod documented
464 3 !!$_[0]->content_list
465 }
466  
467 13 ▶sub cloneNode {
Subroutine cloneNode: called CC=3 SCAR=11.0
Pod documented
468 13 my($self,$deep) = @_;
469 13 ▶ if($deep) {
Branch: 1 branch
BranchTrueFalseTotal
if ($deep) { } 7 6 13
470 7 (my $clown = $self->clone)
471 ->_set_ownerDocument($self->ownerDocument);
472 7 $clown;
473 }
474 else {
475 # ~~~ Do I need to reweaken any attributes?
476 6 bless +(my $clone = { %$self }), ref $self;
477 6 $clone->_set_ownerDocument($self->ownerDocument);
478 6 delete $clone->{$_} for qw/ _parent _content /;
479 6 $clone
480 }
481 }
482  
483 2 ▶sub normalize {
Subroutine normalize: called CC=7 SCAR=19.5
Pod documented
484 2 my @pile = my $self = shift;
485 2 ▶ while(@pile) {
Branch: 1 branch
BranchTrueFalseTotal
if (@pile) 11 2 13
486 11 ▶ if($pile[0]{_tag} eq '~text') {
Branch: 1 branch
BranchTrueFalseTotal
if ($pile[0]{'_tag'} eq '~text') { } 9 2 11
487 9 ▶ if($pile[0]{text} eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($pile[0]{"text"} eq "") 3 6 9
488 3 shift(@pile)->detach, next
489 }
490 6 ▶ _:{while((my $next = $pile[0]->nextSibling||next _)
Branch: 1 branch
BranchTrueFalseTotal
if (+(my $next = $pile[0]->nextSibling || (next _))->{"_tag"} eq "~text") 6 0 6
Condition100%2/2
$pile[0]->nextSibling || (next _)
l!l
66
MC/DC100%1/1
$pile[0]->nextSibling || (next _)
$pile[0]->nextSibling
Truth table100%2/2
A
A= $pile[0]->nextSibling
Aresult
11
00
491 ->{_tag} eq '~text') {
492 6 $pile[0]{text}.=$next->{text};
493 6 $next->detach;
494 }}
495 6 shift @pile;
496 }
497 else {
498 2 ▶ unshift @pile, @{(shift@pile)->{'_content'}||[]};
Condition50%1/2
shift(@pile)->{'_content'} || []
l!l
20
MC/DC0%0/1
shift(@pile)->{'_content'} || []
shift(@pile)->{'_content'}
Truth table50%1/2
A
A= shift(@pile)->{'_content'}
Aresult
11
00
499 }
500 }
501 return
502 2 }
503  
504 3 ▶sub hasAttributes {
Subroutine hasAttributes: called CC=2 SCAR=6.9
Pod documented
505 3 ▶ (shift->attributes||return 0)->length
Condition100%2/2
(shift())->attributes || (return 0)
l!l
21
MC/DC100%1/1
(shift())->attributes || (return 0)
(shift())->attributes
Truth table100%2/2
A
A= (shift())->attributes
Aresult
11
00
506 }
507  
508 2 ▶sub isSupported {
Subroutine isSupported: called CC=1 SCAR=0.0
Pod documented
509 2 my $self = shift;
510 2 $HTML::DOM::Implementation::it->hasFeature(@_)
511 }
512  
513 # ----------- EVENT STUFF ------------- #
514  
515 =item trigger_event
516  
517 This overrides L<HTML::DOM::EventTarget>'s (non-DOM) method of the same
518 name, so that
519 the document's default event handler is called.
520  
521 =cut
522  
523 1845 ▶sub trigger_event { # non-DOM method
Subroutine trigger_event: called CC=2 SCAR=7.0
Pod documented
524 1845 my ($n,$evnt) = (shift,shift);
525 1845 ▶ my $doc = $n->ownerDocument||$n;
Condition66%2/3
$n->ownerDocument || $n
l!l&&r!l&&!r
1833120
MC/DC0%0/2
$n->ownerDocument || $n
$n->ownerDocument $n
Truth table66%2/3
A || B
A= $n->ownerDocumentB= $n
ABresult
1X1
011
000
526 1845 $n->SUPER::trigger_event(
527 $evnt,
528 default => $doc->default_event_handler,
529 view => scalar $doc->defaultView,
530 @_,
531 );
532 }
533  
534 =item as_text
535  
536 =item as_HTML
537  
538 These two (non-DOM) methods of L<HTML::Element> are overridden, so that
539 they work correctly with comment and text nodes.
540  
541 =cut
542  
543 15 ▶sub as_text{
Subroutine as_text: called CC=1 SCAR=0.0
Pod documented
544 15 (my $clone = shift->clone)->deobjectify_text;
545 15 $clone->SUPER::as_text(@_);
546 }
547  
548 48 ▶sub as_HTML{
Subroutine as_HTML: called CC=1 SCAR=0.0
Pod documented
549 48 (my $clone = shift->clone)->deobjectify_text;
550 48 $clone->SUPER::as_HTML(@_);
551 }
552  
553 1561 ▶sub push_content {
Subroutine push_content: called CC=4 SCAR=13.9
Pod undocumented
554 1561 my $self = shift;
555 1561 ▶ @_ or return $self;
Branch: 1 branch
BranchTrueFalseTotal
@_ or return $self 1555 6 1561
556 1555 my $count = ()=$self->content_list;
557 1555 $self->SUPER::push_content(@_);
558 1555 my $ary = $self->{_content};
559 1555 ▶ ref and weaken $_->{_parent} for @$ary[$count-@$ary..-1];
Branch: 1 branch
BranchTrueFalseTotal
ref $_ and builtin::weaken($_->{"_parent"}) 1069 501 1570
560 1555 $self
561 }
562  
563 8 ▶sub unshift_content {
Subroutine unshift_content: called CC=3 SCAR=11.0
Pod undocumented
564 8 my $self = shift;
565 8 my $count = ()=$self->content_list;
566 8 $self->SUPER::unshift_content(@_);
567 8 my $ary = $self->{_content};
568 8 ▶ ref and weaken $_->{_parent} for @$ary[0..$#$ary-$count];
Branch: 1 branch
BranchTrueFalseTotal
ref $_ and builtin::weaken($_->{"_parent"}) 8 0 8
569 8 $self
570 }
571  
572 515 ▶sub splice_content {
Subroutine splice_content: called CC=3 SCAR=11.0
Pod undocumented
573 515 my($self,$start,$deleted) = (shift,@_);
574 515 my $orig_count = ()=$self->content_list;
575 515 $self->SUPER::splice_content(@_);
576 515 my $ary = $self->{_content};
577  
578 # orig_length - deleted_items + x = final_length,
579 # where x is the number of items added (to be weakened), so
580 # x = final_length - orig_length + deleted_items.
581 # x needs to be adjusted so it is an ending offset, so we use
582 # $#$ary instead of the final length (@$ary) and add $start
583 ref and weaken $_->{_parent}
584 515 ▶ for @$ary[$start..$#$ary-$orig_count+$deleted+$start];
Branch: 1 branch
BranchTrueFalseTotal
ref $_ and builtin::weaken($_->{"_parent"}) 516 0 516
585  
586 515 $self
587 }
588  
589 272 ▶sub clone {
Subroutine clone: called CC=3 SCAR=11.0
Pod undocumented
590 272 my $self = shift;
591 272 my $clone = $self->SUPER::clone;
592 272 for ($clone->content_list) {
593 202 ▶ ref or next;
Branch: 1 branch
BranchTrueFalseTotal
ref $_ or next 202 0 202
594 202 weaken $_->{_parent};
595 }
596 272 $clone;
597 }
598  
599 41 ▶sub replace_with {
Subroutine replace_with: called CC=3 SCAR=11.0
Pod undocumented
600 41 my $self = shift;
601 41 $self->SUPER::replace_with(@_);
602 41 for(@_) {
603 28 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
604 65 ▶ ref and weaken $_->{_parent};
Branch: 1 branch
BranchTrueFalseTotal
ref $_ and builtin::weaken($_->{"_parent"}) 65 0 65
605 }
606 41 $self;
607 }
608  
609  
610 =back
611  
612 =cut
613  
614 28 1;
615 __END__
« DOM/Element/Form.pm ↑ Summary DOM/Attr.pm »