Skip to content

Instantly share code, notes, and snippets.

/dwim.diff Secret

Created April 22, 2015 16:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/09ab77c59b39e5d2ecf7 to your computer and use it in GitHub Desktop.
Save anonymous/09ab77c59b39e5d2ecf7 to your computer and use it in GitHub Desktop.
diff --git a/lib/Mojo/DOM.pm b/lib/Mojo/DOM.pm
index 38e1949..3aa7671 100644
--- a/lib/Mojo/DOM.pm
+++ b/lib/Mojo/DOM.pm
@@ -167,7 +167,8 @@ sub xml { shift->_delegate(xml => @_) }
sub _add {
my ($self, $offset, $new) = @_;
- return $self if (my $tree = $self->tree)->[0] eq 'root';
+ my $tree = $self->tree;
+ return $self->_content($offset, 0, $new) if $tree->[0] eq 'root';
my $parent = $self->_parent;
splice @$parent, _offset($parent, $tree) + $offset, 0,
diff --git a/t/mojo/dom.t b/t/mojo/dom.t
index 2171a2f..60cf9fe 100644
--- a/t/mojo/dom.t
+++ b/t/mojo/dom.t
@@ -1246,53 +1246,57 @@ 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');
+$dom->prepend('a')->prepend('alal')->prepend('l');
is "$dom", <<EOF, 'no change';
-<ul>
+lalala<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');
+$dom->append("lala")->append("la\n");
is "$dom", <<EOF, 'no change';
-<ul>
+lalala<ul>
24<div>A-1</div>25<li>A</li><p>A1</p>23
<p>B</p>
<li>C</li>
</ul>
<div>D</div>
+lalala
EOF
$dom->find('div')->each(sub { shift->append('works') });
is "$dom", <<EOF, 'right result';
-<ul>
+lalala<ul>
24<div>A-1</div>works25<li>A</li><p>A1</p>23
<p>B</p>
<li>C</li>
</ul>
<div>D</div>works
+lalala
EOF
$dom->at('li')->prepend_content('A3<p>A2</p>')->prepend_content('A4');
is $dom->at('li')->text, 'A4A3 A', 'right text';
is "$dom", <<EOF, 'right result';
-<ul>
+lalala<ul>
24<div>A-1</div>works25<li>A4A3<p>A2</p>A</li><p>A1</p>23
<p>B</p>
<li>C</li>
</ul>
<div>D</div>works
+lalala
EOF
$dom->find('li')->[1]->append_content('<p>C2</p>C3')->append_content(' C4')
->append_content('C5');
is $dom->find('li')->[1]->text, 'C C3 C4C5', 'right text';
is "$dom", <<EOF, 'right result';
-<ul>
+lalala<ul>
24<div>A-1</div>works25<li>A4A3<p>A2</p>A</li><p>A1</p>23
<p>B</p>
<li>C<p>C2</p>C3 C4C5</li>
</ul>
<div>D</div>works
+lalala
EOF
# Optional "head" and "body" tags
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment