Mojolicious
view release on metacpan or search on metacpan
t/mojo/dom.t view on Meta::CPAN
is $simple->parent->attr->{bar}, 'ba<z', 'right parent attribute';
is $simple->parent->children->[1]->tag, 'test', 'right sibling';
is $simple->to_string, '<simple class="working">easy</simple>', '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';
};
subtest 'Nodes' => sub {
my $dom = Mojo::DOM->new('<!DOCTYPE before><p>test<![CDATA[123]]><!-- 456 --></p><?after?>');
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], '<!DOCTYPE before>', '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", '<!DOCTYPE again><p><![CDATA[123]]><!-- 456 --></p>', 'right result';
};
subtest 'Modify nodes' => sub {
my $dom = Mojo::DOM->new('<script>la<la>la</script>');
is $dom->at('script')->type, 'tag', 'right type';
is $dom->at('script')->[0]->type, 'raw', 'right type';
is $dom->at('script')->[0]->content, 'la<la>la', 'right content';
is "$dom", '<script>la<la>la</script>', 'right result';
is $dom->at('script')->child_nodes->first->replace('a<b>c</b>1<b>d</b>')->tag, 'script', 'right tag';
is "$dom", '<script>a<b>c</b>1<b>d</b></script>', '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", '<script>a<b>fce</b>1<b>d</b></script>', '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('<i>:)</i>')->root, '<script><i>:)a</i><b>fce</b>1<b>d</b></script>',
'right result';
is $dom->at('i')->child_nodes->first->wrap_content('<b></b>')->root,
'<script><i>:)a</i><b>fce</b>1<b>d</b></script>', 'no changes';
is $dom->at('i')->child_nodes->first->wrap('<b></b>')->root, '<script><i><b>:)</b>a</i><b>fce</b>1<b>d</b></script>',
'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", '<script><i><b>h:)g</b>a</i><b>fce</b>1<b>d</b></script>', 'right result';
is $dom->at('script > b:last-of-type')->append('<!--y-->')->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", '<script>z<i><b>h:)g</b>a</i><b>fce</b>1<b>d</b><!--y--></script>', 'right result';
};
subtest 'XML nodes' => sub {
my $dom = Mojo::DOM->new->xml(1)->parse('<b>test<image /></b>');
ok $dom->at('b')->child_nodes->first->xml, 'XML mode active';
ok $dom->at('b')->child_nodes->first->replace('<br>')->child_nodes->first->xml, 'XML mode active';
is "$dom", '<b><br /><image /></b>', 'right result';
};
( run in 1.907 second using v1.01-cache-2.11-cpan-75ffa21a3d4 )