File Coverage

lib/HTML/Object/XPath/LocationPath.pm
Criterion Covered Total %
statement 43 63 68.2
branch 5 14 35.7
condition 2 5 40.0
subroutine 12 16 75.0
pod 8 8 100.0
total 70 106 66.0


line stmt bran cond sub pod time code
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 8     8   60 use strict;
  8         24  
  8         264  
18 8     8   67 use warnings;
  8         29  
  8         256  
19 8     8   52 use parent qw( Module::Generic );
  8         267  
  8         46  
20 8     8   525 use vars qw( $BASE_CLASS $DEBUG $VERSION );
  8         36  
  8         629  
21 8     8   229 our $BASE_CLASS = 'HTML::Object::XPath';
22 8         41 our $DEBUG = 0;
23 8         186 our $VERSION = 'v0.2.0';
24             };
25              
26 8     8   70 use strict;
  8         25  
  8         171  
27 8     8   42 use warnings;
  8         27  
  8         5040  
28              
29             sub new
30             {
31 89     89 1 216 my $this = shift( @_ );
32 89   33     651 return( bless( [] => ( ref( $this ) || $this ) ) );
33             }
34              
35             sub as_string
36             {
37 0     0 1 0 my $self = shift( @_ );
38 0         0 my $string;
39 0         0 for( my $i = 0; $i < @$self; $i++ )
40             {
41 0         0 $string .= $self->[ $i ]->as_string;
42 0 0       0 $string .= '/' if( $self->[ $i + 1 ] );
43             }
44 0         0 return( $string );
45             }
46              
47             sub as_xml
48             {
49 0     0 1 0 my $self = shift( @_ );
50 0         0 my $string = "<LocationPath>\n";
51            
52 0         0 for (my $i = 0; $i < @$self; $i++ )
53             {
54 0         0 $string .= $self->[ $i ]->as_xml;
55             }
56 0         0 $string .= "</LocationPath>\n";
57 0         0 return( $string );
58             }
59              
60             sub evaluate
61             {
62 248     248 1 465 my $self = shift( @_ );
63             # context _MUST_ be a single node
64 248         417 my $context = shift( @_ );
65 248 50       2383 die( "No context" ) unless( $context );
66 248 50       773 if( $self->debug )
67             {
68 0         0 my( $p, $f, $l ) = caller;
69             }
70            
71             # I _think_ this is how it should work :)
72 248         6242 my $nodeset = $self->new_nodeset();
73 248         883 $nodeset->push( $context );
74            
75 248         675 foreach my $step ( @$self )
76             {
77             # For each step
78             # evaluate the step with the nodeset
79 441         3867 my $pos = 1;
80             # die( "Looping !\n" ) if( ref( $step ) eq ref( $self ) );
81 441 50       1823 die( "Looping !\n" ) if( $step eq $self );
82 441         1567 $nodeset = $step->evaluate( $nodeset );
83             }
84 245         5145 return( $nodeset );
85             }
86              
87 248     248 1 697 sub new_nodeset { return( shift->_class_for( 'NodeSet' )->new( @_ ) ); }
88              
89 0     0 1 0 sub new_root { return( shift->_class_for( 'Root' )->new( @_ ) ); }
90              
91             # sub push { return( CORE::push( @{$_[0]}, @_ ) ); }
92             sub push
93             {
94 169     169 1 308 my $self = shift( @_ );
95 169 50       499 if( $self->debug )
96             {
97 0         0 my( $p, $f, $l ) = caller;
98 0         0 for( @_ )
99             {
100 0 0       0 if( ref( $_ ) eq ref( $self ) )
101             {
102 0         0 die( "A LocationPath object was added to its own stack!\n" );
103             }
104             }
105             }
106 169         4184 return( CORE::push( @$self, @_ ) );
107             }
108              
109             sub set_root
110             {
111 0     0 1 0 my $self = shift( @_ );
112 0         0 return( unshift( @$self, $self->new_root ) );
113             }
114              
115             sub _class_for
116             {
117 248     248   506 my( $self, $mod ) = @_;
118 248         13730 eval( "require ${BASE_CLASS}\::${mod};" );
119 248 50       1073 die( $@ ) if( $@ );
120             # ${"${BASE_CLASS}\::${mod}\::DEBUG"} = $DEBUG;
121 248   50     11189 eval( "\$${BASE_CLASS}\::${mod}\::DEBUG = " . ( $DEBUG // 0 ) );
122 248         1886 return( "${BASE_CLASS}::${mod}" );
123             }
124              
125             1;
126             # NOTE: POD
127             __END__
128              
129             =encoding utf-8
130              
131             =head1 NAME
132              
133             HTML::Object::XPath::LocationPath - HTML Object XPath Location Path
134              
135             =head1 SYNOPSIS
136              
137             use HTML::Object::XPath::LocationPath;
138             my $this = HTML::Object::XPath::LocationPath->new ||
139             die( HTML::Object::XPath::LocationPath->error, "\n" );
140              
141             =head1 VERSION
142              
143             v0.2.0
144              
145             =head1 DESCRIPTION
146              
147             This module represents a XML LocationPath.
148              
149             =head1 CONSTRUCTOR
150              
151             Takes no argument and returns a new array-based object.
152              
153             =head1 METHODS
154              
155             =head2 as_string
156              
157             For each element in it the current object array, this will call C<as_string> and concatenate those strings separated by C</>. It returns the result as a regular perl string.
158              
159             =head2 as_xml
160              
161             Calls C<as_xml> on each elements in the current object array and returns the concatenated string enclosed in <LocationPath> and </LocationPath>
162              
163             =head2 evaluate
164              
165             Provided with a L<context|HTML::Object::Element>, and this will call C<evaluate> for each object in the stack. It returns a new L<HTML::Object::XPath::NodeSet> object containing the result with the C<context> initially provided as the first in the node set.
166              
167             =head2 new_nodeset
168              
169             Returns a new L<HTML::Object::XPath::NodeSet> passing it whatever argument was provided.
170              
171             =head2 new_root
172              
173             Returns a new L<HTML::Object::XPath::Root> passing it whatever argument was provided.
174              
175             =head2 push
176              
177             Add the elements provided to the object array.
178              
179             =head2 set_root
180              
181             Add a new L<root object|HTML::Object::XPath::Root> as the first element of our internal array.
182              
183             =head1 AUTHOR
184              
185             Jacques Deguest E<lt>F<jack@deguest.jp>E<gt>
186              
187             =head1 SEE ALSO
188              
189             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>
190              
191             =head1 COPYRIGHT & LICENSE
192              
193             Copyright(c) 2021 DEGUEST Pte. Ltd.
194              
195             All rights reserved
196              
197             This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
198              
199             =cut