blib/lib/XML/LibXML/jQuery.pm

filter: Statementstmt 98.5% 491 / 498 Branchbran 84.9% 214 / 252 Conditioncond 65.0% 39 / 60 MC/DCmcdc 37.8% 14 / 37 Subroutinesub 98.4% 62 / 63 Podpod 97.5% 39 / 40 totaltotal 90.4% 859 / 950 CC 176 SCAR 20.8
CC
176
Cov
92%
data
19.2
new
15.0
attr
14.0
CRAP 8.0
↑ 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.07";
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) 215 148 363
61 215 $self = $class;
62 215 $class = ref $self;
63 215 $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) 215 148 363
102  
103 # my $doc_id = $existing_document->unique_key;
104 $document->adoptNode($_)
105 215 for grep { not $document->isSameNode($_->ownerDocument) }
106 286 grep { $_->nodeType != XML_DOCUMENT_NODE }
107 @$nodes;
108 }
109 }
110  
111 # resolve document
112 363 ▶ unless ($document) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($document) 148 215 363
113  
114 148 ▶ $document = defined $nodes->[0] ? $nodes->[0]->ownerDocument
Branch: 1 branch
BranchTrueFalseTotal
defined $nodes->[0] ? : 148 0 148
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=2 SCAR=7.0
Pod documented
380 1 ▶ return $self unless @{$self->{nodes}};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$self->{"nodes"};} 0 1 1
381  
382 1 my @new = map { $_->cloneNode(1) } @{$self->{nodes}};
383 1 (ref $self)->new(\@new, $self);
384 }
385  
386 sub _node_matches {
387 16 ▶ my ($node, $xpath) = @_;
Subroutine _node_matches: called CC=3 SCAR=11.0
388 # warn sprintf "# matching node: %s (%s)\n", ref $node, $node;
389 16 foreach ($node->parentNode->findnodes($xpath)) {
390 # warn sprintf "# - against node: %s (%s)\n", ref $_, $_;
391 23 ▶ return 1 if $_->isSameNode($node);
Branch: 1 branch
BranchTrueFalseTotal
if $_->isSameNode($node) 7 16 23
392 }
393 9 0;
394 }
395  
396 # TODO add() can ruin our refcount for data()
397 sub add {
398 7 ▶ my ($self, $stuff, $context) = @_;
Subroutine add: called CC=4 SCAR=13.9
Pod documented
399 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
400  
401 # find(): add(selector[, context])
402 # new: add(html), add(elements), add(jQuery)
403 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
404 ? $context->find($stuff)
405 : $self->new($stuff);
406  
407 # prepend our nodes
408 7 unshift @{$new_selection->{nodes}}, @{ $self->{nodes} };
409  
410 7 $new_selection;
411 }
412  
413 sub each {
414 3 ▶ my ($self, $cb) = @_;
Subroutine each: called CC=4 SCAR=13.9
Pod documented
415  
416 3 ▶ for (my $i = 0; $i < @{$self->{nodes}}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$self->{"nodes"};}) 6 2 8
417  
418 6 local $_ = $self->{nodes}[$i];
419 6 my @rv = $cb->($i, $_);
420 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
421 }
422  
423 3 $self;
424 }
425  
426  
427 sub append {
428 3 ▶ my $self = shift;
Subroutine append: called CC=1 SCAR=0.0
Pod documented
429 3 _append_to($self->new(@_)->{nodes}, $self->{nodes});
430 3 $self;
431 }
432  
433 sub append_to {
434 6 ▶ my $self = shift;
Subroutine append_to: called CC=1 SCAR=0.0
Pod documented
435 6 _append_to($self->{nodes}, (ref $self)->new(@_)->{nodes});
436 6 $self;
437 }
438  
439 sub _append_to {
440 9 ▶ my ($content, $target) = @_;
Subroutine _append_to: called CC=10 SCAR=24.0
441  
442 9 ▶ for (my $i = 0; $i < @$target; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$target) 13 9 22
443  
444 13 my $is_last = $i == $#$target;
445 13 my $node = $target->[$i];
446  
447  
448 # thats because appendChild() is not supported on a Document node (as of XML::LibXML 2.0017)
449 13 ▶ if ($node->isa('XML::LibXML::Document')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->isa('XML::LibXML::Document')) { } 1 12 13
450  
451 1 foreach (@$content) {
452 1 ▶ confess "# Document->setDocumentElement: doc\n"
Branch: 1 branch
BranchTrueFalseTotal
if ref $_ eq "XML::LibXML::Document" 0 1 1
453 if ref $_ eq 'XML::LibXML::Document';
454  
455 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
456 : $node->setDocumentElement($is_last ? $_ : $_->cloneNode(1));
457 }
458 }
459 else {
460 $node->appendChild($is_last ? $_ : $_->cloneNode(1))
461 12 ▶ for @$content;
Branch: 1 branch
BranchTrueFalseTotal
$is_last ? : 10 4 14
462 }
463 }
464 }
465  
466  
467 sub prepend {
468 5 ▶ my $self = shift;
Subroutine prepend: called CC=1 SCAR=0.0
Pod documented
469 5 _prepend_to($self->new(@_)->{nodes}, $self->{nodes});
470 5 $self;
471 }
472  
473 sub prepend_to {
474 2 ▶ my $self = shift;
Subroutine prepend_to: called CC=1 SCAR=0.0
Pod documented
475 2 _prepend_to($self->{nodes}, (ref $self)->new(@_)->{nodes});
476 2 $self;
477 }
478  
479 sub _prepend_to {
480 7 ▶ my ($content, $target) = @_;
Subroutine _prepend_to: called CC=11 SCAR=24.3
481  
482 7 ▶ for (my $i = 0; $i < @$target; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$target) 11 7 18
483  
484 11 my $is_last = $i == $#$target;
485 11 my $node = $target->[$i];
486  
487 # thats because insertBefore() is not supported on a Document node (as of XML::LibXML 2.0017)
488 11 ▶ if ($node->isa('XML::LibXML::Document')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->isa("XML::LibXML::Document")) 2 9 11
489  
490 2 foreach (@$content) {
491 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
492 : $node->setDocumentElement($is_last ? $_ : $_->cloneNode(1));
493 }
494  
495 # rotate
496 2 ▶ while (not $node->firstChild->isSameNode($content->[0])) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($node->firstChild->isSameNode($content->[0])) 2 2 4
497 2 my $first_node = $node->firstChild;
498 2 $first_node->unbindNode;
499 2 $node->lastChild->addSibling($first_node);
500  
501 }
502 }
503  
504 # insert before first child
505 11 my $first_child = $node->firstChild;
506 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
507 }
508 }
509  
510  
511 sub before {
512 8 ▶ my $self = shift;
Subroutine before: called CC=2 SCAR=6.9
Pod documented
513 my $content = ref $_[0] eq 'CODE'
514 ? $_[0]
515 8 ▶ : [map { @{ $self->new($_)->{nodes} } } @_];
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'CODE' ? : 1 7 8
516  
517 8 $self->_insert_before($content, $self->{nodes});
518 8 $self;
519 }
520  
521 sub insert_before {
522 5 ▶ my ($self, $target) = @_;
Subroutine insert_before: called CC=2 SCAR=6.9
Pod documented
523 5 ▶ $target = _is_selector($target) ? $self->document->find($target)
Branch: 1 branch
BranchTrueFalseTotal
_is_selector($target) ? : 2 3 5
524 : (ref $self)->new($target);
525  
526 5 $self->_insert_before($self->{nodes}, $target->{nodes});
527 5 $self;
528 }
529  
530 sub _insert_before {
531 13 ▶ my ($self, $content, $target) = @_;
Subroutine _insert_before: called CC=10 SCAR=23.0
532 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
533  
534 13 ▶ for (my $i = 0; $i < @$target; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$target) 29 13 42
535  
536 29 my $is_last = $i == $#$target;
537 29 my $node = $target->[ $i ];
538 29 my $parent = $node->parentNode;
539 29 my $items;
540  
541 29 ▶ if (ref $content eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $content eq 'CODE') { } 2 27 29
542 2 local $_ = $node;
543 2 $items = (ref $self)->new($content->($i, $_))->{nodes};
544 }
545 else {
546 # content is cloned except for last target
547 27 ▶ $items = $i == $#$target ? $content : [map { $_->cloneNode(1) } @$content];
Branch: 1 branch
BranchTrueFalseTotal
$i == $target->$#* ? : 12 15 27
548 }
549  
550 # thats because insertAfter() is not supported on a Document node (as of XML::LibXML 2.0017)
551 29 ▶ unless ($parent->isa('XML::LibXML::Document')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($parent->isa("XML::LibXML::Document")) 24 5 29
552  
553 24 $parent->insertBefore($_, $node) for @$items;
554 24 next;
555 }
556  
557 # workaround for when parent is document:
558 # append nodes then rotate until content is before node
559 5 $parent->lastChild->addSibling($_) for @$items;
560  
561 5 my $next = $node;
562 5 ▶ while (not $next->isSameNode($items->[0])) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($next->isSameNode($items->[0])) 11 5 16
563 11 my $node_to_move = $next;
564 11 $next = $node_to_move->nextSibling;
565 11 $parent->lastChild->addSibling($node_to_move);
566 }
567 }
568 }
569  
570  
571 sub after {
572 8 ▶ my $self = shift;
Subroutine after: called CC=2 SCAR=6.9
Pod documented
573 my $content = ref $_[0] eq 'CODE'
574 ? $_[0]
575 8 ▶ : [map { @{ $self->new($_)->{nodes} } } @_];
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'CODE' ? : 1 7 8
576  
577 8 $self->_insert_after($content, $self->{nodes});
578 8 $self;
579 }
580  
581 sub insert_after {
582 5 ▶ my ($self, $target) = @_;
Subroutine insert_after: called CC=2 SCAR=6.9
Pod documented
583 5 ▶ $target = _is_selector($target) ? $self->document->find($target)
Branch: 1 branch
BranchTrueFalseTotal
_is_selector($target) ? : 2 3 5
584 : (ref $self)->new($target);
585  
586 5 $self->_insert_after($self->{nodes}, $target->{nodes});
587 5 $self;
588 }
589  
590 sub _insert_after {
591 13 ▶ my ($self, $content, $target) = @_;
Subroutine _insert_after: called CC=10 SCAR=23.0
592 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
593  
594 13 ▶ for (my $i = 0; $i < @$target; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$target) 26 13 39
595  
596 26 my $node = $target->[ $i ];
597 26 my $parent = $node->parentNode;
598 26 my $items;
599  
600 26 ▶ if (ref $content eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $content eq 'CODE') { } 2 24 26
601 2 local $_ = $node;
602 2 $items = (ref $self)->new($content->($i, $_))->{nodes};
603 }
604 else {
605  
606 # content is cloned except for last target
607 24 ▶ $items = $i == $#$target ? $content : [map { $_->cloneNode(1) } @$content];
Branch: 1 branch
BranchTrueFalseTotal
$i == $target->$#* ? : 12 12 24
608 }
609  
610 # thats because insertAfter() is not supported on a Document node (as of XML::LibXML 2.0017)
611 26 ▶ unless ($parent->isa('XML::LibXML::Document')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($parent->isa("XML::LibXML::Document")) 21 5 26
612  
613 21 $parent->insertAfter($_, $node) for reverse @$items;
614 21 next;
615 }
616  
617 # workaround for when parent is document:
618 # append nodes then rotate next siblings until content is after node
619 5 $parent->lastChild->addSibling($_) for @$items;
620 # warn "# rotating until $items[0] is after to $node\n";
621 5 ▶ while (not $node->nextSibling->isSameNode($items->[0])) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($node->nextSibling->isSameNode($items->[0])) 4 5 9
622 4 my $next = $node->nextSibling;
623 # warn "# - next: $next\n";
624 # $next->unbindNode;
625 4 $parent->lastChild->addSibling($next);
626 }
627 }
628 }
629  
630  
631 sub contents {
632 2 ▶ my $self = shift;
Subroutine contents: called CC=1 SCAR=0.0
Pod documented
633 2 my @new = map { $_->childNodes } @{$self->{nodes}};
634 2 $self->new(\@new, $self);
635 }
636  
637 {
638 32 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
639 32 *detach = \&remove;
640 }
641  
642 sub remove {
643 4 ▶ my ($self, $selector) = @_;
Subroutine remove: called CC=3 SCAR=11.0
Pod documented
644  
645 4 ▶ if ($selector) {
Branch: 1 branch
BranchTrueFalseTotal
if ($selector) 1 3 4
646 1 $self->find($selector)->remove;
647 1 return $self;
648 }
649  
650 3 foreach (@{$self->{nodes }}) {
651 # TODO test when there is no parent node
652 3 $_->parentNode->removeChild($_);
653 }
654  
655 3 $self;
656 }
657  
658  
659  
660 sub replace_with {
661 12 ▶ my ($self, $content) = @_;
Subroutine replace_with: called CC=10 SCAR=23.0
Pod documented
662 $content = $self->new($content)->{nodes}
663 12 ▶ unless ref $content eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $content eq "CODE" 11 1 12
664  
665 12 my $target = $self->{nodes};
666 12 ▶ for (my $i = 0; $i < @$target; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$target) 16 10 26
667  
668 16 my $node = $target->[ $i ];
669 16 my $parent = $node->parentNode;
670 16 my $final_content = $content;
671  
672 16 ▶ if (ref $content eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $content eq "CODE") 3 13 16
673 3 local $_ = $self->new($node);
674 3 $final_content = $content->($i, $_); # TODO check this callback signature
675 3 $final_content = $self->new($final_content)->{nodes};
676 }
677  
678 # no content, just remove node
679 16 ▶ unless (@$final_content) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@$final_content) 2 14 16
680 2 $parent->removeChild($node);
681 2 delete $data->{$node->ownerDocument->unique_key}->{$node->unique_key};
682 2 return $self;
683 }
684  
685 # content is cloned except for last target
686 14 ▶ my @items = $i == $#$target ? @$final_content : map { $_->cloneNode(1) } @$final_content;
Branch: 1 branch
BranchTrueFalseTotal
$i == $target->$#* ? : 10 4 14
687  
688 # on doc: append then rotate
689 14 ▶ if ($parent->nodeType == XML_DOCUMENT_NODE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parent->nodeType == 9) { } 5 9 14
690  
691 5 $parent->lastChild->addSibling($_) for @items;
692 5 ▶ while (not $node->nextSibling->isSameNode($items[0])) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($node->nextSibling->isSameNode($items[0])) 3 5 8
693 3 $parent->lastChild->addSibling($node->nextSibling);
694 }
695  
696 5 $parent->removeChild($node);
697 }
698 else {
699 9 my $new_node = shift @items;
700 9 $parent->replaceChild($new_node, $node);
701 9 foreach (@items) {
702 2 $parent->insertAfter($_, $new_node);
703 2 $new_node = $_;
704 }
705 }
706  
707 }
708  
709 10 $self;
710 }
711  
712 sub attr {
713 9 ▶ my $self = shift;
Subroutine attr: called CC=14 SCAR=26.4
Pod documented
714 9 my $attr_name = shift;
715  
716 9 ▶ return unless defined $attr_name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $attr_name 0 9 9
717  
718 # only element nodes
719 9 ▶ my @nodes = map { $_->nodeType == XML_ELEMENT_NODE ? $_ : () } @{$self->{nodes}};
Branch: 1 branch
BranchTrueFalseTotal
$_->nodeType == 1 ? : 10 2 12
720  
721 # get
722 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
723 unless @_ || ref $attr_name;
724  
725 # set
726 5 ▶ return $self unless @nodes;
Branch: 1 branch
BranchTrueFalseTotal
unless @nodes 1 4 5
727  
728 # set multiple
729 4 ▶ if (ref $attr_name eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $attr_name eq "HASH") 2 2 4
730  
731 2 foreach (@nodes) {
732 3 ▶ while (my ($k, $v) = CORE::each %$attr_name) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = CORE::each %$attr_name) 7 3 10
733 7 $_->setAttribute($k, $v);
734 }
735 }
736  
737 2 return $self;
738 }
739  
740 2 $attr_name = lc $attr_name;
741  
742 # from callback
743 2 ▶ if (ref $_[0] eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] eq 'CODE') { } 1 1 2
744  
745 1 ▶ for (my $i = 0; $i < @nodes; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @nodes) 2 1 3
746  
747 2 local $_ = $nodes[$i];
748 2 my $value = $_[0]->($i, $_->getAttribute($attr_name));
749 2 ▶ $_->setAttribute($attr_name, $value)
Branch: 1 branch
BranchTrueFalseTotal
if defined $value 1 1 2
750 if defined $value;
751 }
752 }
753 else {
754 $_->setAttribute($attr_name, $_[0])
755 1 for @nodes;
756 }
757  
758 2 $self;
759 }
760  
761 sub remove_attr {
762 3 ▶ my ($self, $attr_name) = @_;
Subroutine remove_attr: called CC=5 SCAR=16.1
Pod documented
763 3 ▶ return $self unless defined $attr_name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $attr_name 0 3 3
764  
765 3 $attr_name =~ s/(?:^\s*|\s$)//g;
766  
767 3 ▶ foreach my $node (map { $_->nodeType == XML_ELEMENT_NODE ? $_ : () } @{$self->{nodes}}) {
Branch: 1 branch
BranchTrueFalseTotal
$_->nodeType == 1 ? : 4 1 5
768 4 foreach my $attr (split /\s+/, $attr_name) {
769 6 $node->removeAttribute($attr);
770 }
771 }
772  
773 3 $self;
774 }
775  
776  
777 sub add_class {
778 5 ▶ my ($self, $class) = @_;
Subroutine add_class: called CC=5 SCAR=16.1
Pod documented
779  
780 # only element nodes
781 5 ▶ my @nodes = map { $_->nodeType == XML_ELEMENT_NODE ? $_ : () } @{$self->{nodes}};
Branch: 1 branch
BranchTrueFalseTotal
$_->nodeType == 1 ? : 6 1 7
782  
783 5 ▶ for (my $i = 0; $i < @nodes; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @nodes) 6 5 11
784  
785 6 my $node = $nodes[$i];
786 6 my $new_classes = $class;
787 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
788  
789 # from callback
790 6 ▶ if (ref $class eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $class eq "CODE") 2 4 6
791 2 local $_ = $self->new($node);
792 2 $new_classes = $class->($i, $current_class);
793 }
794  
795 6 my %distinct;
796 6 $node->setAttribute('class', join ' ', grep { !$distinct{$_}++ } split(/\s+/, "$current_class $new_classes"));
797 }
798  
799 5 $self
800 }
801  
802 sub remove_class {
803 3 ▶ my ($self, $class) = @_;
Subroutine remove_class: called CC=7 SCAR=19.6
Pod documented
804  
805 # only element nodes
806 3 ▶ my @nodes = map { $_->nodeType == XML_ELEMENT_NODE ? $_ : () } @{$self->{nodes}};
Branch: 1 branch
BranchTrueFalseTotal
$_->nodeType == 1 ? : 4 1 5
807  
808 3 ▶ for (my $i = 0; $i < @nodes; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @nodes) 4 3 7
809  
810 4 my $node = $nodes[$i];
811  
812 # remove all classes
813 4 ▶ unless (defined $class) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $class) 0 4 4
814 0 $node->removeAttribute('class');
815 0 next;
816 }
817  
818 4 my $to_remove = $class;
819 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
820  
821 # from callback
822 4 ▶ if (ref $class eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $class eq "CODE") 2 2 4
823 2 local $_ = $self->new($node);
824 2 $to_remove = $class->($i, $current_class);
825 }
826  
827 4 my %to_remove = map { $_ => 1 } split /\s+/, $to_remove;
828 4 my @new_classes = grep { !$to_remove{$_} } split /\s+/, $current_class;
829  
830 4 ▶ @new_classes > 0 ? $node->setAttribute('class', join ' ', @new_classes)
Branch: 1 branch
BranchTrueFalseTotal
@new_classes > 0 ? : 2 2 4
831 : $node->removeAttribute('class');
832 }
833  
834 3 $self
835 }
836  
837 sub data {
838  
839 14 ▶ my $self = shift;
Subroutine data: called CC=19 SCAR=29.6
Pod documented
840  
841 # class method: return whole $data (mainly for test/debug)
842 14 ▶ return $data unless ref $self;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 1 13 14
843  
844 # data(key, val)
845 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
846  
847 $data->{$_->ownerDocument->unique_key}->{$_->unique_key}->{$_[0]} = $_[1]
848 2 foreach @{$self->{nodes}};
849  
850 2 return $self;
851 }
852  
853  
854 11 ▶ if (@_ == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1) 7 4 11
855  
856 # no nodes
857 7 ▶ return unless defined $self->{nodes}->[0];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"nodes"}[0] 0 7 7
858  
859 # data(undefined)
860 7 ▶ return $self unless defined $_[0];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[0] 0 7 7
861  
862 # data(obj)
863 7 ▶ if (ref $_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0]) 1 6 7
864  
865 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
866  
867 $data->{$_->ownerDocument->unique_key}->{$_->unique_key} = $_[0]
868 1 foreach @{$self->{nodes}};
869  
870 1 return $self;
871 }
872  
873 # data(key)
874 6 my $key = $_[0];
875 6 my $node = $self->{nodes}->[0];
876  
877 $data->{$node->ownerDocument->unique_key}->{$node->unique_key} = {}
878 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
879  
880 6 my $node_data = $data->{$node->ownerDocument->unique_key}->{$node->unique_key};
881  
882 # try to pull from data-* attribute
883 6 my $data_attr = 'data-'._decamelize($key);
884 6 $data_attr =~ tr/_/-/;
885  
886 $node_data->{$key} = _convert_data_attr_value($node->getAttribute($data_attr))
887 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
888 && $node->nodeType == XML_ELEMENT_NODE
889 && $node->hasAttribute($data_attr);
890  
891 6 return $node_data->{$key};
892 }
893  
894 # data()
895 4 ▶ if (@_ == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 0) 3 1 4
896  
897 # return all data for first node
898 3 my $node = $self->{nodes}[0];
899 3 ▶ return unless $node;
Branch: 1 branch
BranchTrueFalseTotal
unless $node 0 3 3
900  
901 # poor man's //= {} (for perls < 5.10)
902 3 exists $data->{$node->ownerDocument->unique_key}->{$node->unique_key}->{autovivify_hash};
903 3 my $node_data = $data->{$node->ownerDocument->unique_key}->{$node->unique_key};
904  
905 # pull data-* attributes
906 3 foreach my $attr (grep { $_->name =~ /^data-/ } $node->attributes) {
907  
908 15 my $key = substr($attr->name, 5);
909 15 $key =~ tr/-/_/;
910 15 $key = lcfirst _camelize($key);
911  
912 15 ▶ next if exists $node_data->{$key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $node_data->{$key} 5 10 15
913 10 $node_data->{$key} = _convert_data_attr_value($attr->value);
914 }
915  
916 3 return $node_data;
917 }
918  
919 1 $self;
920 }
921  
922 sub _convert_data_attr_value {
923  
924 # number
925 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
926 if $_[0] =~ /^\d+$/;
927  
928 # json array or object
929 12 ▶ return decode_json($_[0])
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] =~ /^(?:\{|\[)/ 3 9 12
930 if $_[0] =~ /^(?:\{|\[)/;
931  
932 # boolean
933 9 ▶ return JSON::true if $_[0] eq 'true';
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] eq "true" 3 6 9
934 6 ▶ return JSON::false if $_[0] eq 'false';
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] eq "false" 0 6 6
935  
936 # undef
937 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
938  
939 # other stuff, return unmodified
940 3 $_[0];
941 }
942  
943  
944  
945 sub _decamelize
946 {
947 6 ▶ my $s = shift;
Subroutine _decamelize: called CC=5 SCAR=17.3
948 6 $s =~ s{([^a-zA-Z]?)([A-Z]*)([A-Z])([a-z]?)}{
949 1 my $fc = pos($s)==0;
950 1 my ($p0,$p1,$p2,$p3) = ($1,lc$2,lc$3,$4);
951 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
952 1 ▶ $t .= $p3 ? $p1 ? "${p1}_$p2$p3" : "$p2$p3" : "$p1$p2";
Branch: 2 branches
BranchTrueFalseTotal
$p3 ? : 1 0 1
$p1 ? : 0 1 1
953 1 $t;
954 }ge;
955 6 $s;
956 }
957  
958 sub _camelize {
959 15 ▶ my $s = shift;
Subroutine _camelize: called CC=1 SCAR=0.0
960 15 join('', map{ ucfirst $_ } split(/(?<=[A-Za-z])_(?=[A-Za-z])|\b/, $s));
961 }
962  
963 sub _is_selector {
964 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
965 && !ref $_[0]
966 && length $_[0]
967 && $_[0] !~ /<(?!!).*?>/
968 }
969  
970 # TODO rethink this autoload thing... issues:
971 # - global var is bad... spooky action at a distance;
972 # - and if thats ok, we could just add the subref to the symbol table directly
973  
974 sub AUTOLOAD {
975 3 ▶ my $self = shift;
Subroutine AUTOLOAD: called CC=3 SCAR=11.0
976 3 our $AUTOLOAD;
977 3 (my $method = $AUTOLOAD) =~ s/.*:://s;
978  
979 die sprintf "unknown method '$method'"
980 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
981  
982 2 local $_ = $self;
983 2 $fn{$method}{sub}->(@_);
984 }
985  
986  
987  
988 # decrement data ref counter, delete data when counter == 0
989 sub DESTROY {
990 363 ▶ my $self = shift;
Subroutine DESTROY: called CC=3 SCAR=11.0
991  
992 # Don't know why, but document is undefined in some situations..
993 # wiped out by XS code probably.
994 363 ▶ return unless defined $self->{document};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"document"} 0 363 363
995  
996 # decrement $data refcount
997 363 my $doc_id = $self->{document}->unique_key;
998 363 $data->{$doc_id}{refcount}--;
999 # printf STDERR "[%s] decremented document %d data ref count: %d\n", __PACKAGE__, $doc_id, $data->{$doc_id}{refcount};
1000  
1001 # delete document data if refcount is 0
1002 delete $data->{$doc_id}
1003 363 ▶ if $data->{$doc_id}{refcount} == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $data->{$doc_id}{"refcount"} == 0 126 237 363
1004 }
1005  
1006  
1007 # TODO create camelized methods alias
1008  
1009  
1010  
1011 32 1;
1012 __END__
↑ Summary Build »