| 1 | ##---------------------------------------------------------------------------- | ||
| 2 | ## HTML Object - ~/lib/HTML/Object/DOM/Collection.pm | ||
| 3 | ## Version v0.3.0 | ||
| 4 | ## Copyright(c) 2022 DEGUEST Pte. Ltd. | ||
| 5 | ## Author: Jacques Deguest <jack@deguest.jp> | ||
| 6 | ## Created 2021/12/24 | ||
| 7 | ## Modified 2025/10/16 | ||
| 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::Collection; | ||
| 15 | BEGIN | ||
| 16 | { | ||
| 17 | use strict; | ||
| 18 | use warnings; | ||
| 19 | warnings::register_categories( 'HTML::Object' ); | ||
| 20 | use parent qw( Module::Generic::Array ); | ||
| 21 | use vars qw( $AUTOLOAD $VERSION ); | ||
| 22 | use Module::Generic::Null; | ||
| 23 | use Scalar::Util (); | ||
| 24 | use Wanted; | ||
| 25 | use overload ( | ||
| 26 | 'eq' => sub{ Scalar::Util::refaddr( $_[0] ) eq Scalar::Util::refaddr( $_[1] ) }, | ||
| 27 | '==' => sub{ Scalar::Util::refaddr( $_[0] ) eq Scalar::Util::refaddr( $_[1] ) }, | ||
| 28 | bool => sub{ $_[0] }, | ||
| 29 | fallback => 1, | ||
| 30 | ); | ||
| 31 | our $AUTOLOAD; | ||
| 32 | our $VERSION = 'v0.3.0'; | ||
| 33 | }; | ||
| 34 | |||
| 35 | use strict; | ||
| 36 | use warnings; | ||
| 37 | |||
| 38 | sub item { return( shift->index( @_ ) ); } | ||
| 39 | |||
| 40 | sub namedItem | ||
| 41 | { | ||
| 42 | my $self = shift( @_ ); | ||
| 43 | my $name = shift( @_ ); | ||
| 44 | for( @$self ) | ||
| 45 | { | ||
| 46 | return( $_ ) if( $self->_can( $_ => 'id' ) && $_->id eq $name ); | ||
| 47 | return( $_ ) if( $self->_can( $_ => 'name' ) && $_->name eq $name ); | ||
| 48 | } | ||
| 49 | if( want( 'OBJECT' ) ) | ||
| 50 | { | ||
| 51 | return( Module::Generic::Null->new( @_ ) ); | ||
| 52 | } | ||
| 53 | elsif( want( 'ARRAY' ) ) | ||
| 54 | { | ||
| 55 | return( [] ); | ||
| 56 | } | ||
| 57 | elsif( want( 'HASH' ) ) | ||
| 58 | { | ||
| 59 | return( {} ); | ||
| 60 | } | ||
| 61 | elsif( want( 'CODE' ) ) | ||
| 62 | { | ||
| 63 | return( sub{ return; } ); | ||
| 64 | } | ||
| 65 | elsif( want( 'REFSCALAR' ) ) | ||
| 66 | { | ||
| 67 | return( \undef ); | ||
| 68 | } | ||
| 69 | else | ||
| 70 | { | ||
| 71 | return; | ||
| 72 | } | ||
| 73 | } | ||
| 74 | |||
| 75 | sub _can | ||
| 76 | { | ||
| 77 | my $self = shift( @_ ); | ||
| 78 | no overloading; | ||
| 79 | # Nothing provided | ||
| 80 | return if( !scalar( @_ ) ); | ||
| 81 | return if( !defined( $_[0] ) ); | ||
| 82 | return if( !Scalar::Util::blessed( $_[0] ) ); | ||
| 83 | return( $_[0]->can( $_[1] ) ); | ||
| 84 | } | ||
| 85 | |||
| 86 | # NOTE: AUTOLOAD | ||
| 87 | sub AUTOLOAD | ||
| 88 | { | ||
| 89 | my( $meth ) = our $AUTOLOAD =~ /([^:]+)$/; | ||
| 90 | my $self = shift( @_ ); | ||
| 91 | die( "No class function \"$meth\" exists in this package \"", __PACKAGE__, "\".\n" ) if( !defined( $self ) ); | ||
| 92 | return( $self->namedItem( $meth ) ); | ||
| 93 | }; | ||
| 94 | |||
| 95 | # To avoid being caught by AUTOLOAD | ||
| 96 | sub DESTROY {}; | ||
| 97 | |||
| 98 | 1; | ||
| 99 | # NOTE: POD | ||
| 100 | __END__ |