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 12 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 12 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 12 ▶ warnings::register_categories( 'HTML::Object' );
Subroutine BEGIN: called CC=1 SCAR=0.0
20 12 ▶ use parent qw( Module::Generic );
Subroutine BEGIN: called CC=1 SCAR=0.0
21 12 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 12 ▶ );
Subroutine BEGIN: called CC=1 SCAR=0.0
26 12 ▶ use HTML::Object::XPath::Step;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 12 ▶ use HTML::Object::XPath::Expr;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 12 ▶ use HTML::Object::XPath::Function;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 12 ▶ use HTML::Object::XPath::LocationPath;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 12 ▶ use HTML::Object::XPath::Variable;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 12 ▶ use HTML::Object::XPath::Literal;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 12 ▶ use HTML::Object::XPath::Number;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 12 ▶ use HTML::Object::XPath::NodeSet;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 12 ▶ use HTML::Object::XPath::Root;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 12 our $VERSION = 'v0.2.0';
36 12 our $CACHE = {};
37 # Axis name to principal node type mapping
38 12 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 12 my $AXES_KEYS = join( '|', keys( %$AXES ) );
55 12 our $NC_NAME_RE = qr/([A-Za-z_][\w\.\-]*)/;
56 12 our $QNAME_RE = qr/(${NC_NAME_RE}:)?${NC_NAME_RE}/;
57 12 our $NC_WILD_RE = qr/${NC_NAME_RE}:\*/;
58 12 our $QN_WILD_RE = qr/\*/;
59 12 our $NODE_TYPE_RE = qr/((text|comment|processing-instruction|node)\(\))/;
60 12 our $AXIS_NAME_RE = qr/(${AXES_KEYS})::/;
61 12 our $NUMBER_RE = qr/\d+(\.\d*)?|\.\d+/;
62 12 our $LITERAL_RE = qr/\"[^\"]*\"|\'[^\']*\'/;
63 12 our $REGEXP_RE = qr{(?:m?/(?:\\.|[^/])*/)};
64 12 our $REGEXP_MOD_RE = qr{(?:[imsx]+)};
65 12 our $BASE_CLASS = __PACKAGE__;
66 };
67  
68 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
69 12 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
70  
71 sub init
72 {
73 10 ▶ my $self = shift( @_ );
Subroutine init: called CC=2 SCAR=6.9
Pod documented
74 10 $self->{_init_strict_use_sub} = 1;
75 10 ▶ $self->SUPER::init( @_ ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->SUPER::init(@_) or return $self->pass_error 0 10 10
76 10 $self->{context_pos} = undef; # 1 based position in array context
77 10 $self->{context_set} = $self->new_nodeset;
78 10 $self->{context_size} = 0; # total size of context
79 10 $self->clear_namespaces();
80 10 $self->{cache} = {};
81 10 $self->{direction} = 'forward';
82 10 $self->{namespaces} = {};
83 10 $self->{vars} = {};
84 10 $self->{_tokpos} = 0;
85 10 return( $self );
86 }
87  
88 sub clear_namespaces
89 {
90 10 ▶ my $self = shift( @_ );
Subroutine clear_namespaces: called CC=1 SCAR=0.0
Pod documented
91 10 $self->{uses_namespaces} = 0;
92 10 $self->{namespaces} = {};
93 10 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 65 ▶ my $self = shift( @_ );
Subroutine find: called CC=2 SCAR=6.9
Pod documented
108 # xpath expression and $context is a HTML::Object::Element
109 65 my( $path, $context ) = @_;
110 # _parse returns an HTML::Object::XPath::Expr object
111 65 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 65 my $results = $parsed_path->evaluate( $context );
115 62 ▶ if( $self->_is_a( $results, 'HTML::Object::XPath::NodeSet' ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_a($results, 'HTML::Object::XPath::NodeSet')) { } 49 13 62
116 {
117 49 return( $results->sort->remove_duplicates );
118 }
119 else
120 {
121 13 return( $results );
122 }
123 }
124  
125 sub findnodes
126 {
127 29 ▶ my $self = shift( @_ );
Subroutine findnodes: called CC=4 SCAR=16.4
Pod documented
128 29 my( $path, $context ) = @_;
129
130 29 my $results = $self->find( $path, $context );
131
132 29 ▶ if( $self->_is_a( $results => 'HTML::Object::XPath::NodeSet' ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_a($results, 'HTML::Object::XPath::NodeSet')) { } 29 0 29
133 {
134 29 ▶ return( wantarray() ? $results->get_nodelist : $results );
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 29 0 29
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 65 ▶sub namespaces { return( shift->_set_get_hash_as_mix_object( 'namespaces', @_ ) ); }
Subroutine namespaces: called CC=1 SCAR=0.0
Pod documented
248  
249 239 ▶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 85 ▶sub new_location_path { return( shift->_class_for( 'LocationPath' )->new( @_ ) ); }
Subroutine new_location_path: called CC=1 SCAR=0.0
Pod documented
256  
257 10 ▶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 45 ▶sub new_root { return( shift->_class_for( 'Root' )->new( @_ ) ); }
Subroutine new_root: called CC=1 SCAR=0.0
Pod documented
262  
263 159 ▶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 53 ▶ my $self = shift( @_ );
Subroutine _analyze: called CC=1 SCAR=0.0
294 # Array object
295 53 my $tokens = shift( @_ );
296 # lexical analysis
297 53 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 764 ▶ my( $self, $mod ) = @_;
Subroutine _class_for: called CC=3 SCAR=11.3
320 764 eval( "require ${BASE_CLASS}\::${mod};" );
321 764 ▶ die( $@ ) if( $@ );
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 764 764
322 # ${"${BASE_CLASS}\::${mod}\::DEBUG"} = $self->debug;
323 764 ▶ eval( "\$${BASE_CLASS}\::${mod}\::DEBUG = " . ( $self->debug // 0 ) );
Condition50%1/2
$self->debug // 0
l!l
0764
MC/DC0%0/1
$self->debug // 0
$self->debug
Truth table50%1/2
A
A= $self->debug
Aresult
11
00
324 764 return( "${BASE_CLASS}::${mod}" );
325 }
326  
327 sub _expr
328 {
329 110 ▶ 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 110 return( $self->_expr_or( $tokens ) );
333 }
334  
335 sub _expr_additive
336 {
337 138 ▶ my( $self, $tokens ) = @_;
Subroutine _expr_additive: called CC=2 SCAR=10.3
338
339 138 my $expr = $self->_expr_multiplicative( $tokens );
340 138 ▶ while( $self->_match( $tokens, qr/[\+\-]/ ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_match($tokens, qr/[\+\-]/)) 0 138 138
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 138 return( $expr );
352 }
353  
354 sub _expr_and
355 {
356 111 ▶ my( $self, $tokens ) = @_;
Subroutine _expr_and: called CC=2 SCAR=6.9
357 111 my $expr = $self->_expr_match( $tokens );
358 111 ▶ while( $self->_match( $tokens, 'and' ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_match($tokens, "and")) 2 111 113
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 111 return( $expr );
368 }
369  
370 sub _expr_equality
371 {
372 116 ▶ my( $self, $tokens ) = @_;
Subroutine _expr_equality: called CC=2 SCAR=6.9
373
374 116 my $expr = $self->_expr_relational( $tokens );
375 116 ▶ while( $self->_match( $tokens, qr/!?=/ ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_match($tokens, qr/!?=/)) 21 116 137
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 116 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 113 ▶ my( $self, $tokens ) = @_;
Subroutine _expr_match: called CC=2 SCAR=6.9
406
407 113 my $expr = $self->_expr_equality( $tokens );
408  
409 113 ▶ while( $self->_match( $tokens, qr/[=!]~/ ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_match($tokens, qr/[=!]~/)) 3 113 116
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 113 return( $expr );
421 }
422  
423 sub _expr_multiplicative
424 {
425 138 ▶ my( $self, $tokens ) = @_;
Subroutine _expr_multiplicative: called CC=2 SCAR=10.3
426
427 138 my $expr = $self->_expr_unary( $tokens );
428 138 ▶ while( $self->_match( $tokens, qr/(\*|div|mod)/ ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_match($tokens, qr/(\*|div|mod)/)) 0 138 138
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 138 return( $expr );
440 }
441  
442 sub _expr_or
443 {
444 110 ▶ my( $self, $tokens ) = @_;
Subroutine _expr_or: called CC=2 SCAR=6.9
445
446 110 my $expr = $self->_expr_and( $tokens );
447 110 ▶ while( $self->_match( $tokens, 'or' ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_match($tokens, "or")) 1 110 111
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 110 return( $expr );
459 }
460  
461 sub _expr_path
462 {
463 138 ▶ 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 138 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 138 my $test = $tokens->[ $self->{_tokpos} ];
472
473 # Test for AbsoluteLocationPath and AbbreviatedRelativeLocationPath
474 138 ▶ if( $test =~ /^(\/\/?|\.\.?)$/ )
Branch: 2 branches
BranchTrueFalseTotal
if ($test =~ m[^(//?|\.\.?)$]) { } 54 84 138
elsif ($self->_is_step($tokens)) { } 21 63 84
475 {
476 # LocationPath
477 54 $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 128 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 137 ▶ my( $self, $tokens ) = @_;
Subroutine _expr_relational: called CC=2 SCAR=6.9
573
574 137 my $expr = $self->_expr_additive( $tokens );
575 137 ▶ while( $self->_match( $tokens, qr/(<|>|<=|>=)/ ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_match($tokens, qr/(<|>|<=|>=)/)) 1 137 138
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 137 return( $expr );
587 }
588  
589 sub _expr_unary
590 {
591 138 ▶ my( $self, $tokens ) = @_;
Subroutine _expr_unary: called CC=2 SCAR=10.5
592 # $tokens are an array object of expression tokens
593
594 138 ▶ if( $self->_match( $tokens, qr/-/ ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_match($tokens, qr/-/)) { } 0 138 138
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 138 return( $self->_expr_union( $tokens ) );
605 }
606 }
607  
608 sub _expr_union
609 {
610 138 ▶ my( $self, $tokens ) = @_;
Subroutine _expr_union: called CC=2 SCAR=10.3
611 # $tokens are an array object of expression tokens
612
613 138 my $expr = $self->_expr_path( $tokens );
614 138 ▶ while( $self->_match( $tokens, qr/\|/ ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_match($tokens, qr/\|/)) 0 138 138
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 138 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 357 ▶sub _get_context_pos { return( shift->{context_pos} ); }
Subroutine _get_context_pos: called CC=1 SCAR=0.0
630  
631 357 ▶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 75 ▶ my( $self, $tokens ) = @_;
Subroutine _location_path: called CC=5 SCAR=16.1
661 75 my $loc_path = $self->new_location_path;
662
663 75 ▶ if( $self->_match( $tokens, qr/\// ) )
Branch: 2 branches
BranchTrueFalseTotal
if ($self->_match($tokens, qr"/")) { } 5 70 75
elsif ($self->_match($tokens, qr"//")) { } 40 30 70
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 40 $loc_path->push( $self->new_root );
679 40 my $optimised = $self->_optimise_descendant_or_self( $tokens );
680 40 ▶ if( !$optimised )
Branch: 1 branch
BranchTrueFalseTotal
if (not $optimised) { } 18 22 40
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 22 $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 75 return( $loc_path );
697 }
698  
699 sub _match
700 {
701 2223 ▶ 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 2223 $self->{_curr_match} = '';
706 2223 ▶ return(0) unless( $self->{_tokpos} < scalar( @$tokens ) );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_tokpos"} < scalar @$tokens 531 1692 2223
707 # return(0) unless( $self->{_tokpos} < $tokens->length );
708  
709 # local $^W;
710 1692 ▶ if( $tokens->[ $self->{_tokpos} ] =~ /^$match$/ )
Branch: 1 branch
BranchTrueFalseTotal
if ($$tokens[$self->{'_tokpos'}] =~ /^$match$/) { } 317 1375 1692
711 {
712 317 $self->{_curr_match} = $tokens->[ $self->{_tokpos} ];
713 317 $self->{_tokpos}++;
714 317 return(1);
715 }
716 else
717 {
718 1375 ▶ if( $fatal )
Branch: 1 branch
BranchTrueFalseTotal
if ($fatal) { } 0 1375 1375
719 {
720 0 die( "Invalid token: ", $tokens->[$self->{_tokpos}], "\n" );
721 # return( $self->error( "Invalid token: ", $tokens->[ $self->{_tokpos} ] ) );
722 }
723 else
724 {
725 1375 return(0);
726 }
727 }
728 }
729  
730 sub _optimise_descendant_or_self
731 {
732 47 ▶ my( $self, $tokens ) = @_;
Subroutine _optimise_descendant_or_self: called CC=5 SCAR=16.1
733
734 47 my $tokpos = $self->{_tokpos};
735
736 # // must be followed by a Step.
737 47 ▶ if( $tokens->[ $tokpos + 1 ] && $tokens->[ $tokpos + 1 ] eq '[' )
Branch: 2 branches
BranchTrueFalseTotal
if ($$tokens[$tokpos + 1] and $$tokens[$tokpos + 1] eq '[') { } 17 30 47
elsif ($tokens->[$tokpos] =~ /^\.\.?$/) { } 0 30 30
Condition100%3/3
$$tokens[$tokpos + 1] and $$tokens[$tokpos + 1] eq "["
!ll&&!rl&&r
171317
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 30 my $step = $self->_step( $tokens );
750 30 ▶ if( $step->axis ne 'child' )
Branch: 1 branch
BranchTrueFalseTotal
if ($step->axis ne "child") 1 29 30
751 {
752 # can't optimise axes other than child for now...
753 1 $self->{_tokpos} = $tokpos;
754 1 return;
755 }
756 29 $step->axis( 'descendant' );
757 29 $step->axis_method( 'axis_descendant' );
758 29 $self->{_tokpos}--;
759 29 $tokens->[ $self->{_tokpos} ] = '.';
760 29 return( $step );
761 }
762 }
763  
764 sub _parse
765 {
766 65 ▶ my $self = shift( @_ );
Subroutine _parse: called CC=3 SCAR=11.1
767 65 my $path = shift( @_ );
768  
769 # $context is something like: //*[@att2="vv"]
770 # my $context = join( '&&', $path, map { "$_=>$self->{namespaces}->{$_}" } sort keys %{$self->{namespaces}});
771 65 ▶ 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 65 ▶ return( $CACHE->{ $context } ) if( $CACHE->{ $context } );
Branch: 1 branch
BranchTrueFalseTotal
if $HTML::Object::XPath::CACHE->{$context} 12 53 65
774  
775 # my $tokens = $self->_tokenize( $path ) || return( $self->pass_error );
776 # Get an Module::Generic::Array object in return
777 53 my $tokens = $self->_tokenize( $path );
778  
779 53 $self->{_tokpos} = 0;
780 53 my $tree = $self->_analyze( $tokens );
781
782 53 ▶ if( $self->{_tokpos} < $tokens->length )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_tokpos"} < $tokens->length) 0 53 53
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 53 $tree->{uses_namespaces} = $self->{uses_namespaces};
790 53 $tree->{strict_namespaces} = $self->{strict_namespaces};
791
792 53 $CACHE->{ $context } = $tree;
793
794 53 return( $tree );
795 }
796  
797 sub _relative_location_path
798 {
799 85 ▶ my( $self, $tokens ) = @_;
Subroutine _relative_location_path: called CC=7 SCAR=19.7
800 85 my @steps;
801
802 85 push( @steps, $self->_step( $tokens ) );
803 85 ▶ while( $self->_match( $tokens, qr/\/\/?/ ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_match($tokens, qr"//?")) 23 85 108
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 85 return( @steps );
826 }
827  
828 1391 ▶sub _set_context_pos { return( shift->_set_get_scalar( 'context_pos', @_ ) ); }
Subroutine _set_context_pos: called CC=1 SCAR=0.0
829  
830 900 ▶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 138 ▶ my( $self, $tokens ) = @_;
Subroutine _step: called CC=28 SCAR=47.7
835 138 ▶ if( $self->_match( $tokens, qr/\./ ) )
Branch: 2 branches
BranchTrueFalseTotal
if ($self->_match($tokens, qr/\./)) { } 38 100 138
elsif ($self->_match($tokens, qr/\.\./)) { } 1 99 100
836 {
837 # self::node()
838 38 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 99 my $token = $tokens->[ $self->{_tokpos} ];
849
850 99 my $step;
851 99 ▶ if( $token eq 'processing-instruction' )
Branch: 10 branches
BranchTrueFalseTotal
if ($token eq 'processing-instruction') { } 0 99 99
elsif ($token =~ /^\@($HTML::Object::XPath::NC_WILD_RE|$HTML::Object::XPath::QNAME_RE|$HTML::Object::XPath::QN_WILD_RE)$/o) { } 31 68 99
elsif ($token =~ /^($HTML::Object::XPath::NC_NAME_RE):\*$/o) { } 0 68 68
elsif ($token =~ /^$HTML::Object::XPath::QN_WILD_RE$/o) { } 11 57 68
elsif ($token =~ /^$HTML::Object::XPath::QNAME_RE$/o) { } 50 7 57
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 50 $self->{_tokpos}++;
916 50 $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 99 ▶ while( $self->_match( $tokens, qr/\[/ ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_match($tokens, qr/\[/)) 28 99 127
1046 {
1047 28 push( @{$step->{predicates}}, $self->_expr( $tokens ) );
1048 28 $self->_match( $tokens, qr/\]/, 1 );
1049 }
1050 99 return( $step );
1051 }
1052 }
1053  
1054 sub _tokenize
1055 {
1056 53 ▶ my $self = shift( @_ );
Subroutine _tokenize: called CC=8 SCAR=21.6
1057 53 my $path = shift( @_ );
1058 53 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 53 my $expected = '';
1065  
1066 53 ▶ while( length( $path ) )
Branch: 1 branch
BranchTrueFalseTotal
if (length $path) 492 0 492
1067 {
1068 492 my $token = '';
1069 492 ▶ 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 489 492
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) { } 436 53 489
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
48903
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 436 $token = $1;
1099 436 ▶ $expected = ( $token =~ m{^[=!]~$} ) ? 'RE' : '';
Branch: 1 branch
BranchTrueFalseTotal
$token =~ /^[=!]~$/ ? : 3 433 436
1100 }
1101 else
1102 {
1103 53 $token = '';
1104 53 last;
1105 }
1106  
1107 439 ▶ if( length( $token ) )
Branch: 1 branch
BranchTrueFalseTotal
if (length $token) 386 53 439
1108 {
1109 # push( @tokens, $token );
1110 386 $tokens->push( $token );
1111 }
1112 }
1113
1114 53 ▶ if( pos( $path ) < length( $path ) )
Branch: 1 branch
BranchTrueFalseTotal
if (pos $path < length $path) 0 53 53
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 53 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 12 1;
1135 # NOTE: POD
1136 __END__
1137  
1138 =encoding utf-8
1139  
1140 =head1 NAME
1141  
1142 HTML::Object::XPath - HTML Object XPath Class
1143  
1144 =head1 SYNOPSIS
1145  
1146 use HTML::Object;
1147 use HTML::Object::XQuery;
1148 use HTML::Object::XPath;
1149 my $this = HTML::Object::XPath->new || die( HTML::Object::XPath->error, "\n" );
1150  
1151 my $p = HTML::Object->new;
1152 my $doc = $p->parse_file( $path_to_html_file ) || die( $p->error );
1153 # Returns a list of HTML::Object::Element objects matching the select, which is
1154 # converted into a xpath
1155 my @nodes = $doc->find( 'p' );
1156  
1157 # or directly:
1158 use HTML::Object::XPath;
1159 my $xp = use HTML::Object::XPath->new;
1160 my @nodes = $xp->findnodes( $xpath, $element_object );
1161  
1162 =head1 VERSION
1163  
1164 v0.2.0
1165  
1166 =head1 DESCRIPTION
1167  
1168 This module implements the XPath engine used by L<HTML::Object::XQuery> to provide a jQuery-like interface to query the parsed DOM object.
1169  
1170 =head1 METHODS
1171  
1172 =head2 clear_namespaces
1173  
1174 Clears all previously set namespace mappings.
1175  
1176 =head2 exists
1177  
1178 Provided with a C<path> and a C<context> and this returns true if the given path exists.
1179  
1180 =head2 findnodes
1181  
1182 Provided with a C<path> and a C<context> this returns a list of nodes found by C<path>, optionally in context C<context>.
1183  
1184 In scalar context it returns an HTML::Object::XPath::NodeSet object.
1185  
1186 =head2 findnodes_as_string
1187  
1188 Provided with a C<path> and a C<context> and this returns the nodes found as a single string. The result is not guaranteed to be valid HTML though (it could for example be just text if the query returns attribute values).
1189  
1190 =head2 findnodes_as_strings
1191  
1192 Provided with a C<path> and a C<context> and this returns the nodes found as a list of strings, one per node found.
1193  
1194 =head2 findvalue
1195  
1196 Provided with a C<path> and a C<context> and this returns the result as a string (the concatenation of the values of the result nodes).
1197  
1198 =head2 findvalues
1199  
1200 Provided with a C<path> and a C<context> and this returns the values of the result nodes as a list of strings.
1201  
1202 =head2 matches($node, $path, $context)
1203  
1204 Provided with a C<node> L<object|HTML::Object::Element>, C<path> and a C<context> and this returns true if the node matches the path.
1205  
1206 =head2 find
1207  
1208 Provided with a C<path> and a C<context> and this returns either a L<HTML::Object::XPath::NodeSet> object containing the nodes it found (or empty if no nodes matched the path), or one of L<HTML::Object::XPath::Literal> (a string), L<HTML::Object::XPath::Number>, or L<HTML::Object::XPath::Boolean>. It should always return something - and you can use ->isa() to find out what it returned. If you need to check how many nodes it found you should check $nodeset->size.
1209  
1210 See L<HTML::Object::XPath::NodeSet>.
1211  
1212 =head2 get_namespace ($prefix, $node)
1213  
1214 Provided with a C<prefix> and a C<node> L<object|HTML::Object::Element> and this returns the uri associated to the prefix for the node (mostly for internal usage)
1215  
1216 =head2 get_var
1217  
1218 Provided with a variable name, and this returns the value of the XPath variable (mostly for internal usage)
1219  
1220 =head2 getNodeText
1221  
1222 Provided with a C<path> and this returns the text string for a particular node. It returns a string, or C<undef> if the node does not exist.
1223  
1224 =head2 namespaces
1225  
1226 Sets or gets an hash reference of namespace attributes.
1227  
1228 =head2 new_expr
1229  
1230 Create a new L<HTML::Object::XPath::Expr>, passing it whatever argument was provided, and returns the newly instantiated object, or C<undef> upon L<error|Module::Generic/error>
1231  
1232 =head2 new_function
1233  
1234 Create a new L<HTML::Object::XPath::Function> object, passing it whatever argument was provided, and returns the newly instantiated object, or C<undef> upon L<error|Module::Generic/error>
1235  
1236 =head2 new_literal
1237  
1238 Create a new L<HTML::Object::XPath::Literal> object, passing it whatever argument was provided, and returns the newly instantiated object, or C<undef> upon L<error|Module::Generic/error>
1239  
1240 =head2 new_location_path
1241  
1242 Create a new L<HTML::Object::XPath::LocationPath> object, passing it whatever argument was provided, and returns the newly instantiated object, or C<undef> upon L<error|Module::Generic/error>
1243  
1244 =head2 new_nodeset
1245  
1246 Create a new L<HTML::Object::XPath::NodeSet> object, passing it whatever argument was provided, and returns the newly instantiated object, or C<undef> upon L<error|Module::Generic/error>
1247  
1248 =head2 new_number
1249  
1250 Create a new L<HTML::Object::XPath::Number> object, passing it whatever argument was provided, and returns the newly instantiated object, or C<undef> upon L<error|Module::Generic/error>
1251  
1252 =head2 new_root
1253  
1254 Create a new L<HTML::Object::XPath::Root> object, passing it whatever argument was provided, and returns the newly instantiated object, or C<undef> upon L<error|Module::Generic/error>
1255  
1256 =head2 new_step
1257  
1258 Create a new L<HTML::Object::XPath::Step> object, passing it whatever argument was provided, and returns the newly instantiated object, or C<undef> upon L<error|Module::Generic/error>
1259  
1260 =head2 new_variable
1261  
1262 Create a new L<HTML::Object::XPath::Variable> object, passing it whatever argument was provided, and returns the newly instantiated object, or C<undef> upon L<error|Module::Generic/error>
1263  
1264 =head2 set_namespace
1265  
1266 Provided with a C<prefix> and an C<uri> and this sets the namespace prefix mapping to the uri.
1267  
1268 Normally in L<HTML::Object::XPath> the prefixes in XPath node tests take their context from the current node. This means that foo:bar will always match an element <foo:bar> regardless of the namespace that the prefix foo is mapped to (which might even change within the document, resulting in unexpected results). In order to make prefixes in XPath node tests actually map to a real URI, you need to enable that via a call to the set_namespace method of your HTML::Object::XPath object.
1269  
1270 =head2 parse
1271  
1272 Provided with an XPath expression and this returns a new L<HTML::Object::XPath::Expr> object that can then be used repeatedly.
1273  
1274 You can create an XPath expression from a CSS selector expression using L<HTML::selector::XPath>
1275  
1276 =head2 set_strict_namespaces
1277  
1278 Takes a boolean value.
1279  
1280 By default, for historical as well as convenience reasons, L<HTML::Object::XPath> has a slightly non-standard way of dealing with the default namespace.
1281  
1282 If you search for C<//tag> it will return elements C<tag>. As far as I understand it, if the document has a default namespace, this should not return anything. You would have to first do a C<set_namespace>, and then search using the namespace.
1283  
1284 Passing a true value to C<set_strict_namespaces> will activate this behaviour, passing a false value will return it to its default behaviour.
1285  
1286 =head2 set_var
1287  
1288 Provided with a variable name and its value and this sets an XPath variable (that can be used in queries as C<$var>)
1289  
1290 =head1 NODE STRUCTURE
1291  
1292 All nodes have the same first 2 entries in the array: node_parent and node_pos. The type of the node is determined using the ref() function.
1293  
1294 The node_parent always contains an entry for the parent of the current node - except for the root node which has undef in there. And node_pos is the position of this node in the array that it is in (think: $node == $node->[node_parent]->[node_children]->[$node->[node_pos]] )
1295  
1296 Nodes are structured as follows:
1297  
1298 =head2 Root Node
1299  
1300 The L<root node|HTML::Object::Root> is just an element node with no parent.
1301  
1302 [
1303 undef, # node_parent - check for undef to identify root node
1304 undef, # node_pos
1305 undef, # node_prefix
1306 [ ... ], # node_children (see below)
1307 ]
1308  
1309 =head2 L<Element|HTML::Object::Element> Node
1310  
1311 [
1312 $parent, # node_parent
1313 <position in current array>, # node_pos
1314 'xxx', # node_prefix - namespace prefix on this element
1315 [ ... ], # node_children
1316 'yyy', # node_name - element tag name
1317 [ ... ], # node_attribs - attributes on this element
1318 [ ... ], # node_namespaces - namespaces currently in scope
1319 ]
1320  
1321 =head2 L<Attribute|HTML::Object::Attribute> Node
1322  
1323 [
1324 $parent, # node_parent - the element node
1325 <position in current array>, # node_pos
1326 'xxx', # node_prefix - namespace prefix on this element
1327 'href', # node_key - attribute name
1328 'ftp://ftp.com/', # node_value - value in the node
1329 ]
1330  
1331 =head2 L<Text|HTML::Object::Text> Nodes
1332  
1333 [
1334 $parent,
1335 <pos>,
1336 'This is some text' # node_text - the text in the node
1337 ]
1338  
1339 =head2 L<Comment|HTML::Object::Comment> Nodes
1340  
1341 [
1342 $parent,
1343 <pos>,
1344 'This is a comment' # node_comment
1345 ]
1346  
1347 =head1 AUTHOR
1348  
1349 Jacques Deguest E<lt>F<jack@deguest.jp>E<gt>
1350  
1351 =head1 SEE ALSO
1352  
1353 L<HTML::Object::XPath::Boolean>, L<HTML::Object::XPath::Expr>, L<HTML::Object::XPath::Function>, L<HTML::Object::XPath::Literal>, L<HTML::Object::XPath::LocationPath>, L<HTML::Object::XPath::NodeSet>, L<HTML::Object::XPath::Number>, L<HTML::Object::XPath::Root>, L<HTML::Object::XPath::Step>, L<HTML::Object::XPath::Variable>
1354  
1355 L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/XPath/Introduction_to_using_XPath_in_JavaScript>
1356  
1357 =head1 COPYRIGHT & LICENSE
1358  
1359 Copyright(c) 2021 DEGUEST Pte. Ltd.
1360  
1361 You can use, copy, modify and redistribute this package and associated files under the same terms as Perl itself.
1362  
1363 =cut
« lib/HTML/Object/DOM/TextTrackList.pm ↑ Summary lib/HTML/Object/DOM/Implementation.pm »