Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

729 lines
16 KiB

  1. package HTML::Form;
  2. use strict;
  3. use URI;
  4. use Carp ();
  5. use vars qw($VERSION);
  6. $VERSION='0.03';
  7. my %form_tags = map {$_ => 1} qw(input textarea button select option);
  8. my %type2class = (
  9. text => "TextInput",
  10. password => "TextInput",
  11. file => "TextInput",
  12. hidden => "TextInput",
  13. textarea => "TextInput",
  14. button => "IgnoreInput",
  15. "reset" => "IgnoreInput",
  16. radio => "ListInput",
  17. checkbox => "ListInput",
  18. option => "ListInput",
  19. submit => "SubmitInput",
  20. image => "ImageInput",
  21. );
  22. =head1 NAME
  23. HTML::Form - Class that represents HTML forms
  24. =head1 SYNOPSIS
  25. use HTML::Form;
  26. $form = HTML::Form->parse($html, $base_uri);
  27. $form->value(query => "Perl");
  28. use LWP;
  29. LWP::UserAgent->new->request($form->click);
  30. =head1 DESCRIPTION
  31. Objects of the C<HTML::Form> class represents a single HTML <form>
  32. ... </form> instance. A form consist of a sequence of inputs that
  33. usually have names, and which can take on various values.
  34. The following methods are available:
  35. =over 4
  36. =item $form = HTML::Form->new($method, $action_uri, [[$enctype], $input,...])
  37. The constructor takes a $method and a $uri as argument. The $enctype
  38. and and initial inputs are optional. You will normally use
  39. HTML::Form->parse() to create new HTML::Form objects.
  40. =cut
  41. sub new {
  42. my $class = shift;
  43. my $self = bless {}, $class;
  44. $self->{method} = uc(shift || "GET");
  45. $self->{action} = shift || Carp::croak("No action defined");
  46. $self->{enctype} = shift || "application/x-www-form-urlencoded";
  47. $self->{inputs} = [@_];
  48. $self;
  49. }
  50. =item @forms = HTML::Form->parse($html_document, $base_uri)
  51. The parse() class method will parse an HTML document and build up
  52. C<HTML::Form> objects for each <form> found. If called in scalar
  53. context only returns the first <form>. Returns an empty list if there
  54. are no forms to be found.
  55. The $base_uri is (usually) the URI used to access the $html_document.
  56. It is needed to resolve relative action URIs. For LWP this parameter
  57. is obtained from the $response->base() method.
  58. =cut
  59. sub parse
  60. {
  61. my($class, $html, $base_uri) = @_;
  62. require HTML::TokeParser;
  63. my $p = HTML::TokeParser->new(\$html);
  64. my @forms;
  65. my $f; # current form
  66. while (my $t = $p->get_tag) {
  67. my($tag,$attr) = @$t;
  68. if ($tag eq "form") {
  69. my $action = $attr->{'action'};
  70. $action = "" unless defined $action;
  71. $action = URI->new_abs($action, $base_uri);
  72. $f = $class->new($attr->{'method'},
  73. $action,
  74. $attr->{'enctype'});
  75. push(@forms, $f);
  76. while (my $t = $p->get_tag) {
  77. my($tag, $attr) = @$t;
  78. last if $tag eq "/form";
  79. if ($tag eq "input") {
  80. my $type = delete $attr->{type} || "text";
  81. $f->push_input($type, $attr);
  82. } elsif ($tag eq "textarea") {
  83. $attr->{textarea_value} = $attr->{value}
  84. if exists $attr->{value};
  85. my $text = $p->get_text("/textarea");
  86. $attr->{value} = $text;
  87. $f->push_input("textarea", $attr);
  88. } elsif ($tag eq "select") {
  89. $attr->{select_value} = $attr->{value}
  90. if exists $attr->{value};
  91. while ($t = $p->get_tag) {
  92. my $tag = shift @$t;
  93. last if $tag eq "/select";
  94. next if $tag =~ m,/?optgroup,;
  95. next if $tag eq "/option";
  96. if ($tag eq "option") {
  97. my %a = (%$attr, %{$t->[0]});
  98. $a{value} = $p->get_trimmed_text
  99. unless defined $a{value};
  100. $f->push_input("option", \%a);
  101. } else {
  102. Carp::carp("Bad <select> tag '$tag'") if $^W;
  103. }
  104. }
  105. }
  106. }
  107. } elsif ($form_tags{$tag}) {
  108. Carp::carp("<$tag> outside <form>") if $^W;
  109. }
  110. }
  111. for (@forms) {
  112. $_->fixup;
  113. }
  114. wantarray ? @forms : $forms[0];
  115. }
  116. =item $form->push_input($type, \%attr)
  117. Adds a new input to the form.
  118. =cut
  119. sub push_input
  120. {
  121. my($self, $type, $attr) = @_;
  122. $type = lc $type;
  123. my $class = $type2class{$type};
  124. unless ($class) {
  125. Carp::carp("Unknown input type '$type'") if $^W;
  126. $class = "IgnoreInput";
  127. }
  128. $class = "IgnoreInput" if exists $attr->{disabled};
  129. $class = "HTML::Form::$class";
  130. my $input = $class->new(type => $type, %$attr);
  131. $input->add_to_form($self);
  132. }
  133. =item $form->method( [$new] )
  134. =item $form->action( [$new] )
  135. =item $form->enctype( [$new] )
  136. These method can be used to get/set the corresponding attribute of the
  137. form.
  138. =cut
  139. BEGIN {
  140. # Set up some accesor
  141. for (qw(method action enctype)) {
  142. my $m = $_;
  143. no strict 'refs';
  144. *{$m} = sub {
  145. my $self = shift;
  146. my $old = $self->{$m};
  147. $self->{$m} = shift if @_;
  148. $old;
  149. };
  150. }
  151. *uri = \&action; # alias
  152. }
  153. =item $form->inputs
  154. This method returns the list of inputs in the form.
  155. =cut
  156. sub inputs
  157. {
  158. my $self = shift;
  159. @{$self->{'inputs'}};
  160. }
  161. =item $form->find_input($name, $type, $no)
  162. This method is used to locate some specific input within the form. At
  163. least one of the arguments must be defined. If no matching input is
  164. found, C<undef> is returned.
  165. If $name is specified, then the input must have the indicated name.
  166. If $type is specified then the input must have the specified type. In
  167. addition to the types possible for <input> HTML tags, we also have
  168. "textarea" and "option". The $no is the sequence number of the input
  169. with the indicated $name and/or $type (where 1 is the first).
  170. =cut
  171. sub find_input
  172. {
  173. my($self, $name, $type, $no) = @_;
  174. $no ||= 1;
  175. for (@{$self->{'inputs'}}) {
  176. if (defined $name) {
  177. next unless exists $_->{name};
  178. next if $name ne $_->{name};
  179. }
  180. next if $type && $type ne $_->{type};
  181. next if --$no;
  182. return $_;
  183. }
  184. return;
  185. }
  186. sub fixup
  187. {
  188. my $self = shift;
  189. for (@{$self->{'inputs'}}) {
  190. $_->fixup;
  191. }
  192. }
  193. =item $form->value($name, [$value])
  194. The value() method can be used to get/set the value of some input. If
  195. no input have the indicated name, then this method will croak.
  196. =cut
  197. sub value
  198. {
  199. my $self = shift;
  200. my $key = shift;
  201. my $input = $self->find_input($key);
  202. Carp::croak("No such field '$key'") unless $input;
  203. local $Carp::CarpLevel = 1;
  204. $input->value(@_);
  205. }
  206. =item $form->try_others(\&callback)
  207. This method will iterate over all permutations of unvisited enumerated
  208. values (<select>, <radio>, <checkbox>) and invoke the callback for
  209. each. The callback is passed the $form as argument.
  210. =cut
  211. sub try_others
  212. {
  213. my($self, $cb) = @_;
  214. my @try;
  215. for (@{$self->{'inputs'}}) {
  216. my @not_tried_yet = $_->other_possible_values;
  217. next unless @not_tried_yet;
  218. push(@try, [\@not_tried_yet, $_]);
  219. }
  220. return unless @try;
  221. $self->_try($cb, \@try, 0);
  222. }
  223. sub _try
  224. {
  225. my($self, $cb, $try, $i) = @_;
  226. for (@{$try->[$i][0]}) {
  227. $try->[$i][1]->value($_);
  228. &$cb($self);
  229. $self->_try($cb, $try, $i+1) if $i+1 < @$try;
  230. }
  231. }
  232. =item $form->make_request
  233. Will return a HTTP::Request object that reflects the current setting
  234. of the form. You might want to use the click method instead.
  235. =cut
  236. sub make_request
  237. {
  238. my $self = shift;
  239. my $method = uc $self->{'method'};
  240. my $uri = $self->{'action'};
  241. my $enctype = $self->{'enctype'};
  242. my @form = $self->form;
  243. if ($method eq "GET") {
  244. require HTTP::Request;
  245. $uri = URI->new($uri, "http");
  246. $uri->query_form(@form);
  247. return HTTP::Request->new(GET => $uri);
  248. } elsif ($method eq "POST") {
  249. require HTTP::Request::Common;
  250. return HTTP::Request::Common::POST($uri, \@form,
  251. Content_Type => $enctype);
  252. } else {
  253. Carp::croak("Unknown method '$method'");
  254. }
  255. }
  256. =item $form->click([$name], [$x, $y])
  257. Will click on the first clickable input (C<input/submit> or
  258. C<input/image>), with the indicated $name, if specified. You can
  259. optinally specify a coordinate clicked, which only makes a difference
  260. if you clicked on an image. The default coordinate is (1,1).
  261. =cut
  262. sub click
  263. {
  264. my $self = shift;
  265. my $name;
  266. $name = shift if (@_ % 2) == 1; # odd number of arguments
  267. # try to find first submit button to activate
  268. for (@{$self->{'inputs'}}) {
  269. next unless $_->can("click");
  270. next if $name && $_->name ne $name;
  271. return $_->click($self, @_);
  272. }
  273. Carp::croak("No clickable input with name $name") if $name;
  274. $self->make_request;
  275. }
  276. =item $form->form
  277. Returns the current setting as a sequence of key/value pairs.
  278. =cut
  279. sub form
  280. {
  281. my $self = shift;
  282. map {$_->form_name_value} @{$self->{'inputs'}};
  283. }
  284. =item $form->dump
  285. Returns a textual representation of the form. Mainly useful for
  286. debugging. If called in void context, then the dump is printed on
  287. STDERR.
  288. =cut
  289. sub dump
  290. {
  291. my $self = shift;
  292. my $method = $self->{'method'};
  293. my $uri = $self->{'action'};
  294. my $enctype = $self->{'enctype'};
  295. my $dump = "$method $uri";
  296. $dump .= " ($enctype)"
  297. if $enctype eq "application/xxx-www-form-urlencoded";
  298. $dump .= "\n";
  299. for ($self->inputs) {
  300. $dump .= " " . $_->dump . "\n";
  301. }
  302. print STDERR $dump unless defined wantarray;
  303. $dump;
  304. }
  305. #---------------------------------------------------
  306. package HTML::Form::Input;
  307. =back
  308. =head1 INPUTS
  309. An C<HTML::Form> contains a sequence of inputs. References to the
  310. inputs can be obtained with the $form->inputs or $form->find_input
  311. methods. Once you have such a reference, then one of the following
  312. methods can be used on it:
  313. =over 4
  314. =cut
  315. sub new
  316. {
  317. my $class = shift;
  318. my $self = bless {@_}, $class;
  319. $self;
  320. }
  321. sub add_to_form
  322. {
  323. my($self, $form) = @_;
  324. push(@{$form->{'inputs'}}, $self);
  325. $self;
  326. }
  327. sub fixup {}
  328. =item $input->type
  329. Returns the type of this input. Types are stuff like "text",
  330. "password", "hidden", "textarea", "image", "submit", "radio",
  331. "checkbox", "option"...
  332. =cut
  333. sub type
  334. {
  335. shift->{type};
  336. }
  337. =item $input->name([$new])
  338. =item $input->value([$new])
  339. These methods can be used to set/get the current name or value of an
  340. input. If the input only can take an enumerated list of values, then
  341. it is an error to try to set it to something else and the method will
  342. croak if you try.
  343. =cut
  344. sub name
  345. {
  346. my $self = shift;
  347. my $old = $self->{name};
  348. $self->{name} = shift if @_;
  349. $old;
  350. }
  351. sub value
  352. {
  353. my $self = shift;
  354. my $old = $self->{value};
  355. $self->{value} = shift if @_;
  356. $old;
  357. }
  358. =item $input->possible_values
  359. Returns a list of all values that and input can take. For inputs that
  360. does not have discrete values this returns an empty list.
  361. =cut
  362. sub possible_values
  363. {
  364. return;
  365. }
  366. =item $input->other_possible_values
  367. Returns a list of all values not tried yet.
  368. =cut
  369. sub other_possible_values
  370. {
  371. return;
  372. }
  373. =item $input->form_name_value
  374. Returns a (possible empty) list of key/value pairs that should be
  375. incorporated in the form value from this input.
  376. =cut
  377. sub form_name_value
  378. {
  379. my $self = shift;
  380. my $name = $self->{'name'};
  381. return unless defined $name;
  382. my $value = $self->value;
  383. return unless defined $value;
  384. return ($name => $value);
  385. }
  386. sub dump
  387. {
  388. my $self = shift;
  389. my $name = $self->name;
  390. $name = "<NONAME>" unless defined $name;
  391. my $value = $self->value;
  392. $value = "<UNDEF>" unless defined $value;
  393. my $dump = "$name=$value";
  394. my $type = $self->type;
  395. return $dump if $type eq "text";
  396. $type = ($type eq "text") ? "" : " ($type)";
  397. my $menu = $self->{menu} || "";
  398. if ($menu) {
  399. my @menu;
  400. for (0 .. @$menu-1) {
  401. my $opt = $menu->[$_];
  402. $opt = "<UNDEF>" unless defined $opt;
  403. substr($opt,0,0) = "*" if $self->{seen}[$_];
  404. push(@menu, $opt);
  405. }
  406. $menu = "[" . join("|", @menu) . "]";
  407. }
  408. sprintf "%-30s %-10s %s", $dump, $type, $menu;
  409. }
  410. #---------------------------------------------------
  411. package HTML::Form::TextInput;
  412. @HTML::Form::TextInput::ISA=qw(HTML::Form::Input);
  413. #input/text
  414. #input/password
  415. #input/file
  416. #input/hidden
  417. #textarea
  418. sub value
  419. {
  420. my $self = shift;
  421. if (@_) {
  422. if (exists($self->{readonly}) || $self->{type} eq "hidden") {
  423. Carp::carp("Input '$self->{name}' is readonly") if $^W;
  424. }
  425. }
  426. $self->SUPER::value(@_);
  427. }
  428. #---------------------------------------------------
  429. package HTML::Form::IgnoreInput;
  430. @HTML::Form::IgnoreInput::ISA=qw(HTML::Form::Input);
  431. #input/button
  432. #input/reset
  433. sub value { return }
  434. #---------------------------------------------------
  435. package HTML::Form::ListInput;
  436. @HTML::Form::ListInput::ISA=qw(HTML::Form::Input);
  437. #select/option (val1, val2, ....)
  438. #input/radio (undef, val1, val2,...)
  439. #input/checkbox (undef, value)
  440. sub new
  441. {
  442. my $class = shift;
  443. my $self = $class->SUPER::new(@_);
  444. if ($self->type eq "checkbox") {
  445. my $value = delete $self->{value};
  446. $value = "on" unless defined $value;
  447. $self->{menu} = [undef, $value];
  448. $self->{current} = (exists $self->{checked}) ? 1 : 0;
  449. delete $self->{checked};
  450. } else {
  451. $self->{menu} = [delete $self->{value}];
  452. my $checked = exists $self->{checked} || exists $self->{selected};
  453. delete $self->{checked};
  454. delete $self->{selected};
  455. if (exists $self->{multiple}) {
  456. unshift(@{$self->{menu}}, undef);
  457. $self->{current} = $checked ? 1 : 0;
  458. } else {
  459. $self->{current} = 0 if $checked;
  460. }
  461. }
  462. $self;
  463. }
  464. sub add_to_form
  465. {
  466. my($self, $form) = @_;
  467. my $type = $self->type;
  468. return $self->SUPER::add_to_form($form)
  469. if $type eq "checkbox" ||
  470. ($type eq "option" && exists $self->{multiple});
  471. my $prev = $form->find_input($self->{name}, $self->{type});
  472. return $self->SUPER::add_to_form($form) unless $prev;
  473. # merge menues
  474. push(@{$prev->{menu}}, @{$self->{menu}});
  475. $prev->{current} = @{$prev->{menu}} - 1 if exists $self->{current};
  476. }
  477. sub fixup
  478. {
  479. my $self = shift;
  480. if ($self->{type} eq "option" && !(exists $self->{current})) {
  481. $self->{current} = 0;
  482. }
  483. $self->{seen} = [(0) x @{$self->{menu}}];
  484. $self->{seen}[$self->{current}] = 1 if exists $self->{current};
  485. }
  486. sub value
  487. {
  488. my $self = shift;
  489. my $old;
  490. $old = $self->{menu}[$self->{current}] if exists $self->{current};
  491. if (@_) {
  492. my $i = 0;
  493. my $val = shift;
  494. my $cur;
  495. for (@{$self->{menu}}) {
  496. if ((defined($val) && defined($_) && $val eq $_) ||
  497. (!defined($val) && !defined($_))
  498. )
  499. {
  500. $cur = $i;
  501. last;
  502. }
  503. $i++;
  504. }
  505. Carp::croak("Illegal value '$val'") unless defined $cur;
  506. $self->{current} = $cur;
  507. $self->{seen}[$cur] = 1;
  508. }
  509. $old;
  510. }
  511. sub possible_values
  512. {
  513. my $self = shift;
  514. @{$self->{menu}};
  515. }
  516. sub other_possible_values
  517. {
  518. my $self = shift;
  519. map { $self->{menu}[$_] }
  520. grep {!$self->{seen}[$_]}
  521. 0 .. (@{$self->{seen}} - 1);
  522. }
  523. #---------------------------------------------------
  524. package HTML::Form::SubmitInput;
  525. @HTML::Form::SubmitInput::ISA=qw(HTML::Form::Input);
  526. #input/image
  527. #input/submit
  528. =item $input->click($form, $x, $y)
  529. Some input types (currently "sumbit" buttons and "images") can be
  530. clicked to submit the form. The click() method returns the
  531. corrsponding C<HTTP::Request> object.
  532. =cut
  533. sub click
  534. {
  535. my($self,$form,$x,$y) = @_;
  536. for ($x, $y) { $_ = 1 unless defined; }
  537. local($self->{clicked}) = [$x,$y];
  538. return $form->make_request;
  539. }
  540. sub form_name_value
  541. {
  542. my $self = shift;
  543. return unless $self->{clicked};
  544. return $self->SUPER::form_name_value(@_);
  545. }
  546. #---------------------------------------------------
  547. package HTML::Form::ImageInput;
  548. @HTML::Form::ImageInput::ISA=qw(HTML::Form::SubmitInput);
  549. sub form_name_value
  550. {
  551. my $self = shift;
  552. my $clicked = $self->{clicked};
  553. return unless $clicked;
  554. my $name = $self->{name};
  555. return unless defined $name;
  556. return ("$name.x" => $clicked->[0],
  557. "$name.y" => $clicked->[1]
  558. );
  559. }
  560. 1;
  561. __END__
  562. =back
  563. =head1 SEE ALSO
  564. L<LWP>, L<HTML::Parser>, L<webchatpp>
  565. =head1 COPYRIGHT
  566. Copyright 1998-2000 Gisle Aas.
  567. This library is free software; you can redistribute it and/or
  568. modify it under the same terms as Perl itself.
  569. =cut