blib/lib/XML/LibXML/jQuery.pm

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