#!/usr/bin/perl -T
BEGIN { use_ok
'HTML::DOM::NodeList'
; }
BEGIN { use_ok
'HTML::DOM::NodeList::Magic'
; }
my
(
@plain
,
@magic
);
my
$plain
= new HTML::DOM::NodeList \
@plain
;
isa_ok
$plain
,
'HTML::DOM::NodeList'
;
my
$magic
= new HTML::DOM::NodeList::Magic
sub
{
@magic
};
isa_ok
$magic
,
'HTML::DOM::NodeList::Magic'
;
ok DOES
$magic
'HTML::DOM::NodeList'
,
'magic node list does NodeList'
;
@plain
=
qw'Te hypermachoi strategoi ta niketeria'
;
@magic
=
qw'Hos lytrotheisa ton deinon eucharisteria'
;
is +(item
$plain
2),
'strategoi'
,
'item'
;
is +(item
$magic
3),
'deinon'
,
'item (magic)'
;
is
$$plain
[2],
'strategoi'
,
'overloading'
;
is
$$magic
[3],
'deinon'
,
'overloading (magic)'
;
is
$plain
->
length
, 5,
'length'
;
is
$magic
->
length
, 5,
'length (magic)'
;
splice
@plain
, 1,1;
splice
@magic
, 2,1;
is +(item
$magic
3),
'deinon'
,
'item when magic list is stale'
;
is
$magic
->
length
, 5,
'length when magic list is stale'
;
$magic
->_you_are_stale;
is +(item
$plain
2),
'ta'
,
'item after modification'
;
is +(item
$magic
3),
'eucharisteria'
,
'item after modification (magic)'
;
is
$$plain
[2],
'ta'
,
'overloading after modification'
;
is
$$magic
[3],
'eucharisteria'
,
'overloading after modification (magic)'
;
is
$plain
->
length
, 4,
'length after modification'
;
is
$magic
->
length
, 4,
'length after modification (magic)'
;
no
warnings
'once'
;
*HTML::DOM::NodeList::Magic::DESTROY
=
sub
{ $::bye_bye++ };
bless
$magic
,
ref
$magic
;
undef
$magic
;
ok $::bye_bye,
'make sure the dustbin man does his job'
;
my
$doc
= new HTML::DOM;
$doc
->
open
;
$magic
= new HTML::DOM::NodeList::Magic
sub
{
$doc
->childNodes },
$doc
;
$magic
->
length
;
$doc
->appendChild(
$doc
->createElement(
'br'
));
is
$magic
->
length
, 2,
'second arg to magic node list constructor automatically registers the node list with the document'
;
$doc
= new HTML::DOM;
my
$divs
=
$doc
->getElementsByTagName(
'div'
);
()=
@$divs
;
$doc
->
write
(
"<div><div><div></div></div></div>"
);
$doc
->
close
;
is
@$divs
, 3,
"node lists are updated by document->write"
;
$doc
->elem_handler(
script
=>
sub
{
is
@$divs
, 2,
"node lists are updated before an elem_handler is called"
;
});
$doc
->
write
(
"<div><div><script></script><div></div><div></div></div></div>"
);
is
@$divs
, 4,
'node lists are updated when doc->write finishes'
;