blib/lib/HTML/Selector/XPath.pm

filter: Statementstmt 91.3% 138 / 151 Branchbran 87.3% 117 / 134 Conditioncond 100% 12 / 12 MC/DCmcdc 100% 7 / 7 Subroutinesub 92.8% 13 / 14 Podpod 40.0% 4 / 10 totaltotal 88.7% 291 / 328 CC 73 SCAR 35.6
CC
73
Cov
90%
consume
54.7
consume_An_plus_B
9.2
convert_attribute_match
7.0
CRAP 35.1
↑ Summary
1 package HTML::Selector::XPath;
2  
3 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 8 ▶use 5.008_001;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 8 our $VERSION = '0.28';
6  
7 8 require Exporter;
8 8 our @EXPORT_OK = qw(selector_to_xpath);
9 8 *import = \&Exporter::import;
10  
11 8 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 23 ▶sub selector_to_xpath {
Subroutine selector_to_xpath: called CC=1 SCAR=0.0
Pod documented
14 23 __PACKAGE__->new(shift)->to_xpath(@_);
15 }
16  
17 # XXX: Identifiers should also allow any characters U+00A0 and higher, and any
18 # escaped characters.
19 8 my $ident = qr/(?![0-9]|-[-0-9])[-_a-zA-Z0-9]+/;
20  
21 8 my $reg = {
22 # tag name/id/class
23 element => qr/^([#.]?)([^\s'"#.\/:@,=~>()\[\]|]*)((\|)([a-z0-9\\*_-]*))?/i,
24 # attribute presence
25 attr1 => qr/^\[ \s* ($ident) \s* \]/x,
26 # attribute value match
27 attr2 => qr/^\[ \s* ($ident) \s*
28 ( [~|*^\$!]? = ) \s*
29 (?: ($ident) | "([^"]*)" | '([^']*)') \s* \] /x,
30 badattr => qr/^\[/,
31 attrN => qr/^:not\(/i, # we chop off the closing parenthesis below in the code
32 pseudo => qr/^:([-\w]+\(?)/i,
33 # adjacency/direct descendance
34 combinator => qr/^(\s*[>+~\s](?!,))/i,
35 # rule separator
36 comma => qr/^\s*,\s*/i,
37 };
38  
39 184 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
40 184 my($class, $exp) = @_;
41 184 bless { expression => $exp }, $class;
42 }
43  
44 0 ▶sub selector {
Subroutine selector: not called CC=2 SCAR=17.9
Pod undocumented
45 0 my $self = shift;
46 0 ▶ $self->{expression} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
47 0 $self->{expression};
48 }
49  
50 30 ▶sub convert_attribute_match {
Subroutine convert_attribute_match: called CC=7 SCAR=19.5
Pod undocumented
51 30 my ($left,$op,$right) = @_;
52 # negation (e.g. [input!="text"]) isn't implemented in CSS, but include it anyway:
53 30 ▶ if ($op eq '!=') {
Branch: 6 branches
BranchTrueFalseTotal
if ($op eq '!=') { } 2 28 30
elsif ($op eq '~=') { } 3 25 28
elsif ($op eq '*=') { } 6 19 25
elsif ($op eq '|=') { } 3 16 19
elsif ($op eq '^=') { } 4 12 16
elsif ($op eq '$=') { } 2 10 12
54 2 "\@$left!='$right'";
55 } elsif ($op eq '~=') { # substring attribute match
56 3 "contains(concat(' ', \@$left, ' '), ' $right ')";
57 } elsif ($op eq '*=') { # real substring attribute match
58 6 "contains(\@$left, '$right')";
59 } elsif ($op eq '|=') {
60 3 "\@$left='$right' or starts-with(\@$left, '$right-')";
61 } elsif ($op eq '^=') {
62 4 "starts-with(\@$left,'$^N')";
63 } elsif ($op eq '$=') {
64 2 my $n = length($^N) - 1;
65 2 "substring(\@$left,string-length(\@$left)-$n)='$^N'";
66 } else { # exact match
67 10 "\@$left='$^N'";
68 }
69 }
70  
71 48 ▶sub _generate_child {
Subroutine _generate_child: called CC=3 SCAR=11.0
72 48 my ($direction,$name,$a,$b) = @_;
73 48 ▶ if ($a == 0) { # 0n+b
Branch: 2 branches
BranchTrueFalseTotal
if ($a == 0) { } 32 16 48
elsif ($a > 0) { } 11 5 16
74 32 $b--;
75 32 "[count($direction-sibling::$name) = $b and parent::*]"
76 } elsif ($a > 0) { # an + b
77 11 return "[not((count($direction-sibling::$name)+1)<$b) and ((count($direction-sibling::$name) + 1) - $b) mod $a = 0 and parent::*]"
78 } else { # -an + $b
79 5 $a = -$a;
80 5 return "[not((count($direction-sibling::$name)+1)>$b) and (($b - (count($direction-sibling::$name) + 1)) mod $a) = 0 and parent::*]"
81 }
82 }
83  
84 32 ▶sub nth_child {
Subroutine nth_child: called CC=2 SCAR=6.9
Pod undocumented
85 32 my ($a,$b) = @_;
86 32 ▶ if (@_ == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1) 15 17 32
87 15 ($a,$b) = (0,$a);
88 }
89 32 _generate_child('preceding', '*', $a, $b);
90 }
91  
92 11 ▶sub nth_last_child {
Subroutine nth_last_child: called CC=2 SCAR=6.9
Pod undocumented
93 11 my ($a,$b) = @_;
94 11 ▶ if (@_ == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1) 7 4 11
95 7 ($a,$b) = (0,$a);
96 }
97 11 _generate_child('following', '*', $a, $b);
98 }
99  
100 # A hacky recursive descent
101 # Only descends for :not(...)
102 25 ▶sub consume_An_plus_B {
Subroutine consume_An_plus_B: called CC=9 SCAR=22.2
Pod undocumented
103 25 my( $rrule ) = @_;
104  
105 25 my( $A, $B );
106  
107 25 ▶ if( $$rrule =~ s/^odd\s*\)// ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($$rrule =~ s/^odd\s*\)//) { } 1 24 25
elsif ($$rrule =~ s/^even\s*\)//) { } 1 23 24
elsif ($$rrule =~ s/^\s*(-?\d+)\s*\)//) { } 9 14 23
elsif ($$rrule =~ s/^\s*(-?\d*)\s*n\s*(?:\+\s*(\d+))?\s*\)//) { } 14 0 14
108 1 ($A,$B) = (2, 1)
109 } elsif( $$rrule =~ s/^even\s*\)// ) {
110 1 ($A,$B) = (2, 0)
111 } elsif( $$rrule =~ s/^\s*(-?\d+)\s*\)// ) {
112 9 ($A,$B) = (0, $1)
113 } elsif( $$rrule =~ s/^\s*(-?\d*)\s*n\s*(?:\+\s*(\d+))?\s*\)// ) {
114 14 ▶ ($A,$B) = ($1, $2 || 0);
Condition100%2/2
$2 || 0
l!l
104
MC/DC100%1/1
$2 || 0
$2
Truth table100%2/2
A
A= $2
Aresult
11
00
115 14 ▶ if( ! defined $A ) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $A) { } 0 14 14
elsif ($A eq '-') { } 3 11 14
elsif ($A eq '') { } 1 10 11
116 0 $A = '0';
117 } elsif( $A eq '-') {
118 3 $A = '-1';
119 } elsif( $A eq '' ) {
120 1 $A = '1';
121 }
122 } else {
123 0 croak "Can't parse formula from '$$rrule'";
124 }
125  
126 25 return ($A, $B);
127 }
128  
129 190 ▶sub consume {
Subroutine consume: called CC=52 SCAR=40.0
Pod undocumented
130 190 my ($self, $rule, %parms) = @_;
131 190 ▶ my $root = $parms{root} || '/';
Condition100%2/2
$parms{'root'} || '/'
l!l
18172
MC/DC100%1/1
$parms{'root'} || '/'
$parms{'root'}
Truth table100%2/2
A
A= $parms{'root'}
Aresult
11
00
132  
133 190 ▶ return [$rule,''] if $rule =~ m!^/!; # If we start with a slash, we're already an XPath?!
Branch: 1 branch
BranchTrueFalseTotal
if $rule =~ m[^/] 1 189 190
134  
135 189 my @parts = ("$root/");
136 189 my $last_rule = '';
137 189 my @next_parts;
138  
139 my $wrote_tag;
140 189 my $root_index = 0; # points to the current root
141 # Loop through each "unit" of the rule
142 189 ▶ while (length $rule && $rule ne $last_rule) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $rule and $rule ne $last_rule) 291 175 466
Condition100%3/3
length $rule and $rule ne $last_rule
!ll&&!rl&&r
1696291
MC/DC100%2/2
length $rule and $rule ne $last_rule
length $rule $rule ne $last_rule
Truth table100%3/3
A and B
A= length $ruleB= $rule ne $last_rule
ABresult
0X0
100
111
143 291 $last_rule = $rule;
144  
145 291 $rule =~ s/^\s*|\s*$//g;
146 291 ▶ last unless length $rule;
Branch: 1 branch
BranchTrueFalseTotal
unless length $rule 0 291 291
147  
148 # Prepend explicit first selector if we have an implicit selector
149 # (that is, if we start with a combinator)
150 291 ▶ if ($rule =~ /$reg->{combinator}/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rule =~ /$reg->{"combinator"}/) 1 290 291
151 1 $rule = "* $rule";
152 }
153  
154 # Match elements
155 291 ▶ if ($rule =~ s/$reg->{element}//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rule =~ s/$reg->{"element"}//) 291 0 291
156 291 my ($id_class,$name,$lang) = ($1,$2,$3);
157  
158 # to add *[1]/self:: for follow-sibling
159 291 ▶ if (@next_parts) {
Branch: 1 branch
BranchTrueFalseTotal
if (@next_parts) 0 291 291
160 0 push @parts, @next_parts; #, (pop @parts);
161 0 @next_parts = ();
162 }
163  
164 291 ▶ my $tag = $id_class eq '' ? $name || '*' : '*';
Branch: 1 branch
BranchTrueFalseTotal
$id_class eq '' ? : 243 48 291
Condition100%2/2
$name or "*"
l!l
20340
MC/DC100%1/1
$name or "*"
$name
Truth table100%2/2
A
A= $name
Aresult
11
00
165  
166 291 ▶ if (defined $parms{prefix} and not $tag =~ /[*:|]/) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $parms{"prefix"} and not $tag =~ /[*:|]/) 12 279 291
Condition100%3/3
defined $parms{"prefix"} and not $tag =~ /[*:|]/
!ll&&!rl&&r
276312
MC/DC100%2/2
defined $parms{"prefix"} and not $tag =~ /[*:|]/
defined $parms{"prefix"} not $tag =~ /[*:|]/
Truth table100%3/3
A and not(B)
A= defined $parms{"prefix"}B= not $tag =~ /[*:|]/
ABresult
0X0
100
111
167 12 $tag = join ':', $parms{prefix}, $tag;
168 }
169  
170 291 ▶ if (! $wrote_tag++) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($wrote_tag++) 250 41 291
171 250 push @parts, $tag;
172 }
173  
174 # XXX Shouldn't the RE allow both, ID and class?
175 291 ▶ if ($id_class eq '#') { # ID
Branch: 2 branches
BranchTrueFalseTotal
if ($id_class eq '#') { } 21 270 291
elsif ($id_class eq '.') { } 27 243 270
176 21 push @parts, "[\@id='$name']";
177 } elsif ($id_class eq '.') { # class
178 27 push @parts, "[contains(concat(' ', normalize-space(\@class), ' '), ' $name ')]";
179 };
180 };
181  
182 # Match attribute selectors
183 291 ▶ if ($rule =~ s/$reg->{attr2}//) {
Branch: 3 branches
BranchTrueFalseTotal
if ($rule =~ s/$reg->{'attr2'}//) { } 23 268 291
elsif ($rule =~ s/$reg->{'attr1'}//) { } 6 262 268
elsif ($rule =~ /$reg->{'badattr'}/) { } 14 248 262
184 23 push @parts, "[", convert_attribute_match( $1, $2, $^N ), "]";
185 } elsif ($rule =~ s/$reg->{attr1}//) {
186 # If we have no tag output yet, write the tag:
187 6 ▶ if (! $wrote_tag++) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($wrote_tag++) 0 6 6
188 0 push @parts, '*';
189 }
190 6 push @parts, "[\@$1]";
191 } elsif ($rule =~ $reg->{badattr}) {
192 14 Carp::croak "Invalid attribute-value selector '$rule'";
193 }
194  
195 # Match negation
196 277 ▶ if ($rule =~ s/$reg->{attrN}//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rule =~ s/$reg->{"attrN"}//) 14 263 277
197 # Now we parse the rest, and after parsing the subexpression
198 # has stopped, we must find a matching closing parenthesis:
199 14 ▶ if ($rule =~ s/$reg->{attr2}//) {
Branch: 3 branches
BranchTrueFalseTotal
if ($rule =~ s/$reg->{'attr2'}//) { } 7 7 14
elsif ($rule =~ s/$reg->{'attr1'}//) { } 1 6 7
elsif ($rule =~ /$reg->{'badattr'}/) { } 0 6 6
200 7 push @parts, "[not(", convert_attribute_match( $1, $2, $^N ), ")]";
201 } elsif ($rule =~ s/$reg->{attr1}//) {
202 1 push @parts, "[not(\@$1)]";
203 } elsif ($rule =~ /$reg->{badattr}/) {
204 0 Carp::croak "Invalid negated attribute-value selector ':not($rule)'";
205 } else {
206 6 my( $new_parts, $leftover ) = $self->consume( $rule, %parms );
207 6 shift @$new_parts; # remove '//'
208 6 my $xpath = join '', @$new_parts;
209  
210 6 push @parts, "[not(self::$xpath)]";
211 6 $rule = $leftover;
212 }
213 14 ▶ $rule =~ s!^\s*\)!!
Branch: 1 branch
BranchTrueFalseTotal
$rule =~ s/^\s*\)// or die "Unbalanced parentheses at '${rule}'" 14 0 14
214 or die "Unbalanced parentheses at '$rule'";
215 }
216  
217 # Ignore pseudoclasses/pseudoelements
218 277 ▶ while ($rule =~ s/$reg->{pseudo}//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rule =~ s/$reg->{"pseudo"}//) 84 277 361
219 84 ▶ if ( my @expr = $self->parse_pseudo($1, \$rule) ) {
Branch: 17 branches
BranchTrueFalseTotal
if (my(@expr) = $self->parse_pseudo($1, \$rule)) { } 3 81 84
elsif ($1 eq 'disabled') { } 1 80 81
elsif ($1 eq 'checked') { } 1 79 80
elsif ($1 eq 'selected') { } 1 78 79
elsif ($1 eq 'text') { } 0 78 78
elsif ($1 eq 'first-child') { } 13 65 78
elsif ($1 eq 'last-child') { } 5 60 65
elsif ($1 eq 'only-child') { } 2 58 60
elsif ($1 =~ /^lang\($/) { } 11 47 58
elsif ($1 =~ /^nth-child\(\s*$/) { } 17 30 47
elsif ($1 =~ /^nth-last-child\(\s*$/) { } 4 26 30
elsif ($1 =~ /^first-of-type\s*$/) { } 1 25 26
elsif ($1 =~ /^nth-of-type\(\s*$/) { } 4 21 25
elsif ($1 =~ /^last-of-type$/) { } 2 19 21
elsif ($1 =~ /^contains\($/) { } 13 6 19
elsif ($1 eq 'root') { } 3 3 6
elsif ($1 eq 'empty') { } 3 0 3
220 3 push @parts, @expr;
221 } elsif ( $1 eq 'disabled') {
222 1 push @parts, '[@disabled]';
223 } elsif ( $1 eq 'checked') {
224 1 push @parts, '[@checked]';
225 } elsif ( $1 eq 'selected') {
226 1 push @parts, '[@selected]';
227 } elsif ( $1 eq 'text') {
228 0 push @parts, '*[@type="text"]';
229 } elsif ( $1 eq 'first-child') {
230 # Translates to :nth-child(1)
231 13 push @parts, nth_child(1);
232 } elsif ( $1 eq 'last-child') {
233 5 push @parts, nth_last_child(1);
234 } elsif ( $1 eq 'only-child') {
235 2 push @parts, nth_child(1), nth_last_child(1);
236 } elsif ($1 =~ /^lang\($/) {
237 11 ▶ $rule =~ s/\s*([\w\-]+)\s*\)//
Branch: 1 branch
BranchTrueFalseTotal
$rule =~ s/\s*([\w\-]+)\s*\)// or Carp::croak("Can't parse language part from $rule") 11 0 11
238 or Carp::croak "Can't parse language part from $rule";
239 11 push @parts, "[\@xml:lang='$1' or starts-with(\@xml:lang, '$1-')]";
240 } elsif ($1 =~ /^nth-child\(\s*$/) {
241 17 my( $A, $B ) = consume_An_plus_B(\$rule);
242 17 push @parts, nth_child($A, $B);
243 } elsif ($1 =~ /^nth-last-child\(\s*$/) {
244 4 my( $A, $B ) = consume_An_plus_B(\$rule);
245 4 push @parts, nth_last_child($A, $B);
246 } elsif ($1 =~ /^first-of-type\s*$/) {
247 1 my $type = $parts[-1];
248 1 push @parts, _generate_child('preceding', $type, 0, 1);
249  
250 } elsif ($1 =~ /^nth-of-type\(\s*$/) {
251 4 my( $A, $B ) = consume_An_plus_B(\$rule);
252 4 my $type = $parts[-1];
253 4 push @parts, _generate_child('preceding', $type, $A, $B);
254  
255 } elsif ($1 =~ /^last-of-type$/) {
256 2 push @parts, "[last()]";
257  
258 # Err?! This one does not really exist in the CSS spec...
259 # Why did I add this?
260 } elsif ($1 =~ /^contains\($/) {
261 13 ▶ if( $rule =~ s/^\s*"([^"]*)"\s*\)// ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($rule =~ s/^\s*"([^"]*)"\s*\)//) { } 11 2 13
elsif ($rule =~ s/^\s*'([^']*)'\s*\)//) { } 2 0 2
262 11 push @parts, qq{[text()[contains(string(.),"$1")]]};
263 } elsif( $rule =~ s/^\s*'([^']*)'\s*\)// ) {
264 2 push @parts, qq{[text()[contains(string(.),"$1")]]};
265 } else {
266 0 return( \@parts, $rule );
267 #die "Malformed string in :contains(): '$rule'";
268 };
269 } elsif ( $1 eq 'root') {
270 # This will give surprising results if you do E > F:root
271 3 $parts[$root_index] = $root;
272 } elsif ( $1 eq 'empty') {
273 3 push @parts, "[not(* or text())]";
274 } else {
275 0 Carp::croak "Can't translate '$1' pseudo-class";
276 }
277 }
278  
279 # Match combinators (whitespace, >, + and ~)
280 277 ▶ if ($rule =~ s/$reg->{combinator}//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rule =~ s/$reg->{"combinator"}//) 53 224 277
281 53 my $match = $1;
282 53 ▶ if ($match =~ />/) {
Branch: 4 branches
BranchTrueFalseTotal
if ($match =~ />/) { } 13 40 53
elsif ($match =~ /\+/) { } 5 35 40
elsif ($match =~ /\~/) { } 11 24 35
elsif ($match =~ /^\s*$/) { } 24 0 24
283 13 push @parts, "/";
284 } elsif ($match =~ /\+/) {
285 5 push @parts, "/following-sibling::*[1]/self::";
286 } elsif ($match =~ /\~/) {
287 11 push @parts, "/following-sibling::";
288 } elsif ($match =~ /^\s*$/) {
289 24 push @parts, "//"
290 } else {
291 0 die "Weird combinator '$match'"
292 }
293  
294 # new context
295 53 undef $wrote_tag;
296 }
297  
298 # Match commas
299 277 ▶ if ($rule =~ s/$reg->{comma}//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rule =~ s/$reg->{"comma"}//) 8 269 277
300 8 push @parts, " | ", "$root/"; # ending one rule and beginning another
301 8 $root_index = $#parts;
302 8 undef $wrote_tag;
303 }
304 }
305 175 return \@parts, $rule
306 }
307  
308 184 ▶sub to_xpath {
Subroutine to_xpath: called CC=3 SCAR=11.2
Pod documented
309 184 my $self = shift;
310 184 ▶ my $rule = $self->{expression} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $rule = $self->{"expression"} or return 184 0 184
311 184 my %parms = @_;
312  
313 184 my($result,$leftover) = $self->consume( $rule, %parms );
314 170 ▶ $leftover
Branch: 1 branch
BranchTrueFalseTotal
$leftover and die "Invalid rule, couldn't parse '${leftover}'" 0 170 170
315 and die "Invalid rule, couldn't parse '$leftover'";
316 170 return join '', @$result;
317  
318 }
319  
320 81 ▶sub parse_pseudo {
Subroutine parse_pseudo: called CC=1 SCAR=0.0
Pod documented
321 # nop
322 }
323  
324 8 1;
325 __END__
↑ Summary