| 1 | ##---------------------------------------------------------------------------- | ||
| 2 | ## HTML Object - ~/lib/HTML/Object/Collection.pm | ||
| 3 | ## Version v0.2.0 | ||
| 4 | ## Copyright(c) 2021 DEGUEST Pte. Ltd. | ||
| 5 | ## Author: Jacques Deguest <jack@deguest.jp> | ||
| 6 | ## Created 2021/04/28 | ||
| 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::Collection; | ||
| 15 | BEGIN | ||
| 16 | { | ||
| 17 | use strict; | ||
| 18 | use warnings; | ||
| 19 | warnings::register_categories( 'HTML::Object' ); | ||
| 20 | use parent qw( HTML::Object::DOM::Element ); | ||
| 21 | use vars qw( $VERSION ); | ||
| 22 | our $VERSION = 'v0.2.0'; | ||
| 23 | }; | ||
| 24 | |||
| 25 | use strict; | ||
| 26 | use warnings; | ||
| 27 | |||
| 28 | # Purpose of this class is to serve as a distinctive class for search result from find() | ||
| 29 | sub init | ||
| 30 | { | ||
| 31 | my $self = shift( @_ ); | ||
| 32 | $self->{end} = ''; | ||
| 33 | $self->{tag} = '_collection'; | ||
| 34 | $self->{_init_strict_use_sub} = 1; | ||
| 35 | $self->{_exception_class} = 'HTML::Object::Exception'; | ||
| 36 | $self->SUPER::init( @_ ) || return( $self->pass_error ); | ||
| 37 | my $class = ref( $self ); | ||
| 38 | # To make collection exception also available as $HTML::Object::ERROR | ||
| 39 | $self->{_error_handler} = sub | ||
| 40 | { | ||
| 41 | my $ex = shift( @_ ); | ||
| 42 | # ${"${class}\::ERROR"} = $ex; | ||
| 43 | no warnings 'once'; | ||
| 44 | $HTML::Object::ERROR = $ex; | ||
| 45 | warn( $ex ) if( $self->_is_warnings_enabled( 'HTML::Object' ) ); | ||
| 46 | }; | ||
| 47 | return( $self ); | ||
| 48 | } | ||
| 49 | |||
| 50 | sub as_string | ||
| 51 | { | ||
| 52 | my $self = shift( @_ ); | ||
| 53 | my $opts = $self->_get_args_as_hash( @_ ); | ||
| 54 | $opts->{all} = 0 if( !CORE::exists( $opts->{all} ) ); | ||
| 55 | $opts->{all} //= 0; | ||
| 56 | # In conformity with jQuery, we return the stringified version of the first object in our set | ||
| 57 | # unless the 'all' option is provided and true. This is a divergence | ||
| 58 | if( $opts->{all} ) | ||
| 59 | { | ||
| 60 | my $res = $self->new_array; | ||
| 61 | $self->children->foreach(sub | ||
| 62 | { | ||
| 63 | my $child = shift( @_ ); | ||
| 64 | # This will instruct the HTML::Object::Element to automatically close | ||
| 65 | my $str = $_->as_string( inside_collection => 1 ); | ||
| 66 | $res->push( $str ); | ||
| 67 | }); | ||
| 68 | return( $res->join( '' ) ); | ||
| 69 | } | ||
| 70 | else | ||
| 71 | { | ||
| 72 | my $first = $self->children->first; | ||
| 73 | # If there is no element object in our collection, we return an empty scalar object | ||
| 74 | # to avoid a potential perl error of "called on undefined value" | ||
| 75 | # The user can then easily check if anything was provided with: | ||
| 76 | # $e->as_string->length | ||
| 77 | return( $self->new_scalar ) if( !$first || !$self->_is_a( $first, 'HTML::Object::Element' ) ); | ||
| 78 | return( $first->as_string( inside_collection => 1 ) ); | ||
| 79 | } | ||
| 80 | } | ||
| 81 | |||
| 82 | sub end { return( shift->_set_get_object({ | ||
| 83 | field => 'end', | ||
| 84 | weaken => 1, | ||
| 85 | }, 'HTML::Object::Element', @_ ) ); } | ||
| 86 | |||
| 87 | # Note: Property | ||
| 88 | sub nodeValue { return; } | ||
| 89 | |||
| 90 | 1; | ||
| 91 | # NOTE: POD | ||
| 92 | __END__ |