lib/HTML/Object/XPath.pm

filter: Statementstmt 75.1% 353 / 470 Branchbran 63.9% 119 / 186 Conditioncond 80.6% 25 / 31 MC/DCmcdc 50.0% 7 / 14 Subroutinesub 81.0% 60 / 74 Podpod 100% 26 / 26 totaltotal 73.6% 590 / 801 CC 105 SCAR 31.3
CC
105
Cov
72%
_step
118.1
findnodes_as_string
20.0
findnodes_as_strings
20.0
CRAP 22.8
« lib/HTML/Object/DOM/TextTrackList.pm ↑ Summary lib/HTML/Object/DOM/Implementation.pm »
1 ##----------------------------------------------------------------------------
2 ## HTML Object - ~/lib/HTML/Object/XPath.pm
3 ## Version v0.2.0
4 ## Copyright(c) 2021 DEGUEST Pte. Ltd.
5 ## Author: Jacques Deguest <jack@deguest.jp>
6 ## Created 2021/12/04
7 ## Modified 2022/09/18
8 ## All rights reserved.
9 ##
10 ##
11 ## This program is free software; you can redistribute it and/or modify it
12 ## under the same terms as Perl itself.
13 ##----------------------------------------------------------------------------
14 package HTML::Object::XPath;
15 BEGIN
16 {
17 14 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 14 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 14 ▶ warnings::register_categories( 'HTML::Object' );
Subroutine BEGIN: called CC=1 SCAR=0.0
20 14 ▶ use parent qw( Module::Generic );
Subroutine BEGIN: called CC=1 SCAR=0.0
21 14 use vars qw(
22 $CACHE $AXES $AXES_KEYS $NC_NAME_RE $QNAME_RE $NC_WILD_RE $QN_WILD_RE
23 $NODE_TYPE_RE $AXIS_NAME_RE $NUMBER_RE $LITERAL_RE $REGEXP_RE $REGEXP_MOD_RE
24 $BASE_CLASS $VERSION
25 14 ▶ );
Subroutine BEGIN: called CC=1 SCAR=0.0
26 14 ▶ use HTML::Object::XPath::Step;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 14 ▶ use HTML::Object::XPath::Expr;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 14 ▶ use HTML::Object::XPath::Function;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 14 ▶ use HTML::Object::XPath::LocationPath;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 14 ▶ use HTML::Object::XPath::Variable;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 14 ▶ use HTML::Object::XPath::Literal;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 14 ▶ use HTML::Object::XPath::Number;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 14 ▶ use HTML::Object::XPath::NodeSet;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 14 ▶ use HTML::Object::XPath::Root;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 14 our $VERSION = 'v0.2.0';
36 14 our $CACHE = {};
37 # Axis name to principal node type mapping
38 14 our $AXES =
39 {
40 'ancestor' => 'element',
41 'ancestor-or-self' => 'element',
42 'attribute' => 'attribute',
43 'namespace' => 'namespace',
44 'child' => 'element',
45 'descendant' => 'element',
46 'descendant-or-self' => 'element',
47 'following' => 'element',
48 'following-sibling' => 'element',
49 'parent' => 'element',
50 'preceding' => 'element',
51 'preceding-sibling' => 'element',
52 'self' => 'element',
53 };
54 14 my $AXES_KEYS = join( '|', keys( %$AXES ) );
55 14 our $NC_NAME_RE = qr/([A-Za-z_][\w\.\-]*)/;
56 14 our $QNAME_RE = qr/(${NC_NAME_RE}:)?${NC_NAME_RE}/;
57 14 our $NC_WILD_RE = qr/${NC_NAME_RE}:\*/;
58 14 our $QN_WILD_RE = qr/\*/;
59 14 our $NODE_TYPE_RE = qr/((text|comment|processing-instruction|node)\(\))/;
60 14 our $AXIS_NAME_RE = qr/(${AXES_KEYS})::/;
61 14 our $NUMBER_RE = qr/\d+(\.\d*)?|\.\d+/;
62 14 our $LITERAL_RE = qr/\"[^\"]*\"|\'[^\']*\'/;
63 14 our $REGEXP_RE = qr{(?:m?/(?:\\.|[^/])*/)};
64 14 our $REGEXP_MOD_RE = qr{(?:[imsx]+)};
65 14 our $BASE_CLASS = __PACKAGE__;
66 };
67  
68 14 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
69 14 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
70  
71 sub init
72 {
73 11 ▶ my $self = shift( @_ );
Subroutine init: called CC=2 SCAR=6.9
Pod documented
74 11 $self->{_init_strict_use_sub} = 1;
75 11 ▶ $self->SUPER::init( @_ ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->SUPER::init(@_) or return $self->pass_error 0 11 11
76 11 $self->{context_pos} = undef; # 1 based position in array context
77 11 $self->{context_set} = $self->new_nodeset;
78 11 $self->{context_size} = 0; # total size of context
79 11 $self->clear_namespaces();
80 11 $self->{cache} = {};
81 11 $self->{direction} = 'forward';
82 11 $self->{namespaces} = {};
83 11 $self->{vars} = {};
84 11 $self->{_tokpos} = 0;
85 11 return( $self );
86 }
87  
88 sub clear_namespaces
89 {
90 11 ▶ my $self = shift( @_ );
Subroutine clear_namespaces: called CC=1 SCAR=0.0
Pod documented
91 11 $self->{uses_namespaces} = 0;
92 11 $self->{namespaces} = {};
93 11 return( $self );
94 }
95  
96 sub exists
97 {
98 0 ▶ my $self = shift( @_ );
Subroutine exists: not called CC=3 SCAR=24.8
Pod documented
99 0 my( $path, $context ) = @_;
100 0 ▶ $self = '/' if( !defined( $self ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self 0 0 0
101 0 my @nodeset = $self->findnodes( $path, $context );
102 0 ▶ return( scalar( @nodeset ) ? 1 : 0 );
Branch: 1 branch
BranchTrueFalseTotal
scalar @nodeset ? : 0 0 0
103 }
104  
105 sub find
106 {
107 66 ▶ my $self = shift( @_ );
Subroutine find: called CC=2 SCAR=6.9
Pod documented
108 # xpath expression and $context is a HTML::Object::Element
109 66 my( $path, $context ) = @_;
110 # _parse returns an HTML::Object::XPath::Expr object
111 66 my $parsed_path = $self->_parse( $path );
112 # $parsed_path is an HTML:: Object::XPath::Expr object
113 # $results could be a HTML::Object::XPath::NodeSet or something else like HTML::Object::XPath::Number
114 66 my $results = $parsed_path->evaluate( $context );
115 63 ▶ if( $self->_is_a( $results, 'HTML::Object::XPath::NodeSet' ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_a($results, 'HTML::Object::XPath::NodeSet')) { } 50 13 63
116 {
117 50 return( $results->sort->remove_duplicates );
118 }
119 else
120 {
121 13 return( $results );
122 }
123 }
124  
125 sub findnodes
126 {
127 30 ▶ my $self = shift( @_ );
Subroutine findnodes: called CC=4 SCAR=16.4
Pod documented
128 30 my( $path, $context ) = @_;
129  
130 30 my $results = $self->find( $path, $context );
131  
132 30 ▶ if( $self->_is_a( $results => 'HTML::Object::XPath::NodeSet' ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_a($results, 'HTML::Object::XPath::NodeSet')) { } 30 0 30
133 {
134 30 ▶ return( wantarray() ? $results->get_nodelist : $results );
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 30 0 30
135 }
136 # result should be SCALAR
137 else
138 {
139 0 ▶ return( wantarray() ? $self->new_nodeset( $results ) : $results );
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
140 }
141 }
142  
143 sub findnodes_as_string
144 {
145 0 ▶ my $self = shift( @_ );
Subroutine findnodes_as_string: not called CC=4 SCAR=30.0
Pod documented
146 0 my( $path, $context ) = @_;
147  
148 0 my $results = $self->find( $path, $context );
149 0 ▶ if( $self->_is_a( $results => 'HTML::Object::XPath::NodeSet' ) )
Branch: 3 branches
BranchTrueFalseTotal
if ($self->_is_a($results, 'HTML::Object::XPath::NodeSet')) { } 0 0 0
elsif ($self->_is_a($results, 'HTML::Object::XPath::Boolean')) { } 0 0 0
elsif ($self->_is_a($results, 'HTML::Object::XPath::Node')) { } 0 0 0
150 {
151 0 return( join( '', map{ $_->toString } $results->get_nodelist ) );
152 }
153 elsif( $self->_is_a( $results => 'HTML::Object::XPath::Boolean' ) )
154 {
155 # to behave like XML::LibXML
156 0 return( '' );
157 }
158 elsif( $self->_is_a( $results => 'HTML::Object::XPath::Node' ) )
159 {
160 0 return( $results->toString );
161 }
162 else
163 {
164 0 return( $self->_xml_escape_text( $results->value ) );
165 }
166 }
167  
168 sub findnodes_as_strings
169 {
170 0 ▶ my $self = shift( @_ );
Subroutine findnodes_as_strings: not called CC=4 SCAR=30.0
Pod documented
171 0 my( $path, $context ) = @_;
172 0 my $results = $self->find( $path, $context );
173  
174 0 ▶ if( $self->_is_a( $results => 'HTML::Object::XPath::NodeSet' ) )
Branch: 3 branches
BranchTrueFalseTotal
if ($self->_is_a($results, 'HTML::Object::XPath::NodeSet')) { } 0 0 0
elsif ($self->_is_a($results, 'HTML::Object::XPath::Boolean')) { } 0 0 0
elsif ($self->_is_a($results, 'HTML::Object::XPath::Node')) { } 0 0 0
175 {
176 0 return( map{ $_->getValue } $results->get_nodelist );
177 }
178 elsif( $self->_is_a( $results => 'HTML::Object::XPath::Boolean' ) )
179 {
180 # to behave like XML::LibXML
181 0 return( () );
182 }
183 elsif( $self->_is_a( $results => 'HTML::Object::XPath::Node' ) )
184 {
185 0 return( $results->getValue );
186 }
187 else
188 {
189 0 return( $self->_xml_escape_text( $results->value ) );
190 }
191 }
192  
193 sub findvalue
194 {
195 31 ▶ my $self = shift( @_ );
Subroutine findvalue: called CC=2 SCAR=6.9
Pod documented
196 31 my( $path, $context ) = @_;
197 31 my $results = $self->find( $path, $context );
198 31 ▶ if( $self->_is_a( $results => 'HTML::Object::XPath::NodeSet' ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_a($results, "HTML::Object::XPath::NodeSet")) 18 13 31
199 {
200 18 return( $results->to_final_value );
201 }
202 #{ return $results->to_literal; }
203 13 return( $results->value );
204 }
205  
206 sub findvalues
207 {
208 5 ▶ my $self = shift( @_ );
Subroutine findvalues: called CC=2 SCAR=7.2
Pod documented
209 5 my( $path, $context ) = @_;
210 5 my $results = $self->find( $path, $context );
211 2 ▶ if( $self->_is_a( $results => 'HTML::Object::XPath::NodeSet' ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_a($results, "HTML::Object::XPath::NodeSet")) 2 0 2
212 {
213 2 return( $results->string_values );
214 }
215 0 return( $results->string_value );
216 }
217  
218 sub get_namespace
219 {
220 0 ▶ my $self = shift( @_ );
Subroutine get_namespace: not called CC=3 SCAR=24.8
Pod documented
221 0 my( $prefix, $node ) = @_;
222  
223 my $ns = $node
224 ? $node->getNamespace( $prefix )
225 : $self->{uses_namespaces}
226 0 ▶ ? $self->{namespaces}->{ $prefix }
Branch: 2 branches
BranchTrueFalseTotal
$node ? : 0 0 0
$self->{'uses_namespaces'} ? : 0 0 0
227 : $prefix;
228 0 return( $ns );
229 }
230  
231 sub get_var
232 {
233 0 ▶ my $self = shift( @_ );
Subroutine get_var: not called CC=1 SCAR=6.9
Pod documented
234 0 my $var = shift( @_ );
235 0 $self->{vars}->{ $var };
236 }
237  
238 sub matches
239 {
240 7 ▶ my $self = shift( @_ );
Subroutine matches: called CC=2 SCAR=6.9
Pod documented
241 7 my( $node, $path, $context ) = @_;
242 7 my @nodes = $self->findnodes( $path, $context );
243 7 ▶ return(1) if( grep{ "$node" eq "$_" } @nodes );
Branch: 1 branch
BranchTrueFalseTotal
if grep {"$node" eq "$_";} @nodes 5 2 7
244 2 return;
245 }
246  
247 66 ▶sub namespaces { return( shift->_set_get_hash_as_mix_object( 'namespaces', @_ ) ); }
Subroutine namespaces: called CC=1 SCAR=0.0
Pod documented
248  
249 240 ▶sub new_expr { return( shift->_class_for( 'Expr' )->new( @_ ) ); }
Subroutine new_expr: called CC=1 SCAR=0.0
Pod documented
250  
251 22 ▶sub new_function { return( shift->_class_for( 'Function' )->new( @_ ) ); }
Subroutine new_function: called CC=1 SCAR=0.0
Pod documented
252  
253 26 ▶sub new_literal { return( shift->_class_for( 'Literal' )->new( @_ ) ); }
Subroutine new_literal: called CC=1 SCAR=0.0
Pod documented
254  
255 86 ▶sub new_location_path { return( shift->_class_for( 'LocationPath' )->new( @_ ) ); }
Subroutine new_location_path: called CC=1 SCAR=0.0
Pod documented
256  
257 11 ▶sub new_nodeset { return( shift->_class_for( 'NodeSet' )->new( @_ ) ); }
Subroutine new_nodeset: called CC=1 SCAR=0.0
Pod documented
258  
259 12 ▶sub new_number { return( shift->_class_for( 'Number' )->new( @_ ) ); }
Subroutine new_number: called CC=1 SCAR=0.0
Pod documented
260  
261 46 ▶sub new_root { return( shift->_class_for( 'Root' )->new( @_ ) ); }
Subroutine new_root: called CC=1 SCAR=0.0
Pod documented
262  
263 161 ▶sub new_step { return( shift->_class_for( 'Step' )->new( @_ ) ); }
Subroutine new_step: called CC=1 SCAR=0.0
Pod documented
264  
265 0 ▶sub new_variable { return( shift->_class_for( 'Variable' )->new( @_ ) ); }
Subroutine new_variable: not called CC=1 SCAR=6.9
Pod documented
266  
267 0 ▶sub parse { return( shift->_parse( @_ ) ); }
Subroutine parse: not called CC=1 SCAR=6.9
Pod documented
268  
269 sub set_namespace
270 {
271 0 ▶ my $self = shift( @_ );
Subroutine set_namespace: not called CC=1 SCAR=6.9
Pod documented
272 0 my( $prefix, $expanded ) = @_;
273 0 $self->{uses_namespaces} = 1;
274 0 $self->{namespaces}{ $prefix } = $expanded;
275 }
276  
277 sub set_strict_namespaces
278 {
279 0 ▶ my( $self, $strict ) = @_;
Subroutine set_strict_namespaces: not called CC=1 SCAR=6.9
Pod documented
280 0 $self->{strict_namespaces} = $strict;
281 }
282  
283 sub set_var
284 {
285 0 ▶ my $self = shift( @_ );
Subroutine set_var: not called CC=1 SCAR=6.9
Pod documented
286 0 my $var = shift( @_ );
287 0 my $val = shift( @_ );
288 0 $self->{vars}->{ $var } = $val;
289 }
290  
291 sub _analyze
292 {
293 54 ▶ my $self = shift( @_ );
Subroutine _analyze: called CC=1 SCAR=0.0
294 # Array object
295 54 my $tokens = shift( @_ );
296 # lexical analysis
297 54 return( $self->_expr( $tokens ) );
298 }
299  
300 sub _arguments
301 {
302 22 ▶ my( $self, $tokens ) = @_;
Subroutine _arguments: called CC=3 SCAR=11.1
303 22 my $args = $self->new_array;
304 22 ▶ if( $tokens->[ $self->{_tokpos} ] eq ')' )
Branch: 1 branch
BranchTrueFalseTotal
if ($$tokens[$self->{"_tokpos"}] eq ")") 0 22 22
305 {
306 0 return( $args );
307 }
308  
309 22 $args->push( $self->_expr( $tokens ) );
310 22 ▶ while( $self->_match( $tokens, qr/\,/ ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_match($tokens, qr/\,/)) 2 22 24
311 {
312 2 $args->push( $self->_expr( $tokens ) );
313 }
314 22 return( $args );
315 }
316  
317 sub _class_for
318 {
319 772 ▶ my( $self, $mod ) = @_;
Subroutine _class_for: called CC=3 SCAR=11.3
320 772 eval( "require ${BASE_CLASS}\::${mod};" );
321 772 ▶ die( $@ ) if( $@ );
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 772 772
322 # ${"${BASE_CLASS}\::${mod}\::DEBUG"} = $self->debug;
323 772 ▶ eval( "\$${BASE_CLASS}\::${mod}\::DEBUG = " . ( $self->debug // 0 ) );
Condition50%1/2
$self->debug // 0
l!l
0772
MC/DC0%0/1
$self->debug // 0
$self->debug
Truth table50%1/2
A
A= $self->debug
Aresult
11
00
324 772 return( "${BASE_CLASS}::${mod}" );
325 }
326  
327 sub _expr
328 {
329 111 ▶ my( $self, $tokens ) = @_;
Subroutine _expr: called CC=1 SCAR=0.0
330 # $tokens are an array object of xpath expression token
331 # Returning an HTML::Object::XPath::Expr object
332 111 return( $self->_expr_or( $tokens ) );
333 }
334  
335 sub _expr_additive
336 {
337 139 ▶ my( $self, $tokens ) = @_;
Subroutine _expr_additive: called CC=2 SCAR=10.3
338  
339 139 my $expr = $self->_expr_multiplicative( $tokens );
340 139 ▶ while( $self->_match( $tokens, qr/[\+\-]/ ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_match($tokens, qr/[\+\-]/)) 0 139 139
341 {
342 0 my $add_expr = $self->new_expr( $self );
343 0 $add_expr->set_lhs( $expr );
344 0 $add_expr->set_op( $self->{_curr_match} );
345  
346 0 my $rhs = $self->_expr_multiplicative( $tokens );
347  
348 0 $add_expr->set_rhs( $rhs );
349 0 $expr = $add_expr;
350 }
351 139 return( $expr );
352 }
353  
354 sub _expr_and
355 {
356 112 ▶ my( $self, $tokens ) = @_;
Subroutine _expr_and: called CC=2 SCAR=6.9
357 112 my $expr = $self->_expr_match( $tokens );
358 112 ▶ while( $self->_match( $tokens, 'and' ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_match($tokens, "and")) 2 112 114
359 {
360 2 my $and_expr = $self->new_expr( $self );
361 2 $and_expr->set_lhs( $expr );
362 2 $and_expr->set_op( 'and' );
363 2 my $rhs = $self->_expr_match( $tokens );
364 2 $and_expr->set_rhs( $rhs );
365 2 $expr = $and_expr;
366 }
367 112 return( $expr );
368 }
369  
370 sub _expr_equality
371 {
372 117 ▶ my( $self, $tokens ) = @_;
Subroutine _expr_equality: called CC=2 SCAR=6.9
373  
374 117 my $expr = $self->_expr_relational( $tokens );
375 117 ▶ while( $self->_match( $tokens, qr/!?=/ ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_match($tokens, qr/!?=/)) 21 117 138
376 {
377 21 my $eq_expr = $self->new_expr( $self );
378 21 $eq_expr->set_lhs( $expr );
379 21 $eq_expr->set_op( $self->{_curr_match} );
380  
381 21 my $rhs = $self->_expr_relational( $tokens );
382  
383 21 $eq_expr->set_rhs( $rhs );
384 21 $expr = $eq_expr;
385 }
386 117 return( $expr );
387 }
388  
389 sub _expr_filter
390 {
391 63 ▶ my( $self, $tokens ) = @_;
Subroutine _expr_filter: called CC=2 SCAR=6.9
392  
393 63 my $expr = $self->_expr_primary( $tokens );
394 63 ▶ while( $self->_match( $tokens, qr/\[/ ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_match($tokens, qr/\[/)) 2 63 65
395 {
396 # really PredicateExpr...
397 2 $expr->push_predicate( $self->_expr( $tokens ) );
398 2 $self->_match( $tokens, qr/\]/, 1 );
399 }
400 63 return( $expr );
401 }
402  
403 sub _expr_match
404 {
405 114 ▶ my( $self, $tokens ) = @_;
Subroutine _expr_match: called CC=2 SCAR=6.9
406  
407 114 my $expr = $self->_expr_equality( $tokens );
408  
409 114 ▶ while( $self->_match( $tokens, qr/[=!]~/ ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_match($tokens, qr/[=!]~/)) 3 114 117
410 {
411 3 my $match_expr = $self->new_expr( $self );
412 3 $match_expr->set_lhs( $expr );
413 3 $match_expr->set_op( $self->{_curr_match} );
414  
415 3 my $rhs = $self->_expr_equality( $tokens );
416  
417 3 $match_expr->set_rhs( $rhs );
418 3 $expr = $match_expr;
419 }
420 114 return( $expr );
421 }
422  
423 sub _expr_multiplicative
424 {
425 139 ▶ my( $self, $tokens ) = @_;
Subroutine _expr_multiplicative: called CC=2 SCAR=10.3
426  
427 139 my $expr = $self->_expr_unary( $tokens );
428 139 ▶ while( $self->_match( $tokens, qr/(\*|div|mod)/ ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_match($tokens, qr/(\*|div|mod)/)) 0 139 139
429 {
430 0 my $mult_expr = $self->new_expr( $self );
431 0 $mult_expr->set_lhs( $expr );
432 0 $mult_expr->set_op( $self->{_curr_match} );
433  
434 0 my $rhs = $self->_expr_unary( $tokens );
435  
436 0 $mult_expr->set_rhs( $rhs );
437 0 $expr = $mult_expr;
438 }
439 139 return( $expr );
440 }
441  
442 sub _expr_or
443 {
444 111 ▶ my( $self, $tokens ) = @_;
Subroutine _expr_or: called CC=2 SCAR=6.9
445  
446 111 my $expr = $self->_expr_and( $tokens );
447 111 ▶ while( $self->_match( $tokens, 'or' ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_match($tokens, "or")) 1 111 112
448 {
449 1 my $or_expr = $self->new_expr( $self );
450 1 $or_expr->set_lhs( $expr );
451 1 $or_expr->set_op( 'or' );
452  
453 1 my $rhs = $self->_expr_and( $tokens );
454  
455 1 $or_expr->set_rhs( $rhs );
456 1 $expr = $or_expr;
457 }
458 111 return( $expr );
459 }
460  
461 sub _expr_path
462 {
463 139 ▶ my( $self, $tokens ) = @_;
Subroutine _expr_path: called CC=5 SCAR=16.1
464 # _expr_path is _location_path | _expr_filter | _expr_filter '//?' _relative_location_path
465 # Since we are being predictive we need to find out which function to call next, then.
466 # LocationPath either starts with "/", "//", ".", ".." or a proper Step.
467 139 my $expr = $self->new_expr( $self );
468  
469 # $test is a fragment of the xpath initially provided and broken down into bits by
470 # HTML::Object::XPath::_tokenize
471 139 my $test = $tokens->[ $self->{_tokpos} ];
472  
473 # Test for AbsoluteLocationPath and AbbreviatedRelativeLocationPath
474 139 ▶ if( $test =~ /^(\/\/?|\.\.?)$/ )
Branch: 2 branches
BranchTrueFalseTotal
if ($test =~ m[^(//?|\.\.?)$]) { } 55 84 139
elsif ($self->_is_step($tokens)) { } 21 63 84
475 {
476 # LocationPath
477 55 $expr->set_lhs( $self->_location_path( $tokens ) );
478 }
479 # Test for AxisName::...
480 elsif( $self->_is_step( $tokens ) )
481 {
482 21 $expr->set_lhs( $self->_location_path( $tokens ) );
483 }
484 else
485 {
486 # Not a LocationPath
487 # Use _expr_filter instead:
488 63 $expr = $self->_expr_filter( $tokens );
489 63 ▶ if( $self->_match( $tokens, qr/\/\/?/ ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_match($tokens, qr"//?")) 10 53 63
490 {
491 10 my $loc_path = $self->new_location_path();
492 10 $loc_path->push( $expr );
493 10 ▶ if( $self->{_curr_match} eq '//' )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_curr_match"} eq "//") 3 7 10
494 {
495 3 $loc_path->push( $self->new_step( $self, 'descendant-or-self', $self->_class_for( 'Step' )->TEST_NT_NODE ) );
496 }
497 10 $loc_path->push( $self->_relative_location_path( $tokens ) );
498 10 my $new_expr = $self->new_expr( $self );
499 10 $new_expr->set_lhs( $loc_path );
500 10 return( $new_expr );
501 }
502 }
503 129 return( $expr );
504 }
505  
506 sub _expr_primary
507 {
508 63 ▶ my( $self, $tokens ) = @_;
Subroutine _expr_primary: called CC=8 SCAR=21.4
509  
510 63 my $expr = $self->new_expr( $self );
511  
512 63 ▶ if( $self->_match( $tokens, $LITERAL_RE ) )
Branch: 6 branches
BranchTrueFalseTotal
if ($self->_match($tokens, $HTML::Object::XPath::LITERAL_RE)) { } 23 40 63
elsif ($self->_match($tokens, qr/$HTML::Object::XPath::REGEXP_RE$HTML::Object::XPath::REGEXP_MOD_RE?/)) { } 3 37 40
elsif ($self->_match($tokens, $HTML::Object::XPath::NUMBER_RE)) { } 12 25 37
elsif ($self->_match($tokens, qr/\(/)) { } 3 22 25
elsif ($self->_match($tokens, qr/\$$HTML::Object::XPath::QNAME_RE/)) { } 0 22 22
elsif ($self->_match($tokens, $HTML::Object::XPath::QNAME_RE)) { } 22 0 22
513 {
514 # new Literal with $self->{_curr_match}...
515 23 $self->{_curr_match} =~ m/^(["'])(.*)\1$/;
516 23 $expr->set_lhs( $self->new_literal( $2 ) );
517 }
518 elsif( $self->_match( $tokens, qr/${REGEXP_RE}${REGEXP_MOD_RE}?/ ) )
519 {
520 # new Literal with $self->{_curr_match} turned into a regexp...
521 3 my( $regexp, $mod)= $self->{_curr_match} =~ m{($REGEXP_RE)($REGEXP_MOD_RE?)};
522 3 $regexp =~ s{^m?s*/}{};
523 3 $regexp =~ s{/$}{};
524 # move the mods inside the regexp
525 3 ▶ if( $mod )
Branch: 1 branch
BranchTrueFalseTotal
if ($mod) 0 3 3
526 {
527 0 $regexp =~ qr/(?$mod:$regexp)/;
528 }
529 3 $expr->set_lhs( $self->new_literal( $regexp ) );
530 }
531 elsif( $self->_match( $tokens, $NUMBER_RE ) )
532 {
533 # new Number with $self->{_curr_match}...
534 12 $expr->set_lhs( $self->new_number( $self->{_curr_match} ) );
535 }
536 elsif( $self->_match( $tokens, qr/\(/ ) )
537 {
538 3 $expr->set_lhs( $self->_expr( $tokens ) );
539 3 $self->_match( $tokens, qr/\)/, 1 );
540 }
541 elsif( $self->_match( $tokens, qr/\$$QNAME_RE/ ) )
542 {
543 # new Variable with $self->{_curr_match}...
544 0 $self->{_curr_match} =~ /^\$(.*)$/;
545 0 $expr->set_lhs( $self->new_variable( $self, $1 ) );
546 }
547 elsif( $self->_match( $tokens, $QNAME_RE ) )
548 {
549 # check match not Node_Type - done in lexer...
550 # new Function
551 22 my $func_name = $self->{_curr_match};
552 22 $self->_match( $tokens, qr/\(/, 1 );
553 22 $expr->set_lhs(
554 $self->new_function(
555 $self,
556 $func_name,
557 $self->_arguments( $tokens )
558 )
559 );
560 22 $self->_match( $tokens, qr/\)/, 1 );
561 }
562 else
563 {
564 # die "Not a _expr_primary at ", $tokens->[$self->{_tokpos}], "\n";
565 0 return( $self->error( "Not a _expr_primary at ", $tokens->[ $self->{_tokpos} ] ) );
566 }
567 63 return( $expr );
568 }
569  
570 sub _expr_relational
571 {
572 138 ▶ my( $self, $tokens ) = @_;
Subroutine _expr_relational: called CC=2 SCAR=6.9
573  
574 138 my $expr = $self->_expr_additive( $tokens );
575 138 ▶ while( $self->_match( $tokens, qr/(<|>|<=|>=)/ ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_match($tokens, qr/(<|>|<=|>=)/)) 1 138 139
576 {
577 1 my $rel_expr = $self->new_expr( $self );
578 1 $rel_expr->set_lhs( $expr );
579 1 $rel_expr->set_op( $self->{_curr_match} );
580  
581 1 my $rhs = $self->_expr_additive( $tokens );
582  
583 1 $rel_expr->set_rhs( $rhs );
584 1 $expr = $rel_expr;
585 }
586 138 return( $expr );
587 }
588  
589 sub _expr_unary
590 {
591 139 ▶ my( $self, $tokens ) = @_;
Subroutine _expr_unary: called CC=2 SCAR=10.5
592 # $tokens are an array object of expression tokens
593  
594 139 ▶ if( $self->_match( $tokens, qr/-/ ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_match($tokens, qr/-/)) { } 0 139 139
595 {
596 0 my $expr = $self->new_expr( $self );
597 0 $expr->set_lhs( $self->new_number(0) );
598 0 $expr->set_op( '-' );
599 0 $expr->set_rhs( $self->_expr_unary( $tokens ) );
600 0 return( $expr );
601 }
602 else
603 {
604 139 return( $self->_expr_union( $tokens ) );
605 }
606 }
607  
608 sub _expr_union
609 {
610 139 ▶ my( $self, $tokens ) = @_;
Subroutine _expr_union: called CC=2 SCAR=10.3
611 # $tokens are an array object of expression tokens
612  
613 139 my $expr = $self->_expr_path( $tokens );
614 139 ▶ while( $self->_match( $tokens, qr/\|/ ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_match($tokens, qr/\|/)) 0 139 139
615 {
616 0 my $un_expr = $self->new_expr( $self );
617 0 $un_expr->set_lhs( $expr );
618 0 $un_expr->set_op( '|' );
619  
620 0 my $rhs = $self->_expr_path( $tokens );
621 0 $un_expr->set_rhs( $rhs );
622 0 $expr = $un_expr;
623 }
624 139 return( $expr );
625 }
626  
627 0 ▶sub _get_context_node { return( $_[0]->{context_set}->get_node( $_[0]->{context_pos} ) ); }
Subroutine _get_context_node: not called CC=1 SCAR=6.9
628  
629 359 ▶sub _get_context_pos { return( shift->{context_pos} ); }
Subroutine _get_context_pos: called CC=1 SCAR=0.0
630  
631 359 ▶sub _get_context_set { return( shift->{context_set} ); }
Subroutine _get_context_set: called CC=1 SCAR=0.0
632  
633 0 ▶sub _get_context_size { return( shift->{context_set}->size ); }
Subroutine _get_context_size: not called CC=1 SCAR=6.9
634  
635 sub _is_step
636 {
637 89 ▶ my( $self, $tokens ) = @_;
Subroutine _is_step: called CC=9 SCAR=22.1
638 89 my $token = $tokens->[ $self->{_tokpos} ];
639 89 ▶ return unless( defined( $token ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $token 0 89 89
640  
641 # local $^W = 0;
642 89 ▶ if( ( $token eq 'processing-instruction' ) ||
Branch: 1 branch
BranchTrueFalseTotal
if ($token eq 'processing-instruction' or $token =~ /^\@($HTML::Object::XPath::NC_WILD_RE|$HTML::Object::XPath::QNAME_RE|$HTML::Object::XPath::QN_WILD_RE)$/o or $token =~ /^($HTML::Object::XPath::NC_WILD_RE|$HTML::Object::XPath::QNAME_RE|$HTML::Object::XPath::QN_WILD_RE)$/o and ($$tokens[$self->{'_tokpos'} + 1] || '') ne '(' or $token =~ /^$HTML::Object::XPath::NODE_TYPE_RE$/o or $token =~ /^$HTML::Object::XPath::AXIS_NAME_RE($HTML::Object::XPath::NC_WILD_RE|$HTML::Object::XPath::QNAME_RE|$HTML::Object::XPath::QN_WILD_RE|$HTML::Object::XPath::NODE_TYPE_RE)$/o) { } 26 63 89
Condition88%15/17
$token eq "processing-instruction" or $token =~ /^\@($HTML::Object::XPath::NC_WILD_RE|$HTML::Object::XPath::QNAME_RE|$HTML::Object::XPath::QN_WILD_RE)$/o or $token =~ /^($HTML::Object::XPath::NC_WILD_RE|$HTML::Object::XPath::QNAME_RE|$HTML::Object::XPath::QN_WILD_RE)$/o and ($$tokens[$self->{"_tokpos"} + 1] || "") ne "(" or $token =~ /^$HTML::Object::XPath::NODE_TYPE_RE$/o or $token =~ /^$HTML::Object::XPath::AXIS_NAME_RE($HTML::Object::XPath::NC_WILD_RE|$HTML::Object::XPath::QNAME_RE|$HTML::Object::XPath::QN_WILD_RE|$HTML::Object::XPath::NODE_TYPE_RE)$/o
l!l&&r!l&&!r
25163
$token eq "processing-instruction" or $token =~ /^\@($HTML::Object::XPath::NC_WILD_RE|$HTML::Object::XPath::QNAME_RE|$HTML::Object::XPath::QN_WILD_RE)$/o or $token =~ /^($HTML::Object::XPath::NC_WILD_RE|$HTML::Object::XPath::QNAME_RE|$HTML::Object::XPath::QN_WILD_RE)$/o and ($$tokens[$self->{"_tokpos"} + 1] || "") ne "(" or $token =~ /^$HTML::Object::XPath::NODE_TYPE_RE$/o
l!l&&r!l&&!r
25064
$token eq "processing-instruction" or $token =~ /^\@($HTML::Object::XPath::NC_WILD_RE|$HTML::Object::XPath::QNAME_RE|$HTML::Object::XPath::QN_WILD_RE)$/o or $token =~ /^($HTML::Object::XPath::NC_WILD_RE|$HTML::Object::XPath::QNAME_RE|$HTML::Object::XPath::QN_WILD_RE)$/o and ($$tokens[$self->{"_tokpos"} + 1] || "") ne "("
l!l&&r!l&&!r
19664
$token eq "processing-instruction" or $token =~ /^\@($HTML::Object::XPath::NC_WILD_RE|$HTML::Object::XPath::QNAME_RE|$HTML::Object::XPath::QN_WILD_RE)$/o
l!l&&r!l&&!r
01970
$token =~ /^($HTML::Object::XPath::NC_WILD_RE|$HTML::Object::XPath::QNAME_RE|$HTML::Object::XPath::QN_WILD_RE)$/o and ($$tokens[$self->{"_tokpos"} + 1] || "") ne "("
!ll&&!rl&&r
42226
$$tokens[$self->{'_tokpos'} + 1] || ''
l!l
271
MC/DC50%3/6
$token eq "processing-instruction" or $token =~ /^\@($HTML::Object::XPath::NC_WILD_RE|$HTML::Object::XPath::QNAME_RE|$HTML::Object::XPath::QN_WILD_RE)$/o or $token =~ /^($HTML::Object::XPath::NC_WILD_RE|$HTML::Object::XPath::QNAME_RE|$HTML::Object::XPath::QN_WILD_RE)$/o and ($$tokens[$self->{"_tokpos"} + 1] || "") ne "(" or $token =~ /^$HTML::Object::XPath::NODE_TYPE_RE$/o or $token =~ /^$HTML::Object::XPath::AXIS_NAME_RE($HTML::Object::XPath::NC_WILD_RE|$HTML::Object::XPath::QNAME_RE|$HTML::Object::XPath::QN_WILD_RE|$HTML::Object::XPath::NODE_TYPE_RE)$/o
$token eq "processing-instruction" $token =~ /^\@($HTML::Object::XPath::NC_WILD_RE|$HTML::Object::XPath::QNAME_RE|$HTML::Object::XPath::QN_WILD_RE)$/o $token =~ /^($HTML::Object::XPath::NC_WILD_RE|$HTML::Object::XPath::QNAME_RE|$HTML::Object::XPath::QN_WILD_RE)$/o $$tokens[$self->{'_tokpos'} + 1] $token =~ /^$HTML::Object::XPath::NODE_TYPE_RE$/o $token =~ /^$HTML::Object::XPath::AXIS_NAME_RE($HTML::Object::XPath::NC_WILD_RE|$HTML::Object::XPath::QNAME_RE|$HTML::Object::XPath::QN_WILD_RE|$HTML::Object::XPath::NODE_TYPE_RE)$/o
Truth table44%4/9
A or B or C and D or E or F
A= $token eq "processing-instruction"B= $token =~ /^\@($HTML::Object::XPath::NC_WILD_RE|$HTML::Object::XPath::QNAME_RE|$HTML::Object::XPath::QN_WILD_RE)$/oC= $token =~ /^($HTML::Object::XPath::NC_WILD_RE|$HTML::Object::XPath::QNAME_RE|$HTML::Object::XPath::QN_WILD_RE)$/oD= $$tokens[$self->{'_tokpos'} + 1]E= $token =~ /^$HTML::Object::XPath::NODE_TYPE_RE$/oF= $token =~ /^$HTML::Object::XPath::AXIS_NAME_RE($HTML::Object::XPath::NC_WILD_RE|$HTML::Object::XPath::QNAME_RE|$HTML::Object::XPath::QN_WILD_RE|$HTML::Object::XPath::NODE_TYPE_RE)$/o
ABCDEFresult
1XXXXX1
01XXXX1
0011XX1
000X1X1
00101X1
000X011
0010011
000X000
0010000
643 ( $token =~ /^\@($NC_WILD_RE|$QNAME_RE|$QN_WILD_RE)$/o ) ||
644 ( ( $token =~ /^($NC_WILD_RE|$QNAME_RE|$QN_WILD_RE)$/o ) &&
645 ( ( $tokens->[ $self->{_tokpos} + 1 ] || '' ) ne '(' ) ) ||
646 ( $token =~ /^$NODE_TYPE_RE$/o ) ||
647 ( $token =~ /^$AXIS_NAME_RE($NC_WILD_RE|$QNAME_RE|$QN_WILD_RE|$NODE_TYPE_RE)$/o )
648 )
649 {
650 26 return(1);
651 }
652 else
653 {
654 63 return;
655 }
656 }
657  
658 sub _location_path
659 {
660 76 ▶ my( $self, $tokens ) = @_;
Subroutine _location_path: called CC=5 SCAR=16.1
661 76 my $loc_path = $self->new_location_path;
662  
663 76 ▶ if( $self->_match( $tokens, qr/\// ) )
Branch: 2 branches
BranchTrueFalseTotal
if ($self->_match($tokens, qr"/")) { } 5 71 76
elsif ($self->_match($tokens, qr"//")) { } 41 30 71
664 {
665 # root
666 # push @$loc_path, HTML::Object::XPath::Root->new();
667 5 $loc_path->push( $self->new_root );
668 # Is it a valid token step?
669 5 ▶ if( $self->_is_step( $tokens ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_step($tokens)) 5 0 5
670 {
671 # push @$loc_path, $self->_relative_location_path( $tokens);
672 5 $loc_path->push( $self->_relative_location_path( $tokens ) );
673 }
674 }
675 elsif( $self->_match( $tokens, qr/\/\// ) )
676 {
677 # root
678 41 $loc_path->push( $self->new_root );
679 41 my $optimised = $self->_optimise_descendant_or_self( $tokens );
680 41 ▶ if( !$optimised )
Branch: 1 branch
BranchTrueFalseTotal
if (not $optimised) { } 18 23 41
681 {
682 18 $loc_path->push(
683 $self->new_step( $self, 'descendant-or-self', $self->_class_for( 'Step' )->TEST_NT_NODE )
684 );
685 18 $loc_path->push( $self->_relative_location_path( $tokens ) );
686 }
687 else
688 {
689 23 $loc_path->push( $optimised, $self->_relative_location_path( $tokens ) );
690 }
691 }
692 else
693 {
694 30 $loc_path->push( $self->_relative_location_path( $tokens ) );
695 }
696 76 return( $loc_path );
697 }
698  
699 sub _match
700 {
701 2239 ▶ my( $self, $tokens, $match, $fatal ) = @_;
Subroutine _match: called CC=4 SCAR=13.9
702 # Enabling this debugging section will take a lot more time, because of the
703 # $tokens->length that creates a new Module::Generic::Number every time
704 # and _match gets called a lot
705 2239 $self->{_curr_match} = '';
706 2239 ▶ return(0) unless( $self->{_tokpos} < scalar( @$tokens ) );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_tokpos"} < scalar @$tokens 541 1698 2239
707 # return(0) unless( $self->{_tokpos} < $tokens->length );
708  
709 # local $^W;
710 1698 ▶ if( $tokens->[ $self->{_tokpos} ] =~ /^$match$/ )
Branch: 1 branch
BranchTrueFalseTotal
if ($$tokens[$self->{'_tokpos'}] =~ /^$match$/) { } 319 1379 1698
711 {
712 319 $self->{_curr_match} = $tokens->[ $self->{_tokpos} ];
713 319 $self->{_tokpos}++;
714 319 return(1);
715 }
716 else
717 {
718 1379 ▶ if( $fatal )
Branch: 1 branch
BranchTrueFalseTotal
if ($fatal) { } 0 1379 1379
719 {
720 0 die( "Invalid token: ", $tokens->[$self->{_tokpos}], "\n" );
721 # return( $self->error( "Invalid token: ", $tokens->[ $self->{_tokpos} ] ) );
722 }
723 else
724 {
725 1379 return(0);
726 }
727 }
728 }
729  
730 sub _optimise_descendant_or_self
731 {
732 48 ▶ my( $self, $tokens ) = @_;
Subroutine _optimise_descendant_or_self: called CC=5 SCAR=16.1
733  
734 48 my $tokpos = $self->{_tokpos};
735  
736 # // must be followed by a Step.
737 48 ▶ if( $tokens->[ $tokpos + 1 ] && $tokens->[ $tokpos + 1 ] eq '[' )
Branch: 2 branches
BranchTrueFalseTotal
if ($$tokens[$tokpos + 1] and $$tokens[$tokpos + 1] eq '[') { } 17 31 48
elsif ($tokens->[$tokpos] =~ /^\.\.?$/) { } 0 31 31
Condition100%3/3
$$tokens[$tokpos + 1] and $$tokens[$tokpos + 1] eq "["
!ll&&!rl&&r
181317
MC/DC100%2/2
$$tokens[$tokpos + 1] and $$tokens[$tokpos + 1] eq "["
$$tokens[$tokpos + 1] $$tokens[$tokpos + 1] eq "["
Truth table100%3/3
A and B
A= $$tokens[$tokpos + 1]B= $$tokens[$tokpos + 1] eq "["
ABresult
0X0
100
111
738 {
739 # next token is a predicate
740 17 return;
741 }
742 elsif( $tokens->[ $tokpos ] =~ /^\.\.?$/ )
743 {
744 # abbreviatedStep - can't optimise.
745 0 return;
746 }
747 else
748 {
749 31 my $step = $self->_step( $tokens );
750 31 ▶ if( $step->axis ne 'child' )
Branch: 1 branch
BranchTrueFalseTotal
if ($step->axis ne "child") 1 30 31
751 {
752 # can't optimise axes other than child for now...
753 1 $self->{_tokpos} = $tokpos;
754 1 return;
755 }
756 30 $step->axis( 'descendant' );
757 30 $step->axis_method( 'axis_descendant' );
758 30 $self->{_tokpos}--;
759 30 $tokens->[ $self->{_tokpos} ] = '.';
760 30 return( $step );
761 }
762 }
763  
764 sub _parse
765 {
766 66 ▶ my $self = shift( @_ );
Subroutine _parse: called CC=3 SCAR=11.1
767 66 my $path = shift( @_ );
768  
769 # $context is something like: //*[@att2="vv"]
770 # my $context = join( '&&', $path, map { "$_=>$self->{namespaces}->{$_}" } sort keys %{$self->{namespaces}});
771 66 ▶ my $context = $self->namespaces->keys->sort->map(sub{ sprintf( '%s=>%s', $_, $self->namespaces->get( $_ ) ); })->prepend( $path )->join( '&&' );
Subroutine __ANON__: not called CC=1 SCAR=1.2
772  
773 66 ▶ return( $CACHE->{ $context } ) if( $CACHE->{ $context } );
Branch: 1 branch
BranchTrueFalseTotal
if $HTML::Object::XPath::CACHE->{$context} 12 54 66
774  
775 # my $tokens = $self->_tokenize( $path ) || return( $self->pass_error );
776 # Get an Module::Generic::Array object in return
777 54 my $tokens = $self->_tokenize( $path );
778  
779 54 $self->{_tokpos} = 0;
780 54 my $tree = $self->_analyze( $tokens );
781  
782 54 ▶ if( $self->{_tokpos} < $tokens->length )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_tokpos"} < $tokens->length) 0 54 54
783 {
784 # didn't manage to parse entire expression - throw an exception
785 0 die "Parse of expression $path failed - junk after end of expression: $tokens->[$self->{_tokpos}]";
786 # return( $self->error( "Parse of expression $path failed - junk after end of expression: $tokens->[$self->{_tokpos}]" ) );
787 }
788  
789 54 $tree->{uses_namespaces} = $self->{uses_namespaces};
790 54 $tree->{strict_namespaces} = $self->{strict_namespaces};
791  
792 54 $CACHE->{ $context } = $tree;
793  
794 54 return( $tree );
795 }
796  
797 sub _relative_location_path
798 {
799 86 ▶ my( $self, $tokens ) = @_;
Subroutine _relative_location_path: called CC=7 SCAR=19.7
800 86 my @steps;
801  
802 86 push( @steps, $self->_step( $tokens ) );
803 86 ▶ while( $self->_match( $tokens, qr/\/\/?/ ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_match($tokens, qr"//?")) 23 86 109
804 {
805 23 ▶ if( $self->{_curr_match} eq '//' )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_curr_match"} eq "//") 7 16 23
806 {
807 7 my $optimised = $self->_optimise_descendant_or_self( $tokens);
808 7 ▶ if( !$optimised )
Branch: 1 branch
BranchTrueFalseTotal
if (not $optimised) { } 0 7 7
809 {
810 0 push( @steps, $self->new_step( $self, 'descendant-or-self', $self->_class_for( 'Step' )->TEST_NT_NODE ) );
811 }
812 else
813 {
814 7 push( @steps, $optimised );
815 }
816 }
817 23 push( @steps, $self->_step( $tokens ) );
818 23 ▶ if( @steps > 1 &&
Branch: 1 branch
BranchTrueFalseTotal
if (@steps > 1 and $steps[-1]->axis eq "self" and $steps[-1]->test == $self->_class_for("Step")->TEST_NT_NODE) 7 16 23
Condition66%4/6
@steps > 1 and $steps[-1]->axis eq "self" and $steps[-1]->test == $self->_class_for("Step")->TEST_NT_NODE
!ll&&!rl&&r
1607
@steps > 1 and $steps[-1]->axis eq "self"
!ll&&!rl&&r
0167
MC/DC33%1/3
@steps > 1 and $steps[-1]->axis eq "self" and $steps[-1]->test == $self->_class_for("Step")->TEST_NT_NODE
@steps > 1 $steps[-1]->axis eq "self" $steps[-1]->test == $self->_class_for("Step")->TEST_NT_NODE
Truth table50%2/4
A and B and C
A= @steps > 1B= $steps[-1]->axis eq "self"C= $steps[-1]->test == $self->_class_for("Step")->TEST_NT_NODE
ABCresult
0XX0
10X0
1100
1111
819 $steps[-1]->axis eq 'self' &&
820 $steps[-1]->test == $self->_class_for( 'Step' )->TEST_NT_NODE )
821 {
822 7 pop( @steps );
823 }
824 }
825 86 return( @steps );
826 }
827  
828 1395 ▶sub _set_context_pos { return( shift->_set_get_scalar( 'context_pos', @_ ) ); }
Subroutine _set_context_pos: called CC=1 SCAR=0.0
829  
830 904 ▶sub _set_context_set { return( shift->_set_get_scalar( 'context_set', @_ ) ); }
Subroutine _set_context_set: called CC=1 SCAR=0.0
831  
832 sub _step
833 {
834 140 ▶ my( $self, $tokens ) = @_;
Subroutine _step: called CC=28 SCAR=47.7
835 140 ▶ if( $self->_match( $tokens, qr/\./ ) )
Branch: 2 branches
BranchTrueFalseTotal
if ($self->_match($tokens, qr/\./)) { } 39 101 140
elsif ($self->_match($tokens, qr/\.\./)) { } 1 100 101
836 {
837 # self::node()
838 39 return( $self->new_step( $self, 'self', $self->_class_for( 'Step' )->TEST_NT_NODE ) );
839 }
840 elsif( $self->_match( $tokens, qr/\.\./ ) )
841 {
842 # parent::node()
843 1 return( $self->new_step( $self, 'parent', $self->_class_for( 'Step' )->TEST_NT_NODE ) );
844 }
845 else
846 {
847 # AxisSpecifier NodeTest Predicate(s?)
848 100 my $token = $tokens->[ $self->{_tokpos} ];
849  
850 100 my $step;
851 100 ▶ if( $token eq 'processing-instruction' )
Branch: 10 branches
BranchTrueFalseTotal
if ($token eq 'processing-instruction') { } 0 100 100
elsif ($token =~ /^\@($HTML::Object::XPath::NC_WILD_RE|$HTML::Object::XPath::QNAME_RE|$HTML::Object::XPath::QN_WILD_RE)$/o) { } 31 69 100
elsif ($token =~ /^($HTML::Object::XPath::NC_NAME_RE):\*$/o) { } 0 69 69
elsif ($token =~ /^$HTML::Object::XPath::QN_WILD_RE$/o) { } 11 58 69
elsif ($token =~ /^$HTML::Object::XPath::QNAME_RE$/o) { } 51 7 58
elsif ($token eq 'comment()') { } 0 7 7
elsif ($token eq 'text()') { } 0 7 7
elsif ($token eq 'node()') { } 0 7 7
elsif ($token eq 'processing-instruction()') { } 0 7 7
elsif ($token =~ /^$HTML::Object::XPath::AXIS_NAME_RE($HTML::Object::XPath::NC_WILD_RE|$HTML::Object::XPath::QNAME_RE|$HTML::Object::XPath::QN_WILD_RE|$HTML::Object::XPath::NODE_TYPE_RE)$/o) { } 7 0 7
852 {
853 0 $self->{_tokpos}++;
854 0 $self->_match( $tokens, qr/\(/, 1 );
855 0 $self->_match( $tokens, $LITERAL_RE );
856 0 $self->{_curr_match} =~ /^["'](.*)["']$/;
857 0 $step = $self->new_step(
858 $self, 'child',
859 $self->_class_for( 'Step' )->TEST_NT_PI,
860 $self->new_literal( $1 )
861 );
862 0 $self->_match( $tokens, qr/\)/, 1 );
863 }
864 elsif( $token =~ /^\@($NC_WILD_RE|$QNAME_RE|$QN_WILD_RE)$/o )
865 {
866 31 $self->{_tokpos}++;
867 31 ▶ if( $token eq '@*' )
Branch: 3 branches
BranchTrueFalseTotal
if ($token eq '@*') { } 3 28 31
elsif ($token =~ /^\@($HTML::Object::XPath::NC_NAME_RE):\*$/o) { } 0 28 28
elsif ($token =~ /^\@($HTML::Object::XPath::QNAME_RE)$/o) { } 28 0 28
868 {
869 3 $step = $self->new_step(
870 $self, 'attribute',
871 $self->_class_for( 'Step' )->TEST_ATTR_ANY,
872 '*'
873 );
874 }
875 elsif( $token =~ /^\@($NC_NAME_RE):\*$/o )
876 {
877 0 $step = $self->new_step(
878 $self, 'attribute',
879 $self->_class_for( 'Step' )->TEST_ATTR_NCWILD,
880 $1
881 );
882 }
883 elsif( $token =~ /^\@($QNAME_RE)$/o )
884 {
885 28 $step = $self->new_step(
886 $self, 'attribute',
887 $self->_class_for( 'Step' )->TEST_ATTR_QNAME,
888 $1
889 );
890 }
891 }
892 # ns:*
893 elsif( $token =~ /^($NC_NAME_RE):\*$/o )
894 {
895 0 $self->{_tokpos}++;
896 0 $step = $self->new_step(
897 $self, 'child',
898 $self->_class_for( 'Step' )->TEST_NCWILD,
899 $1
900 );
901 }
902 # *
903 elsif( $token =~ /^$QN_WILD_RE$/o )
904 {
905 11 $self->{_tokpos}++;
906 11 $step = $self->new_step(
907 $self, 'child',
908 $self->_class_for( 'Step' )->TEST_ANY,
909 $token
910 );
911 }
912 # name:name
913 elsif( $token =~ /^$QNAME_RE$/o )
914 {
915 51 $self->{_tokpos}++;
916 51 $step = $self->new_step(
917 $self, 'child',
918 $self->_class_for( 'Step' )->TEST_QNAME,
919 $token
920 );
921 }
922 elsif( $token eq 'comment()' )
923 {
924 0 $self->{_tokpos}++;
925 0 $step = $self->new_step(
926 $self, 'child',
927 $self->_class_for( 'Step' )->TEST_NT_COMMENT
928 );
929 }
930 elsif( $token eq 'text()' )
931 {
932 0 $self->{_tokpos}++;
933 0 $step = $self->new_step(
934 $self, 'child',
935 $self->_class_for( 'Step' )->TEST_NT_TEXT
936 );
937 }
938 elsif( $token eq 'node()' )
939 {
940 0 $self->{_tokpos}++;
941 0 $step = $self->new_step(
942 $self, 'child',
943 $self->_class_for( 'Step' )->TEST_NT_NODE
944 );
945 }
946 elsif( $token eq 'processing-instruction()' )
947 {
948 0 $self->{_tokpos}++;
949 0 $step = $self->new_step(
950 $self, 'child',
951 $self->_class_for( 'Step' )->TEST_NT_PI
952 );
953 }
954 elsif( $token =~ /^$AXIS_NAME_RE($NC_WILD_RE|$QNAME_RE|$QN_WILD_RE|$NODE_TYPE_RE)$/o )
955 {
956 7 my $axis = $1;
957 7 $self->{_tokpos}++;
958 7 $token = $2;
959 7 ▶ if( $token eq 'processing-instruction' )
Branch: 8 branches
BranchTrueFalseTotal
if ($token eq 'processing-instruction') { } 0 7 7
elsif ($token =~ /^($HTML::Object::XPath::NC_NAME_RE):\*$/o) { } 0 7 7
elsif ($token =~ /^$HTML::Object::XPath::QN_WILD_RE$/o) { } 1 6 7
elsif ($token =~ /^$HTML::Object::XPath::QNAME_RE$/o) { } 6 0 6
elsif ($token eq 'comment()') { } 0 0 0
elsif ($token eq 'text()') { } 0 0 0
elsif ($token eq 'node()') { } 0 0 0
elsif ($token eq 'processing-instruction()') { } 0 0 0
960 {
961 0 $self->_match( $tokens, qr/\(/, 1 );
962 0 $self->_match( $tokens, $LITERAL_RE );
963 0 $self->{_curr_match} =~ /^["'](.*)["']$/;
964 0 $step = $self->new_step(
965 $self, $axis,
966 $self->_class_for( 'Step' )->TEST_NT_PI,
967 HTML::Object::XPath::Literal->new( $1 )
968 );
969 0 $self->_match( $tokens, qr/\)/, 1 );
970 }
971 # ns:*
972 elsif( $token =~ /^($NC_NAME_RE):\*$/o )
973 {
974 0 ▶ $step = $self->new_step(
Branch: 1 branch
BranchTrueFalseTotal
$axis eq 'attribute' ? : 0 0 0
975 $self, $axis,
976 ( ( $axis eq 'attribute' )
977 ? $self->_class_for( 'Step' )->TEST_ATTR_NCWILD
978 : $self->_class_for( 'Step' )->TEST_NCWILD
979 ),
980 $1
981 );
982 }
983 # *
984 elsif( $token =~ /^$QN_WILD_RE$/o )
985 {
986 1 ▶ $step = $self->new_step(
Branch: 1 branch
BranchTrueFalseTotal
$axis eq 'attribute' ? : 0 1 1
987 $self, $axis,
988 ( ( $axis eq 'attribute' )
989 ? $self->_class_for( 'Step' )->TEST_ATTR_ANY
990 : $self->_class_for( 'Step' )->TEST_ANY
991 ),
992 $token
993 );
994 }
995 # name:name
996 elsif( $token =~ /^$QNAME_RE$/o )
997 {
998 6 ▶ $step = $self->new_step(
Branch: 1 branch
BranchTrueFalseTotal
$axis eq 'attribute' ? : 0 6 6
999 $self, $axis,
1000 ( ( $axis eq 'attribute' )
1001 ? $self->_class_for( 'Step' )->TEST_ATTR_QNAME
1002 : $self->_class_for( 'Step' )->TEST_QNAME
1003 ),
1004 $token
1005 );
1006 }
1007 elsif( $token eq 'comment()' )
1008 {
1009 0 $step = $self->new_step(
1010 $self, $axis,
1011 $self->_class_for( 'Step' )->TEST_NT_COMMENT
1012 );
1013 }
1014 elsif( $token eq 'text()' )
1015 {
1016 0 $step = $self->new_step(
1017 $self, $axis,
1018 $self->_class_for( 'Step' )->TEST_NT_TEXT
1019 );
1020 }
1021 elsif( $token eq 'node()' )
1022 {
1023 0 $step = $self->new_step(
1024 $self, $axis,
1025 $self->_class_for( 'Step' )->TEST_NT_NODE
1026 );
1027 }
1028 elsif( $token eq 'processing-instruction()' )
1029 {
1030 0 $step = $self->new_step(
1031 $self, $axis,
1032 $self->_class_for( 'Step' )->TEST_NT_PI
1033 );
1034 }
1035 else
1036 {
1037 0 die( "Shouldn't get here" );
1038 }
1039 }
1040 else
1041 {
1042 0 die( "token $token does not match format of a 'Step'\n" );
1043 }
1044  
1045 100 ▶ while( $self->_match( $tokens, qr/\[/ ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_match($tokens, qr/\[/)) 28 100 128
1046 {
1047 28 push( @{$step->{predicates}}, $self->_expr( $tokens ) );
1048 28 $self->_match( $tokens, qr/\]/, 1 );
1049 }
1050 100 return( $step );
1051 }
1052 }
1053  
1054 sub _tokenize
1055 {
1056 54 ▶ my $self = shift( @_ );
Subroutine _tokenize: called CC=8 SCAR=21.6
1057 54 my $path = shift( @_ );
1058 54 my $tokens = $self->new_array;
1059  
1060  
1061 # Bug: We do not allow "'@' NodeType" which is in the grammar, but I think is just plain stupid.
1062  
1063 # used to desambiguate conflicts (for REs)
1064 54 my $expected = '';
1065  
1066 54 ▶ while( length( $path ) )
Branch: 1 branch
BranchTrueFalseTotal
if (length $path) 496 0 496
1067 {
1068 496 my $token = '';
1069 496 ▶ if( $expected eq 'RE' && ( $path =~ m{\G\s*($REGEXP_RE $REGEXP_MOD_RE?)\s*}gcxso ) )
Branch: 2 branches
BranchTrueFalseTotal
if ($expected eq 'RE' and $path =~ /\G\s*($HTML::Object::XPath::REGEXP_RE $HTML::Object::XPath::REGEXP_MOD_RE?)\s*/cgosx) { } 3 493 496
elsif ($path =~ m{\G \s* # ignore all whitespace ( # tokens $HTML::Object::XPath::LITERAL_RE| $HTML::Object::XPath::NUMBER_RE| # digits \.\.| # parent \.| # current ($HTML::Object::XPath::AXIS_NAME_RE)?$HTML::Object::XPath::NODE_TYPE_RE| # tests processing-instruction| \@($HTML::Object::XPath::NC_WILD_RE|$HTML::Object::XPath::QNAME_RE|$HTML::Object::XPath::QN_WILD_RE)| # attrib \$$HTML::Object::XPath::QNAME_RE| # variable reference ($HTML::Object::XPath::AXIS_NAME_RE)?($HTML::Object::XPath::NC_WILD_RE|$HTML::Object::XPath::QNAME_RE|$HTML::Object::XPath::QN_WILD_RE)| # NCName,NodeType,Axis::Test \!=|<=|\-|>=|//|and|or|mod|div| # multi-char seps =~|\!~| # regexp (not in the XPath spec) [,\+=\|<>/\(\[\]\)]| # single char seps (?<!(\@|\(|\[))\*| # multiply operator rules (see xpath spec) (?<!::)\*| $ # end of query ) \s* # ignore all whitespace }cgosx) { } 439 54 493
Condition66%2/3
$expected eq "RE" and $path =~ /\G\s*($HTML::Object::XPath::REGEXP_RE $HTML::Object::XPath::REGEXP_MOD_RE?)\s*/cgosx
!ll&&!rl&&r
49303
MC/DC50%1/2
$expected eq "RE" and $path =~ /\G\s*($HTML::Object::XPath::REGEXP_RE $HTML::Object::XPath::REGEXP_MOD_RE?)\s*/cgosx
$expected eq "RE" $path =~ /\G\s*($HTML::Object::XPath::REGEXP_RE $HTML::Object::XPath::REGEXP_MOD_RE?)\s*/cgosx
Truth table66%2/3
A and B
A= $expected eq "RE"B= $path =~ /\G\s*($HTML::Object::XPath::REGEXP_RE $HTML::Object::XPath::REGEXP_MOD_RE?)\s*/cgosx
ABresult
0X0
100
111
1070 {
1071 # special case: regexp expected after =~ or !~, regular parsing rules do not apply
1072 # ( the / is now the regexp delimiter)
1073 3 $token = $1;
1074 3 $expected = '';
1075 }
1076 elsif( $path =~ m/\G
1077 \s* # ignore all whitespace
1078 ( # tokens
1079 $LITERAL_RE|
1080 $NUMBER_RE| # digits
1081 \.\.| # parent
1082 \.| # current
1083 ($AXIS_NAME_RE)?$NODE_TYPE_RE| # tests
1084 processing-instruction|
1085 \@($NC_WILD_RE|$QNAME_RE|$QN_WILD_RE)| # attrib
1086 \$$QNAME_RE| # variable reference
1087 ($AXIS_NAME_RE)?($NC_WILD_RE|$QNAME_RE|$QN_WILD_RE)| # NCName,NodeType,Axis::Test
1088 \!=|<=|\-|>=|\/\/|and|or|mod|div| # multi-char seps
1089 =~|\!~| # regexp (not in the XPath spec)
1090 [,\+=\|<>\/\(\[\]\)]| # single char seps
1091 (?<!(\@|\(|\[))\*| # multiply operator rules (see xpath spec)
1092 (?<!::)\*|
1093 $ # end of query
1094 )
1095 \s* # ignore all whitespace
1096 /gcxso )
1097 {
1098 439 $token = $1;
1099 439 ▶ $expected = ( $token =~ m{^[=!]~$} ) ? 'RE' : '';
Branch: 1 branch
BranchTrueFalseTotal
$token =~ /^[=!]~$/ ? : 3 436 439
1100 }
1101 else
1102 {
1103 54 $token = '';
1104 54 last;
1105 }
1106  
1107 442 ▶ if( length( $token ) )
Branch: 1 branch
BranchTrueFalseTotal
if (length $token) 388 54 442
1108 {
1109 # push( @tokens, $token );
1110 388 $tokens->push( $token );
1111 }
1112 }
1113  
1114 54 ▶ if( pos( $path ) < length( $path ) )
Branch: 1 branch
BranchTrueFalseTotal
if (pos $path < length $path) 0 54 54
1115 {
1116 0 my $marker = ( '.' x ( pos( $path ) -1 ) );
1117 0 $path = substr( $path, 0, pos( $path ) + 8 ) . '...';
1118 0 $path =~ s/\n/ /g;
1119 0 $path =~ s/\t/ /g;
1120 0 die "Query:\n", "$path\n", $marker, "^^^\n", "Invalid query somewhere around here (I think)\n";
1121 # return( $self->error( "Query:\n", "$path\n", $marker, "^^^\n", "Invalid query somewhere around here (I think)" ) );
1122 }
1123 54 return( $tokens );
1124 }
1125  
1126 sub _xml_escape_text
1127 {
1128 0 ▶ my( $self, $text ) = @_;
Subroutine _xml_escape_text: not called CC=1 SCAR=6.9
1129 0 my $entities = { '&' => '&amp;', '<' => '&lt;', '>' => '&gt;', '"' => '&quote;' };
1130 0 $text =~ s{([&<>])}{$entities->{$1}}g;
1131 0 return( $text );
1132 }
1133  
1134 14 1;
1135 # NOTE: POD
1136 __END__
« lib/HTML/Object/DOM/TextTrackList.pm ↑ Summary lib/HTML/Object/DOM/Implementation.pm »