Skip to content

Commit

Permalink
remove support for <form> elements from Mojo::DOM::val again
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Jun 5, 2017
1 parent c45e783 commit 861193d
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 94 deletions.
2 changes: 1 addition & 1 deletion Changes
Expand Up @@ -2,7 +2,7 @@
7.33 2017-06-05
- Added EXPERIMENTAL support for :matches pseudo-class and :not pseudo-class
with compount selectors to Mojo::DOM::CSS.
- Improved val method in Mojo::DOM with support for <form> elements.
- Fixed a few form element value extraction bugs in Mojo::DOM.
- Fixed version command to use the new MetaCPAN API, since the old one got
shut down.

Expand Down
55 changes: 5 additions & 50 deletions lib/Mojo/DOM.pm
Expand Up @@ -161,11 +161,8 @@ sub type { shift->tree->[0] }
sub val {
my $self = shift;

# "form"
return $self->_form(@_) if (my $tag = $self->tag) eq 'form';

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

# "input" ("type=checkbox" and "type=radio")
my $type = $self->{type} // '';
Expand Down Expand Up @@ -245,40 +242,6 @@ sub _delegate {
return $self;
}

sub _form {
my ($self, $selector) = @_;
$selector //= 'button:not([disabled]), input:matches([type=button],'
. ' [type=submit], [type=image]):not([disabled])';

# The submit button
my $form = {};
if (my $e = $self->at($selector)) { _input($form, $e->{name}, $e->val) }

# "select" and "textarea"
_input($form, $_->{name}, $_->val)
for $self->find('select:not([disabled]), textarea:not([disabled])')->each;

# "input"
my $input = $self->find(
'input:not([type=button], [type=image], [type=submit], [disabled])');
for my $e ($input->each) {
my $type = $e->{type} // '';
_input($form, $e->{name}, $e->val)
if ($type ne 'radio' && $type ne 'checkbox') || exists $e->{checked};
}

return $form;
}

sub _input {
my ($form, $name, $value) = @_;
$form->{$name}
= exists $form->{$name}
? [ref $form->{$name} ? @{$form->{$name}} : $form->{$name}, $value]
: $value
if defined $name && defined $value;
}

sub _link {
my ($parent, @children) = @_;

Expand Down Expand Up @@ -957,16 +920,12 @@ C<root>, C<tag> or C<text>.
=head2 val
my $value = $dom->val;
my $value = $dom->val('input[type=submit]');
Extract values from C<button>, C<form>, 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> with C<multiple> attribute, find C<option> elements with
Extract value from form element (such as C<button>, C<input>, C<option>,
C<select> and C<textarea>), 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, or C<undef>
if none could be found. In the case of C<form>, find all elements mentioned
before that would be submitted by pressing the first button or the button
matching the CSS selector, and return a hash reference with their names and
values. All selectors from L<Mojo::DOM::CSS/"SELECTORS"> are supported.
if none could be found.
# "a"
$dom->parse('<input name=test value=a>')->at('input')->val;
Expand All @@ -985,10 +944,6 @@ values. All selectors from L<Mojo::DOM::CSS/"SELECTORS"> are supported.
$dom->parse('<select multiple><option selected>e</option></select>')
->at('select')->val->[0];
# "f"
$dom->parse('<form action="/"><input name="a" value="f"></form>')
->at('form')->val->{a};
# "on"
$dom->parse('<input name=test type=checkbox>')->at('input')->val;
Expand Down
44 changes: 1 addition & 43 deletions t/mojo/dom.t
Expand Up @@ -2217,10 +2217,8 @@ $dom = Mojo::DOM->new(<<EOF);
<form action="/foo">
<p>Test</p>
<input type="text" name="a" value="A" />
<input type=text name="a" value="A2">
<input type="checkbox" name="q">
<input type="checkbox" checked name="b" value="B">
<input type="checkbox" checked name="b">
<input type="radio" name="r">
<input type="radio" checked name="c" value="C">
<input name="s">
Expand Down Expand Up @@ -2248,23 +2246,9 @@ $dom = Mojo::DOM->new(<<EOF);
<option selected>D</option>
</select>
<textarea name="m">M</textarea>
<textarea name="m">M3</textarea>
<textarea name="m2" disabled>M2</textarea>
<button name="o" value="O">No!</button>
<input type=text name="x" value="X" disabled>
<input type="submit" name="p" value="P" />
</form>
<form><input type="submit" name="a" value="A"></form>
<form>
<input type="button" name="b" value="B" disabled>
<input type="button" name="c" value="C">
</form>
<form><input type="image" name="c" value="C"></form>
<form>
<button name="e" value="E" disabled>
<button name="d" value="D">
</form>
<form></form>
EOF
is $dom->at('p')->val, undef, 'no value';
is $dom->at('input')->val, 'A', 'right value';
Expand All @@ -2281,39 +2265,13 @@ is $dom->at('select[disabled]')->val, 'Y', 'right value';
is $dom->find('select')->last->val, 'D', 'right value';
is $dom->find('select')->last->at('option')->val, 'R', 'right value';
is $dom->at('textarea')->val, 'M', 'right value';
is $dom->find('textarea')->last->val, 'M2', 'right value';
is $dom->at('button')->val, 'O', 'right value';
is $dom->at('button')->val, 'O', 'right value';
is $dom->at('form')->find('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';
is $dom->at('input[name=u]')->val, undef, 'no value';
my $form = {
a => ['A', 'A2'],
b => ['B', 'on'],
c => 'C',
d => 'D',
f => ['I', 'J'],
m => ['M', 'M3'],
o => 'O'
};
is_deeply $dom->at('form')->val, $form, 'right structure';
$form = {
a => ['A', 'A2'],
b => ['B', 'on'],
c => 'C',
d => 'D',
f => ['I', 'J'],
m => ['M', 'M3'],
p => 'P'
};
is_deeply $dom->at('form')->val('input[type=submit]'), $form, 'right structure';
is_deeply $dom->find('form')->[1]->val, {a => 'A'}, 'right structure';
is_deeply $dom->find('form')->[2]->val, {c => 'C'}, 'right structure';
is_deeply $dom->find('form')->[3]->val, {c => 'C'}, 'right structure';
is_deeply $dom->find('form')->[4]->val, {d => 'D'}, 'right structure';
is_deeply $dom->find('form')->[5]->val, {}, 'right structure';

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

0 comments on commit 861193d

Please sign in to comment.