Query.pm

filter: Statementstmt 99.0% 436 / 440 Branchbran 91.2% 135 / 148 Conditioncond 44.5% 37 / 83 MC/DCmcdc 9.2% 5 / 54 Subroutinesub 100% 76 / 76 Podpod 95.9% 47 / 49 totaltotal 86.5% 736 / 850 CC 122 SCAR 15.0
CC
122
Cov
91%
_resolve_new
11.7
replace_with
11.6
attr
8.0
CRAP 4.5
↑ Summary Query/LibXML.pm »
1 package Web::Query;
2 48 our $AUTHORITY = 'cpan:TOKUHIROM';
3 # ABSTRACT: Yet another scraping library like jQuery
4 48 $Web::Query::VERSION = '1.01';
5 48 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 48 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 48 ▶use 5.008001;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 48 ▶use parent qw/Exporter/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 48 ▶use HTML::TreeBuilder::XPath;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 48 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 48 ▶use HTML::Selector::XPath 0.20 qw/selector_to_xpath/;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 48 ▶use Scalar::Util qw/blessed refaddr/;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 48 ▶use HTML::Entities qw/encode_entities/;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 48 ▶use List::Util 1.44 qw/ reduce uniq /;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 48 ▶use Scalar::Util qw/ refaddr /;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 48 our @EXPORT = qw/wq/;
19  
20 48 our $RESPONSE;
21  
22 90 ▶sub wq { Web::Query->new(@_) }
Subroutine wq: called CC=1 SCAR=0.0
Pod documented
23  
24 48 our $UserAgent;
25  
26 8 ▶sub __ua { $UserAgent ||= LWP::UserAgent->new }
Subroutine __ua: called CC=2 SCAR=7.2
Condition66%2/3
$Web::Query::UserAgent ||= "LWP::UserAgent"->new
l!l&&r!l&&!r
710
MC/DC0%0/2
$Web::Query::UserAgent ||= "LWP::UserAgent"->new
$Web::Query::UserAgent "LWP::UserAgent"->new
Truth table66%2/3
A ||= B
A= $Web::Query::UserAgentB= "LWP::UserAgent"->new
ABresult
1X1
011
000
27  
28 122 ▶sub _build_tree {
Subroutine _build_tree: called CC=3 SCAR=11.0
29 122 my( $self, $options ) = @_;
30  
31 my $no_space_compacting = ref $self ? $self->{no_space_compacting}
32 122 ▶ : ref $options eq 'HASH' ? $options->{no_space_compacting} : 0;
Branch: 2 branches
BranchTrueFalseTotal
ref $self ? : 2 120 122
ref $options eq 'HASH' ? : 3 117 120
33  
34 122 my $tree = HTML::TreeBuilder::XPath->new(
35 no_space_compacting => $no_space_compacting
36 );
37 122 $tree->ignore_unknown(0);
38 122 $tree->store_comments(1);
39 122 $tree;
40 }
41  
42 477 ▶sub new {
Subroutine new: called CC=3 SCAR=11.0
Pod documented
43 477 my ($class, $stuff, $options) = @_;
44  
45 477 ▶ my $self = $class->_resolve_new($stuff,$options)
Branch: 1 branch
BranchTrueFalseTotal
my $self = $class->_resolve_new($stuff, $options) or return undef 474 0 474
46 or return undef;
47  
48 474 ▶ $self->{indent} = $options->{indent} if $options->{indent};
Branch: 1 branch
BranchTrueFalseTotal
if $options->{"indent"} 1 473 474
49  
50 474 $self->{no_space_compacting} = $options->{no_space_compacting};
51  
52 474 return $self;
53 }
54  
55 477 ▶sub _resolve_new {
Subroutine _resolve_new: called CC=11 SCAR=24.6
56 477 my( $class, $stuff, $options) = @_;
57  
58 477 ▶ return $class->new_from_element([],undef,$options) unless defined $stuff;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $stuff 2 475 477
59  
60 475 ▶ if (blessed $stuff) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($stuff)) 268 207 475
61 268 ▶ return $class->new_from_element([$stuff],undef,$options)
Branch: 1 branch
BranchTrueFalseTotal
if $stuff->isa("HTML::Element") 149 119 268
62 if $stuff->isa('HTML::Element');
63  
64 119 ▶ return $class->new_from_url($stuff->as_string,$options)
Branch: 1 branch
BranchTrueFalseTotal
if $stuff->isa("URI") 2 117 119
65 if $stuff->isa('URI');
66  
67 117 ▶ return $class->new_from_element($stuff->{trees}, undef, $options)
Branch: 1 branch
BranchTrueFalseTotal
if $stuff->isa($class) 117 0 117
68 if $stuff->isa($class);
69  
70 0 die "Unknown source type: $stuff";
71 }
72  
73 207 ▶ return $class->new_from_element($stuff,undef,$options) if ref $stuff eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $stuff eq "ARRAY" 2 205 207
74  
75 205 ▶ return $class->new_from_url($stuff,$options) if $stuff =~ m{^(?:https?|file)://};
Branch: 1 branch
BranchTrueFalseTotal
if $stuff =~ m[^(?:https?|file)://] 6 199 205
76  
77 199 ▶ return $class->new_from_html($stuff,$options) if $stuff =~ /<.*?>/;
Branch: 1 branch
BranchTrueFalseTotal
if $stuff =~ /<.*?>/ 181 18 199
78  
79 18 ▶ return $class->new_from_file($stuff,$options) if $stuff !~ /\n/ && -f $stuff;
Branch: 1 branch
BranchTrueFalseTotal
if CORE::not $stuff =~ /\n/ and -f $stuff 18 0 18
Condition33%1/3
CORE::not $stuff =~ /\n/ and -f $stuff
!ll&&!rl&&r
0018
MC/DC0%0/2
CORE::not $stuff =~ /\n/ and -f $stuff
CORE::not $stuff =~ /\n/ -f $stuff
Truth table33%1/3
not(A) and B
A= CORE::not $stuff =~ /\n/B= -f $stuff
ABresult
0X0
100
111
80  
81 0 die "Unknown source type: $stuff";
82 }
83  
84 8 ▶sub new_from_url {
Subroutine new_from_url: called CC=2 SCAR=6.9
Pod documented
85 8 my ($class, $url,$options) = @_;
86  
87 8 $RESPONSE = __ua()->get($url);
88  
89 48 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
90  
91 8 ▶ unless( $RESPONSE->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($Web::Query::RESPONSE->is_success) 3 5 8
92 3 die "failed to retrieve '$url', " . $RESPONSE->code. " "
93 . $RESPONSE->message."\n";
94 };
95  
96 5 return $class->new_from_html($RESPONSE->decoded_content,$options);
97 }
98  
99 18 ▶sub new_from_file {
Subroutine new_from_file: called CC=1 SCAR=0.0
Pod documented
100 18 my ($class, $fname, $options) = @_;
101 18 my $tree = $class->_build_tree($options);
102 18 $tree->parse_file($fname);
103 18 my $self = $class->new_from_element([$tree->disembowel],undef,$options);
104 18 $self->{need_delete}++;
105 18 return $self;
106 }
107  
108 218 ▶sub new_from_html {
Subroutine new_from_html: called CC=2 SCAR=6.9
Pod documented
109 218 my ($class, $html,$options) = @_;
110 218 my $tree = $class->_build_tree($options);
111 218 $tree->parse_content($html);
112 my $self = $class->new_from_element([
113 map {
114 218 ▶ ref $_ ? $_ : bless { _content => $_ }, 'HTML::TreeBuilder::XPath::TextNode'
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 236 1 237
115 } $tree->disembowel
116 ],undef,$options);
117 218 $self->{need_delete}++;
118 218 return $self;
119 }
120  
121 1268 ▶sub new_from_element {
Subroutine new_from_element: called CC=3 SCAR=11.0
Pod documented
122 1268 my $self_or_class = shift;
123  
124 1268 ▶ my $trees = ref $_[0] eq 'ARRAY' ? $_[0] : [$_[0]];
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'ARRAY' ? : 1190 78 1268
125  
126 1268 ▶ return bless { trees => [ @$trees ], before => $_[1] },
Condition66%2/3
ref $self_or_class || $self_or_class
l!l&&r!l&&!r
7711910
MC/DC0%0/2
ref $self_or_class || $self_or_class
ref $self_or_class $self_or_class
Truth table66%2/3
A || B
A= ref $self_or_classB= $self_or_class
ABresult
1X1
011
000
127 ref $self_or_class || $self_or_class;
128 }
129  
130 108 ▶sub end {
Subroutine end: called CC=1 SCAR=0.0
Pod documented
131 108 my $self = shift;
132 108 return $self->{before};
133 }
134  
135 142 ▶sub size {
Subroutine size: called CC=1 SCAR=0.0
Pod documented
136 142 my $self = shift;
137 142 return scalar(@{$self->{trees}});
138 }
139  
140 4 ▶sub parent {
Subroutine parent: called CC=2 SCAR=7.2
Pod documented
141 4 my $self = shift;
142  
143 4 my @new = map { $_->parent } @{$self->{trees}};
144  
145 4 ▶ return (ref $self || $self)->new_from_element(\@new, $self);
Condition33%1/3
ref $self || $self
l!l&&r!l&&!r
400
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
146 }
147  
148 12 ▶sub first {
Subroutine first: called CC=1 SCAR=0.0
Pod documented
149 12 my $self = shift;
150 12 return $self->eq(0);
151 }
152  
153 6 ▶sub last {
Subroutine last: called CC=1 SCAR=0.0
Pod documented
154 6 my $self = shift;
155 6 return $self->eq(-1);
156 }
157  
158 89 ▶sub get {
Subroutine get: called CC=1 SCAR=0.0
Pod documented
159 89 my ($self, $index) = @_;
160 89 return $self->{trees}[$index];
161 }
162  
163 26 ▶sub eq {
Subroutine eq: called CC=3 SCAR=12.5
Pod undocumented
164 26 my ($self, $index) = @_;
165 26 ▶ return (ref $self || $self)->new_from_element([$self->{trees}[$index] || ()], $self);
Condition50%3/6
ref $self || $self
l!l&&r!l&&!r
2600
$self->{'trees'}[$index] || ()
l!l&&r!l&&!r
2240
MC/DC0%0/2
ref $self || $self
ref $self $self
MC/DC0%0/2
$self->{'trees'}[$index] || ()
$self->{'trees'}[$index] ()
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
Truth table66%2/3
A || B
A= $self->{'trees'}[$index]B= ()
ABresult
1X1
011
000
166 }
167  
168 295 ▶sub find {
Subroutine find: called CC=3 SCAR=11.1
Pod documented
169 295 my ($self, $selector) = @_;
170
171 295 ▶ my $xpath = ref $selector ? $$selector : selector_to_xpath($selector, root => './');
Branch: 1 branch
BranchTrueFalseTotal
ref $selector ? : 1 294 295
172 295 my @new = map { eval{ $_->findnodes($xpath) } } @{$self->{trees}};
173
174 295 ▶ return (ref $self || $self)->new_from_element(\@new, $self);
Condition33%1/3
ref $self || $self
l!l&&r!l&&!r
29500
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
175 }
176  
177 20 ▶sub contents {
Subroutine contents: called CC=4 SCAR=14.1
Pod documented
178 20 my ($self, $selector) = @_;
179  
180 20 my @new = map { $_->content_list } @{$self->{trees}};
181  
182 20 ▶ if ($selector) {
Branch: 1 branch
BranchTrueFalseTotal
if ($selector) 2 18 20
183 2 ▶ my $xpath = ref $selector ? $$selector : selector_to_xpath($selector);
Branch: 1 branch
BranchTrueFalseTotal
ref $selector ? : 0 2 2
184 2 @new = grep { $_->matches($xpath) } @new;
185 }
186  
187 20 ▶ return (ref $self || $self)->new_from_element(\@new, $self);
Condition33%1/3
ref $self || $self
l!l&&r!l&&!r
2000
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
188 }
189  
190 105 ▶sub as_html {
Subroutine as_html: called CC=6 SCAR=17.9
Pod documented
191 105 my $self = shift;
192 105 my %args = @_;
193  
194 my @html = map {
195 ref $_ ? ( $_->isa('HTML::TreeBuilder::XPath::TextNode') || $_->isa('HTML::TreeBuilder::XPath::CommentNode' ) )
196 ? $_->getValue
197 : $_->as_HTML( q{&<>'"}, $self->{indent}, {} )
198 147 ▶ : $_
Branch: 2 branches
BranchTrueFalseTotal
ref $_ ? : 145 2 147
$_->isa('HTML::TreeBuilder::XPath::TextNode') || $_->isa('HTML::TreeBuilder::XPath::CommentNode') ? : 1 144 145
Condition66%2/3
$_->isa("HTML::TreeBuilder::XPath::TextNode") or $_->isa("HTML::TreeBuilder::XPath::CommentNode")
l!l&&r!l&&!r
10144
MC/DC50%1/2
$_->isa("HTML::TreeBuilder::XPath::TextNode") or $_->isa("HTML::TreeBuilder::XPath::CommentNode")
$_->isa("HTML::TreeBuilder::XPath::TextNode") $_->isa("HTML::TreeBuilder::XPath::CommentNode")
Truth table66%2/3
A or B
A= $_->isa("HTML::TreeBuilder::XPath::TextNode")B= $_->isa("HTML::TreeBuilder::XPath::CommentNode")
ABresult
1X1
011
000
199 105 } @{$self->{trees}};
200  
201 105 ▶ return join $args{join}, @html if defined $args{join};
Branch: 1 branch
BranchTrueFalseTotal
if defined $args{"join"} 20 85 105
202  
203 85 ▶ return wantarray ? @html : $html[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 22 63 85
204 }
205  
206 48 ▶sub html {
Subroutine html: called CC=5 SCAR=16.1
Pod documented
207 48 my $self = shift;
208  
209 48 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 4 44 48
210 map {
211 4 $_->delete_content;
212 4 my $tree = $self->_build_tree;
213
214 4 $tree->parse_content($_[0]);
215 4 $_->push_content($tree->disembowel);
216 4 } @{$self->{trees}};
217 4 return $self;
218 }
219  
220 44 my @html;
221 44 for my $t ( @{$self->{trees}} ) {
222 push @html, join '', map {
223 ref $_ ? $_->as_HTML( q{&<>'"}, $self->{indent}, {})
224 84 ▶ : encode_entities($_)
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 71 13 84
225 44 } eval { $t->content_list };
226 }
227
228 44 ▶ return wantarray ? @html : $html[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 18 26 44
229 }
230  
231 88 ▶sub text {
Subroutine text: called CC=4 SCAR=13.9
Pod documented
232 88 my $self = shift;
233  
234 88 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 2 86 88
235 2 map { $_->delete_content; $_->push_content($_[0]) } @{$self->{trees}};
236 2 return $self;
237 }
238  
239 my @html = map {
240 86 ▶ ref $_ ? $_->as_text : $_
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 84 2 86
241 86 } @{$self->{trees}};
242 86 ▶ return wantarray ? @html : $html[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 6 80 86
243 }
244  
245 100 ▶sub attr {
Subroutine attr: called CC=8 SCAR=20.8
Pod documented
246 100 my $self = shift;
247  
248 100 ▶ if ( @_ == 1 ) { # getter
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1) 66 34 100
249 return wantarray
250 16 ? map { $_->attr(@_) } @{$self->{trees}}
251 66 ▶ : eval { $self->{trees}[0]->attr(@_) }
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 10 56 66
252 ;
253 }
254  
255 34 ▶ while( my( $attr, $value ) = splice @_, 0, 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@_, 0, 2)) 36 34 70
256 36 ▶ my $code = ref $value eq 'CODE' ? $value : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $value eq 'CODE' ? : 2 34 36
257  
258 36 for my $t ( @{$self->{trees}} ) {
259 42 ▶ if ( $code ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code) 4 38 42
260 48 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
261 4 my $orig = $_ = $t->attr($attr);
262 4 $code->();
263 4 ▶ next if $orig eq $_;
Branch: 1 branch
BranchTrueFalseTotal
if $orig eq $_ 2 2 4
264 2 $value = $_;
265 }
266 40 $t->attr($attr => $value);
267 }
268 }
269  
270 34 return $self;
271 }
272  
273 20 ▶sub id {
Subroutine id: called CC=6 SCAR=17.9
Pod documented
274 20 my $self = shift;
275  
276 20 ▶ if ( @_ ) { # setter
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 8 12 20
277 8 my $new_id = shift;
278  
279 8 ▶ return $self if $self->size == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $self->size == 0 0 8 8
280  
281 6 ▶ return $self->each(sub{
Subroutine __ANON__: called CC=1 SCAR=0.0
282 6 $_->attr( id => $new_id->(@_) )
283 8 ▶ }) if ref $new_id eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $new_id eq "CODE" 2 6 8
284  
285 6 ▶ if ( $self->size == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->size == 1) { } 2 4 6
286 2 $self->attr( id => $new_id );
287 }
288 else {
289 4 return $self->first->attr( id => $new_id );
290 }
291 }
292 else { # getter
293  
294 # the eval is there in case there is no tree
295 return wantarray
296 8 ? map { $_->attr('id') } @{$self->{trees}}
297 12 ▶ : eval { $self->{trees}[0]->attr('id') }
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 4 8 12
298 ;
299 }
300 }
301  
302 12 ▶sub name {
Subroutine name: called CC=1 SCAR=0.0
Pod documented
303 12 my $self = shift;
304 12 $self->attr( 'name', @_ );
305 }
306  
307 4 ▶sub data {
Subroutine data: called CC=1 SCAR=0.0
Pod documented
308 4 my $self = shift;
309 4 my $name = shift;
310 4 $self->attr( join( '-', 'data', $name ), @_ );
311 }
312  
313 12 ▶sub tagname {
Subroutine tagname: called CC=6 SCAR=18.0
Pod documented
314 12 my $self = shift;
315 12 ▶ my @retval = map { $_ eq '~comment' ? '#comment' : $_ }
Branch: 1 branch
BranchTrueFalseTotal
$_ eq '~comment' ? : 2 10 12
316 12 ▶ map { ref $_ eq 'HTML::TreeBuilder::XPath::TextNode' ? '#text'
Branch: 3 branches
BranchTrueFalseTotal
ref $_ eq 'HTML::TreeBuilder::XPath::TextNode' ? : 1 11 12
ref $_ eq 'HTML::TreeBuilder::XPath::CommentNode' ? : 0 11 11
ref $_ ? : 9 2 11
317 : ref $_ eq 'HTML::TreeBuilder::XPath::CommentNode' ? '#comment'
318 : ref $_ ? $_->tag(@_)
319 : '#text'
320 ;
321 12 } @{$self->{trees}};
322 12 ▶ return wantarray ? @retval : $retval[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 12 12
323 }
324  
325 51 ▶sub each {
Subroutine each: called CC=3 SCAR=11.2
Pod documented
326 51 my ($self, $code) = @_;
327 51 my $i = 0;
328  
329 # make a copy such that if we modify the list via 'delete',
330 # it won't change from under our feet (see t/each-and-delete.t
331 # for a case where it can)
332 51 my @trees = @{ $self->{trees} };
333 51 for my $tree ( @trees ) {
334 140 ▶ local $_ = (ref $self || $self)->new_from_element([$tree], $self);
Condition33%1/3
ref $self || $self
l!l&&r!l&&!r
14000
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
335 140 $code->($i++, $_);
336 }
337 51 return $self;
338 }
339  
340 87 ▶sub map {
Subroutine map: called CC=2 SCAR=7.1
Pod documented
341 87 my ($self, $code) = @_;
342 87 my $i = 0;
343 return +[map {
344 125 my $tree = $_;
345 125 ▶ local $_ = (ref $self || $self)->new($tree);
Condition33%1/3
ref $self || $self
l!l&&r!l&&!r
12500
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
346 125 $code->($i++, $_);
347 87 } @{$self->{trees}}];
348 }
349  
350 64 ▶sub filter {
Subroutine filter: called CC=5 SCAR=16.6
Pod documented
351 64 my $self = shift;
352  
353 64 my @new;
354  
355 64 ▶ if (ref($_[0]) eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] eq 'CODE') { } 48 16 64
356 48 my $code = $_[0];
357 48 my $i = 0;
358 @new = grep {
359 78 my $tree = $_;
360 78 ▶ local $_ = (ref $self || $self)->new_from_element($tree);
Condition33%1/3
ref $self || $self
l!l&&r!l&&!r
7800
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
361 78 $code->($i++, $_);
362 48 } @{$self->{trees}};
363 }
364 else {
365 0 ▶ my $xpath = ref $_[0] ? ${$_[0]} : selector_to_xpath($_[0]);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 16 16
366 16 @new = grep { eval { $_->matches($xpath) } } @{$self->{trees}};
367 }
368  
369 64 ▶ return (ref $self || $self)->new_from_element(\@new, $self);
Condition33%1/3
ref $self || $self
l!l&&r!l&&!r
6400
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
370 }
371  
372 22 ▶sub _is_same_node {
Subroutine _is_same_node: called CC=1 SCAR=0.0
373 22 refaddr($_[1]) == refaddr($_[2]);
374 }
375  
376 30 ▶sub remove {
Subroutine remove: called CC=3 SCAR=11.0
Pod documented
377 30 my $self = shift;
378 30 my $before = $self->end;
379
380 30 ▶ while (defined $before) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $before) 28 30 58
381 28 @{$before->{trees}} = grep {
382 40 my $el = $_;
383 40 not grep { $self->_is_same_node( $el, $_ ) } @{$self->{trees}};
384 28 } @{$before->{trees}};
385  
386 28 $before = $before->end;
387 }
388
389 30 $_->delete for @{$self->{trees}};
390 30 @{$self->{trees}} = ();
391
392 30 $self;
393 }
394  
395 10 ▶sub replace_with {
Subroutine replace_with: called CC=11 SCAR=24.5
Pod documented
396 10 my ( $self, $replacement ) = @_;
397  
398 10 my $i = 0;
399 10 for my $node ( @{ $self->{trees} } ) {
400 18 my $rep = $replacement;
401  
402 18 ▶ if ( ref $rep eq 'CODE' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $rep eq "CODE") 8 10 18
403 8 ▶ local $_ = (ref $self || $self)->new($node);
Condition33%1/3
ref $self || $self
l!l&&r!l&&!r
800
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
404 8 $rep = $rep->( $i++ => $_ );
405 }
406  
407 18 ▶ $rep = (ref $self || $self)->new_from_html( $rep )
Branch: 1 branch
BranchTrueFalseTotal
unless ref $rep 14 4 18
Condition33%1/3
ref $self || $self
l!l&&r!l&&!r
1400
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
408 unless ref $rep;
409  
410  
411
412 18 my $r = $rep->{trees}->[0];
413 48 ▶ { no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
414 18 ▶ $r = $r->clone if ref $r;
Branch: 1 branch
BranchTrueFalseTotal
if ref $r 17 1 18
415 }
416 18 ▶ $r->parent( $node->parent ) if ref $r and $node->parent;
Branch: 1 branch
BranchTrueFalseTotal
if ref $r and $node->parent 17 1 18
Condition66%2/3
ref $r and $node->parent
!ll&&!rl&&r
1017
MC/DC50%1/2
ref $r and $node->parent
ref $r $node->parent
Truth table66%2/3
A and B
A= ref $rB= $node->parent
ABresult
0X0
100
111
417  
418 18 $node->replace_with( $r );
419 }
420  
421 10 ▶ $replacement->remove if ref $replacement eq (ref $self || $self);
Branch: 1 branch
BranchTrueFalseTotal
if ref $replacement eq (ref $self || $self) 2 8 10
Condition33%1/3
ref $self || $self
l!l&&r!l&&!r
1000
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
422  
423 10 return $self;
424 }
425  
426 2 ▶sub append {
Subroutine append: called CC=4 SCAR=14.2
Pod documented
427 2 my ($self, $stuff) = @_;
428 2 ▶ $stuff = (ref $self || $self)->new($stuff);
Condition33%1/3
ref $self || $self
l!l&&r!l&&!r
200
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
429  
430 2 foreach my $t (@{$self->{trees}}) {
431 4 $t->push_content($_) for ref($t)->clone_list(@{$stuff->{trees}});
432 }
433  
434 2 $self;
435 }
436  
437 2 ▶sub prepend {
Subroutine prepend: called CC=4 SCAR=14.2
Pod documented
438 2 my ($self, $stuff) = @_;
439 2 ▶ $stuff = (ref $self || $self)->new($stuff);
Condition33%1/3
ref $self || $self
l!l&&r!l&&!r
200
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
440
441 2 foreach my $t (@{$self->{trees}}) {
442 4 $t->unshift_content($_) for ref($t)->clone_list(@{$stuff->{trees}});
443 }
444
445 2 $self;
446 }
447  
448  
449 2 ▶sub before {
Subroutine before: called CC=3 SCAR=11.2
Pod documented
450 2 my ($self, $stuff) = @_;
451 2 ▶ $stuff = (ref $self || $self)->new($stuff);
Condition33%1/3
ref $self || $self
l!l&&r!l&&!r
200
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
452
453 2 foreach my $t (@{$self->{trees}}) {
454 4 $t->preinsert(ref($t)->clone_list(@{$stuff->{trees}}));
455 }
456
457 2 $self;
458 }
459  
460  
461 2 ▶sub after {
Subroutine after: called CC=3 SCAR=11.2
Pod documented
462 2 my ($self, $stuff) = @_;
463 2 ▶ $stuff = (ref $self || $self)->new($stuff);
Condition33%1/3
ref $self || $self
l!l&&r!l&&!r
200
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
464
465 2 foreach my $t (@{$self->{trees}}) {
466 4 $t->postinsert(ref($t)->clone_list(@{$stuff->{trees}}));
467 }
468
469 2 $self;
470 }
471  
472  
473 2 ▶sub insert_before {
Subroutine insert_before: called CC=2 SCAR=6.9
Pod documented
474 2 my ($self, $target) = @_;
475
476 2 foreach my $t (@{$target->{trees}}) {
477 4 $t->preinsert(ref($t)->clone_list(@{$self->{trees}}));
478 }
479
480 2 $self;
481 }
482  
483 2 ▶sub insert_after {
Subroutine insert_after: called CC=2 SCAR=6.9
Pod documented
484 2 my ($self, $target) = @_;
485
486 2 foreach my $t (@{$target->{trees}}) {
487 4 $t->postinsert(ref($t)->clone_list(@{$self->{trees}}));
488 }
489
490 2 $self;
491 }
492  
493 2 ▶sub detach {
Subroutine detach: called CC=2 SCAR=6.9
Pod documented
494 2 my ($self) = @_;
495 2 $_->detach for @{$self->{trees}};
496 2 $self;
497 }
498  
499 16 ▶sub add_class {
Subroutine add_class: called CC=6 SCAR=17.9
Pod documented
500 16 my ($self, $class) = @_;
501
502 16 ▶ for (my $i = 0; $i < @{$self->{trees}}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$self->{"trees"};}) 20 16 36
503 20 my $t = $self->{trees}->[$i];
504 20 ▶ my $current_class = $t->attr('class') || '';
Condition100%2/2
$t->attr('class') || ''
l!l
146
MC/DC100%1/1
$t->attr('class') || ''
$t->attr('class')
Truth table100%2/2
A
A= $t->attr('class')
Aresult
11
00
505
506 20 ▶ my $classes = ref $class eq 'CODE' ? $class->($i, $current_class, $t) : $class;
Branch: 1 branch
BranchTrueFalseTotal
ref $class eq 'CODE' ? : 4 16 20
507 20 my @classes = split /\s+/, $classes;
508
509 20 foreach (@classes) {
510 32 ▶ $current_class .= " $_" unless $current_class =~ /(?:^|\s)$_(?:\s|$)/;
Branch: 1 branch
BranchTrueFalseTotal
unless $current_class =~ /(?:^|\s)$_(?:\s|$)/ 28 4 32
511 }
512
513 20 $current_class =~ s/(?:^\s*|\s*$)//g;
514 20 $current_class =~ s/\s\s+/ /g;
515
516 20 $t->attr('class', $current_class);
517 }
518
519 16 $self;
520 }
521  
522  
523 16 ▶sub remove_class {
Subroutine remove_class: called CC=4 SCAR=13.9
Pod documented
524 16 my ($self, $class) = @_;
525  
526 16 ▶ for (my $i = 0; $i < @{$self->{trees}}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$self->{"trees"};}) 20 16 36
527 20 my $t = $self->{trees}->[$i];
528 20 my $current_class = $t->attr('class');
529 20 ▶ next unless defined $current_class;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $current_class 0 20 20
530
531 20 ▶ my $classes = ref $class eq 'CODE' ? $class->($i, $current_class, $t) : $class;
Branch: 1 branch
BranchTrueFalseTotal
ref $class eq 'CODE' ? : 4 16 20
532 20 my @remove_classes = split /\s+/, $classes;
533 my @final = grep {
534 20 my $existing_class = $_;
535 42 not grep { $existing_class eq $_} @remove_classes;
536 } split /\s+/, $current_class;
537
538 20 $t->attr('class', join ' ', @final);
539 }
540
541 16 $self;
542
543 }
544  
545 6 ▶sub toggle_class {
Subroutine toggle_class: called CC=1 SCAR=0.0
Pod documented
546 6 my $self = shift;
547
548 6 my @classes = uniq @_;
549  
550 18 ▶ $self->each(sub{
Subroutine __ANON__: called CC=3 SCAR=11.0
551 18 for my $class ( @classes ) {
552 24 ▶ my $method = $_->has_class($class) ? 'remove_class' : 'add_class';
Branch: 1 branch
BranchTrueFalseTotal
$_->has_class($class) ? : 12 12 24
553 24 $_->$method($class);
554 }
555 6 });
556 }
557  
558 56 ▶sub has_class {
Subroutine has_class: called CC=3 SCAR=11.0
Pod documented
559 56 my ($self, $class) = @_;
560
561 56 foreach my $t (@{$self->{trees}}) {
562 48 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
563 58 ▶ return 1 if $t->attr('class') =~ /(?:^|\s)$class(?:\s|$)/;
Branch: 1 branch
BranchTrueFalseTotal
if $t->attr("class") =~ /(?:^|\s)$class(?:\s|$)/ 28 30 58
564 }
565
566 28 return undef;
567 }
568  
569 2 ▶sub clone {
Subroutine clone: called CC=2 SCAR=7.2
Pod documented
570 2 my ($self) = @_;
571 2 my @clones = map { $_->clone } @{$self->{trees}};
572 2 ▶ return (ref $self || $self)->new_from_element(\@clones);
Condition33%1/3
ref $self || $self
l!l&&r!l&&!r
200
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
573 }
574  
575 77 ▶sub add {
Subroutine add: called CC=7 SCAR=19.7
Pod documented
576 77 my ($self, @stuff) = @_;
577 77 my @nodes;
578
579 # add(selector, context)
580 77 ▶ if (@stuff == 2 && !ref $stuff[0] && $stuff[1]->isa('HTML::Element')) {
Branch: 1 branch
BranchTrueFalseTotal
if (@stuff == 2 and CORE::not ref $stuff[0] and $stuff[1]->isa('HTML::Element')) { } 2 75 77
Condition83%5/6
@stuff == 2 and CORE::not ref $stuff[0] and $stuff[1]->isa("HTML::Element")
!ll&&!rl&&r
7502
@stuff == 2 and CORE::not ref $stuff[0]
!ll&&!rl&&r
7322
MC/DC66%2/3
@stuff == 2 and CORE::not ref $stuff[0] and $stuff[1]->isa("HTML::Element")
@stuff == 2 CORE::not ref $stuff[0] $stuff[1]->isa("HTML::Element")
Truth table75%3/4
A and not(B) and C
A= @stuff == 2B= CORE::not ref $stuff[0]C= $stuff[1]->isa("HTML::Element")
ABCresult
0XX0
10X0
1100
1111
581 0 ▶ my $xpath = ref $stuff[0] ? ${$stuff[0]} : selector_to_xpath($stuff[0]);
Branch: 1 branch
BranchTrueFalseTotal
ref $stuff[0] ? : 0 2 2
582 2 push @nodes, $stuff[1]->findnodes( $xpath, root => './');
583 }
584 else {
585 # handle any combination of html string, element object and web::query object
586 push @nodes, map {
587 77 ▶ $self->{need_delete} = 1 if $_->{need_delete};
Branch: 1 branch
BranchTrueFalseTotal
if $_->{"need_delete"} 4 73 77
588 77 delete $_->{need_delete};
589 77 @{$_->{trees}};
590 75 ▶ } map { (ref $self || $self)->new($_) } @stuff;
Condition33%1/3
ref $self || $self
l!l&&r!l&&!r
7700
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
591 }
592  
593 77 my %ids = map { $self->_node_id($_) => 1 } @{ $self->{trees} };
594  
595 $self->new_from_element( [
596 77 @{$self->{trees}}, grep { ! $ids{ $self->_node_id($_) } } @nodes
597 ], $self );
598 }
599  
600 76 ▶sub _node_id {
Subroutine _node_id: called CC=1 SCAR=0.0
601 76 my( undef, $node ) = @_;
602 76 refaddr $node;
603 }
604  
605 1 ▶sub prev {
Subroutine prev: called CC=3 SCAR=11.3
Pod documented
606 1 my $self = shift;
607 1 my @new;
608 1 for my $tree (@{$self->{trees}}) {
609 2 push @new, $tree->getPreviousSibling;
610 }
611 1 ▶ return (ref $self || $self)->new_from_element(\@new, $self);
Condition33%1/3
ref $self || $self
l!l&&r!l&&!r
100
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
612 }
613  
614 22 ▶sub next {
Subroutine next: called CC=2 SCAR=7.1
Pod documented
615 22 my $self = shift;
616  
617 22 my @new = grep { $_ } map { $_->getNextSibling } @{ $self->{trees} };
618  
619 22 ▶ return (ref $self || $self)->new_from_element(\@new, $self);
Condition33%1/3
ref $self || $self
l!l&&r!l&&!r
2200
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
620 }
621  
622 73 ▶sub match {
Subroutine match: called CC=3 SCAR=11.1
Pod documented
623 73 my( $self, $selector ) = @_;
624  
625 73 ▶ my $xpath = ref $selector ? $$selector : selector_to_xpath($selector);
Branch: 1 branch
BranchTrueFalseTotal
ref $selector ? : 0 73 73
626  
627 81 ▶ my $results = $self->map(sub{
Subroutine __ANON__: called CC=2 SCAR=7.1
628 81 my(undef,$e) = @_;
629 81 ▶ return 0 unless ref $e; # it's a string
Branch: 1 branch
BranchTrueFalseTotal
unless ref $e 0 81 81
630 81 return !!$e->get(0)->matches($xpath);
631 73 });
632  
633 73 ▶ return wantarray ? @$results : $results->[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 2 71 73
634 }
635  
636 38 ▶sub not {
Subroutine not: called CC=2 SCAR=7.0
Pod documented
637 38 my( $self, $selector ) = @_;
638  
639 38 my $class = ref $self;
640  
641 38 ▶ my $xpath = ref $selector ? $$selector : selector_to_xpath($selector);
Branch: 1 branch
BranchTrueFalseTotal
ref $selector ? : 0 38 38
642 38 ▶ $self->filter(sub { ! grep { $_->matches($xpath) } grep { ref $_ } $class->new($_)->{trees}[0] } );
Subroutine __ANON__: called CC=1 SCAR=0.0
643 }
644  
645 2 ▶sub and_back {
Subroutine and_back: called CC=1 SCAR=0.0
Pod documented
646 2 my $self = shift;
647
648 2 $self->add( $self->end );
649 }
650  
651 10 ▶sub next_until {
Subroutine next_until: called CC=2 SCAR=6.9
Pod documented
652 10 my( $self, $selector ) = @_;
653  
654 10 my $class = ref $self;
655 10 my $collection = $class->new_from_element([],$self);
656  
657 10 my $next = $self->next->not($selector);
658 10 ▶ while( $next->size ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($next->size) 26 10 36
659 26 $collection = $collection->add($next);
660 26 $next = $next->next->not( $selector );
661 }
662  
663 # hide the loop from the outside world
664 10 $collection->{before} = $self;
665  
666 10 return $collection;
667 }
668  
669 6 ▶sub split {
Subroutine split: called CC=3 SCAR=11.0
Pod undocumented
670 6 my( $self, $selector, %args ) = @_;
671  
672 6 my @current;
673 my @list;
674  
675 69 ▶ $self->contents->each(sub{
Subroutine __ANON__: called CC=3 SCAR=11.0
676 69 my(undef,$e)=@_;
677  
678 69 ▶ if( $e->match($selector) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($e->match($selector)) { } 18 51 69
679 18 push @list, [ @current ];
680 18 @current = ( $e );
681 }
682 else {
683 51 ▶ if ( $current[1] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($current[1]) { } 30 21 51
684 30 $current[1] = $current[1]->add($e);
685 }
686 else {
687 21 $current[1] = $e;
688 }
689 }
690 6 });
691 6 push @list, [ @current ];
692  
693 6 ▶ if( $args{skip_leading} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"skip_leading"}) 2 4 6
694 2 @list = grep { $_->[0] } @list;
695 }
696  
697 6 ▶ unless ( $args{pairs} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($args{"pairs"}) 2 4 6
698 2 ▶ @list = map { reduce { $a->add($b) } grep { $_ } @$_ } @list;
Subroutine __ANON__: called CC=1 SCAR=0.0
699 }
700  
701 6 return @list;
702 }
703  
704 4 ▶sub last_response {
Subroutine last_response: called CC=1 SCAR=0.0
Pod documented
705 4 return $RESPONSE;
706 }
707  
708 1269 ▶sub DESTROY {
Subroutine DESTROY: called CC=3 SCAR=11.0
709 1269 ▶ return unless $_[0]->{need_delete};
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0]{"need_delete"} 1032 237 1269
710  
711 # avoid memory leaks
712 237 local $@;
713 237 eval { $_->delete } for @{$_[0]->{trees}};
714 }
715  
716 48 1;
717  
718 __END__
↑ Summary Query/LibXML.pm »