Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
deprecated Mojo::Collection::AUTOLOAD and Mojo::DOM::AUTOLOAD
  • Loading branch information
kraih committed Oct 29, 2014
1 parent bbd5f40 commit f785183
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 61 deletions.
15 changes: 4 additions & 11 deletions lib/Mojo/Collection.pm
Expand Up @@ -6,11 +6,15 @@ use Carp 'croak';
use Exporter 'import';
use List::Util;
use Mojo::ByteStream;
use Mojo::Util 'deprecated';
use Scalar::Util 'blessed';

our @EXPORT_OK = ('c');

# DEPRECATED in Tiger Face!
sub AUTOLOAD {
deprecated 'Mojo::Collection::AUTOLOAD is DEPRECATED '
. 'in favor of Mojo::Collection::pluck';
my $self = shift;
my ($package, $method) = our $AUTOLOAD =~ /^(.+)::(.+)$/;
croak "Undefined subroutine &${package}::$method called"
Expand Down Expand Up @@ -331,17 +335,6 @@ Alias for L<Mojo::Base/"tap">.
Create a new collection without duplicate elements.
=head1 AUTOLOAD
In addition to the L</"METHODS"> above, you can also call methods provided by
all elements in the collection directly and create a new collection from the
results, similar to L</"pluck">.
# "<h2>Test1</h2><h2>Test2</h2>"
my $collection = Mojo::Collection->new(
Mojo::DOM->new("<h1>1</h1>"), Mojo::DOM->new("<h1>2</h1>"));
$collection->at('h1')->type('h2')->prepend_content('Test')->join;
=head1 OPERATORS
L<Mojo::Collection> overloads the following operators.
Expand Down
61 changes: 21 additions & 40 deletions lib/Mojo/DOM.pm
Expand Up @@ -14,17 +14,19 @@ use List::Util 'first';
use Mojo::Collection;
use Mojo::DOM::CSS;
use Mojo::DOM::HTML;
use Mojo::Util 'squish';
use Mojo::Util qw(deprecated squish);
use Scalar::Util qw(blessed weaken);

# DEPRECATED in Tiger Face!
sub AUTOLOAD {
deprecated
'Mojo::DOM::AUTOLOAD is DEPRECATED in favor of Mojo::DOM::children';
my $self = shift;

my ($package, $method) = our $AUTOLOAD =~ /^(.+)::(.+)$/;
croak "Undefined subroutine &${package}::$method called"
unless blessed $self && $self->isa(__PACKAGE__);

# Search children of current element
my $children = $self->children($method);
return @$children > 1 ? $children : $children->[0] if @$children;
croak qq{Can't locate object method "$method" via package "$package"};
Expand Down Expand Up @@ -417,11 +419,7 @@ Mojo::DOM - Minimalistic HTML/XML DOM parser with CSS selectors
# Find
say $dom->at('#b')->text;
say $dom->find('p')->text;
say $dom->find('[id]')->attr('id');
# Walk
say $dom->div->p->[0]->text;
say $dom->div->children('p')->first->{id};
say $dom->find('[id]')->pluck(attr => 'id');
# Iterate
$dom->find('p[id]')->reverse->each(sub { say $_->{id} });
Expand All @@ -432,8 +430,8 @@ Mojo::DOM - Minimalistic HTML/XML DOM parser with CSS selectors
}
# Modify
$dom->div->p->last->append('<p id="c">456</p>');
$dom->find(':not(p)')->strip;
$dom->find('div p')->last->append('<p id="c">456</p>');
$dom->find(':not(p)')->pluck('strip');
# Render
say "$dom";
Expand All @@ -451,14 +449,12 @@ are lowercased and selectors need to be lowercase as well.
my $dom = Mojo::DOM->new('<P ID="greeting">Hi!</P>');
say $dom->at('p')->text;
say $dom->p->{id};
If XML processing instructions are found, the parser will automatically switch
into XML mode and everything becomes case sensitive.
my $dom = Mojo::DOM->new('<?xml version="1.0"?><P ID="greeting">Hi!</P>');
say $dom->at('P')->text;
say $dom->P->{ID};
XML detection can also be disabled with the L</"xml"> method.
Expand All @@ -480,8 +476,8 @@ Return a L<Mojo::Collection> object containing all nodes in DOM structure as
L<Mojo::DOM> objects.
# "<p><b>123</b></p>"
$dom->parse('<p><!-- Test --><b>123<!-- 456 --></b></p>')
->all_contents->grep(sub { $_->node eq 'comment' })->remove->first;
$dom->parse('<p><!-- Test --><b>123<!-- 456 --></b></p>')->all_contents
->grep(sub { $_->node eq 'comment' })->pluck('remove')->first;
=head2 all_text
Expand All @@ -492,10 +488,10 @@ Extract all text content from DOM structure, smart whitespace trimming is
enabled by default.
# "foo bar baz"
$dom->parse("<div>foo\n<p>bar</p>baz\n</div>")->div->all_text;
$dom->parse("<div>foo\n<p>bar</p>baz\n</div>")->at('div')->all_text;
# "foo\nbarbaz\n"
$dom->parse("<div>foo\n<p>bar</p>baz\n</div>")->div->all_text(0);
$dom->parse("<div>foo\n<p>bar</p>baz\n</div>")->at('div')->all_text(0);
=head2 ancestors
Expand Down Expand Up @@ -561,7 +557,7 @@ from L<Mojo::DOM::CSS/"SELECTORS"> are supported.
This element's attributes.
# List id attributes
say $dom->find('*')->attr('id')->compact;
say $dom->find('*')->pluck(attr => 'id')->compact;
=head2 children
Expand All @@ -584,7 +580,7 @@ Return this node's content or replace it with HTML/XML fragment (for C<root>
and C<tag> nodes) or raw content.
# "<b>Test</b>"
$dom->parse('<div><b>Test</b></div>')->div->content;
$dom->parse('<div><b>Test</b></div>')->at('div')->content;
# "<div><h1>123</h1></div>"
$dom->parse('<div><h1>Test</h1></div>')->at('h1')->content('123')->root;
Expand Down Expand Up @@ -627,10 +623,11 @@ All selectors from L<Mojo::DOM::CSS/"SELECTORS"> are supported.
my $id = $dom->find('div')->[23]{id};
# Extract information from multiple elements
my @headers = $dom->find('h1, h2, h3')->text->each;
my @headers = $dom->find('h1, h2, h3')->pluck('text')->each;
# Count all the different tags
my $hash = $dom->find('*')->type->reduce(sub { $a->{$b}++; $a }, {});
my $hash = $dom->find('*')->pluck('type')
->reduce(sub { $a->{$b}++; $a }, {});
# Find elements with a class that contains dots
my @divs = $dom->find('div.foo\.bar')->each;
Expand Down Expand Up @@ -800,7 +797,7 @@ L<Mojo::Collection> object containing these elements as L<Mojo::DOM> objects.
All selectors from L<Mojo::DOM::CSS/"SELECTORS"> are supported.
# List types of sibling elements
say $dom->siblings->type;
say $dom->siblings->pluck('type');
=head2 strip
Expand All @@ -826,10 +823,10 @@ Extract text content from this element only (not including child elements),
smart whitespace trimming is enabled by default.
# "foo baz"
$dom->parse("<div>foo\n<p>bar</p>baz\n</div>")->div->text;
$dom->parse("<div>foo\n<p>bar</p>baz\n</div>")->at('div')->text;
# "foo\nbaz\n"
$dom->parse("<div>foo\n<p>bar</p>baz\n</div>")->div->text(0);
$dom->parse("<div>foo\n<p>bar</p>baz\n</div>")->at('div')->text(0);
=head2 to_string
Expand All @@ -838,7 +835,7 @@ smart whitespace trimming is enabled by default.
Render this node and its content to HTML/XML.
# "<b>Test</b>"
$dom->parse('<div><b>Test</b></div>')->div->b->to_string;
$dom->parse('<div><b>Test</b></div>')->at('div b')->to_string;
=head2 tree
Expand All @@ -856,7 +853,7 @@ carefully since it is very dynamic.
This element's type.
# List types of child elements
say $dom->children->type;
say $dom->children->pluck('type');
=head2 val
Expand Down Expand Up @@ -916,22 +913,6 @@ children of the first innermost element.
Disable HTML semantics in parser and activate case sensitivity, defaults to
auto detection based on processing instructions.
=head1 AUTOLOAD
In addition to the L</"METHODS"> above, many child elements are also
automatically available as object methods, which return a L<Mojo::DOM> or
L<Mojo::Collection> object, depending on number of children. For more power
and consistent results you can also use L</"children">.
# "Test"
$dom->parse('<p>Test</p>')->p->text;
# "123"
$dom->parse('<div>Test</div><div>123</div>')->div->[2]->text;
# "Test"
$dom->parse('<div>Test</div>')->div->text;
=head1 OPERATORS
L<Mojo::DOM> overloads the following operators.
Expand Down
4 changes: 2 additions & 2 deletions lib/Mojo/Message.pm
Expand Up @@ -488,11 +488,11 @@ whole message body needs to be loaded into memory to parse it, so you have to
make sure it is not excessively large, there's a 10MB limit by default.
# Perform "find" right away
say $msg->dom('h1, h2, h3')->text;
say $msg->dom('h1, h2, h3')->pluck('text');
# Use everything else Mojo::DOM has to offer
say $msg->dom->at('title')->text;
say $msg->dom->html->body->children->type->uniq;
say $msg->dom->at('body')->children->type->uniq;
=head2 error
Expand Down
5 changes: 3 additions & 2 deletions lib/Mojo/UserAgent.pm
Expand Up @@ -378,10 +378,11 @@ Mojo::UserAgent - Non-blocking I/O HTTP and WebSocket user agent
->res->json('/results/0/title');
# Extract data from HTML and XML resources
say $ua->get('www.perl.org')->res->dom->html->head->title->text;
say $ua->get('www.perl.org')->res->dom->at('title')->text;
# Scrape the latest headlines from a news site with CSS selectors
say $ua->get('blogs.perl.org')->res->dom('h2 > a')->text->shuffle;
say $ua->get('blogs.perl.org')
->res->dom->find('h2 > a')->pluck('text')->shuffle;
# Search DuckDuckGo anonymously through Tor
$ua->proxy->http('socks://127.0.0.1:9050');
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojolicious/Guides/Cookbook.pod
Expand Up @@ -1197,7 +1197,7 @@ This can be an invaluable tool for testing your applications.
For quick hacks and especially testing, L<ojo> one-liners are also a great
choice.

$ perl -Mojo -E 'say g("mojolicio.us")->dom->html->head->title->text'
$ perl -Mojo -E 'say g("mojolicio.us")->dom->at("title")->text'

=head1 APPLICATIONS

Expand Down
6 changes: 3 additions & 3 deletions lib/Mojolicious/Lite.pm
Expand Up @@ -874,7 +874,7 @@ L<Mojo::JSON> and L<Mojo::DOM> this can be a very powerful tool.
my $c = shift;
my $url = $c->param('url') || 'http://mojolicio.us';
my $dom = $c->ua->get($url)->res->dom;
$c->render(json => [$dom->find('h1, h2, h3')->text->each]);
$c->render(json => [$dom->find('h1, h2, h3')->pluck('text')->each]);
};
# Non-blocking
Expand All @@ -898,8 +898,8 @@ L<Mojo::JSON> and L<Mojo::DOM> this can be a very powerful tool.
sub {
my ($delay, $mojo, $cpan) = @_;
$c->render(json => {
mojo => $mojo->res->dom->html->head->title->text,
cpan => $cpan->res->dom->html->head->title->text
mojo => $mojo->res->dom->at('title')->text,
cpan => $cpan->res->dom->at('title')->text
});
}
);
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojolicious/Plugin/PODRenderer.pm
Expand Up @@ -42,7 +42,7 @@ sub _html {
my $perldoc = $c->url_for('/perldoc/');
$_->{href} =~ s!^https://metacpan\.org/pod/!$perldoc!
and $_->{href} =~ s!::!/!gi
for $dom->find('a[href]')->attr->each;
for $dom->find('a[href]')->pluck('attr')->each;

# Rewrite code blocks for syntax highlighting and correct indentation
for my $e ($dom->find('pre > code')->each) {
Expand Down
2 changes: 1 addition & 1 deletion lib/ojo.pm
Expand Up @@ -125,7 +125,7 @@ resulting L<Mojo::Message::Response> object.
Perform C<GET> request with L<Mojo::UserAgent/"get"> and return resulting
L<Mojo::Message::Response> object.
$ perl -Mojo -E 'say g("mojolicio.us")->dom("h1, h2, h3")->text'
$ perl -Mojo -E 'say g("mojolicio.us")->dom("h1, h2, h3")->pluck("text")'
=head2 h
Expand Down

0 comments on commit f785183

Please sign in to comment.