Skip to content

Commit

Permalink
added remove method to Mojo::DOM
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Jul 23, 2012
1 parent 29164e7 commit ec77b0e
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
1 change: 1 addition & 0 deletions Changes
@@ -1,5 +1,6 @@

3.13 2012-07-23
- Added remove method to Mojo::DOM.
- Improved Mojolicious::Plugin::Config log messages. (jberger)
- Improved documentation.
- Improved tests.
Expand Down
11 changes: 11 additions & 0 deletions lib/Mojo/DOM.pm
Expand Up @@ -179,6 +179,8 @@ sub prepend_content {
return $self;
}

sub remove { shift->replace('') }

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

Expand Down Expand Up @@ -611,6 +613,15 @@ Prepend to element content.
# "<div><h2>AB</h2></div>"
$dom->parse('<div><h2>B</h2></div>')->at('h2')->prepend_content('A')->root;
=head2 C<remove>
my $old = $dom->remove;
Remove element.
# "<div></div>"
$dom->parse('<div><h1>A</h1></div>')->at('h1')->remove->root;
=head2 C<replace>
my $old = $dom->replace('<div>test</div>');
Expand Down
9 changes: 8 additions & 1 deletion t/mojo/dom.t
Expand Up @@ -2,7 +2,7 @@ use Mojo::Base -strict;

use utf8;

use Test::More tests => 783;
use Test::More tests => 787;

# "Homer gave me a kidney: it wasn't his, I didn't need it,
# and it came postage due- but I appreciated the gesture!"
Expand Down Expand Up @@ -321,6 +321,13 @@ is $dom->at('div')->replace('<p>♥</p>')->root, '<p>♥</p>', 'right result';
is $dom->to_xml, '<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';
$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')->pluck('remove')->first->root->at('b')->text, 'whatever',
'right result';
is "$dom", '<b>whatever</b>', 'right result';
$dom->remove;
is "$dom", '', 'right result';

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

0 comments on commit ec77b0e

Please sign in to comment.