Skip to content

Instantly share code, notes, and snippets.

/val.diff Secret

Created September 22, 2015 17:29
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/fed493669bc5ec64706b to your computer and use it in GitHub Desktop.
Save anonymous/fed493669bc5ec64706b to your computer and use it in GitHub Desktop.
diff --git a/lib/Mojo/DOM.pm b/lib/Mojo/DOM.pm
index 62c66b7..d5b545b 100644
--- a/lib/Mojo/DOM.pm
+++ b/lib/Mojo/DOM.pm
@@ -165,12 +165,12 @@ sub val {
# "option"
return $self->{value} // $self->text if (my $tag = $self->tag) eq 'option';
- # "select"
- return $self->find('option:checked')->map('val')->to_array
- if $tag eq 'select';
-
# "textarea", "input" or "button"
- return $tag eq 'textarea' ? $self->text : $self->{value};
+ return $tag eq 'textarea' ? $self->text : $self->{value} if $tag ne 'select';
+
+ # "select"
+ my $values = $self->find('option:checked')->map('val');
+ return exists $self->{multiple} ? $values->to_array : $values->first;
}
sub wrap { shift->_wrap(0, @_) }
@@ -947,8 +947,8 @@ C<root>, C<tag> or C<text>.
Extract values from C<button>, C<input>, C<option>, C<select> and C<textarea>
elements or return C<undef> if this element has no value. In the case of
-C<select>, find C<option> elements with C<selected> attribute and return an
-array reference with all values.
+C<select> with C<multiple> attribute, find C<option> elements with C<selected>
+attribute and return an array reference with all values.
# "a"
$dom->parse('<input name="test" value="a">')->at('input')->val;
@@ -964,6 +964,10 @@ array reference with all values.
# "e"
$dom->parse('<select><option selected>e</option></select>')
+ ->at('select')->val;
+
+ # "f"
+ $dom->parse('<select multiple><option selected>f</option></select>')
->at('select')->val->[0];
=head2 wrap
diff --git a/t/mojo/dom.t b/t/mojo/dom.t
index 391dbcc..ff705d1 100644
--- a/t/mojo/dom.t
+++ b/t/mojo/dom.t
@@ -2189,6 +2189,7 @@ $dom = Mojo::DOM->new(<<EOF);
<option value="J" selected>K</option>
</select>
<select name="n"><option>N</option></select>
+ <select multiple name="q"><option>Q</option></select>
<select name="d"><option selected>D</option></select>
<textarea name="m">M</textarea>
<button name="o" value="O">No!</button>
@@ -2203,8 +2204,10 @@ is_deeply $dom->at('select')->val, ['I', 'J'], 'right values';
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]->at('option')->val, 'N', 'right value';
-is_deeply $dom->find('select')->[1]->val, [], 'no values';
-is_deeply $dom->find('select')->last->val, ['D'], 'right values';
+is $dom->find('select')->[1]->val, undef, 'no value';
+is_deeply $dom->find('select')->[2]->val, [], 'no values';
+is $dom->find('select')->[2]->at('option')->val, 'Q', 'right value';
+is_deeply $dom->find('select')->last->val, 'D', 'right 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';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment