blib/lib/HTML/Object/DOM/NodeFilter.pm untested

filter: Statementstmt 0.0% 0 / 35 Branchbran n/a Conditioncond 0.0% 0 / 2 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 51 CC 1 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
init
2.0
acceptNode
2.0
CRAP 2.0
1 ##----------------------------------------------------------------------------
2 ## HTML Object - ~/lib/HTML/Object/DOM/NodeFilter.pm
3 ## Version v0.2.0
4 ## Copyright(c) 2022 DEGUEST Pte. Ltd.
5 ## Author: Jacques Deguest <jack@deguest.jp>
6 ## Created 2022/01/01
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::NodeFilter;
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( @EXPORT_OK %EXPORT_TAGS $VERSION );
22 use HTML::Object::Exception;
23 use constant {
24 # Shows all nodes.
25 SHOW_ALL => 4294967295,
26 # Shows Element nodes.
27 SHOW_ELEMENT => 1,
28 # Shows attribute Attr nodes.
29 SHOW_ATTRIBUTE => 2,
30 # Shows Text nodes.
31 SHOW_TEXT => 4,
32 # Shows CDATASection nodes.
33 SHOW_CDATA_SECTION => 8,
34 # Legacy, no more used.
35 SHOW_ENTITY_REFERENCE => 16,
36 # Legacy, no more used.
37 SHOW_ENTITY => 32,
38 # Shows ProcessingInstruction nodes.
39 SHOW_PROCESSING_INSTRUCTION => 64,
40 # Shows Comment nodes.
41 SHOW_COMMENT => 128,
42 # Shows Document nodes.
43 SHOW_DOCUMENT => 256,
44 # Shows DocumentType nodes.
45 SHOW_DOCUMENT_TYPE => 512,
46 # Shows DocumentFragment nodes.
47 SHOW_DOCUMENT_FRAGMENT => 1024,
48 # Legacy, no more used.
49 SHOW_NOTATION => 2048,
50 # Show spaces; non-standard addition
51 SHOW_SPACE => 4096,
52
53 FILTER_ACCEPT => 1,
54 FILTER_REJECT => 2,
55 FILTER_SKIP => 3,
56 };
57 our @EXPORT_OK = qw(
58 SHOW_ALL SHOW_ELEMENT SHOW_ATTRIBUTE SHOW_TEXT SHOW_CDATA_SECTION
59 SHOW_ENTITY_REFERENCE SHOW_ENTITY SHOW_PROCESSING_INSTRUCTION SHOW_COMMENT
60 SHOW_DOCUMENT SHOW_DOCUMENT_TYPE SHOW_DOCUMENT_FRAGMENT SHOW_NOTATION SHOW_SPACE
61 FILTER_ACCEPT FILTER_REJECT FILTER_SKIP
62 );
63 our %EXPORT_TAGS = ( all => [@EXPORT_OK] );
64 our $VERSION = 'v0.2.0';
65 };
66  
67 use strict;
68 use warnings;
69  
70 sub init
71 {
72 my $self = shift( @_ );
73 $self->{_init_strict_use_sub} = 1;
74 $self->SUPER::init( @_ ) || return( $self->pass_error );
75 return( $self );
76 }
77  
78 sub acceptNode { return( FILTER_ACCEPT ); }
79  
80 1;
81 # NOTE: POD
82 __END__
83  
84 =encoding utf-8
85  
86 =head1 NAME
87  
88 HTML::Object::DOM::NodeFilter - HTML Object DOM Node Filter
89  
90 =head1 SYNOPSIS
91  
92 use HTML::Object::DOM::NodeFilter;
93 my $filter = HTML::Object::DOM::NodeFilter->new ||
94 die( HTML::Object::DOM::NodeFilter->error, "\n" );
95  
96 =head1 VERSION
97  
98 v0.2.0
99  
100 =head1 DESCRIPTION
101  
102 A C<NodeFilter> interface represents an object used to filter the nodes in a L<HTML::Object::DOM::NodeIterator> or L<HTML::Object::DOM::::TreeWalker>. A C<NodeFilter> knows nothing about the document or traversing nodes; it only knows how to evaluate a single node against the provided filter.
103  
104 =head1 PROPERTIES
105  
106 There are no properties.
107  
108 =head1 METHODS
109  
110 =head2 acceptNode
111  
112 Returns an unsigned short that will be used to tell if a given L<Node|HTML::Object::DOM::Node> must be accepted or not by the L<HTML::Object::DOM::NodeIterator> or L<HTML::Object::DOM::TreeWalker> iteration algorithm.
113  
114 This method is expected to be written by the user of a C<NodeFilter>. Possible return values are:
115  
116 =over 4
117  
118 =item FILTER_ACCEPT
119  
120 Value returned by the L</acceptNode> method when a node should be accepted.
121  
122 =item FILTER_REJECT
123  
124 Value to be returned by the L</acceptNode> method when a node should be rejected. For L<HTML::Object::DOM::TreeWalker>, child nodes are also rejected.
125  
126 For C<NodeIterator>, this flag is synonymous with C<FILTER_SKIP>.
127  
128 =item FILTER_SKIP
129  
130 Value to be returned by L</acceptNode> for nodes to be skipped by the L<HTML::Object::DOM::NodeIterator> or L<HTML::Object::DOM::TreeWalker> object.
131  
132 The children of skipped nodes are still considered. This is treated as "skip this node but not its children".
133  
134 Example:
135  
136 use HTML::Object::DOM::NodeFilter qw( :all );
137 my $nodeIterator = $doc->createNodeIterator(
138 # Node to use as root
139 $doc->getElementById('someId'),
140  
141 # Only consider nodes that are text nodes (nodeType 3)
142 SHOW_TEXT,
143  
144 # Object containing the sub to use for the acceptNode method
145 # of the NodeFilter
146 { acceptNode => sub
147 {
148 my $node = shift( @_ ); # also available as $_
149 # Logic to determine whether to accept, reject or skip node
150 # In this case, only accept nodes that have content other than whitespace
151 if( $node->data !~ /^\s*$/ )
152 {
153 return( FILTER_ACCEPT );
154 }
155 }
156 },
157 0 # false
158 );
159  
160 # Show the content of every non-empty text node that is a child of root
161 my $node;
162 while( ( $node = $nodeIterator->nextNode() ) )
163 {
164 say( $node->data );
165 }
166  
167 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/NodeFilter/acceptNode>
168  
169 =back
170  
171 =head1 CONSTANTS
172  
173 =over 4
174  
175 =item SHOW_ALL (4294967295)
176  
177 Shows all nodes.
178  
179 =item SHOW_ELEMENT (1)
180  
181 Shows Element nodes.
182  
183 =item SHOW_ATTRIBUTE (2)
184  
185 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.
186  
187 =item SHOW_TEXT (4)
188  
189 Shows Text nodes.
190  
191 Example:
192  
193 use HTML::Object::DOM::NodeFilter qw( :all );
194 my $nodeIterator = $doc->createNodeIterator(
195 $doc->body,
196 SHOW_ELEMENT | SHOW_COMMENT | SHOW_TEXT,
197 { acceptNode => sub{ return( FILTER_ACCEPT ); } },
198 0 # false
199 );
200 if( ( $nodeIterator->whatToShow & SHOW_ALL ) ||
201 ( $nodeIterator->whatToShow & SHOW_COMMENT ) )
202 {
203 # $nodeIterator will show comments
204 }
205  
206 =item SHOW_CDATA_SECTION (8)
207  
208 Will always returns nothing, because there is no support for xml documents.
209  
210 =item SHOW_ENTITY_REFERENCE (16)
211  
212 Legacy, no more used.
213  
214 =item SHOW_ENTITY (32)
215  
216 Legacy, no more used.
217  
218 =item SHOW_PROCESSING_INSTRUCTION (64)
219  
220 Shows ProcessingInstruction nodes.
221  
222 =item SHOW_COMMENT (128)
223  
224 Shows Comment nodes.
225  
226 =item SHOW_DOCUMENT (256)
227  
228 Shows Document nodes
229  
230 =item SHOW_DOCUMENT_TYPE (512)
231  
232 Shows C<DocumentType> nodes
233  
234 =item SHOW_DOCUMENT_FRAGMENT (1024)
235  
236 Shows L<HTML::Object::DOM::DocumentFragment> nodes.
237  
238 =item SHOW_NOTATION (2048)
239  
240 Legacy, no more used.
241  
242 =item SHOW_SPACE (4096)
243  
244 Show Space nodes. This is a non-standard extension under this perl framework.
245  
246 =back
247  
248 And for the callback control:
249  
250 =over 4
251  
252 =item FILTER_ACCEPT (1)
253  
254 =item FILTER_REJECT (2)
255  
256 =item FILTER_SKIP (3)
257  
258 =back
259  
260 =head1 AUTHOR
261  
262 Jacques Deguest E<lt>F<jack@deguest.jp>E<gt>
263  
264 =head1 SEE ALSO
265  
266 L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/NodeFilter>, L<W3C specifications|https://dom.spec.whatwg.org/#interface-nodefilter>
267  
268 =head1 COPYRIGHT & LICENSE
269  
270 Copyright(c) 2022 DEGUEST Pte. Ltd.
271  
272 All rights reserved
273  
274 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
275  
276 =cut