Object/DOM/TreeWalker.pm

filter: Statementstmt 74.2% 95 / 128 Branchbran 37.5% 24 / 64 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 15 / 15 Podpod 100% 6 / 6 totaltotal 65.7% 140 / 213 CC 33 SCAR 28.7
CC
33
Cov
62%
nextSibling
41.5
previousSibling
40.8
lastChild
8.3
CRAP 17.6
« Object/DOM/VideoTrackList.pm ↑ Summary Object/DOM/Collection.pm »
1 ##----------------------------------------------------------------------------
2 ## HTML Object - ~/lib/HTML/Object/DOM/TreeWalker.pm
3 ## Version v0.2.0
4 ## Copyright(c) 2022 DEGUEST Pte. Ltd.
5 ## Author: Jacques Deguest <jack@deguest.jp>
6 ## Created 2022/01/02
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::TreeWalker;
15 BEGIN
16 2 ▶{
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 2 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 2 warnings::register_categories( 'HTML::Object' );
20 2 ▶ use parent qw( HTML::Object::DOM::NodeIteratorShared );
Subroutine BEGIN: called CC=1 SCAR=0.0
21 2 ▶ use vars qw( $VERSION );
Subroutine BEGIN: called CC=1 SCAR=0.0
22 # To import its constants
23 2 ▶ use HTML::Object::DOM::Node;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 2 ▶ use HTML::Object::DOM::NodeFilter qw( :all );
Subroutine BEGIN: called CC=1 SCAR=0.0
25 2 our $VERSION = 'v0.2.0';
26 };
27  
28 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 # Note: method init is inherited
32  
33 # Note: property currentNode
34 sub currentNode
35 8 ▶{
Subroutine currentNode: called CC=1 SCAR=0.0
Pod documented
36 8 my $self = shift( @_ );
37 8 my $elems = $self->_elements;
38 8 return( $elems->index( $self->{_pos} ) );
39 }
40  
41 # Note: property expandEntityReferences read-only is inherited
42  
43 # Note: property filter read-only is inherited
44  
45 sub firstChild
46 3 ▶{
Subroutine firstChild: called CC=8 SCAR=21.2
Pod documented
47 3 my $self = shift( @_ );
48 3 my $elems = $self->_elements;
49 3 my $this = $elems->index( $self->{_pos} );
50 3 ▶ return( $self->error( "Unable to find the current node at position '$self->{_pos}' in our tree array!" ) ) if( !defined( $this ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $this 0 3 3
51 # Only elements can have children
52 3 ▶ return if( $this->nodeType != ELEMENT_NODE );
Branch: 1 branch
BranchTrueFalseTotal
if $this->nodeType != 1 0 3 3
53 3 my $node;
54 3 my $tmpPos = 0;
55 3 my $children = $this->children;
56 3 my $size = $children->size;
57 # We seek the first appropriate first child depending on the value of $whatToShow
58 3 while(1)
59 {
60 5 ▶ last if( $tmpPos > $size );
Branch: 1 branch
BranchTrueFalseTotal
if $tmpPos > $size 0 5 5
61 5 my $tmpNode = $children->index( $tmpPos );
62 5 my $rv = $self->_check_element( $tmpNode );
63 5 ▶ last if( !defined( $rv ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rv 0 5 5
64 5 ▶ $tmpPos++, next if( !$rv );
Branch: 1 branch
BranchTrueFalseTotal
unless $rv 2 3 5
65 3 $node = $tmpNode;
66 3 last;
67 }
68 3 ▶ return if( !defined( $node ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $node 0 3 3
69 3 my $pos = $elems->pos( $node );
70 # Amazingly enough, the first child of this node cannot be found among the list of all nodes in this tree!
71 3 ▶ return if( !defined( $pos ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pos 0 3 3
72 3 $self->{_pos} = $pos;
73 3 return( $node );
74 }
75  
76 sub lastChild
77 1 ▶{
Subroutine lastChild: called CC=8 SCAR=21.2
Pod documented
78 1 my $self = shift( @_ );
79 1 my $elems = $self->_elements;
80 1 my $this = $elems->index( $self->{_pos} );
81 1 ▶ return( $self->error( "Unable to find the current node at position '$self->{_pos}' in our tree array!" ) ) if( !defined( $this ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $this 0 1 1
82 # Only elements can have children
83 1 ▶ return if( $this->nodeType != ELEMENT_NODE );
Branch: 1 branch
BranchTrueFalseTotal
if $this->nodeType != 1 0 1 1
84 1 my $node;
85 1 my $children = $this->children;
86 1 my $tmpPos = $children->size;
87 # We seek the first appropriate first child depending on the value of $whatToShow
88 1 while(1)
89 {
90 2 ▶ last if( $tmpPos < 0 );
Branch: 1 branch
BranchTrueFalseTotal
if $tmpPos < 0 0 2 2
91 2 my $tmpNode = $children->index( $tmpPos );
92 2 my $rv = $self->_check_element( $tmpNode );
93 2 ▶ last if( !defined( $rv ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rv 0 2 2
94 2 ▶ $tmpPos--, next if( !$rv );
Branch: 1 branch
BranchTrueFalseTotal
unless $rv 1 1 2
95 1 $node = $tmpNode;
96 1 last;
97 }
98 1 ▶ return if( !defined( $node ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $node 0 1 1
99 1 my $pos = $elems->pos( $node );
100 # Amazingly enough, the last child of this node cannot be found among the list of all nodes in this tree!
101 1 ▶ return if( !defined( $pos ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pos 0 1 1
102 1 $self->{_pos} = $pos;
103 1 return( $node );
104 }
105  
106 # Note: method nextNode is inherited
107  
108 sub nextSibling
109 1 ▶{
Subroutine nextSibling: called CC=8 SCAR=37.2
Pod documented
110 1 my $self = shift( @_ );
111 1 my $elems = $self->_elements;
112 1 my $this = $elems->index( $self->{_pos} );
113 1 ▶ return( $self->error( "Unable to find the current node at position '$self->{_pos}' in our tree array!" ) ) if( !defined( $this ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $this 0 1 1
114 # No need to bother if our current node is the root node. Its siblings are not part of the tree
115 1 ▶ return if( $this eq $self->root );
Branch: 1 branch
BranchTrueFalseTotal
if $this eq $self->root 1 0 1
116 # Get all next siblings
117 0 my $node;
118 0 my $siblings = $this->right;
119 0 my $size = $siblings->size;
120 0 my $tmpPos = 0;
121 # We seek the first appropriate sibling depending on the value of $whatToShow
122 0 while(1)
123 {
124 0 ▶ last if( $tmpPos > $size );
Branch: 1 branch
BranchTrueFalseTotal
if $tmpPos > $size 0 0 0
125 0 my $tmpNode = $siblings->index( $tmpPos );
126 0 my $rv = $self->_check_element( $tmpNode );
127 0 ▶ last if( !defined( $rv ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rv 0 0 0
128 0 ▶ $tmpPos++, next if( !$rv );
Branch: 1 branch
BranchTrueFalseTotal
unless $rv 0 0 0
129 0 $node = $tmpNode;
130 0 last;
131 }
132 0 ▶ return if( !defined( $node ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $node 0 0 0
133 0 my $pos = $elems->pos( $node );
134 # Amazingly enough, the next sibling of this node cannot be found among the list of all nodes in this tree!
135 0 ▶ return if( !defined( $pos ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pos 0 0 0
136 0 $self->{_pos} = $pos;
137 0 return( $node );
138 }
139  
140 sub parentNode
141 1 ▶{
Subroutine parentNode: called CC=5 SCAR=16.5
Pod documented
142 1 my $self = shift( @_ );
143 1 my $elems = $self->_elements;
144 1 my $this = $elems->index( $self->{_pos} );
145 1 ▶ return( $self->error( "Unable to find the current node at position '$self->{_pos}' in our tree array!" ) ) if( !defined( $this ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $this 0 1 1
146 1 my $root = $self->root;
147 # We should not be here in the first place.
148 1 ▶ return if( $this eq $root );
Branch: 1 branch
BranchTrueFalseTotal
if $this eq $root 0 1 1
149 1 my $node = $this->parent;
150 1 ▶ return if( !defined( $node ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $node 0 1 1
151 # We cannot go up to the root, but only within the root itself.
152 # return if( $node eq $root );
153 1 my $pos = $elems->pos( $node );
154 # Amazingly enough, the last child of this node cannot be found among the list of all nodes in this tree!
155 1 ▶ return if( !defined( $pos ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pos 0 1 1
156 1 $self->{_pos} = $pos;
157 1 return( $node );
158 }
159  
160 # Note: method previousNode is inherited
161  
162 sub previousSibling
163 1 ▶{
Subroutine previousSibling: called CC=8 SCAR=37.1
Pod documented
164 1 my $self = shift( @_ );
165 1 my $elems = $self->_elements;
166 1 my $this = $elems->index( $self->{_pos} );
167 1 ▶ return( $self->error( "Unable to find the current node at position '$self->{_pos}' in our tree array!" ) ) if( !defined( $this ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $this 0 1 1
168 # No need to bother if our current node is the root node. Its siblings are not part of the tree
169 1 ▶ return if( $this eq $self->root );
Branch: 1 branch
BranchTrueFalseTotal
if $this eq $self->root 1 0 1
170 # Get all previous siblings
171 0 my $node;
172 0 my $siblings = $this->left;
173 0 my $tmpPos = $siblings->size;
174 # We seek the first appropriate sibling depending on the value of $whatToShow
175 0 while(1)
176 {
177 0 ▶ last if( $tmpPos < 0 );
Branch: 1 branch
BranchTrueFalseTotal
if $tmpPos < 0 0 0 0
178 0 my $tmpNode = $siblings->index( $tmpPos );
179 0 my $rv = $self->_check_element( $tmpNode );
180 0 ▶ last if( !defined( $rv ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rv 0 0 0
181 0 ▶ $tmpPos--, next if( !$rv );
Branch: 1 branch
BranchTrueFalseTotal
unless $rv 0 0 0
182 0 $node = $tmpNode;
183 0 last;
184 }
185 0 ▶ return if( !defined( $node ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $node 0 0 0
186 0 my $pos = $elems->pos( $node );
187 # Amazingly enough, the previous sibling of this node cannot be found among the list of all nodes in this tree!
188 0 ▶ return if( !defined( $pos ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pos 0 0 0
189 0 $self->{_pos} = $pos;
190 0 return( $node );
191 }
192  
193 # Note: property root read-only is inherited
194  
195 # Note: property whatToShow read-only is inherited
196  
197 2 1;
198 # NOTE: POD
199 __END__
« Object/DOM/VideoTrackList.pm ↑ Summary Object/DOM/Collection.pm »