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

filter: Statementstmt 0.0% 0 / 69 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 7 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 16 Podpod 0.0% 0 / 10 totaltotal 0.0% 0 / 114 CC 7 SCAR 17.0
CC
7
Cov
0%
evaluate
12.0
push
12.0
as_string
6.0
CRAP 5.5
1 ##----------------------------------------------------------------------------
2 ## HTML Object - ~/lib/HTML/Object/XPath/LocationPath.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::LocationPath;
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( $BASE_CLASS $DEBUG $VERSION );
22 our $BASE_CLASS = 'HTML::Object::XPath';
23 our $DEBUG = 0;
24 our $VERSION = 'v0.2.0';
25 };
26  
27 use strict;
28 use warnings;
29  
30 sub new
31 {
32 my $this = shift( @_ );
33 return( bless( [] => ( ref( $this ) || $this ) ) );
34 }
35  
36 sub as_string
37 {
38 my $self = shift( @_ );
39 my $string = '';
40 for( my $i = 0; $i < @$self; $i++ )
41 {
42 $string .= ( $self->[ $i ]->as_string // '' );
43 $string .= '/' if( $self->[ $i + 1 ] );
44 }
45 return( $string );
46 }
47  
48 sub as_xml
49 {
50 my $self = shift( @_ );
51 my $string = "<LocationPath>\n";
52  
53 for (my $i = 0; $i < @$self; $i++ )
54 {
55 $string .= $self->[ $i ]->as_xml;
56 }
57 $string .= "</LocationPath>\n";
58 return( $string );
59 }
60  
61 sub evaluate
62 {
63 my $self = shift( @_ );
64 # context _MUST_ be a single node
65 my $context = shift( @_ );
66 die( "No context" ) unless( $context );
67  
68 # I _think_ this is how it should work :)
69 my $nodeset = $self->new_nodeset();
70 $nodeset->push( $context );
71  
72 foreach my $step ( @$self )
73 {
74 # For each step
75 # evaluate the step with the nodeset
76 my $pos = 1;
77 # die( "Looping !\n" ) if( ref( $step ) eq ref( $self ) );
78 die( "Looping !\n" ) if( $step eq $self );
79 $nodeset = $step->evaluate( $nodeset );
80 }
81 return( $nodeset );
82 }
83  
84 sub new_nodeset { return( shift->_class_for( 'NodeSet' )->new( @_ ) ); }
85  
86 sub new_root { return( shift->_class_for( 'Root' )->new( @_ ) ); }
87  
88 # sub push { return( CORE::push( @{$_[0]}, @_ ) ); }
89 sub push
90 {
91 my $self = shift( @_ );
92 if( $self->debug )
93 {
94 for( @_ )
95 {
96 if( ref( $_ ) eq ref( $self ) )
97 {
98 die( "A LocationPath object was added to its own stack!\n" );
99 }
100 }
101 }
102 return( CORE::push( @$self, @_ ) );
103 }
104  
105 sub set_root
106 {
107 my $self = shift( @_ );
108 return( unshift( @$self, $self->new_root ) );
109 }
110  
111 sub _class_for
112 {
113 my( $self, $mod ) = @_;
114 eval( "require ${BASE_CLASS}\::${mod};" );
115 die( $@ ) if( $@ );
116 # ${"${BASE_CLASS}\::${mod}\::DEBUG"} = $DEBUG;
117 eval( "\$${BASE_CLASS}\::${mod}\::DEBUG = " . ( $DEBUG // 0 ) );
118 return( "${BASE_CLASS}::${mod}" );
119 }
120  
121 1;
122 # NOTE: POD
123 __END__