Skip to content

Commit

Permalink
merged and replaced a few more methods in Mojo::DOM
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Feb 5, 2014
1 parent cad4da1 commit f44ad68
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 61 deletions.
3 changes: 3 additions & 0 deletions Changes
Expand Up @@ -2,6 +2,9 @@
4.77 2014-02-05
- Deprecated Mojo::DOM::text_after and Mojo::DOM::text_before in favor of
Mojo::DOM::contents.
- Deprecated Mojo::DOM::content_xml and Mojo::DOM::replace_content in favor
of Mojo::DOM::content.
- Deprecated Mojo::DOM::to_xml in favor of Mojo::DOM::to_string.
- Added wrap_content method to Mojo::DOM.
- Improved wrap method in Mojo::DOM to allow wrapping of the root node.

Expand Down
70 changes: 42 additions & 28 deletions lib/Mojo/DOM.pm
Expand Up @@ -3,7 +3,7 @@ use Mojo::Base -strict;
use overload
'%{}' => sub { shift->attr },
bool => sub {1},
'""' => sub { shift->to_xml },
'""' => sub { shift->to_string },
fallback => 1;

# "Fry: This snow is beautiful. I'm glad global warming never happened.
Expand Down Expand Up @@ -71,10 +71,18 @@ sub children {
$self->_collect(grep { $_->[0] eq 'tag' } _nodes($self->tree)), @_);
}

sub content_xml {
sub content {
my $self = shift;
my $xml = $self->xml;
return join '', map { _render($_, $xml) } _nodes($self->tree);
return $self->_content(0, 1, @_) if @_;
my $html = Mojo::DOM::HTML->new(xml => $self->xml);
return join '', map { $html->tree($_)->render } _nodes($self->tree);
}

# DEPRECATED in Top Hat!
sub content_xml {
deprecated
'Mojo::DOM::content_xml is DEPRECATED in favor of Mojo::DOM::content';
shift->content;
}

sub contents { $_[0]->_collect(_nodes($_[0]->tree)) }
Expand Down Expand Up @@ -135,7 +143,12 @@ sub replace {
return $self->_replace($tree, $self->_parse("$new"));
}

sub replace_content { shift->_content(0, 1, @_) }
# DEPRECATED in Top Hat!
sub replace_content {
deprecated
'Mojo::DOM::replace_content is DEPRECATED in favor of Mojo::DOM::content';
shift->content(@_);
}

sub root {
my $self = shift;
Expand Down Expand Up @@ -191,7 +204,14 @@ sub text_before {
return _text(\@nodes, 0, _trim($tree->[3], $trim));
}

sub to_xml { shift->[0]->render }
sub to_string { shift->[0]->render }

# DEPRECATED in Top Hat!
sub to_xml {
deprecated
'Mojo::DOM::to_xml is DEPRECATED in favor of Mojo::DOM::to_string';
shift->to_string;
}

sub tree { shift->_delegate(tree => @_) }

Expand Down Expand Up @@ -294,8 +314,6 @@ sub _offset {

sub _parse { Mojo::DOM::HTML->new(xml => shift->xml)->parse(shift)->tree }

sub _render { Mojo::DOM::HTML->new(tree => shift, xml => shift)->render }

sub _replace {
my ($self, $tree, $new) = @_;
my $parent = $tree->[3];
Expand Down Expand Up @@ -566,14 +584,22 @@ All selectors from L<Mojo::DOM::CSS/"SELECTORS"> are supported.
# Show type of random child element
say $dom->children->shuffle->first->type;
=head2 content_xml
=head2 content
my $str = $dom->content_xml;
my $str = $dom->content;
$dom = $dom->content('<p>I ♥ Mojolicious!</p>');
Render content of this element to HTML/XML.
Render content of this element to HTML/XML or replace this element's content
with HTML/XML fragment.
# "<b>test</b>"
$dom->parse('<div><b>test</b></div>')->div->content_xml;
$dom->parse('<div><b>test</b></div>')->div->content;
# "<div><h1>B</h1></div>"
$dom->parse('<div><h1>A</h1></div>')->at('h1')->content('B')->root;
# "<div><h1></h1></div>"
$dom->parse('<div><h1>A</h1></div>')->at('h1')->content('')->root;
=head2 contents
Expand Down Expand Up @@ -709,18 +735,6 @@ Replace this element with HTML/XML fragment and return L</"parent">.
# "<div></div>"
$dom->parse('<div><h1>A</h1></div>')->at('h1')->replace('');
=head2 replace_content
$dom = $dom->replace_content('<p>I ♥ Mojolicious!</p>');
Replace this element's content with HTML/XML fragment.
# "<div><h1>B</h1></div>"
$dom->parse('<div><h1>A</h1></div>')->at('h1')->replace_content('B')->root;
# "<div><h1></h1></div>"
$dom->parse('<div><h1>A</h1></div>')->at('h1')->replace_content('')->root;
=head2 root
my $root = $dom->root;
Expand Down Expand Up @@ -768,14 +782,14 @@ smart whitespace trimming is enabled by default.
# "foo\nbaz\n"
$dom->parse("<div>foo\n<p>bar</p>baz\n</div>")->div->text(0);
=head2 to_xml
=head2 to_string
my $str = $dom->to_xml;
my $str = $dom->to_string;
Render this element and its content to HTML/XML.
# "<b>test</b>"
$dom->parse('<div><b>test</b></div>')->div->b->to_xml;
$dom->parse('<div><b>test</b></div>')->div->b->to_string;
=head2 tree
Expand Down Expand Up @@ -865,7 +879,7 @@ Alias for L</"attr">.
my $str = "$dom";
Alias for L</"to_xml">.
Alias for L</"to_string">.
=head1 SEE ALSO
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/DOM/Node.pm
Expand Up @@ -73,7 +73,7 @@ following new ones.
my $content = $node->content;
$node = $node->content('foo');
This node's content.
Return or replace this node's content.
=head2 node
Expand Down
4 changes: 2 additions & 2 deletions lib/Mojolicious/Plugin/PODRenderer.pm
Expand Up @@ -48,7 +48,7 @@ sub _html {

# Rewrite code blocks for syntax highlighting and correct indentation
for my $e ($dom->find('pre')->each) {
$e->replace_content(my $str = unindent $e->content_xml);
$e->content(my $str = unindent $e->content);
next if $str =~ /^\s*(?:\$|Usage:)\s+/m || $str !~ /[\$\@\%]\w|-&gt;\w/m;
my $attrs = $e->attr;
my $class = $attrs->{class};
Expand All @@ -71,7 +71,7 @@ sub _html {
# Rewrite
push @parts, [] if $e->type eq 'h1' || !@parts;
push @{$parts[-1]}, $text, Mojo::URL->new->fragment($anchor);
$e->replace_content($self->link_to($text => $toc, id => $anchor));
$e->content($self->link_to($text => $toc, id => $anchor));
}

# Try to find a title
Expand Down
50 changes: 25 additions & 25 deletions t/mojo/dom.t
Expand Up @@ -120,7 +120,7 @@ is $simple->text, 'easy', 'right text';
is $simple->parent->type, 'foo', 'right parent type';
is $simple->parent->attr->{bar}, 'ba<z', 'right parent attribute';
is $simple->parent->children->[1]->type, 'test', 'right sibling';
is $simple->to_xml, '<simple class="working">easy</simple>',
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';
Expand Down Expand Up @@ -340,8 +340,8 @@ 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_xml, $html, 'right result';
is $dom->content_xml, $html, 'right result';
is $dom->to_string, $html, 'right result';
is $dom->content, $html, 'right result';

# Looks remotely like HTML
$dom = Mojo::DOM->new->parse(
Expand Down Expand Up @@ -374,15 +374,15 @@ is "$dom", '<div>foo<p>lalala</p>bar</div>', 'right result';
$dom->find('p')->replace('');
is "$dom", '<div>foobar</div>', 'right result';
$dom = Mojo::DOM->new->parse('<div>♥</div>');
$dom->at('div')->replace_content('');
$dom->at('div')->content('');
is "$dom", '<div>☃</div>', 'right result';
$dom = Mojo::DOM->new->parse('<div>♥</div>');
$dom->at('div')->replace_content("\x{2603}");
is $dom->to_xml, '<div>☃</div>', 'right result';
$dom->at('div')->content("\x{2603}");
is $dom->to_string, '<div>☃</div>', 'right result';
is $dom->at('div')->replace('<p>♥</p>')->root, '<p>♥</p>', 'right result';
is $dom->to_xml, '<p>♥</p>', 'right result';
is $dom->to_string, '<p>♥</p>', 'right result';
is $dom->replace('<b>whatever</b>')->root, '<b>whatever</b>', 'right result';
is $dom->to_xml, '<b>whatever</b>', 'right result';
is $dom->to_string, '<b>whatever</b>', 'right result';
$dom->at('b')->prepend('<p>foo</p>')->append('<p>bar</p>');
is "$dom", '<p>foo</p><b>whatever</b><p>bar</p>', 'right result';
is $dom->find('p')->remove->first->root->at('b')->text, 'whatever',
Expand All @@ -394,33 +394,33 @@ is $dom->remove, '', 'right result';
$dom->replace('A<div>B<p>C<b>D<i><u>E</u></i>F</b>G</p><div>H</div></div>I');
is $dom->find(':not(div):not(i):not(u)')->strip->first->root,
'A<div>BCD<i><u>E</u></i>FG<div>H</div></div>I', 'right result';
is $dom->at('i')->to_xml, '<i><u>E</u></i>', 'right result';
is $dom->at('i')->to_string, '<i><u>E</u></i>', 'right result';

# Replace element content
$dom = Mojo::DOM->new->parse('<div>foo<p>lalala</p>bar</div>');
is $dom->at('p')->replace_content('bar'), '<p>bar</p>', 'right result';
is $dom->at('p')->content('bar'), '<p>bar</p>', 'right result';
is "$dom", '<div>foo<p>bar</p>bar</div>', 'right result';
$dom->at('p')->replace_content(Mojo::DOM->new->parse('text'));
$dom->at('p')->content(Mojo::DOM->new->parse('text'));
is "$dom", '<div>foo<p>text</p>bar</div>', 'right result';
$dom = Mojo::DOM->new->parse('<div>foo</div><div>bar</div>');
$dom->find('div')->each(sub { shift->replace_content('<p>test</p>') });
$dom->find('div')->each(sub { shift->content('<p>test</p>') });
is "$dom", '<div><p>test</p></div><div><p>test</p></div>', 'right result';
$dom->find('p')->each(sub { shift->replace_content('') });
$dom->find('p')->each(sub { shift->content('') });
is "$dom", '<div><p></p></div><div><p></p></div>', 'right result';
$dom = Mojo::DOM->new->parse('<div><p id="☃" /></div>');
$dom->at('#☃')->replace_content('');
$dom->at('#☃')->content('');
is "$dom", '<div><p id="☃">♥</p></div>', 'right result';
$dom = Mojo::DOM->new->parse('<div>foo<p>lalala</p>bar</div>');
$dom->replace_content('');
$dom->content('');
is "$dom", '', 'right result';
is $dom->replace_content('<div>foo<p>lalala</p>bar</div>'),
is $dom->content('<div>foo<p>lalala</p>bar</div>'),
'<div>foo<p>lalala</p>bar</div>', 'right result';
is "$dom", '<div>foo<p>lalala</p>bar</div>', 'right result';
is $dom->replace_content(''), '', 'no result';
is $dom->content(''), '', 'no result';
is "$dom", '', 'no result';
$dom->replace_content('<div>foo<p>lalala</p>bar</div>');
$dom->content('<div>foo<p>lalala</p>bar</div>');
is "$dom", '<div>foo<p>lalala</p>bar</div>', 'right result';
is $dom->at('p')->replace_content(''), '<p></p>', 'right result';
is $dom->at('p')->content(''), '<p></p>', 'right result';

# Mixed search and tree walk
$dom = Mojo::DOM->new->parse(<<EOF);
Expand Down Expand Up @@ -663,8 +663,8 @@ is $dom->at('[test3=""]'), undef, 'no result';
# Whitespaces before closing bracket
$dom = Mojo::DOM->new->parse('<div >content</div>');
ok $dom->at('div'), 'tag found';
is $dom->at('div')->text, 'content', 'right text';
is $dom->at('div')->content_xml, 'content', 'right text';
is $dom->at('div')->text, 'content', 'right text';
is $dom->at('div')->content, 'content', 'right text';

# Class with hyphen
$dom
Expand All @@ -686,12 +686,12 @@ is_deeply \@div, [qw(A B 0)], 'found all div elements with id';
# Empty tags
$dom = Mojo::DOM->new->parse('<hr /><br/><br id="br"/><br />');
is "$dom", '<hr><br><br id="br"><br>', 'right result';
is $dom->at('br')->content_xml, '', 'empty result';
is $dom->at('br')->content, '', 'empty result';

# Inner XML
$dom = Mojo::DOM->new->parse('<a>xxx<x>x</x>xxx</a>');
is $dom->at('a')->content_xml, 'xxx<x>x</x>xxx', 'right result';
is $dom->content_xml, '<a>xxx<x>x</x>xxx</a>', 'right result';
is $dom->at('a')->content, 'xxx<x>x</x>xxx', 'right result';
is $dom->content, '<a>xxx<x>x</x>xxx</a>', 'right result';

# Multiple selectors
$dom = Mojo::DOM->new->parse(
Expand Down Expand Up @@ -1778,7 +1778,7 @@ $dom = Mojo::DOM->new->parse(<<'EOF');
<XMLTest />
EOF
ok $dom->xml, 'XML mode detected';
$dom->at('XMLTest')->replace_content('<Element />');
$dom->at('XMLTest')->content('<Element />');
my $element = $dom->at('Element');
is $element->type, 'Element', 'right type';
ok $element->xml, 'XML mode active';
Expand Down
9 changes: 5 additions & 4 deletions t/mojo/response.t
Expand Up @@ -1003,13 +1003,14 @@ is $res->dom->at('p > a')->text, 'bar', 'right value';
is $res->dom('p')->first->text, 'foo', 'right value';
is_deeply [$res->dom('p > a')->pluck('text')->each], [qw(bar baz)],
'right values';
my @text = $res->dom('a')->pluck(replace_content => 'yada')
->first->root->find('p > a')->pluck('text')->each;
my @text = $res->dom('a')->pluck(content => 'yada')->first->root->find('p > a')
->pluck('text')->each;
is_deeply \@text, [qw(yada yada)], 'right values';
is_deeply [$res->dom('p > a')->pluck('text')->each], [qw(yada yada)],
'right values';
@text = $res->dom->find('a')->pluck(replace_content => 'test')
->first->root->find('p > a')->pluck('text')->each;
@text
= $res->dom->find('a')->pluck(content => 'test')->first->root->find('p > a')
->pluck('text')->each;
is_deeply \@text, [qw(test test)], 'right values';
is_deeply [$res->dom->find('p > a')->pluck('text')->each], [qw(test test)],
'right values';
Expand Down
5 changes: 4 additions & 1 deletion t/pod_coverage.t
Expand Up @@ -8,7 +8,10 @@ plan skip_all => 'Test::Pod::Coverage 1.04 required for this test!'
unless eval 'use Test::Pod::Coverage 1.04; 1';

# DEPRECATED in Top Hat!
my @tophat = qw(secret text_after text_before to_rel);
my @tophat = (
qw(content_xml replace_content secret text_after text_before to_rel),
qw(to_xml)
);

# False positive constants
all_pod_coverage_ok({also_private => [qw(IPV6 TLS), @tophat]});

0 comments on commit f44ad68

Please sign in to comment.