lib/HTML/Object/DOM/NodeIteratorShared.pm

filter: Statementstmt 95.1% 138 / 145 Branchbran 68.3% 41 / 60 Conditioncond 56.8% 50 / 88 MC/DCmcdc 5.7% 2 / 35 Subroutinesub 86.9% 20 / 23 Podpod 100% 8 / 8 totaltotal 72.1% 259 / 359 CC 62 SCAR 29.7
CC
62
Cov
78%
_check_element
41.7
init
20.8
nextNode
11.0
CRAP 19.4
« lib/HTML/Object/DOM/Document.pm ↑ Summary lib/HTML/Object/DOM/VideoTrackList.pm »
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 3 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 3 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 3 ▶ warnings::register_categories( 'HTML::Object' );
Subroutine BEGIN: called CC=1 SCAR=0.0
20 3 ▶ use parent qw( Module::Generic );
Subroutine BEGIN: called CC=1 SCAR=0.0
21 3 ▶ use vars qw( $VERSION );
Subroutine BEGIN: called CC=1 SCAR=0.0
22 # To import its constants
23 3 ▶ use HTML::Object::DOM::Node;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 3 ▶ use HTML::Object::DOM::NodeFilter qw( :all );
Subroutine BEGIN: called CC=1 SCAR=0.0
25 3 our $VERSION = 'v0.2.0';
26 };
27  
28 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 sub init
32 {
33 10 ▶ my $self = shift( @_ );
Subroutine init: called CC=17 SCAR=30.3
Pod documented
34 10 ▶ return( $self->error({
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ < 1 0 10 10
35 message => sprintf( "Expected at least 1 arguments, but only got %d.", scalar( @_ ) ),
36 class => 'HTML::Object::SyntaxError',
37 }) ) if( scalar( @_ ) < 1 );
38 10 my $root = shift( @_ );
39 10 my $what = shift( @_ );
40 10 my( $filterDef, $filter );
41 10 ▶ $filterDef = shift( @_ ) if( ref( $_[0] ) eq 'CODE' );
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] eq "CODE" 4 6 10
42 10 my $opts = $self->_get_args_as_hash( @_ );
43 10 ▶ return( $self->error({
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_a($root, "HTML::Object::DOM::Node") 0 10 10
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 10 ▶ $what = HTML::Object::DOM::NodeFilter::SHOW_ALL if( !defined( $what ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $what 0 10 10
48 # Default value
49 10 ▶ if( !defined( $filterDef ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $filterDef) 6 4 10
50 {
51 6 $filterDef = HTML::Object::DOM::NodeFilter->new;
52 }
53
54 10 ▶ return( $self->error({
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_integer($what) 0 10 10
55 message => "Value provided for what to show is not an integer.",
56 class => 'HTML::Object::TypeError',
57 }) ) if( !$self->_is_integer( $what ) );
58 10 ▶ if( scalar( keys( %$opts ) ) )
Branch: 1 branch
BranchTrueFalseTotal
if (scalar keys %$opts) 4 6 10
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 4 ▶ }) ) if( !defined( $filterDef ) && ( !exists( $opts->{acceptNode} ) || ref( $opts->{acceptNode} ) ne 'CODE' ) );
Branch: 1 branch
BranchTrueFalseTotal
if not defined $filterDef and !exists $opts->{"acceptNode"} || ref $opts->{"acceptNode"} ne "CODE" 0 4 4
Condition16%1/6
not defined $filterDef and !exists $opts->{"acceptNode"} || ref $opts->{"acceptNode"} ne "CODE"
!ll&&!rl&&r
400
not exists $opts->{"acceptNode"} or ref $opts->{"acceptNode"} ne "CODE"
l!l&&r!l&&!r
000
MC/DC0%0/3
not defined $filterDef and !exists $opts->{"acceptNode"} || ref $opts->{"acceptNode"} ne "CODE"
not defined $filterDef not exists $opts->{"acceptNode"} ref $opts->{"acceptNode"} ne "CODE"
Truth table25%1/4
not(A) and not(B) or C
A= not defined $filterDefB= not exists $opts->{"acceptNode"}C= ref $opts->{"acceptNode"} ne "CODE"
ABCresult
0XX0
1000
11X1
1011
64 4 ▶ $filterDef = CORE::delete( $opts->{acceptNode} ) if( CORE::exists( $opts->{acceptNode} ) && ref( $opts->{acceptNode} ) eq 'CODE' );
Branch: 1 branch
BranchTrueFalseTotal
if exists $opts->{"acceptNode"} and ref $opts->{"acceptNode"} eq "CODE" 0 4 4
Condition33%1/3
exists $opts->{"acceptNode"} and ref $opts->{"acceptNode"} eq "CODE"
!ll&&!rl&&r
400
MC/DC0%0/2
exists $opts->{"acceptNode"} and ref $opts->{"acceptNode"} eq "CODE"
exists $opts->{"acceptNode"} ref $opts->{"acceptNode"} eq "CODE"
Truth table33%1/3
A and B
A= exists $opts->{"acceptNode"}B= ref $opts->{"acceptNode"} eq "CODE"
ABresult
0X0
100
111
65 }
66
67 10 ▶ if( $self->_is_object( $filterDef ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_object($filterDef)) { } 6 4 10
68 {
69 6 ▶ return( $self->error({
Branch: 1 branch
BranchTrueFalseTotal
unless $filterDef->can("acceptNode") 0 6 6
70 message => "Object provided does not implement the \"acceptNode\" method.",
71 class => 'HTML::Object::TypeError',
72 }) ) if( !$filterDef->can( 'acceptNode' ) );
73 6 ▶ $filter = sub{ return( $filterDef->acceptNode( @_ ) ); };
Subroutine __ANON__: called CC=1 SCAR=0.0
74 }
75 else
76 {
77 4 ▶ return( $self->error({
Branch: 1 branch
BranchTrueFalseTotal
if ref $filterDef ne "CODE" 0 4 4
78 message => "Filter parameter provided is not a code reference.",
79 class => 'HTML::Object::TypeError',
80 }) ) if( ref( $filterDef ) ne 'CODE' );
81 4 $filter = $filterDef;
82 }
83 10 $self->{_init_strict_use_sub} = 1;
84 10 ▶ $self->SUPER::init( @_ ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->SUPER::init(@_) or return $self->pass_error 0 10 10
85 10 $self->{children} = [];
86 10 $self->{pointerbeforereferencenode} = 1;
87 10 $self->root( $root );
88 10 $self->whatToShow( $what );
89 10 $self->filter( $filter );
90 10 $self->{_parent} = $root;
91 # This is the position of our cursor in the flatten tree represented by an array of all elements
92 10 $self->{_pos} = 0;
93 10 my $elems = $self->_flatten;
94 10 $self->_elements( $elems );
95 10 return( $self );
96 }
97  
98 # Note: property expandEntityReferences read-only
99 0 ▶sub expandEntityReferences : lvalue { return( shift->_set_get_boolean( 'expandentityreferences', @_ ) ); }
Subroutine expandEntityReferences: not called CC=1 SCAR=6.9
Pod documented
100  
101 # Note: property filter read-only
102 64 ▶sub filter : lvalue { return( shift->_set_get_code( 'filter', @_ ) ); }
Subroutine filter: called CC=1 SCAR=0.0
Pod documented
103  
104 sub nextNode
105 {
106 40 ▶ my $self = shift( @_ );
Subroutine nextNode: called CC=10 SCAR=24.0
Pod documented
107 40 my $elems = $self->_elements;
108 # Would be -1 if empty
109 40 my $size = $elems->size;
110 # We reached the end of this array
111 40 ▶ return if( $self->{_pos} >= $size );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_pos"} >= $size 0 40 40
112 40 my $whattoshow = $self->whatToShow;
113 40 ▶ return( $self->error( "Somehow the bitwise value of what to show (", ( $whattoshow // 'undef' ), ") is not an integer!" ) ) if( !$self->_is_integer( $whattoshow ) );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_integer($whattoshow) 0 40 40
Condition0%0/2
$whattoshow // "undef"
l!l
00
MC/DC0%0/1
$whattoshow // "undef"
$whattoshow
Truth table0%0/2
A
A= $whattoshow
Aresult
11
00
114 40 my $filter = $self->filter;
115 40 my $class = ref( $self );
116 # Somehow it has been changed maybe? End our iteration
117 40 ▶ if( ref( $filter ) ne 'CODE' )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $filter ne "CODE") 0 40 40
118 {
119 0 $self->{_pos} = $size;
120 0 return( $self->error({
121 message => "Filter is not a code reference!",
122 class => 'HTML::Object::TypeError',
123 }) );
124 }
125 40 my $node;
126 40 my $tmpPos = $self->{_pos} + 1;
127 40 while(1)
128 {
129 # We reached the end of the array
130 145 ▶ last if( $tmpPos > $size );
Branch: 1 branch
BranchTrueFalseTotal
if $tmpPos > $size 5 140 145
131 140 my $tmpNode = $elems->index( $tmpPos );
132 140 my $type = $tmpNode->nodeType;
133 140 ▶ $tmpPos++, next if( !$self->_check_element( $tmpNode ) );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_check_element($tmpNode) 71 69 140
134 # This is for the pos() method
135 69 $self->{_relative_pos} = $tmpNode->parent->children->pos( $tmpNode );
136 69 local $_ = $tmpNode;
137 69 my $rv = $filter->( $tmpNode );
138 # Filter should return FILTER_ACCEPT or FILTER_REJECT or FILTER_SKIP
139 69 ▶ $tmpPos++, next if( !defined( $rv ) || $rv == FILTER_REJECT || $rv == FILTER_SKIP );
Branch: 1 branch
BranchTrueFalseTotal
if not defined $rv or $rv == 2 or $rv == 3 34 35 69
Condition50%3/6
not defined $rv or $rv == 2 or $rv == 3
l!l&&r!l&&!r
03435
not defined $rv or $rv == 2
l!l&&r!l&&!r
0069
MC/DC33%1/3
not defined $rv or $rv == 2 or $rv == 3
not defined $rv $rv == 2 $rv == 3
Truth table50%2/4
not(A) or B or C
A= not defined $rvB= $rv == 2C= $rv == 3
ABCresult
1XX1
01X1
0011
0000
140 35 $node = $tmpNode;
141 35 $self->{_pos} = $tmpPos;
142 35 last;
143 }
144 # Return the node to our caller
145 40 return( $node );
146 }
147  
148 0 ▶sub pos { return( shift->{_relative_pos} ); }
Subroutine pos: not called CC=1 SCAR=6.9
Pod documented
149  
150 sub previousNode
151 {
152 16 ▶ my $self = shift( @_ );
Subroutine previousNode: called CC=9 SCAR=22.7
Pod documented
153 16 my $elems = $self->_elements;
154 # Would be -1 if empty
155 16 my $size = $elems->size;
156 # Already at the beginning
157 16 ▶ return if( $self->{_pos} <= 0 );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_pos"} <= 0 2 14 16
158 14 my $whattoshow = $self->whatToShow;
159 14 ▶ return( $self->error( "Somehow the bitwise value of what to show is not an integer!" ) ) if( !$self->_is_integer( $whattoshow ) );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_integer($whattoshow) 0 14 14
160 14 my $filter = $self->filter;
161 14 my $class = ref( $self );
162 # Somehow it has been changed maybe? End our iteration
163 14 ▶ if( ref( $filter ) ne 'CODE' )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $filter ne "CODE") 0 14 14
164 {
165 0 $self->{_pos} = $size;
166 0 return( $self->error({
167 message => "Filter is not a code reference!",
168 class => 'HTML::Object::TypeError',
169 }) );
170 }
171 14 my $node;
172 14 my $tmpPos = $self->{_pos} - 1;
173 14 while(1)
174 {
175 # We reached the start of the array
176 44 ▶ last if( $tmpPos < 0 );
Branch: 1 branch
BranchTrueFalseTotal
if $tmpPos < 0 0 44 44
177 44 my $tmpNode = $elems->index( $tmpPos );
178 44 my $type = $tmpNode->nodeType;
179 44 ▶ $tmpPos--, next if( !$self->_check_element( $tmpNode ) );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_check_element($tmpNode) 0 44 44
180 # This is for the pos() method
181 44 $self->{_relative_pos} = $tmpNode->parent->children->pos( $tmpNode );
182 44 local $_ = $tmpNode;
183 44 my $rv = $filter->( $tmpNode );
184 # Filter should return FILTER_ACCEPT or FILTER_REJECT or FILTER_SKIP
185 44 ▶ $tmpPos--, next if( !defined( $rv ) || $rv == FILTER_REJECT || $rv == FILTER_SKIP );
Branch: 1 branch
BranchTrueFalseTotal
if not defined $rv or $rv == 2 or $rv == 3 30 14 44
Condition50%3/6
not defined $rv or $rv == 2 or $rv == 3
l!l&&r!l&&!r
03014
not defined $rv or $rv == 2
l!l&&r!l&&!r
0044
MC/DC33%1/3
not defined $rv or $rv == 2 or $rv == 3
not defined $rv $rv == 2 $rv == 3
Truth table50%2/4
not(A) or B or C
A= not defined $rvB= $rv == 2C= $rv == 3
ABCresult
1XX1
01X1
0011
0000
186 14 $node = $tmpNode;
187 # Decrement the position for the next turn
188 14 $self->{_pos} = $tmpPos;
189 14 last;
190 }
191 # Return the node to our caller
192 14 return( $node );
193 }
194  
195 # Note: property root read-only
196 25 ▶sub root : lvalue { return( shift->_set_get_object_lvalue( 'root', 'HTML::Object::DOM::Node', @_ ) ); }
Subroutine root: called CC=1 SCAR=0.0
Pod documented
197  
198 # Note: property whatToShow read-only
199 258 ▶sub whatToShow : lvalue { return( shift->_set_get_number( 'whattoshow', @_ ) ); }
Subroutine whatToShow: called CC=1 SCAR=0.0
Pod documented
200  
201 sub _check_element
202 {
203 191 ▶ my $self = shift( @_ );
Subroutine _check_element: called CC=25 SCAR=37.3
204 191 ▶ my $node = shift( @_ ) || return;
Condition50%1/2
shift @_ || (return)
l!l
1910
MC/DC0%0/1
shift @_ || (return)
shift @_
Truth table50%1/2
A
A= shift @_
Aresult
11
00
205 191 my $type = $node->nodeType;
206 191 my $whattoshow = $self->whatToShow;
207 191 ▶ unless( $whattoshow == SHOW_ALL )
Branch: 1 branch
BranchTrueFalseTotal
unless ($whattoshow == 4294967295) 98 93 191
208 {
209 98 ▶ if( ( $type == ELEMENT_NODE && !( $whattoshow & SHOW_ELEMENT ) ) ||
Branch: 1 branch
BranchTrueFalseTotal
if ($type == 1 and not $whattoshow & 1 or $type == 2 and not $whattoshow & 2 or $type == 3 and not $whattoshow & 4 or $type == 4 and not $whattoshow & 8 or $type == 7 and not $whattoshow & 64 or $type == 8 and not $whattoshow & 128 or $type == 9 and not $whattoshow & 256 or $type == 10 and not $whattoshow & 512 or $type == 11 and not $whattoshow & 1024 or $type == 12 and not $whattoshow & 2048 or $type == 13 and not $whattoshow & 4096) 41 24 65
Condition65%41/63
$type == 1 and not $whattoshow & 1 or $type == 2 and not $whattoshow & 2 or $type == 3 and not $whattoshow & 4 or $type == 4 and not $whattoshow & 8 or $type == 7 and not $whattoshow & 64 or $type == 8 and not $whattoshow & 128 or $type == 9 and not $whattoshow & 256 or $type == 10 and not $whattoshow & 512 or $type == 11 and not $whattoshow & 1024 or $type == 12 and not $whattoshow & 2048 or $type == 13 and not $whattoshow & 4096
l!l&&r!l&&!r
33641
$type == 1 and not $whattoshow & 1 or $type == 2 and not $whattoshow & 2 or $type == 3 and not $whattoshow & 4 or $type == 4 and not $whattoshow & 8 or $type == 7 and not $whattoshow & 64 or $type == 8 and not $whattoshow & 128 or $type == 9 and not $whattoshow & 256 or $type == 10 and not $whattoshow & 512 or $type == 11 and not $whattoshow & 1024 or $type == 12 and not $whattoshow & 2048
l!l&&r!l&&!r
33065
$type == 1 and not $whattoshow & 1 or $type == 2 and not $whattoshow & 2 or $type == 3 and not $whattoshow & 4 or $type == 4 and not $whattoshow & 8 or $type == 7 and not $whattoshow & 64 or $type == 8 and not $whattoshow & 128 or $type == 9 and not $whattoshow & 256 or $type == 10 and not $whattoshow & 512 or $type == 11 and not $whattoshow & 1024
l!l&&r!l&&!r
33065
$type == 1 and not $whattoshow & 1 or $type == 2 and not $whattoshow & 2 or $type == 3 and not $whattoshow & 4 or $type == 4 and not $whattoshow & 8 or $type == 7 and not $whattoshow & 64 or $type == 8 and not $whattoshow & 128 or $type == 9 and not $whattoshow & 256 or $type == 10 and not $whattoshow & 512
l!l&&r!l&&!r
33065
$type == 1 and not $whattoshow & 1 or $type == 2 and not $whattoshow & 2 or $type == 3 and not $whattoshow & 4 or $type == 4 and not $whattoshow & 8 or $type == 7 and not $whattoshow & 64 or $type == 8 and not $whattoshow & 128 or $type == 9 and not $whattoshow & 256
l!l&&r!l&&!r
33065
$type == 1 and not $whattoshow & 1 or $type == 2 and not $whattoshow & 2 or $type == 3 and not $whattoshow & 4 or $type == 4 and not $whattoshow & 8 or $type == 7 and not $whattoshow & 64 or $type == 8 and not $whattoshow & 128
l!l&&r!l&&!r
30365
$type == 1 and not $whattoshow & 1 or $type == 2 and not $whattoshow & 2 or $type == 3 and not $whattoshow & 4 or $type == 4 and not $whattoshow & 8 or $type == 7 and not $whattoshow & 64
l!l&&r!l&&!r
30068
$type == 1 and not $whattoshow & 1 or $type == 2 and not $whattoshow & 2 or $type == 3 and not $whattoshow & 4 or $type == 4 and not $whattoshow & 8
l!l&&r!l&&!r
30068
$type == 1 and not $whattoshow & 1 or $type == 2 and not $whattoshow & 2 or $type == 3 and not $whattoshow & 4
l!l&&r!l&&!r
131768
$type == 1 and not $whattoshow & 1 or $type == 2 and not $whattoshow & 2
l!l&&r!l&&!r
13085
$type == 1 and not $whattoshow & 1
!ll&&!rl&&r
681713
$type == 2 and not $whattoshow & 2
!ll&&!rl&&r
8500
$type == 3 and not $whattoshow & 4
!ll&&!rl&&r
62617
$type == 4 and not $whattoshow & 8
!ll&&!rl&&r
6800
$type == 7 and not $whattoshow & 64
!ll&&!rl&&r
6800
$type == 8 and not $whattoshow & 128
!ll&&!rl&&r
6413
$type == 9 and not $whattoshow & 256
!ll&&!rl&&r
6500
$type == 10 and not $whattoshow & 512
!ll&&!rl&&r
6500
$type == 11 and not $whattoshow & 1024
!ll&&!rl&&r
6500
$type == 12 and not $whattoshow & 2048
!ll&&!rl&&r
6500
$type == 13 and not $whattoshow & 4096
!ll&&!rl&&r
24041
MC/DC0%0/22
$type == 1 and not $whattoshow & 1 or $type == 2 and not $whattoshow & 2 or $type == 3 and not $whattoshow & 4 or $type == 4 and not $whattoshow & 8 or $type == 7 and not $whattoshow & 64 or $type == 8 and not $whattoshow & 128 or $type == 9 and not $whattoshow & 256 or $type == 10 and not $whattoshow & 512 or $type == 11 and not $whattoshow & 1024 or $type == 12 and not $whattoshow & 2048 or $type == 13 and not $whattoshow & 4096
too many conditions
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 74 return(0);
224 }
225 }
226 117 return(1);
227 }
228  
229 88 ▶sub _elements { return( shift->_set_get_array_as_object( '_elements', @_ ) ); }
Subroutine _elements: called CC=1 SCAR=0.0
230  
231 sub _flatten
232 {
233 10 ▶ my $self = shift( @_ );
Subroutine _flatten: called CC=2 SCAR=7.0
234 10 my $root = $self->root;
235 # Should not happen
236 10 ▶ return( $self->error( "root element is gone!" ) ) if( !defined( $root ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $root 0 10 10
237 10 my $elems = $self->new_array;
238 10 my $seen = {};
239 10 my $crawl;
240 $crawl = sub
241 {
242 250 ▶ my $e = shift( @_ );
Subroutine __ANON__: called CC=4 SCAR=14.0
243 250 my $addr = $self->_refaddr( $e );
244 250 ▶ return if( ++$seen->{ $addr } > 1 );
Branch: 1 branch
BranchTrueFalseTotal
if ++$seen->{$addr} > 1 0 250 250
245 250 my $kids = $e->children;
246 250 foreach my $kid ( @$kids )
247 {
248 # Junk somehow although it should not happen
249 240 ▶ next if( !$self->_is_a( $kid => 'HTML::Object::DOM::Node' ) );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_a($kid, "HTML::Object::DOM::Node") 0 240 240
250 240 $elems->push( $kid );
251 # Drill down...
252 240 $crawl->( $kid );
253 }
254 10 };
255 10 $elems->push( $root );
256 10 $crawl->( $root );
257 10 return( $elems );
258 }
259  
260 0 ▶sub _parent { return( shift->_set_get_object_without_init( '_parent', 'HTML::Object::DOM::Node', @_ ) ); }
Subroutine _parent: not called CC=1 SCAR=6.9
261  
262 3 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
« lib/HTML/Object/DOM/Document.pm ↑ Summary lib/HTML/Object/DOM/VideoTrackList.pm »