Skip to content

Commit

Permalink
handle select elements a little different
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Sep 22, 2015
1 parent 7370e5a commit 1b35296
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 18 deletions.
22 changes: 13 additions & 9 deletions lib/Mojo/DOM.pm
Expand Up @@ -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 $v = $self->find('option:checked')->map('val');
return exists $self->{multiple} ? $v->size ? $v->to_array : undef : $v->last;
}

sub wrap { shift->_wrap(0, @_) }
Expand Down Expand Up @@ -945,10 +945,10 @@ C<root>, C<tag> or C<text>.
my $value = $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 C<option> elements with C<selected> attribute and return an
array reference with all values.
Extract value from C<button>, C<input>, C<option>, C<select> and C<textarea>
element or return C<undef> if this element has no value. In the case of
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;
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/Parameters.pm
Expand Up @@ -21,7 +21,7 @@ sub append {
if (ref $value eq 'ARRAY') { push @$old, $name => $_ // '' for @$value }

# Single value
else { push @$old, $name => $value }
elsif (defined $value) { push @$old, $name => $value }
}

return $self;
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/UserAgent/Transactor.pm
Expand Up @@ -192,7 +192,7 @@ sub _multipart {

my @parts;
for my $name (sort keys %$form) {
my $values = $form->{$name};
next unless defined(my $values = $form->{$name});
for my $value (ref $values eq 'ARRAY' ? @$values : ($values)) {
push @parts, my $part = Mojo::Content::Single->new;

Expand Down
13 changes: 10 additions & 3 deletions t/mojo/dom.t
Expand Up @@ -2189,7 +2189,11 @@ $dom = Mojo::DOM->new(<<EOF);
<option value="J" selected>K</option>
</select>
<select name="n"><option>N</option></select>
<select name="d"><option selected>D</option></select>
<select multiple name="q"><option>Q</option></select>
<select name="d">
<option selected>R</option>
<option selected>D</option>
</select>
<textarea name="m">M</textarea>
<button name="o" value="O">No!</button>
<input type="submit" name="p" value="P" />
Expand All @@ -2203,8 +2207,11 @@ 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, undef, 'no value';
is $dom->find('select')->[2]->at('option')->val, 'Q', 'right value';
is_deeply $dom->find('select')->last->val, 'D', 'right value';
is_deeply $dom->find('select')->last->at('option')->val, 'R', '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';
Expand Down
2 changes: 1 addition & 1 deletion t/mojo/parameters.t
Expand Up @@ -51,7 +51,7 @@ is_deeply $params->names, [qw(a b c foo x y z)], 'right structure';

# Append
$params = Mojo::Parameters->new('q=1');
$params->append(a => 4, a => 5, b => 6, b => 7);
$params->append(a => 4, a => 5, b => 6, b => 7, c => undef, c => []);
is_deeply $params->to_hash, {a => [4, 5], b => [6, 7], q => 1},
'right structure';
is_deeply $params->names, [qw(a b q)], 'right structure';
Expand Down
8 changes: 5 additions & 3 deletions t/mojo/transactor.t
Expand Up @@ -150,7 +150,7 @@ is $tx->req->body, 'test=12345678912', 'right content';
$tx
= $t->tx(POST => 'http://example.com/foo' =>
{Accept => '*/*', 'Content-Type' => 'application/mojo-form'} => form =>
{test => 123} => charset => 'UTF-8');
{test => 123, nothing => undef} => charset => 'UTF-8');
is $tx->req->url->to_abs, 'http://example.com/foo', 'right URL';
is $tx->req->method, 'POST', 'right method';
is $tx->req->headers->content_type, 'application/mojo-form',
Expand All @@ -159,8 +159,10 @@ is $tx->req->headers->accept, '*/*', 'right "Accept" value';
is $tx->req->body, 'test=123', 'right content';

# Multipart form
$tx = $t->tx(POST => 'http://example.com/foo' =>
{'Content-Type' => 'multipart/form-data'} => form => {test => 123});
$tx
= $t->tx(POST => 'http://example.com/foo' =>
{'Content-Type' => 'multipart/form-data'} => form =>
{test => 123, nothing => undef});
is $tx->req->url->to_abs, 'http://example.com/foo', 'right URL';
is $tx->req->method, 'POST', 'right method';
is $tx->req->headers->content_type, 'multipart/form-data',
Expand Down

0 comments on commit 1b35296

Please sign in to comment.