blib/lib/HTML/TreeBuilder/XPath.pm

filter: Statementstmt 49.0% 176 / 359 Branchbran 26.6% 33 / 124 Conditioncond 18.6% 14 / 75 MC/DCmcdc 9.3% 4 / 43 Subroutinesub 59.8% 61 / 102 Podpod 0.0% 0 / 32 totaltotal 39.1% 288 / 735 CC 95 SCAR 41.8
CC
95
Cov
40%
as_XML_indented
420.0
as_XML_compact
156.0
cmp
83.5
CRAP 65.6
↑ Summary
1 package HTML::TreeBuilder::XPath;
2  
3 4 ▶use List::Util qw( first);
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 4 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 4 $VERSION = '0.14';
12  
13 4 my %CHAR2DEFAULT_ENT= ( '&' => '&amp;', '<' => '&lt;', '>' => '&gt;', '"' => '&quot;');
14 4 my %NUM2DEFAULT_ENT= ( '38' => 'amp', '60' => 'lt', '62' => 'gt', '"' => '&quot;');
15  
16 package HTML::TreeBuilder::XPath;
17  
18 4 ▶use base( 'HTML::TreeBuilder');
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 package HTML::TreeBuilder::XPath::Node;
21  
22 938 ▶sub isElementNode { 0 }
Subroutine isElementNode: called CC=1 SCAR=0.0
23 2 ▶sub isAttributeNode { 0 }
Subroutine isAttributeNode: called CC=1 SCAR=0.0
24 0 ▶sub isNamespaceNode { 0 }
Subroutine isNamespaceNode: not called CC=1 SCAR=6.9
25 0 ▶sub isTextNode { 0 }
Subroutine isTextNode: not called CC=1 SCAR=6.9
26 0 ▶sub isProcessingInstructionNode { 0 }
Subroutine isProcessingInstructionNode: not called CC=1 SCAR=6.9
27 0 ▶sub isPINode { 0 }
Subroutine isPINode: not called CC=1 SCAR=6.9
28 0 ▶sub isCommentNode { 0 }
Subroutine isCommentNode: not called CC=1 SCAR=6.9
29  
30 1322 ▶sub getChildNodes { return wantarray ? () : []; }
Subroutine getChildNodes: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 827 495 1322
31 80 ▶sub getFirstChild { return undef; }
Subroutine getFirstChild: called CC=1 SCAR=0.0
32 9 ▶sub getLastChild { return undef; }
Subroutine getLastChild: called CC=1 SCAR=0.0
33  
34 # need to do a complete look_down each time, as the id could have been changed
35 # without any object being involved, hence without a potential cache being
36 # up to date
37 sub getElementById
38 2 ▶ { my ($self, $id) = @_;
Subroutine getElementById: called CC=1 SCAR=0.0
39 2 return scalar $self->look_down( id => $id);
40 }
41  
42 0 ▶sub to_number { return XML::XPathEngine::Number->new( shift->getValue); }
Subroutine to_number: not called CC=1 SCAR=6.9
43  
44 sub cmp
45 52 ▶ { my( $a, $b)=@_;
Subroutine cmp: called CC=17 SCAR=44.3
46  
47 # comparison with the root (in $b, or processed in HTML::TreeBuilder::XPath::Root)
48 0 ▶ if( $b->isa( 'HTML::TreeBuilder::XPath::Root') ) { return -1; }
Branch: 1 branch
BranchTrueFalseTotal
if ($b->isa("HTML::TreeBuilder::XPath::Root")) 0 52 52
49  
50 # easy cases
51 52 ▶ return 0 if( $a == $b);
Branch: 1 branch
BranchTrueFalseTotal
if $a == $b 0 52 52
52 52 ▶ return 1 if( $a->is_inside($b)); # a starts after b
Branch: 1 branch
BranchTrueFalseTotal
if $a->is_inside($b) 0 52 52
53 52 ▶ return -1 if( $b->is_inside($a)); # a starts before b
Branch: 1 branch
BranchTrueFalseTotal
if $b->is_inside($a) 0 52 52
54  
55 # lineage does not include the element itself
56 52 my @a_pile= ($a, $a->lineage);
57 52 my @b_pile= ($b, $b->lineage);
58
59 # the 2 elements are not in the same twig
60 52 ▶ unless( $a_pile[-1] == $b_pile[-1])
Branch: 1 branch
BranchTrueFalseTotal
unless ($a_pile[-1] == $b_pile[-1]) 0 52 52
61 0 { warn "2 nodes not in the same pile: ", ref( $a), " - ", ref( $b), "\n";
62 0 print "a: ", $a->string_value, "\nb: ", $b->string_value, "\n";
63 0 return undef;
64 }
65  
66 # find the first non common ancestors (they are siblings)
67 52 my $a_anc= pop @a_pile;
68 52 my $b_anc= pop @b_pile;
69  
70 52 ▶ while( $a_anc == $b_anc)
Branch: 1 branch
BranchTrueFalseTotal
if ($a_anc == $b_anc) 138 52 190
71 138 { $a_anc= pop @a_pile;
72 138 $b_anc= pop @b_pile;
73 }
74  
75 52 ▶ if( defined( $a_anc->{_rank}) && defined( $b_anc->{_rank}))
Branch: 1 branch
BranchTrueFalseTotal
if (defined $a_anc->{'_rank'} and defined $b_anc->{'_rank'}) { } 52 0 52
Condition33%1/3
defined $a_anc->{"_rank"} and defined $b_anc->{"_rank"}
!ll&&!rl&&r
0052
MC/DC0%0/2
defined $a_anc->{"_rank"} and defined $b_anc->{"_rank"}
defined $a_anc->{"_rank"} defined $b_anc->{"_rank"}
Truth table33%1/3
A and B
A= defined $a_anc->{"_rank"}B= defined $b_anc->{"_rank"}
ABresult
0X0
100
111
76 52 { return $a_anc->{_rank} <=> $b_anc->{_rank}; }
77 else
78 {
79 # from there move left and right and figure out the order
80 0 my( $a_prev, $a_next, $b_prev, $b_next)= ($a_anc, $a_anc, $b_anc, $b_anc);
81 0 while()
82 0 ▶ { $a_prev= $a_prev->getPreviousSibling || return -1;
Condition0%0/2
$a_prev->getPreviousSibling || (return -1)
l!l
00
MC/DC0%0/1
$a_prev->getPreviousSibling || (return -1)
$a_prev->getPreviousSibling
Truth table0%0/2
A
A= $a_prev->getPreviousSibling
Aresult
11
00
83 0 ▶ return 1 if( $a_prev == $b_anc);
Branch: 1 branch
BranchTrueFalseTotal
if $a_prev == $b_anc 0 0 0
84 0 ▶ $a_next= $a_next->getNextSibling || return 1;
Condition0%0/2
$a_next->getNextSibling || (return 1)
l!l
00
MC/DC0%0/1
$a_next->getNextSibling || (return 1)
$a_next->getNextSibling
Truth table0%0/2
A
A= $a_next->getNextSibling
Aresult
11
00
85 0 ▶ return -1 if( $a_next == $b_anc);
Branch: 1 branch
BranchTrueFalseTotal
if $a_next == $b_anc 0 0 0
86 0 ▶ $b_prev= $b_prev->getPreviousSibling || return 1;
Condition0%0/2
$b_prev->getPreviousSibling || (return 1)
l!l
00
MC/DC0%0/1
$b_prev->getPreviousSibling || (return 1)
$b_prev->getPreviousSibling
Truth table0%0/2
A
A= $b_prev->getPreviousSibling
Aresult
11
00
87 0 ▶ return -1 if( $b_prev == $a_next);
Branch: 1 branch
BranchTrueFalseTotal
if $b_prev == $a_next 0 0 0
88 0 ▶ $b_next= $b_next->getNextSibling || return -1;
Condition0%0/2
$b_next->getNextSibling || (return -1)
l!l
00
MC/DC0%0/1
$b_next->getNextSibling || (return -1)
$b_next->getNextSibling
Truth table0%0/2
A
A= $b_next->getNextSibling
Aresult
11
00
89 0 ▶ return 1 if( $b_next == $a_prev);
Branch: 1 branch
BranchTrueFalseTotal
if $b_next == $a_prev 0 0 0
90 }
91 }
92 }
93  
94  
95 # need to modify directly the HTML::Element package, because HTML::TreeBuilder won't let me
96 # change the class of the nodes it generates
97 package HTML::Element;
98 4 ▶use Scalar::Util qw(weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
99 4 ▶use vars qw(@ISA);
Subroutine BEGIN: called CC=1 SCAR=0.0
100  
101 4 push @ISA, 'HTML::TreeBuilder::XPath::Node';
102  
103 4 ▶use XML::XPathEngine;
Subroutine BEGIN: called CC=1 SCAR=0.0
104  
105 4 { my $xp;
106 sub xp
107 74 ▶ { $xp ||=XML::XPathEngine->new();
Subroutine xp: called CC=2 SCAR=7.1
Pod undocumented
Condition66%2/3
$xp ||= "XML::XPathEngine"->new
l!l&&r!l&&!r
7130
MC/DC0%0/2
$xp ||= "XML::XPathEngine"->new
$xp "XML::XPathEngine"->new
Truth table66%2/3
A ||= B
A= $xpB= "XML::XPathEngine"->new
ABresult
1X1
011
000
108 74 return $xp;
109 }
110 }
111  
112  
113 2 ▶sub findnodes { my( $elt, $path)= @_; return xp->findnodes( $path, $elt); }
Subroutine findnodes: called CC=1 SCAR=0.0
Pod undocumented
114 0 ▶sub findnodes_as_string { my( $elt, $path)= @_; return xp->findnodes_as_string( $path, $elt); }
Subroutine findnodes_as_string: not called CC=1 SCAR=6.9
Pod undocumented
115 2 ▶sub findnodes_as_strings { my( $elt, $path)= @_; return xp->findnodes_as_strings( $path, $elt); }
Subroutine findnodes_as_strings: called CC=1 SCAR=0.0
Pod undocumented
116 68 ▶sub findvalue { my( $elt, $path)= @_; return xp->findvalue( $path, $elt); }
Subroutine findvalue: called CC=1 SCAR=0.0
Pod undocumented
117 2 ▶sub findvalues { my( $elt, $path)= @_; return xp->findvalues( $path, $elt); }
Subroutine findvalues: called CC=1 SCAR=0.0
Pod undocumented
118 0 ▶sub exists { my( $elt, $path)= @_; return xp->exists( $path, $elt); }
Subroutine exists: not called CC=1 SCAR=6.9
Pod undocumented
119 0 ▶sub find_xpath { my( $elt, $path)= @_; return xp->find( $path, $elt); }
Subroutine find_xpath: not called CC=1 SCAR=6.9
Pod undocumented
120 0 ▶sub matches { my( $elt, $path)= @_; return xp->matches( $elt, $path, $elt); }
Subroutine matches: not called CC=1 SCAR=6.9
Pod undocumented
121 0 ▶sub set_namespace { my $elt= shift; xp->new->set_namespace( @_); }
Subroutine set_namespace: not called CC=1 SCAR=6.9
Pod undocumented
122  
123 sub getRootNode
124 75 ▶ { my $elt= shift;
Subroutine getRootNode: called CC=1 SCAR=0.0
Pod undocumented
125 # The parent of root is a HTML::TreeBuilder::XPath::Root
126 # that helps getting the tree to mimic a DOM tree
127 75 return $elt->root->getParentNode; # I like this one!
128 }
129  
130 sub getParentNode
131 150 ▶ { my $elt= shift;
Subroutine getParentNode: called CC=2 SCAR=6.9
Pod undocumented
132 150 ▶ return $elt->{_parent} || bless { _root => $elt }, 'HTML::TreeBuilder::XPath::Root';
Condition100%2/2
$elt->{'_parent'} || bless({'_root', $elt}, 'HTML::TreeBuilder::XPath::Root')
l!l
5199
MC/DC100%1/1
$elt->{'_parent'} || bless({'_root', $elt}, 'HTML::TreeBuilder::XPath::Root')
$elt->{'_parent'}
Truth table100%2/2
A
A= $elt->{'_parent'}
Aresult
11
00
133 }
134 1102 ▶sub getName { return shift->tag; }
Subroutine getName: called CC=1 SCAR=0.0
Pod undocumented
135 0 ▶sub getLocalName { (my $name= $_[0]->tag) =~ s{^.*:}{}; $name; }
Subroutine getLocalName: not called CC=1 SCAR=6.9
Pod undocumented
136 143 ▶sub getNextSibling { my( $elt)= @_;
Subroutine getNextSibling: called CC=3 SCAR=11.0
Pod undocumented
137 143 ▶ my $parent= $elt->{_parent} || return undef;
Condition100%2/2
$elt->{'_parent'} || (return undef)
l!l
12221
MC/DC100%1/1
$elt->{'_parent'} || (return undef)
$elt->{'_parent'}
Truth table100%2/2
A
A= $elt->{'_parent'}
Aresult
11
00
138 122 ▶ return $parent->_child_as_object( scalar $elt->right, ($elt->{_rank} || 0) + 1);
Condition100%2/2
$elt->{'_rank'} || 0
l!l
1166
MC/DC100%1/1
$elt->{'_rank'} || 0
$elt->{'_rank'}
Truth table100%2/2
A
A= $elt->{'_rank'}
Aresult
11
00
139 }
140 21 ▶sub getPreviousSibling { my( $elt)= @_;
Subroutine getPreviousSibling: called CC=3 SCAR=11.0
Pod undocumented
141 21 ▶ my $parent= $elt->{_parent} || return undef;
Condition100%2/2
$elt->{'_parent'} || (return undef)
l!l
183
MC/DC100%1/1
$elt->{'_parent'} || (return undef)
$elt->{'_parent'}
Truth table100%2/2
A
A= $elt->{'_parent'}
Aresult
11
00
142 18 ▶ return undef unless $elt->{_rank};
Branch: 1 branch
BranchTrueFalseTotal
unless $elt->{"_rank"} 6 12 18
143 12 return $parent->_child_as_object( scalar $elt->left, $elt->{_rank} - 1);
144 }
145 1102 ▶sub isElementNode { return ref $_[0] && ($_[0]->{_tag}!~ m{^~}) ? 1 : 0; }
Subroutine isElementNode: called CC=3 SCAR=14.2
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] && !($_[0]{'_tag'} =~ /^~/) ? : 1102 0 1102
Condition33%1/3
ref $_[0] and not $_[0]{"_tag"} =~ /^~/
!ll&&!rl&&r
001102
MC/DC0%0/2
ref $_[0] and not $_[0]{"_tag"} =~ /^~/
ref $_[0] not $_[0]{"_tag"} =~ /^~/
Truth table33%1/3
A and not(B)
A= ref $_[0]B= not $_[0]{"_tag"} =~ /^~/
ABresult
0X0
100
111
146 77 ▶sub isCommentNode { return ref $_[0] && ($_[0]->{_tag} eq '~comment') ? 1 : 0; }
Subroutine isCommentNode: called CC=3 SCAR=14.2
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] && $_[0]{'_tag'} eq '~comment' ? : 0 77 77
Condition33%1/3
ref $_[0] and $_[0]{"_tag"} eq "~comment"
!ll&&!rl&&r
0770
MC/DC0%0/2
ref $_[0] and $_[0]{"_tag"} eq "~comment"
ref $_[0] $_[0]{"_tag"} eq "~comment"
Truth table33%1/3
A and B
A= ref $_[0]B= $_[0]{"_tag"} eq "~comment"
ABresult
0X0
100
111
147 0 ▶sub isProcessingInstructionNode { return ref $_[0] && ($_[0]->{_tag} eq '~pi') ? 1 : 0; }
Subroutine isProcessingInstructionNode: not called CC=3 SCAR=24.8
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] && $_[0]{'_tag'} eq '~pi' ? : 0 0 0
Condition0%0/3
ref $_[0] and $_[0]{"_tag"} eq "~pi"
!ll&&!rl&&r
000
MC/DC0%0/2
ref $_[0] and $_[0]{"_tag"} eq "~pi"
ref $_[0] $_[0]{"_tag"} eq "~pi"
Truth table0%0/3
A and B
A= ref $_[0]B= $_[0]{"_tag"} eq "~pi"
ABresult
0X0
100
111
148 5 ▶sub isTextNode { return ref $_[0] ? 0 : 1; }
Subroutine isTextNode: called CC=2 SCAR=7.6
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 5 0 5
149  
150 sub getValue
151 2 ▶ { my $elt= shift;
Subroutine getValue: called CC=2 SCAR=7.6
Pod undocumented
152 0 ▶ if( $elt->isCommentNode) { return $elt->{text}; }
Branch: 1 branch
BranchTrueFalseTotal
if ($elt->isCommentNode) 0 2 2
153 2 return $elt->as_text;
154 }
155
156 sub getChildNodes
157 1544 ▶ { my $parent= shift;
Subroutine getChildNodes: called CC=2 SCAR=6.9
Pod undocumented
158 1544 my $rank=0;
159 1544 my @children= map { $parent->_child_as_object( $_, $rank++) } $parent->content_list;
160 1544 ▶ return wantarray ? @children : \@children;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 963 581 1544
161 }
162  
163 sub getFirstChild
164 80 ▶ { my $parent= shift;
Subroutine getFirstChild: called CC=2 SCAR=7.4
Pod undocumented
165 80 my @content= $parent->content_list;
166 80 ▶ if( @content)
Branch: 1 branch
BranchTrueFalseTotal
if (@content) { } 80 0 80
167 80 { return $parent->_child_as_object( $content[0], 0); }
168 else
169 0 { return undef; }
170 }
171 sub getLastChild
172 12 ▶ { my $parent= shift;
Subroutine getLastChild: called CC=2 SCAR=6.9
Pod undocumented
173 12 my @content= $parent->content_list;
174 12 ▶ if( @content)
Branch: 1 branch
BranchTrueFalseTotal
if (@content) { } 9 3 12
175 9 { return $parent->_child_as_object( $content[-1], $#content); }
176 else
177 3 { return undef; }
178 }
179  
180 sub getAttributes
181 324 ▶ { my $elt= shift;
Subroutine getAttributes: called CC=2 SCAR=7.0
Pod undocumented
182 324 my %atts= $elt->all_external_attr;
183 324 my $rank=0;
184 324 my @atts= map { bless( { _name => $_, _value => $atts{$_},
185 _elt => $elt, _rank => $rank++,
186 },
187 'HTML::TreeBuilder::XPath::Attribute'
188 )
189 } sort keys %atts;
190 324 ▶ return wantarray ? @atts : \@atts;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 324 324
191 }
192  
193 0 ▶sub to_number { return XML::XPathEngine::Number->new( $_[0]->as_text); }
Subroutine to_number: not called CC=1 SCAR=6.9
Pod undocumented
194 sub string_value
195 75 ▶ { my $elt= shift;
Subroutine string_value: called CC=2 SCAR=7.6
Pod undocumented
196 0 ▶ if( $elt->isCommentNode) { return $elt->{text}; }
Branch: 1 branch
BranchTrueFalseTotal
if ($elt->isCommentNode) 0 75 75
197 75 return $elt->as_text;
198 };
199  
200 # called on a parent, with a child as second argument and its rank as third
201 # returns the child if it is already an element, or
202 # a new HTML::TreeBuilder::XPath::Text element if it is a plain string
203 sub _child_as_object
204 3101 ▶ { my( $elt, $elt_or_text, $rank)= @_;
Subroutine _child_as_object: called CC=4 SCAR=14.0
205 3101 ▶ return undef unless( defined $elt_or_text);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $elt_or_text 42 3059 3101
206 3059 ▶ if( ! ref $elt_or_text)
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $elt_or_text) 1477 1582 3059
207 { # $elt_or_text is a string, turn it into a TextNode object
208 1477 $elt_or_text= bless { _content => $elt_or_text, _parent => $elt, },
209 'HTML::TreeBuilder::XPath::TextNode'
210 ;
211 1477 Scalar::Util::weaken($elt_or_text->{_parent});
212 }
213 0 ▶ if( ref $rank) { warn "rank is a ", ref( $rank), " elt_or_text is a ", ref( $elt_or_text); }
Branch: 1 branch
BranchTrueFalseTotal
if (ref $rank) 0 3059 3059
214 3059 $elt_or_text->{_rank}= $rank; # used for sorting;
215 3059 return $elt_or_text;
216 }
217  
218 0 ▶sub toString { return shift->as_XML( @_); }
Subroutine toString: not called CC=1 SCAR=6.9
Pod undocumented
219  
220 # produces better looking XML
221 {
222 4 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
223 sub as_XML_compact
224 0 ▶ { my( $node, $opt)= @_;
Subroutine as_XML_compact: not called CC=12 SCAR=50.5
Pod undocumented
225 0 my $name = $node->{'_tag'};
226 0 ▶ if( $name eq '~literal') { return _xml_escape_text( $node->{text}); }
Branch: 1 branch
BranchTrueFalseTotal
if ($name eq "~literal") 0 0 0
227  
228 0 ▶ if( $name eq '~declaration') { return '<!' . _xml_escape_text( $node->{text}) . ">"; }
Branch: 1 branch
BranchTrueFalseTotal
if ($name eq "~declaration") 0 0 0
229 0 ▶ if( $name eq '~pi') { return '<?' . _xml_escape_text( $node->{text}) . '?>'; }
Branch: 1 branch
BranchTrueFalseTotal
if ($name eq "~pi") 0 0 0
230 0 ▶ if( $name eq '~comment') { return '<!--' . _xml_escape_comment( $node->{text}) . '-->'; }
Branch: 1 branch
BranchTrueFalseTotal
if ($name eq "~comment") 0 0 0
231  
232 0 my $lc_name= lc $name;
233  
234 0 my $xml= $node->_start_tag;
235  
236 0 ▶ if( $HTML::Tagset::isCDATA_Parent{$lc_name})
Branch: 1 branch
BranchTrueFalseTotal
if ($HTML::Tagset::isCDATA_Parent{$lc_name}) { } 0 0 0
237 0 ▶ { my $content= $node->{_content} || '';
Condition0%0/2
$node->{'_content'} || ''
l!l
00
MC/DC0%0/1
$node->{'_content'} || ''
$node->{'_content'}
Truth table0%0/2
A
A= $node->{'_content'}
Aresult
11
00
238 0 ▶ if( ref $content eq 'ARRAY' || $content->isa( 'ARRAY'))
Branch: 1 branch
BranchTrueFalseTotal
if (ref $content eq 'ARRAY' or $content->isa('ARRAY')) { } 0 0 0
Condition0%0/3
ref $content eq "ARRAY" or $content->isa("ARRAY")
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $content eq "ARRAY" or $content->isa("ARRAY")
ref $content eq "ARRAY" $content->isa("ARRAY")
Truth table0%0/3
A or B
A= ref $content eq "ARRAY"B= $content->isa("ARRAY")
ABresult
1X1
011
000
239 0 { $xml .= _xml_escape_cdata( join( '', @$content), $opt); }
240 else
241 0 { $xml .= $content; }
242 }
243 else
244 { # start tag
245 0 foreach my $child ($node->content_list)
246 0 ▶ { if( ref $child) { $xml .= $child->as_XML_compact(); }
Branch: 1 branch
BranchTrueFalseTotal
if (ref $child) { } 0 0 0
247 0 else { $xml .= _xml_escape_text( $child); }
248 }
249 }
250 0 ▶ $xml.= "</$name>" unless $HTML::Tagset::emptyElement{$lc_name};
Branch: 1 branch
BranchTrueFalseTotal
unless $HTML::Tagset::emptyElement{$lc_name} 0 0 0
251 0 return $xml;
252 }
253 }
254  
255
256  
257 4 { my %phrase_name; # all phrase tags, + literals (those are not indented)
258 my %extra_newline; # tags that get an extra newline before the end tag
259 4 my $default_indent; # 2 spaces, change with the 'indent' option
260 BEGIN
261 4 ▶ { %phrase_name= %HTML::Tagset::isPhraseMarkup;
Subroutine BEGIN: called CC=1 SCAR=0.0
262 4 $phrase_name{'~literal'}= 1;
263 4 $default_indent= ' ';
264 4 %extra_newline= map { $_ => 1 } qw(html head body script div table tbody thead tfoot tr form dl ol ul);
265 }
266  
267 sub as_XML_indented
268 0 ▶ { my( $node, $opt)= @_;
Subroutine as_XML_indented: not called CC=20 SCAR=60.4
Pod undocumented
269  
270  
271 0 my $name = $node->{'_tag'};
272 0 my $lc_name= lc $name;
273  
274 0 ▶ if( $name eq '~literal') { return _xml_escape_text( $node->{text}); }
Branch: 1 branch
BranchTrueFalseTotal
if ($name eq "~literal") 0 0 0
275 0 ▶ if( $name eq '~declaration') { return '<!' . _xml_escape_text( $node->{text}) . ">\n"; }
Branch: 1 branch
BranchTrueFalseTotal
if ($name eq "~declaration") 0 0 0
276  
277  
278 0 ▶ if( $name eq '~pi') { return '<?' . _xml_escape_text( $node->{text}) . '?>'; }
Branch: 1 branch
BranchTrueFalseTotal
if ($name eq "~pi") 0 0 0
279 0 ▶ if( $name eq '~comment') { return '<!--' . _xml_escape_comment( $node->{text}) . '-->'; }
Branch: 1 branch
BranchTrueFalseTotal
if ($name eq "~comment") 0 0 0
280
281 0 my $xml;
282 0 my $pre_tag_indent='';
283 0 ▶ if(!$phrase_name{$lc_name}) { $pre_tag_indent= "\n" . ($opt->{indent} || $default_indent) x ($opt->{indent_level}||0); }
Branch: 1 branch
BranchTrueFalseTotal
unless ($phrase_name{$lc_name}) 0 0 0
Condition0%0/5
$opt->{'indent'} || $default_indent
l!l&&r!l&&!r
000
$opt->{'indent_level'} || 0
l!l
00
MC/DC0%0/2
$opt->{'indent'} || $default_indent
$opt->{'indent'} $default_indent
MC/DC0%0/1
$opt->{'indent_level'} || 0
$opt->{'indent_level'}
Truth table0%0/3
A || B
A= $opt->{'indent'}B= $default_indent
ABresult
1X1
011
000
Truth table0%0/2
A
A= $opt->{'indent_level'}
Aresult
11
00
284 0 ▶ if( $opt->{indent_level}) { $xml .= $pre_tag_indent; }
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{"indent_level"}) 0 0 0
285  
286 0 $xml.= $node->_start_tag();
287  
288 0 my $content='';
289  
290 0 ▶ if( $HTML::Tagset::isCDATA_Parent{$lc_name})
Branch: 1 branch
BranchTrueFalseTotal
if ($HTML::Tagset::isCDATA_Parent{$lc_name}) { } 0 0 0
291 0 ▶ { my $content= $node->{_content} || '';
Condition0%0/2
$node->{'_content'} || ''
l!l
00
MC/DC0%0/1
$node->{'_content'} || ''
$node->{'_content'}
Truth table0%0/2
A
A= $node->{'_content'}
Aresult
11
00
292 0 ▶ if( ref $content && (ref $content eq 'ARRAY' || $content->isa( 'ARRAY') ))
Branch: 1 branch
BranchTrueFalseTotal
if (ref $content and ref $content eq "ARRAY" || $content->isa("ARRAY")) 0 0 0
Condition0%0/6
ref $content and ref $content eq "ARRAY" || $content->isa("ARRAY")
!ll&&!rl&&r
000
ref $content eq "ARRAY" or $content->isa("ARRAY")
l!l&&r!l&&!r
000
MC/DC0%0/3
ref $content and ref $content eq "ARRAY" || $content->isa("ARRAY")
ref $content ref $content eq "ARRAY" $content->isa("ARRAY")
Truth table0%0/4
A and B or C
A= ref $contentB= ref $content eq "ARRAY"C= $content->isa("ARRAY")
ABCresult
0XX0
1000
11X1
1011
293 0 { $content= _xml_escape_cdata( join( '', @$content), $opt); }
294 }
295 else
296 {
297 0 my %child_opt= %$opt;
298 0 $child_opt{indent_level}++;
299 0 foreach my $child ($node->content_list)
300 0 ▶ { if( ref $child) { $content .= $child->as_XML_indented( \%child_opt ); }
Branch: 1 branch
BranchTrueFalseTotal
if (ref $child) { } 0 0 0
301 0 else { $content .= _xml_escape_text( $child); }
302 }
303 }
304 0 $xml .= $content;
305  
306 0 ▶ if( $extra_newline{$lc_name} && $content ne '' ) { $xml.= $pre_tag_indent; }
Branch: 1 branch
BranchTrueFalseTotal
if ($extra_newline{$lc_name} and $content ne "") 0 0 0
Condition0%0/3
$extra_newline{$lc_name} and $content ne ""
!ll&&!rl&&r
000
MC/DC0%0/2
$extra_newline{$lc_name} and $content ne ""
$extra_newline{$lc_name} $content ne ""
Truth table0%0/3
A and B
A= $extra_newline{$lc_name}B= $content ne ""
ABresult
0X0
100
111
307 0 ▶ $xml.= "</$name>" unless $HTML::Tagset::emptyElement{$lc_name};
Branch: 1 branch
BranchTrueFalseTotal
unless $HTML::Tagset::emptyElement{$lc_name} 0 0 0
308 0 ▶ $xml .="\n" if( !$opt->{indent_level});
Branch: 1 branch
BranchTrueFalseTotal
unless $opt->{"indent_level"} 0 0 0
309
310 0 return $xml;
311 }
312 }
313  
314 sub _start_tag
315 0 ▶ { my( $node)= @_;
Subroutine _start_tag: not called CC=6 SCAR=36.1
316 0 my $name = $node->{'_tag'};
317 0 my $start_tag.= "<$name";
318 0 foreach my $att_name (sort keys %$node)
319 0 ▶ { next if( (!length $att_name) || ($att_name=~ m{^_}) || ($att_name eq '/') );
Branch: 1 branch
BranchTrueFalseTotal
if not length $att_name or $att_name =~ /^_/ or $att_name eq "/" 0 0 0
Condition0%0/6
not length $att_name or $att_name =~ /^_/ or $att_name eq "/"
l!l&&r!l&&!r
000
not length $att_name or $att_name =~ /^_/
l!l&&r!l&&!r
000
MC/DC0%0/3
not length $att_name or $att_name =~ /^_/ or $att_name eq "/"
not length $att_name $att_name =~ /^_/ $att_name eq "/"
Truth table0%0/4
not(A) or B or C
A= not length $att_nameB= $att_name =~ /^_/C= $att_name eq "/"
ABCresult
1XX1
01X1
0011
0000
320 0 my $well_formed_att_name= well_formed_name( $att_name);
321 0 $start_tag .= qq{ $well_formed_att_name="} . _xml_escape_attribute_value( $node->{$att_name}) . qq{"};
322 }
323 0 ▶ $start_tag.= $HTML::Tagset::emptyElement{lc $name} ? " />" : ">";
Branch: 1 branch
BranchTrueFalseTotal
$HTML::Tagset::emptyElement{lc $name} ? : 0 0 0
324 0 return $start_tag;
325 }
326  
327 sub well_formed_name
328 0 ▶ { my( $name)= @_;
Subroutine well_formed_name: not called CC=2 SCAR=17.9
Pod undocumented
329 0 $name=~ s{[^\w:_-]+}{_}g;
330 0 ▶ if( $name=~ m{^\d}) { $name= "a$name"; }
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ /^\d/) 0 0 0
331 0 return $name;
332 }
333  
334 sub _indent_level
335 0 ▶ { my( $node)= @_;
Subroutine _indent_level: not called CC=1 SCAR=6.9
336 0 my $level= scalar grep { !$HTML::Tagset::isPhraseMarkup{lc $_->{_tag}} } $node->lineage;
337 0 return $level;
338 }
339  
340 4 { my( $indent, %extra_newline, $nl);
341 BEGIN
342 4 ▶ { $indent= ' ';
Subroutine BEGIN: called CC=1 SCAR=0.0
343 4 $nl= "\n";
344 4 %extra_newline= map { $_ => 1 } qw(html head body script div table tr form ol ul);
345 }
346
347 sub indents
348 0 ▶ { my( $opt, $name)= @_;
Subroutine indents: not called CC=8 SCAR=42.8
Pod undocumented
349 0 my $indents= { pre_start_tag => '', post_start_tag => '', pre_end_tag => '', post_end_tag => ''};
350 0 ▶ if( $opt->{indented})
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{"indented"}) 0 0 0
351 0 { my $indent_level= $opt->{indent_level};
352 0 my $wrapping_nl= $nl;
353 0 ▶ if( !defined( $indent_level)) { $indent_level = 0; $wrapping_nl= ''; }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $indent_level) 0 0 0
354 0 ▶ if( $HTML::Tagset::isKnown{lc $name} && !$HTML::Tagset::isPhraseMarkup{lc $name} && $indent_level > 0)
Branch: 1 branch
BranchTrueFalseTotal
if ($HTML::Tagset::isKnown{lc $name} and not $HTML::Tagset::isPhraseMarkup{lc $name} and $indent_level > 0) 0 0 0
Condition0%0/6
$HTML::Tagset::isKnown{lc $name} and not $HTML::Tagset::isPhraseMarkup{lc $name} and $indent_level > 0
!ll&&!rl&&r
000
$HTML::Tagset::isKnown{lc $name} and not $HTML::Tagset::isPhraseMarkup{lc $name}
!ll&&!rl&&r
000
MC/DC0%0/3
$HTML::Tagset::isKnown{lc $name} and not $HTML::Tagset::isPhraseMarkup{lc $name} and $indent_level > 0
$HTML::Tagset::isKnown{lc $name} not $HTML::Tagset::isPhraseMarkup{lc $name} $indent_level > 0
Truth table0%0/4
A and not(B) and C
A= $HTML::Tagset::isKnown{lc $name}B= not $HTML::Tagset::isPhraseMarkup{lc $name}C= $indent_level > 0
ABCresult
0XX0
10X0
1100
1111
355 0 { $indents->{pre_start_tag}= $wrapping_nl . ($indent x $indent_level); }
356 0 ▶ if( $extra_newline{lc $name})
Branch: 1 branch
BranchTrueFalseTotal
if ($extra_newline{lc $name}) 0 0 0
357 0 { $indents->{post_start_tag}= $nl;
358 0 $indents->{pre_end_tag}= $nl . ($indent x $indent_level);
359 }
360 0 ▶ if( $indent_level == 0)
Branch: 1 branch
BranchTrueFalseTotal
if ($indent_level == 0) 0 0 0
361 0 { $indents->{post_end_tag} = $wrapping_nl; }
362 }
363 0 return $indents;
364 }
365 }
366  
367
368 sub _xml_escape_attribute_value
369 0 ▶ { my( $text)= @_;
Subroutine _xml_escape_attribute_value: not called CC=1 SCAR=3.5
370 0 $text=~ s{([&<>"])}{$CHAR2DEFAULT_ENT{$1}}g; # escape also quote, as it is the attribute separator
371 0 return $text;
372 }
373  
374 sub _xml_escape_text
375 0 ▶ { my( $text)= @_;
Subroutine _xml_escape_text: not called CC=1 SCAR=6.9
376 0 $text=~ s{([&<>])}{$CHAR2DEFAULT_ENT{$1}}g;
377 0 return $text;
378 }
379  
380 sub _xml_escape_comment
381 0 ▶ { my( $text)= @_;
Subroutine _xml_escape_comment: not called CC=1 SCAR=6.9
382 0 $text=~ s{([&<>])}{$CHAR2DEFAULT_ENT{$1}}g;
383 0 $text=~ s{--}{-&#45;}g; # can't have double --'s in XML comments
384 0 return $text;
385 }
386  
387 sub _xml_escape_cdata
388 0 ▶ { my( $text, $opt)= @_;
Subroutine _xml_escape_cdata: not called CC=3 SCAR=24.8
389 0 ▶ if( $opt->{force_escape_cdata} || $text=~ m{[<&]})
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{"force_escape_cdata"} or $text =~ /[<&]/) 0 0 0
Condition0%0/3
$opt->{"force_escape_cdata"} or $text =~ /[<&]/
l!l&&r!l&&!r
000
MC/DC0%0/2
$opt->{"force_escape_cdata"} or $text =~ /[<&]/
$opt->{"force_escape_cdata"} $text =~ /[<&]/
Truth table0%0/3
A or B
A= $opt->{"force_escape_cdata"}B= $text =~ /[<&]/
ABresult
1X1
011
000
390 0 { $text=~ s{^\s*\Q<![CDATA[}{}s;
391 0 $text=~ s{\Q]]>\E\s*$}{}s;
392 0 $text=~ s{]]>}{]]&#62;}g; # can't have]]> in CDATA
393 0 $text= "<![CDATA[$text]]>";
394 }
395 0 return $text;
396 }
397  
398  
399 package HTML::TreeBuilder::XPath::TextNode;
400  
401 4 ▶use base 'HTML::TreeBuilder::XPath::Node';
Subroutine BEGIN: called CC=1 SCAR=0.0
402  
403 89 ▶sub getParentNode { return shift->{_parent}; }
Subroutine getParentNode: called CC=1 SCAR=0.0
404 0 ▶sub getValue { return shift->{_content}; }
Subroutine getValue: not called CC=1 SCAR=6.9
405 6 ▶sub isTextNode { return 1; }
Subroutine isTextNode: called CC=1 SCAR=0.0
406 38 ▶sub getAttributes { return wantarray ? () : []; }
Subroutine getAttributes: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 38 38
407  
408 # similar to HTML::Element as_XML
409 sub as_XML
410 0 ▶ { my( $node, $entities)= @_;
Subroutine as_XML: not called CC=3 SCAR=24.8
411 0 my $content= $node->{_content};
412 0 ▶ if( $node->{_parent} && $node->{_parent}->{_tag} eq 'script')
Branch: 1 branch
BranchTrueFalseTotal
if ($node->{'_parent'} and $node->{'_parent'}{'_tag'} eq 'script') { } 0 0 0
Condition0%0/3
$node->{"_parent"} and $node->{"_parent"}{"_tag"} eq "script"
!ll&&!rl&&r
000
MC/DC0%0/2
$node->{"_parent"} and $node->{"_parent"}{"_tag"} eq "script"
$node->{"_parent"} $node->{"_parent"}{"_tag"} eq "script"
Truth table0%0/3
A and B
A= $node->{"_parent"}B= $node->{"_parent"}{"_tag"} eq "script"
ABresult
0X0
100
111
413 0 { $content=~ s{(&\w+;)}{HTML::Entities::decode($1)}eg; }
414 else
415 0 { $content= HTML::Element::_xml_escape_text($content); }
416 0 return $content;
417 }
418 4 *as_XML_compact = *as_XML;
419 4 *as_XML_indented = *as_XML;
420  
421  
422 sub getPreviousSibling
423 9 ▶ { my $self= shift;
Subroutine getPreviousSibling: called CC=2 SCAR=7.0
424 9 my $rank= $self->{_rank};
425 #unless( defined $self->{_rank})
426 # { warn "no rank for text node $self->{_content}, parent is $self->{_parent}->{_tag}\n"; }
427 9 my $parent= $self->{_parent};
428 9 ▶ return $rank ? $parent->_child_as_object( $parent->{_content}->[$rank-1], $rank-1) : undef;
Branch: 1 branch
BranchTrueFalseTotal
$rank ? : 0 9 9
429 }
430  
431 sub getNextSibling
432 80 ▶ { my $self= shift;
Subroutine getNextSibling: called CC=2 SCAR=7.0
433 80 my $rank= $self->{_rank};
434 #unless( defined $self->{_rank})
435 # { warn "no rank for text node $self->{_content}, parent is $self->{_parent}->{_tag}\n"; }
436 80 my $parent= $self->{_parent};
437 80 my $next_sibling= $parent->{_content}->[$rank+1];
438 80 ▶ return defined( $next_sibling) ? $parent->_child_as_object( $next_sibling, $rank+1) : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $next_sibling ? : 0 80 80
439 }
440  
441 sub getRootNode
442 0 ▶ { return shift->{_parent}->getRootNode; }
Subroutine getRootNode: not called CC=1 SCAR=6.9
443  
444 1 ▶sub string_value { return shift->{_content}; }
Subroutine string_value: called CC=1 SCAR=0.0
445  
446 # added to provide element-like methods to text nodes, for use by cmp
447 sub lineage
448 0 ▶ { my( $node)= @_;
Subroutine lineage: not called CC=1 SCAR=6.9
449 0 my $parent= $node->{_parent};
450 0 return( $parent, $parent->lineage);
451 }
452  
453 sub is_inside
454 0 ▶ { my( $text, $node)= @_;
Subroutine is_inside: not called CC=1 SCAR=6.9
455 0 return $text->{_parent}->is_inside( $node);
456 }
457  
458 4 1;
459  
460  
461 package HTML::TreeBuilder::XPath::Attribute;
462 4 ▶use base 'HTML::TreeBuilder::XPath::Node';
Subroutine BEGIN: called CC=1 SCAR=0.0
463  
464 1 ▶sub getParentNode { return $_[0]->{_elt}; }
Subroutine getParentNode: called CC=1 SCAR=0.0
465 2 ▶sub getValue { return $_[0]->{_value}; }
Subroutine getValue: called CC=1 SCAR=0.0
466 270 ▶sub getName { return $_[0]->{_name} ; }
Subroutine getName: called CC=1 SCAR=0.0
467 0 ▶sub getLocalName { (my $name= $_[0]->{_name}) =~ s{^.*:}{}; $name; }
Subroutine getLocalName: not called CC=1 SCAR=6.9
468 118 ▶sub string_value { return $_[0]->{_value}; }
Subroutine string_value: called CC=1 SCAR=0.0
469 0 ▶sub to_number { return XML::XPathEngine::Number->new( $_[0]->{_value}); }
Subroutine to_number: not called CC=1 SCAR=6.9
470 0 ▶sub isAttributeNode { 1 }
Subroutine isAttributeNode: not called CC=1 SCAR=6.9
471 0 ▶sub toString { return qq{ $_[0]->{_name}="$_[0]->{_value}"}; }
Subroutine toString: not called CC=1 SCAR=6.9
472 0 ▶sub getAttributes { return $_[0]->{_elt}->getAttributes; }
Subroutine getAttributes: not called CC=1 SCAR=6.9
473  
474 # awfully inefficient, but hopefully this is called only for weird (read test-case) queries
475 sub getPreviousSibling
476 0 ▶ { my $self= shift;
Subroutine getPreviousSibling: not called CC=2 SCAR=17.9
477 0 my $rank= $self->{_rank};
478 0 ▶ return undef unless $rank;
Branch: 1 branch
BranchTrueFalseTotal
unless $rank 0 0 0
479 0 my %atts= $self->{_elt}->all_external_attr;
480 0 my $previous_att_name= (sort keys %atts)[$rank-1];
481 return bless( { _name => $previous_att_name,
482 _value => $atts{$previous_att_name},
483 0 _elt => $self->{_elt}, _rank => $rank-1,
484 }, 'HTML::TreeBuilder::XPath::Attribute'
485 );
486 }
487  
488 sub getNextSibling
489 0 ▶ { my $self= shift;
Subroutine getNextSibling: not called CC=2 SCAR=17.9
490 0 my $rank= $self->{_rank};
491 0 my %atts= $self->{_elt}->all_external_attr;
492 0 ▶ my $next_att_name= (sort keys %atts)[$rank+1] || return undef;
Condition0%0/2
(sort keys %atts)[$rank + 1] || (return undef)
l!l
00
MC/DC0%0/1
(sort keys %atts)[$rank + 1] || (return undef)
(sort keys %atts)[$rank + 1]
Truth table0%0/2
A
A= (sort keys %atts)[$rank + 1]
Aresult
11
00
493 return bless( { _name => $next_att_name, _value => $atts{$next_att_name},
494 0 _elt => $self->{_elt}, _rank => $rank+1,
495 }, 'HTML::TreeBuilder::XPath::Attribute'
496 );
497
498 }
499  
500  
501  
502 # added to provide element-like methods to attributes, for use by cmp
503 sub lineage
504 25 ▶ { my( $att)= @_;
Subroutine lineage: called CC=1 SCAR=0.0
505 25 my $elt= $att->{_elt};
506 25 return( $elt, $elt->lineage);
507 }
508  
509 sub is_inside
510 25 ▶ { my( $att, $node)= @_;
Subroutine is_inside: called CC=2 SCAR=8.1
511 25 ▶ return ($att->{_elt} == $node) || $att->{_elt}->is_inside( $node);
Condition33%1/3
$att->{'_elt'} == $node || $att->{'_elt'}->is_inside($node)
l!l&&r!l&&!r
0025
MC/DC0%0/2
$att->{'_elt'} == $node || $att->{'_elt'}->is_inside($node)
$att->{'_elt'} == $node $att->{'_elt'}->is_inside($node)
Truth table33%1/3
A || B
A= $att->{'_elt'} == $nodeB= $att->{'_elt'}->is_inside($node)
ABresult
1X1
011
000
512 }
513  
514 4 1;
515  
516  
517 package HTML::TreeBuilder::XPath::Root;
518  
519 4 ▶use base 'HTML::TreeBuilder::XPath::Node';
Subroutine BEGIN: called CC=1 SCAR=0.0
520
521 25 ▶sub getParentNode { return (); }
Subroutine getParentNode: called CC=1 SCAR=0.0
522 114 ▶sub getChildNodes { my @content= ( $_[0]->{_root}); return wantarray ? @content : \@content; }
Subroutine getChildNodes: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 72 42 114
523 3 ▶sub getAttributes { return [] }
Subroutine getAttributes: called CC=1 SCAR=0.0
524 0 ▶sub isDocumentNode { return 1 }
Subroutine isDocumentNode: not called CC=1 SCAR=6.9
525 1 ▶sub getRootNode { return $_[0] }
Subroutine getRootNode: called CC=1 SCAR=0.0
526 1 ▶sub getName { return }
Subroutine getName: called CC=1 SCAR=0.0
527 22 ▶sub getNextSibling { return }
Subroutine getNextSibling: called CC=1 SCAR=0.0
528 1 ▶sub getPreviousSibling { return }
Subroutine getPreviousSibling: called CC=1 SCAR=0.0
529  
530 # added to provide element-like methods to root, for use by cmp
531 0 ▶sub lineage { return ($_[0]); }
Subroutine lineage: not called CC=1 SCAR=6.9
532 0 ▶sub is_inside { return 0; }
Subroutine is_inside: not called CC=1 SCAR=6.9
533 0 ▶sub cmp { return $_[1]->isa( ' HTML::TreeBuilder::XPath::Root') ? 0 : 1; }
Subroutine cmp: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
$_[1]->isa(' HTML::TreeBuilder::XPath::Root') ? : 0 0 0
534  
535 4 1;
536  
537 __END__
↑ Summary