Skip to content

Commit

Permalink
store reference to parent with all nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Feb 12, 2014
1 parent 662d7bc commit 47a1d32
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 25 deletions.
26 changes: 7 additions & 19 deletions lib/Mojo/DOM.pm
Expand Up @@ -131,9 +131,8 @@ sub node { shift->tree->[0] }

sub parent {
my $self = shift;
return undef if (my $tree = $self->tree)->[0] eq 'root';
return _tag($self, $tree->[3], $self->xml) if $tree->[0] eq 'tag';
return $self->[1];
return undef if $self->tree->[0] eq 'root';
return _tag($self, $self->_parent, $self->xml);
}

sub parse { shift->_delegate(parse => shift) }
Expand Down Expand Up @@ -274,8 +273,7 @@ sub _ancestors {
sub _collect {
my $self = shift;
my $xml = $self->xml;
return Mojo::Collection->new(
map { $_->[0] eq 'tag' ? _tag($self, $_, $xml) : _node($self, $_) } @_);
return Mojo::Collection->new(map { _tag($self, $_, $xml) } @_);
}

sub _content {
Expand Down Expand Up @@ -307,21 +305,14 @@ sub _link {
my @new;
for my $n (@$children[1 .. $#$children]) {
push @new, $n;
next unless $n->[0] eq 'tag';
$n->[3] = $parent;
weaken $n->[3];
my $offset = $n->[0] eq 'tag' ? 3 : 2;
$n->[$offset] = $parent;
weaken $n->[$offset];
}

return @new;
}

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

sub _nodes {
return unless my $tree = shift;
return @$tree[_start($tree) .. $#$tree];
Expand All @@ -334,10 +325,7 @@ sub _offset {
return $i;
}

sub _parent {
my $self = shift;
return $self->node eq 'tag' ? $self->tree->[3] : $self->[1]->tree;
}
sub _parent { $_[0]->tree->[$_[0]->node eq 'tag' ? 3 : 2] }

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

Expand Down
19 changes: 13 additions & 6 deletions lib/Mojo/DOM/HTML.pm
Expand Up @@ -97,7 +97,7 @@ sub parse {

# Text (and runaway "<")
$text .= '<' if defined $runaway;
push @$current, ['text', html_unescape $text] if defined $text;
_node($current, 'text', html_unescape $text) if defined $text;

# Tag
if (defined $tag) {
Expand Down Expand Up @@ -129,24 +129,24 @@ sub parse {
# Relaxed "script" or "style" HTML elements
next if $xml || ($start ne 'script' && $start ne 'style');
next unless $html =~ m!\G(.*?)<\s*/\s*$start\s*>!gcsi;
push @$current, ['raw', $1];
_node($current, 'raw', $1);
_end($start, 0, \$current);
}
}

# DOCTYPE
elsif (defined $doctype) { push @$current, ['doctype', $doctype] }
elsif (defined $doctype) { _node($current, 'doctype', $doctype) }

# Comment
elsif (defined $comment) { push @$current, ['comment', $comment] }
elsif (defined $comment) { _node($current, 'comment', $comment) }

# CDATA
elsif (defined $cdata) { push @$current, ['cdata', $cdata] }
elsif (defined $cdata) { _node($current, 'cdata', $cdata) }

# Processing instruction (try to detect XML)
elsif (defined $pi) {
$self->xml($xml = 1) if !exists $self->{xml} && $pi =~ /xml/i;
push @$current, ['pi', $pi];
_node($current, 'pi', $pi);
}
}

Expand Down Expand Up @@ -185,6 +185,13 @@ sub _end {
} while $next = $next->[3];
}

sub _node {
my ($current, $type, $content) = @_;
my $new = [$type, $content, $current];
weaken $new->[2];
push @$current, $new;
}

sub _render {
my ($tree, $xml) = @_;

Expand Down

0 comments on commit 47a1d32

Please sign in to comment.