HTML::Object::DOM::TreeWalker - HTML Object DOM Tree Walker Class
With just one argument, this default to search for everything (SHOW_ALL) and to use the default filter, which always returns FILTER_ACCEPT
SHOW_ALL
FILTER_ACCEPT
use HTML::Object::DOM::TreeWalker; my $walker = HTML::Object::DOM::TreeWalker->new( $doc->body ) || die( HTML::Object::DOM::TreeWalker->error, "\n" );
Or, passing an anonymous subroutine as the filter
my $nodes = HTML::Object::DOM::TreeWalker->new( $root_node, $what_to_show_bit, sub{ return( FILTER_ACCEPT ); } ) || die( HTML::Object::DOM::TreeWalker->error, "\n" );
Or, passing an hash reference with a property 'acceptNode' whose value is an anonymous subroutine, as the filter
my $nodes = HTML::Object::DOM::TreeWalker->new( $root_node, $what_to_show_bit, { acceptNode => sub{ return( FILTER_ACCEPT ); } } ) || die( HTML::Object::DOM::TreeWalker->error, "\n" );
Or, passing an object that implements the method "acceptNode"
my $nodes = HTML::Object::DOM::TreeWalker->new( $root_node, $what_to_show_bit, # This object must implement the acceptNode method My::Customer::NodeFilter->new ) || die( HTML::Object::DOM::TreeWalker->error, "\n" );
There is also HTML::Object::DOM::TreeWalker, which performs a somewhat similar function.
Choose HTML::Object::DOM::NodeIterator when you only need a simple iterator to filter and browse the selected nodes, and choose HTML::Object::DOM::TreeWalker when you need to access to the node and its siblings.
v0.2.0
The TreeWalker object represents the nodes of a document subtree and a position within them.
TreeWalker
Is the Node on which the TreeWalker is currently pointing at.
Example:
use HTML::Object::DOM::NodeFilter qw( :all ); my $treeWalker = $doc->createTreeWalker( $doc->body, SHOW_ELEMENT, sub{ return( FILTER_ACCEPT ); } ); my $root = $treeWalker->currentNode; # the root element as it is the first element!
See also Mozilla documentation
Normally this is read-only, but under perl you can set whatever boolean value you want.
Under JavaScript, this is a boolean value indicating if, when discarding an EntityReference its whole sub-tree must be discarded at the same time.
EntityReference
use HTML::Object::DOM::NodeFilter qw( :all ); my $treeWalker = $doc->createTreeWalker( $doc->body, SHOW_ELEMENT, sub{ return( FILTER_ACCEPT ); }, # or # { acceptNode => sub{ return( FILTER_ACCEPT ); } }, ); my $expand = $treeWalker->expandEntityReferences;
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.
Returns a HTML::Object::DOM::NodeFilter used to select the relevant nodes.
use HTML::Object::DOM::NodeFilter qw( :all ); my $treeWalker = $doc->createTreeWalker( $doc->body, SHOW_ELEMENT, sub{ return( FILTER_ACCEPT ); }, # or # { acceptNode => sub{ return( FILTER_ACCEPT ); } }, ); my $nodeFilter = $treeWalker->filter;
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 TreeWalker was created.
use HTML::Object::DOM::NodeFilter qw( :all ); my $treeWalker = $doc->createTreeWalker( $doc->body, SHOW_ELEMENT, sub{ return( FILTER_ACCEPT ); }, # or # { acceptNode => sub{ return( FILTER_ACCEPT ); } }, ); my $root = $treeWalker->root; # $doc->body in this case
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.
Possible constant values (exported by HTML::Object::DOM::NodeFilter) are:
Shows all nodes.
Shows Element nodes.
Shows attribute Attribute nodes. This is meaningful only when creating a TreeWalker 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.
Shows Text nodes.
use HTML::Object::DOM::NodeFilter qw( :all ); my $treeWalker = $doc->createTreeWalker( $doc->body, ( SHOW_ELEMENT | SHOW_COMMENT | SHOW_TEXT ), sub{ return( FILTER_ACCEPT ); }, # or # { acceptNode => sub{ return( FILTER_ACCEPT ); } }, ); if( ( $treeWalker->whatToShow & SHOW_ALL ) || ( $treeWalker->whatToShow & SHOW_COMMENT ) ) { # $treeWalker will show comments }
Will always returns nothing, because there is no support for xml documents.
Legacy, no more used.
Shows ProcessingInstruction nodes.
Shows Comment nodes.
Shows Document nodes
Shows DocumentType nodes
DocumentType
Shows HTML::Object::DOM::DocumentFragment nodes.
Show Space nodes. This is a non-standard extension under this perl framework.
Moves the current Node to the first visible child of the current node, and returns the found child. It also moves the current node to this child. If no such child exists, returns undef and the current node is not changed.
undef
my $treeWalker = $doc->createTreeWalker( $doc->body, SHOW_ELEMENT, sub{ return( FILTER_ACCEPT ); }, ); my $node = $treeWalker->firstChild(); # returns the first child of the root element, or null if none
Moves the current Node to the last visible child of the current node, and returns the found child. It also moves the current node to this child. If no such child exists, undef is returned and the current node is not changed.
my $treeWalker = $doc->createTreeWalker( $doc->body, SHOW_ELEMENT, sub{ return( FILTER_ACCEPT ); }, ); my $node = $treeWalker->lastChild(); # returns the last visible child of the root element
Moves the current Node to the next visible node in the document order, and returns the found node. It also moves the current node to this one. If no such node exists, returns undef and the current node is not changed.
my $treeWalker = $doc->createTreeWalker( $doc->body, SHOW_ELEMENT, sub{ return( FILTER_ACCEPT ); }, ); my $node = $treeWalker->nextNode(); # returns the first child of root, as it is the next $node in document order
Moves the current Node to its next sibling, if any, and returns the found sibling. If there is no such node, undef is returned and the current node is not changed.
my $treeWalker = $doc->createTreeWalker( $doc->body, SHOW_ELEMENT, sub{ return( FILTER_ACCEPT ); }, ); $treeWalker->firstChild(); my $node = $treeWalker->nextSibling(); # returns null if the first child of the root element has no sibling
Moves the current Node to the first visible ancestor node in the document order, and returns the found node. It also moves the current node to this one. If no such node exists, or if it is before that the root node defined at the object construction, returns undef and the current node is not changed.
my $treeWalker = $doc->createTreeWalker( $doc->body, SHOW_ELEMENT, sub{ return( FILTER_ACCEPT ); }, ); my $node = $treeWalker->parentNode(); # returns null as there is no parent
Moves the current Node to the previous visible node in the document order, and returns the found node. It also moves the current node to this one. If no such node exists, or if it is before that the root node defined at the object construction, returns undef and the current node is not changed.
my $treeWalker = $doc->createTreeWalker( $doc->body, SHOW_ELEMENT, sub{ return( FILTER_ACCEPT ); }, ); my $node = $treeWalker->previousNode(); # returns null as there is no parent
Moves the current Node to its previous sibling, if any, and returns the found sibling. If there is no such node, return undef and the current node is not changed.
my $treeWalker = $doc->createTreeWalker( $doc->body, SHOW_ELEMENT, sub{ return( FILTER_ACCEPT ); }, ); my $node = $treeWalker->previousSibling(); # returns null as there is no previous sibiling
Jacques Deguest <jack@deguest.jp>
Mozilla documentation
Copyright(c) 2022 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.
To install HTML::Object, copy and paste the appropriate command in to your terminal.
cpanm
cpanm HTML::Object
CPAN shell
perl -MCPAN -e shell install HTML::Object
For more information on module installation, please visit the detailed CPAN module installation guide.