HTML-Form-6.03/000755 000765 000024 00000000000 11735312375 013364 5ustar00gislestaff000000 000000 HTML-Form-6.03/Changes000644 000765 000024 00000001552 11735311232 014651 0ustar00gislestaff000000 000000 _______________________________________________________________________________ 2012-03-30 HTML-Form 6.03 Support the new HTML5 input types without warning _______________________________________________________________________________ 2012-02-20 HTML-Form 6.02 Fix test failure when HTTP-Message 6.03 (or better) was installed [RT#75155] _______________________________________________________________________________ 2012-02-18 HTML-Form 6.01 Don't pick up label text from textarea [RT#72925] Restore perl-5.8.1 compatibility. _______________________________________________________________________________ 2011-02-27 HTML-Form 6.00 Initial release of HTML-Form as a separate distribution. There are no code changes besides incrementing the version number since libwww-perl-5.837. The HTML::Form module used to be bundled with the libwww-perl distribution. HTML-Form-6.03/lib/000755 000765 000024 00000000000 11735312375 014132 5ustar00gislestaff000000 000000 HTML-Form-6.03/Makefile.PL000644 000765 000024 00000002266 11720537227 015343 0ustar00gislestaff000000 000000 #!perl -w require 5.008001; use strict; use ExtUtils::MakeMaker; WriteMakefile( NAME => 'HTML::Form', VERSION_FROM => 'lib/HTML/Form.pm', ABSTRACT_FROM => 'lib/HTML/Form.pm', AUTHOR => 'Gisle Aas ', LICENSE => "perl", MIN_PERL_VERSION => 5.008001, PREREQ_PM => { 'URI' => "1.10", 'Encode' => "2", 'HTML::TokeParser' => 0, 'HTTP::Request' => 6, 'HTTP::Request::Common' => 6.03, }, META_MERGE => { resources => { repository => 'http://github.com/gisle/html-form', MailingList => 'mailto:libwww@perl.org', } }, ); BEGIN { # compatibility with older versions of MakeMaker my $developer = -f ".gitignore"; my %mm_req = ( LICENCE => 6.31, META_MERGE => 6.45, META_ADD => 6.45, MIN_PERL_VERSION => 6.48, ); undef(*WriteMakefile); *WriteMakefile = sub { my %arg = @_; for (keys %mm_req) { unless (eval { ExtUtils::MakeMaker->VERSION($mm_req{$_}) }) { warn "$_ $@" if $developer; delete $arg{$_}; } } ExtUtils::MakeMaker::WriteMakefile(%arg); }; } HTML-Form-6.03/MANIFEST000644 000765 000024 00000000414 11735312375 014514 0ustar00gislestaff000000 000000 Changes lib/HTML/Form.pm Makefile.PL MANIFEST This list of files README t/form-label.t t/form-maxlength.t t/form-multi-select.t t/form-param.t t/form-selector.t t/form-unicode.t t/form.t META.yml Module meta-data (added by MakeMaker) HTML-Form-6.03/META.yml000644 000765 000024 00000001473 11735312375 014642 0ustar00gislestaff000000 000000 --- #YAML:1.0 name: HTML-Form version: 6.03 abstract: Class that represents an HTML form element author: - Gisle Aas license: perl distribution_type: module configure_requires: ExtUtils::MakeMaker: 0 build_requires: ExtUtils::MakeMaker: 0 requires: Encode: 2 HTML::TokeParser: 0 HTTP::Request: 6 HTTP::Request::Common: 6.03 perl: 5.008001 URI: 1.10 resources: MailingList: mailto:libwww@perl.org repository: http://github.com/gisle/html-form no_index: directory: - t - inc generated_by: ExtUtils::MakeMaker version 6.57_05 meta-spec: url: http://module-build.sourceforge.net/META-spec-v1.4.html version: 1.4 HTML-Form-6.03/README000644 000765 000024 00000041232 11717001374 014240 0ustar00gislestaff000000 000000 NAME HTML::Form - Class that represents an HTML form element SYNOPSIS use HTML::Form; $form = HTML::Form->parse($html, $base_uri); $form->value(query => "Perl"); use LWP::UserAgent; $ua = LWP::UserAgent->new; $response = $ua->request($form->click); DESCRIPTION Objects of the `HTML::Form' class represents a single HTML `
...
' instance. A form consists of a sequence of inputs that usually have names, and which can take on various values. The state of a form can be tweaked and it can then be asked to provide `HTTP::Request' objects that can be passed to the request() method of `LWP::UserAgent'. The following methods are available: @forms = HTML::Form->parse( $html_document, $base_uri ) @forms = HTML::Form->parse( $html_document, base => $base_uri, %opt ) @forms = HTML::Form->parse( $response, %opt ) The parse() class method will parse an HTML document and build up `HTML::Form' objects for each
element found. If called in scalar context only returns the first . Returns an empty list if there are no forms to be found. The required arguments is the HTML document to parse ($html_document) and the URI used to retrieve the document ($base_uri). The base URI is needed to resolve relative action URIs. The provided HTML document should be a Unicode string (or US-ASCII). By default HTML::Form assumes that the original document was UTF-8 encoded and thus encode forms that don't specify an explict *accept-charset* as UTF-8. The charset assumed can be overridden by providing the `charset' option to parse(). It's a good idea to be explict about this parameter as well, thus the recommended simplest invocation becomes: my @forms = HTML::Form->parse( Encode::decode($encoding, $html_document_bytes), base => $base_uri, charset => $encoding, ); If the document was retrieved with LWP then the response object provide methods to obtain a proper value for `base' and `charset': my $ua = LWP::UserAgent->new; my $response = $ua->get("http://www.example.com/form.html"); my @forms = HTML::Form->parse($response->decoded_content, base => $response->base, charset => $response->content_charset, ); In fact, the parse() method can parse from an `HTTP::Response' object directly, so the example above can be more conveniently written as: my $ua = LWP::UserAgent->new; my $response = $ua->get("http://www.example.com/form.html"); my @forms = HTML::Form->parse($response); Note that any object that implements a decoded_content(), base() and content_charset() method with similar behaviour as `HTTP::Response' will do. Additional options might be passed in to control how the parse method behaves. The following are all the options currently recognized: `base => $uri' This is the URI used to retrive the original document. This option is not optional ;-) `charset => $str' Specify what charset the original document was encoded in. This is used as the default for accept_charset. If not provided this defaults to "UTF-8". `verbose => $bool' Warn (print messages to STDERR) about any bad HTML form constructs found. You can trap these with $SIG{__WARN__}. `strict => $bool' Initialize any form objects with the given strict attribute. $method = $form->method $form->method( $new_method ) This method is gets/sets the *method* name used for the `HTTP::Request' generated. It is a string like "GET" or "POST". $action = $form->action $form->action( $new_action ) This method gets/sets the URI which we want to apply the request *method* to. $enctype = $form->enctype $form->enctype( $new_enctype ) This method gets/sets the encoding type for the form data. It is a string like "application/x-www-form-urlencoded" or "multipart/form-data". $accept = $form->accept_charset $form->accept_charset( $new_accept ) This method gets/sets the list of charset encodings that the server processing the form accepts. Current implementation supports only one-element lists. Default value is "UNKNOWN" which we interpret as a request to use document charset as specified by the 'charset' parameter of the parse() method. $value = $form->attr( $name ) $form->attr( $name, $new_value ) This method give access to the original HTML attributes of the tag. The $name should always be passed in lower case. Example: @f = HTML::Form->parse( $html, $foo ); @f = grep $_->attr("id") eq "foo", @f; die "No form named 'foo' found" unless @f; $foo = shift @f; $bool = $form->strict $form->strict( $bool ) Gets/sets the strict attribute of a form. If the strict is turned on the methods that change values of the form will croak if you try to set illegal values or modify readonly fields. The default is not to be strict. @inputs = $form->inputs This method returns the list of inputs in the form. If called in scalar context it returns the number of inputs contained in the form. See INPUTS for what methods are available for the input objects returned. $input = $form->find_input( $selector ) $input = $form->find_input( $selector, $type ) $input = $form->find_input( $selector, $type, $index ) This method is used to locate specific inputs within the form. All inputs that match the arguments given are returned. In scalar context only the first is returned, or `undef' if none match. If $selector is specified, then the input's name, id, class attribute must match. A selector prefixed with '#' must match the id attribute of the input. A selector prefixed with '.' matches the class attribute. A selector prefixed with '^' or with no prefix matches the name attribute. If $type is specified, then the input must have the specified type. The following type names are used: "text", "password", "hidden", "textarea", "file", "image", "submit", "radio", "checkbox" and "option". The $index is the sequence number of the input matched where 1 is the first. If combined with $name and/or $type then it select the *n*th input with the given name and/or type. $value = $form->value( $selector ) $form->value( $selector, $new_value ) The value() method can be used to get/set the value of some input. If strict is enabled and no input has the indicated name, then this method will croak. If multiple inputs have the same name, only the first one will be affected. The call: $form->value('foo') is basically a short-hand for: $form->find_input('foo')->value; @names = $form->param @values = $form->param( $name ) $form->param( $name, $value, ... ) $form->param( $name, \@values ) Alternative interface to examining and setting the values of the form. If called without arguments then it returns the names of all the inputs in the form. The names will not repeat even if multiple inputs have the same name. In scalar context the number of different names is returned. If called with a single argument then it returns the value or values of inputs with the given name. If called in scalar context only the first value is returned. If no input exists with the given name, then `undef' is returned. If called with 2 or more arguments then it will set values of the named inputs. This form will croak if no inputs have the given name or if any of the values provided does not fit. Values can also be provided as a reference to an array. This form will allow unsetting all values with the given name as well. This interface resembles that of the param() function of the CGI module. $form->try_others( \&callback ) This method will iterate over all permutations of unvisited enumerated values ( *elements* in the HTML document. An input object basically represents a name/value pair, so when multiple HTML elements contribute to the same name/value pair in the submitted form they are combined. The input elements that are mapped one-to-one are "text", "textarea", "password", "hidden", "file", "image", "submit" and "checkbox". For the "radio" and "option" inputs the story is not as simple: All elements with the same name will contribute to the same input radio object. The number of radio input objects will be the same as the number of distinct names used for the elements. For a element there will be one input object for each contained
EOT ok($form->param('tt'), 'test content'); } { my $form = HTML::Form->parse(<<"EOT", base => "http://example.com", strict => 1);
EOT ok($form->param('tt'), 'test content'); } HTML-Form-6.03/t/form-maxlength.t000644 000765 000024 00000003365 11717001374 016745 0ustar00gislestaff000000 000000 #!/usr/bin/perl use strict; use warnings; use lib '.'; use Test::More tests => 12; use HTML::Form; my $html = do { local $/ = undef; }; my $form = HTML::Form->parse($html, 'foo.html' ); isa_ok($form, 'HTML::Form'); my $input = $form->find_input('passwd'); isa_ok($input, 'HTML::Form::TextInput'); sub set_value { my $input = shift; my $value = shift; my $len = length($value); my $old = $input->value; is( $input->value($value), $old, "set value length=$len" ); is( $input->value, $value, "got value length=$len" ); } { is( $input->{maxlength}, 8, 'got maxlength: 8' ); set_value( $input, '1234' ); set_value( $input, '1234567890' ); ok(!$input->strict, "not strict by default"); $form->strict(1); ok($input->strict, "input strict change when form strict change"); set_value( $input, '1234' ); eval { set_value( $input, '1234567890' ); }; like($@, qr/^Input 'passwd' has maxlength '8' at /, "Exception raised"); } __DATA__
Login:
Password
remember me
password reminder
Create A New User
HTML-Form-6.03/t/form-multi-select.t000644 000765 000024 00000005153 11717001374 017362 0ustar00gislestaff000000 000000 #!/usr/bin/perl # Test for case when multiple forms are on a page with same-named }; return \$html; } ##### package FakeResponse::TwoForms; sub new { bless {}, shift; } sub base { return "http://foo.com" } sub decoded_content { my $html = qq{
}; return \$html; } HTML-Form-6.03/t/form-param.t000644 000765 000024 00000003513 11717001374 016051 0ustar00gislestaff000000 000000 #!perl -w use strict; use Test qw(plan ok); plan tests => 22; use HTML::Form; my $form = HTML::Form->parse(<<"EOT", base => "http://example.com", strict => 1);
EOT # list names ok($form->param, 4); ok(j($form->param), "hidden_1:checkbox_1:checkbox_2:multi_select_field"); # get ok($form->param('hidden_1'), ''); ok($form->param('checkbox_1'), 'c1_v1'); ok(j($form->param('checkbox_1')), 'c1_v1:c1_v2'); ok($form->param('checkbox_2'), 'c2_v1'); ok(j($form->param('checkbox_2')), 'c2_v1'); ok(!defined($form->param('multi_select_field'))); ok(j($form->param('multi_select_field')), ''); ok(!defined($form->param('unknown'))); ok(j($form->param('unknown')), ''); # set eval { $form->param('hidden_1', 'x'); }; ok($@, qr/readonly/); ok(j($form->param('hidden_1')), ''); eval { $form->param('checkbox_1', 'foo'); }; ok($@, qr/Illegal value/); ok(j($form->param('checkbox_1')), 'c1_v1:c1_v2'); $form->param('checkbox_1', 'c1_v2'); ok(j($form->param('checkbox_1')), 'c1_v2'); $form->param('checkbox_1', 'c1_v2'); ok(j($form->param('checkbox_1')), 'c1_v2'); $form->param('checkbox_1', []); ok(j($form->param('checkbox_1')), ''); $form->param('checkbox_1', ['c1_v2', 'c1_v1']); ok(j($form->param('checkbox_1')), 'c1_v1:c1_v2'); $form->param('checkbox_1', []); ok(j($form->param('checkbox_1')), ''); $form->param('checkbox_1', 'c1_v2', 'c1_v1'); ok(j($form->param('checkbox_1')), 'c1_v1:c1_v2'); $form->param('multi_select_field', 3, 2); ok(j($form->param('multi_select_field')), "2:3"); sub j { join(":", @_); } HTML-Form-6.03/t/form-selector.t000644 000765 000024 00000001743 11717001374 016574 0ustar00gislestaff000000 000000 #!perl -w use strict; use Test qw(plan ok); plan tests => 12; use HTML::Form; my $form = HTML::Form->parse(<<"EOT", base => "http://example.com", strict => 1);
EOT #$form->dump; ok($form->value("n1"), 1); ok($form->value("^n1"), 1); ok($form->value("#id1"), 1); ok($form->value(".A"), 1); ok($form->value("#id2"), 2); ok($form->value(".B"), 3); ok(j(map $_->value, $form->find_input(".A")), "1:2"); $form->find_input("#id2")->name("n2"); $form->value("#id2", 22); ok($form->click->uri->query, "n1=1&n2=22"); # try some odd names ok($form->find_input("##foo")->name, "#bar"); ok($form->find_input("#bar"), undef); ok($form->find_input("^#bar")->class, ".D"); ok($form->find_input("..D")->id, "#foo"); sub j { join(":", @_); } HTML-Form-6.03/t/form-unicode.t000644 000765 000024 00000003502 11717001374 016375 0ustar00gislestaff000000 000000 #!perl -w use strict; use Test qw(plan ok); plan tests => 15; use HTML::Form; my @warn; $SIG{__WARN__} = sub { push(@warn, $_[0]) }; my $f = HTML::Form->parse(<<'EOT', "http://localhost/");
EOT ok($f->value("name"), ""); ok($f->accept_charset, "UNKNOWN"); my $req = $f->click; ok($req->uri, "http://localhost/abc?name=&latin="); $f->value(name => "\x{0424}"); # capital cyrillic ef $f->value(latin => "\xE5"); # aring $req = $f->click; ok($req->method, "GET"); ok($req->uri, "http://localhost/abc?name=%D0%A4&latin=%C3%A5"); $f->method('POST'); $f->enctype('multipart/form-data'); $req = $f->click; ok($req->uri, "http://localhost/abc"); ok($req->content, "--xYzZY\r\nContent-Disposition: form-data; name=\"name\"\r\n\r\n\xD0\xA4\r\n--xYzZY\r\nContent-Disposition: form-data; name=\"latin\"\r\n\r\n\xC3\xA5\r\n--xYzZY--\r\n"); $f->accept_charset('koi8-r'); $req = $f->click; ok($req->uri, "http://localhost/abc"); ok($req->content, "--xYzZY\r\nContent-Disposition: form-data; name=\"name\"\r\n\r\n\xE6\r\n--xYzZY\r\nContent-Disposition: form-data; name=\"latin\"\r\n\r\n?\r\n--xYzZY--\r\n"); $f->method('GET'); $req = $f->click; ok($req->uri, "http://localhost/abc?name=%E6&latin=%3F"); $f = HTML::Form->parse(<<'EOT', "http://localhost/");
EOT ok($f->accept_charset, 'koi8-r'); $f->value(name => "\x{0425}"); # capital cyrillic kha $req = $f->click; ok($req->method, "GET"); ok($req->uri, "http://localhost/abc?name=%E8"); $f->method('POST'); $f->enctype('multipart/form-data'); $req = $f->click; ok($req->uri, "http://localhost/abc"); ok($req->content, "--xYzZY\r\nContent-Disposition: form-data; name=\"name\"\r\n\r\n\xE8\r\n--xYzZY--\r\n"); HTML-Form-6.03/t/form.t000644 000765 000024 00000033400 11735305221 014747 0ustar00gislestaff000000 000000 #!perl -w use strict; use Test qw(plan ok); plan tests => 127; use HTML::Form; my @warn; $SIG{__WARN__} = sub { push(@warn, $_[0]) }; my @f = HTML::Form->parse("", "http://localhost/"); ok(@f, 0); @f = HTML::Form->parse(<<'EOT', "http://localhost/");
EOT ok(@f, 2); my $f = shift @f; ok($f->value("name"), ""); ok($f->dump, "GET http://localhost/abc [foo]\n name= (text)\n"); my $req = $f->click; ok($req->method, "GET"); ok($req->uri, "http://localhost/abc?name="); $f->value(name => "Gisle Aas"); $req = $f->click; ok($req->method, "GET"); ok($req->uri, "http://localhost/abc?name=Gisle+Aas"); ok($f->attr("name"), "foo"); ok($f->attr("method"), undef); $f = shift @f; ok($f->method, "GET"); ok($f->action, "http://localhost/"); ok($f->enctype, "application/x-www-form-urlencoded"); ok($f->dump, "GET http://localhost/\n"); # try some more advanced inputs $f = HTML::Form->parse(<<'EOT', base => "http://localhost/", verbose => 1);
EOT #print $f->dump; #print $f->click->as_string; ok($f->click->as_string, <<'EOT'); POST http://localhost/ Content-Length: 86 Content-Type: application/x-www-form-urlencoded i.x=1&i.y=1&c=on&r=b&t=&p=&tel=&date=&h=xyzzy&f=&x=&a=%0D%0Aabc%0D%0A+++&s=bar&m=a&m=b EOT ok(@warn, 1); ok($warn[0] =~ /^Unknown input type 'xyzzy'/); @warn = (); $f = HTML::Form->parse(<<'EOT', "http://localhost/");
EOT #$f->dump; ok($f->click->as_string, <<'EOT'); GET http://localhost/?x=1&y=1&t=1 EOT # test file upload $f = HTML::Form->parse(<<'EOT', "http://localhost/");
EOT #print $f->dump; #print $f->click->as_string; ok($f->click->as_string, <<'EOT'); POST http://localhost/ Content-Length: 0 Content-Type: multipart/form-data; boundary=none EOT my $filename = sprintf "foo-%08d.txt", $$; die if -e $filename; open(FILE, ">$filename") || die; binmode(FILE); print FILE "This is some text\n"; close(FILE) || die; $f->value(f => $filename); #print $f->click->as_string; ok($f->click->as_string, <<"EOT"); POST http://localhost/ Content-Length: 139 Content-Type: multipart/form-data; boundary=xYzZY --xYzZY\r Content-Disposition: form-data; name="f"; filename="$filename"\r Content-Type: text/plain\r \r This is some text \r --xYzZY--\r EOT unlink($filename) || warn "Can't unlink '$filename': $!"; ok(@warn, 0); # Try to parse form HTTP::Response directly { package MyResponse; use vars qw(@ISA); require HTTP::Response; @ISA = ('HTTP::Response'); sub base { "http://www.example.com" } } my $response = MyResponse->new(200, 'OK'); $response->content("
"); $f = HTML::Form->parse($response); ok($f->click->as_string, <<"EOT"); GET http://www.example.com?x=42 EOT $f = HTML::Form->parse(< I like it! EOT $f->find_input("x")->check; ok($f->click->as_string, <<"EOT"); GET http://www.example.com?x=on EOT $f->value("x", "off"); ok($f->click->as_string, <<"EOT"); GET http://www.example.com EOT $f->value("x", "I like it!"); ok($f->click->as_string, <<"EOT"); GET http://www.example.com?x=on EOT $f->value("x", "I LIKE IT!"); ok($f->click->as_string, <<"EOT"); GET http://www.example.com?x=on EOT $f = HTML::Form->parse(< EOT $f->value("x", "one"); ok($f->click->as_string, <<"EOT"); GET http://www.example.com?x=1 EOT $f->value("x", "TWO"); ok($f->click->as_string, <<"EOT"); GET http://www.example.com?x=2 EOT ok(join(":", $f->find_input("x")->value_names), "one:two:3"); ok(join(":", map $_->name, $f->find_input(undef, "option")), "x:y"); $f = HTML::Form->parse(< EOT ok($f->value("x"), 1); ok($f->value("y"), 2); ok($f->value("z"), 3); ok($f->click->uri->query, "y=2&z=3"); my $input = $f->find_input("x"); ok($input->type, "text"); ok(!$input->readonly); ok($input->disabled); ok($input->disabled(0)); ok(!$input->disabled); ok($f->click->uri->query, "x=1&y=2&z=3"); $input = $f->find_input("y"); ok($input->type, "text"); ok($input->readonly); ok(!$input->disabled); $input->value(22); ok($f->click->uri->query, "x=1&y=22&z=3"); $input->strict(1); eval { $input->value(23); }; ok($@ =~ /^Input 'y' is readonly/); ok($input->readonly(0)); ok(!$input->readonly); $input->value(222); ok(@warn, 0); ok($f->click->uri->query, "x=1&y=222&z=3"); $input = $f->find_input("z"); ok($input->type, "hidden"); ok($input->readonly); ok(!$input->disabled); $f = HTML::Form->parse(< one one two three one two three EOT #print $f->dump; ok($f->find_input("r0")->disabled); ok(!eval {$f->value("r0", 1);}); ok($@ && $@ =~ /^The value '1' has been disabled for field 'r0'/); ok($f->find_input("r0")->disabled(0)); ok(!$f->find_input("r0")->disabled); ok($f->value("r0", 1), undef); ok($f->value("r0"), 1); ok(!$f->find_input("r1")->disabled); ok($f->value("r1", 2), undef); ok($f->value("r1"), 2); ok(!eval {$f->value("r1", 1);}); ok($@ && $@ =~ /^The value '1' has been disabled for field 'r1'/); ok($f->value("r2", 1), undef); ok(!eval {$f->value("r2", 2);}); ok($@ && $@ =~ /^The value '2' has been disabled for field 'r2'/); ok(!eval {$f->value("r2", "two");}); ok($@ && $@ =~ /^The value 'two' has been disabled for field 'r2'/); ok(!$f->find_input("r2")->disabled(1)); ok(!eval {$f->value("r2", 1);}); ok($@ && $@ =~ /^The value '1' has been disabled for field 'r2'/); ok($f->find_input("r2")->disabled(0)); ok(!$f->find_input("r2")->disabled); ok($f->value("r2", 2), 1); ok($f->find_input("s0")->disabled); ok(!$f->find_input("s1")->disabled); ok(!$f->find_input("s2")->disabled); ok($f->find_input("s3")->disabled); ok(!eval {$f->value("s1", 1);}); ok($@ && $@ =~ /^The value '1' has been disabled for field 's1'/); ok($f->find_input("m0")->disabled); ok($f->find_input("m1", undef, 1)->disabled); ok(!$f->find_input("m1", undef, 2)->disabled); ok(!$f->find_input("m1", undef, 3)->disabled); ok(!$f->find_input("m2", undef, 1)->disabled); ok($f->find_input("m2", undef, 2)->disabled); ok(!$f->find_input("m2", undef, 3)->disabled); ok($f->find_input("m3", undef, 1)->disabled); ok($f->find_input("m3", undef, 2)->disabled); ok($f->find_input("m3", undef, 3)->disabled); $f->find_input("m3", undef, 2)->disabled(0); ok(!$f->find_input("m3", undef, 2)->disabled); ok($f->find_input("m3", undef, 2)->value(2), undef); ok($f->find_input("m3", undef, 2)->value(undef), 2); $f->find_input("m3", undef, 2)->disabled(1); ok($f->find_input("m3", undef, 2)->disabled); ok(eval{$f->find_input("m3", undef, 2)->value(2)}, undef); ok($@ && $@ =~ /^The value '2' has been disabled/); ok(eval{$f->find_input("m3", undef, 2)->value(undef)}, undef); ok($@ && $@ =~ /^The 'm3' field can't be unchecked/); # multiple select with the same name [RT#18993] $f = HTML::Form->parse(< EOT ok(join("|", $f->form), "bug|hi|bug|mom|nobug|mom"); # Try a disabled radiobutton: $f = HTML::Form->parse(< EOT ok($f->click->as_string, <<'EOT'); GET http://localhost/?f=b EOT $f = HTML::Form->parse(<
EOT ok($f->find_input("randomkey")); ok($f->find_input("randomkey")->challenge, "1234567890"); ok($f->find_input("randomkey")->keytype, "rsa"); ok($f->click->as_string, <value(randomkey => "foo"); ok($f->click->as_string, <parse(< EOT ok($f); ok($f->find_input("t")); @f = HTML::Form->parse(< EOT ok(@f, 2); ok($f[0]->find_input("s")); ok($f[1]->find_input("t")); $f = HTML::Form->parse(<
Radio Buttons with Labels
EOT ok(join(":", $f->find_input("r0")->value_names), "zero"); ok(join(":", $f->find_input("r1")->value_names), "one"); ok(join(":", $f->find_input("r2")->value_names), "two"); ok(join(":", $f->find_input("r3")->value_names), "nested"); ok(join(":", $f->find_input("r4")->value_names), "before and after"); $f = HTML::Form->parse(<
    Keep me informed on the progress of this election

The place you are registered to vote:
County or Parish Note 2
EOT ok(join(":", $f->find_input("keep_informed")->value_names), "off:"); $f = HTML::Form->parse(< EOT ok(join(":", $f->find_input("test")->possible_values), "1:2"); ok(join(":", $f->find_input("test")->other_possible_values), "2"); @warn = (); $f = HTML::Form->parse(< EOT ok(@warn, 0); HTML-Form-6.03/lib/HTML/000755 000765 000024 00000000000 11735312375 014676 5ustar00gislestaff000000 000000 HTML-Form-6.03/lib/HTML/Form.pm000644 000765 000024 00000114335 11735311513 016137 0ustar00gislestaff000000 000000 package HTML::Form; use strict; use URI; use Carp (); use Encode (); use vars qw($VERSION); $VERSION = "6.03"; my %form_tags = map {$_ => 1} qw(input textarea button select option); my %type2class = ( text => "TextInput", password => "TextInput", hidden => "TextInput", textarea => "TextInput", "reset" => "IgnoreInput", radio => "ListInput", checkbox => "ListInput", option => "ListInput", button => "SubmitInput", submit => "SubmitInput", image => "ImageInput", file => "FileInput", keygen => "KeygenInput", ); # The new HTML5 input types %type2class = (%type2class, map { $_ => 'TextInput' } qw( tel search url email datetime date month week time datetime-local number range color )); =head1 NAME HTML::Form - Class that represents an HTML form element =head1 SYNOPSIS use HTML::Form; $form = HTML::Form->parse($html, $base_uri); $form->value(query => "Perl"); use LWP::UserAgent; $ua = LWP::UserAgent->new; $response = $ua->request($form->click); =head1 DESCRIPTION Objects of the C class represents a single HTML CformE ... E/formE> instance. A form consists of a sequence of inputs that usually have names, and which can take on various values. The state of a form can be tweaked and it can then be asked to provide C objects that can be passed to the request() method of C. The following methods are available: =over 4 =item @forms = HTML::Form->parse( $html_document, $base_uri ) =item @forms = HTML::Form->parse( $html_document, base => $base_uri, %opt ) =item @forms = HTML::Form->parse( $response, %opt ) The parse() class method will parse an HTML document and build up C objects for each
element found. If called in scalar context only returns the first . Returns an empty list if there are no forms to be found. The required arguments is the HTML document to parse ($html_document) and the URI used to retrieve the document ($base_uri). The base URI is needed to resolve relative action URIs. The provided HTML document should be a Unicode string (or US-ASCII). By default HTML::Form assumes that the original document was UTF-8 encoded and thus encode forms that don't specify an explicit I as UTF-8. The charset assumed can be overridden by providing the C option to parse(). It's a good idea to be explicit about this parameter as well, thus the recommended simplest invocation becomes: my @forms = HTML::Form->parse( Encode::decode($encoding, $html_document_bytes), base => $base_uri, charset => $encoding, ); If the document was retrieved with LWP then the response object provide methods to obtain a proper value for C and C: my $ua = LWP::UserAgent->new; my $response = $ua->get("http://www.example.com/form.html"); my @forms = HTML::Form->parse($response->decoded_content, base => $response->base, charset => $response->content_charset, ); In fact, the parse() method can parse from an C object directly, so the example above can be more conveniently written as: my $ua = LWP::UserAgent->new; my $response = $ua->get("http://www.example.com/form.html"); my @forms = HTML::Form->parse($response); Note that any object that implements a decoded_content(), base() and content_charset() method with similar behaviour as C will do. Additional options might be passed in to control how the parse method behaves. The following are all the options currently recognized: =over =item C<< base => $uri >> This is the URI used to retrive the original document. This option is not optional ;-) =item C<< charset => $str >> Specify what charset the original document was encoded in. This is used as the default for accept_charset. If not provided this defaults to "UTF-8". =item C<< verbose => $bool >> Warn (print messages to STDERR) about any bad HTML form constructs found. You can trap these with $SIG{__WARN__}. The default is not to issue warnings. =item C<< strict => $bool >> Initialize any form objects with the given strict attribute. If the strict is turned on the methods that change values of the form will croak if you try to set illegal values or modify readonly fields. The default is not to be strict. =back =cut sub parse { my $class = shift; my $html = shift; unshift(@_, "base") if @_ == 1; my %opt = @_; require HTML::TokeParser; my $p = HTML::TokeParser->new(ref($html) ? $html->decoded_content(ref => 1) : \$html); die "Failed to create HTML::TokeParser object" unless $p; my $base_uri = delete $opt{base}; my $charset = delete $opt{charset}; my $strict = delete $opt{strict}; my $verbose = delete $opt{verbose}; if ($^W) { Carp::carp("Unrecognized option $_ in HTML::Form->parse") for sort keys %opt; } unless (defined $base_uri) { if (ref($html)) { $base_uri = $html->base; } else { Carp::croak("HTML::Form::parse: No \$base_uri provided"); } } unless (defined $charset) { if (ref($html) and $html->can("content_charset")) { $charset = $html->content_charset; } unless ($charset) { $charset = "UTF-8"; } } my @forms; my $f; # current form my %openselect; # index to the open instance of a select while (my $t = $p->get_tag) { my($tag,$attr) = @$t; if ($tag eq "form") { my $action = delete $attr->{'action'}; $action = "" unless defined $action; $action = URI->new_abs($action, $base_uri); $f = $class->new($attr->{'method'}, $action, $attr->{'enctype'}); $f->accept_charset($attr->{'accept-charset'}) if $attr->{'accept-charset'}; $f->{default_charset} = $charset; $f->{attr} = $attr; $f->strict(1) if $strict; %openselect = (); push(@forms, $f); my(%labels, $current_label); while (my $t = $p->get_tag) { my($tag, $attr) = @$t; last if $tag eq "/form"; if ($tag ne 'textarea') { # if we are inside a label tag, then keep # appending any text to the current label if(defined $current_label) { $current_label = join " ", grep { defined and length } $current_label, $p->get_phrase; } } if ($tag eq "input") { $attr->{value_name} = exists $attr->{id} && exists $labels{$attr->{id}} ? $labels{$attr->{id}} : defined $current_label ? $current_label : $p->get_phrase; } if ($tag eq "label") { $current_label = $p->get_phrase; $labels{ $attr->{for} } = $current_label if exists $attr->{for}; } elsif ($tag eq "/label") { $current_label = undef; } elsif ($tag eq "input") { my $type = delete $attr->{type} || "text"; $f->push_input($type, $attr, $verbose); } elsif ($tag eq "button") { my $type = delete $attr->{type} || "submit"; $f->push_input($type, $attr, $verbose); } elsif ($tag eq "textarea") { $attr->{textarea_value} = $attr->{value} if exists $attr->{value}; my $text = $p->get_text("/textarea"); $attr->{value} = $text; $f->push_input("textarea", $attr, $verbose); } elsif ($tag eq "select") { # rename attributes reserved to come for the option tag for ("value", "value_name") { $attr->{"select_$_"} = delete $attr->{$_} if exists $attr->{$_}; } # count this new select option separately my $name = $attr->{name}; $name = "" unless defined $name; $openselect{$name}++; while ($t = $p->get_tag) { my $tag = shift @$t; last if $tag eq "/select"; 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}; $a{idx} = $openselect{$name}; $f->push_input("option", \%a, $verbose); } else { warn("Bad here, so we # try to do the same. Actually the MSIE behaviour # appears really strange: and