Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
fixed XML mode bug in Mojo::DOM
  • Loading branch information
kraih committed Feb 12, 2014
1 parent 3e9cfa8 commit dedc64c
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 8 deletions.
1 change: 0 additions & 1 deletion Changes
Expand Up @@ -2,7 +2,6 @@
4.80 2014-02-12
- Merged Mojo::DOM::Node into Mojo::DOM.
- Added next_sibling and previous_sibling methods to Mojo::DOM.
- Added next_sibling and previous_sibling methods to Mojo::DOM::Node.
- Improved Mojo::DOM::HTML performance.
- Fixed list parsing bug in Mojo::DOM::HTML.

Expand Down
15 changes: 8 additions & 7 deletions lib/Mojo/DOM.pm
Expand Up @@ -267,20 +267,20 @@ sub _all_text {
sub _ancestors {
my ($self, $root) = @_;

my $node = $self->node;
my $element = $node eq 'root' || $node eq 'tag';
my $tree = $element ? $self->tree : ['tag', 'dummy', {}, $self->_parent];
return if $self->node eq 'root';

my @ancestors;
push @ancestors, $tree while ($tree->[0] eq 'tag') && ($tree = $tree->[3]);
my $tree = $self->_parent;
do { push @ancestors, $tree }
while ($tree->[0] eq 'tag') && ($tree = $tree->[3]);
return $root ? $ancestors[-1] : @ancestors[0 .. $#ancestors - 1];
}

sub _collect {
my $self = shift;
my $xml = $self->xml;
return Mojo::Collection->new(
map { $_->[0] eq 'tag' ? _tag($self, $_, $xml) : _node($_, $self) } @_);
map { $_->[0] eq 'tag' ? _tag($self, $_, $xml) : _node($self, $_) } @_);
}

sub _content {
Expand Down Expand Up @@ -321,8 +321,9 @@ sub _link {
}

sub _node {
my $dom = Mojo::DOM->new->tree(shift);
$dom->[1] = shift;
my ($self, $tree) = @_;
my $dom = $self->new->xml($self->xml)->tree($tree);
$dom->[1] = $self;
return $dom;
}

Expand Down
7 changes: 7 additions & 0 deletions t/mojo/dom.t
Expand Up @@ -212,6 +212,13 @@ is $dom->at('i')->contents->first->wrap_content('<b></b>')->root,
is $dom->at('b')->contents->first->ancestors->type->join(','), 'b,i,script',
'right result';

# XML nodes
$dom = Mojo::DOM->new->xml(1)->parse('<b>test</b>');
ok $dom->at('b')->contents->first->xml, 'XML mode active';
ok $dom->at('b')->contents->first->replace('<br>')->contents->first->xml,
'XML mode active';
is "$dom", '<b><br /></b>', 'right result';

# Treating nodes as elements
$dom = Mojo::DOM->new('foo<b>bar</b>baz');
is $dom->contents->first->contents->size, 0, 'no contents';
Expand Down

0 comments on commit dedc64c

Please sign in to comment.