Skip to content

Commit

Permalink
always return a collection
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Jul 23, 2014
1 parent b54e754 commit cdee141
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 14 deletions.
17 changes: 9 additions & 8 deletions lib/Mojo/DOM.pm
Expand Up @@ -181,16 +181,17 @@ sub val {

# "option"
my $type = $self->type;
return $self->{value} // $self->text if $type eq 'option';
return Mojo::Collection->new($self->{value} // $self->text)
if $type eq 'option';

# "select"
return $self->find('option[selected]')->val || undef if $type eq 'select';
return $self->find('option[selected]')->val->flatten if $type eq 'select';

# "textarea"
return $self->text if $type eq 'textarea';
return Mojo::Collection->new($self->text) if $type eq 'textarea';

# "input" or "button"
return $type eq 'input' || $type eq 'button' ? $self->{value} : undef;
return Mojo::Collection->new($self->{value} // ());
}

sub wrap { shift->_wrap(0, @_) }
Expand Down Expand Up @@ -843,12 +844,12 @@ This element's type.
=head2 val
my $value = $dom->val;
my $collection = $dom->val;
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 all C<option> elements with C<selected> attribute and return a
L<Mojo::Collection> object containing their values.
elements 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('<form><input name="a" value="b"></form>')->at('input')->val;
Expand Down
15 changes: 9 additions & 6 deletions t/mojo/dom.t
Expand Up @@ -2306,7 +2306,7 @@ is $dom->find('div > ul ul')->[1], undef, 'no result';
# Form values
$dom = Mojo::DOM->new(<<EOF);
<form action="/foo">
<progress value="70" max="100">70 %</progress>
<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">
Expand All @@ -2325,17 +2325,20 @@ $dom = Mojo::DOM->new(<<EOF);
<input type="submit" name="p" value="P" />
</form>
EOF
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->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->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->find('select')->[1]->val, undef, 'no 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';
Expand Down

0 comments on commit cdee141

Please sign in to comment.