lib/HTML/Object/XPath/NodeSet.pm

filter: Statementstmt 79.4% 81 / 102 Branchbran 62.5% 5 / 8 Conditioncond 60.0% 3 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 73.5% 25 / 34 Podpod 100% 24 / 24 totaltotal 78.4% 138 / 176 CC 8 SCAR 5.1
CC
8
Cov
77%
_class_for
3.1
remove_duplicates
3.0
new
2.0
CRAP 1.7
« lib/HTML/Object/DOM/Element/Script.pm ↑ Summary lib/HTML/Object/DOM/Element/BR.pm »
1 ##----------------------------------------------------------------------------
2 ## HTML Object - ~/lib/HTML/Object/XPath/NodeSet.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::NodeSet;
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 # use parent qw( Module::Generic );
21 12 ▶ use parent qw( Module::Generic::Array );
Subroutine BEGIN: called CC=1 SCAR=0.0
22 12 ▶ use vars qw( $TRUE $FALSE $BASE_CLASS $DEBUG $VERSION );
Subroutine BEGIN: called CC=1 SCAR=0.0
23 12 ▶ use HTML::Object::XPath::Boolean;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 use overload (
25 12 '""' => \&to_literal,
26 'bool' => \&to_boolean,
27 12 ▶ );
Subroutine BEGIN: called CC=1 SCAR=0.0
28 12 our $TRUE = HTML::Object::XPath::Boolean->True;
29 12 our $FALSE = HTML::Object::XPath::Boolean->False;
30 12 our $BASE_CLASS = 'HTML::Object::XPath';
31 12 our $DEBUG = 0;
32 12 our $VERSION = 'v0.2.0';
33 };
34  
35 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 12 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
37  
38 sub new
39 {
40 1982 ▶ my $this = CORE::shift( @_ );
Subroutine new: called CC=2 SCAR=7.1
Pod documented
41 1982 ▶ return( CORE::bless( [] => ( CORE::ref( $this ) || $this ) ) );
Condition66%2/3
ref $this || $this
l!l&&r!l&&!r
219800
MC/DC0%0/2
ref $this || $this
ref $this $this
Truth table66%2/3
A || B
A= ref $thisB= $this
ABresult
1X1
011
000
42 }
43  
44 sub append
45 {
46 845 ▶ my $self = CORE::shift( @_ );
Subroutine append: called CC=1 SCAR=0.0
Pod documented
47 845 my( $nodeset ) = @_;
48 845 return( CORE::push( @$self, $nodeset->get_nodelist ) );
49 }
50  
51 # uses array index starting at 1, not 0
52 sub get_node
53 {
54 1173 ▶ my $self = CORE::shift( @_ );
Subroutine get_node: called CC=1 SCAR=0.0
Pod documented
55 1173 my( $pos ) = @_;
56 1173 return( $self->[ $pos - 1 ] );
57 }
58  
59 1010 ▶sub get_nodelist { return( @{$_[0]} ); }
Subroutine get_nodelist: called CC=1 SCAR=0.0
Pod documented
60  
61 sub getChildNodes
62 {
63 1 ▶ my $self = CORE::shift( @_ );
Subroutine getChildNodes: called CC=1 SCAR=0.0
Pod documented
64 1 return( CORE::map{ $_->getChildNodes } @$self );
65 }
66  
67 sub getElementById
68 {
69 0 ▶ my $self = CORE::shift( @_ );
Subroutine getElementById: not called CC=1 SCAR=6.9
Pod documented
70 0 return( CORE::map{ $_->getElementById } @$self );
71 }
72
73 sub getRootNode
74 {
75 0 ▶ my $self = CORE::shift( @_ );
Subroutine getRootNode: not called CC=1 SCAR=6.9
Pod documented
76 0 return( $self->[0]->getRootNode );
77 }
78  
79 3944 ▶sub new_literal { return( CORE::shift->_class_for( 'Literal' )->new( @_ ) ); }
Subroutine new_literal: called CC=1 SCAR=0.0
Pod documented
80  
81 0 ▶sub new_number { return( CORE::shift->_class_for( 'Number' )->new( @_ ) ); }
Subroutine new_number: not called CC=1 SCAR=6.9
Pod documented
82  
83 sub pop
84 {
85 0 ▶ my $self = CORE::shift( @_ );
Subroutine pop: not called CC=1 SCAR=6.9
Pod documented
86 0 return( CORE::pop( @$self ) );
87 }
88  
89 sub prepend
90 {
91 0 ▶ my $self = CORE::shift( @_ );
Subroutine prepend: not called CC=1 SCAR=6.9
Pod documented
92 0 my( $nodeset ) = @_;
93 0 return( CORE::unshift( @$self, $nodeset->get_nodelist ) );
94 }
95  
96 sub push
97 {
98 1420 ▶ my $self = CORE::shift( @_ );
Subroutine push: called CC=1 SCAR=0.0
Pod documented
99 1420 my( @nodes ) = @_;
100 1420 return( CORE::push( @$self, @nodes ) );
101 }
102  
103 sub remove_duplicates
104 {
105 49 ▶ my $self = CORE::shift( @_ );
Subroutine remove_duplicates: called CC=3 SCAR=11.0
Pod documented
106 49 my @unique;
107 49 my $last_node = 0;
108 49 foreach my $node ( @$self )
109 {
110 138 ▶ CORE::push( @unique, $node ) unless( $node == $last_node );
Branch: 1 branch
BranchTrueFalseTotal
unless $node == $last_node 138 0 138
111 138 $last_node = $node;
112 }
113 49 @$self = @unique;
114 49 return( $self );
115 }
116  
117 sub reverse
118 {
119 0 ▶ my $self = CORE::shift( @_ );
Subroutine reverse: not called CC=1 SCAR=6.9
Pod documented
120 0 @$self = CORE::reverse( @$self );
121 0 return( $self );
122 }
123  
124 sub shift
125 {
126 0 ▶ my $self = CORE::shift( @_ );
Subroutine shift: not called CC=1 SCAR=6.9
Pod documented
127 0 return( CORE::shift( @$self ) );
128 }
129  
130 sub size
131 {
132 1918 ▶ my $self = CORE::shift( @_ );
Subroutine size: called CC=1 SCAR=0.0
Pod documented
133 1918 return( CORE::scalar( @$self ) );
134 }
135  
136 sub sort
137 {
138 49 ▶ my $self = CORE::shift( @_ );
Subroutine sort: called CC=1 SCAR=0.0
Pod documented
139 49 @$self = CORE::sort { $a->cmp( $b ) } @$self;
140 49 return( $self );
141 }
142  
143 sub string_value
144 {
145 12 ▶ my $self = CORE::shift( @_ );
Subroutine string_value: called CC=2 SCAR=7.1
Pod documented
146 12 ▶ return( '' ) unless( @$self );
Branch: 1 branch
BranchTrueFalseTotal
unless @$self 0 12 12
147 12 return( $self->[0]->string_value );
148 }
149  
150 sub string_values
151 {
152 2 ▶ my $self = CORE::shift( @_ );
Subroutine string_values: called CC=1 SCAR=0.0
Pod documented
153 2 return( CORE::map{ $_->string_value } @$self );
154 }
155  
156 sub to_boolean
157 {
158 735 ▶ my $self = CORE::shift( @_ );
Subroutine to_boolean: called CC=2 SCAR=6.9
Pod documented
159 735 ▶ return( ( @$self > 0 ) ? $TRUE : $FALSE );
Branch: 1 branch
BranchTrueFalseTotal
@$self > 0 ? : 727 8 735
160 }
161  
162 sub to_final_value
163 {
164 18 ▶ my $self = CORE::shift( @_ );
Subroutine to_final_value: called CC=1 SCAR=0.0
Pod documented
165 18 return( CORE::join( '', CORE::map{ $_->string_value } @$self ) );
166 }
167  
168 sub to_literal
169 {
170 3944 ▶ my $self = CORE::shift( @_ );
Subroutine to_literal: called CC=1 SCAR=0.0
Pod documented
171 3944 return( $self->new_literal( CORE::join( '', CORE::map{ $_->string_value } @$self ) ) );
172 }
173  
174 sub to_number
175 {
176 0 ▶ my $self = CORE::shift( @_ );
Subroutine to_number: not called CC=1 SCAR=6.9
Pod documented
177 0 return( $self->new_number( $self->to_literal ) );
178 }
179  
180 sub unshift
181 {
182 0 ▶ my $self = CORE::shift( @_ );
Subroutine unshift: not called CC=1 SCAR=6.9
Pod documented
183 0 my( @nodes ) = @_;
184 0 return( CORE::unshift( @$self, @nodes ) );
185 }
186  
187 sub _class_for
188 {
189 3944 ▶ my( $self, $mod ) = @_;
Subroutine _class_for: called CC=3 SCAR=11.3
190 3944 eval( "require ${BASE_CLASS}\::${mod};" );
191 3944 ▶ die( $@ ) if( $@ );
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 3944 3944
192 # ${"${BASE_CLASS}\::${mod}\::DEBUG"} = $DEBUG;
193 3944 ▶ eval( "\$${BASE_CLASS}\::${mod}\::DEBUG = " . ( $DEBUG // '' ) );
Condition50%1/2
$HTML::Object::XPath::NodeSet::DEBUG // ""
l!l
39440
MC/DC0%0/1
$HTML::Object::XPath::NodeSet::DEBUG // ""
$HTML::Object::XPath::NodeSet::DEBUG
Truth table50%1/2
A
A= $HTML::Object::XPath::NodeSet::DEBUG
Aresult
11
00
194 3944 return( "${BASE_CLASS}::${mod}" );
195 }
196  
197 12 1;
198 # NOTE: POD
199 __END__
200  
201 =encoding utf-8
202  
203 =head1 NAME
204  
205 HTML::Object::XPath::NodeSet - HTML Object XPath Node Set
206  
207 =head1 SYNOPSIS
208  
209 use HTML::Object::XPath::NodeSet;
210 my $set = HTML::Object::XPath::NodeSet->new ||
211 die( HTML::Object::XPath::NodeSet->error, "\n" );
212  
213 my $results = $xp->find( '//someelement' );
214 if( !$results->isa( 'HTML::Object::XPath::NodeSet' ) )
215 {
216 print( "Found $results\n" );
217 exit;
218 }
219
220 foreach my $context ( $results->get_nodelist )
221 {
222 my $newresults = $xp->find( './other/element', $context );
223 # ...
224 }
225  
226 =head1 VERSION
227  
228 v0.2.0
229  
230 =head1 DESCRIPTION
231  
232 This module contains an ordered list of L<nodes|HTML::Object::Element>. The nodes each take the same format as described in L<HTML::Object::XPath>.
233  
234 =head1 METHODS
235  
236 =head2 new
237  
238 You should never have to create a new NodeSet object, as it is all done for you by L<HTML::Object::XPath>.
239  
240 =head2 append
241  
242 Given a C<nodeset>, this appends the list of nodes in C<nodeset> to the end of the current list.
243  
244 =head2 get_node
245  
246 Provided with an integer representing a C<position> and this returns the node at C<position>.
247  
248 The node position in XPath is based at 1, not 0.
249  
250 =head2 get_nodelist
251  
252 Returns a list of nodes. See L<HTML::Object::XPath> for the format of the nodes.
253  
254 =head2 getChildNodes
255  
256 Returns a list of all elements' own child nodes by calling C<getChildNodes> on each one of them.
257  
258 =head2 getElementById
259  
260 Returns a list of all elements' own id by calling C<getElementById> on each one of them.
261  
262 =head2 getRootNode
263  
264 Returns the first element's value of C<getRootNode>
265  
266 =head2 new_literal
267  
268 Returns a new L<HTML::Object::XPath::Literal> object, passing it whatever arguments was provided.
269  
270 =head2 new_number
271  
272 Returns a new L<HTML::Object::XPath::Number> object, passing it whatever arguments was provided.
273  
274 =head2 pop
275  
276 Equivalent to perl's L<perlfunc/pop> function. This removes the last element from our stack and returns it.
277  
278 =head2 prepend
279  
280 Given a C<nodeset>, prepends the list of nodes in C<nodeset> to the front of the current list.
281  
282 =head2 push
283  
284 Provided with a list of nodes and this will add them to the stack in this object.
285  
286 Equivalent to perl's L<perlfunc/push> function.
287  
288 =head2 remove_duplicates
289  
290 This removes any duplicates there might be in our internal list of elements.
291  
292 =head2 reverse
293  
294 This method reverse the order of the elements in our internal list of elements.
295  
296 =head2 shift
297  
298 Equivalent to perl's L<perlfunc/shift> function.
299  
300 =head2 size
301  
302 Returns the number of nodes in the L<HTML::Object::XPath::NodeSet>.
303  
304 =head2 sort
305  
306 This method sorts all the elements by comparing them to one another using C<cmp>
307  
308 =head2 string_value
309  
310 Returns the string-value of the first node in the list.
311  
312 See the L<HTML::Object::XPath> specification for what "string-value" means.
313  
314 =head2 string_values
315  
316 Returns a list of the string-values of all the nodes in the list.
317  
318 =head2 to_boolean
319  
320 Returns L<true|HTML::Object::XPath::Boolean> if there are elements in our internal array of elements, or L<false|HTML::Object::XPath::Boolean> otherwise.
321  
322 =head2 to_final_value
323  
324 Returns a string resulting from the concatenation of each elements' string value.
325  
326 =head2 to_literal
327  
328 Returns the concatenation of all the string-values of all the nodes in the list.
329  
330 =head2 to_number
331  
332 Returns a new L<number object|HTML::Object::XPath::Number> from the value returned from L</to_literal>
333  
334 =head2 unshift
335  
336 Provided with a list of nodes and this will add them at the top of the stack in this object.
337  
338 Equivalent to perl's L<perlfunc/unshift> function.
339  
340 =head1 AUTHOR
341  
342 Jacques Deguest E<lt>F<jack@deguest.jp>E<gt>
343  
344 =head1 SEE ALSO
345  
346 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>
347  
348 =head1 COPYRIGHT & LICENSE
349  
350 Copyright(c) 2021 DEGUEST Pte. Ltd.
351  
352 All rights reserved
353  
354 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
355  
356 =cut
« lib/HTML/Object/DOM/Element/Script.pm ↑ Summary lib/HTML/Object/DOM/Element/BR.pm »