blib/lib/HTML/Object/DOM/XPathResult.pm untested

filter: Statementstmt 0.0% 0 / 78 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 2 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 20 Podpod 0.0% 0 / 12 totaltotal 0.0% 0 / 130 CC 10 SCAR 17.0
CC
10
Cov
0%
iterateNext
12.0
snapshotLength
12.0
booleanValue
6.0
CRAP 5.4
1 ##----------------------------------------------------------------------------
2 ## HTML Object - ~/lib/HTML/Object/DOM/XPathResult.pm
3 ## Version v0.2.0
4 ## Copyright(c) 2022 DEGUEST Pte. Ltd.
5 ## Author: Jacques Deguest <jack@deguest.jp>
6 ## Created 2022/01/01
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::DOM::XPathResult;
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( @EXPORT %EXPORT_TAGS $VERSION );
22 use HTML::Object::Exception;
23 use constant {
24 # A result set containing whatever type naturally results from evaluation of the expression. Note that if the result is a node-set then UNORDERED_NODE_ITERATOR_TYPE is always the resulting type.
25 ANY_TYPE => 0,
26 # A result containing a single number. This is useful for example, in an XPath expression using the count() function.
27 NUMBER_TYPE => 1,
28 # A result containing a single string.
29 STRING_TYPE => 2,
30 # A result containing a single boolean value. This is useful for example, in an XPath expression using the not() function.
31 BOOLEAN_TYPE => 3,
32 # A result node-set containing all the nodes matching the expression. The nodes may not necessarily be in the same order that they appear in the document.
33 UNORDERED_NODE_ITERATOR_TYPE => 4,
34 # A result node-set containing all the nodes matching the expression. The nodes in the result set are in the same order that they appear in the document.
35 ORDERED_NODE_ITERATOR_TYPE => 5,
36 # A result node-set containing snapshots of all the nodes matching the expression. The nodes may not necessarily be in the same order that they appear in the document.
37 UNORDERED_NODE_SNAPSHOT_TYPE => 6,
38 # A result node-set containing snapshots of all the nodes matching the expression. The nodes in the result set are in the same order that they appear in the document.
39 ORDERED_NODE_SNAPSHOT_TYPE => 7,
40 # A result node-set containing any single node that matches the expression. The node is not necessarily the first node in the document that matches the expression.
41 ANY_UNORDERED_NODE_TYPE => 8,
42 # A result node-set containing the first node in the document that matches the expression.
43 FIRST_ORDERED_NODE_TYPE => 9,
44 };
45 our @EXPORT = qw(
46 ANY_TYPE NUMBER_TYPE STRING_TYPE BOOLEAN_TYPE
47 UNORDERED_NODE_ITERATOR_TYPE ORDERED_NODE_ITERATOR_TYPE
48 UNORDERED_NODE_SNAPSHOT_TYPE ORDERED_NODE_SNAPSHOT_TYPE
49 ANY_UNORDERED_NODE_TYPE FIRST_ORDERED_NODE_TYPE
50 );
51 our %EXPORT_TAGS = (
52 all => [qw(
53 ANY_TYPE NUMBER_TYPE STRING_TYPE BOOLEAN_TYPE
54 UNORDERED_NODE_ITERATOR_TYPE ORDERED_NODE_ITERATOR_TYPE
55 UNORDERED_NODE_SNAPSHOT_TYPE ORDERED_NODE_SNAPSHOT_TYPE
56 ANY_UNORDERED_NODE_TYPE FIRST_ORDERED_NODE_TYPE
57 )]
58 );
59 our $VERSION = 'v0.2.0';
60 };
61  
62 use strict;
63 use warnings;
64  
65 sub init
66 {
67 my $self = shift( @_ );
68 $self->{result} = undef;
69 # Any type, by default
70 $self->{resulttype} = 0;
71 $self->{_init_strict_use_sub} = 1;
72 $self->SUPER::init( @_ ) || return( $self->pass_error );
73 $self->{_pos} = 0;
74 return( $self );
75 }
76  
77 # Note: property booleanValue read-only
78 sub booleanValue
79 {
80 my $self = shift( @_ );
81 my $res = $self->result;
82 return( $self->error({
83 message => 'Result is not a boolean',
84 class => 'HTML::Object::TypeError',
85 }) ) if( !$self->_is_a( $res => 'HTML::Object::XPath::Boolean' ) );
86 return( $res );
87 }
88  
89 # Note: property invalidIteratorState read-only
90 sub invalidIteratorState : lvalue { return( shift->_set_get_property( 'invaliditeratorstate', @_ ) ); }
91  
92 sub iterateNext
93 {
94 my $self = shift( @_ );
95 my $res = $self->result;
96 return( $self->error({
97 message => 'Result is not a NodeSet',
98 class => 'HTML::Object::TypeError',
99 }) ) if( !$self->_is_a( $res => 'HTML::Object::XPath::NodeSet' ) );
100 return if( $self->{_pos} >= $res->size );
101 my $node = $res->index( $self->{_pos} );
102 $self->{_pos}++;
103 return( $node );
104 }
105  
106 # Note: property numberValue read-only
107 sub numberValue
108 {
109 my $self = shift( @_ );
110 my $res = $self->result;
111 return( $self->error({
112 message => 'Result is not a number',
113 class => 'HTML::Object::TypeError',
114 }) ) if( !$self->_is_a( $res => 'HTML::Object::XPath::Number' ) );
115 return( $res );
116 }
117  
118 # Note: method to store the result, which is an object of various class, so we use _set_get
119 sub result { return( shift->_set_get( 'result', @_ ) ); }
120  
121 # Note: property resultType read-only
122 sub resultType : lvalue { return( shift->_set_get_number( 'resulttype', @_ ) ); }
123  
124 # Note: property singleNodeValue read-only
125 sub singleNodeValue
126 {
127 my $self = shift( @_ );
128 my $res = $self->result;
129 return( $self->error({
130 message => 'Result is not a node (HTML::Object::DOM::Node)',
131 class => 'HTML::Object::TypeError',
132 }) ) if( !$self->_is_a( $res => 'HTML::Object::DOM::Node' ) );
133 return( $res );
134 }
135  
136 sub snapshotItem
137 {
138 my $self = shift( @_ );
139 my $res = $self->result;
140 if( $self->_is_a( $res => 'HTML::Object::XPath::NodeSet' ) )
141 {
142 return( $res->[ $self->{_pos} ] );
143 }
144 else
145 {
146 return( $res );
147 }
148 }
149  
150 # Note: property snapshotLength read-only
151 sub snapshotLength
152 {
153 my $self = shift( @_ );
154 my $res = $self->result;
155 if( $self->_is_a( $res => 'HTML::Object::XPath::NodeSet' ) )
156 {
157 return( $res->size );
158 }
159 else
160 {
161 return( ref( $res ) ? 1 : 0 );
162 }
163 }
164  
165 # Note: property stringValue read-only
166 sub stringValue
167 {
168 my $self = shift( @_ );
169 my $res = $self->result;
170 return( $self->error({
171 message => 'Result is not a string (HTML::Object::XPath::Literal)',
172 class => 'HTML::Object::TypeError',
173 }) ) if( !$self->_is_a( $res => 'HTML::Object::XPath::Literal' ) );
174 return( $res );
175 }
176  
177 1;
178 # NOTE: POD
179 __END__
180  
181 =encoding utf-8
182  
183 =head1 NAME
184  
185 HTML::Object::DOM::XPathResult - HTML Object DOM XPath Result Class
186  
187 =head1 SYNOPSIS
188  
189 use HTML::Object::DOM::XPathResult;
190 my $result = HTML::Object::DOM::XPathResult->new ||
191 die( HTML::Object::DOM::XPathResult->error, "\n" );
192  
193 =head1 VERSION
194  
195 v0.2.0
196  
197 =head1 DESCRIPTION
198  
199 The C<XPathResult> interface represents the results generated by evaluating an XPath expression within the context of a given L<node|HTML::Object::DOM::Node>.
200  
201 The method you can access vary depending on the type of results returned. The XPath evaluation can return a L<boolean|HTML::Object::XPath::Boolean>, a L<number|HTML::Object::XPath::Number>, a L<string|HTML::Object::XPath::Literal>, or a L<node set|HTML::Object::XPath::NodeSet>
202  
203 =head1 PROPERTIES
204  
205 All properties are read-only, but you can change their returned value by changing the value of L</result>, which contains the result from the XPath search.
206  
207 =head2 booleanValue
208  
209 A boolean representing the value of the result if resultType is C<BOOLEAN_TYPE>, i.e. if the result is a boolean.
210  
211 Example:
212  
213 <div>XPath example</div>
214 <p>Text is 'XPath example': <output></output></p>
215  
216 my $xpath = "//div/text() = 'XPath example'";
217 my $result = $doc->evaluate( $xpath, $doc );
218 $doc->querySelector( 'output' )->textContent = $result->booleanValue;
219  
220 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/XPathResult/booleanValue>
221  
222 =head2 invalidIteratorState
223  
224 This always return C<undef> under perl, but you can change the value of this boolean to whatever boolean value you want.
225  
226 Normally, under JavaScript, this signifies that the iterator has become invalid. It is true if resultType is UNORDERED_NODE_ITERATOR_TYPE or ORDERED_NODE_ITERATOR_TYPE and the document has been modified since this result was returned.
227  
228 Example:
229  
230 <div>XPath example</div>
231 <p>Iterator state: <output></output></p>
232  
233 my $xpath = '//div';
234 my $result = $doc->evaluate( $xpath, $doc );
235 # Invalidates the iterator state
236 $doc->querySelector( 'div' )->remove();
237 $doc->querySelector( 'output' )->textContent = $result->invalidIteratorState ? 'invalid' : 'valid';
238  
239 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/XPathResult/invalidIteratorState>
240  
241 =head2 numberValue
242  
243 A number representing the value of the result if resultType is C<NUMBER_TYPE>, i.e. if the result is a number.
244  
245 Example:
246  
247 <div>XPath example</div>
248 <div>Number of &lt;div&gt;s: <output></output></div>
249  
250 my $xpath = 'count(//div)';
251 my $result = $doc->evaluate( $xpath, $doc );
252 $doc->querySelector( 'output' )->textContent = $result->numberValue;
253  
254 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/XPathResult/numberValue>
255  
256 =head2 result
257  
258 Sets or gets the resulting object from the XPath search. This could be a L<node|HTML::Object::DOM::Node>, a L<boolean|HTML::Object::XPath::Boolean>, a L<number|HTML::Object::XPath::Number>, a L<string|HTML::Object::XPath::Literal>, or a L<set of nodes|HTML::Object::XPath::NodeSet>
259  
260 =head2 resultType
261  
262 A number code representing the type of the result, as defined by the type constants. See L</CONSTANTS>
263  
264 Example:
265  
266 <div>XPath example</div>
267 <div>Is XPath result a node set: <output></output></div>
268  
269 use HTML::Object::DOM::XPathResult;
270 # or
271 use HTML::Object::DOM qw( :xpath );
272 my $xpath = '//div';
273 my $result = $doc->evaluate( $xpath, $doc );
274 $doc->querySelector( 'output' )->textContent =
275 $result->resultType >= UNORDERED_NODE_ITERATOR_TYPE &&
276 $result->resultType <= FIRST_ORDERED_NODE_TYPE;
277  
278 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/XPathResult/resultType>
279  
280 =head2 singleNodeValue
281  
282 A Node representing the value of the single node result, which may be C<undef>. This is set when the result is a single L<node|HTML::Object::DOM::Node>.
283  
284 Example:
285  
286 <div>XPath example</div>
287 <div>Tag name of the element having the text content 'XPath example': <output></output></div>
288  
289 my $xpath = q{//*[text()='XPath example']};
290 my $result = $doc->evaluate( $xpath, $doc );
291 $doc->querySelector( 'output' )->textContent = $result->singleNodeValue->localName;
292  
293 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/XPathResult/singleNodeValue>
294  
295 =head2 snapshotLength
296  
297 The number of nodes in the result snapshot. As a divergence from the standard, this also applies to the number of elements in the L<NodeSet|HTML::Object::XPath::NodeSet> returned.
298  
299 Example:
300  
301 <div>XPath example</div>
302 <div>Number of matched nodes: <output></output></div>
303  
304 my $xpath = '//div';
305 my $result = $doc->evaluate( $xpath, $doc );
306 $doc->querySelector( 'output' )->textContent = $result->snapshotLength;
307  
308 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/XPathResult/snapshotLength>
309  
310 =head2 stringValue
311  
312 A string representing the value of the result if resultType is C<STRING_TYPE>, i.e. when the result is a string.
313  
314 Example:
315  
316 <div>XPath example</div>
317 <div>Text content of the &lt;div&gt; above: <output></output></div>
318  
319 my $xpath = '//div/text()';
320 my $result = $doc->evaluate( $xpath, $doc );
321 $doc->querySelector( 'output' )->textContent = $result->stringValue;
322  
323 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/XPathResult/stringValue>
324  
325 =head1 METHODS
326  
327 =head2 iterateNext
328  
329 If the result is a L<node set|HTML::Object::XPath::NodeSet>, this method iterates over it and returns the next node from it or C<undef> if there are no more nodes.
330  
331 Example:
332  
333 <div>XPath example</div>
334 <div>Tag names of the matched nodes: <output></output></div>
335  
336 use Module::Generic::Array;
337 my $xpath = '//div';
338 my $result = $doc->evaluate( $xpath, $doc );
339 my $node;
340 my $tagNames = Module::Generic::Array->new;
341 while( $node = $result->iterateNext() )
342 {
343 $tagNames->push( $node->localName );
344 }
345 $doc->querySelector( 'output' )->textContent = $tagNames->join( ', ' );
346  
347 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/XPathResult/iterateNext>
348  
349 =head2 snapshotItem
350  
351 Returns an item of the snapshot collection or C<undef> in case the index is not within the range of nodes.
352  
353 Normally, under JavaScript, unlike the iterator result, the snapshot does not become invalid, but may not correspond to the current document if it is mutated.
354  
355 Example:
356  
357 <div>XPath example</div>
358 <div>Tag names of the matched nodes: <output></output></div>
359  
360 use Module::Generic::Array;
361 my $xpath = '//div';
362 my $result = $doc->evaluate( $xpath, $doc );
363 my $node;
364 my $tagNames = Module::Generic::Array->new;
365 for( my $i = 0; $i < $result->snapshotLength; $i++ )
366 {
367 my $node = $result->snapshotItem( $i );
368 $tagNames->push( $node->localName );
369 }
370 $doc->querySelector( 'output' )->textContent = $tagNames->join( ', ' );
371  
372 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/XPathResult/snapshotItem>
373  
374 =head1 CONSTANTS
375  
376 The following constants are exported by default:
377  
378 =over 4
379  
380 =item ANY_TYPE (0)
381  
382 A result set containing whatever type naturally results from evaluation of the expression. Note that if the result is a node-set then C<UNORDERED_NODE_ITERATOR_TYPE> is always the resulting type.
383  
384 =item NUMBER_TYPE (1)
385  
386 A result containing a single number. This is useful for example, in an XPath expression using the count() function.
387  
388 =item STRING_TYPE (2)
389  
390 A result containing a single string.
391  
392 =item BOOLEAN_TYPE (3)
393  
394 A result containing a single boolean value. This is useful for example, in an XPath expression using the not() function.
395  
396 =item UNORDERED_NODE_ITERATOR_TYPE (4)
397  
398 A result node-set containing all the nodes matching the expression. The nodes may not necessarily be in the same order that they appear in the document.
399  
400 =item ORDERED_NODE_ITERATOR_TYPE (5)
401  
402 A result node-set containing all the nodes matching the expression. The nodes in the result set are in the same order that they appear in the document.
403  
404 =item UNORDERED_NODE_SNAPSHOT_TYPE (6)
405  
406 A result node-set containing snapshots of all the nodes matching the expression. The nodes may not necessarily be in the same order that they appear in the document.
407  
408 =item ORDERED_NODE_SNAPSHOT_TYPE (7)
409  
410 A result node-set containing snapshots of all the nodes matching the expression. The nodes in the result set are in the same order that they appear in the document.
411  
412 =item ANY_UNORDERED_NODE_TYPE (8)
413  
414 A result node-set containing any single node that matches the expression. The node is not necessarily the first node in the document that matches the expression.
415  
416 =item FIRST_ORDERED_NODE_TYPE (9)
417  
418 A result node-set containing the first node in the document that matches the expression.
419  
420 =back
421  
422 =head1 AUTHOR
423  
424 Jacques Deguest E<lt>F<jack@deguest.jp>E<gt>
425  
426 =head1 SEE ALSO
427  
428 L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/XPathResult>, L<W3C specifications|https://dom.spec.whatwg.org/#interface-xpathresult>
429  
430 =head1 COPYRIGHT & LICENSE
431  
432 Copyright(c) 2022 DEGUEST Pte. Ltd.
433  
434 All rights reserved
435  
436 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
437  
438 =cut