Skip to content

Commit

Permalink
small optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Dec 29, 2012
1 parent b047b50 commit 64c3d53
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 18 deletions.
2 changes: 1 addition & 1 deletion Changes
@@ -1,5 +1,5 @@

3.71 2012-12-29
3.71 2012-12-30
- Modernized ".travis.yml".
- Improved monkey_patch to patch multiple functions at once. (jberger)
- Improved documentation.
Expand Down
27 changes: 14 additions & 13 deletions lib/Mojo/DOM.pm
Expand Up @@ -78,17 +78,18 @@ sub children {

# Walk tree
my @children;
my $tree = $self->tree;
my $start = $tree->[0] eq 'root' ? 1 : 4;
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]) {

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

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

return Mojo::Collection->new(@children);
Expand All @@ -115,15 +116,12 @@ sub content_xml {
sub find {
my ($self, $selector) = @_;

# Match selector against tree
my $results = Mojo::DOM::CSS->new(tree => $self->tree)->select($selector);

# Upgrade results
@$results
= map { $self->new->charset($self->charset)->tree($_)->xml($self->xml) }
@$results;

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

sub namespace {
Expand Down Expand Up @@ -324,6 +322,8 @@ sub _elements {

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

# Link parent to children
my @new;
for my $e (@$children[1 .. $#$children]) {
if ($e->[0] eq 'tag') {
Expand All @@ -332,6 +332,7 @@ sub _parent {
}
push @new, $e;
}

return \@new;
}

Expand Down
6 changes: 2 additions & 4 deletions lib/Mojolicious/Guides/Cookbook.pod
Expand Up @@ -734,12 +734,10 @@ sense for a standalone parser.
say 'Title: ', $tx->res->dom->at('head > title')->text;

# Extract headings
$tx->res->dom('h1, h2, h3')->each(sub {
say 'Heading: ', shift->all_text;
});
$tx->res->dom('h1, h2, h3')->each(sub { say 'Heading: ', shift->all_text });

# Visit all elements recursively to extract more than just text
for my $e ($tx->res->dom->find('*')->each) {
for my $e ($tx->res->dom('*')->each) {

# Text before this element
print $e->text_before(0);
Expand Down

0 comments on commit 64c3d53

Please sign in to comment.