Skip to content

Commit

Permalink
return collection for select values
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Jul 23, 2014
1 parent 47e8e5b commit b54e754
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 7 deletions.
9 changes: 3 additions & 6 deletions lib/Mojo/DOM.pm
Expand Up @@ -184,10 +184,7 @@ sub val {
return $self->{value} // $self->text if $type eq 'option';

# "select"
if ($type eq 'select') {
my @values = $self->find('option[selected]')->val->each;
return @values ? @values > 1 ? \@values : $values[0] : undef;
}
return $self->find('option[selected]')->val || undef if $type eq 'select';

# "textarea"
return $self->text if $type eq 'textarea';
Expand Down Expand Up @@ -850,8 +847,8 @@ This element's type.
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 an C<option> element with C<selected> attribute and return its
value or an array reference with all values if there are more than one.
C<select>, find all C<option> elements with C<selected> attribute and return a
L<Mojo::Collection> object containing their values.
# "b"
$dom->parse('<form><input name="a" value="b"></form>')->at('input')->val;
Expand Down
3 changes: 2 additions & 1 deletion t/mojo/dom.t
Expand Up @@ -2329,7 +2329,8 @@ is $dom->at('progress')->val, undef, 'no 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_deeply $dom->find('select')->first->val, ['I', 'J'], 'right values';
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, 'F', 'right value';
is $dom->at('select optgroup option:not([selected])')->val, 'H', 'right value';
is $dom->find('select')->[1]->val, undef, 'no value';
Expand Down

0 comments on commit b54e754

Please sign in to comment.