Created
June 25, 2012 11:53
-
-
Save jamadam/2988167 to your computer and use it in GitHub Desktop.
Mojo::DOM tests
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sub dom { | |
return Mojo::DOM->new(<<EOF); | |
<div id="div1"> | |
A | |
<a>B</a> | |
<a>C</a> | |
<span>D</span> | |
</div> | |
<div id="div2"> | |
E | |
</div> | |
EOF | |
} | |
# Collection also capable of dom methods | |
$dom = dom(); | |
is $dom->find("div")->attrs('id'), "div1\ndiv2", 'right result'; | |
is $dom->div->attrs('id'), "div1\ndiv2", 'right result'; | |
$dom = dom(); | |
my $div1_children = $dom->find('#div1 > *'); | |
ok $div1_children->can('find'), 'can method'; | |
ok $div1_children->can('attrs'), 'can method'; | |
# following stands for $div1_children->each(sub { $_->attrs('class', 'foo') }); | |
# or emulation of jQuery(div1_childs).attr('class', 'foo'); | |
$div1_children->attrs('class', 'foo'); | |
# following stands for $div1_children->each(sub { $_->attrs('class') }); | |
# or emulation of jQuery(div1_childs).attr('class'); | |
is $div1_children->attrs('class'), "foo\nfoo\nfoo"; | |
# stands for $dom->find('div')->each(sub { $_->prepend_content('<new></new>') }); | |
# or emulation of jQuery('div').prepend('<new></new>') | |
$dom = dom(); | |
$dom->find('div')->prepend_content('<new></new>'); | |
is $dom->at("#div1 *:first-child")->type, 'new', 'right type'; | |
is $dom->at("#div2 *:first-child")->type, 'new', 'right type'; | |
# stands for $dom->find('div')->each(sub { $_->prepend('<new></new>') }); | |
# or emulation of jQuery('div').before('<new></new>'); | |
$dom = dom(); | |
$dom->find('div')->prepend('<new></new>'); | |
is $dom->find(':root > *')->[0]->type, 'new', 'right type'; | |
is $dom->find(':root > *')->[1]->type, 'div', 'right type'; | |
is $dom->find(':root > *')->[2]->type, 'new', 'right type'; | |
is $dom->find(':root > *')->[3]->type, 'div', 'right type'; | |
# stands for $dom->find('div')->each(sub { $_->append_content('<new></new>') }); | |
# or emulation of jQuery('div').append('<new></new>') | |
$dom = dom(); | |
$dom->find('div')->append_content('<new></new>'); | |
is $dom->at("#div1 *:last-child")->type, 'new', 'right type'; | |
is $dom->at("#div2 *:last-child")->type, 'new', 'right type'; | |
# stands for $dom->find('div')->each(sub { $_->append('<new></new>') }); | |
# or emulation of jQuery('div').after('<new></new>'); | |
$dom = dom(); | |
$dom->find('div')->append('<new></new>'); | |
is $dom->at('#div1 + *')->type('new'), 'new', 'right type'; | |
is $dom->at('#div2 + *')->type('new'), 'new', 'right type'; | |
# stants for $dom->find('a')->each(sub { $_->replace('<span>a</span>') }); | |
# or emulation of jQuery('a').replaceWith('<span>a</span>'); | |
$dom = dom(); | |
$dom->find('a')->replace('<span>a</span>'); | |
is $dom->find('span')->size, 3, 'right number'; | |
# stands for $dom->find('div')->map(sub { shift->text })->each | |
$dom = dom(); | |
is $dom->find('div')->text, "AE", 'right result'; | |
# stands for $dom->find('div')->each(sub {$_->replace_content('')}); | |
# or emultation of jQuery('div').empty(); | |
is $dom->find('div')->replace_content(''), | |
qq{<div id="div1"></div>\n<div id="div2"></div>}; | |
# stands for $dom->find('#div1, #div2')->map(sub { $_->type }); | |
is $dom->find('#div1, #div2')->type, "div\ndiv"; | |
# stands for $dom->find('#div1, #div2')->map(sub { $_->type('h2') }); | |
$dom->find('#div1, #div2')->type('h2'); | |
# stands for $dom->find('#div1, #div2')->map(sub { $_->type }); | |
is $dom->find('#div1, #div2')->type, 'h2\nh2', 'right number'; | |
# | |
# Wish list | |
# | |
# Wish autoloaded selector always returns collection | |
$dom = dom(); | |
is $dom->div->size, 2, 'right result'; | |
is $dom->span->size, 1, 'right result'; | |
is $dom->notfound->size, 0, 'right result'; | |
# stants for $dom->find('a')->each(sub { $_->replace('') }); | |
# or emulate of jQuery('a').remove(); | |
$dom->find('a')->remove; | |
is $dom->find('a')->size, 0, 'right number'; | |
# stants for $dom->find('a')->each(sub { $_->replace("<wrap>$_</wrap>") }); | |
# or emulate of jQuery('a').wrap('<wrap></wrap>'); | |
$dom = dom(); | |
$dom->find('a')->replace(sub { "<wrap>$_</wrap>" }); | |
is $dom->find('wrap')->[0], '<wrap><a>B</a></wrap>', 'right content'; | |
is $dom->find('wrap')->[1], '<wrap><a>C</a></wrap>', 'right content'; | |
$dom = dom(); | |
$dom->find('a')->wrap('<wrap></wrap>'); | |
is $dom->find('wrap')->[0], '<wrap><a>B</a></wrap>', 'right content'; | |
is $dom->find('wrap')->[1], '<wrap><a>C</a></wrap>', 'right content'; | |
# $dom->find('a')->each(sub { | |
# $_->replace_content('<wrap>'. $_->content_xml. '</wrap>') }) | |
# or emulate of jQuery('a').wrapInner('<wrap></wrap>'); | |
$dom = dom(); | |
$dom->find('a')->replace_content(sub { "<wrap>$_</wrap>" }); | |
is $dom->find('a')->[0], '<a><wrap>B</wrap></a>', 'right content'; | |
is $dom->find('a')->[1], '<a><wrap>C</wrap></a>', 'right content'; | |
$dom = dom(); | |
$dom->find('a')->wrap_inner('<wrap></wrap>'); | |
is $dom->find('wrap')->size, 2, 'right number'; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment