| 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 | use strict; | ||
| 18 | use warnings; | ||
| 19 | warnings::register_categories( 'HTML::Object' ); | ||
| 20 | use parent qw( HTML::Object::DOM::NodeIteratorShared ); | ||
| 21 | use vars qw( $VERSION ); | ||
| 22 | # To import its constants | ||
| 23 | use HTML::Object::DOM::Node; | ||
| 24 | use HTML::Object::DOM::NodeFilter qw( :all ); | ||
| 25 | our $VERSION = 'v0.2.0'; | ||
| 26 | }; | ||
| 27 | |||
| 28 | use strict; | ||
| 29 | use warnings; | ||
| 30 | |||
| 31 | # Note: method init is inherited | ||
| 32 | |||
| 33 | # Note: property currentNode | ||
| 34 | sub currentNode | ||
| 35 | { | ||
| 36 | my $self = shift( @_ ); | ||
| 37 | my $elems = $self->_elements; | ||
| 38 | 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 | my $self = shift( @_ ); | ||
| 48 | my $elems = $self->_elements; | ||
| 49 | my $this = $elems->index( $self->{_pos} ); | ||
| 50 | return( $self->error( "Unable to find the current node at position '$self->{_pos}' in our tree array!" ) ) if( !defined( $this ) ); | ||
| 51 | # Only elements can have children | ||
| 52 | return if( $this->nodeType != ELEMENT_NODE ); | ||
| 53 | my $node; | ||
| 54 | my $tmpPos = 0; | ||
| 55 | my $children = $this->children; | ||
| 56 | my $size = $children->size; | ||
| 57 | # We seek the first appropriate first child depending on the value of $whatToShow | ||
| 58 | while(1) | ||
| 59 | { | ||
| 60 | last if( $tmpPos > $size ); | ||
| 61 | my $tmpNode = $children->index( $tmpPos ); | ||
| 62 | my $rv = $self->_check_element( $tmpNode ); | ||
| 63 | last if( !defined( $rv ) ); | ||
| 64 | $tmpPos++, next if( !$rv ); | ||
| 65 | $node = $tmpNode; | ||
| 66 | last; | ||
| 67 | } | ||
| 68 | return if( !defined( $node ) ); | ||
| 69 | 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 | return if( !defined( $pos ) ); | ||
| 72 | $self->{_pos} = $pos; | ||
| 73 | return( $node ); | ||
| 74 | } | ||
| 75 | |||
| 76 | sub lastChild | ||
| 77 | { | ||
| 78 | my $self = shift( @_ ); | ||
| 79 | my $elems = $self->_elements; | ||
| 80 | my $this = $elems->index( $self->{_pos} ); | ||
| 81 | return( $self->error( "Unable to find the current node at position '$self->{_pos}' in our tree array!" ) ) if( !defined( $this ) ); | ||
| 82 | # Only elements can have children | ||
| 83 | return if( $this->nodeType != ELEMENT_NODE ); | ||
| 84 | my $node; | ||
| 85 | my $children = $this->children; | ||
| 86 | my $tmpPos = $children->size; | ||
| 87 | # We seek the first appropriate first child depending on the value of $whatToShow | ||
| 88 | while(1) | ||
| 89 | { | ||
| 90 | last if( $tmpPos < 0 ); | ||
| 91 | my $tmpNode = $children->index( $tmpPos ); | ||
| 92 | my $rv = $self->_check_element( $tmpNode ); | ||
| 93 | last if( !defined( $rv ) ); | ||
| 94 | $tmpPos--, next if( !$rv ); | ||
| 95 | $node = $tmpNode; | ||
| 96 | last; | ||
| 97 | } | ||
| 98 | return if( !defined( $node ) ); | ||
| 99 | 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 | return if( !defined( $pos ) ); | ||
| 102 | $self->{_pos} = $pos; | ||
| 103 | return( $node ); | ||
| 104 | } | ||
| 105 | |||
| 106 | # Note: method nextNode is inherited | ||
| 107 | |||
| 108 | sub nextSibling | ||
| 109 | { | ||
| 110 | my $self = shift( @_ ); | ||
| 111 | my $elems = $self->_elements; | ||
| 112 | my $this = $elems->index( $self->{_pos} ); | ||
| 113 | return( $self->error( "Unable to find the current node at position '$self->{_pos}' in our tree array!" ) ) if( !defined( $this ) ); | ||
| 114 | # No need to bother if our current node is the root node. Its siblings are not part of the tree | ||
| 115 | return if( $this eq $self->root ); | ||
| 116 | # Get all next siblings | ||
| 117 | my $node; | ||
| 118 | my $siblings = $this->right; | ||
| 119 | my $size = $siblings->size; | ||
| 120 | my $tmpPos = 0; | ||
| 121 | # We seek the first appropriate sibling depending on the value of $whatToShow | ||
| 122 | while(1) | ||
| 123 | { | ||
| 124 | last if( $tmpPos > $size ); | ||
| 125 | my $tmpNode = $siblings->index( $tmpPos ); | ||
| 126 | my $rv = $self->_check_element( $tmpNode ); | ||
| 127 | last if( !defined( $rv ) ); | ||
| 128 | $tmpPos++, next if( !$rv ); | ||
| 129 | $node = $tmpNode; | ||
| 130 | last; | ||
| 131 | } | ||
| 132 | return if( !defined( $node ) ); | ||
| 133 | 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 | return if( !defined( $pos ) ); | ||
| 136 | $self->{_pos} = $pos; | ||
| 137 | return( $node ); | ||
| 138 | } | ||
| 139 | |||
| 140 | sub parentNode | ||
| 141 | { | ||
| 142 | my $self = shift( @_ ); | ||
| 143 | my $elems = $self->_elements; | ||
| 144 | my $this = $elems->index( $self->{_pos} ); | ||
| 145 | return( $self->error( "Unable to find the current node at position '$self->{_pos}' in our tree array!" ) ) if( !defined( $this ) ); | ||
| 146 | my $root = $self->root; | ||
| 147 | # We should not be here in the first place. | ||
| 148 | return if( $this eq $root ); | ||
| 149 | my $node = $this->parent; | ||
| 150 | return if( !defined( $node ) ); | ||
| 151 | # We cannot go up to the root, but only within the root itself. | ||
| 152 | # return if( $node eq $root ); | ||
| 153 | 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 | return if( !defined( $pos ) ); | ||
| 156 | $self->{_pos} = $pos; | ||
| 157 | return( $node ); | ||
| 158 | } | ||
| 159 | |||
| 160 | # Note: method previousNode is inherited | ||
| 161 | |||
| 162 | sub previousSibling | ||
| 163 | { | ||
| 164 | my $self = shift( @_ ); | ||
| 165 | my $elems = $self->_elements; | ||
| 166 | my $this = $elems->index( $self->{_pos} ); | ||
| 167 | return( $self->error( "Unable to find the current node at position '$self->{_pos}' in our tree array!" ) ) if( !defined( $this ) ); | ||
| 168 | # No need to bother if our current node is the root node. Its siblings are not part of the tree | ||
| 169 | return if( $this eq $self->root ); | ||
| 170 | # Get all previous siblings | ||
| 171 | my $node; | ||
| 172 | my $siblings = $this->left; | ||
| 173 | my $tmpPos = $siblings->size; | ||
| 174 | # We seek the first appropriate sibling depending on the value of $whatToShow | ||
| 175 | while(1) | ||
| 176 | { | ||
| 177 | last if( $tmpPos < 0 ); | ||
| 178 | my $tmpNode = $siblings->index( $tmpPos ); | ||
| 179 | my $rv = $self->_check_element( $tmpNode ); | ||
| 180 | last if( !defined( $rv ) ); | ||
| 181 | $tmpPos--, next if( !$rv ); | ||
| 182 | $node = $tmpNode; | ||
| 183 | last; | ||
| 184 | } | ||
| 185 | return if( !defined( $node ) ); | ||
| 186 | 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 | return if( !defined( $pos ) ); | ||
| 189 | $self->{_pos} = $pos; | ||
| 190 | return( $node ); | ||
| 191 | } | ||
| 192 | |||
| 193 | # Note: property root read-only is inherited | ||
| 194 | |||
| 195 | # Note: property whatToShow read-only is inherited | ||
| 196 | |||
| 197 | 1; | ||
| 198 | # NOTE: POD | ||
| 199 | __END__ |