HTML::Form -- override select options? am 12.06.2005 23:19:17 von jik
Greetings,
I'm working on using WWW::Mechanize to step through an application's
pages to do some testing. The application utilizes JavaScript to
dynamically set some field values, including sometimes setting the
fields to values that aren't actually listed as valid
s for them. The browser doesn't seem to have any problem with
this. I need to duplicate this behavior within my script.
The problem is that when my script attempts to set the value of a
field to a value that isn't listed as one of its s, I
get this:
Illegal value 'newvalue for field 'fieldname' at /usr/lib/perl5/site_perl/5.8/WWW/Mechanize.pm line 1030
It should be easy enough to add functionality to HTML/Form.pm to allow
the user to override these value checks, i.e., to allow the user to
tell the module, "Set the field to this value even if the HTML says
it's not a valid value."
I'm happy to add this functionality and submit a patch, but before
doing so, I thought I should check in with the list (to which I just
subscribed) to find out whether anyone thinks this is a really bad,
no-way-we'll-accept-that-patch sort of idea, and if so why, or whether
anyone has thoughts on what form the interface to the override
functionality should take.
Thanks,
Jonathan Kamens
Re: HTML::Form -- override select options? am 02.08.2005 15:28:06 von jik
--7t+atCfJI2
Content-Type: text/plain; charset=us-ascii
Content-Description: message body text
Content-Transfer-Encoding: 7bit
As promised (see attached message), here's a patch to HTML::Form to
allow the user to add a valid option to a element so that the
element can be set to that value even if it wasn't originally listed
on the form. This is necessary because form pages can have JavaScript
which sets such elements to valid that aren't explicitly listed, and
Perl programmers thus need to be able to do the same thing.
--- lib/HTML/Form.pm.orig 2005-08-02 08:58:29.000000000 -0400
+++ lib/HTML/Form.pm 2005-08-02 09:15:36.000000000 -0400
@@ -174,19 +174,8 @@
next if $tag =~ m,/?optgroup,;
next if $tag eq "/option";
if ($tag eq "option") {
- my %a = %{$t->[0]};
- # rename keys so they don't clash with %attr
- for (keys %a) {
- next if $_ eq "value";
- $a{"option_$_"} = delete $a{$_};
- }
- while (my($k,$v) = each %$attr) {
- $a{$k} = $v;
- }
- $a{value_name} = $p->get_trimmed_text;
- $a{value} = delete $a{value_name}
- unless defined $a{value};
- $f->push_input("option", \%a);
+ $f->add_option($attr, shift @$t,
+ $p->get_trimmed_text);
}
else {
Carp::carp("Bad tag '$tag'") if $^W;
@@ -399,6 +388,35 @@
}
+=item $form->add_option($select_attr, $option_attr, $text)
+
+This method adds an to a element. Use this to allow
+you to subsequently set the value of the element to one that was not
+listed as valid in the original form.
+
+Specify a reference to a hash of the attributes of the select element
+to modify (most importantly, "name"), a reference to a hash of the
+attributes of the option element (most importantly, "value"), and the
+text of the option element.
+
+=cut
+
+sub add_option {
+ my($f, $attr, $a, $text) = @_;
+ # rename keys so they don't clash with %attr
+ for (keys %$a) {
+ next if $_ eq "value";
+ $a->{"option_$_"} = delete $a->{$_};
+ }
+ while (my($k,$v) = each %$attr) {
+ $a->{$k} = $v;
+ }
+ $a->{value_name} = $text;
+ $a->{value} = delete $a->{value_name} unless defined $a->{value};
+ $f->push_input("option", $a);
+}
+
+
=item $value = $form->value( $name )
=item $form->value( $name, $new_value )
Jonathan Kamens
--7t+atCfJI2
Content-Type: message/rfc822
Content-Transfer-Encoding: 7bit
Received: from jik2.kamens.brookline.ma.us (localhost.localdomain [127.0.0.1])
by jik2.kamens.brookline.ma.us (8.13.4/8.13.4) with ESMTP id j5CLJHbR023999;
Sun, 12 Jun 2005 17:19:17 -0400
Received: (from jik@localhost)
by jik2.kamens.brookline.ma.us (8.13.4/8.13.4/Submit) id j5CLJHll023996;
Sun, 12 Jun 2005 17:19:17 -0400
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Message-ID: <17068.42709.581614.977145@jik2.kamens.brookline.ma.us>
X-Mailer: VM 7.18 under Emacs 21.4.1
From: Jonathan Kamens
To: libwww@perl.org
Subject: HTML::Form -- override select options?
Date: Sun, 12 Jun 2005 17:19:17 -0400
Greetings,
I'm working on using WWW::Mechanize to step through an application's
pages to do some testing. The application utilizes JavaScript to
dynamically set some field values, including sometimes setting the
fields to values that aren't actually listed as valid
s for them. The browser doesn't seem to have any problem with
this. I need to duplicate this behavior within my script.
The problem is that when my script attempts to set the value of a
field to a value that isn't listed as one of its s, I
get this:
Illegal value 'newvalue for field 'fieldname' at /usr/lib/perl5/site_perl/5.8/WWW/Mechanize.pm line 1030
It should be easy enough to add functionality to HTML/Form.pm to allow
the user to override these value checks, i.e., to allow the user to
tell the module, "Set the field to this value even if the HTML says
it's not a valid value."
I'm happy to add this functionality and submit a patch, but before
doing so, I thought I should check in with the list (to which I just
subscribed) to find out whether anyone thinks this is a really bad,
no-way-we'll-accept-that-patch sort of idea, and if so why, or whether
anyone has thoughts on what form the interface to the override
functionality should take.
Thanks,
Jonathan Kamens
--7t+atCfJI2--