blib/lib/HTML/Object/XPath/Expr.pm untested

filter: Statementstmt 0.0% 0 / 307 Branchbran 0.0% 0 / 124 Conditioncond 0.0% 0 / 55 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 43 Podpod 0.0% 0 / 36 totaltotal 0.0% 0 / 565 CC 63 SCAR 42.7
CC
63
Cov
0%
op_equals
272.0
op_ge
110.0
op_gt
110.0
CRAP 71.3
1 ##----------------------------------------------------------------------------
2 ## HTML Object - ~/lib/HTML/Object/XPath/Expr.pm
3 ## Version v0.2.0
4 ## Copyright(c) 2021 DEGUEST Pte. Ltd.
5 ## Author: Jacques Deguest <jack@deguest.jp>
6 ## Created 2021/12/05
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::Expr;
15 BEGIN
16 {
17 use strict;
18 use warnings;
19 warnings::register_categories( 'HTML::Object' );
20 use parent qw( Module::Generic );
21 use vars qw( $TRUE $FALSE $BASE_CLASS $DEBUG $VERSION );
22 use HTML::Object::XPath::Boolean;
23 our $TRUE = HTML::Object::XPath::Boolean->True;
24 our $FALSE = HTML::Object::XPath::Boolean->False;
25 our $BASE_CLASS = 'HTML::Object::XPath';
26 our $DEBUG = 0;
27 our $VERSION = 'v0.2.0';
28 };
29  
30 use strict;
31 use warnings;
32  
33 sub init
34 {
35 my $self = shift( @_ );
36 # XPath Parser -> HTML::Object::XPath
37 my $pp = shift( @_ );
38 $self->{pp} = $pp;
39 $self->{predicates} = [];
40 $self->{_init_strict_use_sub} = 1;
41 $self->SUPER::init( @_ ) || return( $self->pass_error );
42 return( $self );
43 }
44  
45 sub as_string
46 {
47 my $self = shift( @_ );
48 # Use of uninitialized value! grrr
49 # local $^W;
50 my $string = '(' . $self->{lhs}->as_string;
51 $string .= ' ' . $self->{op} . ' ' if( defined( $self->{op} ) );
52 $string .= $self->{rhs}->as_string if( defined( $self->{rhs} ) );
53 $string .= ')';
54 foreach my $predicate ( @{$self->{predicates}} )
55 {
56 $string .= '[' . $predicate->as_string . ']';
57 }
58 return( $string );
59 }
60  
61 sub as_xml
62 {
63 my $self = shift( @_ );
64 # Use of uninitialized value! grrr
65 # local $^W;
66 my $string;
67 if( defined( $self->{op} ) )
68 {
69 $string .= $self->op_xml();
70 }
71 else
72 {
73 $string .= $self->{lhs}->as_xml();
74 }
75 foreach my $predicate ( @{$self->{predicates}} )
76 {
77 $string .= "<Predicate>\n" . $predicate->as_xml() . "</Predicate>\n";
78 }
79 return( $string );
80 }
81  
82 sub evaluate
83 {
84 my $self = shift( @_ );
85 # HTML::Object::XPath::NodeSet
86 my $node = shift( @_ );
87
88 # If there's an op, result is result of that op.
89 # If no op, just resolve Expr
90
91 # warn "Evaluate Expr: ", $self->as_string, "\n";
92
93 my $results;
94
95 if( $self->{op} )
96 {
97 die( "No RHS of ", $self->as_string ) unless( $self->{rhs} );
98 $results = $self->op_eval( $node );
99 }
100 else
101 {
102 # HTML::Object::XPath::LocationPath
103 $results = $self->{lhs}->evaluate( $node );
104 }
105
106 if( !$self->predicates->is_empty )
107 {
108 if( !$self->_is_a( $results => 'HTML::Object::XPath::NodeSet' ) )
109 {
110 die( "Can't have predicates execute on object type: " . ref( $results ) );
111 }
112
113 # filter initial nodeset by each predicate
114 foreach my $predicate ( @{$self->{predicates}} )
115 {
116 $results = $self->filter_by_predicate( $results, $predicate );
117 }
118 }
119 return( $results );
120 }
121  
122 sub filter_by_predicate
123 {
124 my $self = shift( @_ );
125 my( $nodeset, $predicate ) = @_;
126
127 # See spec section 2.4, paragraphs 2 & 3:
128 # For each node in the node-set to be filtered, the predicate Expr
129 # is evaluated with that node as the context node, with the number
130 # of nodes in the node set as the context size, and with the
131 # proximity position of the node in the node set with respect to
132 # the axis as the context position.
133
134 # use ref because nodeset has a bool context
135 if( !ref( $nodeset ) )
136 {
137 die( "No nodeset!!!" );
138 }
139
140 # warn "Filter by predicate: $predicate\n";
141
142 my $newset = $self->new_nodeset->new();
143
144 for( my $i = 1; $i <= $nodeset->size; $i++ )
145 {
146 # set context set each time 'cos a loc-path in the expr could change it
147 $self->{pp}->_set_context_set( $nodeset );
148 $self->{pp}->_set_context_pos( $i );
149 my $result = $predicate->evaluate( $nodeset->get_node( $i ) );
150 if( $self->_is_a( $result => 'HTML::Object::XPath::Boolean' ) )
151 {
152 if( $result->value )
153 {
154 $newset->push( $nodeset->get_node( $i ) );
155 }
156 }
157 elsif( $self->_is_a( $result => 'HTML::Object::XPath::Number' ) )
158 {
159 if( $result->value == $i )
160 {
161 $newset->push( $nodeset->get_node( $i ) );
162 }
163 }
164 else
165 {
166 if( $result->to_boolean->value )
167 {
168 $newset->push( $nodeset->get_node( $i ) );
169 }
170 }
171 }
172 return( $newset );
173 }
174  
175 sub get_lhs { return( $_[0]->{lhs} ); }
176  
177 sub get_rhs { return( $_[0]->{rhs} ); }
178  
179 sub get_op { return( $_[0]->{op} ); }
180  
181 sub new_literal { return( shift->_class_for( 'Literal' )->new( @_ ) ); }
182  
183 sub new_nodeset { return( shift->_class_for( 'NodeSet' )->new( @_ ) ); }
184  
185 sub new_number { return( shift->_class_for( 'Number' )->new( @_ ) ); }
186  
187 sub op_and
188 {
189 my $self = shift( @_ );
190 my( $node, $lhs, $rhs ) = @_;
191 if( ! $lhs->evaluate( $node )->to_boolean->value )
192 {
193 return( $FALSE );
194 }
195 else
196 {
197 return( $rhs->evaluate( $node )->to_boolean );
198 }
199 }
200  
201 sub op_div
202 {
203 my $self = shift( @_ );
204 my( $node, $lhs, $rhs ) = @_;
205 my $lh_results = $lhs->evaluate( $node );
206 my $rh_results = $rhs->evaluate( $node );
207  
208 my $result = eval{ $lh_results->to_number->value / $rh_results->to_number->value; };
209 if( $@ )
210 {
211 # assume divide by zero
212 # This is probably a terrible way to handle this!
213 # Ah well... who wants to live forever...
214 return( $self->new_literal( 'Infinity' ) );
215 }
216 return( $self->new_number( $result ) );
217 }
218  
219 sub op_equals
220 {
221 my $self = shift( @_ );
222 my( $node, $lhs, $rhs ) = @_;
223  
224 my $lh_results = $lhs->evaluate( $node );
225 my $rh_results = $rhs->evaluate( $node );
226
227 if( $lh_results->isa( 'HTML::Object::XPath::NodeSet' ) &&
228 $rh_results->isa( 'HTML::Object::XPath::NodeSet' ) )
229 {
230 # True if and only if there is a node in the
231 # first set and a node in the second set such
232 # that the result of performing the comparison
233 # on the string-values of the two nodes is true.
234 foreach my $lhnode ( $lh_results->get_nodelist )
235 {
236 foreach my $rhnode ( $rh_results->get_nodelist )
237 {
238 if( $lhnode->string_value eq $rhnode->string_value )
239 {
240 return( $TRUE );
241 }
242 }
243 }
244 return( $FALSE );
245 }
246 elsif( ( $lh_results->isa( 'HTML::Object::XPath::NodeSet' ) ||
247 $rh_results->isa( 'HTML::Object::XPath::NodeSet' ) ) &&
248 ( !$lh_results->isa( 'HTML::Object::XPath::NodeSet' ) ||
249 !$rh_results->isa( 'HTML::Object::XPath::NodeSet' ) ) )
250 {
251 # (that says: one is a nodeset, and one is not a nodeset)
252 my( $nodeset, $other );
253 if( $lh_results->isa( 'HTML::Object::XPath::NodeSet' ) )
254 {
255 $nodeset = $lh_results;
256 $other = $rh_results;
257 }
258 else
259 {
260 $nodeset = $rh_results;
261 $other = $lh_results;
262 }
263
264 # True if and only if there is a node in the
265 # nodeset such that the result of performing
266 # the comparison on <type>(string_value($node))
267 # is true.
268 if( $self->_is_a( $other => 'HTML::Object::XPath::Number' ) )
269 {
270 foreach my $node ( $nodeset->get_nodelist )
271 {
272 if( $node->string_value == $other->value )
273 {
274 return( $TRUE );
275 }
276 }
277 }
278 elsif( $self->_is_a( $other => 'HTML::Object::XPath::Literal' ) )
279 {
280 foreach my $node ( $nodeset->get_nodelist )
281 {
282 if( $node->string_value eq $other->value )
283 {
284 return( $TRUE );
285 }
286 }
287 }
288 elsif( $self->_is_a( $other => 'HTML::Object::XPath::Boolean' ) )
289 {
290 if( $nodeset->to_boolean->value == $other->value )
291 {
292 return( $TRUE );
293 }
294 }
295 return( $FALSE );
296 }
297 # Neither is a nodeset
298 else
299 {
300 if( $lh_results->isa( 'HTML::Object::XPath::Boolean' ) ||
301 $rh_results->isa( 'HTML::Object::XPath::Boolean' ) )
302 {
303 # if either is a boolean
304 if( $lh_results->to_boolean->value == $rh_results->to_boolean->value )
305 {
306 return( $TRUE );
307 }
308 return( $FALSE );
309 }
310 elsif( $lh_results->isa( 'HTML::Object::XPath::Number' ) ||
311 $rh_results->isa( 'HTML::Object::XPath::Number' ) )
312 {
313 # if either is a number
314 # 'number' might result in undef
315 local $^W;
316 if ($lh_results->to_number->value == $rh_results->to_number->value )
317 {
318 return( $TRUE );
319 }
320 return( $FALSE );
321 }
322 else
323 {
324 if( $lh_results->to_literal->value eq $rh_results->to_literal->value )
325 {
326 return( $TRUE );
327 }
328 return( $FALSE );
329 }
330 }
331 }
332  
333 sub op_eval
334 {
335 my $self = shift( @_ );
336 my $node = shift( @_ );
337 my $op = $self->{op};
338 my $map =
339 {
340 'or' => 'op_or',
341 'and' => 'op_and',
342 '=~' => 'op_match',
343 '!~' => 'op_not_match',
344 '=' => 'op_equals',
345 '!=' => 'op_nequals',
346 '<=' => 'op_le',
347 '>=' => 'op_ge',
348 '>' => 'op_gt',
349 '<' => 'op_lt',
350 '+' => 'op_plus',
351 '-' => 'op_minus',
352 'div' => 'op_div',
353 'mod' => 'op_mod',
354 '*' => 'op_mult',
355 '|' => 'op_union',
356 };
357 die( "No such operator, or operator unimplemented in ", $self->as_string, "\n" ) if( !CORE::exists( $map->{ $op } ) );
358 my $mod = $map->{ $op };
359 return( $self->$mod( $node, $self->{lhs}, $self->{rhs} ) );
360 }
361  
362 sub op_ge
363 {
364 my $self = shift( @_ );
365 my( $node, $lhs, $rhs ) = @_;
366  
367 my $lh_results = $lhs->evaluate( $node );
368 my $rh_results = $rhs->evaluate( $node );
369
370 if( $lh_results->isa( 'HTML::Object::XPath::NodeSet' ) &&
371 $rh_results->isa( 'HTML::Object::XPath::NodeSet' ) )
372 {
373 foreach my $lhnode ( $lh_results->get_nodelist )
374 {
375 foreach my $rhnode ( $rh_results->get_nodelist )
376 {
377 my $lhNum = $self->new_number( $lhnode->string_value );
378 my $rhNum = $self->new_number( $rhnode->string_value );
379 if( $lhNum->value >= $rhNum->value )
380 {
381 return( $TRUE );
382 }
383 }
384 }
385 return( $FALSE );
386 }
387 elsif( ( $lh_results->isa( 'HTML::Object::XPath::NodeSet' ) ||
388 $rh_results->isa( 'HTML::Object::XPath::NodeSet' ) ) &&
389 ( !$lh_results->isa( 'HTML::Object::XPath::NodeSet' ) ||
390 !$rh_results->isa( 'HTML::Object::XPath::NodeSet' ) ) )
391 {
392 # (that says: one is a nodeset, and one is not a nodeset)
393 if( $lh_results->isa( 'HTML::Object::XPath::NodeSet' ) )
394 {
395 foreach my $node ( $lh_results->get_nodelist )
396 {
397 if( $node->to_number->value >= $rh_results->to_number->value )
398 {
399 return( $TRUE );
400 }
401 }
402 }
403 else
404 {
405 foreach my $node ( $rh_results->get_nodelist )
406 {
407 if( $lh_results->to_number->value >= $node->to_number->value )
408 {
409 return( $TRUE );
410 }
411 }
412 }
413 return( $FALSE );
414 }
415 # Neither is a nodeset
416 else
417 {
418 if( $lh_results->isa( 'HTML::Object::XPath::Boolean' ) ||
419 $rh_results->isa( 'HTML::Object::XPath::Boolean' ) )
420 {
421 # if either is a boolean
422 if( $lh_results->to_boolean->to_number->value >= $rh_results->to_boolean->to_number->value )
423 {
424 return( $TRUE );
425 }
426 }
427 else
428 {
429 if( $lh_results->to_number->value >= $rh_results->to_number->value )
430 {
431 return( $TRUE );
432 }
433 }
434 return( $FALSE );
435 }
436 }
437  
438 sub op_gt
439 {
440 my $self = shift( @_ );
441 my( $node, $lhs, $rhs ) = @_;
442  
443 my $lh_results = $lhs->evaluate( $node );
444 my $rh_results = $rhs->evaluate( $node );
445
446 if( $lh_results->isa( 'HTML::Object::XPath::NodeSet' ) &&
447 $rh_results->isa( 'HTML::Object::XPath::NodeSet' ) )
448 {
449 foreach my $lhnode ( $lh_results->get_nodelist )
450 {
451 foreach my $rhnode ( $rh_results->get_nodelist )
452 {
453 my $lhNum = $self->new_number( $lhnode->string_value );
454 my $rhNum = $self->new_number( $rhnode->string_value );
455 if( $lhNum->value > $rhNum->value )
456 {
457 return( $TRUE );
458 }
459 }
460 }
461 return( $FALSE );
462 }
463 elsif( ( $lh_results->isa( 'HTML::Object::XPath::NodeSet' ) ||
464 $rh_results->isa( 'HTML::Object::XPath::NodeSet' ) ) &&
465 ( !$lh_results->isa( 'HTML::Object::XPath::NodeSet' ) ||
466 !$rh_results->isa( 'HTML::Object::XPath::NodeSet' ) ) )
467 {
468 # (that says: one is a nodeset, and one is not a nodeset)
469 if( $lh_results->isa( 'HTML::Object::XPath::NodeSet' ) )
470 {
471 foreach my $node ( $lh_results->get_nodelist )
472 {
473 if( $node->to_number->value > $rh_results->to_number->value )
474 {
475 return( $TRUE );
476 }
477 }
478 }
479 else
480 {
481 foreach my $node ( $rh_results->get_nodelist )
482 {
483 if( $lh_results->to_number->value > $node->to_number->value )
484 {
485 return( $TRUE );
486 }
487 }
488 }
489 return( $FALSE );
490 }
491 # Neither is a nodeset
492 else
493 {
494 if( $lh_results->isa( 'HTML::Object::XPath::Boolean' ) ||
495 $rh_results->isa( 'HTML::Object::XPath::Boolean' ) )
496 {
497 # if either is a boolean
498 if( $lh_results->to_boolean->value > $rh_results->to_boolean->value )
499 {
500 return( $TRUE );
501 }
502 }
503 else
504 {
505 if( $lh_results->to_number->value > $rh_results->to_number->value )
506 {
507 return( $TRUE );
508 }
509 }
510 return( $FALSE );
511 }
512 }
513  
514 sub op_le
515 {
516 my $self = shift( @_ );
517 my( $node, $lhs, $rhs ) = @_;
518 return( $self->op_ge( $node, $rhs, $lhs ) );
519 }
520  
521 sub op_lt
522 {
523 my $self = shift( @_ );
524 my( $node, $lhs, $rhs ) = @_;
525 return( $self->op_gt( $node, $rhs, $lhs ) );
526 }
527  
528 sub op_match
529 {
530 my $self = shift( @_ );
531 my( $node, $lhs, $rhs ) = @_;
532  
533 my $lh_results = $lhs->evaluate( $node );
534 my $rh_results = $rhs->evaluate( $node );
535 my $rh_value = $rh_results->string_value;
536  
537 if( $lh_results->isa( 'HTML::Object::XPath::NodeSet' ) )
538 {
539 foreach my $lhnode ( $lh_results->get_nodelist )
540 {
541 # / is important here, regexp is / delimited
542 if( $lhnode->string_value =~ m/$rh_value/ )
543 {
544 return( $TRUE );
545 }
546 }
547 return( $FALSE );
548 }
549 else
550 {
551 return(
552 $lh_results->string_value =~ m/$rh_value/
553 ? $TRUE
554 : $FALSE
555 );
556 }
557 }
558
559 sub op_minus
560 {
561 my $self = shift( @_ );
562 my( $node, $lhs, $rhs ) = @_;
563 my $lh_results = $lhs->evaluate( $node );
564 my $rh_results = $rhs->evaluate( $node );
565
566 my $result = $lh_results->to_number->value - $rh_results->to_number->value;
567 return( $self->new_number( $result ) );
568 }
569  
570 sub op_mod
571 {
572 my $self = shift( @_ );
573 my( $node, $lhs, $rhs ) = @_;
574 my $lh_results = $lhs->evaluate( $node );
575 my $rh_results = $rhs->evaluate( $node );
576
577 my $result = $lh_results->to_number->value % $rh_results->to_number->value;
578 return( $self->new_number( $result ) );
579 }
580  
581 sub op_mult
582 {
583 my $self = shift( @_ );
584 my( $node, $lhs, $rhs ) = @_;
585 my $lh_results = $lhs->evaluate( $node );
586 my $rh_results = $rhs->evaluate( $node );
587
588 my $result = $lh_results->to_number->value * $rh_results->to_number->value;
589 return( $self->new_number( $result ) );
590 }
591  
592 sub op_nequals
593 {
594 my $self = shift( @_ );
595 my( $node, $lhs, $rhs ) = @_;
596 if( $self->op_equals( $node, $lhs, $rhs)->value )
597 {
598 return( $FALSE );
599 }
600 return( $TRUE );
601 }
602  
603 sub op_not_match
604 {
605 my $self = shift( @_ );
606 my( $node, $lhs, $rhs ) = @_;
607  
608 my $lh_results = $lhs->evaluate( $node );
609 my $rh_results = $rhs->evaluate( $node );
610 my $rh_value = $rh_results->string_value;
611
612 if( $lh_results->isa( 'HTML::Object::XPath::NodeSet' ) )
613 {
614 foreach my $lhnode ( $lh_results->get_nodelist )
615 {
616 if( $lhnode->string_value !~ m/$rh_value/ )
617 {
618 return( $TRUE );
619 }
620 }
621 return( $FALSE );
622 }
623 else
624 {
625 return( $lh_results->string_value !~ m/$rh_value/ ? $TRUE : $FALSE );
626 }
627 }
628  
629 sub op_or
630 {
631 my $self = shift( @_ );
632 my( $node, $lhs, $rhs ) = @_;
633 if( $lhs->evaluate( $node )->to_boolean->value )
634 {
635 return( $TRUE );
636 }
637 else
638 {
639 return $rhs->evaluate( $node )->to_boolean;
640 }
641 }
642  
643 sub op_plus
644 {
645 my $self = shift( @_ );
646 my( $node, $lhs, $rhs ) = @_;
647 my $lh_results = $lhs->evaluate( $node );
648 my $rh_results = $rhs->evaluate( $node );
649
650 my $result = $lh_results->to_number->value + $rh_results->to_number->value;
651 return( $self->new_number( $result ) );
652 }
653  
654 sub op_union
655 {
656 my $self = shift( @_ );
657 my( $node, $lhs, $rhs ) = @_;
658 my $lh_result = $lhs->evaluate( $node );
659 my $rh_result = $rhs->evaluate( $node );
660
661 if( $lh_result->isa( 'HTML::Object::XPath::NodeSet' ) &&
662 $rh_result->isa( 'HTML::Object::XPath::NodeSet' ) )
663 {
664 my %found;
665 my $results = $self->new_nodeset;
666 foreach my $lhnode ( $lh_result->get_nodelist )
667 {
668 $found{ "$lhnode" }++;
669 $results->push( $lhnode );
670 }
671 foreach my $rhnode ( $rh_result->get_nodelist )
672 {
673 $results->push( $rhnode ) unless( exists( $found{ "$rhnode" } ) );
674 }
675 return( $results->sort->remove_duplicates );
676 }
677 die( "Both sides of a union must be Node Sets\n" );
678 }
679  
680 sub op_xml
681 {
682 my $self = shift( @_ );
683 my $op = $self->{op};
684  
685 my $map =
686 {
687 'and' => 'And',
688 'div' => 'Div',
689 'mod' => 'Mod',
690 'or' => 'Or',
691 '=' => 'Equals',
692 '!=' => 'NotEquals',
693 '<=' => 'LessThanOrEquals',
694 '>=' => 'GreaterThanOrEquals',
695 '>' => 'GreaterThan',
696 '<' => 'LessThan',
697 '+' => 'Plus',
698 '-' => 'Minus',
699 '*' => 'Multiply',
700 '|' => 'Union',
701 };
702 die( "No tag equivalent for operator \"$op\".\n" ) if( !CORE::exists( $map->{ $op } ) );
703 my $tag = $map->{ $op };
704 return( "<$tag>\n" . $self->{lhs}->as_xml() . $self->{rhs}->as_xml() . "</$tag>\n" );
705 }
706  
707 sub predicates { return( shift->_set_get_array_as_object( 'predicates', @_ ) ); }
708  
709 sub push_predicate
710 {
711 my $self = shift( @_ );
712
713 die( "Only 1 predicate allowed on FilterExpr in W3C XPath 1.0" ) if( @{$self->{predicates}} );
714 # push( @{$self->{predicates}}, $_[0] );
715 $self->predicates->push( $_[0] );
716 # Need to return $self
717 # return( $self );
718 return( $self->predicates->length );
719 }
720  
721 sub set_lhs
722 {
723 my $self = shift( @_ );
724 $self->{lhs} = $_[0];
725 }
726  
727 sub set_op
728 {
729 my $self = shift( @_ );
730 $self->{op} = $_[0];
731 }
732  
733 sub set_rhs
734 {
735 my $self = shift( @_ );
736 $self->{rhs} = $_[0];
737 }
738  
739 sub _class_for
740 {
741 my( $self, $mod ) = @_;
742 eval( "require ${BASE_CLASS}\::${mod};" );
743 die( $@ ) if( $@ );
744 # ${"${BASE_CLASS}\::${mod}\::DEBUG"} = $DEBUG;
745 eval( "\$${BASE_CLASS}\::${mod}\::DEBUG = " . ( $DEBUG // 0 ) );
746 return( "${BASE_CLASS}::${mod}" );
747 }
748  
749 1;
750 # NOTE: POD
751 __END__
752  
753 =encoding utf-8
754  
755 =head1 NAME
756  
757 HTML::Object::XPath::Expr - HTML Object XPath Expression
758  
759 =head1 SYNOPSIS
760  
761 use HTML::Object::XPath::Expr;
762 my $this = HTML::Object::XPath::Expr->new || die( HTML::Object::XPath::Expr->error, "\n" );
763  
764 =head1 VERSION
765  
766 v0.2.0
767  
768 =head1 DESCRIPTION
769  
770 This modules represents an L<HTML::Object::XPath> expression.
771  
772 =head1 METHODS
773  
774 =head2 new
775  
776 Provided with an L<HTML::Object::XPath> object and this returns a new L<HTML::Object::XPath::Expr> object.
777  
778 =head2 as_string
779  
780 Returns the expression as a string.
781  
782 =head2 as_xml
783  
784 Returns the expression as xml.
785  
786 =head2 evaluate
787  
788 Provided with a L<HTML::Object::XPath::NodeSet> object, and this will call L</op_eval> with an L<operator|/op> has been set, otherwise, it calls L<HTML::Object::XPath::LocationPath/evaluate> passing it the node set. It returns the result from either call.
789  
790 =head2 filter_by_predicate
791  
792 This takes a nodeset object and a predicate.
793  
794 For each node in the node-set to be filtered, the predicate Expr is evaluated with that node as the context node, with the number of nodes in the node set as the context size, and with the proximity position of the node in the node set with respect to the axis as the context position.
795  
796 It returns a new L<node set|HTML::Object::XPath::NodeSet> object.
797  
798 =head2 get_lhs
799  
800 Returns the L<HTML::Object::XPath::LocationPath> object for the left-hand side of the expression.
801  
802 =head2 get_rhs
803  
804 Returns the L<HTML::Object::XPath::LocationPath> object for the right-hand side of the expression.
805  
806 =head2 get_op
807  
808 Returns the current operator set for this expression.
809  
810 =head2 new_literal
811  
812 Returns a new L<literal object|HTML::Object::XPath::Literal>, passing it whatever argument was provided.
813  
814 =head2 new_nodeset
815  
816 Returns a new L<node set object|HTML::Object::XPath::NodeSet>, passing it whatever argument was provided.
817  
818 =head2 new_number
819  
820 Returns a new L<number object|HTML::Object::XPath::Number>, passing it whatever argument was provided.
821  
822 =head2 op_and
823  
824 Provided a L<node object|HTML::Object:Element>, a left-hand side and right-hand side L<LocationPath objects|HTML::Object::XPath::LocationPath> and this will evaluate the node using the left-hand side LocationPath object, and return L<false|HTML::Object::XPath::Boolean> if it failed, or otherwise it wil evaluate the node using the right-hand side LocationPath object and return its result.
825  
826 =head2 op_div
827  
828 Provided a L<node object|HTML::Object:Element>, a left-hand side and right-hand side L<LocationPath objects|HTML::Object::XPath::LocationPath> and this will attempt to divide the left-hand value by the right-hand value. If ther eis an error, it returns L<infinity|HTML::Object::XPath::Number>, otherwise it returns the value from the division as a L<number object|HTML::Object::XPath::Number>.
829  
830 =head2 op_equals
831  
832 Provided a L<node object|HTML::Object:Element>, a left-hand side and right-hand side L<LocationPath objects|HTML::Object::XPath::LocationPath> and this will check if the left-hand side LocationPath is equal tot he right-hand side LocationPath. Returns L<true|HTML::Object::XPath::Boolean> or L<false|HTML::Object::XPath::Boolean>
833  
834 =head2 op_eval
835  
836 This method will evaluate the L<node|HTML::Object::ELement> provided with the left-hand side and right-hand side L<LocationPath objects|HTML::Object::XPath::LocationPath> object also specified by calling the appropriate method in this module based on the operator value set with L</op>
837  
838 =head2 op_ge
839  
840 Provided a L<node object|HTML::Object:Element>, a left-hand side and right-hand side L<LocationPath objects|HTML::Object::XPath::LocationPath> and this will check if the left-hand side is greater or equal to the right-hand side.
841  
842 Returns L<true|HTML::Object::XPath::Boolean> or L<false|HTML::Object::XPath::Boolean>
843  
844 =head2 op_gt
845  
846 Provided a L<node object|HTML::Object:Element>, a left-hand side and right-hand side L<LocationPath objects|HTML::Object::XPath::LocationPath> and this will check if the left-hand side is greater than the right-hand side.
847  
848 Returns L<true|HTML::Object::XPath::Boolean> or L<false|HTML::Object::XPath::Boolean>
849  
850 =head2 op_le
851  
852 Provided a L<node object|HTML::Object:Element>, a left-hand side and right-hand side L<LocationPath objects|HTML::Object::XPath::LocationPath> and this will check if the left-hand side is lower or equal than the right-hand side.
853  
854 Returns L<true|HTML::Object::XPath::Boolean> or L<false|HTML::Object::XPath::Boolean>
855  
856 =head2 op_lt
857  
858 Provided a L<node object|HTML::Object:Element>, a left-hand side and right-hand side L<LocationPath objects|HTML::Object::XPath::LocationPath> and this will check if the left-hand side is lower than the right-hand side.
859  
860 Returns L<true|HTML::Object::XPath::Boolean> or L<false|HTML::Object::XPath::Boolean>
861  
862 =head2 op_match
863  
864 Provided a L<node object|HTML::Object:Element>, a left-hand side and right-hand side L<LocationPath objects|HTML::Object::XPath::LocationPath> and this will check if the left-hand side match the right-hand side as a regular expression.
865  
866 Returns L<true|HTML::Object::XPath::Boolean> or L<false|HTML::Object::XPath::Boolean>
867  
868 =head2 op_minus
869  
870 Provided a L<node object|HTML::Object:Element>, a left-hand side and right-hand side L<LocationPath objects|HTML::Object::XPath::LocationPath> and this will subtract the right-hand side from the left-hand side and return the result as a L<number object|HTML::Object::XPath::Number>.
871  
872 =head2 op_mod
873  
874 Provided a L<node object|HTML::Object:Element>, a left-hand side and right-hand side L<LocationPath objects|HTML::Object::XPath::LocationPath> and this will get the modulo between the left-hand side and the right-hand side and return the result as a L<number object|HTML::Object::XPath::Number>.
875  
876 =head2 op_mult
877  
878 Provided a L<node object|HTML::Object:Element>, a left-hand side and right-hand side L<LocationPath objects|HTML::Object::XPath::LocationPath> and this will multiply the right-hand side by the left-hand side and return the result as a L<number object|HTML::Object::XPath::Number>.
879  
880 =head2 op_nequals
881  
882 Provided a L<node object|HTML::Object:Element>, a left-hand side and right-hand side L<LocationPath objects|HTML::Object::XPath::LocationPath> and this will return L<true|HTML::Object::XPath::Boolean> if the left-hand side is not equal to the right-hand side, or L<false|HTML::Object::XPath::Boolean> otherwise.
883  
884 =head2 op_not_match
885  
886 Provided a L<node object|HTML::Object:Element>, a left-hand side and right-hand side L<LocationPath objects|HTML::Object::XPath::LocationPath> and this will check if the left-hand side does B<not> match the right-hand side as a regular expression.
887  
888 Returns L<true|HTML::Object::XPath::Boolean> or L<false|HTML::Object::XPath::Boolean>
889  
890 =head2 op_or
891  
892 Provided a L<node object|HTML::Object:Element>, a left-hand side and right-hand side L<LocationPath objects|HTML::Object::XPath::LocationPath> and this will evaluate the node using the left-hand side LocationPath and return L<true|HTML::Object::XPath::Boolean> if it worked, or otherwise return the value from evaluating the node using the right-hand side LocationPath.
893  
894 =head2 op_plus
895  
896 Provided a L<node object|HTML::Object:Element>, a left-hand side and right-hand side L<LocationPath objects|HTML::Object::XPath::LocationPath> and this will add the right-hand side to the left-hand side and return the result as a L<number object|HTML::Object::XPath::Number>.
897  
898 =head2 op_union
899  
900 Provided a L<node object|HTML::Object:Element>, a left-hand side and right-hand side L<LocationPath objects|HTML::Object::XPath::LocationPath> and this will evaluate the node both by the left-hand side and right-hand side LocationPath and return a new L<node set array object|HTML::Object::XPath::NodeSet> containing all the unique nodes resulting from those both evaluation.
901  
902 =head2 op_xml
903  
904 Provided with an operator and this returns its xml equivalent.
905  
906 Operators supported are: C<and>, C<div>, C<mod>, C<or>, C<=>, C<!=>, C<<=>, C<>=>, C<>>, C<<>, C<+>, C<->, C<*>, C<|>
907  
908 They will be converted respectively to: C<And>, C<Div>, C<Mod>, C<Or>, C<Equals>, C<NotEquals>, C<LessThanOrEquals>, C<GreaterThanOrEquals>, C<GreaterThan>, C<LessThan>, C<Plus>, C<Minus>, C<Multiply>, C<Union>
909  
910 =head2 predicates
911  
912 Set or get the predicates as an L<array object|Module::Generic::Array>
913  
914 =head2 push_predicate
915  
916 Add the predicate to the list and will raise an exception if more than one predicate was provided.
917  
918 =head2 set_lhs
919  
920 Set the left-hand side L<LocationPath|HTML::Object::XPath::LocationPath>
921  
922 =head2 set_op
923  
924 Set the operator for this expression.
925  
926 =head2 set_rhs
927  
928 Set the right-hand side L<LocationPath|HTML::Object::XPath::LocationPath>
929  
930 =head1 AUTHOR
931  
932 Jacques Deguest E<lt>F<jack@deguest.jp>E<gt>
933  
934 =head1 SEE ALSO
935  
936 L<HTML::Object::XPath>, 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>
937  
938 =head1 COPYRIGHT & LICENSE
939  
940 Copyright(c) 2021 DEGUEST Pte. Ltd.
941  
942 All rights reserved
943  
944 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
945  
946 =cut