blib/lib/HTML/Xit.pm

filter: Statementstmt 88.5% 216 / 244 Branchbran 52.7% 96 / 182 Conditioncond 47.0% 24 / 51 MC/DCmcdc 21.2% 7 / 33 Subroutinesub 86.3% 38 / 44 Podpod 100% 18 / 18 totaltotal 69.7% 399 / 572 CC 118 SCAR 19.7
CC
118
Cov
70%
__ANON__
20.0
__ANON__
15.5
html
13.3
CRAP 7.2
↑ Summary
1 package HTML::Xit;
2  
3 14 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 14 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 14 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 14 ▶use HTML::Selector::XPath qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
8 selector_to_xpath
9 14 );
10 14 ▶use Scalar::Util qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
11 blessed
12 reftype
13 weaken
14 14 );
15 14 ▶use XML::LibXML;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 14 our $VERSION = '0.05';
18  
19 # default arguments for XML::LibXML
20 14 my $default_libxml_args = {
21 recover => 1,
22 suppress_errors => 1,
23 suppress_warnings => 1,
24 };
25  
26 ###################
27 # #
28 # PRIVATE METHODS #
29 # #
30 ###################
31  
32 # empty_x
33 #
34 # because methods are chained we need an HTML::Xit object to return
35 # even if a previous selection in the chain failed.
36 14 ▶my $empty_x = sub { };
Subroutine __ANON__: not called CC=1 SCAR=0.0
37 14 bless($empty_x, __PACKAGE__);
38  
39 # new_explicit_args
40 #
41 # create new XML::LibXML instance with args that are passed
42 16 ▶my $new_explicit_args = sub {
Subroutine __ANON__: called CC=3 SCAR=11.1
43 16 my %args = @_;
44  
45 16 for my $arg (keys %$default_libxml_args) {
46 # set default args values unless the arg is
47 # explicitly set
48 $args{$arg} = $default_libxml_args->{$arg}
49 48 ▶ unless exists $args{$arg};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args{$arg} 48 0 48
50 }
51  
52 return {
53 16 _xml => XML::LibXML->load_html( %args )
54 };
55 14 };
56  
57 # new_guess_args
58 #
59 # guess source as one of IO, location, or string based
60 # on the type of scalar that is passed.
61 #
62 # call new_explicit_args once type is guessed.
63 16 ▶my $new_guess_args = sub {
Subroutine __ANON__: called CC=8 SCAR=21.0
64 16 ▶ my $arg = shift or die;
Branch: 1 branch
BranchTrueFalseTotal
my $arg = shift() or die 16 0 16
65  
66 16 ▶ if (ref $arg) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $arg) { } 2 14 16
elsif ($arg =~ /^http/ or not $arg =~ /\n/ and -e $arg) { } 13 1 14
67 # file handle
68 2 ▶ if (reftype $arg eq 'GLOB') {
Branch: 2 branches
BranchTrueFalseTotal
if (builtin::reftype($arg) eq 'GLOB') { } 1 1 2
elsif (builtin::reftype($arg) eq 'SCALAR') { } 1 0 1
69 1 return $new_explicit_args->(IO => $arg);
70 }
71 # scalar ref
72 elsif (reftype $arg eq 'SCALAR') {
73 1 return $new_explicit_args->(string => $arg);
74 }
75 }
76 # url or valid file path
77 ▶ elsif ( $arg =~ m{^http} || ($arg !~ m{\n} && -e $arg) ) {
Condition83%5/6
$arg =~ /^http/ or not $arg =~ /\n/ and -e $arg
l!l&&r!l&&!r
1121
not $arg =~ /\n/ and -e $arg
!ll&&!rl&&r
1012
MC/DC66%2/3
$arg =~ /^http/ or not $arg =~ /\n/ and -e $arg
$arg =~ /^http/ not $arg =~ /\n/ -e $arg
Truth table75%3/4
A or not(B) and C
A= $arg =~ /^http/B= not $arg =~ /\n/C= -e $arg
ABCresult
1XX1
0111
00X0
0100
78 13 return $new_explicit_args->(location => $arg);
79 }
80 # text
81 else {
82 1 return $new_explicit_args->(string => $arg);
83 }
84 14 };
85  
86 # declare this first since we need to call in from closure
87 14 my $new_X;
88 # new_X
89 #
90 # create a new X instance which is a function reference that
91 # can return a hidden hash ref instance where data is stored
92 96 ▶$new_X = sub {
Subroutine __ANON__: called CC=2 SCAR=7.1
93 96 my($self) = @_;
94 # self must be a hash ref that includes a XML::LibXML object
95 96 ▶ return $empty_x unless eval { $self->{_xml} };
Branch: 1 branch
BranchTrueFalseTotal
unless eval { do { $self->{"_xml"} } } 0 96 96
96  
97 # need to jump through some hoops here to avoid creating
98 # a circular ref. We can't have $X getting captured in its
99 # own closure, but we need to know the scalar value of $X, so
100 # create $Y, then assign the scalar value of $X to $Y after creating
101 # $X
102 96 my $Y;
103 # HTML::Xit instance
104 155 ▶ my $X = sub {
Subroutine __ANON__: called CC=8 SCAR=21.7
105 155 my($select) = @_;
106 # if we are passed a self-reference then
107 # return our hidden instance variable
108 155 ▶ return $self
Branch: 1 branch
BranchTrueFalseTotal
if ref $select and $select eq $Y 87 68 155
Condition66%2/3
ref $select and $select eq $Y
!ll&&!rl&&r
68087
MC/DC50%1/2
ref $select and $select eq $Y
ref $select $select eq $Y
Truth table66%2/3
A and B
A= ref $selectB= $select eq $Y
ABresult
0X0
100
111
109 if ref $select
110 and $select eq $Y;
111  
112 68 my $xml = $self->{_xml};
113 # else create a new instance
114 68 my $self = {};
115 # create new node
116 68 ▶ if ($select =~ m{^<([^>]+)>$}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($select =~ /^<([^>]+)>$/) { } 3 65 68
117 3 my $node_name = $1;
118 # try to get XML::LibXML::Document
119 3 ▶ my $doc = $xml
Branch: 3 branches
BranchTrueFalseTotal
$xml ? : 3 0 3
$xml->can('createElement') ? : 3 0 3
$xml->can('ownerDocument') ? : 0 0 0
120 # if the current node supports createElement then use it
121 ? $xml->can('createElement')
122 ? $xml
123 # if the current belongs to a Document then use that
124 : $xml->can('ownerDocument')
125 ? $xml->ownerDocument
126 : XML::LibXML::Document->new
127 # create a new Document to create node from
128 : XML::LibXML::Document->new;
129 # create element under document
130 3 $xml = $self->{_xml} = $doc->createElement($node_name);
131 }
132 # value is selector
133 else {
134 # generate xpath from CSS selector using HTML::Selector::XPath
135 65 ▶ my $xpath = selector_to_xpath($select)
Branch: 1 branch
BranchTrueFalseTotal
my $xpath = selector_to_xpath($select) or return $empty_x 65 0 65
136 or return $empty_x;
137 # set the current xml context as the result of the xpath selection
138 65 $self->{_xml} = $xml->find('.'.$xpath);
139 }
140 # return a new HTML::Xit instace with either the selected or created elements
141 68 return $new_X->($self);
142 96 };
143  
144 96 bless($X, __PACKAGE__);
145 # store scalar value of $X without creating another ref
146 96 $Y = "$X";
147 # return blessed sub ref
148 96 return $X;
149 14 };
150  
151 # each
152 #
153 # call callback function for each argument
154 #
155 # unlike the public version of this method the private version does
156 # not create HTML::Xit instances around the returned object.
157 29 ▶my $each = sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
158 29 my($elm, $func) = @_;
159  
160 29 ▶ if (ref $elm && reftype $elm eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $elm and builtin::reftype($elm) eq 'ARRAY') { } 23 6 29
Condition66%2/3
ref $elm and builtin::reftype($elm) eq "ARRAY"
!ll&&!rl&&r
0623
MC/DC50%1/2
ref $elm and builtin::reftype($elm) eq "ARRAY"
ref $elm builtin::reftype($elm) eq "ARRAY"
Truth table66%2/3
A and B
A= ref $elmB= builtin::reftype($elm) eq "ARRAY"
ABresult
0X0
100
111
161 23 $func->($_) for @$elm;
162 }
163 else {
164 6 $func->($elm);
165 }
166 14 };
167  
168 # first
169 #
170 # return the first element from array or return arg if not an array.
171 #
172 # unlike the public version of this method the private version does
173 # not create HTML::Xit instances around the returned object.
174 28 ▶my $first = sub {
Subroutine __ANON__: called CC=3 SCAR=11.1
175 28 my($elm) = @_;
176  
177 28 ▶ return ref $elm && reftype $elm eq 'ARRAY'
Branch: 1 branch
BranchTrueFalseTotal
ref $elm && builtin::reftype($elm) eq 'ARRAY' ? : 17 11 28
Condition66%2/3
ref $elm and builtin::reftype($elm) eq "ARRAY"
!ll&&!rl&&r
01117
MC/DC50%1/2
ref $elm and builtin::reftype($elm) eq "ARRAY"
ref $elm builtin::reftype($elm) eq "ARRAY"
Truth table66%2/3
A and B
A= ref $elmB= builtin::reftype($elm) eq "ARRAY"
ABresult
0X0
100
111
178 ? $elm->[0]
179 : $elm;
180 14 };
181  
182 # arg_to_nodes
183 #
184 # take one or more arguments that may be strings of xml/html
185 # Xit objects referencing XML, or XML objects and return an
186 # XML fragment
187 my $arg_to_nodes = sub
188 4 ▶{
Subroutine __ANON__: called CC=5 SCAR=16.2
189 4 ▶ my $parser = XML::LibXML->new(%$default_libxml_args)
Branch: 1 branch
BranchTrueFalseTotal
my $parser = "XML::LibXML"->new(%$default_libxml_args) or return 4 0 4
190 or return;
191  
192 4 my $nodes = [];
193  
194 4 for my $arg (@_) {
195 5 ▶ if (ref $arg) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $arg) { } 3 2 5
196 3 ▶ if ($arg->isa('HTML::Xit')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg->isa("HTML::Xit")) 3 0 3
197 3 push(@$nodes, @{$arg->get});
198 }
199 }
200 else {
201 2 my $xml = $parser->parse_balanced_chunk($arg);
202 2 push(@$nodes, $xml);
203 }
204 }
205  
206 4 return $nodes;
207 14 };
208  
209 # mod_class
210 #
211 # perform addClass, removeClass, and toggleClass methods
212 10 ▶my $mod_class = sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
213 10 my($action, $X, $value) = @_;
214  
215 10 ▶ return $X unless $value;
Branch: 1 branch
BranchTrueFalseTotal
unless $value 0 10 10
216  
217 10 my $self = $X->($X);
218 10 ▶ my $xml = $self->{_xml} or return $X;
Branch: 1 branch
BranchTrueFalseTotal
my $xml = $self->{"_xml"} or return $X 10 0 10
219 # class list may be one or more space seperated class names
220 10 my @mod_classes = grep {$_ =~ /\w+/} split(/\s+/, $value);
221  
222 10 ▶ return $X unless @mod_classes;
Branch: 1 branch
BranchTrueFalseTotal
unless @mod_classes 0 10 10
223  
224 10 ▶ $each->($xml, sub {
Subroutine __ANON__: called CC=8 SCAR=20.8
225 10 my($sel) = @_;
226  
227 10 my $class = $sel->getAttribute('class');
228  
229 my $classes = {
230 10 map {$_ => 1} grep {$_ =~ /\w+/} split(/\s+/, $class)
231 };
232  
233 10 ▶ if ($action eq 'add') {
Branch: 3 branches
BranchTrueFalseTotal
if ($action eq 'add') { } 5 5 10
elsif ($action eq 'remove') { } 2 3 5
elsif ($action eq 'toggle') { } 3 0 3
234 5 $classes->{$_} = 1 for @mod_classes;
235 }
236 elsif ($action eq 'remove') {
237 2 delete $classes->{$_} for @mod_classes;
238 }
239 elsif ($action eq 'toggle') {
240 3 for my $mod_class (@mod_classes) {
241 4 ▶ if ($classes->{$mod_class}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($classes->{$mod_class}) { } 2 2 4
242 2 delete $classes->{$mod_class};
243 }
244 else {
245 2 $classes->{$mod_class} = 1;
246 }
247 }
248 }
249  
250 10 $class = join(' ', sort keys %$classes);
251  
252 10 $sel->setAttribute('class', $class);
253 10 });
254  
255 10 return $X;
256 14 };
257  
258 ##################
259 # #
260 # PUBLIC METHODS #
261 # #
262 ##################
263  
264 # new
265 #
266 # create new HTML::Xit instance which is a function ref
267 sub new
268 16 ▶{
Subroutine new: called CC=6 SCAR=19.3
Pod documented
269 16 my $class = shift;
270 # process args which may be in the form of:
271 # new("<html>...")
272 # new("http://www...")
273 # new(FH)
274 # new("/my/file.html")
275 # new(a => 1, b => 2, ...)
276 # new({a => 1, b => 2})
277 my $self = @_ == 1
278 ? ref $_[0] && ref $_[0] eq 'HASH'
279 # first arg is hash ref, use as args
280 0 ▶ ? $new_explicit_args->( %{$_[0]} )
Branch: 2 branches
BranchTrueFalseTotal
@_ == 1 ? : 16 0 16
ref $_[0] && ref $_[0] eq 'HASH' ? : 0 16 16
Condition66%2/3
ref $_[0] and ref $_[0] eq "HASH"
!ll&&!rl&&r
1420
MC/DC0%0/2
ref $_[0] and ref $_[0] eq "HASH"
ref $_[0] ref $_[0] eq "HASH"
Truth table66%2/3
A and B
A= ref $_[0]B= ref $_[0] eq "HASH"
ABresult
0X0
100
111
281 # first arg is not hash ref, guess what it is
282 : $new_guess_args->(@_)
283 # treat multiple args as explicit, which are passed
284 # directly to XML::LibXML
285 : $new_explicit_args->(@_);
286 # need XML::LibXML instance to continue
287 16 ▶ return unless $self and $self->{_xml};
Branch: 1 branch
BranchTrueFalseTotal
unless $self and $self->{"_xml"} 1 15 16
Condition66%2/3
$self and $self->{"_xml"}
!ll&&!rl&&r
0115
MC/DC50%1/2
$self and $self->{"_xml"}
$self $self->{"_xml"}
Truth table66%2/3
A and B
A= $selfB= $self->{"_xml"}
ABresult
0X0
100
111
288  
289 15 return $new_X->($self);
290 }
291  
292 # addClass
293 #
294 # add class using $mod_class
295 5 ▶sub addClass { $mod_class->('add', @_) }
Subroutine addClass: called CC=1 SCAR=0.0
Pod documented
296  
297 # append
298 #
299 # add nodes after last child
300 sub append
301 1 ▶{
Subroutine append: called CC=3 SCAR=11.4
Pod documented
302 1 my($X) = shift;
303 1 my $self = $X->($X);
304  
305 1 ▶ my $xml = $self->{_xml} or return $X;
Branch: 1 branch
BranchTrueFalseTotal
my $xml = $self->{"_xml"} or return $X 1 0 1
306  
307 1 ▶ my $child_nodes = $arg_to_nodes->(@_)
Branch: 1 branch
BranchTrueFalseTotal
my $child_nodes = &$arg_to_nodes(@_) or return $X 1 0 1
308 or return $X;
309  
310 1 ▶ $each->($xml, sub {
Subroutine __ANON__: called CC=3 SCAR=11.6
311 1 ▶ my $sel = shift or return;
Branch: 1 branch
BranchTrueFalseTotal
my $sel = shift() or return 1 0 1
312 # must be able to have children
313 1 ▶ return unless $sel->can('appendChild');
Branch: 1 branch
BranchTrueFalseTotal
unless $sel->can("appendChild") 0 1 1
314 # append one or more child nodes
315 1 ▶ $each->($child_nodes, sub {
Subroutine __ANON__: called CC=2 SCAR=7.2
316 1 ▶ my $node = shift or return;
Branch: 1 branch
BranchTrueFalseTotal
my $node = shift() or return 1 0 1
317 # deep clone child
318 1 $sel->appendChild( $node->cloneNode(1) );
319 1 });
320 1 });
321  
322 1 return $X;
323 }
324  
325 # attr
326 #
327 # get or set an attribute on selected XML nodes
328 sub attr
329 14 ▶{
Subroutine attr: called CC=5 SCAR=16.5
Pod documented
330 14 my($X, $name, $value) = @_;
331 14 my $self = $X->($X);
332 14 my $xml = $self->{_xml};
333  
334 14 ▶ if (defined $value)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) { } 5 9 14
335 {
336 5 ▶ return $X unless $xml;
Branch: 1 branch
BranchTrueFalseTotal
unless $xml 0 5 5
337  
338 7 ▶ $each->($xml, sub {
Subroutine __ANON__: called CC=3 SCAR=12.0
339 7 ▶ my $sel = shift or return;
Branch: 1 branch
BranchTrueFalseTotal
my $sel = shift() or return 7 0 7
340  
341 7 ▶ $sel->setAttribute($name, $value)
Branch: 1 branch
BranchTrueFalseTotal
if $sel->can("setAttribute") 7 0 7
342 if $sel->can('setAttribute');
343 5 });
344  
345 5 return $X;
346 }
347 else
348 {
349 9 ▶ return undef unless $xml;
Branch: 1 branch
BranchTrueFalseTotal
unless $xml 0 9 9
350  
351 9 my $sel = $first->($xml);
352  
353 9 ▶ return $sel->can('getAttribute')
Branch: 1 branch
BranchTrueFalseTotal
$sel->can('getAttribute') ? : 9 0 9
354 ? $sel->getAttribute($name)
355 : undef;
356 }
357 }
358  
359 # children
360 #
361 # return child nodes. *does not* process optional selector
362 # at this time.
363 sub children
364 2 ▶{
Subroutine children: called CC=7 SCAR=24.5
Pod documented
365 2 my($X) = @_;
366 2 my $self = $X->($X);
367  
368 2 ▶ my $xml = $self->{_xml} or return $X;
Branch: 1 branch
BranchTrueFalseTotal
my $xml = $self->{"_xml"} or return $X 2 0 2
369  
370 # if we are working on a list of elements then return
371 # the children of all of the elements
372 2 ▶ if (reftype $xml eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::reftype($xml) eq 'ARRAY') { } 2 0 2
373 2 my @child_nodes;
374 # add child nodes of each element to list
375 2 push(@child_nodes, $_->childNodes) for grep {
376 2 $_->can('childNodes')
377 } @$xml;
378 # return empty HTML::Xit unless child nodes were found
379 2 ▶ return $empty_x unless @child_nodes;
Branch: 1 branch
BranchTrueFalseTotal
unless @child_nodes 0 2 2
380 # return new HTML::Xit with child nodes
381 2 return $new_X->( {_xml => \@child_nodes} );
382 }
383 else {
384 # get child nodes or return empty HTML::Xit object
385 0 ▶ return $empty_x unless $xml->can('childNodes')
Branch: 1 branch
BranchTrueFalseTotal
unless $xml->can("childNodes") and my(@child_nodes) = $xml->childNodes 0 0 0
Condition0%0/3
$xml->can("childNodes") and my(@child_nodes) = $xml->childNodes
!ll&&!rl&&r
000
MC/DC0%0/2
$xml->can("childNodes") and my(@child_nodes) = $xml->childNodes
$xml->can("childNodes") my(@child_nodes) = $xml->childNodes
Truth table0%0/3
A and B
A= $xml->can("childNodes")B= my(@child_nodes) = $xml->childNodes
ABresult
0X0
100
111
386 and (my @child_nodes = $xml->childNodes);
387 # create new HTML::Xit object from child nodes
388 0 return $new_X->( {_xml => \@child_nodes} );
389 }
390 }
391  
392 # classes
393 #
394 # return a list of the classes assigned to elements
395 sub classes
396 26 ▶{
Subroutine classes: called CC=6 SCAR=18.4
Pod documented
397 26 my($X) = @_;
398 26 my $self = $X->($X);
399  
400 26 ▶ my $xml = $self->{_xml} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $xml = $self->{"_xml"} or return 26 0 26
401  
402 26 my $classes;
403  
404 # for multiple elements add each element's class attribute
405 # to the list of classes
406 26 ▶ if (reftype $xml eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::reftype($xml) eq 'ARRAY') { } 26 0 26
407 26 $classes .= " " . $_->getAttribute('class') for @$xml;
408 }
409 # get class attribute for single element
410 else {
411 0 $classes = $xml->getAttribute('class');
412 }
413 # build table of unique class names
414 $classes = {
415 26 ▶ map {$_ => 1} grep {defined $_ && $_ =~ /\w+/} split(/\s+/, $classes)
Condition66%2/3
defined $_ && $_ =~ /\w+/
!ll&&!rl&&r
02237
MC/DC50%1/2
defined $_ && $_ =~ /\w+/
defined $_ $_ =~ /\w+/
Truth table66%2/3
A && B
A= defined $_B= $_ =~ /\w+/
ABresult
0X0
100
111
416 };
417  
418 return wantarray
419 # return sorted list of classes
420 26 ▶ ? sort keys %$classes
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 4 22 26
421 # return hashref of classes
422 : $classes;
423 }
424  
425 # each
426 #
427 # call callback function for each argument
428 #
429 # unlike the private version, this method creates a new
430 # HTML::Xit instance for each XML element being iterated on
431 sub each
432 3 ▶{
Subroutine each: called CC=5 SCAR=18.1
Pod documented
433 3 my($X, $func) = @_;
434 3 my $self = $X->($X);
435  
436 3 ▶ my $xml = $self->{_xml} or return $X;
Branch: 1 branch
BranchTrueFalseTotal
my $xml = $self->{"_xml"} or return $X 3 0 3
437  
438 3 ▶ if (ref $xml && reftype $xml eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $xml and builtin::reftype($xml) eq 'ARRAY') { } 3 0 3
Condition33%1/3
ref $xml and builtin::reftype($xml) eq "ARRAY"
!ll&&!rl&&r
003
MC/DC0%0/2
ref $xml and builtin::reftype($xml) eq "ARRAY"
ref $xml builtin::reftype($xml) eq "ARRAY"
Truth table33%1/3
A and B
A= ref $xmlB= builtin::reftype($xml) eq "ARRAY"
ABresult
0X0
100
111
439 # call callback for each element in array, creating a
440 # new HTML::Xit instance for each XML element
441 3 $func->( $new_X->({_xml => $_}) ) for @$xml;
442 }
443 else {
444 # call callback, creating a new HTML::Xit instance
445 0 $func->( $new_X->({_xml => $xml}) );
446 }
447  
448 3 return $X;
449 }
450  
451 # find
452 #
453 # get all matching child elements
454 sub find
455 0 ▶{
Subroutine find: not called CC=1 SCAR=6.9
Pod documented
456 0 my($X, $query) = @_;
457  
458 0 my $nodes = [];
459  
460 0 ▶ $X->each(sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
461 0 my($X) = @_;
462 0 ▶ my $node = $X->($query)->get
Branch: 1 branch
BranchTrueFalseTotal
my $node = &$X($query)->get or return 0 0 0
463 or return;
464 0 ▶ $node = [$node]
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::reftype($node) eq "ARRAY" 0 0 0
465 unless reftype $node eq 'ARRAY';
466 0 push(@$nodes, @$node);
467 0 });
468  
469 0 return $new_X->({_xml => $nodes})
470 }
471  
472 # first
473 #
474 # return the first element from array or return arg if not an array.
475 #
476 # unlike the private version, this method creates a new
477 # HTML::Xit instance for node being returned
478 1 ▶sub first {
Subroutine first: called CC=4 SCAR=15.6
Pod documented
479 1 my($X) = @_;
480 1 my $self = $X->($X);
481  
482 1 ▶ my $xml = $self->{_xml} or return $X;
Branch: 1 branch
BranchTrueFalseTotal
my $xml = $self->{"_xml"} or return $X 1 0 1
483  
484 1 ▶ return ref $xml && reftype $xml eq 'ARRAY'
Branch: 1 branch
BranchTrueFalseTotal
ref $xml && builtin::reftype($xml) eq 'ARRAY' ? : 1 0 1
Condition33%1/3
ref $xml and builtin::reftype($xml) eq "ARRAY"
!ll&&!rl&&r
001
MC/DC0%0/2
ref $xml and builtin::reftype($xml) eq "ARRAY"
ref $xml builtin::reftype($xml) eq "ARRAY"
Truth table33%1/3
A and B
A= ref $xmlB= builtin::reftype($xml) eq "ARRAY"
ABresult
0X0
100
111
485 ? $new_X->( {_xml => $xml->[0]} )
486 : $new_X->( {_xml => $xml} );
487 };
488  
489 # get
490 #
491 # return the XML::LibXML nodes or node identified by index
492 sub get
493 3 ▶{
Subroutine get: called CC=6 SCAR=21.4
Pod documented
494 3 my($X, $index) = @_;
495 3 my $self = $X->($X);
496  
497 3 ▶ my $xml = $self->{_xml} or return $X;
Branch: 1 branch
BranchTrueFalseTotal
my $xml = $self->{"_xml"} or return $X 3 0 3
498 # make sure we have nodes in array
499 3 ▶ my $nodes = ref $xml && reftype $xml eq 'ARRAY'
Branch: 1 branch
BranchTrueFalseTotal
ref $xml && builtin::reftype($xml) eq 'ARRAY' ? : 0 3 3
Condition33%1/3
ref $xml and builtin::reftype($xml) eq "ARRAY"
!ll&&!rl&&r
030
MC/DC0%0/2
ref $xml and builtin::reftype($xml) eq "ARRAY"
ref $xml builtin::reftype($xml) eq "ARRAY"
Truth table33%1/3
A and B
A= ref $xmlB= builtin::reftype($xml) eq "ARRAY"
ABresult
0X0
100
111
500 ? $xml : [ $xml ];
501 # return either all nodes or specified index
502 3 ▶ return defined $index && int $index
Branch: 1 branch
BranchTrueFalseTotal
defined $index && int $index ? : 0 3 3
Condition33%1/3
defined $index and int $index
!ll&&!rl&&r
300
MC/DC0%0/2
defined $index and int $index
defined $index int $index
Truth table33%1/3
A and B
A= defined $indexB= int $index
ABresult
0X0
100
111
503 ? $nodes->[ $index ]
504 : $nodes;
505 }
506  
507 # hasClass
508 #
509 # return true (1) / false (0) if any of the elements have
510 # the class. return undef on errors.
511 sub hasClass
512 21 ▶{
Subroutine hasClass: called CC=4 SCAR=14.2
Pod documented
513 21 my($X, $class) = @_;
514 # require class name to test
515 21 ▶ return unless defined $class;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $class 0 21 21
516 # get hashref of class names
517 21 ▶ my $classes = $X->classes()
Branch: 1 branch
BranchTrueFalseTotal
my $classes = $X->classes or return 21 0 21
518 or return;
519  
520 21 ▶ return $classes->{$class} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$classes->{$class} ? : 13 8 21
521 }
522  
523 # html
524 #
525 # return html content or if that is not possible return text
526 # content
527 sub html
528 11 ▶{
Subroutine html: called CC=10 SCAR=25.8
Pod documented
529 11 my($X) = shift;
530 11 my $self = $X->($X);
531  
532 11 my $xml = $self->{_xml};
533 # if there are args we modify the current elements
534 # and return the current HTML::Xit instance
535 11 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 2 9 11
536 # require current element
537 2 ▶ return $X unless $xml;
Branch: 1 branch
BranchTrueFalseTotal
unless $xml 0 2 2
538 # build nodes to be appended from args which can
539 # be either HTML strings or HTML::Xit objects
540 2 ▶ my $child_nodes = $arg_to_nodes->(@_)
Branch: 1 branch
BranchTrueFalseTotal
my $child_nodes = &$arg_to_nodes(@_) or return $X 2 0 2
541 or return $X;
542 # go through each of the current elements and set
543 # the child nodes from args to be their HTML content
544 4 ▶ $each->($xml, sub {
Subroutine __ANON__: called CC=4 SCAR=14.8
545 4 ▶ my $sel = shift or return;
Branch: 1 branch
BranchTrueFalseTotal
my $sel = shift() or return 4 0 4
546 # must be able to have children
547 4 ▶ return unless $sel->can('appendChild');
Branch: 1 branch
BranchTrueFalseTotal
unless $sel->can("appendChild") 0 4 4
548 # html replaces any existing child nodes
549 4 ▶ $sel->removeChildNodes()
Branch: 1 branch
BranchTrueFalseTotal
if $sel->can("removeChildNodes") 4 0 4
550 if $sel->can('removeChildNodes');
551 # append one or more child nodes
552 5 ▶ $each->($child_nodes, sub {
Subroutine __ANON__: called CC=2 SCAR=7.2
553 5 ▶ my $node = shift or return;
Branch: 1 branch
BranchTrueFalseTotal
my $node = shift() or return 5 0 5
554 # deep clone child
555 5 $sel->appendChild( $node->cloneNode(1) );
556 4 });
557 2 });
558 # return HTML::Xit instance
559 2 return $X;
560 }
561 # if there are no args then we return the HTML text of the
562 # current elements or an empty string
563 else {
564 # require current element
565 9 ▶ return undef unless $xml;
Branch: 1 branch
BranchTrueFalseTotal
unless $xml 0 9 9
566 # get the first element
567 9 my $sel = $first->($xml);
568 # if the current node has children then create html
569 # by concatenating html values of child nodes
570 9 ▶ if ( $sel->can('childNodes') && (my @child_nodes = $sel->childNodes) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sel->can('childNodes') and my(@child_nodes) = $sel->childNodes) { } 9 0 9
Condition33%1/3
$sel->can("childNodes") and my(@child_nodes) = $sel->childNodes
!ll&&!rl&&r
009
MC/DC0%0/2
$sel->can("childNodes") and my(@child_nodes) = $sel->childNodes
$sel->can("childNodes") my(@child_nodes) = $sel->childNodes
Truth table33%1/3
A and B
A= $sel->can("childNodes")B= my(@child_nodes) = $sel->childNodes
ABresult
0X0
100
111
571 return join('', map {
572 9 ▶ $_->can('toStringHTML')
Branch: 2 branches
BranchTrueFalseTotal
$_->can('toStringHTML') ? : 0 14 14
$_->can('toString') ? : 14 0 14
573 ? $_->toStringHTML
574 : $_->can('toString')
575 ? $_->toString
576 : ''
577 } @child_nodes);
578 }
579 # if the node has no children then it can only have
580 # text content (?) maybe not possible
581 else {
582 0 ▶ return $sel->can('toString')
Branch: 1 branch
BranchTrueFalseTotal
$sel->can('toString') ? : 0 0 0
583 ? $sel->toString
584 : '';
585 }
586 }
587 }
588  
589 # last
590 #
591 # return the last matching element
592 1 ▶sub last {
Subroutine last: called CC=4 SCAR=15.6
Pod documented
593 1 my($X) = @_;
594 1 my $self = $X->($X);
595  
596 1 ▶ my $xml = $self->{_xml} or return $X;
Branch: 1 branch
BranchTrueFalseTotal
my $xml = $self->{"_xml"} or return $X 1 0 1
597  
598 1 ▶ return ref $xml && reftype $xml eq 'ARRAY'
Branch: 1 branch
BranchTrueFalseTotal
ref $xml && builtin::reftype($xml) eq 'ARRAY' ? : 1 0 1
Condition33%1/3
ref $xml and builtin::reftype($xml) eq "ARRAY"
!ll&&!rl&&r
001
MC/DC0%0/2
ref $xml and builtin::reftype($xml) eq "ARRAY"
ref $xml builtin::reftype($xml) eq "ARRAY"
Truth table33%1/3
A and B
A= ref $xmlB= builtin::reftype($xml) eq "ARRAY"
ABresult
0X0
100
111
599 ? $new_X->( {_xml => $xml->[-1]} )
600 : $new_X->( {_xml => $xml} );
601 };
602  
603 # prepend
604 #
605 # add nodes before first child
606 sub prepend
607 1 ▶{
Subroutine prepend: called CC=3 SCAR=15.8
Pod documented
608 1 my($X) = shift;
609 1 my $self = $X->($X);
610  
611 1 ▶ my $xml = $self->{_xml} or return $X;
Branch: 1 branch
BranchTrueFalseTotal
my $xml = $self->{"_xml"} or return $X 1 0 1
612  
613 1 ▶ my $child_nodes = $arg_to_nodes->(@_)
Branch: 1 branch
BranchTrueFalseTotal
my $child_nodes = &$arg_to_nodes(@_) or return $X 1 0 1
614 or return $X;
615  
616 1 my $first_child = shift @$child_nodes;
617  
618 1 ▶ $each->($xml, sub {
Subroutine __ANON__: called CC=5 SCAR=27.4
619 1 ▶ my $sel = shift or return;
Branch: 1 branch
BranchTrueFalseTotal
my $sel = shift() or return 1 0 1
620  
621 1 ▶ if ($sel->can('firstChild')) {
Branch: 2 branches
BranchTrueFalseTotal
if ($sel->can('firstChild')) { } 1 0 1
elsif ($self->can('addChild')) { } 0 0 0
622 # insert first node before first child
623 1 ▶ my $insert_after = $sel->insertBefore(
Branch: 1 branch
BranchTrueFalseTotal
my $insert_after = $sel->insertBefore($first_child->cloneNode(1), $sel->firstChild) or return 1 0 1
624 $first_child->cloneNode(1),
625 $sel->firstChild,
626 ) or return;
627  
628 0 ▶ $each->($child_nodes, sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
629 0 ▶ my $node = shift or return;
Branch: 1 branch
BranchTrueFalseTotal
my $node = shift() or return 0 0 0
630  
631 0 ▶ return unless $insert_after
Branch: 1 branch
BranchTrueFalseTotal
unless $insert_after and $insert_after->can("insertAfter") 0 0 0
Condition0%0/3
$insert_after and $insert_after->can("insertAfter")
!ll&&!rl&&r
000
MC/DC0%0/2
$insert_after and $insert_after->can("insertAfter")
$insert_after $insert_after->can("insertAfter")
Truth table0%0/3
A and B
A= $insert_afterB= $insert_after->can("insertAfter")
ABresult
0X0
100
111
632 and $insert_after->can('insertAfter');
633  
634 0 $insert_after = $sel->insertAfter(
635 $node->cloneNode(1),
636 $insert_after
637 );
638 1 });
639 }
640 elsif ($self->can('addChild')) {
641 0 $sel->addChild( $first_child->cloneNode(1) );
642 0 ▶ $each->($child_nodes, sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
643 0 ▶ my $node = shift or return;
Branch: 1 branch
BranchTrueFalseTotal
my $node = shift() or return 0 0 0
644 0 });
645 }
646 else {
647 0 return;
648 }
649 1 });
650  
651 1 return $X;
652 }
653  
654 # removeClass
655 #
656 # remove class using $mod_class
657 2 ▶sub removeClass { $mod_class->('remove', @_) }
Subroutine removeClass: called CC=1 SCAR=0.0
Pod documented
658  
659 # text
660 #
661 # return text content
662 sub text
663 14 ▶{
Subroutine text: called CC=6 SCAR=20.7
Pod documented
664 14 my($X, $value) = @_;
665 14 my $self = $X->($X);
666  
667 14 my $xml = $self->{_xml};
668  
669 14 ▶ if (defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) { } 4 10 14
670 4 ▶ return $X unless $xml;
Branch: 1 branch
BranchTrueFalseTotal
unless $xml 0 4 4
671  
672 6 ▶ $each->($xml, sub {
Subroutine __ANON__: called CC=6 SCAR=25.2
673 6 ▶ my $sel = shift or return;
Branch: 1 branch
BranchTrueFalseTotal
my $sel = shift() or return 6 0 6
674 # text replaces everything else so remove child nodes
675 # if they exist
676 6 ▶ $sel->removeChildNodes() if $sel->can('removeChildNodes');
Branch: 1 branch
BranchTrueFalseTotal
if $sel->can("removeChildNodes") 6 0 6
677 # attempt different methods of adding text
678 # XML::LibXML::Element
679 6 ▶ if ($sel->can('appendText')) {
Branch: 3 branches
BranchTrueFalseTotal
if ($sel->can('appendText')) { } 6 0 6
elsif ($sel->can('setData')) { } 0 0 0
elsif ($sel->can('appendChild')) { } 0 0 0
680 6 $sel->appendText($value);
681 }
682 # XML::LibXML::Text
683 elsif ($sel->can('setData')) {
684 0 $sel->setData($value);
685 }
686 # XML::LibXML::Node
687 elsif ($sel->can('appendChild')) {
688 0 $sel->appendChild( $sel->createTextNode($value) );
689 }
690 4 });
691 }
692 else {
693 10 ▶ return undef unless $xml;
Branch: 1 branch
BranchTrueFalseTotal
unless $xml 0 10 10
694  
695 10 my $sel = $first->($xml);
696 10 ▶ return $sel && $sel->can('textContent')
Branch: 1 branch
BranchTrueFalseTotal
$sel && $sel->can('textContent') ? : 10 0 10
Condition33%1/3
$sel and $sel->can("textContent")
!ll&&!rl&&r
0010
MC/DC0%0/2
$sel and $sel->can("textContent")
$sel $sel->can("textContent")
Truth table33%1/3
A and B
A= $selB= $sel->can("textContent")
ABresult
0X0
100
111
697 ? $sel->textContent
698 : undef;
699 }
700  
701 4 return $X;
702 }
703  
704 # trimText
705 sub trimText
706 0 ▶{
Subroutine trimText: not called CC=2 SCAR=17.9
Pod documented
707 0 my($X) = @_;
708  
709 0 my $text = $X->text;
710 0 ▶ return unless defined $text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text 0 0 0
711  
712 0 $text =~ s{^\s*|\s*$}{}g;
713  
714 0 return $text;
715 }
716  
717 # toggleClass
718 #
719 # toggle class using $mod_class
720 3 ▶sub toggleClass { $mod_class->('toggle', @_) }
Subroutine toggleClass: called CC=1 SCAR=0.0
Pod documented
721  
722 14 1;
723  
724 __END__
↑ Summary