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

filter: Statementstmt 0.0% 0 / 154 Branchbran 0.0% 0 / 58 Conditioncond 0.0% 0 / 90 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 21 Podpod 0.0% 0 / 13 totaltotal 0.0% 0 / 336 CC 30 SCAR 41.9
CC
30
Cov
0%
init
182.0
nextNode
56.0
previousNode
56.0
CRAP 66.0
1 ##----------------------------------------------------------------------------
2 ## HTML Object - ~/lib/HTML/Object/DOM/NodeIteratorShared.pm
3 ## Version v0.2.0
4 ## Copyright(c) 2021 DEGUEST Pte. Ltd.
5 ## Author: Jacques Deguest <jack@deguest.jp>
6 ## Created 2021/12/20
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::NodeIteratorShared;
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( $VERSION );
22 # To import its constants
23 use HTML::Object::DOM::Node;
24 use HTML::Object::DOM::NodeFilter qw( :all );
25 our $VERSION = 'v0.2.0';
26 };
27  
28 use strict;
29 use warnings;
30  
31 sub init
32 {
33 my $self = shift( @_ );
34 return( $self->error({
35 message => sprintf( "Expected at least 1 arguments, but only got %d.", scalar( @_ ) ),
36 class => 'HTML::Object::SyntaxError',
37 }) ) if( scalar( @_ ) < 1 );
38 my $root = shift( @_ );
39 my $what = shift( @_ );
40 my( $filterDef, $filter );
41 $filterDef = shift( @_ ) if( ref( $_[0] ) eq 'CODE' );
42 my $opts = $self->_get_args_as_hash( @_ );
43 return( $self->error({
44 message => "Root node provided is not a HTML::Object::DOM::Node object.",
45 class => 'HTML::Object::TypeError',
46 }) ) if( !$self->_is_a( $root => 'HTML::Object::DOM::Node' ) );
47 $what = HTML::Object::DOM::NodeFilter::SHOW_ALL if( !defined( $what ) );
48 # Default value
49 if( !defined( $filterDef ) )
50 {
51 $filterDef = HTML::Object::DOM::NodeFilter->new;
52 }
53  
54 return( $self->error({
55 message => "Value provided for what to show is not an integer.",
56 class => 'HTML::Object::TypeError',
57 }) ) if( !$self->_is_integer( $what ) );
58 if( scalar( keys( %$opts ) ) )
59 {
60 return( $self->error({
61 message => "Filter parameter provided is an hash reference, but it does not have a \"acceptNode\" property or that property is not a code reference.",
62 class => 'HTML::Object::TypeError',
63 }) ) if( !defined( $filterDef ) && ( !exists( $opts->{acceptNode} ) || ref( $opts->{acceptNode} ) ne 'CODE' ) );
64 $filterDef = CORE::delete( $opts->{acceptNode} ) if( CORE::exists( $opts->{acceptNode} ) && ref( $opts->{acceptNode} ) eq 'CODE' );
65 }
66  
67 if( $self->_is_object( $filterDef ) )
68 {
69 return( $self->error({
70 message => "Object provided does not implement the \"acceptNode\" method.",
71 class => 'HTML::Object::TypeError',
72 }) ) if( !$filterDef->can( 'acceptNode' ) );
73 $filter = sub{ return( $filterDef->acceptNode( @_ ) ); };
74 }
75 else
76 {
77 return( $self->error({
78 message => "Filter parameter provided is not a code reference.",
79 class => 'HTML::Object::TypeError',
80 }) ) if( ref( $filterDef ) ne 'CODE' );
81 $filter = $filterDef;
82 }
83 $self->{_init_strict_use_sub} = 1;
84 $self->SUPER::init( @_ ) || return( $self->pass_error );
85 $self->{children} = [];
86 $self->{pointerbeforereferencenode} = 1;
87 $self->root( $root );
88 $self->whatToShow( $what );
89 $self->filter( $filter );
90 $self->{_parent} = $root;
91 # This is the position of our cursor in the flatten tree represented by an array of all elements
92 $self->{_pos} = 0;
93 my $elems = $self->_flatten;
94 $self->_elements( $elems );
95 return( $self );
96 }
97  
98 # Note: property expandEntityReferences read-only
99 sub expandEntityReferences : lvalue { return( shift->_set_get_boolean( 'expandentityreferences', @_ ) ); }
100  
101 # Note: property filter read-only
102 sub filter : lvalue { return( shift->_set_get_code( 'filter', @_ ) ); }
103  
104 sub nextNode
105 {
106 my $self = shift( @_ );
107 my $elems = $self->_elements;
108 # Would be -1 if empty
109 my $size = $elems->size;
110 # We reached the end of this array
111 return if( $self->{_pos} >= $size );
112 my $whattoshow = $self->whatToShow;
113 return( $self->error( "Somehow the bitwise value of what to show (", ( $whattoshow // 'undef' ), ") is not an integer!" ) ) if( !$self->_is_integer( $whattoshow ) );
114 my $filter = $self->filter;
115 my $class = ref( $self );
116 # Somehow it has been changed maybe? End our iteration
117 if( ref( $filter ) ne 'CODE' )
118 {
119 $self->{_pos} = $size;
120 return( $self->error({
121 message => "Filter is not a code reference!",
122 class => 'HTML::Object::TypeError',
123 }) );
124 }
125 my $node;
126 my $tmpPos = $self->{_pos} + 1;
127 while(1)
128 {
129 # We reached the end of the array
130 last if( $tmpPos > $size );
131 my $tmpNode = $elems->index( $tmpPos );
132 my $type = $tmpNode->nodeType;
133 $tmpPos++, next if( !$self->_check_element( $tmpNode ) );
134 # This is for the pos() method
135 $self->{_relative_pos} = $tmpNode->parent->children->pos( $tmpNode );
136 local $_ = $tmpNode;
137 my $rv = $filter->( $tmpNode );
138 # Filter should return FILTER_ACCEPT or FILTER_REJECT or FILTER_SKIP
139 $tmpPos++, next if( !defined( $rv ) || $rv == FILTER_REJECT || $rv == FILTER_SKIP );
140 $node = $tmpNode;
141 $self->{_pos} = $tmpPos;
142 last;
143 }
144 # Return the node to our caller
145 return( $node );
146 }
147  
148 sub pos { return( shift->{_relative_pos} ); }
149  
150 sub previousNode
151 {
152 my $self = shift( @_ );
153 my $elems = $self->_elements;
154 # Would be -1 if empty
155 my $size = $elems->size;
156 # Already at the beginning
157 return if( $self->{_pos} <= 0 );
158 my $whattoshow = $self->whatToShow;
159 return( $self->error( "Somehow the bitwise value of what to show is not an integer!" ) ) if( !$self->_is_integer( $whattoshow ) );
160 my $filter = $self->filter;
161 my $class = ref( $self );
162 # Somehow it has been changed maybe? End our iteration
163 if( ref( $filter ) ne 'CODE' )
164 {
165 $self->{_pos} = $size;
166 return( $self->error({
167 message => "Filter is not a code reference!",
168 class => 'HTML::Object::TypeError',
169 }) );
170 }
171 my $node;
172 my $tmpPos = $self->{_pos} - 1;
173 while(1)
174 {
175 # We reached the start of the array
176 last if( $tmpPos < 0 );
177 my $tmpNode = $elems->index( $tmpPos );
178 my $type = $tmpNode->nodeType;
179 $tmpPos--, next if( !$self->_check_element( $tmpNode ) );
180 # This is for the pos() method
181 $self->{_relative_pos} = $tmpNode->parent->children->pos( $tmpNode );
182 local $_ = $tmpNode;
183 my $rv = $filter->( $tmpNode );
184 # Filter should return FILTER_ACCEPT or FILTER_REJECT or FILTER_SKIP
185 $tmpPos--, next if( !defined( $rv ) || $rv == FILTER_REJECT || $rv == FILTER_SKIP );
186 $node = $tmpNode;
187 # Decrement the position for the next turn
188 $self->{_pos} = $tmpPos;
189 last;
190 }
191 # Return the node to our caller
192 return( $node );
193 }
194  
195 # Note: property root read-only
196 sub root : lvalue { return( shift->_set_get_object_lvalue( 'root', 'HTML::Object::DOM::Node', @_ ) ); }
197  
198 # Note: property whatToShow read-only
199 sub whatToShow : lvalue { return( shift->_set_get_number( 'whattoshow', @_ ) ); }
200  
201 sub _check_element
202 {
203 my $self = shift( @_ );
204 my $node = shift( @_ ) || return;
205 my $type = $node->nodeType;
206 my $whattoshow = $self->whatToShow;
207 unless( $whattoshow == SHOW_ALL )
208 {
209 if( ( $type == ELEMENT_NODE && !( $whattoshow & SHOW_ELEMENT ) ) ||
210 ( $type == ATTRIBUTE_NODE && !( $whattoshow & SHOW_ATTRIBUTE ) ) ||
211 ( $type == TEXT_NODE && !( $whattoshow & SHOW_TEXT ) ) ||
212 ( $type == CDATA_SECTION_NODE && !( $whattoshow & SHOW_CDATA_SECTION ) ) ||
213 ( $type == PROCESSING_INSTRUCTION_NODE && !( $whattoshow & SHOW_PROCESSING_INSTRUCTION ) ) ||
214 ( $type == COMMENT_NODE && !( $whattoshow & SHOW_COMMENT ) ) ||
215 ( $type == DOCUMENT_NODE && !( $whattoshow & SHOW_DOCUMENT ) ) ||
216 ( $type == DOCUMENT_TYPE_NODE && !( $whattoshow & SHOW_DOCUMENT_TYPE ) ) ||
217 ( $type == DOCUMENT_FRAGMENT_NODE && !( $whattoshow & SHOW_DOCUMENT_FRAGMENT ) ) ||
218 # Notation nodes are deprecated, but we list them here anyway
219 ( $type == NOTATION_NODE && !( $whattoshow & SHOW_NOTATION ) ) ||
220 # This is a non-standard addition to provide more granularity
221 ( $type == SPACE_NODE && !( $whattoshow & SHOW_SPACE ) ) )
222 {
223 return(0);
224 }
225 }
226 return(1);
227 }
228  
229 sub _elements { return( shift->_set_get_array_as_object( '_elements', @_ ) ); }
230  
231 sub _flatten
232 {
233 my $self = shift( @_ );
234 my $root = $self->root;
235 # Should not happen
236 return( $self->error( "root element is gone!" ) ) if( !defined( $root ) );
237 my $elems = $self->new_array;
238 my $seen = {};
239 my $crawl;
240 $crawl = sub
241 {
242 my $e = shift( @_ );
243 my $addr = $self->_refaddr( $e );
244 return if( ++$seen->{ $addr } > 1 );
245 my $kids = $e->children;
246 foreach my $kid ( @$kids )
247 {
248 # Junk somehow although it should not happen
249 next if( !$self->_is_a( $kid => 'HTML::Object::DOM::Node' ) );
250 $elems->push( $kid );
251 # Drill down...
252 $crawl->( $kid );
253 }
254 };
255 $elems->push( $root );
256 $crawl->( $root );
257 return( $elems );
258 }
259  
260 sub _parent { return( shift->_set_get_object_without_init( '_parent', 'HTML::Object::DOM::Node', @_ ) ); }
261  
262 1;
263 # NOTE: POD
264 __END__