Skip to content

Instantly share code, notes, and snippets.

/val.diff Secret

Created December 7, 2014 04:46
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/d2b2bf98f2b9e1b56b2f to your computer and use it in GitHub Desktop.
Save anonymous/d2b2bf98f2b9e1b56b2f to your computer and use it in GitHub Desktop.
diff --git a/Changes b/Changes
index eb8df42..597a5f2 100644
--- a/Changes
+++ b/Changes
@@ -1,7 +1,8 @@
-5.69 2014-12-06
+5.69 2014-12-07
- Removed deprecated emit_safe method from Mojo::EventEmitter.
- Removed deprecated render_static method from Mojolicious::Controller.
+ - Reverted deprecation of Mojo::DOM::val.
- Improved Mojo::DOM::HTML performance slightly.
- Fixed parent combinator bug in Mojo::DOM::CSS.
- Fixed whitespace bug in Mojo::DOM::HTML.
diff --git a/lib/Mojo/DOM.pm b/lib/Mojo/DOM.pm
index c7a6f05..89306f1 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"
@@ -866,6 +864,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 7588ffd..bc846c3 100644
--- a/lib/Test/Mojo.pm
+++ b/lib/Test/Mojo.pm
@@ -662,7 +662,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 12e1a30..e63117c 100644
--- a/t/mojo/dom.t
+++ b/t/mojo/dom.t
@@ -2289,6 +2289,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->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->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