| 1 | ##---------------------------------------------------------------------------- | ||
| 2 | ## HTML Object - ~/lib/HTML/Object/XPath/Root.pm | ||
| 3 | ## Version v0.2.0 | ||
| 4 | ## Copyright(c) 2021 DEGUEST Pte. Ltd. | ||
| 5 | ## Author: Jacques Deguest <jack@deguest.jp> | ||
| 6 | ## Created 2021/12/04 | ||
| 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::Root; | ||
| 15 | BEGIN | ||
| 16 | { | ||
| 17 | use strict; | ||
| 18 | use warnings; | ||
| 19 | warnings::register_categories( 'HTML::Object' ); | ||
| 20 | use parent qw( Module::Generic ); | ||
| 21 | use vars qw( $BASE_CLASS $DEBUG $VERSION ); | ||
| 22 | our $BASE_CLASS = 'HTML::Object::XPath'; | ||
| 23 | our $DEBUG = 0; | ||
| 24 | our $VERSION = 'v0.2.0'; | ||
| 25 | }; | ||
| 26 | |||
| 27 | use strict; | ||
| 28 | use warnings; | ||
| 29 | |||
| 30 | sub new | ||
| 31 | { | ||
| 32 | my $this = shift( @_ ); | ||
| 33 | my $str = shift( @_ ); | ||
| 34 | return( bless( \$str => ( ref( $this ) || $this ) ) ); | ||
| 35 | } | ||
| 36 | |||
| 37 | sub as_string { return; } | ||
| 38 | |||
| 39 | sub as_xml { return( "<Root/>\n" ); } | ||
| 40 | |||
| 41 | sub evaluate | ||
| 42 | { | ||
| 43 | my $self = shift( @_ ); | ||
| 44 | my $nodeset = shift( @_ ); | ||
| 45 | |||
| 46 | # must only ever occur on 1 node | ||
| 47 | die "Can't go to root on > 1 node!" unless $nodeset->size == 1; | ||
| 48 | # return( $self->error( "Can't go to root on > 1 node!" ) ) unless( $nodeset->size == 1 ); | ||
| 49 | |||
| 50 | my $newset = $self->new_nodeset; | ||
| 51 | # $newset->push($nodeset->get_node(1)->getRootNode()); | ||
| 52 | my $node = $nodeset->get_node(1); | ||
| 53 | # $node->debug(4); | ||
| 54 | my $rootNode = $node->getRootNode(); | ||
| 55 | $newset->push( $rootNode ); | ||
| 56 | return( $newset ); | ||
| 57 | } | ||
| 58 | |||
| 59 | sub new_nodeset { return( shift->_class_for( 'NodeSet' )->new( @_ ) ); } | ||
| 60 | |||
| 61 | sub _class_for | ||
| 62 | { | ||
| 63 | my( $self, $mod ) = @_; | ||
| 64 | eval( "require ${BASE_CLASS}\::${mod};" ); | ||
| 65 | die( $@ ) if( $@ ); | ||
| 66 | # ${"${BASE_CLASS}\::${mod}\::DEBUG"} = $DEBUG; | ||
| 67 | eval( "\$${BASE_CLASS}\::${mod}\::DEBUG = " . ( $DEBUG // 0 ) ); | ||
| 68 | return( "${BASE_CLASS}::${mod}" ); | ||
| 69 | } | ||
| 70 | |||
| 71 | 1; | ||
| 72 | # NOTE: POD | ||
| 73 | __END__ |