Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
fixed small memory leak in Mojo::DOM
  • Loading branch information
kraih committed Dec 30, 2012
1 parent 64c3d53 commit 04d56b9
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 55 deletions.
1 change: 1 addition & 0 deletions Changes
Expand Up @@ -3,6 +3,7 @@
- Modernized ".travis.yml".
- Improved monkey_patch to patch multiple functions at once. (jberger)
- Improved documentation.
- Fixed small memory leak in Mojo::DOM.

3.70 2012-12-23
- Added accept_interval setting to Hypnotoad.
Expand Down
79 changes: 25 additions & 54 deletions lib/Mojo/DOM.pm
Expand Up @@ -71,7 +71,7 @@ sub attrs {
return $self;
}

sub charset { shift->_parser(charset => @_) }
sub charset { shift->_html(charset => @_) }

sub children {
my ($self, $type) = @_;
Expand All @@ -81,14 +81,11 @@ sub children {
my $charset = $self->charset;
my $xml = $self->xml;
my $tree = $self->tree;
my $start = $tree->[0] eq 'root' ? 1 : 4;
for my $e (@$tree[$start .. $#$tree]) {
for my $e (@$tree[($tree->[0] eq 'root' ? 1 : 4) .. $#$tree]) {

# Make sure child is a tag
# Make sure child is the right type
next unless $e->[0] eq 'tag';
next if defined $type && $e->[1] ne $type;

# Add child
push @children, $self->new->charset($charset)->tree($e)->xml($xml);
}

Expand All @@ -98,19 +95,13 @@ sub children {
sub content_xml {
my $self = shift;

# Walk tree
my $result = '';
my $tree = $self->tree;
my $start = $tree->[0] eq 'root' ? 1 : 4;
for my $e (@$tree[$start .. $#$tree]) {
$result .= Mojo::DOM::HTML->new(
charset => $self->charset,
tree => $e,
xml => $self->xml
)->render;
}

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

sub find {
Expand Down Expand Up @@ -153,11 +144,7 @@ sub next { shift->_sibling(1) }

sub parent {
my $self = shift;

# Not a tag
return undef if (my $tree = $self->tree)->[0] eq 'root';

# Parent
return $self->new->charset($self->charset)->tree($tree->[3])
->xml($self->xml);
}
Expand Down Expand Up @@ -204,22 +191,9 @@ sub replace {

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

# Parse
$new = $self->_parse("$new");

# Replacements
my $tree = $self->tree;
my @new;
for my $e (@$new[1 .. $#$new]) {
$e->[3] = $tree if $e->[0] eq 'tag';
push @new, $e;
}

# Replace
my $start = $tree->[0] eq 'root' ? 1 : 4;
splice @$tree, $start, $#$tree, @new;

splice @$tree, $tree->[0] eq 'root' ? 1 : 4, $#$tree,
@{_parent($self->_parse("$new"), $tree)};
return $self;
}

Expand Down Expand Up @@ -275,7 +249,7 @@ sub text_before {

sub to_xml { shift->[0]->render }

sub tree { shift->_parser(tree => @_) }
sub tree { shift->_html(tree => @_) }

sub type {
my ($self, $type) = @_;
Expand All @@ -290,27 +264,24 @@ sub type {
return $self;
}

sub xml { shift->_parser(xml => @_) }
sub xml { shift->_html(xml => @_) }

sub _add {
my ($self, $offset, $new) = @_;

# Parse
$new = $self->_parse("$new");

# Not a tag
return $self if (my $tree = $self->tree)->[0] eq 'root';

# Find
# Find parent
my $parent = $tree->[3];
my $i = $parent->[0] eq 'root' ? 1 : 4;
for my $e (@$parent[$i .. $#$parent]) {
last if $e == $tree;
$i++;
}

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

return $self;
}
Expand All @@ -320,6 +291,13 @@ sub _elements {
return [@$e[($e->[0] eq 'root' ? 1 : 4) .. $#$e]];
}

sub _html {
my ($self, $method) = (shift, shift);
return $self->[0]->$method unless @_;
$self->[0]->$method(@_);
return $self;
}

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

Expand All @@ -342,17 +320,10 @@ sub _parse {
->parse(shift)->tree;
}

sub _parser {
my ($self, $method) = (shift, shift);
return $self->[0]->$method unless @_;
$self->[0]->$method(@_);
return $self;
}

sub _sibling {
my ($self, $next) = @_;

# Root
# Make sure we have a parent
return undef unless my $parent = $self->parent;

# Find previous or next sibling
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojolicious/Guides/Rendering.pod
Expand Up @@ -186,7 +186,7 @@ shortcut.

Some renderers such as C<ep> allow templates to be passed inline.

$self->render(inline => 'The result is <%= 1 + 1%>.');
$self->render(inline => 'The result is <%= 1 + 1 %>.');

Since auto detection depends on a path you might have to supply a C<handler>
too.
Expand Down

0 comments on commit 04d56b9

Please sign in to comment.