Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
improved all_text performance in Mojo::DOM
  • Loading branch information
kraih committed Jul 14, 2014
1 parent 9c54f98 commit d24ddfc
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 18 deletions.
1 change: 1 addition & 0 deletions Changes
@@ -1,5 +1,6 @@

5.14 2014-07-14
- Improved all_text performance in Mojo::DOM.
- Fixed deep recursion warnings in Mojo::DOM and Mojo::DOM::HTML. (jberger)

5.13 2014-07-13
Expand Down
29 changes: 11 additions & 18 deletions lib/Mojo/DOM.pm
Expand Up @@ -198,8 +198,16 @@ sub _all {
}

sub _all_text {
my $tree = shift->tree;
return _text([_nodes($tree)], shift, _trim($tree, @_));
my ($self, $recurse, $trim) = @_;

# Detect "pre" tag
my $tree = $self->tree;
if (!defined $trim || $trim) {
$trim = 1;
$_->[1] eq 'pre' and $trim = 0 for $self->_ancestors, $tree;
}

return _text([_nodes($tree)], $recurse, $trim);
}

sub _ancestors {
Expand Down Expand Up @@ -326,7 +334,7 @@ sub _text {
my $content = '';
if ($type eq 'tag' && $recurse) {
no warnings 'recursion';
$content = _text([_nodes($n)], 1, _trim($n, $trim));
$content = _text([_nodes($n)], 1, $n->[1] eq 'pre' ? 0 : $trim);
}

# Text
Expand All @@ -345,21 +353,6 @@ sub _text {
return $text;
}

sub _trim {
my ($n, $trim) = @_;

# Disabled
return 0 unless $n && ($trim = defined $trim ? $trim : 1);

# Detect "pre" tag
while ($n->[0] eq 'tag') {
return 0 if $n->[1] eq 'pre';
last unless $n = $n->[3];
}

return 1;
}

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

Expand Down

0 comments on commit d24ddfc

Please sign in to comment.