Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
make many Mojo::DOM methods a little faster
  • Loading branch information
kraih committed Jun 29, 2017
1 parent 6e52bd4 commit 81916d1
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Changes
@@ -1,5 +1,5 @@

7.34 2017-06-19
7.34 2017-06-29

7.33 2017-06-05
- Added EXPERIMENTAL support for :matches pseudo-class and :not pseudo-class
Expand Down
26 changes: 14 additions & 12 deletions lib/Mojo/DOM.pm
Expand Up @@ -100,8 +100,8 @@ sub new {
return @_ ? $self->parse(@_) : $self;
}

sub next { $_[0]->_maybe($_[0]->_siblings(1, 0)->[1]) }
sub next_node { $_[0]->_maybe($_[0]->_siblings(0, 0)->[1]) }
sub next { $_[0]->_maybe($_[0]->_siblings(1)->[1][0]) }
sub next_node { $_[0]->_maybe($_[0]->_siblings(0)->[1][0]) }

sub parent {
my $self = shift;
Expand All @@ -117,8 +117,8 @@ sub preceding_nodes { $_[0]->_collect($_[0]->_siblings->[0]) }
sub prepend { shift->_add(0, @_) }
sub prepend_content { shift->_content(0, 0, @_) }

sub previous { $_[0]->_maybe($_[0]->_siblings(1, -1)->[0]) }
sub previous_node { $_[0]->_maybe($_[0]->_siblings(0, -1)->[0]) }
sub previous { $_[0]->_maybe($_[0]->_siblings(1)->[0][-1]) }
sub previous_node { $_[0]->_maybe($_[0]->_siblings(0)->[0][-1]) }

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

Expand Down Expand Up @@ -289,19 +289,21 @@ sub _select {
}

sub _siblings {
my ($self, $tags, $i) = @_;
my ($self, $tags) = @_;

my $tree = $self->tree;
return [] unless my $parent = $tree->[0] eq 'root' ? undef : _parent($tree);

my (@before, @after, $match);
for my $node (@{_nodes($parent)}) {
++$match and next if !$match && $node eq $tree;
next if $tags && $node->[0] ne 'tag';
$match ? push @after, $node : push @before, $node;
}
my $nodes = _nodes($parent);
my $num = -1;
defined($num++) and $_ eq $tree and last for @$nodes;
my @before = $num > 0 ? @$nodes[0 .. ($num - 1)] : ();
my @after = $#$nodes > $num ? @$nodes[($num + 1) .. $#$nodes] : ();

return [\@before, \@after] unless $tags;

return defined $i ? [$before[$i], $after[$i]] : [\@before, \@after];
return [[grep { $_->[0] eq 'tag' } @before],
[grep { $_->[0] eq 'tag' } @after]];
}

sub _start { $_[0][0] eq 'root' ? 1 : 4 }
Expand Down

0 comments on commit 81916d1

Please sign in to comment.