From: Matt S Trout Date: Mon, 1 Aug 2016 16:03:56 +0000 (+0000) Subject: gut tests X-Git-Tag: v0.005~1 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FDOM-Tiny.git;a=commitdiff_plain;h=14742a9c33b73de9cc14e2fa9938ce1ad5ba6f74 gut tests --- diff --git a/Changes b/Changes index 2f929b1..a9dacd4 100644 --- a/Changes +++ b/Changes @@ -1,4 +1,5 @@ {{$NEXT}} + - Gut tests to avoid having to keep them in sync with Mojo::DOM58 changes 0.004 2016-04-10 15:09:07 UTC - Murderize dist due to replacement by Mojo::DOM58 diff --git a/t/dom.t b/t/dom.t index e7ad2ec..7c7e16c 100644 --- a/t/dom.t +++ b/t/dom.t @@ -1,2547 +1,10 @@ use strict; use warnings; -use utf8; use Test::More; use DOM::Tiny; -use JSON::PP (); -# Empty -is(DOM::Tiny->new, '', 'right result'); -is(DOM::Tiny->new(''), '', 'right result'); -is(DOM::Tiny->new->parse(''), '', 'right result'); -is(DOM::Tiny->new->at('p'), undef, 'no result'); -is(DOM::Tiny->new->append_content(''), '', 'right result'); -is(DOM::Tiny->new->all_text, '', 'right result'); +cmp_ok(scalar(@DOM::Tiny::ISA), '==', 1); -# Simple (basics) -my $dom - = DOM::Tiny->new('
A
B
'); -is $dom->at('#b')->text, 'B', 'right text'; -my @div; -push @div, $dom->find('div[id]')->map('text')->each; -is_deeply \@div, [qw(A B)], 'found all div elements with id'; -@div = (); -$dom->find('div[id]')->each(sub { push @div, $_->text }); -is_deeply \@div, [qw(A B)], 'found all div elements with id'; -is $dom->at('#a')->attr('foo'), 0, 'right attribute'; -is $dom->at('#a')->attr->{foo}, 0, 'right attribute'; -is "$dom", '
A
B
', - 'right result'; - -# Tap into method chain -$dom = DOM::Tiny->new->parse('
A
B
'); -is_deeply [$dom->find('[id]')->map(attr => 'id')->each], [qw(a b)], - 'right result'; -is $dom->tap(sub { $_->at('#b')->remove }), '
A
', - 'right result'; - -# Build tree from scratch -is(DOM::Tiny->new->append_content('

')->at('p')->append_content('0')->text, - '0', 'right text'); - -# Simple nesting with healing (tree structure) -$dom = DOM::Tiny->new(<justworks -EOF -is $dom->tree->[0], 'root', 'right type'; -is $dom->tree->[1][0], 'tag', 'right type'; -is $dom->tree->[1][1], 'foo', 'right tag'; -is_deeply $dom->tree->[1][2], {}, 'empty attributes'; -is $dom->tree->[1][3], $dom->tree, 'right parent'; -is $dom->tree->[1][4][0], 'tag', 'right type'; -is $dom->tree->[1][4][1], 'bar', 'right tag'; -is_deeply $dom->tree->[1][4][2], {a => 'btree->[1][4][3], $dom->tree->[1], 'right parent'; -is $dom->tree->[1][4][4][0], 'text', 'right type'; -is $dom->tree->[1][4][4][1], 'ju', 'right text'; -is $dom->tree->[1][4][4][2], $dom->tree->[1][4], 'right parent'; -is $dom->tree->[1][4][5][0], 'tag', 'right type'; -is $dom->tree->[1][4][5][1], 'baz', 'right tag'; -is_deeply $dom->tree->[1][4][5][2], {a23 => undef}, 'right attributes'; -is $dom->tree->[1][4][5][3], $dom->tree->[1][4], 'right parent'; -is $dom->tree->[1][4][5][4][0], 'text', 'right type'; -is $dom->tree->[1][4][5][4][1], 's', 'right text'; -is $dom->tree->[1][4][5][4][2], $dom->tree->[1][4][5], 'right parent'; -is $dom->tree->[1][4][5][5][0], 'tag', 'right type'; -is $dom->tree->[1][4][5][5][1], 'bazz', 'right tag'; -is_deeply $dom->tree->[1][4][5][5][2], {}, 'empty attributes'; -is $dom->tree->[1][4][5][5][3], $dom->tree->[1][4][5], 'right parent'; -is $dom->tree->[1][4][5][6][0], 'text', 'right type'; -is $dom->tree->[1][4][5][6][1], 't', 'right text'; -is $dom->tree->[1][4][5][6][2], $dom->tree->[1][4][5], 'right parent'; -is $dom->tree->[1][5][0], 'text', 'right type'; -is $dom->tree->[1][5][1], 'works', 'right text'; -is $dom->tree->[1][5][2], $dom->tree->[1], 'right parent'; -is "$dom", <justworks -EOF - -# Select based on parent -$dom = DOM::Tiny->new(< -

test1
-
test2
- -EOF -is $dom->find('body > div')->[0]->text, 'test1', 'right text'; -is $dom->find('body > div')->[1]->text, '', 'no content'; -is $dom->find('body > div')->[2], undef, 'no result'; -is $dom->find('body > div')->size, 2, 'right number of elements'; -is $dom->find('body > div > div')->[0]->text, 'test2', 'right text'; -is $dom->find('body > div > div')->[1], undef, 'no result'; -is $dom->find('body > div > div')->size, 1, 'right number of elements'; - -# A bit of everything (basic navigation) -$dom = DOM::Tiny->new->parse(< - - test - easy - - - works well - - - - < very broken -
- more text -
-EOF -ok !$dom->xml, 'XML mode not detected'; -is $dom->tag, undef, 'no tag'; -is $dom->attr('foo'), undef, 'no attribute'; -is $dom->attr(foo => 'bar')->attr('foo'), undef, 'no attribute'; -is $dom->tree->[1][0], 'doctype', 'right type'; -is $dom->tree->[1][1], ' foo', 'right doctype'; -is "$dom", < - - test - easy - - - works well - - - - < very broken -
- more text -
-EOF -my $simple = $dom->at('foo simple.working[class^="wor"]'); -is $simple->parent->all_text, - 'test easy works well yada yada < very broken more text', 'right text'; -is $simple->tag, 'simple', 'right tag'; -is $simple->attr('class'), 'working', 'right class attribute'; -is $simple->text, 'easy', 'right text'; -is $simple->parent->tag, 'foo', 'right parent tag'; -is $simple->parent->attr->{bar}, 'baparent->children->[1]->tag, 'test', 'right sibling'; -is $simple->to_string, 'easy', - 'stringified right'; -$simple->parent->attr(bar => 'baz')->attr({this => 'works', too => 'yea'}); -is $simple->parent->attr('bar'), 'baz', 'right parent attribute'; -is $simple->parent->attr('this'), 'works', 'right parent attribute'; -is $simple->parent->attr('too'), 'yea', 'right parent attribute'; -is $dom->at('test#test')->tag, 'test', 'right tag'; -is $dom->at('[class$="ing"]')->tag, 'simple', 'right tag'; -is $dom->at('[class="working"]')->tag, 'simple', 'right tag'; -is $dom->at('[class$=ing]')->tag, 'simple', 'right tag'; -is $dom->at('[class=working][class]')->tag, 'simple', 'right tag'; -is $dom->at('foo > simple')->next->tag, 'test', 'right tag'; -is $dom->at('foo > simple')->next->next->tag, 'a', 'right tag'; -is $dom->at('foo > test')->previous->tag, 'simple', 'right tag'; -is $dom->next, undef, 'no siblings'; -is $dom->previous, undef, 'no siblings'; -is $dom->at('foo > a')->next, undef, 'no next sibling'; -is $dom->at('foo > simple')->previous, undef, 'no previous sibling'; -is_deeply [$dom->at('simple')->ancestors->map('tag')->each], ['foo'], - 'right results'; -ok !$dom->at('simple')->ancestors->first->xml, 'XML mode not active'; - -# Nodes -$dom = DOM::Tiny->new( - '

test

'); -is $dom->at('p')->preceding_nodes->first->content, ' before', 'right content'; -is $dom->at('p')->preceding_nodes->size, 1, 'right number of nodes'; -is $dom->at('p')->child_nodes->last->preceding_nodes->first->content, 'test', - 'right content'; -is $dom->at('p')->child_nodes->last->preceding_nodes->last->content, '123', - 'right content'; -is $dom->at('p')->child_nodes->last->preceding_nodes->size, 2, - 'right number of nodes'; -is $dom->preceding_nodes->size, 0, 'no preceding nodes'; -is $dom->at('p')->following_nodes->first->content, 'after', 'right content'; -is $dom->at('p')->following_nodes->size, 1, 'right number of nodes'; -is $dom->child_nodes->first->following_nodes->first->tag, 'p', 'right tag'; -is $dom->child_nodes->first->following_nodes->last->content, 'after', - 'right content'; -is $dom->child_nodes->first->following_nodes->size, 2, 'right number of nodes'; -is $dom->following_nodes->size, 0, 'no following nodes'; -is $dom->at('p')->previous_node->content, ' before', 'right content'; -is $dom->at('p')->previous_node->previous_node, undef, 'no more siblings'; -is $dom->at('p')->next_node->content, 'after', 'right content'; -is $dom->at('p')->next_node->next_node, undef, 'no more siblings'; -is $dom->at('p')->child_nodes->last->previous_node->previous_node->content, - 'test', 'right content'; -is $dom->at('p')->child_nodes->first->next_node->next_node->content, ' 456 ', - 'right content'; -is $dom->descendant_nodes->[0]->type, 'doctype', 'right type'; -is $dom->descendant_nodes->[0]->content, ' before', 'right content'; -is $dom->descendant_nodes->[0], '', 'right content'; -is $dom->descendant_nodes->[1]->tag, 'p', 'right tag'; -is $dom->descendant_nodes->[2]->type, 'text', 'right type'; -is $dom->descendant_nodes->[2]->content, 'test', 'right content'; -is $dom->descendant_nodes->[5]->type, 'pi', 'right type'; -is $dom->descendant_nodes->[5]->content, 'after', 'right content'; -is $dom->at('p')->descendant_nodes->[0]->type, 'text', 'right type'; -is $dom->at('p')->descendant_nodes->[0]->content, 'test', 'right type'; -is $dom->at('p')->descendant_nodes->last->type, 'comment', 'right type'; -is $dom->at('p')->descendant_nodes->last->content, ' 456 ', 'right type'; -is $dom->child_nodes->[1]->child_nodes->first->parent->tag, 'p', 'right tag'; -is $dom->child_nodes->[1]->child_nodes->first->content, 'test', 'right content'; -is $dom->child_nodes->[1]->child_nodes->first, 'test', 'right content'; -is $dom->at('p')->child_nodes->first->type, 'text', 'right type'; -is $dom->at('p')->child_nodes->first->remove->tag, 'p', 'right tag'; -is $dom->at('p')->child_nodes->first->type, 'cdata', 'right type'; -is $dom->at('p')->child_nodes->first->content, '123', 'right content'; -is $dom->at('p')->child_nodes->[1]->type, 'comment', 'right type'; -is $dom->at('p')->child_nodes->[1]->content, ' 456 ', 'right content'; -is $dom->[0]->type, 'doctype', 'right type'; -is $dom->[0]->content, ' before', 'right content'; -is $dom->child_nodes->[2]->type, 'pi', 'right type'; -is $dom->child_nodes->[2]->content, 'after', 'right content'; -is $dom->child_nodes->first->content(' again')->content, ' again', - 'right content'; -is $dom->child_nodes->grep(sub { $_->type eq 'pi' })->map('remove') - ->first->type, 'root', 'right type'; -is "$dom", '

', 'right result'; - -# Modify nodes -$dom = DOM::Tiny->new(''); -is $dom->at('script')->type, 'tag', 'right type'; -is $dom->at('script')->[0]->type, 'raw', 'right type'; -is $dom->at('script')->[0]->content, 'lala', 'right content'; -is "$dom", '', 'right result'; -is $dom->at('script')->child_nodes->first->replace('ac1d')->tag, - 'script', 'right tag'; -is "$dom", '', 'right result'; -is $dom->at('b')->child_nodes->first->append('e')->content, 'c', - 'right content'; -is $dom->at('b')->child_nodes->first->prepend('f')->type, 'text', 'right type'; -is "$dom", '', 'right result'; -is $dom->at('script')->child_nodes->first->following->first->tag, 'b', - 'right tag'; -is $dom->at('script')->child_nodes->first->next->content, 'fce', - 'right content'; -is $dom->at('script')->child_nodes->first->previous, undef, 'no siblings'; -is $dom->at('script')->child_nodes->[2]->previous->content, 'fce', - 'right content'; -is $dom->at('b')->child_nodes->[1]->next, undef, 'no siblings'; -is $dom->at('script')->child_nodes->first->wrap(':)')->root, - '', 'right result'; -is $dom->at('i')->child_nodes->first->wrap_content('')->root, - '', 'no changes'; -is $dom->at('i')->child_nodes->first->wrap('')->root, - '', 'right result'; -is $dom->at('b')->child_nodes->first->ancestors->map('tag')->join(','), - 'b,i,script', 'right result'; -is $dom->at('b')->child_nodes->first->append_content('g')->content, ':)g', - 'right content'; -is $dom->at('b')->child_nodes->first->prepend_content('h')->content, 'h:)g', - 'right content'; -is "$dom", '', - 'right result'; -is $dom->at('script > b:last-of-type')->append('') - ->following_nodes->first->content, 'y', 'right content'; -is $dom->at('i')->prepend('z')->preceding_nodes->first->content, 'z', - 'right content'; -is $dom->at('i')->following->last->text, 'd', 'right text'; -is $dom->at('i')->following->size, 2, 'right number of following elements'; -is $dom->at('i')->following('b:last-of-type')->first->text, 'd', 'right text'; -is $dom->at('i')->following('b:last-of-type')->size, 1, - 'right number of following elements'; -is $dom->following->size, 0, 'no following elements'; -is $dom->at('script > b:last-of-type')->preceding->first->tag, 'i', 'right tag'; -is $dom->at('script > b:last-of-type')->preceding->size, 2, - 'right number of preceding elements'; -is $dom->at('script > b:last-of-type')->preceding('b')->first->tag, 'b', - 'right tag'; -is $dom->at('script > b:last-of-type')->preceding('b')->size, 1, - 'right number of preceding elements'; -is $dom->preceding->size, 0, 'no preceding elements'; -is "$dom", '', - 'right result'; - -# XML nodes -$dom = DOM::Tiny->new->xml(1)->parse('test'); -ok $dom->at('b')->child_nodes->first->xml, 'XML mode active'; -ok $dom->at('b')->child_nodes->first->replace('
')->child_nodes->first->xml, - 'XML mode active'; -is "$dom", '
', 'right result'; - -# Treating nodes as elements -$dom = DOM::Tiny->new('foobarbaz'); -is $dom->child_nodes->first->child_nodes->size, 0, 'no nodes'; -is $dom->child_nodes->first->descendant_nodes->size, 0, 'no nodes'; -is $dom->child_nodes->first->children->size, 0, 'no children'; -is $dom->child_nodes->first->strip->parent, 'foobarbaz', 'no changes'; -is $dom->child_nodes->first->at('b'), undef, 'no result'; -is $dom->child_nodes->first->find('*')->size, 0, 'no results'; -ok !$dom->child_nodes->first->matches('*'), 'no match'; -is_deeply $dom->child_nodes->first->attr, {}, 'no attributes'; -is $dom->child_nodes->first->namespace, undef, 'no namespace'; -is $dom->child_nodes->first->tag, undef, 'no tag'; -is $dom->child_nodes->first->text, '', 'no text'; -is $dom->child_nodes->first->all_text, '', 'no text'; - -# Class and ID -$dom = DOM::Tiny->new('
a
'); -is $dom->at('div#id.class')->text, 'a', 'right text'; - -# Deep nesting (parent combinator) -$dom = DOM::Tiny->new(< - - Foo - - -
- -
-
-

Bar

-
-
-
More stuff
-
- - -EOF -my $p = $dom->find('body > #container > div p[id]'); -is $p->[0]->attr('id'), 'foo', 'right id attribute'; -is $p->[1], undef, 'no second result'; -is $p->size, 1, 'right number of elements'; -my @p; -@div = (); -$dom->find('div')->each(sub { push @div, $_->attr('id') }); -$dom->find('p')->each(sub { push @p, $_->attr('id') }); -is_deeply \@p, [qw(foo bar)], 'found all p elements'; -my $ids = [qw(container header logo buttons buttons content)]; -is_deeply \@div, $ids, 'found all div elements'; -is_deeply [$dom->at('p')->ancestors->map('tag')->each], - [qw(div div div body html)], 'right results'; -is_deeply [$dom->at('html')->ancestors->each], [], 'no results'; -is_deeply [$dom->ancestors->each], [], 'no results'; - -# Script tag -$dom = DOM::Tiny->new(<alert('lalala'); -EOF -is $dom->at('script')->text, "alert('lalala');", 'right script content'; - -# HTML5 (unquoted values) -$dom = DOM::Tiny->new( - '
works
'); -is $dom->at('#test')->text, 'works', 'right text'; -is $dom->at('div')->text, 'works', 'right text'; -is $dom->at('[foo=bar][foo="bar"]')->text, 'works', 'right text'; -is $dom->at('[foo="ba"]'), undef, 'no result'; -is $dom->at('[foo=bar]')->text, 'works', 'right text'; -is $dom->at('[foo=ba]'), undef, 'no result'; -is $dom->at('.tset')->text, 'works', 'right text'; -is $dom->at('[bar=/baz/]')->text, 'works', 'right text'; -is $dom->at('[baz=//]')->text, 'works', 'right text'; - -# HTML1 (single quotes, uppercase tags and whitespace in attributes) -$dom = DOM::Tiny->new(q{
works
}); -is $dom->at('#test')->text, 'works', 'right text'; -is $dom->at('div')->text, 'works', 'right text'; -is $dom->at('[foo="bar"]')->text, 'works', 'right text'; -is $dom->at('[foo="ba"]'), undef, 'no result'; -is $dom->at('[foo=bar]')->text, 'works', 'right text'; -is $dom->at('[foo=ba]'), undef, 'no result'; -is $dom->at('.tset')->text, 'works', 'right text'; - -# Already decoded Unicode snowman and quotes in selector -$dom = DOM::Tiny->new('
☃
'); -is $dom->at('[id="snow\'m\"an"]')->text, '☃', 'right text'; -is $dom->at('[id="snow\'m\22 an"]')->text, '☃', 'right text'; -is $dom->at('[id="snow\'m\000022an"]')->text, '☃', 'right text'; -is $dom->at('[id="snow\'m\22an"]'), undef, 'no result'; -is $dom->at('[id="snow\'m\21 an"]'), undef, 'no result'; -is $dom->at('[id="snow\'m\000021an"]'), undef, 'no result'; -is $dom->at('[id="snow\'m\000021 an"]'), undef, 'no result'; -is $dom->at("[id='snow\\'m\"an']")->text, '☃', 'right text'; -is $dom->at("[id='snow\\27m\"an']")->text, '☃', 'right text'; - -# Unicode and escaped selectors -my $html - = '
Snowman
Heart
'; -$dom = DOM::Tiny->new($html); -is $dom->at("#\\\n\\002603x")->text, 'Snowman', 'right text'; -is $dom->at('#\\2603 x')->text, 'Snowman', 'right text'; -is $dom->at("#\\\n\\2603 x")->text, 'Snowman', 'right text'; -is $dom->at(qq{[id="\\\n\\2603 x"]})->text, 'Snowman', 'right text'; -is $dom->at(qq{[id="\\\n\\002603x"]})->text, 'Snowman', 'right text'; -is $dom->at(qq{[id="\\\\2603 x"]})->text, 'Snowman', 'right text'; -is $dom->at("html #\\\n\\002603x")->text, 'Snowman', 'right text'; -is $dom->at('html #\\2603 x')->text, 'Snowman', 'right text'; -is $dom->at("html #\\\n\\2603 x")->text, 'Snowman', 'right text'; -is $dom->at(qq{html [id="\\\n\\2603 x"]})->text, 'Snowman', 'right text'; -is $dom->at(qq{html [id="\\\n\\002603x"]})->text, 'Snowman', 'right text'; -is $dom->at(qq{html [id="\\\\2603 x"]})->text, 'Snowman', 'right text'; -is $dom->at('#☃x')->text, 'Snowman', 'right text'; -is $dom->at('div#☃x')->text, 'Snowman', 'right text'; -is $dom->at('html div#☃x')->text, 'Snowman', 'right text'; -is $dom->at('[id^="☃"]')->text, 'Snowman', 'right text'; -is $dom->at('div[id^="☃"]')->text, 'Snowman', 'right text'; -is $dom->at('html div[id^="☃"]')->text, 'Snowman', 'right text'; -is $dom->at('html > div[id^="☃"]')->text, 'Snowman', 'right text'; -is $dom->at('[id^=☃]')->text, 'Snowman', 'right text'; -is $dom->at('div[id^=☃]')->text, 'Snowman', 'right text'; -is $dom->at('html div[id^=☃]')->text, 'Snowman', 'right text'; -is $dom->at('html > div[id^=☃]')->text, 'Snowman', 'right text'; -is $dom->at(".\\\n\\002665")->text, 'Heart', 'right text'; -is $dom->at('.\\2665')->text, 'Heart', 'right text'; -is $dom->at("html .\\\n\\002665")->text, 'Heart', 'right text'; -is $dom->at('html .\\2665')->text, 'Heart', 'right text'; -is $dom->at(qq{html [class\$="\\\n\\002665"]})->text, 'Heart', 'right text'; -is $dom->at(qq{html [class\$="\\2665"]})->text, 'Heart', 'right text'; -is $dom->at(qq{[class\$="\\\n\\002665"]})->text, 'Heart', 'right text'; -is $dom->at(qq{[class\$="\\2665"]})->text, 'Heart', 'right text'; -is $dom->at('.x')->text, 'Heart', 'right text'; -is $dom->at('html .x')->text, 'Heart', 'right text'; -is $dom->at('.♥')->text, 'Heart', 'right text'; -is $dom->at('html .♥')->text, 'Heart', 'right text'; -is $dom->at('div.♥')->text, 'Heart', 'right text'; -is $dom->at('html div.♥')->text, 'Heart', 'right text'; -is $dom->at('[class$="♥"]')->text, 'Heart', 'right text'; -is $dom->at('div[class$="♥"]')->text, 'Heart', 'right text'; -is $dom->at('html div[class$="♥"]')->text, 'Heart', 'right text'; -is $dom->at('html > div[class$="♥"]')->text, 'Heart', 'right text'; -is $dom->at('[class$=♥]')->text, 'Heart', 'right text'; -is $dom->at('div[class$=♥]')->text, 'Heart', 'right text'; -is $dom->at('html div[class$=♥]')->text, 'Heart', 'right text'; -is $dom->at('html > div[class$=♥]')->text, 'Heart', 'right text'; -is $dom->at('[class~="♥"]')->text, 'Heart', 'right text'; -is $dom->at('div[class~="♥"]')->text, 'Heart', 'right text'; -is $dom->at('html div[class~="♥"]')->text, 'Heart', 'right text'; -is $dom->at('html > div[class~="♥"]')->text, 'Heart', 'right text'; -is $dom->at('[class~=♥]')->text, 'Heart', 'right text'; -is $dom->at('div[class~=♥]')->text, 'Heart', 'right text'; -is $dom->at('html div[class~=♥]')->text, 'Heart', 'right text'; -is $dom->at('html > div[class~=♥]')->text, 'Heart', 'right text'; -is $dom->at('[class~="x"]')->text, 'Heart', 'right text'; -is $dom->at('div[class~="x"]')->text, 'Heart', 'right text'; -is $dom->at('html div[class~="x"]')->text, 'Heart', 'right text'; -is $dom->at('html > div[class~="x"]')->text, 'Heart', 'right text'; -is $dom->at('[class~=x]')->text, 'Heart', 'right text'; -is $dom->at('div[class~=x]')->text, 'Heart', 'right text'; -is $dom->at('html div[class~=x]')->text, 'Heart', 'right text'; -is $dom->at('html > div[class~=x]')->text, 'Heart', 'right text'; -is $dom->at('html'), $html, 'right result'; -is $dom->at('#☃x')->parent, $html, 'right result'; -is $dom->at('#☃x')->root, $html, 'right result'; -is $dom->children('html')->first, $html, 'right result'; -is $dom->to_string, $html, 'right result'; -is $dom->content, $html, 'right result'; - -# Looks remotely like HTML -$dom = DOM::Tiny->new( - '☃♥☃'); -is $dom->at('title')->text, '♥', 'right text'; -is $dom->at('*')->text, '♥', 'right text'; -is $dom->at('.test')->text, '♥', 'right text'; - -# Replace elements -$dom = DOM::Tiny->new('
foo

lalala

bar
'); -is $dom->at('p')->replace('bar'), '
foobarbar
', - 'right result'; -is "$dom", '
foobarbar
', 'right result'; -$dom->at('foo')->replace(DOM::Tiny->new('text')); -is "$dom", '
footextbar
', 'right result'; -$dom = DOM::Tiny->new('
foo
bar
'); -$dom->find('div')->each(sub { shift->replace('

test

') }); -is "$dom", '

test

test

', 'right result'; -$dom = DOM::Tiny->new('
foo

lalala

bar
'); -is $dom->replace('♥'), '♥', 'right result'; -is "$dom", '♥', 'right result'; -$dom->replace('
foo

lalala

bar
'); -is "$dom", '
foo

lalala

bar
', 'right result'; -is $dom->at('p')->replace(''), '
foobar
', 'right result'; -is "$dom", '
foobar
', 'right result'; -is $dom->replace(''), '', 'no result'; -is "$dom", '', 'no result'; -$dom->replace('
foo

lalala

bar
'); -is "$dom", '
foo

lalala

bar
', 'right result'; -$dom->find('p')->map(replace => ''); -is "$dom", '
foobar
', 'right result'; -$dom = DOM::Tiny->new('
♥
'); -$dom->at('div')->content('☃'); -is "$dom", '
☃
', 'right result'; -$dom = DOM::Tiny->new('
♥
'); -$dom->at('div')->content("\x{2603}"); -is $dom->to_string, '
☃
', 'right result'; -is $dom->at('div')->replace('

♥

')->root, '

♥

', 'right result'; -is $dom->to_string, '

♥

', 'right result'; -is $dom->replace('whatever')->root, 'whatever', 'right result'; -is $dom->to_string, 'whatever', 'right result'; -$dom->at('b')->prepend('

foo

')->append('

bar

'); -is "$dom", '

foo

whatever

bar

', 'right result'; -is $dom->find('p')->map('remove')->first->root->at('b')->text, 'whatever', - 'right result'; -is "$dom", 'whatever', 'right result'; -is $dom->at('b')->strip, 'whatever', 'right result'; -is $dom->strip, 'whatever', 'right result'; -is $dom->remove, '', 'right result'; -$dom->replace('A
B

CDEFG

H
I'); -is $dom->find(':not(div):not(i):not(u)')->map('strip')->first->root, - 'A
BCDEFG
H
I', 'right result'; -is $dom->at('i')->to_string, 'E', 'right result'; -$dom = DOM::Tiny->new('
A
B
C
'); -is $dom->at('div')->at('div')->text, 'A', 'right text'; -$dom->at('div')->find('div')->map('strip'); -is "$dom", '
ABC
', 'right result'; - -# Replace element content -$dom = DOM::Tiny->new('
foo

lalala

bar
'); -is $dom->at('p')->content('bar'), '

bar

', 'right result'; -is "$dom", '
foo

bar

bar
', 'right result'; -$dom->at('p')->content(DOM::Tiny->new('text')); -is "$dom", '
foo

text

bar
', 'right result'; -$dom = DOM::Tiny->new('
foo
bar
'); -$dom->find('div')->each(sub { shift->content('

test

') }); -is "$dom", '

test

test

', 'right result'; -$dom->find('p')->each(sub { shift->content('') }); -is "$dom", '

', 'right result'; -$dom = DOM::Tiny->new('

'); -$dom->at('#☃')->content('♥'); -is "$dom", '

♥

', 'right result'; -$dom = DOM::Tiny->new('
foo

lalala

bar
'); -$dom->content('♥'); -is "$dom", '♥', 'right result'; -is $dom->content('
foo

lalala

bar
'), - '
foo

lalala

bar
', 'right result'; -is "$dom", '
foo

lalala

bar
', 'right result'; -is $dom->content(''), '', 'no result'; -is "$dom", '', 'no result'; -$dom->content('
foo

lalala

bar
'); -is "$dom", '
foo

lalala

bar
', 'right result'; -is $dom->at('p')->content(''), '

', 'right result'; - -# Mixed search and tree walk -$dom = DOM::Tiny->new(< - - text1 - text2 - - -EOF -my @data; -for my $tr ($dom->find('table tr')->each) { - for my $td (@{$tr->children}) { - push @data, $td->tag, $td->all_text; - } -} -is $data[0], 'td', 'right tag'; -is $data[1], 'text1', 'right text'; -is $data[2], 'td', 'right tag'; -is $data[3], 'text2', 'right text'; -is $data[4], undef, 'no tag'; - -# RSS -$dom = DOM::Tiny->new(< - - - Test Blog - http://blog.example.com - lalala - DOM::Tiny - - Mon, 12 Jul 2010 20:42:00 - Works! - http://blog.example.com/test - http://blog.example.com/test - - trololololo>]]> - - - - - - - -EOF -ok $dom->xml, 'XML mode detected'; -is $dom->find('rss')->[0]->attr('version'), '2.0', 'right version'; -is_deeply [$dom->at('title')->ancestors->map('tag')->each], [qw(channel rss)], - 'right results'; -is $dom->at('extension')->attr('foo:id'), 'works', 'right id'; -like $dom->at('#works')->text, qr/\[awesome\]\]/, 'right text'; -like $dom->at('[id="works"]')->text, qr/\[awesome\]\]/, 'right text'; -is $dom->find('description')->[1]->text, '

trololololo>', 'right text'; -is $dom->at('pubDate')->text, 'Mon, 12 Jul 2010 20:42:00', 'right text'; -like $dom->at('[id*="ork"]')->text, qr/\[awesome\]\]/, 'right text'; -like $dom->at('[id*="orks"]')->text, qr/\[awesome\]\]/, 'right text'; -like $dom->at('[id*="work"]')->text, qr/\[awesome\]\]/, 'right text'; -like $dom->at('[id*="or"]')->text, qr/\[awesome\]\]/, 'right text'; -ok $dom->at('rss')->xml, 'XML mode active'; -ok $dom->at('extension')->parent->xml, 'XML mode active'; -ok $dom->at('extension')->root->xml, 'XML mode active'; -ok $dom->children('rss')->first->xml, 'XML mode active'; -ok $dom->at('title')->ancestors->first->xml, 'XML mode active'; - -# Namespace -$dom = DOM::Tiny->new(< - - Programming Perl - rocks! - -

Nothing
- - - 978-0596000271 - - -EOF -ok $dom->xml, 'XML mode detected'; -is $dom->namespace, undef, 'no namespace'; -is $dom->at('book comment')->namespace, 'uri:default-ns', 'right namespace'; -is $dom->at('book comment')->text, 'rocks!', 'right text'; -is $dom->at('book nons section')->namespace, '', 'no namespace'; -is $dom->at('book nons section')->text, 'Nothing', 'right text'; -is $dom->at('book meta number')->namespace, 'uri:isbn-ns', 'right namespace'; -is $dom->at('book meta number')->text, '978-0596000271', 'right text'; -is $dom->children('bk\:book')->first->{xmlns}, 'uri:default-ns', - 'right attribute'; -is $dom->children('book')->first->{xmlns}, 'uri:default-ns', 'right attribute'; -is $dom->children('k\:book')->first, undef, 'no result'; -is $dom->children('ook')->first, undef, 'no result'; -is $dom->at('k\:book'), undef, 'no result'; -is $dom->at('ook'), undef, 'no result'; -is $dom->at('[xmlns\:bk]')->{'xmlns:bk'}, 'uri:book-ns', 'right attribute'; -is $dom->at('[bk]')->{'xmlns:bk'}, 'uri:book-ns', 'right attribute'; -is $dom->at('[bk]')->attr('xmlns:bk'), 'uri:book-ns', 'right attribute'; -is $dom->at('[bk]')->attr('s:bk'), undef, 'no attribute'; -is $dom->at('[bk]')->attr('bk'), undef, 'no attribute'; -is $dom->at('[bk]')->attr('k'), undef, 'no attribute'; -is $dom->at('[s\:bk]'), undef, 'no result'; -is $dom->at('[k]'), undef, 'no result'; -is $dom->at('number')->ancestors('meta')->first->{xmlns}, 'uri:meta-ns', - 'right attribute'; -ok $dom->at('nons')->matches('book > nons'), 'element did match'; -ok !$dom->at('title')->matches('book > nons > section'), - 'element did not match'; - -# Dots -$dom = DOM::Tiny->new(< - - - First - Second - - -EOF -is $dom->at('foo bar baz')->text, 'First', 'right text'; -is $dom->at('baz')->namespace, 'uri:first', 'right namespace'; -is $dom->at('foo bar ya\.da')->text, 'Second', 'right text'; -is $dom->at('ya\.da')->namespace, 'uri:second', 'right namespace'; -is $dom->at('foo')->namespace, undef, 'no namespace'; -is $dom->at('[xml\.s]'), undef, 'no result'; -is $dom->at('b\.z'), undef, 'no result'; - -# Yadis -$dom = DOM::Tiny->new(<<'EOF'); - - - - - http://o.r.g/sso/2.0 - - - http://o.r.g/sso/1.0 - - - -EOF -ok $dom->xml, 'XML mode detected'; -is $dom->at('XRDS')->namespace, 'xri://$xrds', 'right namespace'; -is $dom->at('XRD')->namespace, 'xri://$xrd*($v*2.0)', 'right namespace'; -my $s = $dom->find('XRDS XRD Service'); -is $s->[0]->at('Type')->text, 'http://o.r.g/sso/2.0', 'right text'; -is $s->[0]->namespace, 'xri://$xrd*($v*2.0)', 'right namespace'; -is $s->[1]->at('Type')->text, 'http://o.r.g/sso/1.0', 'right text'; -is $s->[1]->namespace, 'xri://$xrd*($v*2.0)', 'right namespace'; -is $s->[2], undef, 'no result'; -is $s->size, 2, 'right number of elements'; - -# Yadis (roundtrip with namespace) -my $yadis = <<'EOF'; - - - - - http://o.r.g/sso/3.0 - - - http://o.r.g/sso/4.0 - - - - - http://o.r.g/sso/2.0 - - - http://o.r.g/sso/1.0 - - - -EOF -$dom = DOM::Tiny->new($yadis); -ok $dom->xml, 'XML mode detected'; -is $dom->at('XRDS')->namespace, 'xri://$xrds', 'right namespace'; -is $dom->at('XRD')->namespace, 'xri://$xrd*($v*2.0)', 'right namespace'; -$s = $dom->find('XRDS XRD Service'); -is $s->[0]->at('Type')->text, 'http://o.r.g/sso/3.0', 'right text'; -is $s->[0]->namespace, 'xri://$xrd*($v*2.0)', 'right namespace'; -is $s->[1]->at('Type')->text, 'http://o.r.g/sso/4.0', 'right text'; -is $s->[1]->namespace, 'xri://$xrds', 'right namespace'; -is $s->[2]->at('Type')->text, 'http://o.r.g/sso/2.0', 'right text'; -is $s->[2]->namespace, 'xri://$xrd*($v*2.0)', 'right namespace'; -is $s->[3]->at('Type')->text, 'http://o.r.g/sso/1.0', 'right text'; -is $s->[3]->namespace, 'xri://$xrd*($v*2.0)', 'right namespace'; -is $s->[4], undef, 'no result'; -is $s->size, 4, 'right number of elements'; -is $dom->at('[Test="23"]')->text, 'http://o.r.g/sso/1.0', 'right text'; -is $dom->at('[test="23"]')->text, 'http://o.r.g/sso/2.0', 'right text'; -is $dom->find('xrds\:Service > Type')->[0]->text, 'http://o.r.g/sso/4.0', - 'right text'; -is $dom->find('xrds\:Service > Type')->[1], undef, 'no result'; -is $dom->find('xrds\3AService > Type')->[0]->text, 'http://o.r.g/sso/4.0', - 'right text'; -is $dom->find('xrds\3AService > Type')->[1], undef, 'no result'; -is $dom->find('xrds\3A Service > Type')->[0]->text, 'http://o.r.g/sso/4.0', - 'right text'; -is $dom->find('xrds\3A Service > Type')->[1], undef, 'no result'; -is $dom->find('xrds\00003AService > Type')->[0]->text, 'http://o.r.g/sso/4.0', - 'right text'; -is $dom->find('xrds\00003AService > Type')->[1], undef, 'no result'; -is $dom->find('xrds\00003A Service > Type')->[0]->text, 'http://o.r.g/sso/4.0', - 'right text'; -is $dom->find('xrds\00003A Service > Type')->[1], undef, 'no result'; -is "$dom", $yadis, 'successful roundtrip'; - -# Result and iterator order -$dom = DOM::Tiny->new('123'); -my @numbers; -$dom->find('b')->each(sub { push @numbers, pop, shift->text }); -is_deeply \@numbers, [1, 1, 2, 2, 3, 3], 'right order'; - -# Attributes on multiple lines -$dom = DOM::Tiny->new("
"); -is $dom->at('div.x')->attr('test'), 23, 'right attribute'; -is $dom->at('[foo="bar"]')->attr('class'), 'x', 'right attribute'; -is $dom->at('div')->attr(baz => undef)->root->to_string, - '
', 'right result'; - -# Markup characters in attribute values -$dom = DOM::Tiny->new(qq{
Test
}); -is $dom->at('div[id=""]')->attr->{test}, '=', 'right attribute'; -is $dom->at('[id=""]')->text, 'Test', 'right text'; -is $dom->at('[id="><"]')->attr->{id}, '><', 'right attribute'; - -# Empty attributes -$dom = DOM::Tiny->new(qq{
}); -is $dom->at('div')->attr->{test}, '', 'empty attribute value'; -is $dom->at('div')->attr->{test2}, '', 'empty attribute value'; -is $dom->at('[test]')->tag, 'div', 'right tag'; -is $dom->at('[test2]')->tag, 'div', 'right tag'; -is $dom->at('[test3]'), undef, 'no result'; -is $dom->at('[test=""]')->tag, 'div', 'right tag'; -is $dom->at('[test2=""]')->tag, 'div', 'right tag'; -is $dom->at('[test3=""]'), undef, 'no result'; - -# Multi-line attribute -$dom = DOM::Tiny->new(qq{
}); -is $dom->at('div')->attr->{class}, "line1\nline2", 'multi-line attribute value'; -is $dom->at('.line1')->tag, 'div', 'right tag'; -is $dom->at('.line2')->tag, 'div', 'right tag'; -is $dom->at('.line3'), undef, 'no result'; - -# Whitespaces before closing bracket -$dom = DOM::Tiny->new('
content
'); -ok $dom->at('div'), 'tag found'; -is $dom->at('div')->text, 'content', 'right text'; -is $dom->at('div')->content, 'content', 'right text'; - -# Class with hyphen -$dom = DOM::Tiny->new('
A
A1
'); -@div = (); -$dom->find('.a')->each(sub { push @div, shift->text }); -is_deeply \@div, ['A'], 'found first element only'; -@div = (); -$dom->find('.a-1')->each(sub { push @div, shift->text }); -is_deeply \@div, ['A1'], 'found last element only'; - -# Defined but false text -$dom = DOM::Tiny->new( - '
A
B
0
'); -@div = (); -$dom->find('div[id]')->each(sub { push @div, shift->text }); -is_deeply \@div, [qw(A B 0)], 'found all div elements with id'; - -# Empty tags -$dom = DOM::Tiny->new('



'); -is "$dom", '



', 'right result'; -is $dom->at('br')->content, '', 'empty result'; - -# Inner XML -$dom = DOM::Tiny->new('
xxxxxxx'); -is $dom->at('a')->content, 'xxxxxxx', 'right result'; -is $dom->content, 'xxxxxxx', 'right result'; - -# Multiple selectors -$dom = DOM::Tiny->new( - '
A
B
C

D

'); -@div = (); -$dom->find('p, div')->each(sub { push @div, shift->text }); -is_deeply \@div, [qw(A B C D)], 'found all elements'; -@div = (); -$dom->find('#a, #c')->each(sub { push @div, shift->text }); -is_deeply \@div, [qw(A C)], 'found all div elements with the right ids'; -@div = (); -$dom->find('div#a, div#b')->each(sub { push @div, shift->text }); -is_deeply \@div, [qw(A B)], 'found all div elements with the right ids'; -@div = (); -$dom->find('div[id="a"], div[id="c"]')->each(sub { push @div, shift->text }); -is_deeply \@div, [qw(A C)], 'found all div elements with the right ids'; -$dom = DOM::Tiny->new( - '
A
B
C
'); -@div = (); -$dom->find('#☃, #♥x')->each(sub { push @div, shift->text }); -is_deeply \@div, [qw(A C)], 'found all div elements with the right ids'; -@div = (); -$dom->find('div#☃, div#b')->each(sub { push @div, shift->text }); -is_deeply \@div, [qw(A B)], 'found all div elements with the right ids'; -@div = (); -$dom->find('div[id="☃"], div[id="♥x"]') - ->each(sub { push @div, shift->text }); -is_deeply \@div, [qw(A C)], 'found all div elements with the right ids'; - -# Multiple attributes -$dom = DOM::Tiny->new(<A
-
B
-
C
-
D
-EOF -@div = (); -$dom->find('div[foo="bar"][bar="baz"]')->each(sub { push @div, shift->text }); -is_deeply \@div, [qw(A C)], 'found all div elements with the right atributes'; -@div = (); -$dom->find('div[foo^="b"][foo$="r"]')->each(sub { push @div, shift->text }); -is_deeply \@div, [qw(A B C)], 'found all div elements with the right atributes'; -is $dom->at('[foo="bar"]')->previous, undef, 'no previous sibling'; -is $dom->at('[foo="bar"]')->next->text, 'B', 'right text'; -is $dom->at('[foo="bar"]')->next->previous->text, 'A', 'right text'; -is $dom->at('[foo="bar"]')->next->next->next->next, undef, 'no next sibling'; - -# Pseudo-classes -$dom = DOM::Tiny->new(< - - - - - -

test 123

-

- -EOF -is $dom->find(':root')->[0]->tag, 'form', 'right tag'; -is $dom->find('*:root')->[0]->tag, 'form', 'right tag'; -is $dom->find('form:root')->[0]->tag, 'form', 'right tag'; -is $dom->find(':root')->[1], undef, 'no result'; -is $dom->find(':checked')->[0]->attr->{name}, 'groovy', 'right name'; -is $dom->find('option:checked')->[0]->attr->{value}, 'e', 'right value'; -is $dom->find(':checked')->[1]->text, 'E', 'right text'; -is $dom->find('*:checked')->[1]->text, 'E', 'right text'; -is $dom->find(':checked')->[2]->text, 'H', 'right name'; -is $dom->find(':checked')->[3]->attr->{name}, 'I', 'right name'; -is $dom->find(':checked')->[4], undef, 'no result'; -is $dom->find('option[selected]')->[0]->attr->{value}, 'e', 'right value'; -is $dom->find('option[selected]')->[1]->text, 'H', 'right text'; -is $dom->find('option[selected]')->[2], undef, 'no result'; -is $dom->find(':checked[value="e"]')->[0]->text, 'E', 'right text'; -is $dom->find('*:checked[value="e"]')->[0]->text, 'E', 'right text'; -is $dom->find('option:checked[value="e"]')->[0]->text, 'E', 'right text'; -is $dom->at('optgroup option:checked[value="e"]')->text, 'E', 'right text'; -is $dom->at('select option:checked[value="e"]')->text, 'E', 'right text'; -is $dom->at('select :checked[value="e"]')->text, 'E', 'right text'; -is $dom->at('optgroup > :checked[value="e"]')->text, 'E', 'right text'; -is $dom->at('select *:checked[value="e"]')->text, 'E', 'right text'; -is $dom->at('optgroup > *:checked[value="e"]')->text, 'E', 'right text'; -is $dom->find(':checked[value="e"]')->[1], undef, 'no result'; -is $dom->find(':empty')->[0]->attr->{name}, 'user', 'right name'; -is $dom->find('input:empty')->[0]->attr->{name}, 'user', 'right name'; -is $dom->at(':empty[type^="ch"]')->attr->{name}, 'groovy', 'right name'; -is $dom->at('p')->attr->{id}, 'content', 'right attribute'; -is $dom->at('p:empty')->attr->{id}, 'no_content', 'right attribute'; - -# More pseudo-classes -$dom = DOM::Tiny->new(< -
  • A
  • -
  • B
  • -
  • C
  • -
  • D
  • -
  • E
  • -
  • F
  • -
  • G
  • -
  • H
  • - -EOF -my @li; -$dom->find('li:nth-child(odd)')->each(sub { push @li, shift->text }); -is_deeply \@li, [qw(A C E G)], 'found all odd li elements'; -@li = (); -$dom->find('li:NTH-CHILD(ODD)')->each(sub { push @li, shift->text }); -is_deeply \@li, [qw(A C E G)], 'found all odd li elements'; -@li = (); -$dom->find('li:nth-last-child(odd)')->each(sub { push @li, shift->text }); -is_deeply \@li, [qw(B D F H)], 'found all odd li elements'; -is $dom->find(':nth-child(odd)')->[0]->tag, 'ul', 'right tag'; -is $dom->find(':nth-child(odd)')->[1]->text, 'A', 'right text'; -is $dom->find(':nth-child(1)')->[0]->tag, 'ul', 'right tag'; -is $dom->find(':nth-child(1)')->[1]->text, 'A', 'right text'; -is $dom->find(':nth-last-child(odd)')->[0]->tag, 'ul', 'right tag'; -is $dom->find(':nth-last-child(odd)')->last->text, 'H', 'right text'; -is $dom->find(':nth-last-child(1)')->[0]->tag, 'ul', 'right tag'; -is $dom->find(':nth-last-child(1)')->[1]->text, 'H', 'right text'; -@li = (); -$dom->find('li:nth-child(2n+1)')->each(sub { push @li, shift->text }); -is_deeply \@li, [qw(A C E G)], 'found all odd li elements'; -@li = (); -$dom->find('li:nth-child(2n + 1)')->each(sub { push @li, shift->text }); -is_deeply \@li, [qw(A C E G)], 'found all odd li elements'; -@li = (); -$dom->find('li:nth-last-child(2n+1)')->each(sub { push @li, shift->text }); -is_deeply \@li, [qw(B D F H)], 'found all odd li elements'; -@li = (); -$dom->find('li:nth-child(even)')->each(sub { push @li, shift->text }); -is_deeply \@li, [qw(B D F H)], 'found all even li elements'; -@li = (); -$dom->find('li:NTH-CHILD(EVEN)')->each(sub { push @li, shift->text }); -is_deeply \@li, [qw(B D F H)], 'found all even li elements'; -@li = (); -$dom->find('li:nth-last-child( even )')->each(sub { push @li, shift->text }); -is_deeply \@li, [qw(A C E G)], 'found all even li elements'; -@li = (); -$dom->find('li:nth-child(2n+2)')->each(sub { push @li, shift->text }); -is_deeply \@li, [qw(B D F H)], 'found all even li elements'; -@li = (); -$dom->find('li:nTh-chILd(2N+2)')->each(sub { push @li, shift->text }); -is_deeply \@li, [qw(B D F H)], 'found all even li elements'; -@li = (); -$dom->find('li:nth-child( 2n + 2 )')->each(sub { push @li, shift->text }); -is_deeply \@li, [qw(B D F H)], 'found all even li elements'; -@li = (); -$dom->find('li:nth-last-child(2n+2)')->each(sub { push @li, shift->text }); -is_deeply \@li, [qw(A C E G)], 'found all even li elements'; -@li = (); -$dom->find('li:nth-child(4n+1)')->each(sub { push @li, shift->text }); -is_deeply \@li, [qw(A E)], 'found the right li elements'; -@li = (); -$dom->find('li:nth-last-child(4n+1)')->each(sub { push @li, shift->text }); -is_deeply \@li, [qw(D H)], 'found the right li elements'; -@li = (); -$dom->find('li:nth-child(4n+4)')->each(sub { push @li, shift->text }); -is_deeply \@li, [qw(D H)], 'found the right li elements'; -@li = (); -$dom->find('li:nth-last-child(4n+4)')->each(sub { push @li, shift->text }); -is_deeply \@li, [qw(A E)], 'found the right li elements'; -@li = (); -$dom->find('li:nth-child(4n)')->each(sub { push @li, shift->text }); -is_deeply \@li, [qw(D H)], 'found the right li elements'; -@li = (); -$dom->find('li:nth-child( 4n )')->each(sub { push @li, shift->text }); -is_deeply \@li, [qw(D H)], 'found the right li elements'; -@li = (); -$dom->find('li:nth-last-child(4n)')->each(sub { push @li, shift->text }); -is_deeply \@li, [qw(A E)], 'found the right li elements'; -@li = (); -$dom->find('li:nth-child(5n-2)')->each(sub { push @li, shift->text }); -is_deeply \@li, [qw(C H)], 'found the right li elements'; -@li = (); -$dom->find('li:nth-child( 5n - 2 )')->each(sub { push @li, shift->text }); -is_deeply \@li, [qw(C H)], 'found the right li elements'; -@li = (); -$dom->find('li:nth-last-child(5n-2)')->each(sub { push @li, shift->text }); -is_deeply \@li, [qw(A F)], 'found the right li elements'; -@li = (); -$dom->find('li:nth-child(-n+3)')->each(sub { push @li, shift->text }); -is_deeply \@li, [qw(A B C)], 'found first three li elements'; -@li = (); -$dom->find('li:nth-child( -n + 3 )')->each(sub { push @li, shift->text }); -is_deeply \@li, [qw(A B C)], 'found first three li elements'; -@li = (); -$dom->find('li:nth-last-child(-n+3)')->each(sub { push @li, shift->text }); -is_deeply \@li, [qw(F G H)], 'found last three li elements'; -@li = (); -$dom->find('li:nth-child(-1n+3)')->each(sub { push @li, shift->text }); -is_deeply \@li, [qw(A B C)], 'found first three li elements'; -@li = (); -$dom->find('li:nth-last-child(-1n+3)')->each(sub { push @li, shift->text }); -is_deeply \@li, [qw(F G H)], 'found first three li elements'; -@li = (); -$dom->find('li:nth-child(3n)')->each(sub { push @li, shift->text }); -is_deeply \@li, [qw(C F)], 'found every third li elements'; -@li = (); -$dom->find('li:nth-last-child(3n)')->each(sub { push @li, shift->text }); -is_deeply \@li, [qw(C F)], 'found every third li elements'; -@li = (); -$dom->find('li:NTH-LAST-CHILD(3N)')->each(sub { push @li, shift->text }); -is_deeply \@li, [qw(C F)], 'found every third li elements'; -@li = (); -$dom->find('li:Nth-Last-Child(3N)')->each(sub { push @li, shift->text }); -is_deeply \@li, [qw(C F)], 'found every third li elements'; -@li = (); -$dom->find('li:nth-child( 3 )')->each(sub { push @li, shift->text }); -is_deeply \@li, ['C'], 'found third li element'; -@li = (); -$dom->find('li:nth-last-child( +3 )')->each(sub { push @li, shift->text }); -is_deeply \@li, ['F'], 'found third last li element'; -@li = (); -$dom->find('li:nth-child(1n+0)')->each(sub { push @li, shift->text }); -is_deeply \@li, [qw(A B C D E F G)], 'found all li elements'; -@li = (); -$dom->find('li:nth-child(1n-0)')->each(sub { push @li, shift->text }); -is_deeply \@li, [qw(A B C D E F G)], 'found all li elements'; -@li = (); -$dom->find('li:nth-child(n+0)')->each(sub { push @li, shift->text }); -is_deeply \@li, [qw(A B C D E F G)], 'found all li elements'; -@li = (); -$dom->find('li:nth-child(n)')->each(sub { push @li, shift->text }); -is_deeply \@li, [qw(A B C D E F G)], 'found all li elements'; -@li = (); -$dom->find('li:nth-child(n+0)')->each(sub { push @li, shift->text }); -is_deeply \@li, [qw(A B C D E F G)], 'found all li elements'; -@li = (); -$dom->find('li:NTH-CHILD(N+0)')->each(sub { push @li, shift->text }); -is_deeply \@li, [qw(A B C D E F G)], 'found all li elements'; -@li = (); -$dom->find('li:Nth-Child(N+0)')->each(sub { push @li, shift->text }); -is_deeply \@li, [qw(A B C D E F G)], 'found all li elements'; -@li = (); -$dom->find('li:nth-child(n)')->each(sub { push @li, shift->text }); -is_deeply \@li, [qw(A B C D E F G)], 'found all li elements'; -@li = (); -$dom->find('li:nth-child(0n+1)')->each(sub { push @li, shift->text }); -is_deeply \@li, [qw(A)], 'found first li element'; -is $dom->find('li:nth-child(0n+0)')->size, 0, 'no results'; -is $dom->find('li:nth-child(0)')->size, 0, 'no results'; -is $dom->find('li:nth-child()')->size, 0, 'no results'; -is $dom->find('li:nth-child(whatever)')->size, 0, 'no results'; -is $dom->find('li:whatever(whatever)')->size, 0, 'no results'; - -# Even more pseudo-classes -$dom = DOM::Tiny->new(< -
  • A
  • -

    B

    -
  • C
  • -

    D

    -
  • E
  • -
  • F
  • -

    G

    -
  • H
  • -
  • I
  • - -
    -
    J
    -
    -
    - DOM -
    K
    - DOM -
    -EOF -my @e; -$dom->find('ul :nth-child(odd)')->each(sub { push @e, shift->text }); -is_deeply \@e, [qw(A C E G I)], 'found all odd elements'; -@e = (); -$dom->find('li:nth-of-type(odd)')->each(sub { push @e, shift->text }); -is_deeply \@e, [qw(A E H)], 'found all odd li elements'; -@e = (); -$dom->find('li:nth-last-of-type( odd )')->each(sub { push @e, shift->text }); -is_deeply \@e, [qw(C F I)], 'found all odd li elements'; -@e = (); -$dom->find('p:nth-of-type(odd)')->each(sub { push @e, shift->text }); -is_deeply \@e, [qw(B G)], 'found all odd p elements'; -@e = (); -$dom->find('p:nth-last-of-type(odd)')->each(sub { push @e, shift->text }); -is_deeply \@e, [qw(B G)], 'found all odd li elements'; -@e = (); -$dom->find('ul :nth-child(1)')->each(sub { push @e, shift->text }); -is_deeply \@e, ['A'], 'found first child'; -@e = (); -$dom->find('ul :first-child')->each(sub { push @e, shift->text }); -is_deeply \@e, ['A'], 'found first child'; -@e = (); -$dom->find('p:nth-of-type(1)')->each(sub { push @e, shift->text }); -is_deeply \@e, ['B'], 'found first child'; -@e = (); -$dom->find('p:first-of-type')->each(sub { push @e, shift->text }); -is_deeply \@e, ['B'], 'found first child'; -@e = (); -$dom->find('li:nth-of-type(1)')->each(sub { push @e, shift->text }); -is_deeply \@e, ['A'], 'found first child'; -@e = (); -$dom->find('li:first-of-type')->each(sub { push @e, shift->text }); -is_deeply \@e, ['A'], 'found first child'; -@e = (); -$dom->find('ul :nth-last-child(-n+1)')->each(sub { push @e, shift->text }); -is_deeply \@e, ['I'], 'found last child'; -@e = (); -$dom->find('ul :last-child')->each(sub { push @e, shift->text }); -is_deeply \@e, ['I'], 'found last child'; -@e = (); -$dom->find('p:nth-last-of-type(-n+1)')->each(sub { push @e, shift->text }); -is_deeply \@e, ['G'], 'found last child'; -@e = (); -$dom->find('p:last-of-type')->each(sub { push @e, shift->text }); -is_deeply \@e, ['G'], 'found last child'; -@e = (); -$dom->find('li:nth-last-of-type(-n+1)')->each(sub { push @e, shift->text }); -is_deeply \@e, ['I'], 'found last child'; -@e = (); -$dom->find('li:last-of-type')->each(sub { push @e, shift->text }); -is_deeply \@e, ['I'], 'found last child'; -@e = (); -$dom->find('ul :nth-child(-n+3):not(li)')->each(sub { push @e, shift->text }); -is_deeply \@e, ['B'], 'found first p element'; -@e = (); -$dom->find('ul :nth-child(-n+3):NOT(li)')->each(sub { push @e, shift->text }); -is_deeply \@e, ['B'], 'found first p element'; -@e = (); -$dom->find('ul :nth-child(-n+3):not(:first-child)') - ->each(sub { push @e, shift->text }); -is_deeply \@e, [qw(B C)], 'found second and third element'; -@e = (); -$dom->find('ul :nth-child(-n+3):not(.♥)')->each(sub { push @e, shift->text }); -is_deeply \@e, [qw(A B)], 'found first and second element'; -@e = (); -$dom->find('ul :nth-child(-n+3):not([class$="♥"])') - ->each(sub { push @e, shift->text }); -is_deeply \@e, [qw(A B)], 'found first and second element'; -@e = (); -$dom->find('ul :nth-child(-n+3):not(li[class$="♥"])') - ->each(sub { push @e, shift->text }); -is_deeply \@e, [qw(A B)], 'found first and second element'; -@e = (); -$dom->find('ul :nth-child(-n+3):not([class$="♥"][class^="test"])') - ->each(sub { push @e, shift->text }); -is_deeply \@e, [qw(A B)], 'found first and second element'; -@e = (); -$dom->find('ul :nth-child(-n+3):not(*[class$="♥"])') - ->each(sub { push @e, shift->text }); -is_deeply \@e, [qw(A B)], 'found first and second element'; -@e = (); -$dom->find('ul :nth-child(-n+3):not(:nth-child(-n+2))') - ->each(sub { push @e, shift->text }); -is_deeply \@e, ['C'], 'found third element'; -@e = (); -$dom->find('ul :nth-child(-n+3):not(:nth-child(1)):not(:nth-child(2))') - ->each(sub { push @e, shift->text }); -is_deeply \@e, ['C'], 'found third element'; -@e = (); -$dom->find(':only-child')->each(sub { push @e, shift->text }); -is_deeply \@e, ['J'], 'found only child'; -@e = (); -$dom->find('div :only-of-type')->each(sub { push @e, shift->text }); -is_deeply \@e, [qw(J K)], 'found only child'; -@e = (); -$dom->find('div:only-child')->each(sub { push @e, shift->text }); -is_deeply \@e, ['J'], 'found only child'; -@e = (); -$dom->find('div div:only-of-type')->each(sub { push @e, shift->text }); -is_deeply \@e, [qw(J K)], 'found only child'; - -# Sibling combinator -$dom = DOM::Tiny->new(< -
  • A
  • -

    B

    -
  • C
  • - -

    D

    -

    E

    -

    FH

    -
    G
    -EOF -is $dom->at('li ~ p')->text, 'B', 'right text'; -is $dom->at('li + p')->text, 'B', 'right text'; -is $dom->at('h1 ~ p ~ p')->text, 'F', 'right text'; -is $dom->at('h1 + p ~ p')->text, 'F', 'right text'; -is $dom->at('h1 ~ p + p')->text, 'F', 'right text'; -is $dom->at('h1 + p + p')->text, 'F', 'right text'; -is $dom->at('h1 + p+p')->text, 'F', 'right text'; -is $dom->at('ul > li ~ li')->text, 'C', 'right text'; -is $dom->at('ul li ~ li')->text, 'C', 'right text'; -is $dom->at('ul>li~li')->text, 'C', 'right text'; -is $dom->at('ul li li'), undef, 'no result'; -is $dom->at('ul ~ li ~ li'), undef, 'no result'; -is $dom->at('ul + li ~ li'), undef, 'no result'; -is $dom->at('ul > li + li'), undef, 'no result'; -is $dom->at('h1 ~ div')->text, 'G', 'right text'; -is $dom->at('h1 + div'), undef, 'no result'; -is $dom->at('p + div')->text, 'G', 'right text'; -is $dom->at('ul + h1 + p + p + div')->text, 'G', 'right text'; -is $dom->at('ul + h1 ~ p + div')->text, 'G', 'right text'; -is $dom->at('h1 ~ #♥')->text, 'E', 'right text'; -is $dom->at('h1 + #♥')->text, 'E', 'right text'; -is $dom->at('#♥~#☃')->text, 'F', 'right text'; -is $dom->at('#♥+#☃')->text, 'F', 'right text'; -is $dom->at('#♥+#☃>b')->text, 'H', 'right text'; -is $dom->at('#♥ > #☃'), undef, 'no result'; -is $dom->at('#♥ #☃'), undef, 'no result'; -is $dom->at('#♥ + #☃ + :nth-last-child(1)')->text, 'G', 'right text'; -is $dom->at('#♥ ~ #☃ + :nth-last-child(1)')->text, 'G', 'right text'; -is $dom->at('#♥ + #☃ ~ :nth-last-child(1)')->text, 'G', 'right text'; -is $dom->at('#♥ ~ #☃ ~ :nth-last-child(1)')->text, 'G', 'right text'; -is $dom->at('#♥ + :nth-last-child(2)')->text, 'F', 'right text'; -is $dom->at('#♥ ~ :nth-last-child(2)')->text, 'F', 'right text'; -is $dom->at('#♥ + #☃ + *:nth-last-child(1)')->text, 'G', 'right text'; -is $dom->at('#♥ ~ #☃ + *:nth-last-child(1)')->text, 'G', 'right text'; -is $dom->at('#♥ + #☃ ~ *:nth-last-child(1)')->text, 'G', 'right text'; -is $dom->at('#♥ ~ #☃ ~ *:nth-last-child(1)')->text, 'G', 'right text'; -is $dom->at('#♥ + *:nth-last-child(2)')->text, 'F', 'right text'; -is $dom->at('#♥ ~ *:nth-last-child(2)')->text, 'F', 'right text'; - -# Adding nodes -$dom = DOM::Tiny->new(< -
  • A
  • -

    B

    -
  • C
  • - -
    D
    -EOF -$dom->at('li')->append('

    A1

    23'); -is "$dom", < -
  • A
  • A1

    23 -

    B

    -
  • C
  • - -
    D
    -EOF -$dom->at('li')->prepend('24')->prepend('
    A-1
    25'); -is "$dom", < - 24
    A-1
    25
  • A
  • A1

    23 -

    B

    -
  • C
  • - -
    D
    -EOF -is $dom->at('div')->text, 'A-1', 'right text'; -is $dom->at('iv'), undef, 'no result'; -is $dom->prepend('l')->prepend('alal')->prepend('a')->type, 'root', - 'right type'; -is "$dom", < - 24
    A-1
    25
  • A
  • A1

    23 -

    B

    -
  • C
  • - -
    D
    -EOF -is $dom->append('lalala')->type, 'root', 'right type'; -is "$dom", < - 24
    A-1
    25
  • A
  • A1

    23 -

    B

    -
  • C
  • - -
    D
    -EOF -$dom->find('div')->each(sub { shift->append('works') }); -is "$dom", < - 24
    A-1
    works25
  • A
  • A1

    23 -

    B

    -
  • C
  • - -
    D
    works -EOF -$dom->at('li')->prepend_content('A3

    A2

    ')->prepend_content('A4'); -is $dom->at('li')->text, 'A4A3 A', 'right text'; -is "$dom", < - 24
    A-1
    works25
  • A4A3

    A2

    A
  • A1

    23 -

    B

    -
  • C
  • - -
    D
    works -EOF -$dom->find('li')->[1]->append_content('

    C2

    C3')->append_content(' C4') - ->append_content('C5'); -is $dom->find('li')->[1]->text, 'C C3 C4C5', 'right text'; -is "$dom", < - 24
    A-1
    works25
  • A4A3

    A2

    A
  • A1

    23 -

    B

    -
  • C

    C2

    C3 C4C5
  • - -
    D
    works -EOF - -# Optional "head" and "body" tags -$dom = DOM::Tiny->new(< - - foo - bar -EOF -is $dom->at('html > head > title')->text, 'foo', 'right text'; -is $dom->at('html > body')->text, 'bar', 'right text'; - -# Optional "li" tag -$dom = DOM::Tiny->new(< -
  • -
      -
    1. F -
    2. G -
    -
  • A
  • -
  • B -
  • C
  • -
  • D -
  • E - -EOF -is $dom->find('ul > li > ol > li')->[0]->text, 'F', 'right text'; -is $dom->find('ul > li > ol > li')->[1]->text, 'G', 'right text'; -is $dom->find('ul > li')->[1]->text, 'A', 'right text'; -is $dom->find('ul > li')->[2]->text, 'B', 'right text'; -is $dom->find('ul > li')->[3]->text, 'C', 'right text'; -is $dom->find('ul > li')->[4]->text, 'D', 'right text'; -is $dom->find('ul > li')->[5]->text, 'E', 'right text'; - -# Optional "p" tag -$dom = DOM::Tiny->new(< -

    A

    -

    B -

    C

    -

    D

    X
    -

    E -

    F
    G -

    H -

  • -EOF -is $dom->find('div > p')->[0]->text, 'A', 'right text'; -is $dom->find('div > p')->[1]->text, 'B', 'right text'; -is $dom->find('div > p')->[2]->text, 'C', 'right text'; -is $dom->find('div > p')->[3]->text, 'D', 'right text'; -is $dom->find('div > p')->[4]->text, 'E', 'right text'; -is $dom->find('div > p')->[5]->text, 'F G', 'right text'; -is $dom->find('div > p')->[6]->text, 'H', 'right text'; -is $dom->find('div > p > p')->[0], undef, 'no results'; -is $dom->at('div > p > img')->attr->{src}, 'foo.png', 'right attribute'; -is $dom->at('div > div')->text, 'X', 'right text'; - -# Optional "dt" and "dd" tags -$dom = DOM::Tiny->new(< -
    A
    -
    B -
    C
    -
    D -
    E -
    F - -EOF -is $dom->find('dl > dt')->[0]->text, 'A', 'right text'; -is $dom->find('dl > dd')->[0]->text, 'B', 'right text'; -is $dom->find('dl > dt')->[1]->text, 'C', 'right text'; -is $dom->find('dl > dd')->[1]->text, 'D', 'right text'; -is $dom->find('dl > dt')->[2]->text, 'E', 'right text'; -is $dom->find('dl > dd')->[2]->text, 'F', 'right text'; - -# Optional "rp" and "rt" tags -$dom = DOM::Tiny->new(< - A - B - C - D - E - F - -EOF -is $dom->find('ruby > rp')->[0]->text, 'A', 'right text'; -is $dom->find('ruby > rt')->[0]->text, 'B', 'right text'; -is $dom->find('ruby > rp')->[1]->text, 'C', 'right text'; -is $dom->find('ruby > rt')->[1]->text, 'D', 'right text'; -is $dom->find('ruby > rp')->[2]->text, 'E', 'right text'; -is $dom->find('ruby > rt')->[2]->text, 'F', 'right text'; - -# Optional "optgroup" and "option" tags -$dom = DOM::Tiny->new(< - A - - E - G -
    -EOF -is $dom->find('div > optgroup')->[0]->text, 'A', 'right text'; -is $dom->find('div > optgroup > #foo')->[0]->text, 'B', 'right text'; -is $dom->find('div > optgroup > option')->[1]->text, 'C', 'right text'; -is $dom->find('div > optgroup > option')->[2]->text, 'D', 'right text'; -is $dom->find('div > optgroup')->[1]->text, 'E', 'right text'; -is $dom->find('div > optgroup > option')->[3]->text, 'F', 'right text'; -is $dom->find('div > optgroup')->[2]->text, 'G', 'right text'; -is $dom->find('div > optgroup > option')->[4]->text, 'H', 'right text'; - -# Optional "colgroup" tag -$dom = DOM::Tiny->new(< - - - - - - - - -EOF -is $dom->find('table > col')->[0]->attr->{id}, 'morefail', 'right attribute'; -is $dom->find('table > col')->[1]->attr->{id}, 'fail', 'right attribute'; -is $dom->find('table > colgroup > col')->[0]->attr->{id}, 'foo', - 'right attribute'; -is $dom->find('table > colgroup > col')->[1]->attr->{class}, 'foo', - 'right attribute'; -is $dom->find('table > colgroup > col')->[2]->attr->{id}, 'bar', - 'right attribute'; - -# Optional "thead", "tbody", "tfoot", "tr", "th" and "td" tags -$dom = DOM::Tiny->new(< - - - A - D - - - C - - - B - -EOF -is $dom->at('table > thead > tr > th')->text, 'A', 'right text'; -is $dom->find('table > thead > tr > th')->[1]->text, 'D', 'right text'; -is $dom->at('table > tbody > tr > td')->text, 'B', 'right text'; -is $dom->at('table > tfoot > tr > td')->text, 'C', 'right text'; - -# Optional "colgroup", "thead", "tbody", "tr", "th" and "td" tags -$dom = DOM::Tiny->new(< - - - - - - - - - - - A - D - - - B - - - E - -EOF -is $dom->find('table > col')->[0]->attr->{id}, 'morefail', 'right attribute'; -is $dom->find('table > col')->[1]->attr->{id}, 'fail', 'right attribute'; -is $dom->find('table > colgroup > col')->[0]->attr->{id}, 'foo', - 'right attribute'; -is $dom->find('table > colgroup > col')->[1]->attr->{class}, 'foo', - 'right attribute'; -is $dom->find('table > colgroup > col')->[2]->attr->{id}, 'bar', - 'right attribute'; -is $dom->at('table > thead > tr > th')->text, 'A', 'right text'; -is $dom->find('table > thead > tr > th')->[1]->text, 'D', 'right text'; -is $dom->at('table > tbody > tr > td')->text, 'B', 'right text'; -is $dom->find('table > tbody > tr > td')->map('text')->join("\n"), "B\nE", - 'right text'; - -# Optional "colgroup", "tbody", "tr", "th" and "td" tags -$dom = DOM::Tiny->new(< - - - - - - - - - B - -EOF -is $dom->find('table > colgroup > col')->[0]->attr->{id}, 'foo', - 'right attribute'; -is $dom->find('table > colgroup > col')->[1]->attr->{class}, 'foo', - 'right attribute'; -is $dom->find('table > colgroup > col')->[2]->attr->{id}, 'bar', - 'right attribute'; -is $dom->at('table > tbody > tr > td')->text, 'B', 'right text'; - -# Optional "tr" and "td" tags -$dom = DOM::Tiny->new(< - - A - B - - C - - - D - -EOF -is $dom->find('table > tr > td')->[0]->text, 'A', 'right text'; -is $dom->find('table > tr > td')->[1]->text, 'B', 'right text'; -is $dom->find('table > tr > td')->[2]->text, 'C', 'right text'; -is $dom->find('table > tr > td')->[3]->text, 'D', 'right text'; - -# Real world table -$dom = DOM::Tiny->new(< - - Real World! - -

    Just a test - - - - - - -
    One - Two - Three - Four -
    Alpha - Beta - Gamma - Delta -
    Alpha Two - Beta Two - Gamma Two - Delta Two -
    -EOF -is $dom->find('html > head > title')->[0]->text, 'Real World!', 'right text'; -is $dom->find('html > body > p')->[0]->text, 'Just a test', 'right text'; -is $dom->find('p')->[0]->text, 'Just a test', 'right text'; -is $dom->find('thead > tr > .three')->[0]->text, 'Three', 'right text'; -is $dom->find('thead > tr > .four')->[0]->text, 'Four', 'right text'; -is $dom->find('tbody > tr > .beta')->[0]->text, 'Beta', 'right text'; -is $dom->find('tbody > tr > .gamma')->[0]->text, '', 'no text'; -is $dom->find('tbody > tr > .gamma > a')->[0]->text, 'Gamma', 'right text'; -is $dom->find('tbody > tr > .alpha')->[1]->text, 'Alpha Two', 'right text'; -is $dom->find('tbody > tr > .gamma > a')->[1]->text, 'Gamma Two', 'right text'; -my @following - = $dom->find('tr > td:nth-child(1)')->map(following => ':nth-child(even)') - ->flatten->map('all_text')->each; -is_deeply \@following, ['Beta', 'Delta', 'Beta Two', 'Delta Two'], - 'right results'; - -# Real world list -$dom = DOM::Tiny->new(< - - Real World! - -

      -
    • - Test -
      - 123 -

      - -

    • - Test -
      - 321 -

      -

    • - Test - 3 - 2 - 1 -

      -

    -EOF -is $dom->find('html > head > title')->[0]->text, 'Real World!', 'right text'; -is $dom->find('body > ul > li')->[0]->text, 'Test 123', 'right text'; -is $dom->find('body > ul > li > p')->[0]->text, '', 'no text'; -is $dom->find('body > ul > li')->[1]->text, 'Test 321', 'right text'; -is $dom->find('body > ul > li > p')->[1]->text, '', 'no text'; -is $dom->find('body > ul > li')->[1]->all_text, 'Test 321', 'right text'; -is $dom->find('body > ul > li > p')->[1]->all_text, '', 'no text'; -is $dom->find('body > ul > li')->[2]->text, 'Test 3 2 1', 'right text'; -is $dom->find('body > ul > li > p')->[2]->text, '', 'no text'; -is $dom->find('body > ul > li')->[2]->all_text, 'Test 3 2 1', 'right text'; -is $dom->find('body > ul > li > p')->[2]->all_text, '', 'no text'; - -# Advanced whitespace trimming (punctuation) -$dom = DOM::Tiny->new(< - - Real World! - -
    foo bar.
    -
    foo, barbaz; yada.
    -
    foo: barbaz? yada!
    -EOF -is $dom->find('html > head > title')->[0]->text, 'Real World!', 'right text'; -is $dom->find('body > div')->[0]->all_text, 'foo bar.', 'right text'; -is $dom->find('body > div')->[1]->all_text, 'foo, bar baz; yada.', 'right text'; -is $dom->find('body > div')->[1]->text, 'foo baz.', 'right text'; -is $dom->find('body > div')->[2]->all_text, 'foo: bar baz? yada!', 'right text'; -is $dom->find('body > div')->[2]->text, 'foo baz!', 'right text'; - -# Real world JavaScript and CSS -$dom = DOM::Tiny->new(< - - - - < sCriPt two="23" >if (b > c) { alert('&') }< / scRiPt > - Foo! -EOF -is $dom->find('html > body')->[0]->text, 'Foo!', 'right text'; -is $dom->find('html > head > style')->[0]->text, - "#style { foo: style(''); }", 'right text'; -is $dom->find('html > head > script')->[0]->text, - "\n if (a < b) {\n alert('<123>');\n }\n ", 'right text'; -is $dom->find('html > head > script')->[1]->text, - "if (b > c) { alert('&') }", 'right text'; - -# More real world JavaScript -$dom = DOM::Tiny->new(< - - - Foo - - - - - Bar - -EOF -is $dom->at('title')->text, 'Foo', 'right text'; -is $dom->find('html > head > script')->[0]->attr('src'), '/js/one.js', - 'right attribute'; -is $dom->find('html > head > script')->[1]->attr('src'), '/js/two.js', - 'right attribute'; -is $dom->find('html > head > script')->[2]->attr('src'), '/js/three.js', - 'right attribute'; -is $dom->find('html > head > script')->[2]->text, '', 'no text'; -is $dom->at('html > body')->text, 'Bar', 'right text'; - -# Even more real world JavaScript -$dom = DOM::Tiny->new(< - - - Foo - - - -EOF -is $dom->find('table > td > tr > thead')->[0]->text, 'foo', 'right text'; -is $dom->find('script > table > td > tr > thead')->[1]->text, 'bar', - 'right text'; -is $dom->find('table > td > tr > thead')->[2], undef, 'no result'; -is $dom->find('table > td > tr > thead')->size, 2, 'right number of elements'; - -# Ensure XML semantics again -$dom = DOM::Tiny->new->xml(1)->parse(<<'EOF'); - - foo - - bar - -
    -
    -
    -EOF -is $dom->find('table > td > tr > thead')->[0]->text, 'foo', 'right text'; -is $dom->find('table > td > tr > thead')->[1]->text, 'bar', 'right text'; -is $dom->find('table > td > tr > thead')->[2], undef, 'no result'; -is $dom->find('table > td > tr > thead')->size, 2, 'right number of elements'; - -# Nested tables -$dom = DOM::Tiny->new(<<'EOF'); - - - - -
    - - - - -
    baz
    -
    -EOF -is $dom->find('#foo > tr > td > #bar > tr >td')->[0]->text, 'baz', 'right text'; -is $dom->find('table > tr > td > table > tr >td')->[0]->text, 'baz', - 'right text'; - -# Nested find -$dom->parse(< - foo - - bar - - baz - - yada - - - - -EOF -my @results; -$dom->find('b')->each( - sub { - $_->find('a')->each(sub { push @results, $_->text }); - } -); -is_deeply \@results, [qw(bar baz yada)], 'right results'; -@results = (); -$dom->find('a')->each(sub { push @results, $_->text }); -is_deeply \@results, [qw(foo bar baz yada)], 'right results'; -@results = (); -$dom->find('b')->each( - sub { - $_->find('c a')->each(sub { push @results, $_->text }); - } -); -is_deeply \@results, [qw(baz yada)], 'right results'; -is $dom->at('b')->at('a')->text, 'bar', 'right text'; -is $dom->at('c > b > a')->text, 'bar', 'right text'; -is $dom->at('b')->at('c > b > a'), undef, 'no result'; - -# Direct hash access to attributes in XML mode -$dom = DOM::Tiny->new->xml(1)->parse(< - - foo - bar - baz - - -EOF -ok $dom->xml, 'XML mode active'; -is $dom->at('a')->{id}, 'one', 'right attribute'; -is_deeply [sort keys %{$dom->at('a')}], ['id'], 'right attributes'; -is $dom->at('a')->at('B')->text, 'foo', 'right text'; -is $dom->at('B')->{class}, 'two', 'right attribute'; -is_deeply [sort keys %{$dom->at('a B')}], [qw(class test)], 'right attributes'; -is $dom->find('a B c')->[0]->text, 'bar', 'right text'; -is $dom->find('a B c')->[0]{id}, 'three', 'right attribute'; -is_deeply [sort keys %{$dom->find('a B c')->[0]}], ['id'], 'right attributes'; -is $dom->find('a B c')->[1]->text, 'baz', 'right text'; -is $dom->find('a B c')->[1]{ID}, 'four', 'right attribute'; -is_deeply [sort keys %{$dom->find('a B c')->[1]}], ['ID'], 'right attributes'; -is $dom->find('a B c')->[2], undef, 'no result'; -is $dom->find('a B c')->size, 2, 'right number of elements'; -@results = (); -$dom->find('a B c')->each(sub { push @results, $_->text }); -is_deeply \@results, [qw(bar baz)], 'right results'; -is $dom->find('a B c')->join("\n"), - qq{bar\nbaz}, 'right result'; -is_deeply [keys %$dom], [], 'root has no attributes'; -is $dom->find('#nothing')->join, '', 'no result'; - -# Direct hash access to attributes in HTML mode -$dom = DOM::Tiny->new(< - - foo - bar - baz - - -EOF -ok !$dom->xml, 'XML mode not active'; -is $dom->at('a')->{id}, 'one', 'right attribute'; -is_deeply [sort keys %{$dom->at('a')}], ['id'], 'right attributes'; -is $dom->at('a')->at('b')->text, 'foo', 'right text'; -is $dom->at('b')->{class}, 'two', 'right attribute'; -is_deeply [sort keys %{$dom->at('a b')}], [qw(class test)], 'right attributes'; -is $dom->find('a b c')->[0]->text, 'bar', 'right text'; -is $dom->find('a b c')->[0]{id}, 'three', 'right attribute'; -is_deeply [sort keys %{$dom->find('a b c')->[0]}], ['id'], 'right attributes'; -is $dom->find('a b c')->[1]->text, 'baz', 'right text'; -is $dom->find('a b c')->[1]{id}, 'four', 'right attribute'; -is_deeply [sort keys %{$dom->find('a b c')->[1]}], ['id'], 'right attributes'; -is $dom->find('a b c')->[2], undef, 'no result'; -is $dom->find('a b c')->size, 2, 'right number of elements'; -@results = (); -$dom->find('a b c')->each(sub { push @results, $_->text }); -is_deeply \@results, [qw(bar baz)], 'right results'; -is $dom->find('a b c')->join("\n"), - qq{bar\nbaz}, 'right result'; -is_deeply [keys %$dom], [], 'root has no attributes'; -is $dom->find('#nothing')->join, '', 'no result'; - -# Append and prepend content -$dom = DOM::Tiny->new('Test'); -$dom->at('b')->append_content(''); -is $dom->children->[0]->tag, 'a', 'right tag'; -is $dom->all_text, 'Test', 'right text'; -is $dom->at('c')->parent->tag, 'b', 'right tag'; -is $dom->at('d')->parent->tag, 'b', 'right tag'; -$dom->at('b')->prepend_content('DOM'); -is $dom->at('e')->parent->tag, 'b', 'right tag'; -is $dom->all_text, 'DOM Test', 'right text'; - -# Wrap elements -$dom = DOM::Tiny->new('Test'); -is "$dom", 'Test', 'right result'; -is $dom->wrap('')->type, 'root', 'right type'; -is "$dom", 'Test', 'no changes'; -is $dom->at('a')->wrap('')->type, 'tag', 'right type'; -is "$dom", 'Test', 'right result'; -is $dom->at('b')->strip->at('a')->wrap('A')->tag, 'a', 'right tag'; -is "$dom", 'Test', 'right result'; -is $dom->at('a')->wrap('')->tag, 'a', 'right tag'; -is "$dom", 'Test', 'right result'; -is $dom->at('a')->wrap('CDEF')->parent->tag, 'd', - 'right tag'; -is "$dom", 'CDTestEF', 'right result'; - -# Wrap content -$dom = DOM::Tiny->new('Test'); -is $dom->at('a')->wrap_content('A')->tag, 'a', 'right tag'; -is "$dom", 'Test', 'right result'; -is $dom->wrap_content('')->type, 'root', 'right type'; -is "$dom", 'Test', 'right result'; -is $dom->at('b')->strip->at('a')->tag('e:a')->wrap_content('1') - ->tag, 'e:a', 'right tag'; -is "$dom", '1Test', 'right result'; -is $dom->at('a')->wrap_content('CDEF')->parent->type, - 'root', 'right type'; -is "$dom", 'CD1TestEF', - 'right result'; - -# Broken "div" in "td" -$dom = DOM::Tiny->new(< - -
    -
    - - -EOF -is $dom->find('table tr td')->[0]->at('div')->{id}, 'A', 'right attribute'; -is $dom->find('table tr td')->[1]->at('div')->{id}, 'B', 'right attribute'; -is $dom->find('table tr td')->[2], undef, 'no result'; -is $dom->find('table tr td')->size, 2, 'right number of elements'; -is "$dom", < - -
    -
    - - -EOF - -# Preformatted text -$dom = DOM::Tiny->new(< - looks -
    like
    -  it
    -    really
    -  
    - works -
    -EOF -is $dom->text, '', 'no text'; -is $dom->text(0), "\n", 'right text'; -is $dom->all_text, "looks like\n it\n really\n works", 'right text'; -is $dom->all_text(0), "\n looks\n like\n it\n really\n \n works\n\n", - 'right text'; -is $dom->at('div')->text, 'looks works', 'right text'; -is $dom->at('div')->text(0), "\n looks\n \n works\n", 'right text'; -is $dom->at('div')->all_text, "looks like\n it\n really\n works", - 'right text'; -is $dom->at('div')->all_text(0), - "\n looks\n like\n it\n really\n \n works\n", 'right text'; -is $dom->at('div pre')->text, "\n ", 'right text'; -is $dom->at('div pre')->text(0), "\n ", 'right text'; -is $dom->at('div pre')->all_text, "like\n it\n really\n ", 'right text'; -is $dom->at('div pre')->all_text(0), "like\n it\n really\n ", 'right text'; -is $dom->at('div pre code')->text, "like\n it\n really", 'right text'; -is $dom->at('div pre code')->text(0), "like\n it\n really", 'right text'; -is $dom->at('div pre code')->all_text, "like\n it\n really", 'right text'; -is $dom->at('div pre code')->all_text(0), "like\n it\n really", - 'right text'; - -# Form values -$dom = DOM::Tiny->new(< -

    Test

    - - - - - - - - - - - - - - - - -EOF -is $dom->at('p')->val, undef, 'no value'; -is $dom->at('input')->val, 'A', 'right value'; -is $dom->at('input:checked')->val, 'B', 'right value'; -is $dom->at('input:checked[type=radio]')->val, 'C', 'right value'; -is_deeply $dom->at('select')->val, ['I', 'J'], 'right values'; -is $dom->at('select option')->val, 'F', 'right value'; -is $dom->at('select optgroup option:not([selected])')->val, 'H', 'right value'; -is $dom->find('select')->[1]->at('option')->val, 'N', 'right value'; -is $dom->find('select')->[1]->val, undef, 'no value'; -is_deeply $dom->find('select')->[2]->val, undef, 'no value'; -is $dom->find('select')->[2]->at('option')->val, 'Q', 'right value'; -is_deeply $dom->find('select')->last->val, 'D', 'right value'; -is_deeply $dom->find('select')->last->at('option')->val, 'R', 'right value'; -is $dom->at('textarea')->val, 'M', 'right value'; -is $dom->at('button')->val, 'O', 'right value'; -is $dom->find('form input')->last->val, 'P', 'right value'; -is $dom->at('input[name=q]')->val, 'on', 'right value'; -is $dom->at('input[name=r]')->val, 'on', 'right value'; -is $dom->at('input[name=s]')->val, undef, 'no value'; -is $dom->at('input[name=t]')->val, '', 'right value'; -is $dom->at('input[name=u]')->val, undef, 'no value'; - -# PoCo example with whitespace sensitive text -$dom = DOM::Tiny->new(< - - - 1286823 - Homer Simpson - - home - - - - - 1286822 - Marge Simpson - - home - 742 Evergreen Terrace -Springfield, VT 12345 USA - - - -EOF -is $dom->find('entry')->[0]->at('displayName')->text, 'Homer Simpson', - 'right text'; -is $dom->find('entry')->[0]->at('id')->text, '1286823', 'right text'; -is $dom->find('entry')->[0]->at('addresses')->children('type')->[0]->text, - 'home', 'right text'; -is $dom->find('entry')->[0]->at('addresses formatted')->text, - "742 Evergreen Terrace\nSpringfield, VT 12345 USA", 'right text'; -is $dom->find('entry')->[0]->at('addresses formatted')->text(0), - "742 Evergreen Terrace\nSpringfield, VT 12345 USA", 'right text'; -is $dom->find('entry')->[1]->at('displayName')->text, 'Marge Simpson', - 'right text'; -is $dom->find('entry')->[1]->at('id')->text, '1286822', 'right text'; -is $dom->find('entry')->[1]->at('addresses')->children('type')->[0]->text, - 'home', 'right text'; -is $dom->find('entry')->[1]->at('addresses formatted')->text, - '742 Evergreen Terrace Springfield, VT 12345 USA', 'right text'; -is $dom->find('entry')->[1]->at('addresses formatted')->text(0), - "742 Evergreen Terrace\nSpringfield, VT 12345 USA", 'right text'; -is $dom->find('entry')->[2], undef, 'no result'; -is $dom->find('entry')->size, 2, 'right number of elements'; - -# Find attribute with hyphen in name and value -$dom = DOM::Tiny->new(< - - -EOF -is $dom->find('[http-equiv]')->[0]{content}, 'text/html', 'right attribute'; -is $dom->find('[http-equiv]')->[1], undef, 'no result'; -is $dom->find('[http-equiv="content-type"]')->[0]{content}, 'text/html', - 'right attribute'; -is $dom->find('[http-equiv="content-type"]')->[1], undef, 'no result'; -is $dom->find('[http-equiv^="content-"]')->[0]{content}, 'text/html', - 'right attribute'; -is $dom->find('[http-equiv^="content-"]')->[1], undef, 'no result'; -is $dom->find('head > [http-equiv$="-type"]')->[0]{content}, 'text/html', - 'right attribute'; -is $dom->find('head > [http-equiv$="-type"]')->[1], undef, 'no result'; - -# Find "0" attribute value -$dom = DOM::Tiny->new(<Zero -O&gTn>e -EOF -is $dom->find('a[accesskey]')->[0]->text, 'Zero', 'right text'; -is $dom->find('a[accesskey]')->[1]->text, 'O&gTn>e', 'right text'; -is $dom->find('a[accesskey]')->[2], undef, 'no result'; -is $dom->find('a[accesskey=0]')->[0]->text, 'Zero', 'right text'; -is $dom->find('a[accesskey=0]')->[1], undef, 'no result'; -is $dom->find('a[accesskey^=0]')->[0]->text, 'Zero', 'right text'; -is $dom->find('a[accesskey^=0]')->[1], undef, 'no result'; -is $dom->find('a[accesskey$=0]')->[0]->text, 'Zero', 'right text'; -is $dom->find('a[accesskey$=0]')->[1], undef, 'no result'; -is $dom->find('a[accesskey~=0]')->[0]->text, 'Zero', 'right text'; -is $dom->find('a[accesskey~=0]')->[1], undef, 'no result'; -is $dom->find('a[accesskey*=0]')->[0]->text, 'Zero', 'right text'; -is $dom->find('a[accesskey*=0]')->[1], undef, 'no result'; -is $dom->find('a[accesskey=1]')->[0]->text, 'O&gTn>e', 'right text'; -is $dom->find('a[accesskey=1]')->[1], undef, 'no result'; -is $dom->find('a[accesskey^=1]')->[0]->text, 'O&gTn>e', 'right text'; -is $dom->find('a[accesskey^=1]')->[1], undef, 'no result'; -is $dom->find('a[accesskey$=1]')->[0]->text, 'O&gTn>e', 'right text'; -is $dom->find('a[accesskey$=1]')->[1], undef, 'no result'; -is $dom->find('a[accesskey~=1]')->[0]->text, 'O&gTn>e', 'right text'; -is $dom->find('a[accesskey~=1]')->[1], undef, 'no result'; -is $dom->find('a[accesskey*=1]')->[0]->text, 'O&gTn>e', 'right text'; -is $dom->find('a[accesskey*=1]')->[1], undef, 'no result'; -is $dom->at('a[accesskey*="."]'), undef, 'no result'; - -# Empty attribute value -$dom = DOM::Tiny->new(< - test - -after -EOF -is $dom->tree->[0], 'root', 'right type'; -is $dom->tree->[1][0], 'tag', 'right type'; -is $dom->tree->[1][1], 'foo', 'right tag'; -is_deeply $dom->tree->[1][2], {bar => ''}, 'right attributes'; -is $dom->tree->[1][4][0], 'text', 'right type'; -is $dom->tree->[1][4][1], "\n test\n", 'right text'; -is $dom->tree->[3][0], 'tag', 'right type'; -is $dom->tree->[3][1], 'bar', 'right tag'; -is $dom->tree->[3][4][0], 'text', 'right type'; -is $dom->tree->[3][4][1], 'after', 'right text'; -is "$dom", < - test - -after -EOF - -# Case-insensitive attribute values -$dom = DOM::Tiny->new(<A

    -

    B

    -

    C

    -EOF -is $dom->find('.foo')->map('text')->join(','), 'A,B', 'right result'; -is $dom->find('.FOO')->map('text')->join(','), 'C', 'right result'; -is $dom->find('[class=foo]')->map('text')->join(','), 'A', 'right result'; -is $dom->find('[class=foo i]')->map('text')->join(','), 'A,C', 'right result'; -is $dom->find('[class="foo" i]')->map('text')->join(','), 'A,C', 'right result'; -is $dom->find('[class="foo bar"]')->size, 0, 'no results'; -is $dom->find('[class="foo bar" i]')->map('text')->join(','), 'B', - 'right result'; -is $dom->find('[class~=foo]')->map('text')->join(','), 'A,B', 'right result'; -is $dom->find('[class~=foo i]')->map('text')->join(','), 'A,B,C', - 'right result'; -is $dom->find('[class*=f]')->map('text')->join(','), 'A,B', 'right result'; -is $dom->find('[class*=f i]')->map('text')->join(','), 'A,B,C', 'right result'; -is $dom->find('[class^=F]')->map('text')->join(','), 'C', 'right result'; -is $dom->find('[class^=F i]')->map('text')->join(','), 'A,B,C', 'right result'; -is $dom->find('[class$=O]')->map('text')->join(','), 'C', 'right result'; -is $dom->find('[class$=O i]')->map('text')->join(','), 'A,C', 'right result'; - -# Nested description lists -$dom = DOM::Tiny->new(< -
    A
    -
    -
    -
    B -
    C -
    -
    - -EOF -is $dom->find('dl > dd > dl > dt')->[0]->text, 'B', 'right text'; -is $dom->find('dl > dd > dl > dd')->[0]->text, 'C', 'right text'; -is $dom->find('dl > dt')->[0]->text, 'A', 'right text'; - -# Nested lists -$dom = DOM::Tiny->new(< -
      -
    • - A -
        -
      • B
      • - C -
      -
    • -
    -
    -EOF -is $dom->find('div > ul > li')->[0]->text, 'A', 'right text'; -is $dom->find('div > ul > li')->[1], undef, 'no result'; -is $dom->find('div > ul li')->[0]->text, 'A', 'right text'; -is $dom->find('div > ul li')->[1]->text, 'B', 'right text'; -is $dom->find('div > ul li')->[2], undef, 'no result'; -is $dom->find('div > ul ul')->[0]->text, 'C', 'right text'; -is $dom->find('div > ul ul')->[1], undef, 'no result'; - -# Unusual order -$dom - = DOM::Tiny->new('Ok!'); -is $dom->at('a:not([href$=foo])[href^=h]')->text, 'Ok!', 'right text'; -is $dom->at('a:not([href$=example.com])[href^=h]'), undef, 'no result'; -is $dom->at('a[href^=h]#foo.bar')->text, 'Ok!', 'right text'; -is $dom->at('a[href^=h]#foo.baz'), undef, 'no result'; -is $dom->at('a[href^=h]#foo:not(b)')->text, 'Ok!', 'right text'; -is $dom->at('a[href^=h]#foo:not(a)'), undef, 'no result'; -is $dom->at('[href^=h].bar:not(b)[href$=m]#foo')->text, 'Ok!', 'right text'; -is $dom->at('[href^=h].bar:not(b)[href$=m]#bar'), undef, 'no result'; -is $dom->at(':not(b)#foo#foo')->text, 'Ok!', 'right text'; -is $dom->at(':not(b)#foo#bar'), undef, 'no result'; -is $dom->at(':not([href^=h]#foo#bar)')->text, 'Ok!', 'right text'; -is $dom->at(':not([href^=h]#foo#foo)'), undef, 'no result'; - -# Slash between attributes -$dom = DOM::Tiny->new('
    '); -is_deeply $dom->at('input')->attr, - {type => 'checkbox', value => '/a/', checked => undef}, 'right attributes'; -is "$dom", '
    ', 'right result'; - -# Dot and hash in class and id attributes -$dom = DOM::Tiny->new('

    A

    B

    '); -is $dom->at('p.a\#b\.c')->text, 'A', 'right text'; -is $dom->at(':not(p.a\#b\.c)')->text, 'B', 'right text'; -is $dom->at('p#a\#b\.c')->text, 'B', 'right text'; -is $dom->at(':not(p#a\#b\.c)')->text, 'A', 'right text'; - -# Extra whitespace -$dom = DOM::Tiny->new('< span>a< /span>bc'); -is $dom->at('span')->text, 'a', 'right text'; -is $dom->at('span + b')->text, 'b', 'right text'; -is $dom->at('b + span')->text, 'c', 'right text'; -is "$dom", 'abc', 'right result'; - -# Selectors with leading and trailing whitespace -$dom = DOM::Tiny->new('
    works
    '); -is $dom->at(' div b ')->text, 'works', 'right text'; -is $dom->at(' :not( #foo ) ')->text, 'works', 'right text'; - -# "0" -$dom = DOM::Tiny->new('0'); -is "$dom", '0', 'right result'; -$dom->append_content('☃'); -is "$dom", '0☃', 'right result'; -is $dom->parse(''), '', 'successful roundtrip'; -is $dom->parse(''), '', 'successful roundtrip'; -is $dom->parse(''), '', 'successful roundtrip'; -is $dom->parse(''), '', 'successful roundtrip'; - -# Not self-closing -$dom = DOM::Tiny->new('
    < div >
    test
    123'); -is $dom->at('div > div > pre')->text, 'test', 'right text'; -is "$dom", '
    test
    123
    ', 'right result'; -$dom = DOM::Tiny->new('

    '); -is $dom->find('p > svg > circle')->size, 2, 'two circles'; -is "$dom", '

    ', - 'right result'; - -# "image" -$dom = DOM::Tiny->new('test'); -is $dom->at('img')->{src}, 'foo.png', 'right attribute'; -is "$dom", 'test', 'right result'; - -# "title" -$dom = DOM::Tiny->new(' <p>test<'); -is $dom->at('title')->text, '

    test<', 'right text'; -is "$dom", ' <p>test<', 'right result'; - -# "textarea" -$dom = DOM::Tiny->new(''); -is $dom->at('textarea#a')->text, '

    test<', 'right text'; -is "$dom", '', 'right result'; - -# Comments -$dom = DOM::Tiny->new(< - -