Navigation Menu

Skip to content

Commit

Permalink
added wrap_content method to Mojo::DOM
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Feb 5, 2014
1 parent 05e4c32 commit a08584e
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 17 deletions.
1 change: 1 addition & 0 deletions Changes
@@ -1,5 +1,6 @@

4.77 2014-02-05
- Added wrap_content method to Mojo::DOM.

4.76 2014-02-04
- Added wrap method to Mojo::DOM.
Expand Down
53 changes: 36 additions & 17 deletions lib/Mojo/DOM.pm
Expand Up @@ -199,23 +199,8 @@ sub type {
return $self;
}

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

return $self if (my $tree = $self->tree)->[0] eq 'root';

# Find innermost tag
my $current;
my $first = $new = $self->_parse("$new");
$current = $first while $first = first { $_->[0] eq 'tag' } _nodes($first);
return $self unless $current;

# Wrap element
$self->_replace($tree, $new);
push @$current, _link(['root', $tree], $current);

return $self;
}
sub wrap { shift->_wrap(0, @_) }
sub wrap_content { shift->_wrap(1, @_) }

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

Expand Down Expand Up @@ -390,6 +375,30 @@ sub _trim {
return 1;
}

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

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

# Find innermost tag
my $current;
my $first = $new = $self->_parse("$new");
$current = $first while $first = first { $_->[0] eq 'tag' } _nodes($first);
return $self unless $current;

# Wrap content
if ($content) {
push @$current, _link(['root', _nodes($tree)], $current);
splice @$tree, _start($tree), $#$tree, _link($new, $tree);
return $self;
}

# Wrap element
$self->_replace($tree, $new);
push @$current, _link(['root', $tree], $current);
return $self;
}

1;

=encoding utf8
Expand Down Expand Up @@ -826,6 +835,16 @@ the first innermost element.
# "<p><b>A</b></p><p>B</p>"
$dom->parse('<b>A</b>')->at('b')->wrap('<p></p><p>B</p>')->root;
=head2 wrap_content
$dom = $dom->wrap_content('<div></div>');
Wrap HTML/XML fragment around this element's content, placing it as the last
children of the first innermost element.
# "<p><b>BA</b></p>"
$dom->parse('<p>A<p>')->at('p')->wrap_content('<b>B</b>')->root;
=head2 xml
my $bool = $dom->xml;
Expand Down
12 changes: 12 additions & 0 deletions t/mojo/dom.t
Expand Up @@ -2021,6 +2021,18 @@ is $dom->at('a')->wrap('C<c><d>D</d><e>E</e></c>F')->parent->type, 'd',
'right element';
is "$dom", '<b>C<c><d>D<a>Test</a></d><e>E</e></c>F</b>', 'right result';

# Wrap content
$dom = Mojo::DOM->new('<a>Test</a>');
is $dom->wrap_content('<b></b>')->node, 'root', 'right node';
is "$dom", '<b><a>Test</a></b>', 'right result';
is $dom->at('b')->strip->at('a')->wrap_content('1<b c="d"></b>')->type, 'a',
'right element';
is "$dom", '<a>1<b c="d">Test</b></a>', 'right result';
is $dom->at('a')->wrap_content('C<c><d>D</d><e>E</e></c>F')->parent->node,
'root', 'right node';
is "$dom", '<a>C<c><d>D1<b c="d">Test</b></d><e>E</e></c>F</a>',
'right result';

# Broken "div" in "td"
$dom = Mojo::DOM->new(<<EOF);
<table>
Expand Down

0 comments on commit a08584e

Please sign in to comment.