#!/usr/bin/perl -T
BEGIN { use_ok
'HTML::DOM::Collection'
; }
BEGIN { use_ok
'HTML::DOM::NodeList'
; }
my
(
@array
);
my
$col
= new HTML::DOM::Collection (
new HTML::DOM::NodeList \
@array
);
isa_ok
$col
,
'HTML::DOM::Collection'
;
sub
id {
defined
${
$_
[0]}[0] ?
reverse
${
$_
[0]}[0] :
undef
}
sub
attr {
return
${
$_
[0]}[0]
if
$_
[1] eq
'name'
}
sub
tag {
'a'
} }
@array
=
map
+(
bless
[
$_
],
'_Elem'
),
qw'Able was I ere I saw Elba'
,
undef
;
is +(item
$col
3)->id,
'ere'
,
'item'
;
is
$$col
[3]->id,
'ere'
,
'array overloading'
;
is
$col
->
length
, 8,
'length'
;
is
$col
->namedItem(
'saw'
)->id,
'saw'
,
'namedItem (id overrides name)'
;
is
$col
->namedItem(
'Able'
)->id,
'elbA'
,
'namedItem (name works nonetheless)'
;
is
$col
->namedItem(
'I'
),
$col
->[2],
'The first item is chosen if two share the same name.'
;
is
$col
->{Elba},
$col
->namedItem(
'Elba'
),
'hash overloading'
;
ok
exists
$col
->{Elba},
'exists'
;
ok!
exists
$col
->{eLba},
'doesn\'t exist'
;
is_deeply[
keys
%$col
], [
qw[elbA saw I ere was ablE Able Elba]
],
'keys'
;
ok
%$col
,
'hash in scalar context'
;
{
my
@copy
=
@array
;
@array
= ();
ok!
%$col
,
'empty hash in scalar context'
;
@array
=
@copy
;
}
splice
@array
, 1,1;
is +(item
$col
2)->id,
'ere'
,
'item after modification'
;
is +(namedItem
$col
'saw'
)->attr(
'name'
),
'saw'
,
'namedItem afer modification'
;
sub
_elem2::id { }
sub
_elem2::attr {
$_
[0][1] }
sub
_elem2::tag {
$_
[0][0] }
{
my
@nameable_elems
=
qw/ button textarea applet select form frame
iframe img a input object map param meta/
;
@array
=
map
bless
([
$_
=>
"$_-1"
],
'_elem2'
),
@nameable_elems
;
my
$count
= 0;
is
$col
->namedItem(
"$_-1"
),
$array
[
$count
++],
"namedItem can return a"
.
'n'
x /^[aio]/ .
" $_ element"
for
@nameable_elems
;
}
weaken
$col
;
is
$col
,
undef
,
'rubbish disposal'
;