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

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