Skip to content

Commit

Permalink
small optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Jul 28, 2013
1 parent 805d75d commit 019b22f
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 18 deletions.
2 changes: 2 additions & 0 deletions Changes
@@ -1,4 +1,6 @@

4.22 2013-07-30

4.21 2013-07-29
- Added strip method to Mojo::DOM.
- Fixed return values of remove and replace methods in Mojo::DOM.
Expand Down
30 changes: 13 additions & 17 deletions lib/Mojo/DOM.pm
Expand Up @@ -46,7 +46,7 @@ sub append { shift->_add(1, @_) }
sub append_content {
my ($self, $new) = @_;
my $tree = $self->tree;
push @$tree, @{_parent($self->_parse("$new"), $tree)};
push @$tree, _link($self->_parse("$new"), $tree);
return $self;
}

Expand Down Expand Up @@ -79,9 +79,8 @@ sub children {
my ($self, $type) = @_;

my @children;
my $xml = $self->xml;
my $tree = $self->tree;
for my $e (@$tree[($tree->[0] eq 'root' ? 1 : 4) .. $#$tree]) {
my $xml = $self->xml;
for my $e (@{_elements($self->tree)}) {

# Make sure child is the right type
next if $e->[0] ne 'tag' || (defined $type && $e->[1] ne $type);
Expand All @@ -93,13 +92,8 @@ sub children {

sub content_xml {
my $self = shift;

# Render children individually
my $tree = $self->tree;
my $xml = $self->xml;
return join '',
map { Mojo::DOM::HTML->new(tree => $_, xml => $xml)->render }
@$tree[($tree->[0] eq 'root' ? 1 : 4) .. $#$tree];
return join '', map { _render($_, $xml) } @{_elements($self->tree)};
}

sub find {
Expand Down Expand Up @@ -148,7 +142,7 @@ sub prepend_content {
my ($self, $new) = @_;
my $tree = $self->tree;
splice @$tree, $tree->[0] eq 'root' ? 1 : 4, 0,
@{_parent($self->_parse("$new"), $tree)};
_link($self->_parse("$new"), $tree);
return $self;
}

Expand All @@ -167,7 +161,7 @@ sub replace_content {
my ($self, $new) = @_;
my $tree = $self->tree;
splice @$tree, $tree->[0] eq 'root' ? 1 : 4, $#$tree,
@{_parent($self->_parse("$new"), $tree)};
_link($self->_parse("$new"), $tree);
return $self;
}

Expand All @@ -187,7 +181,7 @@ sub strip {
my $self = shift;
my $tree = $self->tree;
return $self if $tree->[0] eq 'root';
return $self->_replace($tree, ['root', @$tree[4 .. $#$tree]]);
return $self->_replace($tree, ['root', @{_elements($tree)}]);
}

sub text {
Expand Down Expand Up @@ -260,7 +254,7 @@ sub _add {
}

# Add children
splice @$parent, $i + $offset, 0, @{_parent($self->_parse("$new"), $parent)};
splice @$parent, $i + $offset, 0, _link($self->_parse("$new"), $parent);

return $self;
}
Expand All @@ -277,7 +271,7 @@ sub _html {
return $self;
}

sub _parent {
sub _link {
my ($children, $parent) = @_;

# Link parent to children
Expand All @@ -290,11 +284,13 @@ sub _parent {
push @new, $e;
}

return \@new;
return @new;
}

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

sub _render { Mojo::DOM::HTML->new(tree => shift, xml => shift)->render }

sub _replace {
my ($self, $tree, $new) = @_;

Expand All @@ -304,7 +300,7 @@ sub _replace {
last if $e == $tree;
$i++;
}
splice @$parent, $i, 1, @{_parent($new, $parent)};
splice @$parent, $i, 1, _link($new, $parent);

return $self->parent;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojolicious.pm
Expand Up @@ -41,7 +41,7 @@ has static => sub { Mojolicious::Static->new };
has types => sub { Mojolicious::Types->new };

our $CODENAME = 'Top Hat';
our $VERSION = '4.21';
our $VERSION = '4.22';

sub AUTOLOAD {
my $self = shift;
Expand Down

0 comments on commit 019b22f

Please sign in to comment.