Skip to content

Commit

Permalink
a few more small optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed May 6, 2013
1 parent 692b400 commit d5d5bd5
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 24 deletions.
24 changes: 9 additions & 15 deletions lib/Mojo/DOM.pm
Expand Up @@ -37,9 +37,8 @@ sub new {
}

sub all_text {
my ($self, $trim) = @_;
my $tree = $self->tree;
return _text(_elements($tree), 1, _trim($tree, $trim));
my $tree = shift->tree;
return _text(_elements($tree), 1, _trim($tree, @_));
}

sub append { shift->_add(1, @_) }
Expand Down Expand Up @@ -82,8 +81,7 @@ sub children {
for my $e (@$tree[($tree->[0] eq 'root' ? 1 : 4) .. $#$tree]) {

# Make sure child is the right type
next unless $e->[0] eq 'tag';
next if defined $type && $e->[1] ne $type;
next if $e->[0] ne 'tag' || (defined $type && $e->[1] ne $type);
push @children, $self->new->charset($charset)->tree($e)->xml($xml);
}

Expand All @@ -93,7 +91,7 @@ sub children {
sub content_xml {
my $self = shift;

# Render children
# Render children individually
my $tree = $self->tree;
my $charset = $self->charset;
my $xml = $self->xml;
Expand All @@ -107,9 +105,9 @@ sub find {

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

sub namespace {
Expand All @@ -118,8 +116,7 @@ sub namespace {
# Extract namespace prefix and search parents
return '' if (my $current = $self->tree)->[0] eq 'root';
my $ns = $current->[1] =~ /^(.*?):/ ? "xmlns:$1" : undef;
while ($current) {
last if $current->[0] eq 'root';
while ($current->[0] ne 'root') {

# Namespace for prefix
my $attrs = $current->[2];
Expand Down Expand Up @@ -167,12 +164,10 @@ sub remove { shift->replace('') }
sub replace {
my ($self, $new) = @_;

# Parse
my $tree = $self->tree;
if ($tree->[0] eq 'root') { return $self->xml(undef)->parse($new) }
else { $new = $self->_parse("$new") }

# Find and replace
my $parent = $tree->[3];
my $i = $parent->[0] eq 'root' ? 1 : 4;
for my $e (@$parent[$i .. $#$parent]) {
Expand Down Expand Up @@ -205,9 +200,8 @@ sub root {
}

sub text {
my ($self, $trim) = @_;
my $tree = $self->tree;
return _text(_elements($tree), 0, _trim($tree, $trim));
my $tree = shift->tree;
return _text(_elements($tree), 0, _trim($tree, @_));
}

sub text_after {
Expand Down
12 changes: 3 additions & 9 deletions lib/Mojo/DOM/HTML.pm
Expand Up @@ -160,8 +160,7 @@ sub _close {

# Check if parents need to be closed
my $parent = $$current;
while ($parent) {
last if $parent->[0] eq 'root' || $parent->[1] eq $stop;
while ($parent->[0] ne 'root' && $parent->[1] ne $stop) {

# Close
$tags->{$parent->[1]} and $self->_end($parent->[1], $current);
Expand All @@ -174,14 +173,10 @@ sub _close {
sub _end {
my ($self, $end, $current) = @_;

# Not a tag
return if $$current->[0] eq 'root';

# Search stack for start tag
my $found = 0;
my $next = $$current;
while ($next) {
last if $next->[0] eq 'root';
while ($next->[0] ne 'root') {

# Right tag
++$found and last if $next->[1] eq $end;
Expand All @@ -198,8 +193,7 @@ sub _end {

# Walk backwards
$next = $$current;
while ($$current = $next) {
last if $$current->[0] eq 'root';
while (($$current = $next) && $$current->[0] ne 'root') {
$next = $$current->[3];

# Match
Expand Down

0 comments on commit d5d5bd5

Please sign in to comment.