-
-
Save anonymous/8b6d33f31a01a5b6aaf5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/Changes b/Changes | |
index 9609327..660a5b2 100644 | |
--- a/Changes | |
+++ b/Changes | |
@@ -1,5 +1,6 @@ | |
5.58 2014-11-06 | |
+ - Undeprecated Mojo::DOM::val. | |
- Improved error handling in Mojo::IOLoop::Client. | |
5.57 2014-11-02 | |
diff --git a/lib/Mojo/DOM.pm b/lib/Mojo/DOM.pm | |
index ac3008c..4618a9f 100644 | |
--- a/lib/Mojo/DOM.pm | |
+++ b/lib/Mojo/DOM.pm | |
@@ -180,9 +180,7 @@ sub type { | |
return $self; | |
} | |
-# DEPRECATED in Tiger Face! | |
sub val { | |
- deprecated 'Mojo::DOM::val is DEPRECATED'; | |
my $self = shift; | |
# "option" | |
@@ -858,6 +856,24 @@ This element's type. | |
# List types of child elements | |
say $dom->children->map('type')->join("\n"); | |
+=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->first; | |
+ | |
+ # "c" | |
+ $dom->parse('<option value="c">Test</option>')->at('option')->val->first; | |
+ | |
+ # "d" | |
+ $dom->parse('<option>d</option>')->at('option')->val->first; | |
+ | |
=head2 wrap | |
$dom = $dom->wrap('<div></div>'); | |
diff --git a/lib/Test/Mojo.pm b/lib/Test/Mojo.pm | |
index 79c966a..3f7704a 100644 | |
--- a/lib/Test/Mojo.pm | |
+++ b/lib/Test/Mojo.pm | |
@@ -649,7 +649,7 @@ arguments as L<Mojo::UserAgent/"get">, except for the callba | |
# Run additional tests on the transaction | |
$t->get_ok('/foo')->status_is(200); | |
- is $t->tx->res->dom->at('input')->{value}, 'whatever', 'right value'; | |
+ is $t->tx->res->dom->at('input')->val->first, 'whatever', 'right value'; | |
=head2 head_ok | |
diff --git a/t/mojo/dom.t b/t/mojo/dom.t | |
index b1e8ac7..95deda7 100644 | |
--- a/t/mojo/dom.t | |
+++ b/t/mojo/dom.t | |
@@ -2278,6 +2278,47 @@ 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->join, '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->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->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/>'); | |
is_deeply $dom->at('input')->attr, |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment