| 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__ | ||
| 265 | |||
| 266 | =encoding utf-8 | ||
| 267 | |||
| 268 | =head1 NAME | ||
| 269 | |||
| 270 | HTML::Object::DOM::NodeIterator - HTML Object DOM Node Iterator Shared Class | ||
| 271 | |||
| 272 | =head1 SYNOPSIS | ||
| 273 | |||
| 274 | 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> | ||
| 275 | |||
| 276 | use HTML::Object::DOM::NodeIterator; | ||
| 277 | my $nodes = HTML::Object::DOM::NodeIterator->new( $root_node ) || | ||
| 278 | die( HTML::Object::DOM::NodeIterator->error, "\n" ); | ||
| 279 | |||
| 280 | Or, passing an anonymous subroutine as the filter | ||
| 281 | |||
| 282 | my $nodes = HTML::Object::DOM::NodeIterator->new( | ||
| 283 | $root_node, | ||
| 284 | $what_to_show_bit, | ||
| 285 | sub{ return( FILTER_ACCEPT ); } | ||
| 286 | ) || die( HTML::Object::DOM::NodeIterator->error, "\n" ); | ||
| 287 | |||
| 288 | Or, passing an hash reference with a property 'acceptNode' whose value is an anonymous subroutine, as the filter | ||
| 289 | |||
| 290 | my $nodes = HTML::Object::DOM::NodeIterator->new( | ||
| 291 | $root_node, | ||
| 292 | $what_to_show_bit, | ||
| 293 | { | ||
| 294 | acceptNode => sub{ return( FILTER_ACCEPT ); } | ||
| 295 | } | ||
| 296 | ) || die( HTML::Object::DOM::NodeIterator->error, "\n" ); | ||
| 297 | |||
| 298 | Or, passing an object that implements the method "acceptNode" | ||
| 299 | |||
| 300 | my $nodes = HTML::Object::DOM::NodeIterator->new( | ||
| 301 | $root_node, | ||
| 302 | $what_to_show_bit, | ||
| 303 | # This object must implement the acceptNode method | ||
| 304 | My::Customer::NodeFilter->new | ||
| 305 | ) || die( HTML::Object::DOM::NodeIterator->error, "\n" ); | ||
| 306 | |||
| 307 | There is also L<HTML::Object::DOM::TreeWalker>, which performs a somewhat similar function. | ||
| 308 | |||
| 309 | Choose C<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. | ||
| 310 | |||
| 311 | =head1 VERSION | ||
| 312 | |||
| 313 | v0.2.0 | ||
| 314 | |||
| 315 | =head1 DESCRIPTION | ||
| 316 | |||
| 317 | This class is to be inherited by either L<HTML::Object::DOM::NodeIterator> or L<HTML::Object::DOM::TreeWalker> and implements basic tree crawling mechanism. | ||
| 318 | |||
| 319 | =head1 PROPERTIES | ||
| 320 | |||
| 321 | =head2 expandEntityReferences | ||
| 322 | |||
| 323 | Normally this is read-only, but under perl you can set whatever boolean value you want. | ||
| 324 | |||
| 325 | 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. | ||
| 326 | |||
| 327 | Example: | ||
| 328 | |||
| 329 | use HTML::Object::DOM::NodeFilter qw( :all ); | ||
| 330 | my $nodeIterator = $doc->createNodeIterator( | ||
| 331 | $doc->body, | ||
| 332 | SHOW_ELEMENT, | ||
| 333 | sub{ return( FILTER_ACCEPT ); }, | ||
| 334 | # or | ||
| 335 | # { acceptNode => sub{ return( FILTER_ACCEPT ); } }, | ||
| 336 | ); | ||
| 337 | my $expand = $nodeIterator->expandEntityReferences; | ||
| 338 | |||
| 339 | See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/NodeIterator/expandEntityReferences> | ||
| 340 | |||
| 341 | =head2 filter | ||
| 342 | |||
| 343 | 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. | ||
| 344 | |||
| 345 | Returns a L<HTML::Object::DOM::NodeFilter> used to select the relevant nodes. | ||
| 346 | |||
| 347 | Example: | ||
| 348 | |||
| 349 | use HTML::Object::DOM::NodeFilter qw( :all ); | ||
| 350 | my $nodeIterator = $doc->createNodeIterator( | ||
| 351 | $doc->body, | ||
| 352 | SHOW_ELEMENT, | ||
| 353 | sub{ return( FILTER_ACCEPT ); }, | ||
| 354 | # or | ||
| 355 | # { acceptNode => sub{ return( FILTER_ACCEPT ); } }, | ||
| 356 | ); | ||
| 357 | my $nodeFilter = $nodeIterator->filter; | ||
| 358 | |||
| 359 | See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/NodeIterator/filter> | ||
| 360 | |||
| 361 | =head2 pos | ||
| 362 | |||
| 363 | Read-only. | ||
| 364 | |||
| 365 | This is a non-standard property, which returns the 0-based position in the array of the anchor element's children. | ||
| 366 | |||
| 367 | You can poll this to know where the iterator is at. | ||
| 368 | |||
| 369 | Example: | ||
| 370 | |||
| 371 | use HTML::Object::DOM::NodeFilter qw( :all ); | ||
| 372 | # You need to first declare $nodeIterator to be able to use it in the callback | ||
| 373 | my $nodeIterator; | ||
| 374 | $nodeIterator = $doc->createNodeIterator( | ||
| 375 | $doc->body, | ||
| 376 | SHOW_ELEMENT, | ||
| 377 | sub | ||
| 378 | { | ||
| 379 | say "Current position is: ", $nodeIterator->pos ); | ||
| 380 | return( $_->getName eq 'div' ? FILTER_ACCEPT : FILTER_SKIP ); | ||
| 381 | }, | ||
| 382 | ); | ||
| 383 | |||
| 384 | =head2 root | ||
| 385 | |||
| 386 | Normally this is read-only, but under perl you can set whatever L<node value|HTML::Object::DOM::Node> you want. | ||
| 387 | |||
| 388 | Returns a L<Node|HTML::Object::DOM::Node> representing the root node as specified when the C<NodeIterator> was created. | ||
| 389 | |||
| 390 | Example: | ||
| 391 | |||
| 392 | use HTML::Object::DOM::NodeFilter qw( :all ); | ||
| 393 | my $nodeIterator = $doc->createNodeIterator( | ||
| 394 | $doc->body, | ||
| 395 | SHOW_ELEMENT, | ||
| 396 | sub{ return( FILTER_ACCEPT ); }, | ||
| 397 | # or | ||
| 398 | # { acceptNode => sub{ return( FILTER_ACCEPT ); } }, | ||
| 399 | ); | ||
| 400 | my $root = $nodeIterator->root; # $doc->body in this case | ||
| 401 | |||
| 402 | See L<for more information|https://developer.mozilla.org/en-US/docs/Web/API/NodeIterator/root> | ||
| 403 | |||
| 404 | =head2 whatToShow | ||
| 405 | |||
| 406 | Normally this is read-only, but under perl you can set whatever number value you want. | ||
| 407 | |||
| 408 | 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. | ||
| 409 | |||
| 410 | Possible constant values (exported by L<HTML::Object::DOM::NodeFilter>) are: | ||
| 411 | |||
| 412 | =over 4 | ||
| 413 | |||
| 414 | =item SHOW_ALL (4294967295) | ||
| 415 | |||
| 416 | Shows all nodes. | ||
| 417 | |||
| 418 | =item SHOW_ELEMENT (1) | ||
| 419 | |||
| 420 | Shows Element nodes. | ||
| 421 | |||
| 422 | =item SHOW_ATTRIBUTE (2) | ||
| 423 | |||
| 424 | Shows attribute L<Attribute nodes|HTML::Object::DOM::Attribute>. This is meaningful only when creating a 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. | ||
| 425 | |||
| 426 | =item SHOW_TEXT (4) | ||
| 427 | |||
| 428 | Shows Text nodes. | ||
| 429 | |||
| 430 | Example: | ||
| 431 | |||
| 432 | use HTML::Object::DOM::NodeFilter qw( :all ); | ||
| 433 | my $nodeIterator = $doc->createNodeIterator( | ||
| 434 | $doc->body, | ||
| 435 | ( SHOW_ELEMENT | SHOW_COMMENT | SHOW_TEXT ), | ||
| 436 | sub{ return( FILTER_ACCEPT ); }, | ||
| 437 | # or | ||
| 438 | # { acceptNode => sub{ return( FILTER_ACCEPT ); } }, | ||
| 439 | ); | ||
| 440 | if( ( $nodeIterator->whatToShow & SHOW_ALL ) || | ||
| 441 | ( $nodeIterator->whatToShow & SHOW_COMMENT ) ) | ||
| 442 | { | ||
| 443 | # $nodeIterator will show comments | ||
| 444 | } | ||
| 445 | |||
| 446 | =item SHOW_CDATA_SECTION (8) | ||
| 447 | |||
| 448 | Will always returns nothing, because there is no support for xml documents. | ||
| 449 | |||
| 450 | =item SHOW_ENTITY_REFERENCE (16) | ||
| 451 | |||
| 452 | Legacy, no more used. | ||
| 453 | |||
| 454 | =item SHOW_ENTITY (32) | ||
| 455 | |||
| 456 | Legacy, no more used. | ||
| 457 | |||
| 458 | =item SHOW_PROCESSING_INSTRUCTION (64) | ||
| 459 | |||
| 460 | Shows ProcessingInstruction nodes. | ||
| 461 | |||
| 462 | =item SHOW_COMMENT (128) | ||
| 463 | |||
| 464 | Shows Comment nodes. | ||
| 465 | |||
| 466 | =item SHOW_DOCUMENT (256) | ||
| 467 | |||
| 468 | Shows Document nodes | ||
| 469 | |||
| 470 | =item SHOW_DOCUMENT_TYPE (512) | ||
| 471 | |||
| 472 | Shows C<DocumentType> nodes | ||
| 473 | |||
| 474 | =item SHOW_DOCUMENT_FRAGMENT (1024) | ||
| 475 | |||
| 476 | Shows L<HTML::Object::DOM::DocumentFragment> nodes. | ||
| 477 | |||
| 478 | =item SHOW_NOTATION (2048) | ||
| 479 | |||
| 480 | Legacy, no more used. | ||
| 481 | |||
| 482 | =item SHOW_SPACE (4096) | ||
| 483 | |||
| 484 | Show Space nodes. This is a non-standard extension under this perl framework. | ||
| 485 | |||
| 486 | =back | ||
| 487 | |||
| 488 | See L<for more information|https://developer.mozilla.org/en-US/docs/Web/API/NodeIterator/whatToShow> | ||
| 489 | |||
| 490 | =head1 CONSTRUCTOR | ||
| 491 | |||
| 492 | =head2 new | ||
| 493 | |||
| 494 | 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 or tree walker depending on the class used. | ||
| 495 | |||
| 496 | =head1 METHODS | ||
| 497 | |||
| 498 | =head2 nextNode | ||
| 499 | |||
| 500 | Returns the next L<Node|HTML::Object::DOM::Node> in the document, or C<undef> if there are none. | ||
| 501 | |||
| 502 | Example: | ||
| 503 | |||
| 504 | use HTML::Object::DOM::NodeFilter qw( :all ); | ||
| 505 | my $nodeIterator = $doc->createNodeIterator( | ||
| 506 | $doc->body, | ||
| 507 | SHOW_ELEMENT, | ||
| 508 | sub{ return( FILTER_ACCEPT ); }, | ||
| 509 | # or | ||
| 510 | # { acceptNode => sub{ return( FILTER_ACCEPT ); } }, | ||
| 511 | 0 # false; this optional argument is not used any more | ||
| 512 | ); | ||
| 513 | my $currentNode = $nodeIterator->nextNode(); # returns the next node | ||
| 514 | |||
| 515 | See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/NodeIterator/nextNode> | ||
| 516 | |||
| 517 | =head2 previousNode | ||
| 518 | |||
| 519 | Returns the previous L<Node|HTML::Object::DOM::Node> in the document, or C<undef> if there are none. | ||
| 520 | |||
| 521 | Example: | ||
| 522 | |||
| 523 | use HTML::Object::DOM::NodeFilter qw( :all ); | ||
| 524 | my $nodeIterator = $doc->createNodeIterator( | ||
| 525 | $doc->body, | ||
| 526 | SHOW_ELEMENT, | ||
| 527 | sub{ return( FILTER_ACCEPT ); }, | ||
| 528 | # or | ||
| 529 | # { acceptNode => sub{ return( FILTER_ACCEPT ); } }, | ||
| 530 | 0 # false; this optional argument is not used any more | ||
| 531 | ); | ||
| 532 | my $currentNode = $nodeIterator->nextNode(); # returns the next node | ||
| 533 | my $previousNode = $nodeIterator->previousNode(); # same result, since we backtracked to the previous node | ||
| 534 | |||
| 535 | See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/NodeIterator/previousNode> | ||
| 536 | |||
| 537 | =head1 AUTHOR | ||
| 538 | |||
| 539 | Jacques Deguest E<lt>F<jack@deguest.jp>E<gt> | ||
| 540 | |||
| 541 | =head1 SEE ALSO | ||
| 542 | |||
| 543 | L<HTML::Object::DOM::NodeIterator>, L<HTML::Object::DOM::TreeWalker> | ||
| 544 | |||
| 545 | 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> | ||
| 546 | |||
| 547 | =head1 COPYRIGHT & LICENSE | ||
| 548 | |||
| 549 | Copyright(c) 2021 DEGUEST Pte. Ltd. | ||
| 550 | |||
| 551 | All rights reserved | ||
| 552 | |||
| 553 | This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. | ||
| 554 | |||
| 555 | =cut |