NAME
HTML::Object::DOM::Collection - HTML Object DOM Collection
SYNOPSIS
my
$this
= HTML::Object::DOM::Collection->new ||
die
( HTML::Object::DOM::Collection->error,
"\n"
);
my
$html
=
<<EOT;
<html>
<head><title>Demo</title></head>
<body>
<form id="myForm">
<input type="text" />
<button>Ok</button>
</form>
</body>
</html>
EOT
my
(
$elem1
,
$elem2
);
my
$p
= HTML::Object::DOM->new;
my
$doc
=
$p
->parse_data(
$html
);
# $doc->forms is an HTML::Object::DOM::Collection
$elem1
=
$doc
->forms->[0];
$elem2
=
$doc
->forms->item(0);
say
(
$elem1
==
$elem2
);
# returns: "1" (i.e. true)
# or, similarly
say
(
$elem1
eq
$elem2
);
# returns: "1" (i.e. true)
$elem1
=
$doc
->forms->myForm;
$elem2
=
$doc
->forms->namedItem(
"myForm"
);
say
(
$elem1
==
$elem2
);
# returns: "1" (i.e. true)
# or, similarly
say
(
$elem1
eq
$elem2
);
# returns: "1" (i.e. true)
# This is possible under JavaScript, but not possible under perl
# $elem1 = $doc->forms->[ 'named.item.with.periods' ];
VERSION
v0.2.0
DESCRIPTION
The Collection
interface represents a generic collection (array-like object inheriting from Module::Generic::Array) of a list of elements (in document order) and offers methods and properties for selecting from that list.
This is fundamentally different from HTML::Object::Collection, which is used by HTML::Object::XQuery
PROPERTIES
length
Returns the number of items in the collection.
METHODS
item
Provided with an integer representing an index
and this returns the specific node at the given zero-based index
into the list. Returns undef
if the index is out of range.
This is also an alternative to accessing $collection-
[$i]> (which instead returns undef
when $i
is out-of-bounds).
Example:
my
$c
=
$doc
->images;
# This is an HTMLCollection
my
$img0
=
$c
->item(0);
# You can use the item() method this way
my
$img1
=
$c
->[1];
# But this notation is easier and more common
namedItem
Provided with a name
and this returns the specific node whose ID
or, as a fallback, name
matches the string specified by name
. Matching by name
is only done as a last resort, only in HTML, and only if the referenced element supports the name
attribute. Returns undef
if no node exists by the given name
.
An alternative to accessing $collection-
[ $name ]> (which is possible in JavaScript, but not under perl).
Example:
<div id=
"personal"
>
<span name=
"title"
>Dr.</span>
<span name=
"firstname"
>John</span>
<span name=
"lastname"
>Doe</span>
</div>
my
$container
=
$doc
->getElementById(
'personal'
);
# Returns the span element object with the name "title" if no such element exists undef is returned
my
$titleSpan
=
$container
->children->namedItem(
'title'
);
# The following variants return undefined instead of null if there's no element with a matching name or id
# Not possible in perl!
# my $firstnameSpan = $container->children->['firstname'];
my
$firstnameSpan
=
$container
->children->[1];
my
$lastnameSpan
=
$container
->children->lastname;
AUTHOR
Jacques Deguest <jack@deguest.jp>
SEE ALSO
Mozilla documentation, HTML::Object::Collection
COPYRIGHT & LICENSE
Copyright(c) 2021 DEGUEST Pte. Ltd.
All rights reserved
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.