Skip to content

Commit

Permalink
deprecated Mojo::DOM::val and stringify support in Mojo::Collection (c…
Browse files Browse the repository at this point in the history
…loses #684)
  • Loading branch information
kraih committed Nov 2, 2014
1 parent e45c263 commit 3a29749
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 97 deletions.
2 changes: 2 additions & 0 deletions Changes
@@ -1,5 +1,7 @@

5.57 2014-11-02
- Deprecated stringify support in Mojo::Collection.
- Deprecated Mojo::DOM::val.

5.56 2014-10-29
- Deprecated Mojo::Collection::AUTOLOAD in favor of Mojo::Collection::pluck.
Expand Down
27 changes: 9 additions & 18 deletions lib/Mojo/Collection.pm
@@ -1,6 +1,5 @@
package Mojo::Collection;
use Mojo::Base -strict;
use overload bool => sub {1}, '""' => sub { shift->join("\n") }, fallback => 1;

use Carp 'croak';
use Exporter 'import';
Expand All @@ -9,6 +8,15 @@ use Mojo::ByteStream;
use Mojo::Util 'deprecated';
use Scalar::Util 'blessed';

# DEPRECATED in Tiger Face!
use overload
bool => sub {1},
'""' => sub {
deprecated 'Stringify support in Mojo::Collection is DEPRECATED';
shift->join("\n");
},
fallback => 1;

our @EXPORT_OK = ('c');

# DEPRECATED in Tiger Face!
Expand Down Expand Up @@ -143,7 +151,6 @@ Mojo::Collection - Collection
# Stringify collection
say $collection->join("\n");
say "$collection";
# Use the alternative constructor
use Mojo::Collection 'c';
Expand Down Expand Up @@ -336,22 +343,6 @@ Alias for L<Mojo::Base/"tap">.
Create a new collection without duplicate elements.
=head1 OPERATORS
L<Mojo::Collection> overloads the following operators.
=head2 bool
my $bool = !!$collection;
Always true.
=head2 stringify
my $str = "$collection";
Stringify elements in collection and L</"join"> them with newlines.
=head1 SEE ALSO
L<Mojolicious>, L<Mojolicious::Guides>, L<http://mojolicio.us>.
Expand Down
32 changes: 8 additions & 24 deletions lib/Mojo/DOM.pm
Expand Up @@ -180,7 +180,9 @@ sub type {
return $self;
}

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

# "option"
Expand Down Expand Up @@ -420,8 +422,8 @@ Mojo::DOM - Minimalistic HTML/XML DOM parser with CSS selectors
# Find
say $dom->at('#b')->text;
say $dom->find('p')->pluck('text');
say $dom->find('[id]')->pluck(attr => 'id');
say $dom->find('p')->pluck('text')->join("\n");
say $dom->find('[id]')->pluck(attr => 'id')->join("\n");
# Iterate
$dom->find('p[id]')->reverse->each(sub { say $_->{id} });
Expand Down Expand Up @@ -505,7 +507,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 ancestor elements
say $dom->ancestors->pluck('type');
say $dom->ancestors->pluck('type')->join("\n");
=head2 append
Expand Down Expand Up @@ -559,7 +561,7 @@ from L<Mojo::DOM::CSS/"SELECTORS"> are supported.
This element's attributes.
# List id attributes
say $dom->find('*')->pluck(attr => 'id')->compact;
say $dom->find('*')->pluck(attr => 'id')->compact->join("\n");
=head2 children
Expand Down Expand Up @@ -798,7 +800,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->pluck('type');
say $dom->siblings->pluck('type')->join("\n");
=head2 strip
Expand Down Expand Up @@ -854,25 +856,7 @@ carefully since it is very dynamic.
This element's type.
# List types of child elements
say $dom->children->pluck('type');
=head2 val
my $collection = $dom->val;
Extract values from C<button>, C<input>, C<option>, C<select> or C<textarea>
element and return a L<Mojo::Collection> object containing these values. In
the case of C<select>, find all C<option> elements it contains that have a
C<selected> attribute and extract their values.
# "b"
$dom->parse('<input name="a" value="b">')->at('input')->val;
# "c"
$dom->parse('<option value="c">Test</option>')->at('option')->val;
# "d"
$dom->parse('<option>d</option>')->at('option')->val;
say $dom->children->pluck('type')->join("\n");
=head2 wrap
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/Message.pm
Expand Up @@ -492,7 +492,7 @@ make sure it is not excessively large, there's a 10MB limit by default.
# Use everything else Mojo::DOM has to offer
say $msg->dom->at('title')->text;
say $msg->dom->at('body')->children->pluck('type')->uniq;
say $msg->dom->at('body')->children->pluck('type')->uniq->join("\n");
=head2 error
Expand Down
3 changes: 2 additions & 1 deletion lib/Mojo/UserAgent.pm
Expand Up @@ -381,7 +381,8 @@ Mojo::UserAgent - Non-blocking I/O HTTP and WebSocket user agent
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->find('h2 > a')->pluck('text');
say $ua->get('blogs.perl.org')
->res->dom->find('h2 > a')->pluck('text')->join("\n");
# Search DuckDuckGo anonymously through Tor
$ua->proxy->http('socks://127.0.0.1:9050');
Expand Down
2 changes: 1 addition & 1 deletion lib/Test/Mojo.pm
Expand Up @@ -649,7 +649,7 @@ arguments as L<Mojo::UserAgent/"get">, except for the callback.
# Run additional tests on the transaction
$t->get_ok('/foo')->status_is(200);
is $t->tx->res->dom->at('input')->val, 'whatever', 'right value';
is $t->tx->res->dom->at('input')->{value}, 'whatever', 'right value';
=head2 head_ok
Expand Down
8 changes: 4 additions & 4 deletions t/mojo/collection.t
Expand Up @@ -75,8 +75,6 @@ is $collection->join(''), '123', 'right result';
is $collection->join('---'), '1---2---3', 'right result';
is $collection->join("\n"), "1\n2\n3", 'right result';
is $collection->join('/')->url_escape, '1%2F2%2F3', 'right result';
$collection = c(c(1, 2, 3), c(3, 2, 1));
is $collection->join(''), "1\n2\n33\n2\n1", 'right result';

# map
$collection = c(1, 2, 3);
Expand Down Expand Up @@ -147,8 +145,10 @@ is_deeply [$collection->slice(6 .. 9)->each], [7, 10, 9, 8], 'right result';
is c({foo => 'bar'}, {foo => 'baz'})->pluck('foo')->join, 'barbaz',
'right result';
$collection = c(c(1, 2, 3), c(4, 5, 6), c(7, 8, 9));
is $collection->pluck('reverse'), "3\n2\n1\n6\n5\n4\n9\n8\n7", 'right result';
is $collection->pluck(join => '-'), "1-2-3\n4-5-6\n7-8-9", 'right result';
is $collection->pluck('reverse')->pluck(join => "\n")->join("\n"),
"3\n2\n1\n6\n5\n4\n9\n8\n7", 'right result';
is $collection->pluck(join => '-')->join("\n"), "1-2-3\n4-5-6\n7-8-9",
'right result';

# uniq
$collection = c(1, 2, 3, 2, 3, 4, 5, 4);
Expand Down
55 changes: 8 additions & 47 deletions t/mojo/dom.t
Expand Up @@ -1427,7 +1427,8 @@ is $dom->find('table > colgroup > col')->[2]->attr->{id}, 'bar',
is $dom->at('table > thead > tr > th')->text, 'A', 'right text';
is $dom->find('table > thead > tr > th')->[1]->text, 'D', 'right text';
is $dom->at('table > tbody > tr > td')->text, 'B', 'right text';
is $dom->find('table > tbody > tr > td')->pluck('text'), "B\nE", 'right text';
is $dom->find('table > tbody > tr > td')->pluck('text')->join("\n"), "B\nE",
'right text';

# Optional "colgroup", "tbody", "tr", "th" and "td" tags
$dom = Mojo::DOM->new->parse(<<EOF);
Expand Down Expand Up @@ -1993,10 +1994,10 @@ is $dom->find('a B c')->size, 2, 'right number of elements';
@results = ();
$dom->find('a B c')->each(sub { push @results, $_->text });
is_deeply \@results, [qw(bar baz)], 'right results';
is $dom->find('a B c'), qq{<c id="three">bar</c>\n<c ID="four">baz</c>},
'right result';
is $dom->find('a B c')->join("\n"),
qq{<c id="three">bar</c>\n<c ID="four">baz</c>}, 'right result';
is_deeply [keys %$dom], [], 'root has no attributes';
is $dom->find('#nothing'), '', 'no result';
is $dom->find('#nothing')->join, '', 'no result';

# Direct hash access to attributes in HTML mode
$dom = Mojo::DOM->new(<<EOF);
Expand Down Expand Up @@ -2025,10 +2026,10 @@ is $dom->find('a b c')->size, 2, 'right number of elements';
@results = ();
$dom->find('a b c')->each(sub { push @results, $_->text });
is_deeply \@results, [qw(bar baz)], 'right results';
is $dom->find('a b c'), qq{<c id="three">bar</c>\n<c id="four">baz</c>},
'right result';
is $dom->find('a b c')->join("\n"),
qq{<c id="three">bar</c>\n<c id="four">baz</c>}, 'right result';
is_deeply [keys %$dom], [], 'root has no attributes';
is $dom->find('#nothing'), '', 'no result';
is $dom->find('#nothing')->join, '', 'no result';

# Append and prepend content
$dom = Mojo::DOM->new('<a><b>Test<c /></b></a>');
Expand Down Expand Up @@ -2277,46 +2278,6 @@ is $dom->find('div > ul li')->[2], undef, 'no result';
is $dom->find('div > ul ul')->[0]->text, 'C', 'right text';
is $dom->find('div > ul ul')->[1], undef, 'no result';

# Form values
$dom = Mojo::DOM->new(<<EOF);
<form action="/foo">
<p>Test</p>
<input type="text" name="a" value="A" />
<input type="checkbox" checked name="b" value="B">
<input type="radio" checked name="c" value="C">
<select name="f">
<option value="F">G</option>
<optgroup>
<option>H</option>
<option selected>I</option>
</optgroup>
<option value="J" selected>K</option>
</select>
<select name="n"><option>N</option></select>
<select name="d"><option selected>D</option></select>
<textarea name="m">M</textarea>
<button name="o" value="O">No!</button>
<input type="submit" name="p" value="P" />
</form>
EOF
is_deeply [$dom->at('p')->val->each], [], 'no values';
is $dom->at('input')->val->size, 1, 'one value';
is $dom->at('input')->val, 'A', 'right value';
is $dom->at('input:checked')->val, 'B', 'right value';
is $dom->at('input:checked[type=radio]')->val, 'C', 'right value';
is $dom->find('select')->first->val->join(':'), 'I:J', 'right value';
is_deeply [$dom->find('select')->first->val->each], ['I', 'J'], 'right values';
is $dom->at('select option')->val->size, 1, 'one value';
is $dom->at('select option')->val, 'F', 'right value';
is $dom->at('select optgroup option:not([selected])')->val, 'H', 'right value';
is $dom->find('select')->[1]->val->size, 0, 'no values';
is $dom->find('select')->[1]->at('option')->val, 'N', 'right value';
is $dom->find('select')->last->val, 'D', 'right value';
is $dom->at('textarea')->val->size, 1, 'one value';
is $dom->at('textarea')->val, 'M', 'right value';
is $dom->at('button')->val, 'O', 'right value';
is $dom->find('form input')->last->val, 'P', 'right value';

# Slash between attributes
$dom = Mojo::DOM->new('<input /type=checkbox / value="/a/" checked/><br/>');
is_deeply $dom->at('input')->attr,
Expand Down
2 changes: 1 addition & 1 deletion t/pod_coverage.t
Expand Up @@ -9,7 +9,7 @@ plan skip_all => 'Test::Pod::Coverage 1.04 required for this test!'

# DEPRECATED in Tiger Face!
my @tiger
= (qw(decode emit_safe encode error has_conditions new render_static));
= (qw(decode emit_safe encode error has_conditions new render_static val));

# False positive constants
all_pod_coverage_ok({also_private => [qw(IPV6 TLS), @tiger]});

0 comments on commit 3a29749

Please sign in to comment.