Skip to content

Commit

Permalink
renamed search method in Mojo::DOM to find
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Jul 21, 2010
1 parent 47cd27a commit 18856d9
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 43 deletions.
1 change: 1 addition & 0 deletions Changes
Expand Up @@ -19,6 +19,7 @@ This file documents the revision history for Perl extension Mojolicious.
- Added say method to Mojo::ByteStream.
- Added custom socket support to Mojo::Client.
- Renamed attributes method in Mojo::DOM to attrs.
- Renamed search method in Mojo::DOM to find.
- Cleaned up tests. (memowe)
- Cleaned up regular expressions in Mojo::DOM. (mpu)
- Improved Mojo::Client error logging.
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/Client.pm
Expand Up @@ -1199,7 +1199,7 @@ Mojo::Client - Async IO HTTP 1.1 And WebSocket Client
# Scrape the latest headlines from a news site
my $news = 'http://digg.com';
$client->get($news)->res->dom->search("h3 > a.offsite")->each(sub {
$client->get($news)->res->dom->find("h3 > a.offsite")->each(sub {
print shift->text . "\n";
});
Expand Down
3 changes: 2 additions & 1 deletion lib/Mojo/CookieJar.pm
Expand Up @@ -35,7 +35,8 @@ sub add {
$cookie->max_age(0) unless $cookie->expires || $cookie->max_age;

# Cookie too big
next if (length($cookie->value || '')) > $self->max_cookie_size;
my $value = $cookie->value;
next if length(defined $value ? $value : '') > $self->max_cookie_size;

# Initialize
$self->{_jar}->{$domain} ||= [];
Expand Down
54 changes: 27 additions & 27 deletions lib/Mojo/DOM.pm
Expand Up @@ -92,7 +92,7 @@ sub all_text {
return $text;
}

sub at { shift->search(@_)->[0] }
sub at { shift->find(@_)->[0] }

sub attrs {
my $self = shift;
Expand Down Expand Up @@ -129,6 +129,16 @@ sub children {
return \@children;
}

sub find {
my ($self, $css) = @_;

# Parse CSS selectors
my $pattern = $self->_parse_css($css);

# Filter tree
return $self->_select($self->tree, $pattern);
}

sub name {
my ($self, $name) = @_;

Expand Down Expand Up @@ -275,16 +285,6 @@ sub root {
return $self->new(charset => $self->charset, tree => $root);
}

sub search {
my ($self, $css) = @_;

# Parse CSS selectors
my $pattern = $self->_parse_css($css);

# Filter tree
return $self->_select($self->tree, $pattern);
}

sub text {
my $self = shift;

Expand Down Expand Up @@ -855,7 +855,7 @@ Mojo::DOM - Minimalistic XML DOM Parser With CSS3 Selectors
print $b->text;
# Iterate
$dom->search('div[id]')->each(sub { print shift->text });
$dom->find('div[id]')->each(sub { print shift->text });
=head1 DESCRIPTION
Expand All @@ -881,26 +881,26 @@ An element of type C<E>.
=item C<E[foo]>
my $links = $dom->search('a[href]');
my $links = $dom->find('a[href]');
An C<E> element with a C<foo> attribute.
=item C<E[foo="bar"]>
my $fields = $dom->search('input[name="foo"]');
my $fields = $dom->find('input[name="foo"]');
An C<E> element whose C<foo> attribute value is exactly equal to C<bar>.
=item C<E[foo^="bar"]>
my $fields = $dom->search('input[name^="f"]');
my $fields = $dom->find('input[name^="f"]');
An C<E> element whose C<foo> attribute value begins exactly with the string
C<bar>.
=item C<E[foo$="bar"]>
my $fields = $dom->search('input[name$="o"]');
my $fields = $dom->find('input[name$="o"]');
An C<E> element whose C<foo> attribute value ends exactly with the string
C<bar>.
Expand All @@ -913,13 +913,13 @@ An C<E> element, root of the document.
=item C<E F>
my $headlines = $dom->search('div h1');
my $headlines = $dom->find('div h1');
An C<F> element descendant of an C<E> element.
=item C<E E<gt> F>
my $headlines = $dom->search('html > body > div > h1');
my $headlines = $dom->find('html > body > div > h1');
An C<F> element child of an C<E> element.
Expand Down Expand Up @@ -958,7 +958,7 @@ Extract all text content from DOM structure.
my $result = $dom->at('html title');
Search for a single element with CSS3 selectors.
Find a single element with CSS3 selectors.
=head2 C<attrs>
Expand All @@ -972,6 +972,14 @@ Element attributes.
Children of element.
=head2 C<find>
my $results = $dom->find('html title');
Find elements with CSS3 selectors.
$dom->find('div')->each(sub { print shift->text });
=head2 C<name>
my $name = $dom->name;
Expand Down Expand Up @@ -1015,14 +1023,6 @@ Replace element content.
Find root element.
=head2 C<search>
my $results = $dom->search('html title');
Search for elements with CSS3 selectors.
$dom->search('div')->each(sub { print shift->text });
=head2 C<text>
my $text = $dom->text;
Expand Down
28 changes: 14 additions & 14 deletions t/mojo/dom.t
Expand Up @@ -19,7 +19,7 @@ my $dom = Mojo::DOM->new;
$dom->parse('<div><div id="a">A</div><div id="b">B</div></div>');
is($dom->at('#b')->text, 'B', 'right text');
my @div;
$dom->search('div[id]')->each(sub { push @div, shift->text });
$dom->find('div[id]')->each(sub { push @div, shift->text });
is_deeply(\@div, [qw/A B/], 'found all div elements with id');

# Simple nesting (tree structure)
Expand Down Expand Up @@ -130,12 +130,12 @@ $dom->parse(<<EOF);
</body>
</html>
EOF
my $p = $dom->search('body > #container > div p[id]');
my $p = $dom->find('body > #container > div p[id]');
is($p->[0]->attrs->{id}, 'foo', 'right id attribute');
is($p->[1], undef, 'no second result');
my @p;
@div = ();
$dom->search('div')->each(sub { push @div, $_->attrs->{id} })->search('p')
$dom->find('div')->each(sub { push @div, $_->attrs->{id} })->find('p')
->each(sub { push @p, $_->attrs->{id} });
is_deeply(\@p, [qw/foo bar/], 'found all p elements');
my $ids = [qw/container header logo buttons buttons content/];
Expand Down Expand Up @@ -221,7 +221,7 @@ is("$dom", '<div>foo<foo>bar</foo>bar</div>', 'right text');
$dom->at('foo')->replace(Mojo::DOM->new->parse('text'));
is("$dom", '<div>footextbar</div>', 'right text');
$dom->parse('<div>foo</div><div>bar</div>');
$dom->search('div')->each(sub { shift->replace('<p>test</p>') });
$dom->find('div')->each(sub { shift->replace('<p>test</p>') });
is("$dom", '<p>test</p><p>test</p>', 'right text');
$dom->parse('<div>foo<p>lalala</p>bar</div>');
$dom->replace('');
Expand All @@ -232,7 +232,7 @@ $dom->replace('');
is("$dom", '', 'right text');
$dom->replace('<div>foo<p>lalala</p>bar</div>');
is("$dom", '<div>foo<p>lalala</p>bar</div>', 'right text');
$dom->search('p')->each(sub { shift->replace('') });
$dom->find('p')->each(sub { shift->replace('') });
is("$dom", '<div>foobar</div>', 'right text');

# Replace element content
Expand All @@ -242,9 +242,9 @@ is("$dom", '<div>foo<p>bar</p>bar</div>', 'right text');
$dom->at('p')->replace_content(Mojo::DOM->new->parse('text'));
is("$dom", '<div>foo<p>text</p>bar</div>', 'right text');
$dom->parse('<div>foo</div><div>bar</div>');
$dom->search('div')->each(sub { shift->replace_content('<p>test</p>') });
$dom->find('div')->each(sub { shift->replace_content('<p>test</p>') });
is("$dom", '<div><p>test</p></div><div><p>test</p></div>', 'right text');
$dom->search('p')->each(sub { shift->replace_content('') });
$dom->find('p')->each(sub { shift->replace_content('') });
is("$dom", '<div><p /></div><div><p /></div>', 'right text');
$dom->parse('<div><p id="☃" /></div>');
$dom->at('#☃')->replace_content('');
Expand All @@ -269,7 +269,7 @@ $dom->parse(<<EOF);
</table>
EOF
my @data;
for my $tr ($dom->search('table tr')->each) {
for my $tr ($dom->find('table tr')->each) {
for my $td (@{$tr->children}) {
push @data, $td->name, $td->all_text;
}
Expand Down Expand Up @@ -306,11 +306,11 @@ $dom->parse(<<EOF);
</channel>
</rss>
EOF
is($dom->search('rss')->[0]->attrs->{version}, '2.0', 'right version');
is($dom->at('extension')->attrs->{'foo:id'}, 'works', 'right id');
is($dom->find('rss')->[0]->attrs->{version}, '2.0', 'right version');
is($dom->at('extension')->attrs->{'foo:id'}, 'works', 'right id');
like($dom->at('#works')->text, qr/\[awesome\]\]/, 'right text');
like($dom->at('[id="works"]')->text, qr/\[awesome\]\]/, 'right text');
is($dom->search('description')->[1]->text, '<p>trololololo>', 'right text');
is($dom->find('description')->[1]->text, '<p>trololololo>', 'right text');
is($dom->at('pubdate')->text, 'Mon, 12 Jul 2010 20:42:00', 'right text');

# Yadis
Expand All @@ -329,7 +329,7 @@ $dom->parse(<<'EOF');
EOF
is($dom->at('xrds')->namespace, 'xri://$xrds', 'right namespace');
is($dom->at('xrd')->namespace, 'xri://$xrd*($v*2.0)', 'right namespace');
my $s = $dom->search('xrds xrd service');
my $s = $dom->find('xrds xrd service');
is($s->[0]->at('type')->text, 'http://o.r.g/sso/2.0', 'right text');
is($s->[0]->namespace, 'xri://$xrd*($v*2.0)', 'right namespace');
is($s->[1]->at('type')->text, 'http://o.r.g/sso/1.0', 'right text');
Expand Down Expand Up @@ -360,7 +360,7 @@ $dom->parse(<<'EOF');
EOF
is($dom->at('xrds')->namespace, 'xri://$xrds', 'right namespace');
is($dom->at('xrd')->namespace, 'xri://$xrd*($v*2.0)', 'right namespace');
$s = $dom->search('xrds xrd service');
$s = $dom->find('xrds xrd service');
is($s->[0]->at('type')->text, 'http://o.r.g/sso/3.0', 'right text');
is($s->[0]->namespace, 'xri://$xrd*($v*2.0)', 'right namespace');
is($s->[1]->at('type')->text, 'http://o.r.g/sso/4.0', 'right text');
Expand All @@ -374,5 +374,5 @@ is($s->[4], undef, 'no text');
# Result and iterator order
$dom->parse('<a><b>1</b></a><b>2</b><b>3</b>');
my @numbers;
$dom->search("b")->each(sub { push @numbers, shift->text });
$dom->find("b")->each(sub { push @numbers, shift->text });
is_deeply(\@numbers, [1, 2, 3], 'right order');

0 comments on commit 18856d9

Please sign in to comment.