Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
small optimizations
  • Loading branch information
kraih committed Aug 8, 2013
1 parent b8307fd commit 00bc857
Showing 1 changed file with 13 additions and 14 deletions.
27 changes: 13 additions & 14 deletions lib/Mojo/DOM.pm
Expand Up @@ -38,12 +38,7 @@ sub new {

sub all_text { shift->_content(1, @_) }

sub ancestors {
my $self = shift;
my $xml = $self->xml;
return Mojo::Collection->new(map { $self->new->tree($_)->xml($xml) }
@{$self->_ancestors});
}
sub ancestors { $_[0]->_collection(@{_ancestors($_[0]->tree, 0)}) }

sub append { shift->_add(1, @_) }

Expand Down Expand Up @@ -101,11 +96,9 @@ sub content_xml {
}

sub find {
my ($self, $selector) = @_;
my $xml = $self->xml;
my $results = Mojo::DOM::CSS->new(tree => $self->tree)->select($selector);
return Mojo::Collection->new(map { $self->new->tree($_)->xml($xml) }
@$results);
my $self = shift;
my $results = Mojo::DOM::CSS->new(tree => $self->tree)->select(@_);
return $self->_collection(@$results);
}

sub namespace {
Expand Down Expand Up @@ -169,7 +162,7 @@ sub replace_content {

sub root {
my $self = shift;
return $self unless my $tree = $self->_ancestors(1)->[-1];
return $self unless my $tree = _ancestors($self->tree, 1)->[-1];
return $self->new->tree($tree)->xml($self->xml);
}

Expand Down Expand Up @@ -240,17 +233,23 @@ sub _add {
}

sub _ancestors {
my ($self, $root) = @_;
my ($parent, $root) = @_;

my @ancestors;
my $parent = $self->tree;
push @ancestors, $parent
while ($parent->[0] eq 'tag') && ($parent = $parent->[3]);
pop @ancestors unless $root;

return \@ancestors;
}

sub _collection {
my $self = shift;
my $xml = $self->xml;
return Mojo::Collection->new(@_)
->map(sub { $self->new->tree($_)->xml($xml) });
}

sub _content {
my $tree = shift->tree;
return _text(_elements($tree), shift, _trim($tree, @_));
Expand Down

0 comments on commit 00bc857

Please sign in to comment.