blib/lib/XML/LibXML/jQuery.pm

filter: Statementstmt 98.5% 487 / 494 Branchbran 85.7% 211 / 246 Conditioncond 65.0% 39 / 60 MC/DCmcdc 37.8% 14 / 37 Subroutinesub 98.4% 62 / 63 Podpod 97.5% 39 / 40 totaltotal 90.6% 852 / 940 SCAR 52.3
CC
173
Cov
92%
data
19.2
new
15.0
attr
14.0
CRAP 187.6
↑ Summary Build »
1 package XML::LibXML::jQuery;
2  
3 32 ▶use 5.008001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 32 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 32 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 32 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 32 ▶use Scalar::Util qw/ blessed /;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 32 ▶use XML::LibXML;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 32 ▶use HTML::Selector::XPath qw/selector_to_xpath/;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 32 ▶use Carp qw/ confess /;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 32 ▶use JSON qw/ decode_json /;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 32 our $VERSION = "0.06";
14  
15 32 our @EXPORT_OK = qw/ j fn /;
16 32 our @EXPORT = qw/ j /;
17  
18 use constant {
19 32 XML_ELEMENT_NODE => 1,
20 XML_TEXT_NODE => 3,
21 XML_COMMENT_NODE => 8,
22 XML_DOCUMENT_NODE => 9,
23 XML_DOCUMENT_FRAG_NODE => 11,
24 XML_HTML_DOCUMENT_NODE => 13
25 32 ▶};
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 32 our ($PARSER);
28  
29 # plugin functions
30 32 my %fn;
31  
32 # for data()
33 32 my $data = {};
34  
35 sub fn($$) {
36 2 ▶ my ($name, $sub) = @_;
Subroutine fn: called CC=2 SCAR=6.9
Pod undocumented
37  
38 1 die sprintf("fn '$name' already defined by %s (at %s line %s)", @{$fn{$name}->{caller}})
39 2 ▶ if exists $fn{$name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $fn{$name} 1 1 2
40  
41 1 $fn{$name} = {
42 sub => $sub,
43 caller => [caller]
44 };
45 }
46  
47  
48  
49 #*j = \&jQuery;
50 sub j {
51 128 ▶ __PACKAGE__->new(@_);
Subroutine j: called CC=1 SCAR=0.0
Pod documented
52 }
53  
54  
55 sub new {
56 363 ▶ my ($class, $stuff, $before) = @_;
Subroutine new: called CC=14 SCAR=27.1
Pod documented
57 363 my ($self, $document, $nodes);
58  
59 # instance method, reuse document
60 363 ▶ if (ref $class) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $class) 216 147 363
61 216 $self = $class;
62 216 $class = ref $self;
63 216 $document = $self->{document};
64 }
65  
66 363 ▶ if (defined $stuff) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $stuff) 363 0 363
67  
68 363 ▶ if (blessed $stuff) {
Branch: 2 branches
BranchTrueFalseTotal
if (builtin::blessed($stuff)) { } 40 323 363
elsif (my $reftype = ref $stuff) { } 162 161 323
69  
70 40 ▶ if ($stuff->isa(__PACKAGE__)) {
Branch: 2 branches
BranchTrueFalseTotal
if ($stuff->isa(__PACKAGE__)) { } 22 18 40
elsif ($stuff->isa('XML::LibXML::Element')) { } 18 0 18
71 22 $nodes = $stuff->{nodes};
72 }
73 # TODO this is too restrictive.. what about text, comment, other nodes?
74 elsif ($stuff->isa('XML::LibXML::Element')) {
75 18 $nodes = [$stuff];
76 }
77 else {
78 0 confess "can't handle this type of object: ".ref $stuff;
79 }
80 }
81 elsif (my $reftype = ref $stuff) {
82 162 ▶ confess "Only ArrayRef accepted. (not '$reftype')"
Branch: 1 branch
BranchTrueFalseTotal
unless $reftype eq "ARRAY" 0 162 162
83 unless $reftype eq 'ARRAY';
84  
85 162 $nodes = $stuff;
86 }
87 else {
88 # parse as string
89 161 ▶ if ($stuff =~ /^\s*<\?xml/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stuff =~ /^\s*<\?xml/) { } 0 161 161
90 0 die "xml parsing disabled!";
91 0 $nodes = [ $PARSER->parse_string($stuff) ];
92 } else {
93 161 $nodes = _parse_html($stuff);
94 }
95 }
96  
97 # catch bugs :)
98 363 ▶ confess "undefined node" if grep { !defined } @$nodes;
Branch: 1 branch
BranchTrueFalseTotal
if grep {not defined $_;} @$nodes 0 363 363
99  
100 # adopt nodes to existing document
101 363 ▶ if ($document) {
Branch: 1 branch
BranchTrueFalseTotal
if ($document) 216 147 363
102  
103 # my $doc_id = $existing_document->unique_key;
104 $document->adoptNode($_)
105 216 for grep { not $document->isSameNode($_->ownerDocument) }
106 287 grep { $_->nodeType != XML_DOCUMENT_NODE }
107 @$nodes;
108 }
109 }
110  
111 # resolve document
112 363 ▶ unless ($document) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($document) 147 216 363
113  
114 147 ▶ $document = defined $nodes->[0] ? $nodes->[0]->ownerDocument
Branch: 1 branch
BranchTrueFalseTotal
defined $nodes->[0] ? : 147 0 147
115 : XML::LibXML->createDocument;
116 }
117  
118 # increment document data refcount
119 363 my $doc_id = $document->unique_key;
120 363 $data->{$doc_id}{refcount}++;
121 # printf STDERR "[%s] incremented document %d data ref count: %d\n", __PACKAGE__, $doc_id, $data->{$doc_id}{refcount};
122  
123 363 bless {
124 document => $document,
125 nodes => $nodes,
126 before => $before
127 }, $class;
128 }
129  
130 sub _parse_html {
131 161 ▶ my $source = $_[0];
Subroutine _parse_html: called CC=11 SCAR=24.1
132  
133 161 ▶ if (!$PARSER){
Branch: 1 branch
BranchTrueFalseTotal
unless ($XML::LibXML::jQuery::PARSER) 31 130 161
134 31 $PARSER = XML::LibXML->new();
135 31 $PARSER->recover(1);
136 31 $PARSER->recover_silently(1);
137 31 $PARSER->keep_blanks(0);
138 31 $PARSER->expand_entities(1);
139 31 $PARSER->no_network(1);
140 # local $XML::LibXML::skipXMLDeclaration = 0;
141 # local $XML::LibXML::skipDTD = 0;
142 }
143  
144 161 my $dom = $PARSER->parse_html_string($source);
145 161 my @nodes;
146  
147  
148 # full html
149 161 ▶ if ($source =~ /<html/i) {
Branch: 2 branches
BranchTrueFalseTotal
if ($source =~ /<html/i) { } 7 154 161
elsif ($source =~ /<(?!!).*?>/) { } 151 3 154
150 7 @nodes = $dom->getDocumentElement;
151 }
152 # html fragment
153 elsif ($source =~ /<(?!!).*?>/) { # < not followed by ! then stuff until > (match a html tag)
154 151 @nodes = map { $_->childNodes } $dom->findnodes('/html/head | /html/body');
155 }
156 # plain text
157 else {
158 3 $dom->removeInternalSubset;
159 3 ▶ @nodes = $dom->exists('//p') ? $dom->findnodes('/html/body/p')->pop->childNodes : $dom->childNodes;
Branch: 1 branch
BranchTrueFalseTotal
$dom->exists('//p') ? : 0 3 3
160 }
161  
162 161 ▶ confess "empy nodes :[" unless @nodes;
Branch: 1 branch
BranchTrueFalseTotal
unless @nodes 0 161 161
163 161 ▶ confess "undefined node :[" if grep { ! defined } @nodes;
Branch: 1 branch
BranchTrueFalseTotal
if grep {not defined $_;} @nodes 0 161 161
164 # new doc (setDocumentElement accepts only element nodes)
165 161 ▶ if ($nodes[0]->nodeType == XML_ELEMENT_NODE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nodes[0]->nodeType == 1) 160 1 161
166 160 my $doc = XML::LibXML->createDocument;
167 160 ▶ if ($source =~ /^\s*<!DOCTYPE/ && (my $dtd = $nodes[0]->ownerDocument->internalSubset)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($source =~ /^\s*<!DOCTYPE/ and my $dtd = $nodes[0]->ownerDocument->internalSubset) 2 158 160
Condition66%2/3
$source =~ /^\s*<!DOCTYPE/ and my $dtd = $nodes[0]->ownerDocument->internalSubset
!ll&&!rl&&r
15802
MC/DC50%1/2
$source =~ /^\s*<!DOCTYPE/ and my $dtd = $nodes[0]->ownerDocument->internalSubset
$source =~ /^\s*<!DOCTYPE/ my $dtd = $nodes[0]->ownerDocument->internalSubset
Truth table66%2/3
A and B
A= $source =~ /^\s*<!DOCTYPE/B= my $dtd = $nodes[0]->ownerDocument->internalSubset
ABresult
0X0
100
111
168 2 $doc->createInternalSubset( $dtd->getName, $dtd->publicId, $dtd->systemId );
169 }
170 160 $doc->setDocumentElement($nodes[0]);
171 160 $nodes[0]->addSibling($_) foreach @nodes[1..$#nodes];
172 }
173  
174 161 \@nodes;
175 }
176  
177  
178 sub get {
179 14 ▶ my ($self, $i) = @_;
Subroutine get: called CC=1 SCAR=0.0
Pod documented
180 14 $self->{nodes}->[$i];
181 }
182  
183 sub eq {
184 0 ▶ my ($self, $i) = @_;
Subroutine eq: not called CC=2 SCAR=17.9
Pod documented
185 0 ▶ $self->new([ $self->{nodes}[$i] || () ], $self);
Condition0%0/3
$self->{'nodes'}[$i] || ()
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{'nodes'}[$i] || ()
$self->{'nodes'}[$i] ()
Truth table0%0/3
A || B
A= $self->{'nodes'}[$i]B= ()
ABresult
1X1
011
000
186 }
187  
188  
189 sub end {
190 17 ▶ shift->{before};
Subroutine end: called CC=1 SCAR=0.0
Pod documented
191 }
192  
193 sub document {
194 39 ▶ my $self = shift;
Subroutine document: called CC=1 SCAR=0.0
Pod documented
195 39 $self->new([ $self->{document} ], $self);
196 }
197  
198 sub tagname {
199 6 ▶ my $self = shift;
Subroutine tagname: called CC=2 SCAR=7.0
Pod documented
200 6 ▶ return unless @{$self->{nodes}};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$self->{"nodes"};} 0 6 6
201 6 $self->{nodes}[0]->localname;
202 }
203  
204 sub first {
205 3 ▶ my $self = shift;
Subroutine first: called CC=2 SCAR=8.1
Pod documented
206 3 ▶ $self->new([ $self->{nodes}[0] || () ], $self);
Condition33%1/3
$self->{'nodes'}[0] || ()
l!l&&r!l&&!r
300
MC/DC0%0/2
$self->{'nodes'}[0] || ()
$self->{'nodes'}[0] ()
Truth table33%1/3
A || B
A= $self->{'nodes'}[0]B= ()
ABresult
1X1
011
000
207 }
208  
209 sub last {
210 1 ▶ my $self = shift;
Subroutine last: called CC=2 SCAR=8.1
Pod documented
211 1 ▶ $self->new([ $self->{nodes}[-1] || () ], $self);
Condition33%1/3
$self->{'nodes'}[-1] || ()
l!l&&r!l&&!r
100
MC/DC0%0/2
$self->{'nodes'}[-1] || ()
$self->{'nodes'}[-1] ()
Truth table33%1/3
A || B
A= $self->{'nodes'}[-1]B= ()
ABresult
1X1
011
000
212 }
213  
214 sub serialize {
215 3 ▶ my ($self) = @_;
Subroutine serialize: called CC=2 SCAR=6.9
Pod documented
216 3 my $output = '';
217  
218 $output .= $_->serialize
219 3 for (@{$self->{nodes}});
220  
221 3 $output;
222 }
223  
224  
225 sub as_html {
226 86 ▶ my ($self) = @_;
Subroutine as_html: called CC=4 SCAR=13.9
Pod documented
227  
228 86 my $output = '';
229  
230 86 foreach (@{$self->{nodes}}) {
231  
232 # TODO benchmark as_html() using can() vs nodeType to detect document nodes
233 # best method, but only document nodes can toStringHTML()
234 107 ▶ if ($_->can('toStringHTML')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_->can("toStringHTML")) 17 90 107
235 # printf STDERR "%s: toStringHTML\n", ref $_;
236 17 $output .= $_->toStringHTML;
237 17 next;
238 }
239  
240  
241 # second best is to call toStringC14N(1), which generates valid HTML (eg. no auto closed <div/>),
242 # but dies on some cases with "Failed to convert doc to string in doc->toStringC14N" error.
243 # so we fallback to toString()
244 # the most common case where toStringC14N fails is unbound nodes (getOwner returns a DocumentFragment)
245 {
246 90 local $@; # protect existing $@
247 90 my $html = eval { $_->toStringC14N(1) };
248 # printf STDERR "%s: %s\n", ref $_->getOwner, ($@ ? "toString: $@" : 'toStringC14N');
249 90 ▶ $output .= $@ ? $_->toString : $html;
Branch: 1 branch
BranchTrueFalseTotal
$@ ? : 0 90 90
250 }
251 }
252  
253 86 $output;
254 }
255  
256 sub html {
257 3 ▶ my ($self, $stuff) = @_;
Subroutine html: called CC=6 SCAR=17.9
Pod documented
258  
259 # output
260 3 ▶ unless ($stuff) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($stuff) 2 1 3
261 2 my $out = '';
262 2 foreach my $node (map { $_->childNodes } @{$self->{nodes}}) {
263 {
264 3 local $@;
265 3 my $html = eval { $node->toStringC14N(1) };
266 3 ▶ $out .= $@ ? $node->toString : $html;
Branch: 1 branch
BranchTrueFalseTotal
$@ ? : 0 3 3
267 }
268 }
269 2 return $out;
270 }
271  
272 # replace content
273 1 my $nodes = $self->new($stuff)->{nodes};
274  
275 1 foreach my $node (@{$self->{nodes}}) {
276 2 $node->removeChildNodes;
277 2 $node->appendChild($_->cloneNode(1)) for @$nodes;
278 }
279  
280 1 $self;
281 }
282  
283 sub text {
284 2 ▶ my ($self, $stuff) = @_;
Subroutine text: called CC=5 SCAR=16.1
Pod documented
285  
286 # output
287 2 ▶ unless (defined $stuff) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $stuff) 1 1 2
288 1 my $out = '';
289 1 $out .= $_->textContent for @{$self->{nodes}};
290 1 return $out;
291 }
292  
293 # replace content
294 1 ▶ return $self unless @{$self->{nodes}};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$self->{"nodes"};} 0 1 1
295  
296 1 my $textnode = $self->{nodes}[0]->ownerDocument->createTextNode($stuff);
297  
298 1 foreach my $node (@{$self->{nodes}}) {
299 1 $node->removeChildNodes;
300 1 $node->appendChild($textnode->cloneNode(1));
301 }
302  
303 1 $self;
304 }
305  
306  
307 sub size {
308 30 ▶ my ($self) = @_;
Subroutine size: called CC=1 SCAR=0.0
Pod documented
309 30 scalar @{$self->{nodes}};
310 }
311  
312 sub children {
313 5 ▶ my ($self, $selector) = @_;
Subroutine children: called CC=4 SCAR=13.9
Pod documented
314  
315 5 ▶ my $xpath = selector_to_xpath($selector, root => '.')
Branch: 1 branch
BranchTrueFalseTotal
if $selector 3 2 5
316 if $selector;
317  
318 9 ▶ my @new = map { $_->nodeType == XML_ELEMENT_NODE ? $_ : () }
Branch: 1 branch
BranchTrueFalseTotal
$_->nodeType == 1 ? : 5 4 9
319 5 ▶ map { $xpath ? $_->findnodes($xpath) : $_->childNodes }
Branch: 1 branch
BranchTrueFalseTotal
$xpath ? : 3 2 5
320 5 @{$self->{nodes}};
321  
322 5 $self->new(\@new, $self);
323 }
324  
325 sub find {
326 95 ▶ my ($self, $selector) = @_;
Subroutine find: called CC=2 SCAR=7.0
Pod documented
327  
328 95 my $xpath = selector_to_xpath($selector, root => './');
329 156 ▶ my @new = map { $_->nodeType == XML_ELEMENT_NODE ? $_ : () }
Branch: 1 branch
BranchTrueFalseTotal
$_->nodeType == 1 ? : 156 0 156
330 101 map { $_->findnodes($xpath) }
331 95 @{$self->{nodes}};
332  
333 95 $self->new(\@new, $self);
334 }
335  
336 sub xfind {
337 1 ▶ my ($self, $xpath) = @_;
Subroutine xfind: called CC=1 SCAR=0.0
Pod documented
338 1 my @new = map { $_->findnodes($xpath) } @{$self->{nodes}};
339 1 $self->new(\@new, $self);
340 }
341  
342 sub filter {
343 2 ▶ my ($self, $selector) = @_;
Subroutine filter: called CC=3 SCAR=11.0
Pod documented
344  
345 2 my $xpath = selector_to_xpath($selector, root => '.');
346 8 ▶ my @new = map { _node_matches($_, $xpath) ? $_ : () }
Branch: 1 branch
BranchTrueFalseTotal
_node_matches($_, $xpath) ? : 4 4 8
347 2 ▶ map { $_->nodeType == XML_ELEMENT_NODE ? $_ : () } @{$self->{nodes}};
Branch: 1 branch
BranchTrueFalseTotal
$_->nodeType == 1 ? : 8 6 14
348  
349 2 $self->new(\@new, $self);
350 }
351  
352 sub xfilter {
353 1 ▶ my ($self, $xpath) = @_;
Subroutine xfilter: called CC=3 SCAR=11.0
Pod documented
354  
355 4 ▶ my @new = map { _node_matches($_, $xpath) ? $_ : () }
Branch: 1 branch
BranchTrueFalseTotal
_node_matches($_, $xpath) ? : 2 2 4
356 1 ▶ map { $_->nodeType == XML_ELEMENT_NODE ? $_ : () } @{$self->{nodes}};
Branch: 1 branch
BranchTrueFalseTotal
$_->nodeType == 1 ? : 4 3 7
357  
358 1 $self->new(\@new, $self);
359 }
360  
361 sub parent {
362 7 ▶ my ($self, $selector) = @_;
Subroutine parent: called CC=4 SCAR=13.9
Pod documented
363  
364 7 ▶ my $xpath = selector_to_xpath($selector, root => '.')
Branch: 1 branch
BranchTrueFalseTotal
if $selector 2 5 7
365 if $selector;
366  
367 my @new = map {
368  
369 10 ▶ !$xpath ? $_
Branch: 2 branches
BranchTrueFalseTotal
!$xpath ? : 6 4 10
_node_matches($_, $xpath) ? : 1 3 4
370 : _node_matches($_, $xpath) ? $_ : ()
371 }
372 11 grep { defined }
373 7 map { $_->parentNode } @{$self->{nodes}};
374  
375 7 $self->new(\@new, $self);
376 }
377  
378 sub clone {
379 1 ▶ my ($self) = @_;
Subroutine clone: called CC=1 SCAR=0.0
Pod documented
380 1 my @new = map { $_->cloneNode(1) } @{$self->{nodes}};
381 1 $self->new(\@new, $self);
382 }
383  
384 sub _node_matches {
385 16 ▶ my ($node, $xpath) = @_;
Subroutine _node_matches: called CC=3 SCAR=11.0
386 # warn sprintf "# matching node: %s (%s)\n", ref $node, $node;
387 16 foreach ($node->parentNode->findnodes($xpath)) {
388 # warn sprintf "# - against node: %s (%s)\n", ref $_, $_;
389 23 ▶ return 1 if $_->isSameNode($node);
Branch: 1 branch
BranchTrueFalseTotal
if $_->isSameNode($node) 7 16 23
390 }
391 9 0;
392 }
393  
394 # TODO add() can ruin our refcount for data()
395 sub add {
396 7 ▶ my ($self, $stuff, $context) = @_;
Subroutine add: called CC=4 SCAR=13.9
Pod documented
397 7 ▶ $context ||= $self->document;
Condition66%2/3
$context ||= $self->document
l!l&&r!l&&!r
160
MC/DC0%0/2
$context ||= $self->document
$context $self->document
Truth table66%2/3
A ||= B
A= $contextB= $self->document
ABresult
1X1
011
000
398  
399 # find(): add(selector[, context])
400 # new: add(html), add(elements), add(jQuery)
401 7 ▶ my $new_selection = !ref $stuff && $stuff !~ /<(?!!).*?>/
Branch: 1 branch
BranchTrueFalseTotal
!(ref $stuff || $stuff =~ /<(?!!).*?>/) ? : 4 3 7
Condition100%3/3
ref $stuff || $stuff =~ /<(?!!).*?>/
l!l&&r!l&&!r
214
MC/DC100%2/2
ref $stuff || $stuff =~ /<(?!!).*?>/
ref $stuff $stuff =~ /<(?!!).*?>/
Truth table100%3/3
A || B
A= ref $stuffB= $stuff =~ /<(?!!).*?>/
ABresult
1X1
011
000
402 ? $context->find($stuff)
403 : $self->new($stuff);
404  
405 # prepend our nodes
406 7 unshift @{$new_selection->{nodes}}, @{ $self->{nodes} };
407  
408 7 $new_selection;
409 }
410  
411 sub each {
412 3 ▶ my ($self, $cb) = @_;
Subroutine each: called CC=4 SCAR=13.9
Pod documented
413  
414 3 ▶ for (my $i = 0; $i < @{$self->{nodes}}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$self->{"nodes"};}) 6 2 8
415  
416 6 local $_ = $self->{nodes}[$i];
417 6 my @rv = $cb->($i, $_);
418 6 ▶ last if @rv == 1 && !defined $rv[0];
Branch: 1 branch
BranchTrueFalseTotal
if @rv == 1 and not defined $rv[0] 1 5 6
Condition100%3/3
@rv == 1 and not defined $rv[0]
!ll&&!rl&&r
411
MC/DC100%2/2
@rv == 1 and not defined $rv[0]
@rv == 1 not defined $rv[0]
Truth table100%3/3
A and not(B)
A= @rv == 1B= not defined $rv[0]
ABresult
0X0
100
111
419 }
420  
421 3 $self;
422 }
423  
424  
425 sub append {
426 3 ▶ my $self = shift;
Subroutine append: called CC=1 SCAR=0.0
Pod documented
427 3 _append_to($self->new(@_)->{nodes}, $self->{nodes});
428 3 $self;
429 }
430  
431 sub append_to {
432 6 ▶ my $self = shift;
Subroutine append_to: called CC=1 SCAR=0.0
Pod documented
433 6 _append_to($self->{nodes}, (ref $self)->new(@_)->{nodes});
434 6 $self;
435 }
436  
437 sub _append_to {
438 9 ▶ my ($content, $target) = @_;
Subroutine _append_to: called CC=9 SCAR=22.7
439  
440 9 ▶ for (my $i = 0; $i < @$target; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$target) 13 9 22
441  
442 13 my $is_last = $i == $#$target;
443 13 my $node = $target->[$i];
444  
445  
446 # thats because appendChild() is not supported on a Document node (as of XML::LibXML 2.0017)
447 13 ▶ if ($node->isa('XML::LibXML::Document')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->isa('XML::LibXML::Document')) { } 1 12 13
448  
449 1 foreach (@$content) {
450 1 ▶ $node->hasChildNodes ? $node->lastChild->addSibling($is_last ? $_ : $_->cloneNode(1))
Branch: 3 branches
BranchTrueFalseTotal
$node->hasChildNodes ? : 1 0 1
$is_last ? : 1 0 1
$is_last ? : 0 0 0
451 : $node->setDocumentElement($is_last ? $_ : $_->cloneNode(1));
452 }
453 }
454 else {
455 $node->appendChild($is_last ? $_ : $_->cloneNode(1))
456 12 ▶ for @$content;
Branch: 1 branch
BranchTrueFalseTotal
$is_last ? : 10 4 14
457 }
458 }
459 }
460  
461  
462 sub prepend {
463 5 ▶ my $self = shift;
Subroutine prepend: called CC=1 SCAR=0.0
Pod documented
464 5 _prepend_to($self->new(@_)->{nodes}, $self->{nodes});
465 5 $self;
466 }
467  
468 sub prepend_to {
469 2 ▶ my $self = shift;
Subroutine prepend_to: called CC=1 SCAR=0.0
Pod documented
470 2 _prepend_to($self->{nodes}, (ref $self)->new(@_)->{nodes});
471 2 $self;
472 }
473  
474 sub _prepend_to {
475 7 ▶ my ($content, $target) = @_;
Subroutine _prepend_to: called CC=11 SCAR=24.3
476  
477 7 ▶ for (my $i = 0; $i < @$target; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$target) 11 7 18
478  
479 11 my $is_last = $i == $#$target;
480 11 my $node = $target->[$i];
481  
482 # thats because insertBefore() is not supported on a Document node (as of XML::LibXML 2.0017)
483 11 ▶ if ($node->isa('XML::LibXML::Document')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->isa("XML::LibXML::Document")) 2 9 11
484  
485 2 foreach (@$content) {
486 2 ▶ $node->hasChildNodes ? $node->lastChild->addSibling($is_last ? $_ : $_->cloneNode(1))
Branch: 3 branches
BranchTrueFalseTotal
$node->hasChildNodes ? : 2 0 2
$is_last ? : 2 0 2
$is_last ? : 0 0 0
487 : $node->setDocumentElement($is_last ? $_ : $_->cloneNode(1));
488 }
489  
490 # rotate
491 2 ▶ while (not $node->firstChild->isSameNode($content->[0])) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($node->firstChild->isSameNode($content->[0])) 2 2 4
492 2 my $first_node = $node->firstChild;
493 2 $first_node->unbindNode;
494 2 $node->lastChild->addSibling($first_node);
495  
496 }
497 }
498  
499 # insert before first child
500 11 my $first_child = $node->firstChild;
501 11 ▶ $node->insertBefore($is_last ? $_ : $_->cloneNode(1), $first_child || undef) for @$content;
Branch: 1 branch
BranchTrueFalseTotal
$is_last ? : 7 4 11
Condition100%2/2
$first_child || undef
l!l
101
MC/DC100%1/1
$first_child || undef
$first_child
Truth table100%2/2
A
A= $first_child
Aresult
11
00
502 }
503 }
504  
505  
506 sub before {
507 8 ▶ my $self = shift;
Subroutine before: called CC=2 SCAR=6.9
Pod documented
508 my $content = ref $_[0] eq 'CODE'
509 ? $_[0]
510 8 ▶ : [map { @{ $self->new($_)->{nodes} } } @_];
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'CODE' ? : 1 7 8
511  
512 8 $self->_insert_before($content, $self->{nodes});
513 8 $self;
514 }
515  
516 sub insert_before {
517 5 ▶ my ($self, $target) = @_;
Subroutine insert_before: called CC=2 SCAR=6.9
Pod documented
518 5 ▶ $target = _is_selector($target) ? $self->document->find($target)
Branch: 1 branch
BranchTrueFalseTotal
_is_selector($target) ? : 2 3 5
519 : (ref $self)->new($target);
520  
521 5 $self->_insert_before($self->{nodes}, $target->{nodes});
522 5 $self;
523 }
524  
525 sub _insert_before {
526 13 ▶ my ($self, $content, $target) = @_;
Subroutine _insert_before: called CC=10 SCAR=23.0
527 13 ▶ return if ref $content eq 'ARRAY' && @$content == 0;
Branch: 1 branch
BranchTrueFalseTotal
if ref $content eq "ARRAY" and @$content == 0 0 13 13
Condition66%2/3
ref $content eq "ARRAY" and @$content == 0
!ll&&!rl&&r
1120
MC/DC0%0/2
ref $content eq "ARRAY" and @$content == 0
ref $content eq "ARRAY" @$content == 0
Truth table66%2/3
A and B
A= ref $content eq "ARRAY"B= @$content == 0
ABresult
0X0
100
111
528  
529 13 ▶ for (my $i = 0; $i < @$target; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$target) 29 13 42
530  
531 29 my $is_last = $i == $#$target;
532 29 my $node = $target->[ $i ];
533 29 my $parent = $node->parentNode;
534 29 my $items;
535  
536 29 ▶ if (ref $content eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $content eq 'CODE') { } 2 27 29
537 2 local $_ = $node;
538 2 $items = (ref $self)->new($content->($i, $_))->{nodes};
539 }
540 else {
541 # content is cloned except for last target
542 27 ▶ $items = $i == $#$target ? $content : [map { $_->cloneNode(1) } @$content];
Branch: 1 branch
BranchTrueFalseTotal
$i == $target->$#* ? : 12 15 27
543 }
544  
545 # thats because insertAfter() is not supported on a Document node (as of XML::LibXML 2.0017)
546 29 ▶ unless ($parent->isa('XML::LibXML::Document')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($parent->isa("XML::LibXML::Document")) 24 5 29
547  
548 24 $parent->insertBefore($_, $node) for @$items;
549 24 next;
550 }
551  
552 # workaround for when parent is document:
553 # append nodes then rotate until content is before node
554 5 $parent->lastChild->addSibling($_) for @$items;
555  
556 5 my $next = $node;
557 5 ▶ while (not $next->isSameNode($items->[0])) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($next->isSameNode($items->[0])) 11 5 16
558 11 my $node_to_move = $next;
559 11 $next = $node_to_move->nextSibling;
560 11 $parent->lastChild->addSibling($node_to_move);
561 }
562 }
563 }
564  
565  
566 sub after {
567 8 ▶ my $self = shift;
Subroutine after: called CC=2 SCAR=6.9
Pod documented
568 my $content = ref $_[0] eq 'CODE'
569 ? $_[0]
570 8 ▶ : [map { @{ $self->new($_)->{nodes} } } @_];
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'CODE' ? : 1 7 8
571  
572 8 $self->_insert_after($content, $self->{nodes});
573 8 $self;
574 }
575  
576 sub insert_after {
577 5 ▶ my ($self, $target) = @_;
Subroutine insert_after: called CC=2 SCAR=6.9
Pod documented
578 5 ▶ $target = _is_selector($target) ? $self->document->find($target)
Branch: 1 branch
BranchTrueFalseTotal
_is_selector($target) ? : 2 3 5
579 : (ref $self)->new($target);
580  
581 5 $self->_insert_after($self->{nodes}, $target->{nodes});
582 5 $self;
583 }
584  
585 sub _insert_after {
586 13 ▶ my ($self, $content, $target) = @_;
Subroutine _insert_after: called CC=10 SCAR=23.0
587 13 ▶ return if ref $content eq 'ARRAY' && @$content == 0;
Branch: 1 branch
BranchTrueFalseTotal
if ref $content eq "ARRAY" and @$content == 0 0 13 13
Condition66%2/3
ref $content eq "ARRAY" and @$content == 0
!ll&&!rl&&r
1120
MC/DC0%0/2
ref $content eq "ARRAY" and @$content == 0
ref $content eq "ARRAY" @$content == 0
Truth table66%2/3
A and B
A= ref $content eq "ARRAY"B= @$content == 0
ABresult
0X0
100
111
588  
589 13 ▶ for (my $i = 0; $i < @$target; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$target) 26 13 39
590  
591 26 my $node = $target->[ $i ];
592 26 my $parent = $node->parentNode;
593 26 my $items;
594  
595 26 ▶ if (ref $content eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $content eq 'CODE') { } 2 24 26
596 2 local $_ = $node;
597 2 $items = (ref $self)->new($content->($i, $_))->{nodes};
598 }
599 else {
600  
601 # content is cloned except for last target
602 24 ▶ $items = $i == $#$target ? $content : [map { $_->cloneNode(1) } @$content];
Branch: 1 branch
BranchTrueFalseTotal
$i == $target->$#* ? : 12 12 24
603 }
604  
605 # thats because insertAfter() is not supported on a Document node (as of XML::LibXML 2.0017)
606 26 ▶ unless ($parent->isa('XML::LibXML::Document')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($parent->isa("XML::LibXML::Document")) 21 5 26
607  
608 21 $parent->insertAfter($_, $node) for reverse @$items;
609 21 next;
610 }
611  
612 # workaround for when parent is document:
613 # append nodes then rotate next siblings until content is after node
614 5 $parent->lastChild->addSibling($_) for @$items;
615 # warn "# rotating until $items[0] is after to $node\n";
616 5 ▶ while (not $node->nextSibling->isSameNode($items->[0])) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($node->nextSibling->isSameNode($items->[0])) 4 5 9
617 4 my $next = $node->nextSibling;
618 # warn "# - next: $next\n";
619 # $next->unbindNode;
620 4 $parent->lastChild->addSibling($next);
621 }
622 }
623 }
624  
625  
626 sub contents {
627 2 ▶ my $self = shift;
Subroutine contents: called CC=1 SCAR=0.0
Pod documented
628 2 my @new = map { $_->childNodes } @{$self->{nodes}};
629 2 $self->new(\@new, $self);
630 }
631  
632 {
633 32 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
634 32 *detach = \&remove;
635 }
636  
637 sub remove {
638 4 ▶ my ($self, $selector) = @_;
Subroutine remove: called CC=3 SCAR=11.0
Pod documented
639  
640 4 ▶ if ($selector) {
Branch: 1 branch
BranchTrueFalseTotal
if ($selector) 1 3 4
641 1 $self->find($selector)->remove;
642 1 return $self;
643 }
644  
645 3 foreach (@{$self->{nodes }}) {
646 # TODO test when there is no parent node
647 3 $_->parentNode->removeChild($_);
648 }
649  
650 3 $self;
651 }
652  
653  
654  
655 sub replace_with {
656 12 ▶ my ($self, $content) = @_;
Subroutine replace_with: called CC=10 SCAR=23.0
Pod documented
657 $content = $self->new($content)->{nodes}
658 12 ▶ unless ref $content eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $content eq "CODE" 11 1 12
659  
660 12 my $target = $self->{nodes};
661 12 ▶ for (my $i = 0; $i < @$target; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$target) 16 10 26
662  
663 16 my $node = $target->[ $i ];
664 16 my $parent = $node->parentNode;
665 16 my $final_content = $content;
666  
667 16 ▶ if (ref $content eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $content eq "CODE") 3 13 16
668 3 local $_ = $self->new($node);
669 3 $final_content = $content->($i, $_); # TODO check this callback signature
670 3 $final_content = $self->new($final_content)->{nodes};
671 }
672  
673 # no content, just remove node
674 16 ▶ unless (@$final_content) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@$final_content) 2 14 16
675 2 $parent->removeChild($node);
676 2 delete $data->{$node->ownerDocument->unique_key}->{$node->unique_key};
677 2 return $self;
678 }
679  
680 # content is cloned except for last target
681 14 ▶ my @items = $i == $#$target ? @$final_content : map { $_->cloneNode(1) } @$final_content;
Branch: 1 branch
BranchTrueFalseTotal
$i == $target->$#* ? : 10 4 14
682  
683 # on doc: append then rotate
684 14 ▶ if ($parent->nodeType == XML_DOCUMENT_NODE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parent->nodeType == 9) { } 5 9 14
685  
686 5 $parent->lastChild->addSibling($_) for @items;
687 5 ▶ while (not $node->nextSibling->isSameNode($items[0])) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($node->nextSibling->isSameNode($items[0])) 3 5 8
688 3 $parent->lastChild->addSibling($node->nextSibling);
689 }
690  
691 5 $parent->removeChild($node);
692 }
693 else {
694 9 my $new_node = shift @items;
695 9 $parent->replaceChild($new_node, $node);
696 9 foreach (@items) {
697 2 $parent->insertAfter($_, $new_node);
698 2 $new_node = $_;
699 }
700 }
701  
702 }
703  
704 10 $self;
705 }
706  
707 sub attr {
708 9 ▶ my $self = shift;
Subroutine attr: called CC=14 SCAR=26.4
Pod documented
709 9 my $attr_name = shift;
710  
711 9 ▶ return unless defined $attr_name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $attr_name 0 9 9
712  
713 # only element nodes
714 9 ▶ my @nodes = map { $_->nodeType == XML_ELEMENT_NODE ? $_ : () } @{$self->{nodes}};
Branch: 1 branch
BranchTrueFalseTotal
$_->nodeType == 1 ? : 10 2 12
715  
716 # get
717 9 ▶ return $nodes[0] ? $nodes[0]->getAttribute(lc $attr_name) : undef
Branch: 2 branches
BranchTrueFalseTotal
unless @_ or ref $attr_name 4 5 9
$nodes[0] ? : 3 1 4
Condition100%3/3
@_ or ref $attr_name
l!l&&r!l&&!r
324
MC/DC100%2/2
@_ or ref $attr_name
@_ ref $attr_name
Truth table100%3/3
A or B
A= @_B= ref $attr_name
ABresult
1X1
011
000
718 unless @_ || ref $attr_name;
719  
720 # set
721 5 ▶ return $self unless @nodes;
Branch: 1 branch
BranchTrueFalseTotal
unless @nodes 1 4 5
722  
723 # set multiple
724 4 ▶ if (ref $attr_name eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $attr_name eq "HASH") 2 2 4
725  
726 2 foreach (@nodes) {
727 3 ▶ while (my ($k, $v) = CORE::each %$attr_name) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = CORE::each %$attr_name) 7 3 10
728 7 $_->setAttribute($k, $v);
729 }
730 }
731  
732 2 return $self;
733 }
734  
735 2 $attr_name = lc $attr_name;
736  
737 # from callback
738 2 ▶ if (ref $_[0] eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] eq 'CODE') { } 1 1 2
739  
740 1 ▶ for (my $i = 0; $i < @nodes; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @nodes) 2 1 3
741  
742 2 local $_ = $nodes[$i];
743 2 my $value = $_[0]->($i, $_->getAttribute($attr_name));
744 2 ▶ $_->setAttribute($attr_name, $value)
Branch: 1 branch
BranchTrueFalseTotal
if defined $value 1 1 2
745 if defined $value;
746 }
747 }
748 else {
749 $_->setAttribute($attr_name, $_[0])
750 1 for @nodes;
751 }
752  
753 2 $self;
754 }
755  
756 sub remove_attr {
757 3 ▶ my ($self, $attr_name) = @_;
Subroutine remove_attr: called CC=5 SCAR=16.1
Pod documented
758 3 ▶ return $self unless defined $attr_name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $attr_name 0 3 3
759  
760 3 $attr_name =~ s/(?:^\s*|\s$)//g;
761  
762 3 ▶ foreach my $node (map { $_->nodeType == XML_ELEMENT_NODE ? $_ : () } @{$self->{nodes}}) {
Branch: 1 branch
BranchTrueFalseTotal
$_->nodeType == 1 ? : 4 1 5
763 4 foreach my $attr (split /\s+/, $attr_name) {
764 6 $node->removeAttribute($attr);
765 }
766 }
767  
768 3 $self;
769 }
770  
771  
772 sub add_class {
773 5 ▶ my ($self, $class) = @_;
Subroutine add_class: called CC=5 SCAR=16.1
Pod documented
774  
775 # only element nodes
776 5 ▶ my @nodes = map { $_->nodeType == XML_ELEMENT_NODE ? $_ : () } @{$self->{nodes}};
Branch: 1 branch
BranchTrueFalseTotal
$_->nodeType == 1 ? : 6 1 7
777  
778 5 ▶ for (my $i = 0; $i < @nodes; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @nodes) 6 5 11
779  
780 6 my $node = $nodes[$i];
781 6 my $new_classes = $class;
782 6 ▶ my $current_class = $node->getAttribute('class') || '';
Condition50%1/2
$node->getAttribute('class') || ''
l!l
60
MC/DC0%0/1
$node->getAttribute('class') || ''
$node->getAttribute('class')
Truth table50%1/2
A
A= $node->getAttribute('class')
Aresult
11
00
783  
784 # from callback
785 6 ▶ if (ref $class eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $class eq "CODE") 2 4 6
786 2 local $_ = $self->new($node);
787 2 $new_classes = $class->($i, $current_class);
788 }
789  
790 6 my %distinct;
791 6 $node->setAttribute('class', join ' ', grep { !$distinct{$_}++ } split(/\s+/, "$current_class $new_classes"));
792 }
793  
794 5 $self
795 }
796  
797 sub remove_class {
798 3 ▶ my ($self, $class) = @_;
Subroutine remove_class: called CC=7 SCAR=19.6
Pod documented
799  
800 # only element nodes
801 3 ▶ my @nodes = map { $_->nodeType == XML_ELEMENT_NODE ? $_ : () } @{$self->{nodes}};
Branch: 1 branch
BranchTrueFalseTotal
$_->nodeType == 1 ? : 4 1 5
802  
803 3 ▶ for (my $i = 0; $i < @nodes; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @nodes) 4 3 7
804  
805 4 my $node = $nodes[$i];
806  
807 # remove all classes
808 4 ▶ unless (defined $class) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $class) 0 4 4
809 0 $node->removeAttribute('class');
810 0 next;
811 }
812  
813 4 my $to_remove = $class;
814 4 ▶ my $current_class = $node->getAttribute('class') || '';
Condition50%1/2
$node->getAttribute('class') || ''
l!l
40
MC/DC0%0/1
$node->getAttribute('class') || ''
$node->getAttribute('class')
Truth table50%1/2
A
A= $node->getAttribute('class')
Aresult
11
00
815  
816 # from callback
817 4 ▶ if (ref $class eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $class eq "CODE") 2 2 4
818 2 local $_ = $self->new($node);
819 2 $to_remove = $class->($i, $current_class);
820 }
821  
822 4 my %to_remove = map { $_ => 1 } split /\s+/, $to_remove;
823 4 my @new_classes = grep { !$to_remove{$_} } split /\s+/, $current_class;
824  
825 4 ▶ @new_classes > 0 ? $node->setAttribute('class', join ' ', @new_classes)
Branch: 1 branch
BranchTrueFalseTotal
@new_classes > 0 ? : 2 2 4
826 : $node->removeAttribute('class');
827 }
828  
829 3 $self
830 }
831  
832 sub data {
833  
834 14 ▶ my $self = shift;
Subroutine data: called CC=19 SCAR=29.6
Pod documented
835  
836 # class method: return whole $data (mainly for test/debug)
837 14 ▶ return $data unless ref $self;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 1 13 14
838  
839 # data(key, val)
840 13 ▶ if (@_ == 2 && defined $_[1]) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 2 and defined $_[1]) 2 11 13
Condition100%3/3
@_ == 2 and defined $_[1]
!ll&&!rl&&r
1012
MC/DC100%2/2
@_ == 2 and defined $_[1]
@_ == 2 defined $_[1]
Truth table100%3/3
A and B
A= @_ == 2B= defined $_[1]
ABresult
0X0
100
111
841  
842 $data->{$_->ownerDocument->unique_key}->{$_->unique_key}->{$_[0]} = $_[1]
843 2 foreach @{$self->{nodes}};
844  
845 2 return $self;
846 }
847  
848  
849 11 ▶ if (@_ == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1) 7 4 11
850  
851 # no nodes
852 7 ▶ return unless defined $self->{nodes}->[0];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"nodes"}[0] 0 7 7
853  
854 # data(undefined)
855 7 ▶ return $self unless defined $_[0];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[0] 0 7 7
856  
857 # data(obj)
858 7 ▶ if (ref $_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0]) 1 6 7
859  
860 1 ▶ die 'data(obj) only accepts a hashref' unless ref $_[0] eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_[0] eq "HASH" 0 1 1
861  
862 $data->{$_->ownerDocument->unique_key}->{$_->unique_key} = $_[0]
863 1 foreach @{$self->{nodes}};
864  
865 1 return $self;
866 }
867  
868 # data(key)
869 6 my $key = $_[0];
870 6 my $node = $self->{nodes}->[0];
871  
872 $data->{$node->ownerDocument->unique_key}->{$node->unique_key} = {}
873 6 ▶ unless $data->{$node->ownerDocument->unique_key}->{$node->unique_key};
Branch: 1 branch
BranchTrueFalseTotal
unless $$data{$node->ownerDocument->unique_key}{$node->unique_key} 1 5 6
874  
875 6 my $node_data = $data->{$node->ownerDocument->unique_key}->{$node->unique_key};
876  
877 # try to pull from data-* attribute
878 6 my $data_attr = 'data-'._decamelize($key);
879 6 $data_attr =~ tr/_/-/;
880  
881 $node_data->{$key} = _convert_data_attr_value($node->getAttribute($data_attr))
882 6 ▶ if !exists $node_data->{$key}
Branch: 1 branch
BranchTrueFalseTotal
if not exists $node_data->{$key} and $node->nodeType == 1 and $node->hasAttribute($data_attr) 5 1 6
Condition66%4/6
not exists $node_data->{$key} and $node->nodeType == 1 and $node->hasAttribute($data_attr)
!ll&&!rl&&r
105
not exists $node_data->{$key} and $node->nodeType == 1
!ll&&!rl&&r
105
MC/DC33%1/3
not exists $node_data->{$key} and $node->nodeType == 1 and $node->hasAttribute($data_attr)
not exists $node_data->{$key} $node->nodeType == 1 $node->hasAttribute($data_attr)
Truth table50%2/4
not(A) and B and C
A= not exists $node_data->{$key}B= $node->nodeType == 1C= $node->hasAttribute($data_attr)
ABCresult
0XX0
10X0
1100
1111
883 && $node->nodeType == XML_ELEMENT_NODE
884 && $node->hasAttribute($data_attr);
885  
886 6 return $node_data->{$key};
887 }
888  
889 # data()
890 4 ▶ if (@_ == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 0) 3 1 4
891  
892 # return all data for first node
893 3 my $node = $self->{nodes}[0];
894 3 ▶ return unless $node;
Branch: 1 branch
BranchTrueFalseTotal
unless $node 0 3 3
895  
896 # poor man's //= {} (for perls < 5.10)
897 3 exists $data->{$node->ownerDocument->unique_key}->{$node->unique_key}->{autovivify_hash};
898 3 my $node_data = $data->{$node->ownerDocument->unique_key}->{$node->unique_key};
899  
900 # pull data-* attributes
901 3 foreach my $attr (grep { $_->name =~ /^data-/ } $node->attributes) {
902  
903 15 my $key = substr($attr->name, 5);
904 15 $key =~ tr/-/_/;
905 15 $key = lcfirst _camelize($key);
906  
907 15 ▶ next if exists $node_data->{$key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $node_data->{$key} 5 10 15
908 10 $node_data->{$key} = _convert_data_attr_value($attr->value);
909 }
910  
911 3 return $node_data;
912 }
913  
914 1 $self;
915 }
916  
917 sub _convert_data_attr_value {
918  
919 # number
920 15 ▶ return $_[0] += 0
Subroutine _convert_data_attr_value: called CC=7 SCAR=19.5
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] =~ /^\d+$/ 3 12 15
921 if $_[0] =~ /^\d+$/;
922  
923 # json array or object
924 12 ▶ return decode_json($_[0])
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] =~ /^(?:\{|\[)/ 3 9 12
925 if $_[0] =~ /^(?:\{|\[)/;
926  
927 # boolean
928 9 ▶ return JSON::true if $_[0] eq 'true';
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] eq "true" 3 6 9
929 6 ▶ return JSON::false if $_[0] eq 'false';
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] eq "false" 0 6 6
930  
931 # undef
932 6 ▶ return undef if $_[0] eq 'null' || $_[0] eq 'undefined';
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] eq "null" or $_[0] eq "undefined" 3 3 6
Condition66%2/3
$_[0] eq "null" or $_[0] eq "undefined"
l!l&&r!l&&!r
303
MC/DC50%1/2
$_[0] eq "null" or $_[0] eq "undefined"
$_[0] eq "null" $_[0] eq "undefined"
Truth table66%2/3
A or B
A= $_[0] eq "null"B= $_[0] eq "undefined"
ABresult
1X1
011
000
933  
934 # other stuff, return unmodified
935 3 $_[0];
936 }
937  
938  
939  
940 sub _decamelize
941 {
942 6 ▶ my $s = shift;
Subroutine _decamelize: called CC=5 SCAR=17.3
943 6 $s =~ s{([^a-zA-Z]?)([A-Z]*)([A-Z])([a-z]?)}{
944 1 my $fc = pos($s)==0;
945 1 my ($p0,$p1,$p2,$p3) = ($1,lc$2,lc$3,$4);
946 1 ▶ my $t = $p0 || $fc ? $p0 : '_';
Branch: 1 branch
BranchTrueFalseTotal
$p0 || $fc ? : 0 1 1
Condition33%1/3
$p0 or $fc
l!l&&r!l&&!r
001
MC/DC0%0/2
$p0 or $fc
$p0 $fc
Truth table33%1/3
A or B
A= $p0B= $fc
ABresult
1X1
011
000
947 1 ▶ $t .= $p3 ? $p1 ? "${p1}_$p2$p3" : "$p2$p3" : "$p1$p2";
Branch: 2 branches
BranchTrueFalseTotal
$p3 ? : 1 0 1
$p1 ? : 0 1 1
948 1 $t;
949 }ge;
950 6 $s;
951 }
952  
953 sub _camelize {
954 15 ▶ my $s = shift;
Subroutine _camelize: called CC=1 SCAR=0.0
955 15 join('', map{ ucfirst $_ } split(/(?<=[A-Za-z])_(?=[A-Za-z])|\b/, $s));
956 }
957  
958 sub _is_selector {
959 10 ▶ defined $_[0]
Subroutine _is_selector: called CC=4 SCAR=14.3
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] and not ref $_[0] and length $_[0] and not $_[0] =~ /<(?!!).*?>/ 4 6 10
Condition66%4/6
defined $_[0] and not ref $_[0] and length $_[0]
!ll&&!rl&&r
604
defined $_[0] and not ref $_[0]
!ll&&!rl&&r
064
MC/DC33%1/3
defined $_[0] and not ref $_[0] and length $_[0]
defined $_[0] not ref $_[0] length $_[0]
Truth table50%2/4
A and not(B) and C
A= defined $_[0]B= not ref $_[0]C= length $_[0]
ABCresult
0XX0
10X0
1100
1111
960 && !ref $_[0]
961 && length $_[0]
962 && $_[0] !~ /<(?!!).*?>/
963 }
964  
965 # TODO rethink this autoload thing... issues:
966 # - global var is bad... spooky action at a distance;
967 # - and if thats ok, we could just add the subref to the symbol table directly
968  
969 sub AUTOLOAD {
970 3 ▶ my $self = shift;
Subroutine AUTOLOAD: called CC=3 SCAR=11.0
971 3 our $AUTOLOAD;
972 3 (my $method = $AUTOLOAD) =~ s/.*:://s;
973  
974 die sprintf "unknown method '$method'"
975 3 ▶ unless ref $self && exists $fn{$method};
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self and exists $fn{$method} 1 2 3
Condition66%2/3
ref $self and exists $fn{$method}
!ll&&!rl&&r
012
MC/DC50%1/2
ref $self and exists $fn{$method}
ref $self exists $fn{$method}
Truth table66%2/3
A and B
A= ref $selfB= exists $fn{$method}
ABresult
0X0
100
111
976  
977 2 local $_ = $self;
978 2 $fn{$method}{sub}->(@_);
979 }
980  
981  
982  
983 # decrement data ref counter, delete data when counter == 0
984 sub DESTROY {
985 363 ▶ my $self = shift;
Subroutine DESTROY: called CC=2 SCAR=6.9
986  
987 # decrement $data refcount
988 363 my $doc_id = $self->{document}->unique_key;
989 363 $data->{$doc_id}{refcount}--;
990 # printf STDERR "[%s] decremented document %d data ref count: %d\n", __PACKAGE__, $doc_id, $data->{$doc_id}{refcount};
991  
992 # delete document data if refcount is 0
993 delete $data->{$doc_id}
994 363 ▶ if $data->{$doc_id}{refcount} == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $data->{$doc_id}{"refcount"} == 0 126 237 363
995 }
996  
997  
998 # TODO create camelized methods alias
999  
1000  
1001  
1002 32 1;
1003 __END__
↑ Summary Build »