Skip to content

Instantly share code, notes, and snippets.

/val.diff Secret

Created September 22, 2015 17:54
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/9bc635ccd44c56d2817c to your computer and use it in GitHub Desktop.
Save anonymous/9bc635ccd44c56d2817c to your computer and use it in GitHub Desktop.
diff --git a/lib/Mojo/DOM.pm b/lib/Mojo/DOM.pm
index 62c66b7..f03fe9f 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 $v = $self->find('option:checked')->map('val');
+ return exists $self->{multiple} ? $v->size ? $v->to_array : undef : $v->first;
}
sub wrap { shift->_wrap(0, @_) }
@@ -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;
@@ -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/lib/Mojo/Parameters.pm b/lib/Mojo/Parameters.pm
index 1ead7aa..bbfabaa 100644
--- a/lib/Mojo/Parameters.pm
+++ b/lib/Mojo/Parameters.pm
@@ -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;
diff --git a/lib/Mojo/UserAgent/Transactor.pm b/lib/Mojo/UserAgent/Transactor.pm
index 028b96d..9920424 100644
--- a/lib/Mojo/UserAgent/Transactor.pm
+++ b/lib/Mojo/UserAgent/Transactor.pm
@@ -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;
diff --git a/t/mojo/dom.t b/t/mojo/dom.t
index 391dbcc..4cd713a 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, 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 $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