NG/HTTP/DOM.pm

filter: Statementstmt 23.1% 19 / 82 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 27 MC/DCmcdc 0.0% 0 / 18 Subroutinesub 22.2% 6 / 27 Podpod 46.1% 6 / 13 totaltotal 17.7% 31 / 175 CC 15 SCAR 19.6
CC
15
Cov
16%
__ANON__
20.0
new_from_element
12.0
first
12.0
CRAP 7.1
« NG/HTTP/Client.pm ↑ Summary NG/Array.pm »
1 package NG::HTTP::DOM;
2 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use base 'NG::Object';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use NG::Array;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use HTML::TreeBuilder::XPath;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use HTML::Selector::XPath qw(selector_to_xpath);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 =head1 DESCRIPTION
10 A mini implement of Web::Query, but all method return Array object, no matter wantarray or no.
11 I only want something work with web_get, so modify-relate methods all deleted.
12 =cut
13  
14 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
Pod documented
15 0 my ( $pkg, $html ) = @_;
16 0 my $tree = HTML::TreeBuilder::XPath->new;
17 0 $tree->ignore_unknown(0);
18 0 $tree->store_comments(1);
19 0 $tree->parse_content($html);
20 0 my $self = $pkg->new_from_element( NG::Array->new( $tree->guts ) );
21 0 $self->{need_delete}++;
22 0 return $self;
23 }
24  
25 0 ▶sub new_from_element {
Subroutine new_from_element: not called CC=3 SCAR=24.8
Pod undocumented
26 0 my $class = shift;
27 0 ▶ my $trees = ref $_[0] eq 'NG::Array' ? $_[0] : NG::Array->new( $_[0] );
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'NG::Array' ? : 0 0 0
28 0 ▶ return bless { trees => $trees, before => $_[1] }, ref($class) || $class;
Condition0%0/3
ref $class || $class
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table0%0/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
29 }
30  
31 0 ▶sub end {
Subroutine end: not called CC=1 SCAR=6.9
Pod undocumented
32 0 my $self = shift;
33 0 return $self->{before};
34 }
35  
36 0 ▶sub size {
Subroutine size: not called CC=1 SCAR=6.9
Pod undocumented
37 0 my $self = shift;
38 0 return $self->{trees}->size;
39 }
40  
41 0 ▶sub parent {
Subroutine parent: not called CC=2 SCAR=17.9
Pod undocumented
42 0 my $self = shift;
43 0 my $new = NG::Array->new;
44 $self->{trees}->each(
45 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
46 0 $new->push( shift->getParentNode() );
47 }
48 0 );
49 0 ▶ return ( ref $self || $self )->new_from_element( $new, $self );
Condition0%0/3
ref $self || $self
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table0%0/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
50 }
51  
52 0 ▶sub first {
Subroutine first: not called CC=3 SCAR=24.8
Pod undocumented
53 0 my $self = shift;
54 return ( ref $self || $self )
55 0 ▶ ->new_from_element( NG::Array->new( $self->{trees}->get(0) || () ), $self );
Condition0%0/6
ref $self || $self
l!l&&r!l&&!r
000
$self->{'trees'}->get(0) || ()
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $self || $self
ref $self $self
MC/DC0%0/2
$self->{'trees'}->get(0) || ()
$self->{'trees'}->get(0) ()
Truth table0%0/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
Truth table0%0/3
A || B
A= $self->{'trees'}->get(0)B= ()
ABresult
1X1
011
000
56 }
57  
58 0 ▶sub last {
Subroutine last: not called CC=3 SCAR=24.8
Pod undocumented
59 0 my $self = shift;
60 return ( ref $self || $self )
61 0 ▶ ->new_from_element( NG::Array->new( $self->{trees}->get(-1) || () ), $self );
Condition0%0/6
ref $self || $self
l!l&&r!l&&!r
000
$self->{'trees'}->get(-1) || ()
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $self || $self
ref $self $self
MC/DC0%0/2
$self->{'trees'}->get(-1) || ()
$self->{'trees'}->get(-1) ()
Truth table0%0/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
Truth table0%0/3
A || B
A= $self->{'trees'}->get(-1)B= ()
ABresult
1X1
011
000
62 }
63  
64 =head2 find
65 my $dom = HTTP::DOM->new($content);
66 print $dom->find('#m')->text;
67 =cut
68 0 ▶sub find {
Subroutine find: not called CC=2 SCAR=17.9
Pod documented
69 0 my ( $self, $selector ) = @_;
70 0 my $xpath_rootless = selector_to_xpath($selector);
71  
72 0 my $new = NG::Array->new;
73 $self->{trees}->each(
74 0 ▶ sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
75 0 my $tree = shift;
76 0 ▶ $new->push($tree)
Branch: 1 branch
BranchTrueFalseTotal
if defined $tree->parent and $tree->matches($xpath_rootless) 0 0 0
Condition0%0/3
defined $tree->parent and $tree->matches($xpath_rootless)
!ll&&!rl&&r
000
MC/DC0%0/2
defined $tree->parent and $tree->matches($xpath_rootless)
defined $tree->parent $tree->matches($xpath_rootless)
Truth table0%0/3
A and B
A= defined $tree->parentB= $tree->matches($xpath_rootless)
ABresult
0X0
100
111
77 if defined $tree->parent && $tree->matches($xpath_rootless);
78 0 ▶ $new->push(
Branch: 1 branch
BranchTrueFalseTotal
defined $tree->parent ? : 0 0 0
79 $tree->findnodes(
80 selector_to_xpath(
81 $selector, root => defined $tree->parent ? './' : '/'
82 )
83 )
84 );
85 }
86 0 );
87  
88 0 ▶ return ( ref $self || $self )->new_from_element( $new, $self );
Condition0%0/3
ref $self || $self
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table0%0/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
89 }
90  
91 =head2 each
92 $dom->find('#m')->each(sub {
93 my $i = shift;
94 printf "(%d) %s\n", $i+1, $_->text;
95 })
96 =cut
97 0 ▶sub each {
Subroutine each: not called CC=1 SCAR=6.9
Pod documented
98 0 my ($self, $code) = @_;
99 0 my $i = 0;
100 $self->{trees}->each(
101 0 ▶ sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
102 0 my $tree = shift;
103 0 ▶ local $_ = (ref $self || $self)->new_from_element(NG::Array->new($tree), $self);
Condition0%0/3
ref $self || $self
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table0%0/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
104 0 $code->($i++, $_);
105 }
106 0 );
107 0 return $self;
108 }
109  
110 =head2 html
111 =cut
112 0 ▶sub html {
Subroutine html: not called CC=1 SCAR=6.9
Pod documented
113 0 my $self = shift;
114 0 my $html = NG::Array->new;
115 $self->{trees}->each(
116 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
117 0 $html->push( shift->as_HTML );
118 }
119 0 );
120 0 return $html->join("\n");
121 }
122  
123 0 ▶sub xml {
Subroutine xml: not called CC=1 SCAR=6.9
Pod undocumented
124 0 my $self = shift;
125 0 my $html = NG::Array->new;
126 $self->{trees}->each(
127 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
128 0 $html->push( shift->as_XML_indented );
129 }
130 0 );
131 0 return $html->join("\n");
132 }
133  
134 =head2 text
135 my $dom = HTTP::DOM->new($content);
136 print $dom->text;
137 =cut
138 0 ▶sub text {
Subroutine text: not called CC=1 SCAR=6.9
Pod documented
139 0 my $self = shift;
140 0 my $text = NG::Array->new;
141 $self->{trees}->each(
142 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
143 0 $text->push( shift->as_text );
144 }
145 0 );
146 0 return $text->join("\n");
147 }
148  
149 =head2 attr
150 my $url = 'http://www.baidu.com/';
151 my $hc = HTTP::Client->new;
152 my $content = $hc->web_get($url);
153 my $dom = HTTP::DOM->new($content);
154 print $dom->find('meta')->attr('content');
155 =cut
156 0 ▶sub attr {
Subroutine attr: not called CC=1 SCAR=6.9
Pod documented
157 0 my $self = shift;
158 0 my @attr_keys = @_;
159 0 my $retval = NG::Array->new;
160 $self->{trees}->each(
161 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
162 0 $retval->push( shift->attr(@attr_keys) );
163 }
164 0 );
165 0 return $retval->join("\n");
166 }
167  
168 0 ▶sub DESTROY {
Subroutine DESTROY: not called CC=3 SCAR=24.8
169 0 ▶ if ( $_[0]->{need_delete} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0]{"need_delete"}) 0 0 0
170 0 $_->delete for @{ $_[0]->{trees} };
171 }
172 }
173  
174 1 1;
« NG/HTTP/Client.pm ↑ Summary NG/Array.pm »