Skip to content

Commit

Permalink
the correct name is matches
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Feb 26, 2015
1 parent 993416f commit 409d32e
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 20 deletions.
7 changes: 4 additions & 3 deletions Changes
Expand Up @@ -31,10 +31,11 @@
- Renamed types attribute in Mojolicious::Types to mapping.
- Renamed current attribute in Mojolicious::Routes::Match to position.
- Renamed pattern attribute in Mojolicious::Routes::Route to unparsed.
- Renamed all_contents, contents, following_siblings, next_sibling, node,
preceding_siblings, previous_sibling and type methods in Mojo::DOM to
descendant_nodes, child_nodes, following_nodes, next_node, type,
- Renamed all_contents, contents, following_siblings, match, next_sibling,
node, preceding_siblings, previous_sibling and type methods in Mojo::DOM to
descendant_nodes, child_nodes, following_nodes, matches, next_node, type,
preceding_nodes, previous_node and tag.
- Renamed match method in Mojo::DOM::CSS to matches.
- Renamed extract and inject methods in Mojo::UserAgent::CookieJar to collect
and prepare.
- Renamed inject method in Mojo::UserAgent::Proxy to prepare.
Expand Down
19 changes: 9 additions & 10 deletions lib/Mojo/DOM.pm
Expand Up @@ -73,7 +73,7 @@ sub find { $_[0]->_collect(@{$_[0]->_css->select($_[1])}) }
sub following { _select($_[0]->_collect(@{$_[0]->_siblings(1)->[1]}), $_[1]) }
sub following_nodes { $_[0]->_collect(@{$_[0]->_siblings->[1]}) }

sub match { $_[0]->_css->match($_[1]) ? $_[0] : undef }
sub matches { $_[0]->_css->matches($_[1]) ? $_[0] : undef }

sub namespace {
my $self = shift;
Expand Down Expand Up @@ -278,7 +278,7 @@ sub _replace {
sub _select {
my ($collection, $selector) = @_;
return $collection unless $selector;
return $collection->new(grep { $_->match($selector) } @$collection);
return $collection->new(grep { $_->matches($selector) } @$collection);
}

sub _siblings {
Expand Down Expand Up @@ -615,21 +615,20 @@ node as L<Mojo::DOM> objects.
# "C"
$dom->parse('<p>A</p><!-- B -->C')->at('p')->following_nodes->last->content;
=head2 match
=head2 matches
my $result = $dom->match('div > p');
my $bool = $dom->matches('div > p');
Match the CSS selector against this element and return the L<Mojo::DOM> object
or return C<undef> if it didn't match. All selectors from
Check if this element matches the CSS selector. All selectors from
L<Mojo::DOM::CSS/"SELECTORS"> are supported.
# True
!!$dom->parse('<p class="a">A</p>')->at('p')->match('.a');
!!$dom->parse('<p class="a">A</p>')->at('p')->match('p[class]');
!!$dom->parse('<p class="a">A</p>')->at('p')->matches('.a');
!!$dom->parse('<p class="a">A</p>')->at('p')->matches('p[class]');
# False
!!$dom->parse('<p class="a">A</p>')->at('p')->match('.b');
!!$dom->parse('<p class="a">A</p>')->at('p')->match('p[id]');
!!$dom->parse('<p class="a">A</p>')->at('p')->matches('.b');
!!$dom->parse('<p class="a">A</p>')->at('p')->matches('p[id]');
=head2 namespace
Expand Down
8 changes: 4 additions & 4 deletions lib/Mojo/DOM/CSS.pm
Expand Up @@ -23,7 +23,7 @@ my $TOKEN_RE = qr/
(?:\s*([>+~]))? # Combinator
/x;

sub match {
sub matches {
my $tree = shift->tree;
return $tree->[0] ne 'tag' ? undef : _match(_compile(shift), $tree, $tree);
}
Expand Down Expand Up @@ -556,11 +556,11 @@ carefully since it is very dynamic.
L<Mojo::DOM::CSS> inherits all methods from L<Mojo::Base> and implements the
following new ones.
=head2 match
=head2 matches
my $bool = $css->match('head > title');
my $bool = $css->matches('head > title');
Match CSS selector against first node in L</"tree">.
Check if first node in L</"tree"> matches the CSS selector.
=head2 select
Expand Down
7 changes: 4 additions & 3 deletions t/mojo/dom.t
Expand Up @@ -288,7 +288,7 @@ is $dom->child_nodes->first->children->size, 0, 'no children';
is $dom->child_nodes->first->strip->parent, 'foo<b>bar</b>baz', 'no changes';
is $dom->child_nodes->first->at('b'), undef, 'no result';
is $dom->child_nodes->first->find('*')->size, 0, 'no results';
is $dom->child_nodes->first->match('*'), undef, 'no match';
ok !$dom->child_nodes->first->matches('*'), 'no match';
is_deeply $dom->child_nodes->first->attr, {}, 'no attributes';
is $dom->child_nodes->first->namespace, undef, 'no namespace';
is $dom->child_nodes->first->tag, undef, 'no tag';
Expand Down Expand Up @@ -641,8 +641,9 @@ is $dom->at('[s\:bk]'), undef, 'no result';
is $dom->at('[k]'), undef, 'no result';
is $dom->at('number')->ancestors('meta')->first->{xmlns}, 'uri:meta-ns',
'right attribute';
ok !!$dom->at('nons')->match('book > nons'), 'element did match';
ok !$dom->at('title')->match('book > nons > section'), 'element did not match';
ok $dom->at('nons')->matches('book > nons'), 'element did match';
ok !$dom->at('title')->matches('book > nons > section'),
'element did not match';

# Dots
$dom = Mojo::DOM->new(<<EOF);
Expand Down

0 comments on commit 409d32e

Please sign in to comment.