| 1 | ##---------------------------------------------------------------------------- | ||
| 2 | ## HTML Object - ~/lib/HTML/Object/XPath/NodeSet.pm | ||
| 3 | ## Version v0.2.0 | ||
| 4 | ## Copyright(c) 2021 DEGUEST Pte. Ltd. | ||
| 5 | ## Author: Jacques Deguest <jack@deguest.jp> | ||
| 6 | ## Created 2021/12/05 | ||
| 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::XPath::NodeSet; | ||
| 15 | BEGIN | ||
| 16 | { | ||
| 17 | use strict; | ||
| 18 | use warnings; | ||
| 19 | warnings::register_categories( 'HTML::Object' ); | ||
| 20 | # use parent qw( Module::Generic ); | ||
| 21 | use parent qw( Module::Generic::Array ); | ||
| 22 | use vars qw( $TRUE $FALSE $BASE_CLASS $DEBUG $VERSION ); | ||
| 23 | use HTML::Object::XPath::Boolean; | ||
| 24 | use overload ( | ||
| 25 | '""' => \&to_literal, | ||
| 26 | 'bool' => \&to_boolean, | ||
| 27 | ); | ||
| 28 | our $TRUE = HTML::Object::XPath::Boolean->True; | ||
| 29 | our $FALSE = HTML::Object::XPath::Boolean->False; | ||
| 30 | our $BASE_CLASS = 'HTML::Object::XPath'; | ||
| 31 | our $DEBUG = 0; | ||
| 32 | our $VERSION = 'v0.2.0'; | ||
| 33 | }; | ||
| 34 | |||
| 35 | use strict; | ||
| 36 | use warnings; | ||
| 37 | |||
| 38 | sub new | ||
| 39 | { | ||
| 40 | my $this = CORE::shift( @_ ); | ||
| 41 | return( CORE::bless( [] => ( CORE::ref( $this ) || $this ) ) ); | ||
| 42 | } | ||
| 43 | |||
| 44 | sub append | ||
| 45 | { | ||
| 46 | my $self = CORE::shift( @_ ); | ||
| 47 | my( $nodeset ) = @_; | ||
| 48 | return( CORE::push( @$self, $nodeset->get_nodelist ) ); | ||
| 49 | } | ||
| 50 | |||
| 51 | # uses array index starting at 1, not 0 | ||
| 52 | sub get_node | ||
| 53 | { | ||
| 54 | my $self = CORE::shift( @_ ); | ||
| 55 | my( $pos ) = @_; | ||
| 56 | return( $self->[ $pos - 1 ] ); | ||
| 57 | } | ||
| 58 | |||
| 59 | sub get_nodelist { return( @{$_[0]} ); } | ||
| 60 | |||
| 61 | sub getChildNodes | ||
| 62 | { | ||
| 63 | my $self = CORE::shift( @_ ); | ||
| 64 | return( CORE::map{ $_->getChildNodes } @$self ); | ||
| 65 | } | ||
| 66 | |||
| 67 | sub getElementById | ||
| 68 | { | ||
| 69 | my $self = CORE::shift( @_ ); | ||
| 70 | return( CORE::map{ $_->getElementById } @$self ); | ||
| 71 | } | ||
| 72 | |||
| 73 | sub getRootNode | ||
| 74 | { | ||
| 75 | my $self = CORE::shift( @_ ); | ||
| 76 | return( $self->[0]->getRootNode ); | ||
| 77 | } | ||
| 78 | |||
| 79 | sub new_literal { return( CORE::shift->_class_for( 'Literal' )->new( @_ ) ); } | ||
| 80 | |||
| 81 | sub new_number { return( CORE::shift->_class_for( 'Number' )->new( @_ ) ); } | ||
| 82 | |||
| 83 | sub pop | ||
| 84 | { | ||
| 85 | my $self = CORE::shift( @_ ); | ||
| 86 | return( CORE::pop( @$self ) ); | ||
| 87 | } | ||
| 88 | |||
| 89 | sub prepend | ||
| 90 | { | ||
| 91 | my $self = CORE::shift( @_ ); | ||
| 92 | my( $nodeset ) = @_; | ||
| 93 | return( CORE::unshift( @$self, $nodeset->get_nodelist ) ); | ||
| 94 | } | ||
| 95 | |||
| 96 | sub push | ||
| 97 | { | ||
| 98 | my $self = CORE::shift( @_ ); | ||
| 99 | my( @nodes ) = @_; | ||
| 100 | return( CORE::push( @$self, @nodes ) ); | ||
| 101 | } | ||
| 102 | |||
| 103 | sub remove_duplicates | ||
| 104 | { | ||
| 105 | my $self = CORE::shift( @_ ); | ||
| 106 | my @unique; | ||
| 107 | my $last_node = 0; | ||
| 108 | foreach my $node ( @$self ) | ||
| 109 | { | ||
| 110 | CORE::push( @unique, $node ) unless( $node == $last_node ); | ||
| 111 | $last_node = $node; | ||
| 112 | } | ||
| 113 | @$self = @unique; | ||
| 114 | return( $self ); | ||
| 115 | } | ||
| 116 | |||
| 117 | sub reverse | ||
| 118 | { | ||
| 119 | my $self = CORE::shift( @_ ); | ||
| 120 | @$self = CORE::reverse( @$self ); | ||
| 121 | return( $self ); | ||
| 122 | } | ||
| 123 | |||
| 124 | sub shift | ||
| 125 | { | ||
| 126 | my $self = CORE::shift( @_ ); | ||
| 127 | return( CORE::shift( @$self ) ); | ||
| 128 | } | ||
| 129 | |||
| 130 | sub size | ||
| 131 | { | ||
| 132 | my $self = CORE::shift( @_ ); | ||
| 133 | return( CORE::scalar( @$self ) ); | ||
| 134 | } | ||
| 135 | |||
| 136 | sub sort | ||
| 137 | { | ||
| 138 | my $self = CORE::shift( @_ ); | ||
| 139 | @$self = CORE::sort { $a->cmp( $b ) } @$self; | ||
| 140 | return( $self ); | ||
| 141 | } | ||
| 142 | |||
| 143 | sub string_value | ||
| 144 | { | ||
| 145 | my $self = CORE::shift( @_ ); | ||
| 146 | return( '' ) unless( @$self ); | ||
| 147 | return( $self->[0]->string_value ); | ||
| 148 | } | ||
| 149 | |||
| 150 | sub string_values | ||
| 151 | { | ||
| 152 | my $self = CORE::shift( @_ ); | ||
| 153 | return( CORE::map{ $_->string_value } @$self ); | ||
| 154 | } | ||
| 155 | |||
| 156 | sub to_boolean | ||
| 157 | { | ||
| 158 | my $self = CORE::shift( @_ ); | ||
| 159 | return( ( @$self > 0 ) ? $TRUE : $FALSE ); | ||
| 160 | } | ||
| 161 | |||
| 162 | sub to_final_value | ||
| 163 | { | ||
| 164 | my $self = CORE::shift( @_ ); | ||
| 165 | return( CORE::join( '', CORE::map{ $_->string_value } @$self ) ); | ||
| 166 | } | ||
| 167 | |||
| 168 | sub to_literal | ||
| 169 | { | ||
| 170 | my $self = CORE::shift( @_ ); | ||
| 171 | return( $self->new_literal( CORE::join( '', CORE::map{ $_->string_value } @$self ) ) ); | ||
| 172 | } | ||
| 173 | |||
| 174 | sub to_number | ||
| 175 | { | ||
| 176 | my $self = CORE::shift( @_ ); | ||
| 177 | return( $self->new_number( $self->to_literal ) ); | ||
| 178 | } | ||
| 179 | |||
| 180 | sub unshift | ||
| 181 | { | ||
| 182 | my $self = CORE::shift( @_ ); | ||
| 183 | my( @nodes ) = @_; | ||
| 184 | return( CORE::unshift( @$self, @nodes ) ); | ||
| 185 | } | ||
| 186 | |||
| 187 | sub _class_for | ||
| 188 | { | ||
| 189 | my( $self, $mod ) = @_; | ||
| 190 | eval( "require ${BASE_CLASS}\::${mod};" ); | ||
| 191 | die( $@ ) if( $@ ); | ||
| 192 | # ${"${BASE_CLASS}\::${mod}\::DEBUG"} = $DEBUG; | ||
| 193 | eval( "\$${BASE_CLASS}\::${mod}\::DEBUG = " . ( $DEBUG // '' ) ); | ||
| 194 | return( "${BASE_CLASS}::${mod}" ); | ||
| 195 | } | ||
| 196 | |||
| 197 | 1; | ||
| 198 | # NOTE: POD | ||
| 199 | __END__ | ||
| 200 | |||
| 201 | =encoding utf-8 | ||
| 202 | |||
| 203 | =head1 NAME | ||
| 204 | |||
| 205 | HTML::Object::XPath::NodeSet - HTML Object XPath Node Set | ||
| 206 | |||
| 207 | =head1 SYNOPSIS | ||
| 208 | |||
| 209 | use HTML::Object::XPath::NodeSet; | ||
| 210 | my $set = HTML::Object::XPath::NodeSet->new || | ||
| 211 | die( HTML::Object::XPath::NodeSet->error, "\n" ); | ||
| 212 | |||
| 213 | my $results = $xp->find( '//someelement' ); | ||
| 214 | if( !$results->isa( 'HTML::Object::XPath::NodeSet' ) ) | ||
| 215 | { | ||
| 216 | print( "Found $results\n" ); | ||
| 217 | exit; | ||
| 218 | } | ||
| 219 | |||
| 220 | foreach my $context ( $results->get_nodelist ) | ||
| 221 | { | ||
| 222 | my $newresults = $xp->find( './other/element', $context ); | ||
| 223 | # ... | ||
| 224 | } | ||
| 225 | |||
| 226 | =head1 VERSION | ||
| 227 | |||
| 228 | v0.2.0 | ||
| 229 | |||
| 230 | =head1 DESCRIPTION | ||
| 231 | |||
| 232 | This module contains an ordered list of L<nodes|HTML::Object::Element>. The nodes each take the same format as described in L<HTML::Object::XPath>. | ||
| 233 | |||
| 234 | =head1 METHODS | ||
| 235 | |||
| 236 | =head2 new | ||
| 237 | |||
| 238 | You should never have to create a new NodeSet object, as it is all done for you by L<HTML::Object::XPath>. | ||
| 239 | |||
| 240 | =head2 append | ||
| 241 | |||
| 242 | Given a C<nodeset>, this appends the list of nodes in C<nodeset> to the end of the current list. | ||
| 243 | |||
| 244 | =head2 get_node | ||
| 245 | |||
| 246 | Provided with an integer representing a C<position> and this returns the node at C<position>. | ||
| 247 | |||
| 248 | The node position in XPath is based at 1, not 0. | ||
| 249 | |||
| 250 | =head2 get_nodelist | ||
| 251 | |||
| 252 | Returns a list of nodes. See L<HTML::Object::XPath> for the format of the nodes. | ||
| 253 | |||
| 254 | =head2 getChildNodes | ||
| 255 | |||
| 256 | Returns a list of all elements' own child nodes by calling C<getChildNodes> on each one of them. | ||
| 257 | |||
| 258 | =head2 getElementById | ||
| 259 | |||
| 260 | Returns a list of all elements' own id by calling C<getElementById> on each one of them. | ||
| 261 | |||
| 262 | =head2 getRootNode | ||
| 263 | |||
| 264 | Returns the first element's value of C<getRootNode> | ||
| 265 | |||
| 266 | =head2 new_literal | ||
| 267 | |||
| 268 | Returns a new L<HTML::Object::XPath::Literal> object, passing it whatever arguments was provided. | ||
| 269 | |||
| 270 | =head2 new_number | ||
| 271 | |||
| 272 | Returns a new L<HTML::Object::XPath::Number> object, passing it whatever arguments was provided. | ||
| 273 | |||
| 274 | =head2 pop | ||
| 275 | |||
| 276 | Equivalent to perl's L<perlfunc/pop> function. This removes the last element from our stack and returns it. | ||
| 277 | |||
| 278 | =head2 prepend | ||
| 279 | |||
| 280 | Given a C<nodeset>, prepends the list of nodes in C<nodeset> to the front of the current list. | ||
| 281 | |||
| 282 | =head2 push | ||
| 283 | |||
| 284 | Provided with a list of nodes and this will add them to the stack in this object. | ||
| 285 | |||
| 286 | Equivalent to perl's L<perlfunc/push> function. | ||
| 287 | |||
| 288 | =head2 remove_duplicates | ||
| 289 | |||
| 290 | This removes any duplicates there might be in our internal list of elements. | ||
| 291 | |||
| 292 | =head2 reverse | ||
| 293 | |||
| 294 | This method reverse the order of the elements in our internal list of elements. | ||
| 295 | |||
| 296 | =head2 shift | ||
| 297 | |||
| 298 | Equivalent to perl's L<perlfunc/shift> function. | ||
| 299 | |||
| 300 | =head2 size | ||
| 301 | |||
| 302 | Returns the number of nodes in the L<HTML::Object::XPath::NodeSet>. | ||
| 303 | |||
| 304 | =head2 sort | ||
| 305 | |||
| 306 | This method sorts all the elements by comparing them to one another using C<cmp> | ||
| 307 | |||
| 308 | =head2 string_value | ||
| 309 | |||
| 310 | Returns the string-value of the first node in the list. | ||
| 311 | |||
| 312 | See the L<HTML::Object::XPath> specification for what "string-value" means. | ||
| 313 | |||
| 314 | =head2 string_values | ||
| 315 | |||
| 316 | Returns a list of the string-values of all the nodes in the list. | ||
| 317 | |||
| 318 | =head2 to_boolean | ||
| 319 | |||
| 320 | Returns L<true|HTML::Object::XPath::Boolean> if there are elements in our internal array of elements, or L<false|HTML::Object::XPath::Boolean> otherwise. | ||
| 321 | |||
| 322 | =head2 to_final_value | ||
| 323 | |||
| 324 | Returns a string resulting from the concatenation of each elements' string value. | ||
| 325 | |||
| 326 | =head2 to_literal | ||
| 327 | |||
| 328 | Returns the concatenation of all the string-values of all the nodes in the list. | ||
| 329 | |||
| 330 | =head2 to_number | ||
| 331 | |||
| 332 | Returns a new L<number object|HTML::Object::XPath::Number> from the value returned from L</to_literal> | ||
| 333 | |||
| 334 | =head2 unshift | ||
| 335 | |||
| 336 | Provided with a list of nodes and this will add them at the top of the stack in this object. | ||
| 337 | |||
| 338 | Equivalent to perl's L<perlfunc/unshift> function. | ||
| 339 | |||
| 340 | =head1 AUTHOR | ||
| 341 | |||
| 342 | Jacques Deguest E<lt>F<jack@deguest.jp>E<gt> | ||
| 343 | |||
| 344 | =head1 SEE ALSO | ||
| 345 | |||
| 346 | L<HTML::Object::XPath>, L<HTML::Object::XPath::Boolean>, L<HTML::Object::XPath::Expr>, L<HTML::Object::XPath::Function>, L<HTML::Object::XPath::Literal>, L<HTML::Object::XPath::LocationPath>, L<HTML::Object::XPath::NodeSet>, L<HTML::Object::XPath::Number>, L<HTML::Object::XPath::Root>, L<HTML::Object::XPath::Step>, L<HTML::Object::XPath::Variable> | ||
| 347 | |||
| 348 | =head1 COPYRIGHT & LICENSE | ||
| 349 | |||
| 350 | Copyright(c) 2021 DEGUEST Pte. Ltd. | ||
| 351 | |||
| 352 | All rights reserved | ||
| 353 | |||
| 354 | This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. | ||
| 355 | |||
| 356 | =cut |