Mechanize/Form.pm untested

filter: Statementstmt 0.0% 0 / 133 Branchbran 0.0% 0 / 56 Conditioncond 0.0% 0 / 33 MC/DCmcdc 0.0% 0 / 17 Subroutinesub 0.0% 0 / 16 Podpod 0.0% 0 / 13 totaltotal 0.0% 0 / 268 CC 29 SCAR 34.1
CC
29
Cov
0%
inputs
72.0
attr
56.0
find_input
30.0
CRAP 30.2
1 package Mozilla::Mechanize::Form;
2 use strict;
3 use warnings;
4  
5 # $Id: Form.pm,v 1.4 2005/10/07 12:17:24 slanning Exp $
6  
7 use Mozilla::Mechanize::Input;
8  
9 =head1 NAME
10  
11 Mozilla::Mechanize::Form - Mimic L<HTML::Form>
12  
13 =head1 SYNOPSIS
14  
15 Read the code for now
16  
17 =head1 DESCRIPTION
18  
19 The C<Mozilla::Mechanize::Form> object is a thin wrapper around
20 L<Mozilla::DOM::HTMLFormElement|Mozilla::DOM::HTMLFormElement>.
21  
22 =head1 METHODS
23  
24 =head2 Mozilla::Mechanize::Form->new($form_node, $moz);
25  
26 Initialize a new object. $form_node is a
27 L<Mozilla::DOM::HTMLFormElement|Mozilla::DOM::HTMLFormElement>
28 (or a node that can be QueryInterfaced to one).
29 $moz is a L<Mozilla::Mechanize|Mozilla::Mechanize> object.
30 (This latter is a hack for `submit' and `reset',
31 so that new pages can load in the browser. The GUI has to be
32 able to enter its main loop. If you don't plan to use those
33 methods, you don't have to pass it in.)
34  
35  
36 =cut
37  
38 sub new {
39 my $class = shift;
40 my $node = shift;
41 my $moz = shift;
42  
43 # turn the Node into an HTMLFormElement
44 my $form = $node->QueryInterface(Mozilla::DOM::HTMLFormElement->GetIID);
45  
46 my $self = { form => $form };
47 $self->{moz} = $moz if defined $moz;
48 bless($self, $class);
49 }
50  
51 =head2 $form->method( [$new_method] )
52  
53 Get/Set the I<method> used to submit the from (i.e. B<GET> or B<POST>).
54  
55 =cut
56  
57 sub method {
58 my $self = shift;
59 my $val = shift;
60 my $form = $self->{form};
61 $form->SetMethod($val) if defined $val;
62 my $method = $form->GetMethod;
63  
64 # XXX: provide default value (not sure about this...)
65 $method = 'GET' unless $method =~ /\S/;
66 return $method;
67 }
68  
69 =head2 $form->action( [$new_action] )
70  
71 Get/Set the I<action> for submitting the form.
72  
73 =cut
74  
75 sub action {
76 my $self = shift;
77 my $val = shift;
78 my $form = $self->{form};
79 $form->SetAction($val) if defined $val;
80 # This is supposed to be required in HTML 4.01
81 return $form->GetAction;
82 }
83  
84 =head2 $form->enctype( [$new_enctype] )
85  
86 Get/Set the I<enctype> of the form.
87  
88 =cut
89  
90 sub enctype {
91 my $self = shift;
92 my $val = shift;
93 my $form = $self->{form};
94 $form->SetEnctype($val) if defined $val;
95 my $enctype = $form->GetEnctype;
96  
97 # XXX: provide default value (not sure about this...)
98 $enctype = 'application/x-www-form-urlencoded' unless $enctype =~ /\S/;
99 return $enctype;
100 }
101  
102 =head2 $form->name()
103  
104 Return the name of this form.
105  
106 =cut
107  
108 sub name {
109 my $self = shift;
110 my $val = shift;
111 my $form = $self->{form};
112 $form->SetName($val) if defined $val;
113 return $form->GetName;
114 }
115  
116 =head2 $form->attr( $name[, $new_value] )
117  
118 Get/Set any of the attributes from the FORM-tag
119 (acceptcharset, action, enctype, method, name, target
120 (returns undef if $name isn't one of these)).
121  
122 =cut
123  
124 sub attr {
125 my $self = shift;
126 return unless @_;
127 my $name = shift;
128 my $form = $self->{form};
129  
130 my ($attr) = grep $name =~ /^$_$/i, qw(AcceptCharset Action Enctype Method Name Target);
131 return unless defined $attr;
132  
133 my $method = "Set$attr";
134 $form->$method(shift) if @_;
135 $method = "Get$attr";
136 my $val = $form->$method;
137  
138 # Defaults for non-present attributes
139 unless ($val =~ /\S/) {
140 $val = 'GET' if $attr eq 'Method';
141 $val = 'application/x-www-form-urlencoded' if $attr eq 'Enctype';
142 # action is supposed to be required in HTML 4.01
143 }
144  
145 return $val;
146 }
147  
148 =head2 $form->inputs()
149  
150 Returns a list of L<Mozilla::Mechanize::Input> objects.
151 In scalar context it will return the number of inputs.
152  
153 B<XXX: I'm confused about how radio buttons are implemented.
154 Win32::IE::Mechanize only pushes on the first one for some reason.
155 (I push them all for now.)>
156  
157 =cut
158  
159 {
160 # Recursively get input elements. This is necessary in order
161 # to preserve their order. (cf. _extract_links, _extract_images in Mechanize.pm)
162 my (@inputs, %radio_seen);
163  
164 sub inputs {
165 my ($self, $subelement) = @_; # 2nd arg is undocumented
166 my $node;
167  
168 # The first time, it's called with no subelement
169 if (defined $subelement) {
170 $node = $subelement;
171 } else {
172 @inputs = ();
173 %radio_seen = ();
174 $node = $self->{form};
175 }
176  
177 # If it's an input element, get it; otherwise, recurse if has children
178 if ($node->GetNodeName =~ /^(input|button|select|textarea)$/i) {
179 my $tagname = lc $1;
180 if ($tagname eq 'input') {
181 my $input = $node->QueryInterface(Mozilla::DOM::HTMLInputElement->GetIID);
182 my $name = lc $input->GetName;
183 my $type = lc $input->GetType;
184 # For some reason, we only get the first in a radio group
185 unless ($type eq 'radio' and $radio_seen{$name}++) {
186 push @inputs, Mozilla::Mechanize::Input->new($node, $self->{moz});
187 }
188 } else {
189 push @inputs, Mozilla::Mechanize::Input->new($node, $self->{moz});
190 }
191 $self->debug("added '$tagname' input");
192 } elsif ($node->HasChildNodes) {
193 my @children = $node->GetChildNodes;
194 # skips #text nodes
195 foreach my $child (grep {$_->GetNodeName !~ /^#/} @children) {
196 $self->inputs($child);
197 }
198 }
199  
200 # Continue only at the top-level
201 return if defined $subelement;
202  
203 return wantarray ? @inputs : scalar @inputs;
204 }
205 }
206  
207 =head2 $form->find_input( $name[, $type[, $index]] )
208  
209 This method is used to locate specific inputs within the form. All
210 inputs that match the arguments given are returned. In scalar context
211 only the first is returned, or C<undef> if none match.
212  
213 If $name is specified, then the input must have the indicated name.
214  
215 If $type is specified, then the input must have the specified type.
216 The following type names are used: "text", "password", "hidden",
217 "textarea", "file", "image", "submit", "radio", "checkbox", and "option".
218 (and "button" and "select"?)
219  
220 The $index is the sequence number of the input matched where 1 is the
221 first. If combined with $name and/or $type then it select the I<n>th
222 input with the given name and/or type.
223  
224 (This method is ported from L<HTML::Form>)
225  
226 =cut
227  
228 sub find_input {
229 my $self = shift;
230 my( $name, $type, $index ) = @_;
231  
232 my $form = $self->{form};
233  
234 my $typere = qr/.*/;
235 $type and $typere = $type =~ /^select/i ? qr/^$type/i : qr/^$type$/i;
236  
237 if ( wantarray ) {
238 my( $cnt, @res ) = ( 0 );
239 for my $input ( $self->inputs ) {
240 if ( defined $name ) {
241 $input->name or next;
242 $input->name ne $name and next;
243 }
244  
245 $input->type =~ $typere or next;
246 $cnt++;
247 $index && $index ne $cnt and next;
248 push @res, $input;
249 }
250 return @res;
251 } else {
252 $index ||= 1;
253 for my $input ( $self->inputs ) {
254 if ( defined $name ) {
255 $input->name or next;
256 $input->name ne $name and next;
257 }
258 $input->type =~ $typere or next;
259 --$index and next;
260 return $input;
261 }
262 return undef;
263 }
264 }
265  
266 =head2 $form->value( $name[, $new_value] )
267  
268 Get/Set the value for the input-control with specified name.
269  
270 =cut
271  
272 sub value {
273 my $self = shift;
274 my $input = $self->find_input( shift );
275 return $input->value( @_ );
276 }
277  
278 =head2 $form->submit()
279  
280 Submit this form. (Note: does B<not> trigger onSubmit.)
281  
282 =cut
283  
284 sub submit {
285 my $self = shift;
286 my $form = $self->{form};
287 $form->Submit();
288  
289 # XXX: if they didn't pass $moz to `new', they're stuck..
290 my $moz = $self->{moz} || return;
291 $moz->_wait_while_busy();
292 }
293  
294 =head2 $form->reset()
295  
296 Reset inputs to their default values.
297 (Note: I added this method, though it wasn't in WWW::Mechanize.)
298  
299 =cut
300  
301 sub reset {
302 my $self = shift;
303 my $form = $self->{form};
304 $form->Reset();
305  
306 # XXX: if they didn't pass $moz to `new', they're stuck..
307 my $moz = $self->{moz} || return;
308 $moz->_wait_while_busy();
309 }
310  
311 =head2 $self->_radio_group( $name )
312  
313 Returns a list of Mozilla::DOM::HTMLInputElement objects with name eq $name.
314 (Intended for use with Input.pm's radio_value method.)
315  
316 =cut
317  
318 sub _radio_group {
319 my $self = shift;
320 my $form = $self->{form};
321  
322 my $name = shift or return;
323 my @rgroup;
324  
325 my @inputs = $form->GetElementsByTagName('input');
326 my $iid = Mozilla::DOM::HTMLInputElement->GetIID;
327 foreach my $input (map {$_->QueryInterface($iid)} @inputs) {
328 next unless lc($input->GetType) eq 'radio';
329 next unless lc($input->GetName) eq lc($name);
330 push @rgroup, $input;
331 }
332  
333 return wantarray ? @rgroup : \@rgroup;
334 }
335  
336 sub debug {
337 my ($self, $msg) = @_;
338 my (undef, $file, $line) = caller();
339 print STDERR "$msg at $file line $line\n" if $self->{debug};
340 }
341  
342  
343 1;
344  
345 __END__