line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package HTML::Form; |
2
|
|
|
|
|
|
|
|
3
|
11
|
|
|
11
|
|
615966
|
use strict; |
|
11
|
|
|
|
|
125
|
|
|
11
|
|
|
|
|
347
|
|
4
|
11
|
|
|
11
|
|
6215
|
use URI; |
|
11
|
|
|
|
|
79125
|
|
|
11
|
|
|
|
|
312
|
|
5
|
11
|
|
|
11
|
|
70
|
use Carp (); |
|
11
|
|
|
|
|
21
|
|
|
11
|
|
|
|
|
141
|
|
6
|
11
|
|
|
11
|
|
5750
|
use Encode (); |
|
11
|
|
|
|
|
168232
|
|
|
11
|
|
|
|
|
374
|
|
7
|
|
|
|
|
|
|
|
8
|
11
|
|
|
11
|
|
4981
|
use HTML::Form::TextInput (); |
|
11
|
|
|
|
|
35
|
|
|
11
|
|
|
|
|
222
|
|
9
|
11
|
|
|
11
|
|
4224
|
use HTML::Form::IgnoreInput (); |
|
11
|
|
|
|
|
26
|
|
|
11
|
|
|
|
|
207
|
|
10
|
11
|
|
|
11
|
|
4495
|
use HTML::Form::ListInput (); |
|
11
|
|
|
|
|
31
|
|
|
11
|
|
|
|
|
230
|
|
11
|
11
|
|
|
11
|
|
4296
|
use HTML::Form::SubmitInput (); |
|
11
|
|
|
|
|
29
|
|
|
11
|
|
|
|
|
215
|
|
12
|
11
|
|
|
11
|
|
4887
|
use HTML::Form::ImageInput (); |
|
11
|
|
|
|
|
29
|
|
|
11
|
|
|
|
|
214
|
|
13
|
11
|
|
|
11
|
|
4126
|
use HTML::Form::FileInput (); |
|
11
|
|
|
|
|
33
|
|
|
11
|
|
|
|
|
220
|
|
14
|
11
|
|
|
11
|
|
4207
|
use HTML::Form::KeygenInput (); |
|
11
|
|
|
|
|
26
|
|
|
11
|
|
|
|
|
16674
|
|
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
our $VERSION = '6.11'; |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
my %form_tags = map { $_ => 1 } qw(input textarea button select option); |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
my %type2class = ( |
21
|
|
|
|
|
|
|
text => "TextInput", |
22
|
|
|
|
|
|
|
password => "TextInput", |
23
|
|
|
|
|
|
|
hidden => "TextInput", |
24
|
|
|
|
|
|
|
textarea => "TextInput", |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
"reset" => "IgnoreInput", |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
radio => "ListInput", |
29
|
|
|
|
|
|
|
checkbox => "ListInput", |
30
|
|
|
|
|
|
|
option => "ListInput", |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
button => "SubmitInput", |
33
|
|
|
|
|
|
|
submit => "SubmitInput", |
34
|
|
|
|
|
|
|
image => "ImageInput", |
35
|
|
|
|
|
|
|
file => "FileInput", |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
keygen => "KeygenInput", |
38
|
|
|
|
|
|
|
); |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
# The new HTML5 input types |
41
|
|
|
|
|
|
|
%type2class = ( |
42
|
|
|
|
|
|
|
%type2class, |
43
|
|
|
|
|
|
|
map { $_ => 'TextInput' } qw( |
44
|
|
|
|
|
|
|
tel search url email |
45
|
|
|
|
|
|
|
datetime date month week time datetime-local |
46
|
|
|
|
|
|
|
number range color |
47
|
|
|
|
|
|
|
) |
48
|
|
|
|
|
|
|
); |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
# ABSTRACT: Class that represents an HTML form element |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
sub parse { |
53
|
35
|
|
|
35
|
1
|
42861
|
my $class = shift; |
54
|
35
|
|
|
|
|
87
|
my $html = shift; |
55
|
35
|
100
|
|
|
|
145
|
unshift( @_, "base" ) if @_ == 1; |
56
|
35
|
|
|
|
|
122
|
my %opt = @_; |
57
|
|
|
|
|
|
|
|
58
|
35
|
|
|
|
|
4802
|
require HTML::TokeParser; |
59
|
35
|
100
|
|
|
|
103459
|
my $p = HTML::TokeParser->new( |
60
|
|
|
|
|
|
|
ref($html) ? $html->decoded_content( ref => 1 ) : \$html ); |
61
|
35
|
50
|
|
|
|
5433
|
Carp::croak "Failed to create HTML::TokeParser object" unless $p; |
62
|
|
|
|
|
|
|
|
63
|
35
|
|
|
|
|
88
|
my $base_uri = delete $opt{base}; |
64
|
35
|
|
|
|
|
66
|
my $charset = delete $opt{charset}; |
65
|
35
|
|
|
|
|
62
|
my $strict = delete $opt{strict}; |
66
|
35
|
|
|
|
|
73
|
my $verbose = delete $opt{verbose}; |
67
|
|
|
|
|
|
|
|
68
|
35
|
100
|
|
|
|
119
|
if ($^W) { |
69
|
|
|
|
|
|
|
Carp::carp("Unrecognized option $_ in HTML::Form->parse") |
70
|
1
|
|
|
|
|
207
|
for sort keys %opt; |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
|
73
|
35
|
100
|
|
|
|
94
|
unless ( defined $base_uri ) { |
74
|
3
|
50
|
|
|
|
11
|
if ( ref($html) ) { |
75
|
3
|
|
|
|
|
13
|
$base_uri = $html->base; |
76
|
|
|
|
|
|
|
} |
77
|
|
|
|
|
|
|
else { |
78
|
0
|
|
|
|
|
0
|
Carp::croak("HTML::Form::parse: No \$base_uri provided"); |
79
|
|
|
|
|
|
|
} |
80
|
|
|
|
|
|
|
} |
81
|
35
|
50
|
|
|
|
86
|
unless ( defined $charset ) { |
82
|
35
|
100
|
100
|
|
|
137
|
if ( ref($html) and $html->can("content_charset") ) { |
83
|
2
|
|
|
|
|
23
|
$charset = $html->content_charset; |
84
|
|
|
|
|
|
|
} |
85
|
35
|
100
|
|
|
|
445
|
unless ($charset) { |
86
|
34
|
|
|
|
|
60
|
$charset = "UTF-8"; |
87
|
|
|
|
|
|
|
} |
88
|
|
|
|
|
|
|
} |
89
|
|
|
|
|
|
|
|
90
|
35
|
|
|
|
|
104
|
my @forms; |
91
|
|
|
|
|
|
|
my $f; # current form |
92
|
|
|
|
|
|
|
|
93
|
35
|
|
|
|
|
0
|
my %openselect; # index to the open instance of a select |
94
|
|
|
|
|
|
|
|
95
|
35
|
|
|
|
|
135
|
while ( my $t = $p->get_tag ) { |
96
|
60
|
|
|
|
|
3614
|
my ( $tag, $attr ) = @$t; |
97
|
60
|
100
|
|
|
|
200
|
if ( $tag eq "form" ) { |
|
|
50
|
|
|
|
|
|
98
|
38
|
|
|
|
|
77
|
my $action = delete $attr->{'action'}; |
99
|
38
|
100
|
|
|
|
93
|
$action = "" unless defined $action; |
100
|
38
|
|
|
|
|
175
|
$action = URI->new_abs( $action, $base_uri ); |
101
|
|
|
|
|
|
|
$f = $class->new( |
102
|
|
|
|
|
|
|
$attr->{'method'}, |
103
|
|
|
|
|
|
|
$action, |
104
|
38
|
|
|
|
|
77160
|
$attr->{'enctype'} |
105
|
|
|
|
|
|
|
); |
106
|
|
|
|
|
|
|
$f->accept_charset( $attr->{'accept-charset'} ) |
107
|
38
|
100
|
|
|
|
123
|
if $attr->{'accept-charset'}; |
108
|
38
|
|
|
|
|
73
|
$f->{default_charset} = $charset; |
109
|
38
|
|
|
|
|
67
|
$f->{attr} = $attr; |
110
|
38
|
100
|
|
|
|
96
|
$f->strict(1) if $strict; |
111
|
38
|
|
|
|
|
70
|
%openselect = (); |
112
|
38
|
|
|
|
|
70
|
push( @forms, $f ); |
113
|
38
|
|
|
|
|
70
|
my ( %labels, $current_label ); |
114
|
38
|
|
|
|
|
126
|
while ( my $t = $p->get_tag ) { |
115
|
208
|
|
|
|
|
5617
|
my ( $tag, $attr ) = @$t; |
116
|
208
|
100
|
|
|
|
530
|
last if $tag eq "/form"; |
117
|
|
|
|
|
|
|
|
118
|
172
|
100
|
|
|
|
325
|
if ( $tag ne 'textarea' ) { |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
# if we are inside a label tag, then keep |
121
|
|
|
|
|
|
|
# appending any text to the current label |
122
|
169
|
100
|
|
|
|
316
|
if ( defined $current_label ) { |
123
|
|
|
|
|
|
|
$current_label = join " ", |
124
|
13
|
50
|
|
|
|
48
|
grep { defined and length } $current_label, |
|
26
|
|
|
|
|
812
|
|
125
|
|
|
|
|
|
|
$p->get_phrase; |
126
|
|
|
|
|
|
|
} |
127
|
|
|
|
|
|
|
} |
128
|
|
|
|
|
|
|
|
129
|
172
|
100
|
|
|
|
413
|
if ( $tag eq "input" ) { |
130
|
|
|
|
|
|
|
$attr->{value_name} |
131
|
|
|
|
|
|
|
= exists $attr->{id} && exists $labels{ $attr->{id} } |
132
|
|
|
|
|
|
|
? $labels{ $attr->{id} } |
133
|
66
|
100
|
100
|
|
|
375
|
: defined $current_label ? $current_label |
|
|
100
|
|
|
|
|
|
134
|
|
|
|
|
|
|
: $p->get_phrase; |
135
|
|
|
|
|
|
|
} |
136
|
|
|
|
|
|
|
|
137
|
172
|
100
|
|
|
|
4718
|
if ( $tag eq "label" ) { |
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
138
|
7
|
|
|
|
|
27
|
$current_label = $p->get_phrase; |
139
|
|
|
|
|
|
|
$labels{ $attr->{for} } = $current_label |
140
|
7
|
100
|
|
|
|
538
|
if exists $attr->{for}; |
141
|
|
|
|
|
|
|
} |
142
|
|
|
|
|
|
|
elsif ( $tag eq "/label" ) { |
143
|
7
|
|
|
|
|
23
|
$current_label = undef; |
144
|
|
|
|
|
|
|
} |
145
|
|
|
|
|
|
|
elsif ( $tag eq "input" ) { |
146
|
66
|
|
100
|
|
|
200
|
my $type = delete $attr->{type} || "text"; |
147
|
66
|
|
|
|
|
165
|
$f->push_input( $type, $attr, $verbose ); |
148
|
|
|
|
|
|
|
} |
149
|
|
|
|
|
|
|
elsif ( $tag eq "button" ) { |
150
|
2
|
|
50
|
|
|
16
|
my $type = delete $attr->{type} || "submit"; |
151
|
2
|
|
|
|
|
6
|
$f->push_input( $type, $attr, $verbose ); |
152
|
|
|
|
|
|
|
} |
153
|
|
|
|
|
|
|
elsif ( $tag eq "textarea" ) { |
154
|
|
|
|
|
|
|
$attr->{textarea_value} = $attr->{value} |
155
|
3
|
50
|
|
|
|
22
|
if exists $attr->{value}; |
156
|
3
|
|
|
|
|
15
|
my $text = $p->get_text("/textarea"); |
157
|
3
|
|
|
|
|
176
|
$attr->{value} = $text; |
158
|
3
|
|
|
|
|
13
|
$f->push_input( "textarea", $attr, $verbose ); |
159
|
|
|
|
|
|
|
} |
160
|
|
|
|
|
|
|
elsif ( $tag eq "select" ) { |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
# rename attributes reserved to come for the option tag |
163
|
29
|
|
|
|
|
69
|
for ( "value", "value_name" ) { |
164
|
|
|
|
|
|
|
$attr->{"select_$_"} = delete $attr->{$_} |
165
|
58
|
100
|
|
|
|
138
|
if exists $attr->{$_}; |
166
|
|
|
|
|
|
|
} |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
# count this new select option separately |
169
|
29
|
|
|
|
|
52
|
my $name = $attr->{name}; |
170
|
29
|
100
|
|
|
|
81
|
$name = "" unless defined $name; |
171
|
29
|
|
|
|
|
81
|
$openselect{$name}++; |
172
|
|
|
|
|
|
|
|
173
|
29
|
|
|
|
|
73
|
while ( $t = $p->get_tag ) { |
174
|
126
|
|
|
|
|
3328
|
my $tag = shift @$t; |
175
|
126
|
100
|
|
|
|
286
|
last if $tag eq "/select"; |
176
|
101
|
50
|
|
|
|
195
|
next if $tag =~ m,/?optgroup,; |
177
|
101
|
100
|
|
|
|
211
|
next if $tag eq "/option"; |
178
|
76
|
100
|
|
|
|
149
|
if ( $tag eq "option" ) { |
179
|
71
|
|
|
|
|
91
|
my %a = %{ $t->[0] }; |
|
71
|
|
|
|
|
227
|
|
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
# rename keys so they don't clash with %attr |
182
|
71
|
|
|
|
|
213
|
for ( keys %a ) { |
183
|
56
|
100
|
|
|
|
133
|
next if $_ eq "value"; |
184
|
25
|
|
|
|
|
83
|
$a{"option_$_"} = delete $a{$_}; |
185
|
|
|
|
|
|
|
} |
186
|
71
|
|
|
|
|
233
|
while ( my ( $k, $v ) = each %$attr ) { |
187
|
123
|
|
|
|
|
364
|
$a{$k} = $v; |
188
|
|
|
|
|
|
|
} |
189
|
71
|
|
|
|
|
171
|
$a{value_name} = $p->get_trimmed_text; |
190
|
|
|
|
|
|
|
$a{value} = delete $a{value_name} |
191
|
71
|
100
|
|
|
|
4596
|
unless defined $a{value}; |
192
|
71
|
|
|
|
|
125
|
$a{idx} = $openselect{$name}; |
193
|
71
|
|
|
|
|
194
|
$f->push_input( "option", \%a, $verbose ); |
194
|
|
|
|
|
|
|
} |
195
|
|
|
|
|
|
|
else { |
196
|
5
|
50
|
|
|
|
20
|
warn("Bad |
197
|
|
|
|
|
|
|
if $verbose; |
198
|
5
|
50
|
100
|
|
|
60
|
if ( $tag eq "/form" |
|
|
|
66
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
|
33
|
|
|
|
|
199
|
|
|
|
|
|
|
|| $tag eq "input" |
200
|
|
|
|
|
|
|
|| $tag eq "textarea" |
201
|
|
|
|
|
|
|
|| $tag eq "select" |
202
|
|
|
|
|
|
|
|| $tag eq "keygen" ) { |
203
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
# MSIE implicitly terminates the |
205
|
|
|
|
|
|
|
# try to do the same. Actually the MSIE behaviour |
206
|
|
|
|
|
|
|
# appears really strange: and |
207
|
|
|
|
|
|
|
# do implicitly close, but not |
208
|
|
|
|
|
|
|
# . |
209
|
4
|
100
|
|
|
|
25
|
my $type = ( $tag =~ s,^/,, ) ? "E" : "S"; |
210
|
4
|
|
|
|
|
18
|
$p->unget_token( [ $type, $tag, @$t ] ); |
211
|
4
|
|
|
|
|
40
|
last; |
212
|
|
|
|
|
|
|
} |
213
|
|
|
|
|
|
|
} |
214
|
|
|
|
|
|
|
} |
215
|
|
|
|
|
|
|
} |
216
|
|
|
|
|
|
|
elsif ( $tag eq "keygen" ) { |
217
|
1
|
|
|
|
|
3
|
$f->push_input( "keygen", $attr, $verbose ); |
218
|
|
|
|
|
|
|
} |
219
|
|
|
|
|
|
|
} |
220
|
|
|
|
|
|
|
} |
221
|
|
|
|
|
|
|
elsif ( $form_tags{$tag} ) { |
222
|
0
|
0
|
|
|
|
0
|
warn("<$tag> outside |
223
|
|
|
|
|
|
|
} |
224
|
|
|
|
|
|
|
} |
225
|
35
|
|
|
|
|
1061
|
for (@forms) { |
226
|
38
|
|
|
|
|
139
|
$_->fixup; |
227
|
|
|
|
|
|
|
} |
228
|
|
|
|
|
|
|
|
229
|
35
|
100
|
|
|
|
512
|
wantarray ? @forms : $forms[0]; |
230
|
|
|
|
|
|
|
} |
231
|
|
|
|
|
|
|
|
232
|
|
|
|
|
|
|
sub new { |
233
|
49
|
|
|
49
|
0
|
38438
|
my $class = shift; |
234
|
49
|
|
|
|
|
120
|
my $self = bless {}, $class; |
235
|
49
|
|
100
|
|
|
355
|
$self->{method} = uc( shift || "GET" ); |
236
|
49
|
|
33
|
|
|
193
|
$self->{action} = shift || Carp::croak("No action defined"); |
237
|
49
|
|
100
|
|
|
512
|
$self->{enctype} = lc( shift || "application/x-www-form-urlencoded" ); |
238
|
49
|
|
|
|
|
100
|
$self->{accept_charset} = "UNKNOWN"; |
239
|
49
|
|
|
|
|
87
|
$self->{default_charset} = "UTF-8"; |
240
|
49
|
|
|
|
|
123
|
$self->{inputs} = [@_]; |
241
|
49
|
|
|
|
|
120
|
$self; |
242
|
|
|
|
|
|
|
} |
243
|
|
|
|
|
|
|
|
244
|
|
|
|
|
|
|
sub push_input { |
245
|
154
|
|
|
154
|
1
|
337
|
my ( $self, $type, $attr, $verbose ) = @_; |
246
|
154
|
|
|
|
|
279
|
$type = lc $type; |
247
|
154
|
|
|
|
|
360
|
my $class = $type2class{$type}; |
248
|
154
|
100
|
|
|
|
348
|
unless ($class) { |
249
|
1
|
50
|
|
|
|
317
|
Carp::carp("Unknown input type '$type'") if $verbose; |
250
|
1
|
|
|
|
|
8
|
$class = "TextInput"; |
251
|
|
|
|
|
|
|
} |
252
|
154
|
|
|
|
|
284
|
$class = "HTML::Form::$class"; |
253
|
154
|
|
|
|
|
243
|
my @extra; |
254
|
154
|
100
|
|
|
|
328
|
push( @extra, readonly => 1 ) if $type eq "hidden"; |
255
|
154
|
100
|
|
|
|
327
|
push( @extra, strict => 1 ) if $self->{strict}; |
256
|
154
|
100
|
100
|
|
|
327
|
if ( $type eq "file" && exists $attr->{value} ) { |
257
|
|
|
|
|
|
|
|
258
|
|
|
|
|
|
|
# it's not safe to trust the value set by the server |
259
|
|
|
|
|
|
|
# the user always needs to explicitly set the names of files to upload |
260
|
2
|
|
|
|
|
3
|
$attr->{orig_value} = delete $attr->{value}; |
261
|
|
|
|
|
|
|
} |
262
|
154
|
|
|
|
|
219
|
delete $attr->{type}; # don't confuse the type argument |
263
|
154
|
|
|
|
|
863
|
my $input = $class->new( type => $type, %$attr, @extra ); |
264
|
154
|
|
|
|
|
455
|
$input->add_to_form($self); |
265
|
|
|
|
|
|
|
} |
266
|
|
|
|
|
|
|
|
267
|
|
|
|
|
|
|
BEGIN { |
268
|
|
|
|
|
|
|
# Set up some accessors |
269
|
11
|
|
|
11
|
|
48
|
for my $m (qw(method action enctype accept_charset)) { |
270
|
11
|
|
|
11
|
|
142
|
no strict 'refs'; |
|
11
|
|
|
|
|
37
|
|
|
11
|
|
|
|
|
1175
|
|
271
|
44
|
|
|
|
|
177
|
*{$m} = sub { |
272
|
86
|
|
|
86
|
|
6993
|
my $self = shift; |
273
|
86
|
|
|
|
|
160
|
my $old = $self->{$m}; |
274
|
86
|
100
|
|
|
|
226
|
$self->{$m} = shift if @_; |
275
|
86
|
|
|
|
|
283
|
$old; |
276
|
44
|
|
|
|
|
236
|
}; |
277
|
|
|
|
|
|
|
} |
278
|
11
|
|
|
|
|
19044
|
*uri = \&action; # alias |
279
|
|
|
|
|
|
|
} |
280
|
|
|
|
|
|
|
|
281
|
|
|
|
|
|
|
sub attr { |
282
|
2
|
|
|
2
|
1
|
1175
|
my $self = shift; |
283
|
2
|
|
|
|
|
3
|
my $name = shift; |
284
|
2
|
50
|
|
|
|
8
|
return undef unless defined $name; |
285
|
|
|
|
|
|
|
|
286
|
2
|
|
|
|
|
6
|
my $old = $self->{attr}{$name}; |
287
|
2
|
50
|
|
|
|
6
|
$self->{attr}{$name} = shift if @_; |
288
|
2
|
|
|
|
|
9
|
return $old; |
289
|
|
|
|
|
|
|
} |
290
|
|
|
|
|
|
|
|
291
|
|
|
|
|
|
|
sub strict { |
292
|
6
|
|
|
6
|
1
|
23
|
my $self = shift; |
293
|
6
|
|
|
|
|
19
|
my $old = $self->{strict}; |
294
|
6
|
50
|
|
|
|
20
|
if (@_) { |
295
|
6
|
|
|
|
|
19
|
$self->{strict} = shift; |
296
|
6
|
|
|
|
|
15
|
for my $input ( @{ $self->{inputs} } ) { |
|
6
|
|
|
|
|
24
|
|
297
|
7
|
|
|
|
|
26
|
$input->strict( $self->{strict} ); |
298
|
|
|
|
|
|
|
} |
299
|
|
|
|
|
|
|
} |
300
|
6
|
|
|
|
|
13
|
return $old; |
301
|
|
|
|
|
|
|
} |
302
|
|
|
|
|
|
|
|
303
|
|
|
|
|
|
|
sub inputs { |
304
|
45
|
|
|
45
|
1
|
88
|
my $self = shift; |
305
|
45
|
|
|
|
|
59
|
@{ $self->{'inputs'} }; |
|
45
|
|
|
|
|
135
|
|
306
|
|
|
|
|
|
|
} |
307
|
|
|
|
|
|
|
|
308
|
|
|
|
|
|
|
sub find_input { |
309
|
179
|
|
|
179
|
1
|
18108
|
my ( $self, $selector, $type, $no ) = @_; |
310
|
179
|
100
|
100
|
|
|
849
|
Carp::croak "Invalid index $no" |
311
|
|
|
|
|
|
|
if defined $no && $no < 1; |
312
|
177
|
100
|
|
|
|
340
|
if (wantarray) { |
313
|
4
|
100
|
|
|
|
22
|
warn "find_input called in list context with index specified\n" |
314
|
|
|
|
|
|
|
if defined $no; |
315
|
4
|
|
|
|
|
29
|
my @res; |
316
|
|
|
|
|
|
|
my $c; |
317
|
4
|
|
|
|
|
6
|
for ( @{ $self->{'inputs'} } ) { |
|
4
|
|
|
|
|
13
|
|
318
|
11
|
100
|
|
|
|
20
|
if ( defined($selector) ) { |
319
|
|
|
|
|
|
|
|
320
|
|
|
|
|
|
|
# an input that explicitly has no name |
321
|
9
|
100
|
|
|
|
22
|
if ( ref($selector) eq 'SCALAR' ) { |
322
|
|
|
|
|
|
|
next |
323
|
4
|
100
|
66
|
|
|
20
|
if !defined($$selector) && $_->{name}; |
324
|
|
|
|
|
|
|
} |
325
|
|
|
|
|
|
|
|
326
|
|
|
|
|
|
|
# an input that does not fit this selector |
327
|
|
|
|
|
|
|
else { |
328
|
|
|
|
|
|
|
next |
329
|
5
|
100
|
|
|
|
14
|
if !$_->selected($selector); |
330
|
|
|
|
|
|
|
} |
331
|
|
|
|
|
|
|
} |
332
|
7
|
50
|
66
|
|
|
22
|
next if $type && $type ne $_->{type}; |
333
|
7
|
|
|
|
|
9
|
$c++; |
334
|
7
|
50
|
33
|
|
|
14
|
next if $no && $no != $c; |
335
|
7
|
|
|
|
|
12
|
push( @res, $_ ); |
336
|
|
|
|
|
|
|
} |
337
|
4
|
|
|
|
|
30
|
return @res; |
338
|
|
|
|
|
|
|
|
339
|
|
|
|
|
|
|
} |
340
|
|
|
|
|
|
|
else { |
341
|
173
|
|
100
|
|
|
511
|
$no ||= 1; |
342
|
173
|
|
|
|
|
240
|
for ( @{ $self->{'inputs'} } ) { |
|
173
|
|
|
|
|
366
|
|
343
|
594
|
50
|
|
|
|
1101
|
if ( defined($selector) ) { |
344
|
|
|
|
|
|
|
|
345
|
|
|
|
|
|
|
# an input that explicitly has no name |
346
|
594
|
100
|
|
|
|
928
|
if ( ref($selector) eq 'SCALAR' ) { |
347
|
|
|
|
|
|
|
next |
348
|
19
|
100
|
66
|
|
|
72
|
if !defined($$selector) && $_->{name}; |
349
|
|
|
|
|
|
|
} |
350
|
|
|
|
|
|
|
|
351
|
|
|
|
|
|
|
# an input that does not fit this selector |
352
|
|
|
|
|
|
|
else { |
353
|
|
|
|
|
|
|
next |
354
|
575
|
100
|
|
|
|
1202
|
if !$_->selected($selector); |
355
|
|
|
|
|
|
|
} |
356
|
|
|
|
|
|
|
} |
357
|
170
|
100
|
100
|
|
|
455
|
next if $type && $type ne $_->{type}; |
358
|
162
|
100
|
|
|
|
298
|
next if --$no; |
359
|
140
|
|
|
|
|
593
|
return $_; |
360
|
|
|
|
|
|
|
} |
361
|
33
|
|
|
|
|
88
|
return undef; |
362
|
|
|
|
|
|
|
} |
363
|
|
|
|
|
|
|
} |
364
|
|
|
|
|
|
|
|
365
|
|
|
|
|
|
|
sub fixup { |
366
|
38
|
|
|
38
|
0
|
75
|
my $self = shift; |
367
|
38
|
|
|
|
|
101
|
for ( @{ $self->{'inputs'} } ) { |
|
38
|
|
|
|
|
88
|
|
368
|
105
|
|
|
|
|
312
|
$_->fixup; |
369
|
|
|
|
|
|
|
} |
370
|
|
|
|
|
|
|
} |
371
|
|
|
|
|
|
|
|
372
|
|
|
|
|
|
|
sub value { |
373
|
41
|
|
|
41
|
1
|
11544
|
my $self = shift; |
374
|
41
|
|
|
|
|
73
|
my $key = shift; |
375
|
41
|
|
|
|
|
118
|
my $input = $self->find_input($key); |
376
|
41
|
50
|
|
|
|
88
|
unless ($input) { |
377
|
0
|
0
|
|
|
|
0
|
Carp::croak("No such field '$key'") if $self->{strict}; |
378
|
0
|
0
|
|
|
|
0
|
return undef unless @_; |
379
|
0
|
|
|
|
|
0
|
$input = $self->push_input( "text", { name => $key, value => "" } ); |
380
|
|
|
|
|
|
|
} |
381
|
41
|
|
|
|
|
65
|
local $Carp::CarpLevel = 1; |
382
|
41
|
|
|
|
|
193
|
$input->value(@_); |
383
|
|
|
|
|
|
|
} |
384
|
|
|
|
|
|
|
|
385
|
|
|
|
|
|
|
sub param { |
386
|
32
|
|
|
32
|
1
|
11802
|
my $self = shift; |
387
|
32
|
100
|
|
|
|
74
|
if (@_) { |
388
|
30
|
|
|
|
|
55
|
my $name = shift; |
389
|
30
|
|
|
|
|
43
|
my @inputs; |
390
|
30
|
|
|
|
|
70
|
for ( $self->inputs ) { |
391
|
186
|
|
|
|
|
364
|
my $n = $_->name; |
392
|
186
|
100
|
66
|
|
|
594
|
next if !defined($n) || $n ne $name; |
393
|
51
|
|
|
|
|
94
|
push( @inputs, $_ ); |
394
|
|
|
|
|
|
|
} |
395
|
|
|
|
|
|
|
|
396
|
30
|
100
|
|
|
|
65
|
if (@_) { |
397
|
|
|
|
|
|
|
|
398
|
|
|
|
|
|
|
# set |
399
|
8
|
50
|
|
|
|
25
|
Carp::croak "No '$name' parameter exists" unless @inputs; |
400
|
8
|
|
|
|
|
20
|
my @v = @_; |
401
|
8
|
100
|
100
|
|
|
31
|
@v = @{ $v[0] } if @v == 1 && ref( $v[0] ); |
|
3
|
|
|
|
|
12
|
|
402
|
8
|
|
|
|
|
21
|
while (@v) { |
403
|
9
|
|
|
|
|
17
|
my $v = shift @v; |
404
|
9
|
|
|
|
|
13
|
my $err; |
405
|
9
|
|
|
|
|
22
|
for my $i ( 0 .. @inputs - 1 ) { |
406
|
16
|
|
|
|
|
26
|
eval { $inputs[$i]->value($v); }; |
|
16
|
|
|
|
|
44
|
|
407
|
16
|
100
|
|
|
|
340
|
unless ($@) { |
408
|
7
|
|
|
|
|
14
|
undef($err); |
409
|
7
|
|
|
|
|
13
|
splice( @inputs, $i, 1 ); |
410
|
7
|
|
|
|
|
9
|
last; |
411
|
|
|
|
|
|
|
} |
412
|
9
|
|
66
|
|
|
32
|
$err ||= $@; |
413
|
|
|
|
|
|
|
} |
414
|
9
|
100
|
|
|
|
165
|
Carp::croak $err if $err; |
415
|
|
|
|
|
|
|
} |
416
|
|
|
|
|
|
|
|
417
|
|
|
|
|
|
|
# the rest of the input should be cleared |
418
|
6
|
|
|
|
|
15
|
for (@inputs) { |
419
|
6
|
|
|
|
|
14
|
$_->value(undef); |
420
|
|
|
|
|
|
|
} |
421
|
|
|
|
|
|
|
} |
422
|
|
|
|
|
|
|
else { |
423
|
|
|
|
|
|
|
# get |
424
|
22
|
|
|
|
|
32
|
my @v; |
425
|
22
|
|
|
|
|
37
|
for (@inputs) { |
426
|
35
|
100
|
|
|
|
79
|
if ( defined( my $v = $_->value ) ) { |
427
|
22
|
|
|
|
|
49
|
push( @v, $v ); |
428
|
|
|
|
|
|
|
} |
429
|
|
|
|
|
|
|
} |
430
|
22
|
100
|
|
|
|
147
|
return wantarray ? @v : $v[0]; |
431
|
|
|
|
|
|
|
} |
432
|
|
|
|
|
|
|
} |
433
|
|
|
|
|
|
|
else { |
434
|
|
|
|
|
|
|
# list parameter names |
435
|
2
|
|
|
|
|
4
|
my @n; |
436
|
|
|
|
|
|
|
my %seen; |
437
|
2
|
|
|
|
|
6
|
for ( $self->inputs ) { |
438
|
14
|
|
|
|
|
58
|
my $n = $_->name; |
439
|
14
|
100
|
66
|
|
|
52
|
next if !defined($n) || $seen{$n}++; |
440
|
8
|
|
|
|
|
24
|
push( @n, $n ); |
441
|
|
|
|
|
|
|
} |
442
|
2
|
|
|
|
|
19
|
return @n; |
443
|
|
|
|
|
|
|
} |
444
|
|
|
|
|
|
|
} |
445
|
|
|
|
|
|
|
|
446
|
|
|
|
|
|
|
sub try_others { |
447
|
0
|
|
|
0
|
1
|
0
|
my ( $self, $cb ) = @_; |
448
|
0
|
|
|
|
|
0
|
my @try; |
449
|
0
|
|
|
|
|
0
|
for ( @{ $self->{'inputs'} } ) { |
|
0
|
|
|
|
|
0
|
|
450
|
0
|
|
|
|
|
0
|
my @not_tried_yet = $_->other_possible_values; |
451
|
0
|
0
|
|
|
|
0
|
next unless @not_tried_yet; |
452
|
0
|
|
|
|
|
0
|
push( @try, [ \@not_tried_yet, $_ ] ); |
453
|
|
|
|
|
|
|
} |
454
|
0
|
0
|
|
|
|
0
|
return unless @try; |
455
|
0
|
|
|
|
|
0
|
$self->_try( $cb, \@try, 0 ); |
456
|
|
|
|
|
|
|
} |
457
|
|
|
|
|
|
|
|
458
|
|
|
|
|
|
|
sub _try { |
459
|
0
|
|
|
0
|
|
0
|
my ( $self, $cb, $try, $i ) = @_; |
460
|
0
|
|
|
|
|
0
|
for ( @{ $try->[$i][0] } ) { |
|
0
|
|
|
|
|
0
|
|
461
|
0
|
|
|
|
|
0
|
$try->[$i][1]->value($_); |
462
|
0
|
|
|
|
|
0
|
&$cb($self); |
463
|
0
|
0
|
|
|
|
0
|
$self->_try( $cb, $try, $i + 1 ) if $i + 1 < @$try; |
464
|
|
|
|
|
|
|
} |
465
|
|
|
|
|
|
|
} |
466
|
|
|
|
|
|
|
|
467
|
|
|
|
|
|
|
sub make_request { |
468
|
42
|
|
|
42
|
1
|
87
|
my $self = shift; |
469
|
42
|
|
|
|
|
90
|
my $method = uc $self->{'method'}; |
470
|
42
|
|
|
|
|
66
|
my $uri = $self->{'action'}; |
471
|
42
|
|
|
|
|
59
|
my $enctype = $self->{'enctype'}; |
472
|
42
|
|
|
|
|
85
|
my @form = $self->form; |
473
|
|
|
|
|
|
|
|
474
|
|
|
|
|
|
|
my $charset |
475
|
|
|
|
|
|
|
= $self->accept_charset eq "UNKNOWN" |
476
|
|
|
|
|
|
|
? $self->{default_charset} |
477
|
42
|
100
|
|
|
|
108
|
: $self->accept_charset; |
478
|
42
|
|
|
|
|
81
|
foreach my $fi (@form) { |
479
|
142
|
100
|
|
|
|
7933
|
$fi = Encode::encode( $charset, $fi ) unless ref($fi); |
480
|
|
|
|
|
|
|
} |
481
|
|
|
|
|
|
|
|
482
|
42
|
100
|
|
|
|
782
|
if ( $method eq "GET" ) { |
|
|
50
|
|
|
|
|
|
483
|
21
|
|
|
|
|
1625
|
require HTTP::Request; |
484
|
21
|
|
|
|
|
46872
|
$uri = URI->new( $uri, "http" ); |
485
|
21
|
|
|
|
|
1706
|
$uri->query_form(@form); |
486
|
21
|
|
|
|
|
1817
|
return HTTP::Request->new( GET => $uri ); |
487
|
|
|
|
|
|
|
} |
488
|
|
|
|
|
|
|
elsif ( $method eq "POST" ) { |
489
|
21
|
|
|
|
|
1617
|
require HTTP::Request::Common; |
490
|
21
|
|
|
|
|
23230
|
return HTTP::Request::Common::POST( |
491
|
|
|
|
|
|
|
$uri, \@form, |
492
|
|
|
|
|
|
|
Content_Type => $enctype |
493
|
|
|
|
|
|
|
); |
494
|
|
|
|
|
|
|
} |
495
|
|
|
|
|
|
|
else { |
496
|
0
|
|
|
|
|
0
|
Carp::croak("Unknown method '$method'"); |
497
|
|
|
|
|
|
|
} |
498
|
|
|
|
|
|
|
} |
499
|
|
|
|
|
|
|
|
500
|
|
|
|
|
|
|
sub click { |
501
|
31
|
|
|
31
|
1
|
107
|
my $self = shift; |
502
|
31
|
|
|
|
|
48
|
my $name; |
503
|
31
|
50
|
|
|
|
89
|
$name = shift if ( @_ % 2 ) == 1; # odd number of arguments |
504
|
|
|
|
|
|
|
|
505
|
|
|
|
|
|
|
# try to find first submit button to activate |
506
|
31
|
|
|
|
|
42
|
for ( @{ $self->{'inputs'} } ) { |
|
31
|
|
|
|
|
63
|
|
507
|
59
|
100
|
|
|
|
241
|
next unless $_->can("click"); |
508
|
7
|
50
|
33
|
|
|
23
|
next if $name && !$_->selected($name); |
509
|
7
|
100
|
|
|
|
22
|
next if $_->disabled; |
510
|
6
|
|
|
|
|
26
|
return $_->click( $self, @_ ); |
511
|
|
|
|
|
|
|
} |
512
|
25
|
50
|
|
|
|
60
|
Carp::croak("No clickable input with name $name") if $name; |
513
|
25
|
|
|
|
|
55
|
$self->make_request; |
514
|
|
|
|
|
|
|
} |
515
|
|
|
|
|
|
|
|
516
|
|
|
|
|
|
|
sub form { |
517
|
44
|
|
|
44
|
1
|
88
|
my $self = shift; |
518
|
44
|
|
|
|
|
58
|
map { $_->form_name_value($self) } @{ $self->{'inputs'} }; |
|
94
|
|
|
|
|
251
|
|
|
44
|
|
|
|
|
85
|
|
519
|
|
|
|
|
|
|
} |
520
|
|
|
|
|
|
|
|
521
|
|
|
|
|
|
|
sub dump { |
522
|
2
|
|
|
2
|
1
|
4
|
my $self = shift; |
523
|
2
|
|
|
|
|
5
|
my $method = $self->{'method'}; |
524
|
2
|
|
|
|
|
12
|
my $uri = $self->{'action'}; |
525
|
2
|
|
|
|
|
3
|
my $enctype = $self->{'enctype'}; |
526
|
2
|
|
|
|
|
9
|
my $dump = "$method $uri"; |
527
|
2
|
50
|
|
|
|
19
|
$dump .= " ($enctype)" |
528
|
|
|
|
|
|
|
if $enctype ne "application/x-www-form-urlencoded"; |
529
|
|
|
|
|
|
|
$dump .= " [$self->{attr}{name}]" |
530
|
2
|
100
|
|
|
|
14
|
if exists $self->{attr}{name}; |
531
|
2
|
|
|
|
|
5
|
$dump .= "\n"; |
532
|
|
|
|
|
|
|
|
533
|
2
|
|
|
|
|
8
|
for ( $self->inputs ) { |
534
|
1
|
|
|
|
|
7
|
$dump .= " " . $_->dump . "\n"; |
535
|
|
|
|
|
|
|
} |
536
|
2
|
50
|
|
|
|
6
|
print STDERR $dump unless defined wantarray; |
537
|
2
|
|
|
|
|
9
|
$dump; |
538
|
|
|
|
|
|
|
} |
539
|
|
|
|
|
|
|
|
540
|
|
|
|
|
|
|
1; |
541
|
|
|
|
|
|
|
|
542
|
|
|
|
|
|
|
__END__ |