Skip to content

Commit

Permalink
handle "on" default values in Mojo::DOM as well
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Feb 25, 2016
1 parent fe02d51 commit 7df1f8d
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 1 deletion.
1 change: 1 addition & 0 deletions Changes
Expand Up @@ -2,6 +2,7 @@
6.49 2016-02-25
- Improved check_box and radio_button helpers with support for "on" default
values.
- Improved val method in Mojo::DOM with support for "on" default values.
- Fixed Windows bug in "util.t".

6.48 2016-02-24
Expand Down
10 changes: 9 additions & 1 deletion lib/Mojo/DOM.pm
Expand Up @@ -165,6 +165,11 @@ sub val {
# "option"
return $self->{value} // $self->text if (my $tag = $self->tag) eq 'option';

# "input" ("type=checkbox" and "type=radio")
my $type = $self->{type} // '';
return $self->{value} // 'on'
if $tag eq 'input' && ($type eq 'checkbox' || $type eq 'radio');

# "textarea", "input" or "button"
return $tag eq 'textarea' ? $self->text : $self->{value} if $tag ne 'select';

Expand Down Expand Up @@ -960,7 +965,7 @@ C<selected> attribute and return an array reference with all values, or C<undef>
if none could be found.
# "a"
$dom->parse('<input name="test" value="a">')->at('input')->val;
$dom->parse('<input name=test value=a>')->at('input')->val;
# "b"
$dom->parse('<textarea>b</textarea>')->at('textarea')->val;
Expand All @@ -976,6 +981,9 @@ if none could be found.
$dom->parse('<select multiple><option selected>e</option></select>')
->at('select')->val->[0];
# "on"
$dom->parse('<input name=test type=checkbox>')->at('input')->val;
=head2 wrap
$dom = $dom->wrap('<div></div>');
Expand Down
2 changes: 2 additions & 0 deletions lib/Mojolicious.pm
Expand Up @@ -857,6 +857,8 @@ Dominique Dumont
Douglas Christopher Wilson
Eugen Konkov
Eugene Toropov
Gisle Aas
Expand Down
8 changes: 8 additions & 0 deletions t/mojo/dom.t
Expand Up @@ -2212,8 +2212,12 @@ $dom = Mojo::DOM->new(<<EOF);
<form action="/foo">
<p>Test</p>
<input type="text" name="a" value="A" />
<input type="checkbox" name="q">
<input type="checkbox" checked name="b" value="B">
<input type="radio" name="r">
<input type="radio" checked name="c" value="C">
<input name="s">
<input type="checkbox" name="t" value="">
<select multiple name="f">
<option value="F">G</option>
<optgroup>
Expand Down Expand Up @@ -2249,6 +2253,10 @@ 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';
is $dom->at('input[name=q]')->val, 'on', 'right value';
is $dom->at('input[name=r]')->val, 'on', 'right value';
is $dom->at('input[name=s]')->val, undef, 'no value';
is $dom->at('input[name=t]')->val, '', 'right value';

# PoCo example with whitespace-sensitive text
$dom = Mojo::DOM->new(<<EOF);
Expand Down

0 comments on commit 7df1f8d

Please sign in to comment.