Skip to content

Commit 5b2cdbf

Browse files
simbabqueoalders
authored andcommitted
allow action attribute for all_forms_with and form_with
This fixes libwww-perl/HTML-Form#6 without making changes to HTML::Form at very little extra cost for us.
1 parent 692c053 commit 5b2cdbf

File tree

4 files changed

+52
-8
lines changed

4 files changed

+52
-8
lines changed

Changes

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
Revision history for WWW::Mechanize
22

33
{{$NEXT}}
4+
[ENHANCEMENTS]
5+
- form_with and all_forms_with() now support the "action" attribute to find
6+
forms (GH#349) (Julien Fiegehenn)
47

58
2.14 2022-08-15 19:19:24Z
69
[FIXED]

lib/WWW/Mechanize.pm

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1784,8 +1784,6 @@ sub form_with_fields {
17841784
17851785
Searches for forms with arbitrary attribute/value pairs within the E<lt>formE<gt>
17861786
tag.
1787-
(Currently does not work for attribute C<action> due to implementation details
1788-
of L<HTML::Form>.)
17891787
When given more than one pair, all criteria must match.
17901788
Using C<undef> as value means that the attribute in question must not be present.
17911789
@@ -1796,7 +1794,8 @@ All matching forms (perhaps none) are returned as a list of L<HTML::Form> object
17961794
sub all_forms_with {
17971795
my ( $self, %spec ) = @_;
17981796

1799-
my @forms = $self->forms;
1797+
my $action = delete $spec{action};
1798+
my @forms = grep { !$action || $_->action eq $action } $self->forms;
18001799
foreach my $attr ( keys %spec ) {
18011800
@forms = grep _equal( $spec{$attr}, $_->attr($attr) ), @forms or return;
18021801
}
@@ -1807,9 +1806,6 @@ sub all_forms_with {
18071806
18081807
Searches for forms with arbitrary attribute/value pairs within the E<lt>formE<gt>
18091808
tag.
1810-
(Currently does not work for attribute C<action> due to implementation details
1811-
of L<HTML::Form>. Use C<L<< form_action()|/"$mech->form_action( $action )" >>>
1812-
instead.)
18131809
When given more than one pair, all criteria must match.
18141810
Using C<undef> as value means that the attribute in question must not be present.
18151811

t/form_with_fields.t

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use strict;
55
use Test::More 'no_plan';
66
use Test::Fatal qw( exception );
77
use Test::Warnings ':all';
8-
use Test::Deep qw( cmp_deeply re );
8+
use Test::Deep qw( cmp_deeply re array_each code );
99
use URI::file ();
1010

1111
BEGIN {
@@ -81,13 +81,31 @@ ok( $mech->success, "Fetched $uri" ) or die q{Can't get test page};
8181

8282
{
8383
my @forms = $mech->all_forms_with( name => '3rd_form_ambiguous' );
84-
is( scalar @forms, 2 );
84+
is( scalar @forms, 2 , 'all_forms_with finds similar forms');
8585
isa_ok( $forms[0], 'HTML::Form' );
8686
isa_ok( $forms[1], 'HTML::Form' );
8787
is($forms[0]->attr('name'), '3rd_form_ambiguous', 'first result of 3rd_form_ambiguous');
8888
is($forms[1]->attr('name'), '3rd_form_ambiguous', 'second result of 3rd_form_ambiguous');
8989
}
9090

91+
{
92+
my @forms = $mech->all_forms_with( action => 'http://localhost/' );
93+
is( scalar @forms, 7, 'all_forms_with action finds all 7 forms' );
94+
cmp_deeply(
95+
\@forms,
96+
array_each(
97+
code(
98+
# one of the forms is missing the trailing slash, we
99+
# should not have it
100+
sub {
101+
$_[0]->action eq 'http://localhost/';
102+
}
103+
)
104+
),
105+
'... and all of them have the correct action'
106+
);
107+
}
108+
91109
{
92110
$mech->get($uri);
93111
like(

t/local/form.t

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,33 @@ is(
106106
);
107107
$mech->quiet(1);
108108

109+
# the server's URL may be in a different form than what the Form actually contains
110+
my $cli_form_action
111+
= ( grep { $_->action =~ m{/google-cli$} } $mech->forms )[0]->action;
112+
my $form_with_action = $mech->form_with( action => $cli_form_action );
113+
is(
114+
$form_with_action->attr('id'), 'searchbox',
115+
'form_with - with with action'
116+
);
117+
118+
my $formsubmit_form_action
119+
= ( grep { $_->action =~ m{/formsubmit$} } $mech->forms )[-1]->action;
120+
$form_with_action = $mech->form_with(
121+
action => $formsubmit_form_action,
122+
class => 'test mf2'
123+
);
124+
is(
125+
$form_with_action->attr('class'), 'test mf2',
126+
'form_with - with action and class'
127+
);
128+
129+
$form_with_action
130+
= $mech->form_with( action => '/does_not_exist', class => 'test' );
131+
ok(
132+
!$form_with_action,
133+
'form_with - filters forms when action does not exist'
134+
);
135+
109136
my $form_id_searchbox = $mech->form_action('google-cli');
110137
isa_ok( $form_id_searchbox, 'HTML::Form', 'form_action - can select the form' );
111138
ok( !$mech->form_action('bargle-snark'),

0 commit comments

Comments
 (0)