| 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__ | ||
| 101 | |||
| 102 | =encoding utf-8 | ||
| 103 | |||
| 104 | =head1 NAME | ||
| 105 | |||
| 106 | HTML::Object::DOM::Collection - HTML Object DOM Collection | ||
| 107 | |||
| 108 | =head1 SYNOPSIS | ||
| 109 | |||
| 110 | use HTML::Object::DOM::Collection; | ||
| 111 | my $this = HTML::Object::DOM::Collection->new || die( HTML::Object::DOM::Collection->error, "\n" ); | ||
| 112 | |||
| 113 | my $html = <<EOT; | ||
| 114 | <html> | ||
| 115 | <head><title>Demo</title></head> | ||
| 116 | <body> | ||
| 117 | <form id="myForm"> | ||
| 118 | <input type="text" /> | ||
| 119 | <button>Ok</button> | ||
| 120 | </form> | ||
| 121 | </body> | ||
| 122 | </html> | ||
| 123 | EOT | ||
| 124 | |||
| 125 | my( $elem1, $elem2 ); | ||
| 126 | my $p = HTML::Object::DOM->new; | ||
| 127 | my $doc = $p->parse_data( $html ); | ||
| 128 | # $doc->forms is an HTML::Object::DOM::Collection | ||
| 129 | |||
| 130 | $elem1 = $doc->forms->[0]; | ||
| 131 | $elem2 = $doc->forms->item(0); | ||
| 132 | |||
| 133 | say( $elem1 == $elem2 ); # returns: "1" (i.e. true) | ||
| 134 | # or, similarly | ||
| 135 | say( $elem1 eq $elem2 ); # returns: "1" (i.e. true) | ||
| 136 | |||
| 137 | $elem1 = $doc->forms->myForm; | ||
| 138 | $elem2 = $doc->forms->namedItem("myForm"); | ||
| 139 | |||
| 140 | say( $elem1 == $elem2 ); # returns: "1" (i.e. true) | ||
| 141 | # or, similarly | ||
| 142 | say( $elem1 eq $elem2 ); # returns: "1" (i.e. true) | ||
| 143 | |||
| 144 | # This is possible under JavaScript, but not possible under perl | ||
| 145 | # $elem1 = $doc->forms->[ 'named.item.with.periods' ]; | ||
| 146 | |||
| 147 | =head1 VERSION | ||
| 148 | |||
| 149 | v0.3.0 | ||
| 150 | |||
| 151 | =head1 DESCRIPTION | ||
| 152 | |||
| 153 | The C<Collection> interface represents a generic collection (array-like object inheriting from L<Module::Generic::Array>) of a list of elements (in document order) and offers methods and properties for selecting from that list. | ||
| 154 | |||
| 155 | This is fundamentally different from L<HTML::Object::Collection>, which is used by L<HTML::Object::XQuery> | ||
| 156 | |||
| 157 | =head1 PROPERTIES | ||
| 158 | |||
| 159 | =head2 length | ||
| 160 | |||
| 161 | Returns the number of items in the collection. | ||
| 162 | |||
| 163 | =head1 METHODS | ||
| 164 | |||
| 165 | =head2 item | ||
| 166 | |||
| 167 | Provided with an integer representing an C<index> and this returns the specific L<node|HTML::Object::DOM::Node> at the given zero-based C<index> into the list. Returns C<undef> if the index is out of range. | ||
| 168 | |||
| 169 | This is also an alternative to accessing C<$collection->[$i]> (which instead returns C<undef> when C<$i> is out-of-bounds). | ||
| 170 | |||
| 171 | Example: | ||
| 172 | |||
| 173 | my $c = $doc->images; # This is an HTMLCollection | ||
| 174 | my $img0 = $c->item(0); # You can use the item() method this way | ||
| 175 | my $img1 = $c->[1]; # But this notation is easier and more common | ||
| 176 | |||
| 177 | See L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLCollection/item> | ||
| 178 | |||
| 179 | =head2 namedItem | ||
| 180 | |||
| 181 | Provided with a C<name> and this returns the specific L<node|HTML::Object::DOM::Node> whose C<ID> or, as a fallback, C<name> matches the string specified by C<name>. Matching by C<name> is only done as a last resort, only in HTML, and only if the referenced element supports the C<name> attribute. Returns C<undef> if no L<node|HTML::Object::DOM::Node> exists by the given C<name>. | ||
| 182 | |||
| 183 | An alternative to accessing C<$collection->[ $name ]> (which is possible in JavaScript, but not under perl). | ||
| 184 | |||
| 185 | Example: | ||
| 186 | |||
| 187 | <div id="personal"> | ||
| 188 | <span name="title">Dr.</span> | ||
| 189 | <span name="firstname">John</span> | ||
| 190 | <span name="lastname">Doe</span> | ||
| 191 | </div> | ||
| 192 | |||
| 193 | my $container = $doc->getElementById('personal'); | ||
| 194 | # Returns the span element object with the name "title" if no such element exists undef is returned | ||
| 195 | my $titleSpan = $container->children->namedItem('title'); | ||
| 196 | # The following variants return undefined instead of null if there's no element with a matching name or id | ||
| 197 | # Not possible in perl! | ||
| 198 | # my $firstnameSpan = $container->children->['firstname']; | ||
| 199 | my $firstnameSpan = $container->children->[1]; | ||
| 200 | my $lastnameSpan = $container->children->lastname; | ||
| 201 | |||
| 202 | See L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLCollection/namedItem> | ||
| 203 | |||
| 204 | =head1 AUTHOR | ||
| 205 | |||
| 206 | Jacques Deguest E<lt>F<jack@deguest.jp>E<gt> | ||
| 207 | |||
| 208 | =head1 SEE ALSO | ||
| 209 | |||
| 210 | L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLCollection>, L<HTML::Object::Collection> | ||
| 211 | |||
| 212 | =head1 COPYRIGHT & LICENSE | ||
| 213 | |||
| 214 | Copyright(c) 2021 DEGUEST Pte. Ltd. | ||
| 215 | |||
| 216 | All rights reserved | ||
| 217 | |||
| 218 | This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. | ||
| 219 | |||
| 220 | =cut |