Query.pm

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