Skip to content

Commit

Permalink
add val method to Mojo::DOM
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Sep 21, 2015
1 parent 3d6fc30 commit 75d1990
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 1 deletion.
1 change: 1 addition & 0 deletions Changes
@@ -1,5 +1,6 @@

6.21 2015-09-21
- Added val method to Mojo::DOM.

6.20 2015-09-15
- Deprecated Mojo::UserAgent::CookieJar::collecting in favor of
Expand Down
33 changes: 33 additions & 0 deletions lib/Mojo/DOM.pm
Expand Up @@ -159,6 +159,21 @@ sub tree { shift->_delegate(tree => @_) }

sub type { shift->tree->[0] }

sub val {
my $self = shift;

# "option"
my $tag = $self->tag;
return $self->{value} // $self->text if $tag eq 'option';

# "select"
return $self->find('option[selected]')->map('val')->to_array
if $tag eq 'select';

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

sub wrap { shift->_wrap(0, @_) }
sub wrap_content { shift->_wrap(1, @_) }

Expand Down Expand Up @@ -927,6 +942,24 @@ C<root>, C<tag> or C<text>.
# "text"
$dom->parse('<p>Test</p>')->at('p')->child_nodes->first->type;
=head2 val
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.
# "b"
$dom->parse('<input name="a" value="b">')->at('input')->val;
# "c"
$dom->parse('<option value="c">Test</option>')->at('option')->val;
# "d"
$dom->parse('<option>d</option>')->at('option')->val;
=head2 wrap
$dom = $dom->wrap('<div></div>');
Expand Down
2 changes: 1 addition & 1 deletion lib/Test/Mojo.pm
Expand Up @@ -701,7 +701,7 @@ arguments as L<Mojo::UserAgent/"get">, except for the callback.
# Run additional tests on the transaction
$t->get_ok('/foo')->status_is(200);
is $t->tx->res->dom->at('input')->{value}, 'whatever', 'right value';
is $t->tx->res->dom->at('input')->val, 'whatever', 'right value';
=head2 head_ok
Expand Down
36 changes: 36 additions & 0 deletions t/mojo/dom.t
Expand Up @@ -2173,6 +2173,42 @@ is $dom->at('div pre code')->all_text, "like\n it\n really", 'right text';
is $dom->at('div pre code')->all_text(0), "like\n it\n really",
'right text';

# Form values
$dom = Mojo::DOM->new(<<EOF);
<form action="/foo">
<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">
<select name="f">
<option value="F">G</option>
<optgroup>
<option>H</option>
<option selected>I</option>
</optgroup>
<option value="J" selected>K</option>
</select>
<select name="n"><option>N</option></select>
<select name="d"><option selected>D</option></select>
<textarea name="m">M</textarea>
<button name="o" value="O">No!</button>
<input type="submit" name="p" value="P" />
</form>
EOF
is $dom->at('p')->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('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->at('textarea')->val, 'M', 'right value';
is $dom->at('button')->val, 'O', 'right value';
is $dom->find('form input')->last->val, 'P', 'right value';

# PoCo example with whitespace sensitive text
$dom = Mojo::DOM->new(<<EOF);
<?xml version="1.0" encoding="UTF-8"?>
Expand Down

0 comments on commit 75d1990

Please sign in to comment.