Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
renamed first to select_one
  • Loading branch information
kraih committed Dec 16, 2013
1 parent 6028ab1 commit 9757379
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 16 deletions.
2 changes: 1 addition & 1 deletion Changes
@@ -1,6 +1,6 @@

4.61 2013-12-16
- Added first method to Mojo::DOM::CSS.
- Added select_one method to Mojo::DOM::CSS.
- Improved performance of Mojo::DOM::at significantly.

4.60 2013-12-11
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/DOM.pm
Expand Up @@ -51,7 +51,7 @@ sub append_content {

sub at {
my $self = shift;
return undef unless my $result = $self->_css->first(@_);
return undef unless my $result = $self->_css->select_one(@_);
return $self->new->tree($result)->xml($self->xml);
}

Expand Down
27 changes: 13 additions & 14 deletions lib/Mojo/DOM/CSS.pm
Expand Up @@ -33,16 +33,15 @@ my $TOKEN_RE = qr/
)?
/x;

sub first { shift->_select(1, @_) }

sub match {
my $self = shift;
my $tree = $self->tree;
return undef if $tree->[0] eq 'root';
return $self->_match($self->_compile(shift), $tree, $tree);
}

sub select { shift->_select(0, @_) }
sub select { shift->_select(0, @_) }
sub select_one { shift->_select(1, @_) }

sub _ancestor {
my ($self, $selectors, $current, $tree) = @_;
Expand Down Expand Up @@ -296,10 +295,10 @@ sub _regex {
}

sub _select {
my ($self, $first) = (shift, shift);
my ($self, $one, $selector) = @_;

my @results;
my $pattern = $self->_compile(shift);
my $pattern = $self->_compile($selector);
my $tree = $self->tree;
my @queue = ($tree);
while (my $current = shift @queue) {
Expand All @@ -309,14 +308,14 @@ sub _select {
if ($type eq 'tag') {
unshift @queue, @$current[4 .. $#$current];
next unless $self->_match($pattern, $current, $tree);
$first ? return $current : push @results, $current;
$one ? return $current : push @results, $current;
}

# Root
elsif ($type eq 'root') { unshift @queue, @$current[1 .. $#$current] }
}

return $first ? undef : \@results;
return $one ? undef : \@results;
}

sub _selector {
Expand Down Expand Up @@ -616,13 +615,6 @@ carefully since it is very dynamic.
L<Mojo::DOM::CSS> inherits all methods from L<Mojo::Base> and implements the
following new ones.
=head2 first
my $result = $css->first('head > title');
Run CSS selector against L</"tree"> and stop immediately after the first node
matched.
=head2 match
my $bool = $css->match('head > title');
Expand All @@ -635,6 +627,13 @@ Match CSS selector against first node in L</"tree">.
Run CSS selector against L</"tree">.
=head2 select_one
my $result = $css->select_one('head > title');
Run CSS selector against L</"tree"> and stop as soon as the first node
matched.
=head1 SEE ALSO
L<Mojolicious>, L<Mojolicious::Guides>, L<http://mojolicio.us>.
Expand Down

0 comments on commit 9757379

Please sign in to comment.