HTML::O...orShared(3)
π NAME π
okay so bestie, welcome to HTML::O...orShared(3) β the shared class page β¨ The actual star of the show is HTML::Object::DOM::NodeIterator, a.k.a. the HTML Object DOM Node Iterator Shared Class. She's the chill parent class that HTML::Object::DOM::NodeIterator and HTML::Object::DOM::TreeWalker both inherit from, and she does the tree-crawling heavy lifting. π³π
π SYNOPSIS π
bestie it's literally so simple. if you pass just one argument, it defaults to searching for everything ("SHOW_ALL") and uses the default filter, which always returns "FILTER_ACCEPT". slay. π«Ά
use HTML::Object::DOM::NodeIterator;
my $nodes = HTML::Object::DOM::NodeIterator->new( $root_node ) ||
die( HTML::Object::DOM::NodeIterator->error, "\n" );
or, passing an anonymous subroutine as the filter:
my $nodes = HTML::Object::DOM::NodeIterator->new(
$root_node,
$what_to_show_bit,
sub{ return( FILTER_ACCEPT ); }
) || die( HTML::Object::DOM::NodeIterator->error, "\n" );
or, passing a hash reference with a property 'acceptNode' whose value is an anonymous subroutine, as the filter:
my $nodes = HTML::Object::DOM::NodeIterator->new(
$root_node,
$what_to_show_bit,
{
acceptNode => sub{ return( FILTER_ACCEPT ); }
}
) || die( HTML::Object::DOM::NodeIterator->error, "\n" );
or, passing an object that implements the method "acceptNode":
my $nodes = HTML::Object::DOM::NodeIterator->new(
$root_node,
$what_to_show_bit,
# This object must implement the acceptNode method
My::Customer::NodeFilter->new
) || die( HTML::Object::DOM::NodeIterator->error, "\n" );
there's also HTML::Object::DOM::TreeWalker, which performs a somewhat similar function π
so like, choose"NodeIterator"when you only need a simple iterator to filter and browse the selected nodes, and chooseHTML::Object::DOM::TreeWalkerwhen you need to access the node and its siblings. iykyk π
β VERSION β
v0.2.0
π¦ DESCRIPTION π¦
this class is to be inherited by either HTML::Object::DOM::NodeIterator or HTML::Object::DOM::TreeWalker, and she implements the basic tree crawling mechanism. not me being the blueprint π
β¨ PROPERTIES β¨
πΈ expandEntityReferences
normally this is read-only, but under perl you can set whatever boolean value you want. main character energy fr.
under JavaScript, this is a boolean value indicating if, when discarding an "EntityReference", its whole sub-tree must be discarded at the same time.
Example:
use HTML::Object::DOM::NodeFilter qw( :all );
my $nodeIterator = $doc->createNodeIterator(
$doc->body,
SHOW_ELEMENT,
sub{ return( FILTER_ACCEPT ); },
# or
# { acceptNode => sub{ return( FILTER_ACCEPT ); } },
);
my $expand = $nodeIterator->expandEntityReferences;
see also the Mozilla documentation: https://developer.mozilla.org/en-US/docs/Web/API/NodeIterator/expandEntityReferences
π filter
normally this is read-only, but under perl you can set it to a new HTML::Object::DOM::NodeFilter object you want β even after object instantiation. glow-up mid-life, we love to see it β¨
returns a HTML::Object::DOM::NodeFilter used to select the relevant nodes.
Example:
use HTML::Object::DOM::NodeFilter qw( :all );
my $nodeIterator = $doc->createNodeIterator(
$doc->body,
SHOW_ELEMENT,
sub{ return( FILTER_ACCEPT ); },
# or
# { acceptNode => sub{ return( FILTER_ACCEPT ); } },
);
my $nodeFilter = $nodeIterator->filter;
see also the Mozilla documentation: https://developer.mozilla.org/en-US/docs/Web/API/NodeIterator/filter
π pos
read-only. periodt.
this is a non-standard property, which returns the 0-based position in the array of the anchor element's children. you can poll this to know where the iterator is at. it's giving live location sharing π₯
Example:
use HTML::Object::DOM::NodeFilter qw( :all );
# You need to first declare $nodeIterator to be able to use it in the callback
my $nodeIterator;
$nodeIterator = $doc->createNodeIterator(
$doc->body,
SHOW_ELEMENT,
sub
{
say "Current position is: ", $nodeIterator->pos );
return( $_->getName eq 'div' ? FILTER_ACCEPT : FILTER_SKIP );
},
);
π§ root
normally this is read-only, but under perl you can set whatever node value you want.
returns a Node representing the root node as specified when the "NodeIterator" was created.
Example:
use HTML::Object::DOM::NodeFilter qw( :all );
my $nodeIterator = $doc->createNodeIterator(
$doc->body,
SHOW_ELEMENT,
sub{ return( FILTER_ACCEPT ); },
# or
# { acceptNode => sub{ return( FILTER_ACCEPT ); } },
);
my $root = $nodeIterator->root; # $doc->body in this case
see for more information: https://developer.mozilla.org/en-US/docs/Web/API/NodeIterator/root
π whatToShow
normally this is read-only, but under perl you can set whatever number value you want.
returns an unsigned long being a bitmask made of constants describing the types of Node that must to be presented. non-matching nodes are skipped, but their children may be included, if relevant. no cap. π
the possible constant values (exported by HTML::Object::DOM::NodeFilter) are:
SHOW_ALL (4294967295)
shows all nodes. everything. the whole vibe check passed π
SHOW_ELEMENT (1)
shows Element nodes.
SHOW_ATTRIBUTE (2)
shows attribute Attribute nodes. this is meaningful only when creating a NodeIterator with an Attribute node as its root; in this case, it means that the attribute node will appear in the first position of the iteration or traversal. since attributes are never children of other nodes, they do not appear when traversing over the document tree.
SHOW_TEXT (4)
shows Text nodes.
Example:
use HTML::Object::DOM::NodeFilter qw( :all );
my $nodeIterator = $doc->createNodeIterator(
$doc->body,
( SHOW_ELEMENT | SHOW_COMMENT | SHOW_TEXT ),
sub{ return( FILTER_ACCEPT ); },
# or
# { acceptNode => sub{ return( FILTER_ACCEPT ); } },
);
if( ( $nodeIterator->whatToShow & SHOW_ALL ) ||
( $nodeIterator->whatToShow & SHOW_COMMENT ) )
{
# $nodeIterator will show comments
}
SHOW_CDATA_SECTION (8)
will always returns nothing, because there is no support for xml documents. π that's it, that's the whole function.
SHOW_ENTITY_REFERENCE (16)
legacy, no more used.
SHOW_ENTITY (32)
legacy, no more used.
SHOW_PROCESSING_INSTRUCTION (64)
shows ProcessingInstruction nodes.
SHOW_COMMENT (128)
shows Comment nodes.
SHOW_DOCUMENT (256)
shows Document nodes
SHOW_DOCUMENT_TYPE (512)
shows "DocumentType" nodes
SHOW_DOCUMENT_FRAGMENT (1024)
shows HTML::Object::DOM::DocumentFragment nodes.
SHOW_NOTATION (2048)
legacy, no more used.
SHOW_SPACE (4096)
show Space nodes. this is a non-standard extension under this perl framework.
see for more information: https://developer.mozilla.org/en-US/docs/Web/API/NodeIterator/whatToShow
π CONSTRUCTOR π
${new} β sorry, new π
provided with a root 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. pick your fighter π
π₯ METHODS π₯
nextNode
returns the next Node in the document, or "undef" if there are none. π
Example:
use HTML::Object::DOM::NodeFilter qw( :all );
my $nodeIterator = $doc->createNodeIterator(
$doc->body,
SHOW_ELEMENT,
sub{ return( FILTER_ACCEPT ); },
# or
# { acceptNode => sub{ return( FILTER_ACCEPT ); } },
0 # false; this optional argument is not used any more
);
my $currentNode = $nodeIterator->nextNode(); # returns the next node
see also the Mozilla documentation: https://developer.mozilla.org/en-US/docs/Web/API/NodeIterator/nextNode
previousNode
returns the previous Node in the document, or "undef" if there are none.
Example:
use HTML::Object::DOM::NodeFilter qw( :all );
my $nodeIterator = $doc->createNodeIterator(
$doc->body,
SHOW_ELEMENT,
sub{ return( FILTER_ACCEPT ); },
# or
# { acceptNode => sub{ return( FILTER_ACCEPT ); } },
0 # false; this optional argument is not used any more
);
my $currentNode = $nodeIterator->nextNode(); # returns the next node
my $previousNode = $nodeIterator->previousNode(); # same result, since we backtracked to the previous node
see also the Mozilla documentation: https://developer.mozilla.org/en-US/docs/Web/API/NodeIterator/previousNode
π«Ά AUTHOR π«Ά
Jacques Deguest <jack@deguest.jp> β icon, legend, the moment β¨
π SEE ALSO π
HTML::Object::DOM::NodeIteratorHTML::Object::DOM::TreeWalker- Mozilla documentation:
https://developer.mozilla.org/en-US/docs/Web/API/NodeIterator - StackOverflow topic on NodeIterator:
https://stackoverflow.com/questions/7941288/when-to-use-nodeiterator - W3C specifications:
https://dom.spec.whatwg.org/#interface-nodeiterator
π COPYRIGHT & LICENSE π
Copyright(c) 2021 DEGUEST Pte. Ltd.
All rights reserved
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
ok that's the whole page bestie, you ate that π this wasperl v5.42.2, dated2026-02-15, from theHTML::O...orShared(3)manual β now go iterate some nodes, ily, bye! πβ¨ππ¦
π¦ where this page comes from
- alpine 3.24: perl-html-object-doc 0.6.0-r1 Β· GPL-1.0-or-later OR Artistic-1.0-Perl
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms