Skip to content

Instantly share code, notes, and snippets.

/stringify.patch Secret

Created October 17, 2014 16:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/f4adf4788d66aa04a1fa to your computer and use it in GitHub Desktop.
Save anonymous/f4adf4788d66aa04a1fa to your computer and use it in GitHub Desktop.
diff --git a/lib/Mojo/Collection.pm b/lib/Mojo/Collection.pm
index 6cacfac..dd87632 100644
--- a/lib/Mojo/Collection.pm
+++ b/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';
@@ -342,22 +341,6 @@ results, similar to L</"pluck">.
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.
-
-=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>.
diff --git a/lib/Mojo/DOM.pm b/lib/Mojo/DOM.pm
index 61256c3..b047260 100644
--- a/lib/Mojo/DOM.pm
+++ b/lib/Mojo/DOM.pm
@@ -416,8 +416,8 @@ Mojo::DOM - Minimalistic HTML/XML DOM parser with CSS select
# Find
say $dom->at('#b')->text;
- say $dom->find('p')->text;
- say $dom->find('[id]')->attr('id');
+ say $dom->find('p')->text->join("\n");
+ say $dom->find('[id]')->attr('id')->join("\n");
# Walk
say $dom->div->p->[0]->text;
@@ -561,7 +561,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('*')->attr('id')->compact->join("\n");
=head2 children
diff --git a/t/mojo/collection.t b/t/mojo/collection.t
index 2ef3823..cdc3519 100644
--- a/t/mojo/collection.t
+++ b/t/mojo/collection.t
@@ -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);
@@ -147,12 +145,14 @@ is_deeply [$collection->slice(6 .. 9)->each], [7, 10, 9, 8
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';
-$collection = c(b('one'), b('two'), b('three'));
-is $collection->camelize, "One\nTwo\nThree", 'right result';
-is $collection->url_escape('^netwhr')->reverse, "%54hree\n%54w%6F\n%4Fne",
+is $collection->pluck('reverse')->flatten->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';
+$collection = c(b('one'), b('two'), b('three'));
+is $collection->camelize->join("\n"), "One\nTwo\nThree", 'right result';
+is $collection->url_escape('^netwhr')->reverse->join("\n"),
+ "%54hree\n%54w%6F\n%4Fne", 'right result';
# uniq
$collection = c(1, 2, 3, 2, 3, 4, 5, 4);
diff --git a/t/mojo/dom.t b/t/mojo/dom.t
index 3d2017d..c470eb4 100644
--- a/t/mojo/dom.t
+++ b/t/mojo/dom.t
@@ -1425,8 +1425,8 @@ is $dom->find('table > colgroup > col')->[2]->attr->{id},
'right attribute';
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')->text, "B\nE", 'right text';
+is $dom->at('table > tbody > tr > td')->text, 'B', 'right text';
+is $dom->find('table > tbody > tr > td')->text->join, "BE", 'right text';
# Optional "colgroup", "tbody", "tr", "th" and "td" tags
$dom = Mojo::DOM->new->parse(<<EOF);
@@ -2036,10 +2036,10 @@ is $dom->a->B->c->size, 2, 'right number of elements';
@results = ();
$dom->a->B->c->each(sub { push @results, $_->text });
is_deeply \@results, [qw(bar baz)], 'right results';
-is $dom->a->B->c, qq{<c id="three">bar</c>\n<c ID="four">baz</c>},
+is $dom->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);
@@ -2072,10 +2072,10 @@ is $dom->a->b->c->size, 2, 'right number of elements';
@results = ();
$dom->a->b->c->each(sub { push @results, $_->text });
is_deeply \@results, [qw(bar baz)], 'right results';
-is $dom->a->b->c, qq{<c id="three">bar</c>\n<c id="four">baz</c>},
+is $dom->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>');
@@ -2342,22 +2342,23 @@ $dom = Mojo::DOM->new(<<EOF);
</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->at('input')->val->size, 1, 'one value';
+is $dom->at('input')->val->first, 'A', 'right value';
+is $dom->at('input:checked')->val->first, 'B', 'right value';
+is $dom->at('input:checked[type=radio]')->val->first, '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->at('select option')->val->size, 1, 'one value';
+is $dom->at('select option')->val->first, 'F', 'right value';
+is $dom->at('select optgroup option:not([selected])')->val->first, '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';
+is $dom->find('select')->[1]->at('option')->val->first, 'N', 'right value';
+is $dom->find('select')->last->val->first, 'D', 'right value';
+is $dom->at('textarea')->val->size, 1, 'one value';
+is $dom->at('textarea')->val->first, 'M', 'right value';
+is $dom->at('button')->val->first, 'O', 'right value';
+is $dom->find('form input')->last->val->first, 'P', 'right value';
# Slash between attributes
$dom = Mojo::DOM->new('<input /type=checkbox / value="/a/" checked/><br/>');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment