HTML::Object::DOM::NodeFilter - HTML Object DOM Node Filter
use HTML::Object::DOM::NodeFilter; my $filter = HTML::Object::DOM::NodeFilter->new || die( HTML::Object::DOM::NodeFilter->error, "\n" );
v0.2.0
A NodeFilter interface represents an object used to filter the nodes in a HTML::Object::DOM::NodeIterator or HTML::Object::DOM::::TreeWalker. A NodeFilter knows nothing about the document or traversing nodes; it only knows how to evaluate a single node against the provided filter.
NodeFilter
There are no properties.
Returns an unsigned short that will be used to tell if a given Node must be accepted or not by the HTML::Object::DOM::NodeIterator or HTML::Object::DOM::TreeWalker iteration algorithm.
This method is expected to be written by the user of a NodeFilter. Possible return values are:
Value returned by the "acceptNode" method when a node should be accepted.
Value to be returned by the "acceptNode" method when a node should be rejected. For HTML::Object::DOM::TreeWalker, child nodes are also rejected.
For NodeIterator, this flag is synonymous with FILTER_SKIP.
NodeIterator
FILTER_SKIP
Value to be returned by "acceptNode" for nodes to be skipped by the HTML::Object::DOM::NodeIterator or HTML::Object::DOM::TreeWalker object.
The children of skipped nodes are still considered. This is treated as "skip this node but not its children".
Example:
use HTML::Object::DOM::NodeFilter qw( :all ); my $nodeIterator = $doc->createNodeIterator( # Node to use as root $doc->getElementById('someId'), # Only consider nodes that are text nodes (nodeType 3) SHOW_TEXT, # Object containing the sub to use for the acceptNode method # of the NodeFilter { acceptNode => sub { my $node = shift( @_ ); # also available as $_ # Logic to determine whether to accept, reject or skip node # In this case, only accept nodes that have content other than whitespace if( $node->data !~ /^\s*$/ ) { return( FILTER_ACCEPT ); } } }, 0 # false ); # Show the content of every non-empty text node that is a child of root my $node; while( ( $node = $nodeIterator->nextNode() ) ) { say( $node->data ); }
See also Mozilla documentation
Shows all nodes.
Shows Element nodes.
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.
Shows Text nodes.
use HTML::Object::DOM::NodeFilter qw( :all ); my $nodeIterator = $doc->createNodeIterator( $doc->body, SHOW_ELEMENT | SHOW_COMMENT | SHOW_TEXT, { acceptNode => sub{ return( FILTER_ACCEPT ); } }, 0 # false ); if( ( $nodeIterator->whatToShow & SHOW_ALL ) || ( $nodeIterator->whatToShow & SHOW_COMMENT ) ) { # $nodeIterator 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.
And for the callback control:
Jacques Deguest <jack@deguest.jp>
Mozilla documentation, W3C specifications
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.