| 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 | { | ||||||||||
| 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' ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 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 | { | ||||||||||
| 36 | 8 | ▶ | my $self = shift( @_ ); | ||||||||
|
Subroutine
currentNode: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 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 | { | ||||||||||
| 47 | 3 | ▶ | my $self = shift( @_ ); | ||||||||
|
Subroutine
firstChild: called CC=8 SCAR=21.2
Pod
documented
| |||||||||||
| 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
| |||||||||||
| 51 | # Only elements can have children | ||||||||||
| 52 | 3 | ▶ | return if( $this->nodeType != ELEMENT_NODE ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 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
| |||||||||||
| 61 | 5 | my $tmpNode = $children->index( $tmpPos ); | |||||||||
| 62 | 5 | my $rv = $self->_check_element( $tmpNode ); | |||||||||
| 63 | 5 | ▶ | last if( !defined( $rv ) ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 64 | 5 | ▶ | $tmpPos++, next if( !$rv ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 65 | 3 | $node = $tmpNode; | |||||||||
| 66 | 3 | last; | |||||||||
| 67 | } | ||||||||||
| 68 | 3 | ▶ | return if( !defined( $node ) ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 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
| |||||||||||
| 72 | 3 | $self->{_pos} = $pos; | |||||||||
| 73 | 3 | return( $node ); | |||||||||
| 74 | } | ||||||||||
| 75 | |||||||||||
| 76 | sub lastChild | ||||||||||
| 77 | { | ||||||||||
| 78 | 1 | ▶ | my $self = shift( @_ ); | ||||||||
|
Subroutine
lastChild: called CC=8 SCAR=21.2
Pod
documented
| |||||||||||
| 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
| |||||||||||
| 82 | # Only elements can have children | ||||||||||
| 83 | 1 | ▶ | return if( $this->nodeType != ELEMENT_NODE ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 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
| |||||||||||
| 91 | 2 | my $tmpNode = $children->index( $tmpPos ); | |||||||||
| 92 | 2 | my $rv = $self->_check_element( $tmpNode ); | |||||||||
| 93 | 2 | ▶ | last if( !defined( $rv ) ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 94 | 2 | ▶ | $tmpPos--, next if( !$rv ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 95 | 1 | $node = $tmpNode; | |||||||||
| 96 | 1 | last; | |||||||||
| 97 | } | ||||||||||
| 98 | 1 | ▶ | return if( !defined( $node ) ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 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
| |||||||||||
| 102 | 1 | $self->{_pos} = $pos; | |||||||||
| 103 | 1 | return( $node ); | |||||||||
| 104 | } | ||||||||||
| 105 | |||||||||||
| 106 | # Note: method nextNode is inherited | ||||||||||
| 107 | |||||||||||
| 108 | sub nextSibling | ||||||||||
| 109 | { | ||||||||||
| 110 | 1 | ▶ | my $self = shift( @_ ); | ||||||||
|
Subroutine
nextSibling: called CC=8 SCAR=37.2
Pod
documented
| |||||||||||
| 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
| |||||||||||
| 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
| |||||||||||
| 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
| |||||||||||
| 125 | 0 | my $tmpNode = $siblings->index( $tmpPos ); | |||||||||
| 126 | 0 | my $rv = $self->_check_element( $tmpNode ); | |||||||||
| 127 | 0 | ▶ | last if( !defined( $rv ) ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 128 | 0 | ▶ | $tmpPos++, next if( !$rv ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 129 | 0 | $node = $tmpNode; | |||||||||
| 130 | 0 | last; | |||||||||
| 131 | } | ||||||||||
| 132 | 0 | ▶ | return if( !defined( $node ) ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 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
| |||||||||||
| 136 | 0 | $self->{_pos} = $pos; | |||||||||
| 137 | 0 | return( $node ); | |||||||||
| 138 | } | ||||||||||
| 139 | |||||||||||
| 140 | sub parentNode | ||||||||||
| 141 | { | ||||||||||
| 142 | 1 | ▶ | my $self = shift( @_ ); | ||||||||
|
Subroutine
parentNode: called CC=5 SCAR=16.5
Pod
documented
| |||||||||||
| 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
| |||||||||||
| 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
| |||||||||||
| 149 | 1 | my $node = $this->parent; | |||||||||
| 150 | 1 | ▶ | return if( !defined( $node ) ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 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
| |||||||||||
| 156 | 1 | $self->{_pos} = $pos; | |||||||||
| 157 | 1 | return( $node ); | |||||||||
| 158 | } | ||||||||||
| 159 | |||||||||||
| 160 | # Note: method previousNode is inherited | ||||||||||
| 161 | |||||||||||
| 162 | sub previousSibling | ||||||||||
| 163 | { | ||||||||||
| 164 | 1 | ▶ | my $self = shift( @_ ); | ||||||||
|
Subroutine
previousSibling: called CC=8 SCAR=37.1
Pod
documented
| |||||||||||
| 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
| |||||||||||
| 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
| |||||||||||
| 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
| |||||||||||
| 178 | 0 | my $tmpNode = $siblings->index( $tmpPos ); | |||||||||
| 179 | 0 | my $rv = $self->_check_element( $tmpNode ); | |||||||||
| 180 | 0 | ▶ | last if( !defined( $rv ) ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 181 | 0 | ▶ | $tmpPos--, next if( !$rv ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 182 | 0 | $node = $tmpNode; | |||||||||
| 183 | 0 | last; | |||||||||
| 184 | } | ||||||||||
| 185 | 0 | ▶ | return if( !defined( $node ) ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 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
| |||||||||||
| 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__ | ||||||||||