Skip to content

Commit

Permalink
fixed small selector bug in get command
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Jul 17, 2014
1 parent fbbafd9 commit 9e9ac56
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 11 deletions.
3 changes: 2 additions & 1 deletion Changes
@@ -1,6 +1,7 @@

5.15 2014-07-16
5.15 2014-07-17
- Improved Mojo::DOM::HTML performance slightly.
- Fixed small selector bug in get command.

5.14 2014-07-14
- Improved all_text performance in Mojo::DOM.
Expand Down
18 changes: 8 additions & 10 deletions lib/Mojolicious/Command/get.pm
Expand Up @@ -91,32 +91,30 @@ sub _say { length && say encode('UTF-8', $_) for @_ }
sub _select {
my ($buffer, $selector, $charset, @args) = @_;

# Keep a strong reference to the root
$buffer = decode($charset, $buffer) // $buffer if $charset;
my $results = Mojo::DOM->new($buffer)->find($selector);
my $dom = Mojo::DOM->new($buffer);
my $results = $dom->find($selector);

while (defined(my $command = shift @args)) {

# Number
($results = [$results->[$command]])->[0] ? next : return
if $command =~ /^\d+$/;
($results = $results->slice($command)) and next if $command =~ /^\d+$/;

# Text
return _say(map { $_->text } @$results) if $command eq 'text';
return _say($results->text) if $command eq 'text';

# All text
return _say(map { $_->all_text } @$results) if $command eq 'all';
return _say($results->all_text) if $command eq 'all';

# Attribute
if ($command eq 'attr') {
return unless my $name = shift @args;
return _say(map { $_->attr->{$name} } @$results);
}
return _say($results->attr($args[0] // '')->each) if $command eq 'attr';

# Unknown
die qq{Unknown command "$command".\n};
}

_say(@$results);
_say($results);
}

1;
Expand Down

0 comments on commit 9e9ac56

Please sign in to comment.