Skip to content

Commit

Permalink
fix bug in Mojo::DOM where the wrap_content method would attempt to w…
Browse files Browse the repository at this point in the history
…rap an HTML/XML fragment around nodes that do not have children
  • Loading branch information
kraih committed Nov 26, 2015
1 parent 5bbb90d commit d51b3ae
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 8 deletions.
2 changes: 2 additions & 0 deletions Changes
Expand Up @@ -2,6 +2,8 @@
6.34 2015-11-26
- Fixed bug in Mojo::DOM where the wrap method would attempt to wrap an
HTML/XML fragment around the content of root nodes.
- Fixed bug in Mojo::DOM where the wrap_content method would attempt to wrap
an HTML/XML fragment around nodes that do not have children.

6.33 2015-11-22
- Updated IO::Socket::IP requirement to 0.37 for certain bug fixes.
Expand Down
6 changes: 3 additions & 3 deletions lib/Mojo/DOM.pm
Expand Up @@ -352,7 +352,7 @@ sub _wrap {
my ($self, $content, $new) = @_;

return $self if (my $tree = $self->tree)->[0] eq 'root' && !$content;
$content = 0 if $tree->[0] ne 'root' && $tree->[0] ne 'tag';
return $self if $tree->[0] ne 'root' && $tree->[0] ne 'tag' && $content;

# Find innermost tag
my $current;
Expand Down Expand Up @@ -999,8 +999,8 @@ placing it as the last child of the first innermost element.
$dom = $dom->wrap_content('<div></div>');
Wrap HTML/XML fragment around this node's content, placing it as the last
children of the first innermost element.
Wrap HTML/XML fragment around this node's content (for C<root> and C<tag>
nodes), placing it as the last children of the first innermost element.
# "<p><b>123Test</b></p>"
$dom->parse('<p>Test<p>')->at('p')->wrap_content('<b>123</b>')->root;
Expand Down
13 changes: 8 additions & 5 deletions t/mojo/dom.t
Expand Up @@ -241,6 +241,8 @@ 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';
Expand Down Expand Up @@ -1268,17 +1270,18 @@ is "$dom", <<EOF, 'right result';
EOF
is $dom->at('div')->text, 'A-1', 'right text';
is $dom->at('iv'), undef, 'no result';
$dom->prepend('l')->prepend('alal')->prepend('a');
is "$dom", <<EOF, 'no change';
is $dom->prepend('l')->prepend('alal')->prepend('a')->type, 'root',
'right type';
is "$dom", <<EOF, 'no changes';
<ul>
24<div>A-1</div>25<li>A</li><p>A1</p>23
<p>B</p>
<li>C</li>
</ul>
<div>D</div>
EOF
$dom->append('lalala');
is "$dom", <<EOF, 'no change';
is $dom->append('lalala')->type, 'root', 'right type';
is "$dom", <<EOF, 'no changes';
<ul>
24<div>A-1</div>25<li>A</li><p>A1</p>23
<p>B</p>
Expand Down Expand Up @@ -2124,7 +2127,7 @@ is $dom->all_text, 'Mojo Test', 'right text';
# Wrap elements
$dom = Mojo::DOM->new('<a>Test</a>');
is "$dom", '<a>Test</a>', 'right result';
$dom->wrap('<b></b>');
is $dom->wrap('<b></b>')->type, 'root', 'right type';
is "$dom", '<a>Test</a>', 'no changes';
is $dom->at('a')->wrap('<b></b>')->type, 'tag', 'right type';
is "$dom", '<b><a>Test</a></b>', 'right result';
Expand Down

0 comments on commit d51b3ae

Please sign in to comment.