blib/lib/HTML/TreeBuilder/LibXML/Node.pm

filter: Statementstmt 95.1% 236 / 248 Branchbran 74.1% 89 / 120 Conditioncond 58.3% 7 / 12 MC/DCmcdc 37.5% 3 / 8 Subroutinesub 94.1% 48 / 51 Podpod 0.0% 0 / 42 totaltotal 79.6% 383 / 481 CC 75 SCAR 15.8
CC
75
Cov
87%
look_down
12.4
unshift_content
9.0
replace_with
8.4
CRAP 4.9
« blib/lib/HTML/TreeBuilder/LibXML.pm ↑ Summary Build »
1 package HTML::TreeBuilder::LibXML::Node;
2 21 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 21 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 21 ▶use Carp();
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 sub new {
7 124 ▶ my ($class, $node) = @_;
Subroutine new: called CC=2 SCAR=7.1
Pod undocumented
8 124 ▶ Carp::croak 'missing node' unless $node;
Branch: 1 branch
BranchTrueFalseTotal
unless $node 0 124 124
9 124 bless {node => $node}, $class;
10 }
11  
12 sub attr {
13 27 ▶ my ($self, $key, $value) = @_;
Subroutine attr: called CC=5 SCAR=16.1
Pod undocumented
14 27 ▶ if (@_ == 3) {
Branch: 2 branches
BranchTrueFalseTotal
if (@_ == 3) { } 2 25 27
elsif (@_ == 2 and lc $key eq 'text') { } 9 16 25
15 2 ▶ if (defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) { } 1 1 2
16 1 $self->{node}->setAttribute (lc $key, $value);
17 } else {
18 1 $self->{node}->removeAttribute(lc $key);
19 }
20 ▶ } elsif (@_ == 2 and lc $key eq 'text') {
Condition66%2/3
@_ == 2 and lc $key eq "text"
!ll&&!rl&&r
0169
MC/DC50%1/2
@_ == 2 and lc $key eq "text"
@_ == 2 lc $key eq "text"
Truth table66%2/3
A and B
A= @_ == 2B= lc $key eq "text"
ABresult
0X0
100
111
21 9 return $self->{node}->textContent;
22 }
23 18 $self->{node}->getAttribute(lc $key);
24 }
25  
26 sub isTextNode {
27 1 ▶ my $self = shift;
Subroutine isTextNode: called CC=1 SCAR=0.0
Pod undocumented
28 1 $self->{node}->isa('XML::LibXML::Text');
29 }
30  
31 # The analog of HTML::TreeBuilder::XPath::getValue for comment nodes
32 21 *getValue = \&as_text;
33  
34 sub string_value {
35 1 ▶ $_[0]->{node}->textContent;
Subroutine string_value: called CC=1 SCAR=0.0
Pod undocumented
36 }
37  
38 sub as_text {
39 11 ▶ $_[0]->{node}->textContent;
Subroutine as_text: called CC=1 SCAR=0.0
Pod undocumented
40 }
41  
42 sub as_trimmed_text {
43 2 ▶ my $text = shift->as_text(@_);
Subroutine as_trimmed_text: called CC=1 SCAR=0.0
Pod undocumented
44 2 $text =~ s/[\n\r\f\t ]+$//s;
45 2 $text =~ s/^[\n\r\f\t ]+//s;
46 2 $text =~ s/[\n\r\f\t ]+/ /g;
47 2 return $text;
48 }
49 1 ▶sub as_text_trimmed { shift->as_trimmed_text(@_) } # alias
Subroutine as_text_trimmed: called CC=1 SCAR=0.0
Pod undocumented
50  
51 ▶sub objectify_text { }
Subroutine objectify_text: called CC=1 SCAR=0.0
Pod undocumented
52 ▶sub deobjectify_text { }
Subroutine deobjectify_text: not called CC=1 SCAR=0.0
Pod undocumented
53  
54 sub as_XML {
55 2 ▶ $_[0]->{node}->toString;
Subroutine as_XML: called CC=1 SCAR=0.0
Pod undocumented
56 }
57  
58 sub as_HTML {
59 41 ▶ return $_[0]->{node}->toStringHTML if $_[0]->{node}->can('toStringHTML'); # best method, but only document nodes can toStringHTML()
Subroutine as_HTML: called CC=3 SCAR=11.0
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
if $_[0]{"node"}->can("toStringHTML") 5 36 41
60
61 # second best is to call toStringC14N(1), which generates valid HTML (eg. no auto closed <div/>),
62 # but dies on some cases with "Failed to convert doc to string in doc->toStringC14N" error.
63 # so we fallback to toString()
64 {
65 36 local $@; # protect existing $@
66 36 my $output = eval { $_[0]->{node}->toStringC14N(1) };
67 36 ▶ return $@ ? $_[0]->{node}->toString : $output;
Branch: 1 branch
BranchTrueFalseTotal
$@ ? : 0 36 36
68 }
69 }
70  
71 sub tag {
72 $_[0]->{node}->localname
73 3 ▶}
Subroutine tag: called CC=1 SCAR=0.0
Pod undocumented
74  
75 sub id {
76 4 ▶ if (@_==2) {
Subroutine id: called CC=3 SCAR=11.0
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 2) { } 2 2 4
77 # setter
78 2 ▶ if (defined $_[1]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[1]) { } 1 1 2
79 1 $_[0]->{node}->setAttribute('id', $_[1]);
80 } else {
81 1 $_[0]->{node}->removeAttribute('id');
82 }
83 } else {
84 2 $_[0]->{node}->getAttribute('id');
85 }
86 }
87  
88 # hack for Web::Scraper
89 sub isa {
90 3 ▶ my ($self, $klass) = @_;
Subroutine isa: called CC=2 SCAR=7.2
Pod undocumented
91 3 ▶ $klass eq 'HTML::Element' ? 1 : UNIVERSAL::isa($self, $klass);
Branch: 1 branch
BranchTrueFalseTotal
$klass eq 'HTML::Element' ? : 0 3 3
92 }
93  
94 sub exists {
95 2 ▶ my( $self , $xpath ) = @_;
Subroutine exists: called CC=3 SCAR=11.0
Pod undocumented
96  
97 2 ▶ $self->_eof_or_die unless $self->{node};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"node"} 0 2 2
98 2 my @nodes = $self->{node}->findnodes( $xpath );
99 2 ▶ return scalar( @nodes ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
scalar @nodes ? : 1 1 2
100 }
101  
102 sub find {
103 2 ▶ my( $self , $elem ) = @_;
Subroutine find: called CC=3 SCAR=11.2
Pod undocumented
104  
105 2 ▶ $self->_eof_or_die unless $self->{node};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"node"} 0 2 2
106  
107 2 my @nodes = $self->{node}->getElementsByTagName( $elem );
108 2 @nodes = map { HTML::TreeBuilder::LibXML::Node->new( $_ ) } @nodes;
109  
110 2 ▶ wantarray ? @nodes : \@nodes;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 2 0 2
111 }
112  
113 sub findnodes {
114 23 ▶ my ($self, $xpath) = @_;
Subroutine findnodes: called CC=3 SCAR=11.0
Pod undocumented
115  
116 23 ▶ $self->_eof_or_die unless $self->{node};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"node"} 1 22 23
117 23 my @nodes = $self->{node}->findnodes( $xpath );
118 23 @nodes = map { HTML::TreeBuilder::LibXML::Node->new($_) } @nodes;
119 23 ▶ wantarray ? @nodes : \@nodes;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 16 7 23
120 }
121  
122 21 *findnodes_as_string = \&findvalue;
123 21 *findnodes_as_strings = \&findvalues;
124  
125 sub findnodes_filter {
126 1 ▶ my( $self , $xpath , $callback ) = @_;
Subroutine findnodes_filter: called CC=4 SCAR=15.0
Pod undocumented
127  
128 1 ▶ Carp::croak "Second argument must be coderef"
Branch: 1 branch
BranchTrueFalseTotal
unless $callback and ref $callback eq "CODE" 0 1 1
Condition33%1/3
$callback and ref $callback eq "CODE"
!ll&&!rl&&r
001
MC/DC0%0/2
$callback and ref $callback eq "CODE"
$callback ref $callback eq "CODE"
Truth table33%1/3
A and B
A= $callbackB= ref $callback eq "CODE"
ABresult
0X0
100
111
129 unless $callback and ref $callback eq 'CODE';
130  
131 1 my @nodes = $self->findnodes( $xpath );
132 1 @nodes = grep { $callback->($_) } @nodes;
133  
134 1 ▶ wantarray ? @nodes : \@nodes;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 0 1
135 }
136  
137 sub findvalue {
138 21 ▶ my ($self, $xpath) = @_;
Subroutine findvalue: called CC=2 SCAR=7.1
Pod undocumented
139  
140 21 ▶ $self->_eof_or_die unless $self->{node};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"node"} 0 21 21
141 21 $self->{node}->findvalue( $xpath );
142 }
143  
144 sub findvalues {
145 2 ▶ my( $self , $xpath ) = @_;
Subroutine findvalues: called CC=3 SCAR=11.2
Pod undocumented
146  
147 2 ▶ $self->_eof_or_die unless $self->{node};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"node"} 0 2 2
148 2 my $nodes = $self->{node}->find( $xpath );
149 2 my @nodes = map { $_->textContent } $nodes->get_nodelist;
150 2 ▶ wantarray ? @nodes : \@nodes;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 2 0 2
151 }
152  
153 sub clone {
154 3 ▶ my ($self, ) = @_;
Subroutine clone: called CC=3 SCAR=11.4
Pod undocumented
155  
156 3 my $orignode = $self->{node};
157 3 my $origdoc = $orignode->ownerDocument;
158  
159 3 my $node = $orignode->cloneNode(1);
160  
161 # arguments can be undefined
162 3 my $doc = do {
163 21 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
164 3 XML::LibXML::Document->new($origdoc->version, $origdoc->encoding);
165 };
166
167 3 ▶ if ($node->isa('XML::LibXML::Dtd')) {
Branch: 2 branches
BranchTrueFalseTotal
if ($node->isa('XML::LibXML::Dtd')) { } 0 3 3
elsif ($node->isa('XML::LibXML::Element')) { } 3 0 3
168 0 $doc->createInternalSubset( $node->getName, $node->publicId, $node->systemId );
169 0 $node = $doc->internalSubset;
170 } elsif ($node->isa('XML::LibXML::Element')) {
171 3 $doc->setDocumentElement($node);
172 } else {
173 0 $doc->adoptNode($node);
174 }
175
176 3 my $cloned = __PACKAGE__->new($node);
177 3 return $cloned;
178 }
179  
180 sub clone_list {
181 1 ▶ my $class = shift;
Subroutine clone_list: called CC=1 SCAR=0.0
Pod undocumented
182 1 my @clones = map { $_->clone } @_;
183 1 @clones;
184 }
185  
186 sub detach {
187 3 ▶ my $self = shift;
Subroutine detach: called CC=1 SCAR=0.0
Pod undocumented
188 3 my $parent = $self->parent;
189 #$self->{node}->unbindNode();
190 3 my $doc = XML::LibXML->createDocument;
191 3 $doc->adoptNode($self->{node});
192 3 $doc->setDocumentElement($self->{node});
193 3 $parent;
194 }
195  
196 sub delete {
197 6 ▶ my $self = shift;
Subroutine delete: called CC=1 SCAR=0.0
Pod undocumented
198 6 $self->{node}->unbindNode();
199 }
200  
201 sub delete_content {
202 1 ▶ my ($self) = @_;
Subroutine delete_content: called CC=1 SCAR=0.0
Pod undocumented
203 1 $self->{node}->removeChildNodes;
204 }
205  
206 sub getFirstChild {
207 0 ▶ my $self = shift;
Subroutine getFirstChild: not called CC=1 SCAR=6.9
Pod undocumented
208 0 __PACKAGE__->new($self->{node}->getFirstChild);
209 }
210  
211 sub childNodes {
212 3 ▶ my $self = shift;
Subroutine childNodes: called CC=3 SCAR=11.2
Pod undocumented
213  
214 3 ▶ $self->_eof_or_die unless $self->{node};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"node"} 0 3 3
215 3 my @nodes = $self->{node}->childNodes;
216 3 @nodes = map { __PACKAGE__->new($_) } @nodes;
217 3 ▶ wantarray ? @nodes : \@nodes;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 3 0 3
218 }
219  
220 sub content_list {
221 2 ▶ my ($self) = @_;
Subroutine content_list: called CC=1 SCAR=0.0
Pod undocumented
222 2 my @nodes = $self->childNodes;
223 2 @nodes;
224 }
225  
226 sub replace_with {
227 2 ▶ my $self = shift;
Subroutine replace_with: called CC=8 SCAR=21.2
Pod undocumented
228
229 # TODO handle @_ == 0
230
231 2 my $node = $self->{node};
232 2 my $doc = $node->ownerDocument;
233 2 my $parent = $node->parentNode;
234 2 ▶ die "can't replace_with(), node has no parent!" unless $parent;
Branch: 1 branch
BranchTrueFalseTotal
unless $parent 0 2 2
235
236 2 ▶ my @nodes = map { ref $_ ? $_->{node} : $doc->createTextNode($_) } @_;
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 2 4 6
237
238 2 ▶ if ($parent->isa('XML::LibXML::Document')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parent->isa('XML::LibXML::Document')) { } 1 1 2
239 # can't call insertBefore() in a document node,
240 # so this is the best hack so far :[
241 # works only if $node is the last child
242 1 ▶ die "[not supported] calling replace_with() in a node that is child of a document node, and its not the last child."
Branch: 1 branch
BranchTrueFalseTotal
unless $node->isSameNode($parent->lastChild) 0 1 1
243 unless $node->isSameNode($parent->lastChild);
244
245 1 foreach (@nodes) {
246
247 3 ▶ if ($_->isa('XML::LibXML::Dtd')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_->isa("XML::LibXML::Dtd")) 0 3 3
248 0 $parent->createInternalSubset($_->getName, $_->publicId, $_->systemId);
249 0 next;
250 }
251 3 $parent->adoptNode($_);
252 3 $node->addSibling($_);
253 }
254
255 }
256 else {
257 $parent->insertBefore($_, $node)
258 1 for @nodes;
259 }
260
261 2 $self->detach;
262 2 $self;
263 }
264  
265 sub push_content {
266 2 ▶ my $self = shift;
Subroutine push_content: called CC=7 SCAR=19.5
Pod undocumented
267
268 2 my $node = $self->{node};
269 2 ▶ my $doc = $node->isa('XML::LibXML::Document') ? $node : $node->ownerDocument;
Branch: 1 branch
BranchTrueFalseTotal
$node->isa('XML::LibXML::Document') ? : 1 1 2
270 2 ▶ my @nodes = map { ref $_ ? $_->{node} : $doc->createTextNode($_) } @_;
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 2 2 4
271
272 # thats because appendChild() is not supported on a Document node (as of XML::LibXML 2.0017)
273 2 ▶ if ($node->isa('XML::LibXML::Document')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->isa('XML::LibXML::Document')) { } 1 1 2
274
275 1 foreach (@nodes) {
276 #$node->adoptNode($_);
277 1 ▶ $node->hasChildNodes ? $node->lastChild->addSibling($_)
Branch: 1 branch
BranchTrueFalseTotal
$node->hasChildNodes ? : 1 0 1
278 : $node->setDocumentElement($_);
279 }
280 }
281 else {
282 1 $node->appendChild($_) for @nodes;
283 }
284
285 2 $self;
286 }
287  
288 sub unshift_content {
289 2 ▶ my $self = shift;
Subroutine unshift_content: called CC=9 SCAR=22.0
Pod undocumented
290
291 return $self->push_content(@_)
292 2 ▶ unless $self->{node}->hasChildNodes;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"node"}->hasChildNodes 0 2 2
293  
294 2 my $node = $self->{node};
295 2 ▶ my $doc = $node->isa('XML::LibXML::Document') ? $node : $node->ownerDocument;
Branch: 1 branch
BranchTrueFalseTotal
$node->isa('XML::LibXML::Document') ? : 1 1 2
296 2 ▶ my @nodes = map { ref $_ ? $_->{node} : $doc->createTextNode($_) } @_;
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 2 2 4
297
298 # thats because insertBefore() is not supported on a Document node (as of XML::LibXML 2.0017)
299 2 ▶ if ($node->isa('XML::LibXML::Document')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->isa('XML::LibXML::Document')) { } 1 1 2
300
301 1 foreach (@nodes) {
302 1 ▶ $node->hasChildNodes ? $node->lastChild->addSibling($_)
Branch: 1 branch
BranchTrueFalseTotal
$node->hasChildNodes ? : 1 0 1
303 : $node->setDocumentElement($_);
304 }
305
306 # rotate
307 1 ▶ while (not $node->firstChild->isSameNode($nodes[0])) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($node->firstChild->isSameNode($nodes[0])) 1 1 2
308 1 my $first_node = $node->firstChild;
309 1 $first_node->unbindNode;
310 1 $node->lastChild->addSibling($first_node);
311
312 }
313 }
314 else {
315 1 my $first_child = $node->firstChild;
316 1 $node->insertBefore($_, $first_child) for @nodes;
317 }
318
319 2 $self;
320 }
321  
322 sub left {
323 1 ▶ my $self = shift;
Subroutine left: called CC=3 SCAR=11.4
Pod undocumented
324  
325 1 ▶ $self->_eof_or_die unless $self->{node};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"node"} 0 1 1
326 1 my $prev = $self->{node}->previousNonBlankSibling;
327 1 ▶ return $prev ? __PACKAGE__->new( $prev ) : undef;
Branch: 1 branch
BranchTrueFalseTotal
$prev ? : 1 0 1
328 }
329  
330 sub right {
331 1 ▶ my $self = shift;
Subroutine right: called CC=3 SCAR=11.4
Pod undocumented
332  
333 1 ▶ $self->_eof_or_die unless $self->{node};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"node"} 0 1 1
334 1 my $next = $self->{node}->nextNonBlankSibling;
335 1 ▶ return $next ? __PACKAGE__->new( $next ) : undef;
Branch: 1 branch
BranchTrueFalseTotal
$next ? : 1 0 1
336 }
337  
338 sub look_down {
339 7 ▶ my $self = shift;
Subroutine look_down: called CC=12 SCAR=25.2
Pod undocumented
340 7 my @args = @_;
341  
342 7 ▶ $self->_eof_or_die unless $self->{node};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"node"} 0 7 7
343  
344 7 my @filter;
345 7 my $xpath = "//*"; # default
346 7 ▶ while (@args) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args) 10 7 17
347 10 ▶ if (ref $args[0] eq 'CODE') {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $args[0] eq 'CODE') { } 3 7 10
elsif (@args >= 2 and $args[0] eq '_tag') { } 6 1 7
elsif (@args >= 2) { } 1 0 1
348 3 my $code = shift @args;
349 3 push @filter, $code;
350 ▶ } elsif (@args >= 2 && $args[0] eq '_tag') {
Condition66%2/3
@args >= 2 and $args[0] eq "_tag"
!ll&&!rl&&r
016
MC/DC50%1/2
@args >= 2 and $args[0] eq "_tag"
@args >= 2 $args[0] eq "_tag"
Truth table66%2/3
A and B
A= @args >= 2B= $args[0] eq "_tag"
ABresult
0X0
100
111
351 6 my($tag, $want_tag) = splice(@args, 0, 2);
352 6 $xpath = "//$want_tag";
353 } elsif (@args >= 2) {
354 1 my($attr, $stuff) = splice(@args, 0, 2);
355 1 ▶ if (ref $stuff eq 'Regexp') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $stuff eq 'Regexp') { } 1 0 1
356 21 ▶ push @filter, sub { no warnings 'uninitialized'; $_[0]->attr($attr) =~ $stuff };
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
357 } else {
358 21 ▶ push @filter, sub { no warnings 'uninitialized'; $_[0]->attr($attr) eq $stuff };
Subroutine BEGIN: called CC=1 SCAR=0.6
Subroutine __ANON__: not called CC=1 SCAR=0.6
359 }
360 } else {
361 0 Carp::carp("Don't know what to do with @args");
362 0 shift @args;
363 }
364 }
365  
366 7 $xpath =~ s/~text\b/text()/g;
367  
368 7 my @nodes = $self->findnodes($xpath);
369 my @wants = grep {
370 7 my $node = $_;
371 26 my $ok = 1;
372 26 for my $filter (@filter) {
373 20 ▶ $filter->($_) or $ok = 0;
Branch: 1 branch
BranchTrueFalseTotal
&$filter($_) or $ok = 0 17 3 20
374 }
375 26 ▶ $ok ? $node : ();
Branch: 1 branch
BranchTrueFalseTotal
$ok ? : 9 17 26
376 } @nodes;
377  
378 7 ▶ wantarray ? @wants : $wants[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 6 1 7
379 }
380  
381 sub all_attr {
382 1 ▶ my $self = shift;
Subroutine all_attr: called CC=1 SCAR=0.0
Pod undocumented
383 1 return map { $_->name => $_->value } $self->{node}->attributes;
384 }
385  
386 sub all_attr_names {
387 1 ▶ my $self = shift;
Subroutine all_attr_names: called CC=1 SCAR=0.0
Pod undocumented
388 1 return map $_->name, $self->{node}->attributes;
389 }
390  
391 1 ▶sub all_external_attr { shift->all_attr(@_) }
Subroutine all_external_attr: called CC=1 SCAR=0.0
Pod undocumented
392 1 ▶sub all_external_attr_names { shift->all_attr_names(@_) }
Subroutine all_external_attr_names: called CC=1 SCAR=0.0
Pod undocumented
393  
394 sub _eof_or_die {
395 1 ▶ my $self = shift;
Subroutine _eof_or_die: called CC=2 SCAR=7.6
396 1 ▶ if (defined($self->{_content})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'_content'}) { } 1 0 1
397 1 $self->eof;
398 } else {
399 0 Carp::croak "\$self is not loaded: $self"
400 }
401 }
402  
403  
404 sub matches {
405 2 ▶ my ($self, $xpath) = @_;
Subroutine matches: called CC=3 SCAR=11.1
Pod undocumented
406
407 2 foreach ($self->{node}->ownerDocument->findnodes($xpath)) {
408 1 ▶ return 1 if $_->isEqual($self->{node});
Branch: 1 branch
BranchTrueFalseTotal
if $_->isEqual($self->{"node"}) 1 0 1
409 }
410
411 1 return;
412 }
413  
414 sub parent {
415 22 ▶ my $self = shift;
Subroutine parent: called CC=8 SCAR=20.8
Pod undocumented
416
417 22 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 4 18 22
418
419 # unset
420 4 ▶ unless (defined $_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $_[0]) 1 3 4
421 1 $self->{node}->unbindNode;
422 1 return;
423 }
424
425 # set
426 Carp::croak "an element can't be made its own parent"
427 3 ▶ if ref $_[0]->{node}->isSameNode($self->{node}); # sanity
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0]{"node"}->isSameNode($self->{"node"}) 0 3 3
428
429 3 my $parent = $_[0]->{node};
430
431 3 ▶ if ($_[0]->{node}->isa('XML::LibXML::Document')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0]{'node'}->isa('XML::LibXML::Document')) { } 2 1 3
432  
433 2 ▶ if ($parent->hasChildNodes) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parent->hasChildNodes) { } 1 1 2
434 1 $parent->lastChild->addSibling($self->{node});
435 }
436 else {
437 1 $parent->adoptNode($self->{node});
438 1 $parent->setDocumentElement($self->{node});
439 }
440
441 }
442 else {
443 1 $parent->appendChild($self->{node});
444 }
445
446 }
447 else {
448 # get
449 18 my $parent = $self->{node}->parentNode;
450 18 ▶ return defined $parent && !$parent->isa('XML::LibXML::DocumentFragment')? ref($self)->new($parent) : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $parent && !$parent->isa('XML::LibXML::DocumentFragment') ? : 17 1 18
Condition66%2/3
defined $parent and not $parent->isa("XML::LibXML::DocumentFragment")
!ll&&!rl&&r
0117
MC/DC50%1/2
defined $parent and not $parent->isa("XML::LibXML::DocumentFragment")
defined $parent not $parent->isa("XML::LibXML::DocumentFragment")
Truth table66%2/3
A and not(B)
A= defined $parentB= not $parent->isa("XML::LibXML::DocumentFragment")
ABresult
0X0
100
111
451 }
452  
453 }
454  
455 sub postinsert {
456 1 ▶ my $self = shift;
Subroutine postinsert: called CC=2 SCAR=6.9
Pod undocumented
457 1 my @nodes = map { $_->{node} } @_;
458 1 my $parent = $self->{node}->parentNode;
459
460 $parent->insertAfter($_, $self->{node})
461 1 foreach reverse @nodes;
462
463 1 $self;
464 }
465  
466 sub preinsert {
467 1 ▶ my $self = shift;
Subroutine preinsert: called CC=2 SCAR=6.9
Pod undocumented
468 1 my @nodes = map { $_->{node} } @_;
469 1 my $parent = $self->{node}->parentNode;
470
471 $parent->insertBefore($_, $self->{node})
472 1 foreach @nodes;
473
474 1 $self;
475 }
476  
477  
478  
479 21 1;
480  
481 __END__
« blib/lib/HTML/TreeBuilder/LibXML.pm ↑ Summary Build »