| 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 | 5 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | 5 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | 5 | ▶ | warnings::register_categories( 'HTML::Object' ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | 5 | ▶ | use parent qw( Module::Generic::Array ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | 5 | ▶ | use vars qw( $AUTOLOAD $VERSION ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 22 | 5 | ▶ | use Module::Generic::Null; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 23 | 5 | ▶ | use Scalar::Util (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 24 | 5 | ▶ | use Wanted; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 25 | use overload ( | ||||||||||||||||||||||||||||
| 26 | 1 | ▶ | 'eq' => sub{ Scalar::Util::refaddr( $_[0] ) eq Scalar::Util::refaddr( $_[1] ) }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 27 | 0 | ▶ | '==' => sub{ Scalar::Util::refaddr( $_[0] ) eq Scalar::Util::refaddr( $_[1] ) }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 28 | 976 | ▶ | bool => sub{ $_[0] }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 29 | 5 | fallback => 1, | |||||||||||||||||||||||||||
| 30 | 5 | ▶ | ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 31 | 5 | our $AUTOLOAD; | |||||||||||||||||||||||||||
| 32 | 5 | our $VERSION = 'v0.3.0'; | |||||||||||||||||||||||||||
| 33 | }; | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 5 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 36 | 5 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 0 | ▶ | sub item { return( shift->index( @_ ) ); } | ||||||||||||||||||||||||||
|
Subroutine
item: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | sub namedItem | ||||||||||||||||||||||||||||
| 41 | { | ||||||||||||||||||||||||||||
| 42 | 2 | ▶ | my $self = shift( @_ ); | ||||||||||||||||||||||||||
|
Subroutine
namedItem: called CC=11 SCAR=37.4
Pod
documented
| |||||||||||||||||||||||||||||
| 43 | 2 | my $name = shift( @_ ); | |||||||||||||||||||||||||||
| 44 | 2 | for( @$self ) | |||||||||||||||||||||||||||
| 45 | { | ||||||||||||||||||||||||||||
| 46 | 4 | ▶ | return( $_ ) if( $self->_can( $_ => 'id' ) && $_->id eq $name ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$self->_can($_, "id") and $_->id eq $name
MC/DC50%1/2
$self->_can($_, "id") and $_->id eq $name
$self->_can($_, "id")
$_->id eq $name
Truth table66%2/3
A and B
A= $self->_can($_, "id")B= $_->id eq $name
| |||||||||||||||||||||||||||||
| 47 | 2 | ▶ | return( $_ ) if( $self->_can( $_ => 'name' ) && $_->name eq $name ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$self->_can($_, "name") and $_->name eq $name
MC/DC0%0/2
$self->_can($_, "name") and $_->name eq $name
$self->_can($_, "name")
$_->name eq $name
Truth table66%2/3
A and B
A= $self->_can($_, "name")B= $_->name eq $name
| |||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | 0 | ▶ | if( want( 'OBJECT' ) ) | ||||||||||||||||||||||||||
|
Branch: 5 branches
| |||||||||||||||||||||||||||||
| 50 | { | ||||||||||||||||||||||||||||
| 51 | 0 | return( Module::Generic::Null->new( @_ ) ); | |||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | elsif( want( 'ARRAY' ) ) | ||||||||||||||||||||||||||||
| 54 | { | ||||||||||||||||||||||||||||
| 55 | 0 | return( [] ); | |||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | elsif( want( 'HASH' ) ) | ||||||||||||||||||||||||||||
| 58 | { | ||||||||||||||||||||||||||||
| 59 | 0 | return( {} ); | |||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | elsif( want( 'CODE' ) ) | ||||||||||||||||||||||||||||
| 62 | { | ||||||||||||||||||||||||||||
| 63 | 0 | ▶ | return( sub{ return; } ); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | elsif( want( 'REFSCALAR' ) ) | ||||||||||||||||||||||||||||
| 66 | { | ||||||||||||||||||||||||||||
| 67 | 0 | return( \undef ); | |||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | else | ||||||||||||||||||||||||||||
| 70 | { | ||||||||||||||||||||||||||||
| 71 | 0 | return; | |||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | sub _can | ||||||||||||||||||||||||||||
| 76 | { | ||||||||||||||||||||||||||||
| 77 | 6 | ▶ | my $self = shift( @_ ); | ||||||||||||||||||||||||||
|
Subroutine
_can: called CC=4 SCAR=14.2
| |||||||||||||||||||||||||||||
| 78 | 5 | ▶ | no overloading; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 79 | # Nothing provided | ||||||||||||||||||||||||||||
| 80 | 6 | ▶ | return if( !scalar( @_ ) ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | 6 | ▶ | return if( !defined( $_[0] ) ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | 6 | ▶ | return if( !Scalar::Util::blessed( $_[0] ) ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | 6 | return( $_[0]->can( $_[1] ) ); | |||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | # NOTE: AUTOLOAD | ||||||||||||||||||||||||||||
| 87 | sub AUTOLOAD | ||||||||||||||||||||||||||||
| 88 | { | ||||||||||||||||||||||||||||
| 89 | 1 | ▶ | my( $meth ) = our $AUTOLOAD =~ /([^:]+)$/; | ||||||||||||||||||||||||||
|
Subroutine
AUTOLOAD: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||
| 90 | 1 | my $self = shift( @_ ); | |||||||||||||||||||||||||||
| 91 | 1 | ▶ | die( "No class function \"$meth\" exists in this package \"", __PACKAGE__, "\".\n" ) if( !defined( $self ) ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 92 | 1 | return( $self->namedItem( $meth ) ); | |||||||||||||||||||||||||||
| 93 | }; | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | # To avoid being caught by AUTOLOAD | ||||||||||||||||||||||||||||
| 96 | ▶ | sub DESTROY {}; | |||||||||||||||||||||||||||
|
Subroutine
DESTROY: not called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 5 | 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 | ||||||||||||||||||||||||||||