Skip to content

Instantly share code, notes, and snippets.

@marcusramberg
Created September 25, 2010 13:42
Show Gist options
  • Save marcusramberg/596846 to your computer and use it in GitHub Desktop.
Save marcusramberg/596846 to your computer and use it in GitHub Desktop.
diff --git a/lib/Mojo/Message.pm b/lib/Mojo/Message.pm
index 2b12ec2..60f2576 100644
--- a/lib/Mojo/Message.pm
+++ b/lib/Mojo/Message.pm
@@ -240,7 +240,15 @@ sub dom {
and $charset = $1;
# Parse
- return $class->new(charset => $charset)->parse($self->body);
+ my $dom=$class->new(charset => $charset)->parse($self->body);
+
+ # Shortcut for find?
+ my $selector=shift;
+ if (defined $selector) {
+ return $dom->find($selector);
+ }
+
+ return $dom;
}
sub error {
@@ -784,8 +792,10 @@ Access message cookies.
=head2 C<dom>
my $dom = $message->dom;
+ my $res = $message->dom('a');
-Parses content into a L<Mojo::DOM> object.
+Parses content into a L<Mojo::DOM> object. Take an optional selector
+to perform a find on the dom directly.
Note that this method is EXPERIMENTAL and might change without warning!
=head2 C<error>
diff --git a/t/mojo/message.t b/t/mojo/message.t
index d687ac7..4dd3b8c 100644
--- a/t/mojo/message.t
+++ b/t/mojo/message.t
@@ -5,7 +5,7 @@ use warnings;
use utf8;
-use Test::More tests => 805;
+use Test::More tests => 806;
use File::Spec;
use File::Temp;
@@ -2045,11 +2045,14 @@ $res->parse("HTTP/1.1 200 OK\x0a");
$res->parse(
"Content-Type: application/atom+xml; charset=UTF-8; type=feed\x0a");
$res->parse("\x0a");
-$res->body('<test>Test</test>');
+$res->body('<p>foo <a href="/">bar</a><a href="/baz">baz</a></p>');
ok($res->is_done, 'request is done');
is( $res->headers->content_type,
'application/atom+xml; charset=UTF-8; type=feed',
'right "Content-Type" value'
);
ok($res->dom, 'dom built');
+$count=0;
+$res->dom('a')->each(sub {$count++});
+is($count,2, 'works with selector');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment