Navigation Menu

Skip to content

Commit

Permalink
added strip method to Mojo::DOM and fixed a few return value bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Jul 28, 2013
1 parent 6dac40b commit 1773cb9
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 21 deletions.
2 changes: 2 additions & 0 deletions Changes
@@ -1,5 +1,7 @@

4.21 2013-07-29
- Added strip method to Mojo::DOM.
- Fixed return values of remove and replace methods in Mojo::DOM.

4.20 2013-07-28
- Deprecated Mojo::DOM::attrs in favor of Mojo::DOM::attr.
Expand Down
58 changes: 39 additions & 19 deletions lib/Mojo/DOM.pm
Expand Up @@ -158,20 +158,16 @@ sub remove { shift->replace('') }

sub replace {
my ($self, $new) = @_;

my $tree = $self->tree;
if ($tree->[0] eq 'root') { return $self->xml(undef)->parse($new) }
else { $new = $self->_parse("$new") }

my $parent = $tree->[3];
my $i = $parent->[0] eq 'root' ? 1 : 4;
for my $e (@$parent[$i .. $#$parent]) {
last if $e == $tree;
$i++;
}
splice @$parent, $i, 1, @{_parent($new, $parent)};
return $self->xml(undef)->parse($new) if $tree->[0] eq 'root';
return $self->_replace($tree, $self->_parse("$new"));
}

return $self;
sub strip {
my $self = shift;
my $tree = $self->tree;
return $self if $tree->[0] eq 'root';
return $self->_replace($tree, ['root', @$tree[4 .. $#$tree]]);
}

sub replace_content {
Expand Down Expand Up @@ -299,6 +295,20 @@ sub _parent {

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

sub _replace {
my ($self, $tree, $new) = @_;

my $parent = $tree->[3];
my $i = $parent->[0] eq 'root' ? 1 : 4;
for my $e (@$parent[$i .. $#$parent]) {
last if $e == $tree;
$i++;
}
splice @$parent, $i, 1, @{_parent($new, $parent)};

return $self->parent;
}

sub _sibling {
my ($self, $next) = @_;

Expand Down Expand Up @@ -596,22 +606,22 @@ there are no more siblings.
=head2 remove
my $old = $dom->remove;
my $parent = $dom->remove;
Remove element and return it as a L<Mojo::DOM> object.
Remove element and return L<Mojo::DOM> object for parent of element.
# "<div></div>"
$dom->parse('<div><h1>A</h1></div>')->at('h1')->remove->root;
$dom->parse('<div><h1>A</h1></div>')->at('h1')->remove;
=head2 replace
my $old = $dom->replace('<div>test</div>');
my $parent = $dom->replace('<div>test</div>');
Replace element with HTML/XML and return the replaced element as a
L<Mojo::DOM> object.
Replace element with HTML/XML and return L<Mojo::DOM> object for parent of
element.
# "<div><h2>B</h2></div>"
$dom->parse('<div><h1>A</h1></div>')->at('h1')->replace('<h2>B</h2>')->root;
$dom->parse('<div><h1>A</h1></div>')->at('h1')->replace('<h2>B</h2>');
# "<div></div>"
$dom->parse('<div><h1>A</h1></div>')->at('h1')->replace('')->root;
Expand All @@ -634,6 +644,16 @@ Replace element content with HTML/XML.
Return L<Mojo::DOM> object for root node.
=head2 strip
my $parent = $dom->strip;
Remove element while preserving the content and return L<Mojo::DOM> object for
parent of element.
# "<div>A</div>"
$dom->parse('<div><h1>A</h1></div>')->at('h1')->strip;
=head2 text
my $trimmed = $dom->text;
Expand Down
12 changes: 10 additions & 2 deletions t/mojo/dom.t
Expand Up @@ -292,7 +292,8 @@ is $dom->at('.test')->text, '♥', 'right text';

# Replace elements
$dom = Mojo::DOM->new->parse('<div>foo<p>lalala</p>bar</div>');
is $dom->at('p')->replace('<foo>bar</foo>'), '<p>lalala</p>', 'right result';
is $dom->at('p')->replace('<foo>bar</foo>'),
'<div>foo<foo>bar</foo>bar</div>', 'right result';
is "$dom", '<div>foo<foo>bar</foo>bar</div>', 'right result';
$dom->at('foo')->replace(Mojo::DOM->new->parse('text'));
is "$dom", '<div>footextbar</div>', 'right result';
Expand All @@ -304,7 +305,7 @@ is $dom->replace('♥'), '♥', 'right result';
is "$dom", '', 'right result';
$dom->replace('<div>foo<p>lalala</p>bar</div>');
is "$dom", '<div>foo<p>lalala</p>bar</div>', 'right result';
is $dom->at('p')->replace(''), '<p>lalala</p>', 'right result';
is $dom->at('p')->replace(''), '<div>foobar</div>', 'right result';
is "$dom", '<div>foobar</div>', 'right result';
is $dom->replace(''), '', 'no result';
is "$dom", '', 'no result';
Expand All @@ -327,6 +328,13 @@ is "$dom", '<p>foo</p><b>whatever</b><p>bar</p>', 'right result';
is $dom->find('p')->pluck('remove')->first->root->at('b')->text, 'whatever',
'right result';
is "$dom", '<b>whatever</b>', 'right result';
is $dom->at('b')->strip, 'whatever', 'right result';
is $dom->strip, 'whatever', 'right result';
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)')->pluck('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';

# Replace element content
$dom = Mojo::DOM->new->parse('<div>foo<p>lalala</p>bar</div>');
Expand Down

0 comments on commit 1773cb9

Please sign in to comment.