| 1 | ##---------------------------------------------------------------------------- | ||
| 2 | ## HTML Object - ~/lib/HTML/Object/DOM/NodeIterator.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::NodeIterator; | ||
| 15 | BEGIN | ||
| 16 | { | ||
| 17 | use strict; | ||
| 18 | use warnings; | ||
| 19 | warnings::register_categories( 'HTML::Object' ); | ||
| 20 | use parent qw( HTML::Object::DOM::NodeIteratorShared ); | ||
| 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 | # Note: method init is inherited | ||
| 32 | |||
| 33 | sub detach { return; } | ||
| 34 | |||
| 35 | # Note: property expandEntityReferences read-only is inherited | ||
| 36 | |||
| 37 | # Note: property filter read-only inherited | ||
| 38 | |||
| 39 | # Note: method nextNode is inherited | ||
| 40 | |||
| 41 | # Note: property pointerBeforeReferenceNode read-only | ||
| 42 | sub pointerBeforeReferenceNode : lvalue { return( shift->_set_get_boolean( 'pointerbeforereferencenode', @_ ) ); } | ||
| 43 | |||
| 44 | sub pos { return( shift->{_relative_pos} ); } | ||
| 45 | |||
| 46 | # Note: method previousNode is inherited | ||
| 47 | |||
| 48 | # Note: property referenceNode read-only | ||
| 49 | sub referenceNode | ||
| 50 | { | ||
| 51 | my $self = shift( @_ ); | ||
| 52 | my $elems = $self->_elements; | ||
| 53 | return( $elems->index( $self->{_pos} ) ); | ||
| 54 | } | ||
| 55 | |||
| 56 | # Note: property root read-only is inherited | ||
| 57 | |||
| 58 | # Note: property whatToShow read-only is inherited | ||
| 59 | |||
| 60 | 1; | ||
| 61 | # NOTE: POD | ||
| 62 | __END__ | ||
| 63 | |||
| 64 | =encoding utf-8 | ||
| 65 | |||
| 66 | =head1 NAME | ||
| 67 | |||
| 68 | HTML::Object::DOM::NodeIterator - HTML Object DOM Node Iterator Class | ||
| 69 | |||
| 70 | =head1 SYNOPSIS | ||
| 71 | |||
| 72 | With just one argument, this default to search for everything (C<SHOW_ALL>) and to use the default filter, which always returns C<FILTER_ACCEPT> | ||
| 73 | |||
| 74 | use HTML::Object::DOM::NodeIterator; | ||
| 75 | my $nodes = HTML::Object::DOM::NodeIterator->new( $root_node ) || | ||
| 76 | die( HTML::Object::DOM::NodeIterator->error, "\n" ); | ||
| 77 | |||
| 78 | Or, passing an anonymous subroutine as the filter | ||
| 79 | |||
| 80 | my $nodes = HTML::Object::DOM::NodeIterator->new( | ||
| 81 | $root_node, | ||
| 82 | $what_to_show_bit, | ||
| 83 | sub{ return( FILTER_ACCEPT ); } | ||
| 84 | ) || die( HTML::Object::DOM::NodeIterator->error, "\n" ); | ||
| 85 | |||
| 86 | Or, passing an hash reference with a property 'acceptNode' whose value is an anonymous subroutine, as the filter | ||
| 87 | |||
| 88 | my $nodes = HTML::Object::DOM::NodeIterator->new( | ||
| 89 | $root_node, | ||
| 90 | $what_to_show_bit, | ||
| 91 | { | ||
| 92 | acceptNode => sub{ return( FILTER_ACCEPT ); } | ||
| 93 | } | ||
| 94 | ) || die( HTML::Object::DOM::NodeIterator->error, "\n" ); | ||
| 95 | |||
| 96 | Or, passing an object that implements the method "acceptNode" | ||
| 97 | |||
| 98 | my $nodes = HTML::Object::DOM::NodeIterator->new( | ||
| 99 | $root_node, | ||
| 100 | $what_to_show_bit, | ||
| 101 | # This object must implement the acceptNode method | ||
| 102 | My::Customer::NodeFilter->new | ||
| 103 | ) || die( HTML::Object::DOM::NodeIterator->error, "\n" ); | ||
| 104 | |||
| 105 | There is also L<HTML::Object::DOM::TreeWalker>, which performs a somewhat similar function. | ||
| 106 | |||
| 107 | Choose L<HTML::Object::DOM::NodeIterator> when you only need a simple iterator to filter and browse the selected nodes, and choose L<HTML::Object::DOM::TreeWalker> when you need to access to the node and its siblings. | ||
| 108 | |||
| 109 | =head1 VERSION | ||
| 110 | |||
| 111 | v0.2.0 | ||
| 112 | |||
| 113 | =head1 DESCRIPTION | ||
| 114 | |||
| 115 | The C<NodeIterator> interface represents an iterator over the members of a list of the L<nodes|HTML::Object::DOM::Node> in a subtree of the L<DOM|HTML::Object::DOM>. The nodes will be returned in document order. | ||
| 116 | |||
| 117 | A C<NodeIterator> can be created using the L<HTML::Object::DOM::Document/createNodeIterator> method, as follows: | ||
| 118 | |||
| 119 | use HTML::Object::DOM; | ||
| 120 | my $parser = HTML::Object::DOM->new; | ||
| 121 | my $doc = $parser->parse_data( $some_html_data ) || die( $parser->error ); | ||
| 122 | my $nodeIterator = $doc->createNodeIterator( $root, $whatToShow, $filter ) || | ||
| 123 | die( $doc->error ); | ||
| 124 | |||
| 125 | =head1 PROPERTIES | ||
| 126 | |||
| 127 | =head2 expandEntityReferences | ||
| 128 | |||
| 129 | Normally this is read-only, but under perl you can set whatever boolean value you want. | ||
| 130 | |||
| 131 | Under JavaScript, this is a boolean value indicating if, when discarding an C<EntityReference> its whole sub-tree must be discarded at the same time. | ||
| 132 | |||
| 133 | Example: | ||
| 134 | |||
| 135 | use HTML::Object::DOM::NodeFilter qw( :all ); | ||
| 136 | my $nodeIterator = $doc->createNodeIterator( | ||
| 137 | $doc->body, | ||
| 138 | SHOW_ELEMENT, | ||
| 139 | sub{ return( FILTER_ACCEPT ); }, | ||
| 140 | # or | ||
| 141 | # { acceptNode => sub{ return( FILTER_ACCEPT ); } }, | ||
| 142 | ); | ||
| 143 | my $expand = $nodeIterator->expandEntityReferences; | ||
| 144 | |||
| 145 | See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/NodeIterator/expandEntityReferences> | ||
| 146 | |||
| 147 | =head2 filter | ||
| 148 | |||
| 149 | Normally this is read-only, but under perl you can set it to a new L<HTML::Object::DOM::NodeFilter> object you want, even after object instantiation. | ||
| 150 | |||
| 151 | Returns a L<HTML::Object::DOM::NodeFilter> used to select the relevant nodes. | ||
| 152 | |||
| 153 | Example: | ||
| 154 | |||
| 155 | use HTML::Object::DOM::NodeFilter qw( :all ); | ||
| 156 | my $nodeIterator = $doc->createNodeIterator( | ||
| 157 | $doc->body, | ||
| 158 | SHOW_ELEMENT, | ||
| 159 | sub{ return( FILTER_ACCEPT ); }, | ||
| 160 | # or | ||
| 161 | # { acceptNode => sub{ return( FILTER_ACCEPT ); } }, | ||
| 162 | ); | ||
| 163 | my $nodeFilter = $nodeIterator->filter; | ||
| 164 | |||
| 165 | See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/NodeIterator/filter> | ||
| 166 | |||
| 167 | =head2 pointerBeforeReferenceNode | ||
| 168 | |||
| 169 | Normally this is read-only, but under perl you can set whatever boolean value you want. Defaults to true. | ||
| 170 | |||
| 171 | Returns a boolean flag that indicates whether the C<NodeIterator> is anchored before, the flag being true, or after, the flag being false, the anchor node. | ||
| 172 | |||
| 173 | Example: | ||
| 174 | |||
| 175 | use HTML::Object::DOM::NodeFilter qw( :all ); | ||
| 176 | my $nodeIterator = $doc->createNodeIterator( | ||
| 177 | $doc->body, | ||
| 178 | SHOW_ELEMENT, | ||
| 179 | sub{ return( FILTER_ACCEPT ); }, | ||
| 180 | # or | ||
| 181 | # { acceptNode => sub{ return( FILTER_ACCEPT ); } }, | ||
| 182 | ); | ||
| 183 | my $flag = $nodeIterator->pointerBeforeReferenceNode; | ||
| 184 | |||
| 185 | See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/NodeIterator/pointerBeforeReferenceNode> | ||
| 186 | |||
| 187 | =head2 pos | ||
| 188 | |||
| 189 | Read-only. | ||
| 190 | |||
| 191 | This is a non-standard property, which returns the 0-based position in the array of the anchor element's children. | ||
| 192 | |||
| 193 | You can poll this to know where the iterator is at. | ||
| 194 | |||
| 195 | Example: | ||
| 196 | |||
| 197 | use HTML::Object::DOM::NodeFilter qw( :all ); | ||
| 198 | # You need to first declare $nodeIterator to be able to use it in the callback | ||
| 199 | my $nodeIterator; | ||
| 200 | $nodeIterator = $doc->createNodeIterator( | ||
| 201 | $doc->body, | ||
| 202 | SHOW_ELEMENT, | ||
| 203 | sub | ||
| 204 | { | ||
| 205 | say "Current position is: ", $nodeIterator->pos ); | ||
| 206 | return( $_->getName eq 'div' ? FILTER_ACCEPT : FILTER_SKIP ); | ||
| 207 | }, | ||
| 208 | ); | ||
| 209 | |||
| 210 | =head2 referenceNode | ||
| 211 | |||
| 212 | Read-only. | ||
| 213 | |||
| 214 | Returns the L<Node|HTML::Object::DOM::Node> to which the iterator is anchored. | ||
| 215 | |||
| 216 | Example: | ||
| 217 | |||
| 218 | use HTML::Object::DOM::NodeFilter qw( :all ); | ||
| 219 | my $nodeIterator = $doc->createNodeIterator( | ||
| 220 | $doc->body, | ||
| 221 | SHOW_ELEMENT, | ||
| 222 | sub{ return( FILTER_ACCEPT ); }, | ||
| 223 | # or | ||
| 224 | # { acceptNode => sub{ return( FILTER_ACCEPT ); } }, | ||
| 225 | ); | ||
| 226 | my $node = $nodeIterator->referenceNode; | ||
| 227 | |||
| 228 | See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/NodeIterator/referenceNode> | ||
| 229 | |||
| 230 | =head2 root | ||
| 231 | |||
| 232 | Normally this is read-only, but under perl you can set whatever L<node value|HTML::Object::DOM::Node> you want. | ||
| 233 | |||
| 234 | Returns a L<Node|HTML::Object::DOM::Node> representing the root node as specified when the C<NodeIterator> was created. | ||
| 235 | |||
| 236 | Example: | ||
| 237 | |||
| 238 | use HTML::Object::DOM::NodeFilter qw( :all ); | ||
| 239 | my $nodeIterator = $doc->createNodeIterator( | ||
| 240 | $doc->body, | ||
| 241 | SHOW_ELEMENT, | ||
| 242 | sub{ return( FILTER_ACCEPT ); }, | ||
| 243 | # or | ||
| 244 | # { acceptNode => sub{ return( FILTER_ACCEPT ); } }, | ||
| 245 | ); | ||
| 246 | my $root = $nodeIterator->root; # $doc->body in this case | ||
| 247 | |||
| 248 | See L<for more information|https://developer.mozilla.org/en-US/docs/Web/API/NodeIterator/root> | ||
| 249 | |||
| 250 | =head2 whatToShow | ||
| 251 | |||
| 252 | Normally this is read-only, but under perl you can set whatever number value you want. | ||
| 253 | |||
| 254 | Returns an unsigned long being a bitmask made of L<constants|/CONSTANTS> describing the types of L<Node|HTML::Object::DOM::Node> that must to be presented. Non-matching nodes are skipped, but their children may be included, if relevant. | ||
| 255 | |||
| 256 | Possible constant values (exported by L<HTML::Object::DOM::NodeFilter>) are: | ||
| 257 | |||
| 258 | =over 4 | ||
| 259 | |||
| 260 | =item SHOW_ALL (4294967295) | ||
| 261 | |||
| 262 | Shows all nodes. | ||
| 263 | |||
| 264 | =item SHOW_ELEMENT (1) | ||
| 265 | |||
| 266 | Shows Element nodes. | ||
| 267 | |||
| 268 | =item SHOW_ATTRIBUTE (2) | ||
| 269 | |||
| 270 | Shows attribute L<Attribute nodes|HTML::Object::DOM::Attribute>. This is meaningful only when creating a C<NodeIterator> with an L<Attribute node|HTML::Object::DOM::Attribute> as its root; in this case, it means that the L<attribute node|HTML::Object::DOM::Attribute> will appear in the first position of the iteration or traversal. Since attributes are never children of other L<nodes|HTML::Object::DOM::Node>, they do not appear when traversing over the document tree. | ||
| 271 | |||
| 272 | =item SHOW_TEXT (4) | ||
| 273 | |||
| 274 | Shows Text nodes. | ||
| 275 | |||
| 276 | Example: | ||
| 277 | |||
| 278 | use HTML::Object::DOM::NodeFilter qw( :all ); | ||
| 279 | my $nodeIterator = $doc->createNodeIterator( | ||
| 280 | $doc->body, | ||
| 281 | ( SHOW_ELEMENT | SHOW_COMMENT | SHOW_TEXT ), | ||
| 282 | sub{ return( FILTER_ACCEPT ); }, | ||
| 283 | # or | ||
| 284 | # { acceptNode => sub{ return( FILTER_ACCEPT ); } }, | ||
| 285 | ); | ||
| 286 | if( ( $nodeIterator->whatToShow & SHOW_ALL ) || | ||
| 287 | ( $nodeIterator->whatToShow & SHOW_COMMENT ) ) | ||
| 288 | { | ||
| 289 | # $nodeIterator will show comments | ||
| 290 | } | ||
| 291 | |||
| 292 | =item SHOW_CDATA_SECTION (8) | ||
| 293 | |||
| 294 | Will always returns nothing, because there is no support for xml documents. | ||
| 295 | |||
| 296 | =item SHOW_ENTITY_REFERENCE (16) | ||
| 297 | |||
| 298 | Legacy, no more used. | ||
| 299 | |||
| 300 | =item SHOW_ENTITY (32) | ||
| 301 | |||
| 302 | Legacy, no more used. | ||
| 303 | |||
| 304 | =item SHOW_PROCESSING_INSTRUCTION (64) | ||
| 305 | |||
| 306 | Shows ProcessingInstruction nodes. | ||
| 307 | |||
| 308 | =item SHOW_COMMENT (128) | ||
| 309 | |||
| 310 | Shows Comment nodes. | ||
| 311 | |||
| 312 | =item SHOW_DOCUMENT (256) | ||
| 313 | |||
| 314 | Shows Document nodes | ||
| 315 | |||
| 316 | =item SHOW_DOCUMENT_TYPE (512) | ||
| 317 | |||
| 318 | Shows C<DocumentType> nodes | ||
| 319 | |||
| 320 | =item SHOW_DOCUMENT_FRAGMENT (1024) | ||
| 321 | |||
| 322 | Shows L<HTML::Object::DOM::DocumentFragment> nodes. | ||
| 323 | |||
| 324 | =item SHOW_NOTATION (2048) | ||
| 325 | |||
| 326 | Legacy, no more used. | ||
| 327 | |||
| 328 | =item SHOW_SPACE (4096) | ||
| 329 | |||
| 330 | Show Space nodes. This is a non-standard extension under this perl framework. | ||
| 331 | |||
| 332 | =back | ||
| 333 | |||
| 334 | See L<for more information|https://developer.mozilla.org/en-US/docs/Web/API/NodeIterator/whatToShow> | ||
| 335 | |||
| 336 | =head1 CONSTRUCTOR | ||
| 337 | |||
| 338 | =head2 new | ||
| 339 | |||
| 340 | Provided with a L<root node|HTML::Object::DOM::Node>, an optional bitwise value representing what to show and an optional filter callback and this will return a new node iterator. | ||
| 341 | |||
| 342 | =head1 METHODS | ||
| 343 | |||
| 344 | =head2 detach | ||
| 345 | |||
| 346 | This operation is a no-op. It does not do anything. Previously it was telling the web browser engine that the C<NodeIterator> was no more used, but this is now useless. | ||
| 347 | |||
| 348 | See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/NodeIterator/detach> | ||
| 349 | |||
| 350 | =head2 nextNode | ||
| 351 | |||
| 352 | Returns the next L<Node|HTML::Object::DOM::Node> in the document, or C<undef> if there are none. | ||
| 353 | |||
| 354 | Example: | ||
| 355 | |||
| 356 | use HTML::Object::DOM::NodeFilter qw( :all ); | ||
| 357 | my $nodeIterator = $doc->createNodeIterator( | ||
| 358 | $doc->body, | ||
| 359 | SHOW_ELEMENT, | ||
| 360 | sub{ return( FILTER_ACCEPT ); }, | ||
| 361 | # or | ||
| 362 | # { acceptNode => sub{ return( FILTER_ACCEPT ); } }, | ||
| 363 | 0 # false; this optional argument is not used any more | ||
| 364 | ); | ||
| 365 | my $currentNode = $nodeIterator->nextNode(); # returns the next node | ||
| 366 | |||
| 367 | See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/NodeIterator/nextNode> | ||
| 368 | |||
| 369 | =head2 previousNode | ||
| 370 | |||
| 371 | Returns the previous L<Node|HTML::Object::DOM::Node> in the document, or C<undef> if there are none. | ||
| 372 | |||
| 373 | Example: | ||
| 374 | |||
| 375 | use HTML::Object::DOM::NodeFilter qw( :all ); | ||
| 376 | my $nodeIterator = $doc->createNodeIterator( | ||
| 377 | $doc->body, | ||
| 378 | SHOW_ELEMENT, | ||
| 379 | sub{ return( FILTER_ACCEPT ); }, | ||
| 380 | # or | ||
| 381 | # { acceptNode => sub{ return( FILTER_ACCEPT ); } }, | ||
| 382 | 0 # false; this optional argument is not used any more | ||
| 383 | ); | ||
| 384 | my $currentNode = $nodeIterator->nextNode(); # returns the next node | ||
| 385 | my $previousNode = $nodeIterator->previousNode(); # same result, since we backtracked to the previous node | ||
| 386 | |||
| 387 | See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/NodeIterator/previousNode> | ||
| 388 | |||
| 389 | =head1 AUTHOR | ||
| 390 | |||
| 391 | Jacques Deguest E<lt>F<jack@deguest.jp>E<gt> | ||
| 392 | |||
| 393 | =head1 SEE ALSO | ||
| 394 | |||
| 395 | L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/NodeIterator>, L<StackOverflow topic on NodeIterator|https://stackoverflow.com/questions/7941288/when-to-use-nodeiterator>, L<W3C specifications|https://dom.spec.whatwg.org/#interface-nodeiterator> | ||
| 396 | |||
| 397 | =head1 COPYRIGHT & LICENSE | ||
| 398 | |||
| 399 | Copyright(c) 2021 DEGUEST Pte. Ltd. | ||
| 400 | |||
| 401 | All rights reserved | ||
| 402 | |||
| 403 | This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. | ||
| 404 | |||
| 405 | =cut |