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__