blib/lib/HTML/Form.pm

filter: Statementstmt 92.6% 531 / 573 Branchbran 80.9% 298 / 368 Conditioncond 78.3% 116 / 148 MC/DCmcdc 65.5% 57 / 87 Subroutinesub 92.0% 58 / 63 Podpod 24.0% 13 / 54 totaltotal 82.9% 1073 / 1293 CC 263 SCAR 32.6
CC
263
Cov
87%
dump
116.5
parse
63.2
value
27.1
CRAP 26.2
↑ Summary
1 package HTML::Form;
2  
3 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 10 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 10 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
6 10 ▶use Encode ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 10 our $VERSION = '6.10';
9  
10 10 my %form_tags = map {$_ => 1} qw(input textarea button select option);
11  
12 10 my %type2class = (
13 text => "TextInput",
14 password => "TextInput",
15 hidden => "TextInput",
16 textarea => "TextInput",
17  
18 "reset" => "IgnoreInput",
19  
20 radio => "ListInput",
21 checkbox => "ListInput",
22 option => "ListInput",
23  
24 button => "SubmitInput",
25 submit => "SubmitInput",
26 image => "ImageInput",
27 file => "FileInput",
28  
29 keygen => "KeygenInput",
30 );
31  
32 # The new HTML5 input types
33 10 %type2class = (%type2class, map { $_ => 'TextInput' } qw(
34 tel search url email
35 datetime date month week time datetime-local
36 number range color
37 ));
38  
39 # ABSTRACT: Class that represents an HTML form element
40  
41  
42 sub parse
43 {
44 34 ▶ my $class = shift;
Subroutine parse: called CC=60 SCAR=41.5
Pod documented
45 34 my $html = shift;
46 34 ▶ unshift(@_, "base") if @_ == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 1 23 11 34
47 34 my %opt = @_;
48  
49 34 require HTML::TokeParser;
50 34 ▶ my $p = HTML::TokeParser->new(ref($html) ? $html->decoded_content(ref => 1) : \$html);
Branch: 1 branch
BranchTrueFalseTotal
ref $html ? : 3 31 34
51 34 ▶ Carp::croak "Failed to create HTML::TokeParser object" unless $p;
Branch: 1 branch
BranchTrueFalseTotal
unless $p 0 34 34
52  
53 34 my $base_uri = delete $opt{base};
54 34 my $charset = delete $opt{charset};
55 34 my $strict = delete $opt{strict};
56 34 my $verbose = delete $opt{verbose};
57  
58 34 ▶ if ($^W) {
Branch: 1 branch
BranchTrueFalseTotal
if ($^W) 0 34 34
59 0 Carp::carp("Unrecognized option $_ in HTML::Form->parse") for sort keys %opt;
60 }
61  
62 34 ▶ unless (defined $base_uri) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $base_uri) 3 31 34
63 3 ▶ if (ref($html)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $html) { } 3 0 3
64 3 $base_uri = $html->base;
65 }
66 else {
67 0 Carp::croak("HTML::Form::parse: No \$base_uri provided");
68 }
69 }
70 34 ▶ unless (defined $charset) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $charset) 34 0 34
71 34 ▶ if (ref($html) and $html->can("content_charset")) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $html and $html->can("content_charset")) 2 32 34
Condition100%3/3
ref $html and $html->can("content_charset")
!ll&&!rl&&r
3112
MC/DC100%2/2
ref $html and $html->can("content_charset")
ref $html $html->can("content_charset")
Truth table100%3/3
A and B
A= ref $htmlB= $html->can("content_charset")
ABresult
0X0
100
111
72 2 $charset = $html->content_charset;
73 }
74 34 ▶ unless ($charset) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($charset) 33 1 34
75 33 $charset = "UTF-8";
76 }
77 }
78  
79 34 my @forms;
80 my $f; # current form
81  
82 34 my %openselect; # index to the open instance of a select
83  
84 34 ▶ while (my $t = $p->get_tag) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $t = $p->get_tag) 60 34 94
85 60 my($tag,$attr) = @$t;
86 60 ▶ if ($tag eq "form") {
Branch: 2 branches
BranchTrueFalseTotal
if ($tag eq 'form') { } 38 22 60
elsif ($form_tags{$tag}) { } 0 22 22
87 38 my $action = delete $attr->{'action'};
88 38 ▶ $action = "" unless defined $action;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $action 25 13 38
89 38 $action = URI->new_abs($action, $base_uri);
90 $f = $class->new($attr->{'method'},
91 $action,
92 38 $attr->{'enctype'});
93 38 ▶ $f->accept_charset($attr->{'accept-charset'}) if $attr->{'accept-charset'};
Branch: 1 branch
BranchTrueFalseTotal
if $attr->{"accept-charset"} 1 37 38
94 38 $f->{default_charset} = $charset;
95 38 $f->{attr} = $attr;
96 38 ▶ $f->strict(1) if $strict;
Branch: 1 branch
BranchTrueFalseTotal
if $strict 5 33 38
97 38 %openselect = ();
98 38 push(@forms, $f);
99 38 my(%labels, $current_label);
100 38 ▶ while (my $t = $p->get_tag) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $t = $p->get_tag) 208 2 210
101 208 my($tag, $attr) = @$t;
102 208 ▶ last if $tag eq "/form";
Branch: 1 branch
BranchTrueFalseTotal
if $tag eq "/form" 36 172 208
103  
104 172 ▶ if ($tag ne 'textarea') {
Branch: 1 branch
BranchTrueFalseTotal
if ($tag ne "textarea") 169 3 172
105 # if we are inside a label tag, then keep
106 # appending any text to the current label
107 169 ▶ if(defined $current_label) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $current_label) 13 156 169
108 $current_label = join " ",
109 13 ▶ grep { defined and length }
Branch: 1 branch
BranchTrueFalseTotal
defined $_ and length $_ 26 0 26
110 $current_label,
111 $p->get_phrase;
112 }
113 }
114  
115 172 ▶ if ($tag eq "input") {
Branch: 1 branch
BranchTrueFalseTotal
if ($tag eq "input") 66 106 172
116 $attr->{value_name} =
117 exists $attr->{id} && exists $labels{$attr->{id}} ? $labels{$attr->{id}} :
118 66 ▶ defined $current_label ? $current_label :
Branch: 2 branches
BranchTrueFalseTotal
exists $attr->{'id'} && exists $labels{$attr->{'id'}} ? : 1 65 66
defined $current_label ? : 5 60 65
Condition100%3/3
exists $attr->{"id"} and exists $labels{$attr->{"id"}}
!ll&&!rl&&r
5781
MC/DC100%2/2
exists $attr->{"id"} and exists $labels{$attr->{"id"}}
exists $attr->{"id"} exists $labels{$attr->{"id"}}
Truth table100%3/3
A and B
A= exists $attr->{"id"}B= exists $labels{$attr->{"id"}}
ABresult
0X0
100
111
119 $p->get_phrase;
120 }
121  
122 172 ▶ if ($tag eq "label") {
Branch: 7 branches
BranchTrueFalseTotal
if ($tag eq 'label') { } 7 165 172
elsif ($tag eq '/label') { } 7 158 165
elsif ($tag eq 'input') { } 66 92 158
elsif ($tag eq 'button') { } 2 90 92
elsif ($tag eq 'textarea') { } 3 87 90
elsif ($tag eq 'select') { } 29 58 87
elsif ($tag eq 'keygen') { } 1 57 58
123 7 $current_label = $p->get_phrase;
124 $labels{ $attr->{for} } = $current_label
125 7 ▶ if exists $attr->{for};
Branch: 1 branch
BranchTrueFalseTotal
if exists $attr->{"for"} 1 6 7
126 }
127 elsif ($tag eq "/label") {
128 7 $current_label = undef;
129 }
130 elsif ($tag eq "input") {
131 66 ▶ my $type = delete $attr->{type} || "text";
Condition100%2/2
delete $attr->{'type'} || 'text'
l!l
5313
MC/DC100%1/1
delete $attr->{'type'} || 'text'
delete $attr->{'type'}
Truth table100%2/2
A
A= delete $attr->{'type'}
Aresult
11
00
132 66 $f->push_input($type, $attr, $verbose);
133 }
134 elsif ($tag eq "button") {
135 2 ▶ my $type = delete $attr->{type} || "submit";
Condition50%1/2
delete $attr->{'type'} || 'submit'
l!l
20
MC/DC0%0/1
delete $attr->{'type'} || 'submit'
delete $attr->{'type'}
Truth table50%1/2
A
A= delete $attr->{'type'}
Aresult
11
00
136 2 $f->push_input($type, $attr, $verbose);
137 }
138 elsif ($tag eq "textarea") {
139 $attr->{textarea_value} = $attr->{value}
140 3 ▶ if exists $attr->{value};
Branch: 1 branch
BranchTrueFalseTotal
if exists $attr->{"value"} 0 3 3
141 3 my $text = $p->get_text("/textarea");
142 3 $attr->{value} = $text;
143 3 $f->push_input("textarea", $attr, $verbose);
144 }
145 elsif ($tag eq "select") {
146 # rename attributes reserved to come for the option tag
147 29 for ("value", "value_name") {
148 $attr->{"select_$_"} = delete $attr->{$_}
149 58 ▶ if exists $attr->{$_};
Branch: 1 branch
BranchTrueFalseTotal
if exists $attr->{$_} 1 57 58
150 }
151 # count this new select option separately
152 29 my $name = $attr->{name};
153 29 ▶ $name = "" unless defined $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 4 25 29
154 29 $openselect{$name}++;
155  
156 29 ▶ while ($t = $p->get_tag) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t = $p->get_tag) 126 0 126
157 126 my $tag = shift @$t;
158 126 ▶ last if $tag eq "/select";
Branch: 1 branch
BranchTrueFalseTotal
if $tag eq "/select" 25 101 126
159 101 ▶ next if $tag =~ m,/?optgroup,;
Branch: 1 branch
BranchTrueFalseTotal
if $tag =~ m[/?optgroup] 0 101 101
160 101 ▶ next if $tag eq "/option";
Branch: 1 branch
BranchTrueFalseTotal
if $tag eq "/option" 25 76 101
161 76 ▶ if ($tag eq "option") {
Branch: 1 branch
BranchTrueFalseTotal
if ($tag eq 'option') { } 71 5 76
162 71 my %a = %{$t->[0]};
163 # rename keys so they don't clash with %attr
164 71 for (keys %a) {
165 56 ▶ next if $_ eq "value";
Branch: 1 branch
BranchTrueFalseTotal
if $_ eq "value" 31 25 56
166 25 $a{"option_$_"} = delete $a{$_};
167 }
168 71 ▶ while (my($k,$v) = each %$attr) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$attr) 123 71 194
169 123 $a{$k} = $v;
170 }
171 71 $a{value_name} = $p->get_trimmed_text;
172 $a{value} = delete $a{value_name}
173 71 ▶ unless defined $a{value};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $a{"value"} 40 31 71
174 71 $a{idx} = $openselect{$name};
175 71 $f->push_input("option", \%a, $verbose);
176 }
177 else {
178 5 ▶ warn("Bad <select> tag '$tag' in $base_uri\n") if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 5 5
179 5 ▶ if ($tag eq "/form" ||
Branch: 1 branch
BranchTrueFalseTotal
if ($tag eq "/form" or $tag eq "input" or $tag eq "textarea" or $tag eq "select" or $tag eq "keygen") 0 1 1
Condition75%9/12
$tag eq "/form" or $tag eq "input" or $tag eq "textarea" or $tag eq "select" or $tag eq "keygen"
l!l&&r!l&&!r
401
$tag eq "/form" or $tag eq "input" or $tag eq "textarea" or $tag eq "select"
l!l&&r!l&&!r
401
$tag eq "/form" or $tag eq "input" or $tag eq "textarea"
l!l&&r!l&&!r
401
$tag eq "/form" or $tag eq "input"
l!l&&r!l&&!r
221
MC/DC40%2/5
$tag eq "/form" or $tag eq "input" or $tag eq "textarea" or $tag eq "select" or $tag eq "keygen"
$tag eq "/form" $tag eq "input" $tag eq "textarea" $tag eq "select" $tag eq "keygen"
Truth table50%3/6
A or B or C or D or E
A= $tag eq "/form"B= $tag eq "input"C= $tag eq "textarea"D= $tag eq "select"E= $tag eq "keygen"
ABCDEresult
1XXXX1
01XXX1
001XX1
0001X1
000011
000000
180 $tag eq "input" ||
181 $tag eq "textarea" ||
182 $tag eq "select" ||
183 $tag eq "keygen")
184 {
185 # MSIE implicitly terminates the <select> here, so we
186 # try to do the same. Actually the MSIE behaviour
187 # appears really strange: <input> and <textarea>
188 # do implicitly close, but not <select>, <keygen> or
189 # </form>.
190 4 ▶ my $type = ($tag =~ s,^/,,) ? "E" : "S";
Branch: 1 branch
BranchTrueFalseTotal
$tag =~ s[^/][] ? : 2 2 4
191 4 $p->unget_token([$type, $tag, @$t]);
192 4 last;
193 }
194 }
195 }
196 }
197 elsif ($tag eq "keygen") {
198 1 $f->push_input("keygen", $attr, $verbose);
199 }
200 }
201 }
202 elsif ($form_tags{$tag}) {
203 0 ▶ warn("<$tag> outside <form> in $base_uri\n") if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 0 0
204 }
205 }
206 34 for (@forms) {
207 38 $_->fixup;
208 }
209  
210 34 ▶ wantarray ? @forms : $forms[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 5 29 34
211 }
212  
213 sub new {
214 49 ▶ my $class = shift;
Subroutine new: called CC=4 SCAR=13.9
Pod undocumented
215 49 my $self = bless {}, $class;
216 49 ▶ $self->{method} = uc(shift || "GET");
Condition100%2/2
shift() || 'GET'
l!l
2029
MC/DC100%1/1
shift() || 'GET'
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
217 49 ▶ $self->{action} = shift || Carp::croak("No action defined");
Condition33%1/3
shift() || Carp::croak('No action defined')
l!l&&r!l&&!r
4900
MC/DC0%0/2
shift() || Carp::croak('No action defined')
shift() Carp::croak('No action defined')
Truth table33%1/3
A || B
A= shift()B= Carp::croak('No action defined')
ABresult
1X1
011
000
218 49 ▶ $self->{enctype} = lc(shift || "application/x-www-form-urlencoded");
Condition100%2/2
shift() || 'application/x-www-form-urlencoded'
l!l
1435
MC/DC100%1/1
shift() || 'application/x-www-form-urlencoded'
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
219 49 $self->{accept_charset} = "UNKNOWN";
220 49 $self->{default_charset} = "UTF-8";
221 49 $self->{inputs} = [@_];
222 49 $self;
223 }
224  
225  
226 sub push_input
227 {
228 154 ▶ my($self, $type, $attr, $verbose) = @_;
Subroutine push_input: called CC=7 SCAR=19.5
Pod documented
229 154 $type = lc $type;
230 154 my $class = $type2class{$type};
231 154 ▶ unless ($class) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($class) 1 153 154
232 1 ▶ Carp::carp("Unknown input type '$type'") if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 1 0 1
233 1 $class = "TextInput";
234 }
235 154 $class = "HTML::Form::$class";
236 154 my @extra;
237 154 ▶ push(@extra, readonly => 1) if $type eq "hidden";
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "hidden" 7 147 154
238 154 ▶ push(@extra, strict => 1) if $self->{strict};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"strict"} 43 111 154
239 154 ▶ if ($type eq "file" && exists $attr->{value}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "file" and exists $attr->{"value"}) 2 152 154
Condition100%3/3
$type eq "file" and exists $attr->{"value"}
!ll&&!rl&&r
141112
MC/DC100%2/2
$type eq "file" and exists $attr->{"value"}
$type eq "file" exists $attr->{"value"}
Truth table100%3/3
A and B
A= $type eq "file"B= exists $attr->{"value"}
ABresult
0X0
100
111
240 # it's not safe to trust the value set by the server
241 # the user always needs to explicitly set the names of files to upload
242 2 $attr->{orig_value} = delete $attr->{value};
243 }
244 154 delete $attr->{type}; # don't confuse the type argument
245 154 my $input = $class->new(type => $type, %$attr, @extra);
246 154 $input->add_to_form($self);
247 }
248  
249  
250  
251 BEGIN {
252 # Set up some accessors
253 10 ▶ for my $m (qw(method action enctype accept_charset)) {
Subroutine BEGIN: called CC=2 SCAR=6.9
254 10 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
255 40 *{$m} = sub {
256 86 ▶ my $self = shift;
Subroutine __ANON__: called CC=2 SCAR=6.9
257 86 my $old = $self->{$m};
258 86 ▶ $self->{$m} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 7 79 86
259 86 $old;
260 40 };
261 }
262 10 *uri = \&action; # alias
263 }
264  
265  
266 sub attr {
267 2 ▶ my $self = shift;
Subroutine attr: called CC=3 SCAR=11.2
Pod documented
268 2 my $name = shift;
269 2 ▶ return undef unless defined $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 0 2 2
270  
271 2 my $old = $self->{attr}{$name};
272 2 ▶ $self->{attr}{$name} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 2 2
273 2 return $old;
274 }
275  
276  
277 sub strict {
278 6 ▶ my $self = shift;
Subroutine strict: called CC=3 SCAR=11.0
Pod documented
279 6 my $old = $self->{strict};
280 6 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 6 0 6
281 6 $self->{strict} = shift;
282 6 for my $input (@{$self->{inputs}}) {
283 7 $input->strict($self->{strict});
284 }
285 }
286 6 return $old;
287 }
288  
289  
290  
291 sub inputs
292 {
293 45 ▶ my $self = shift;
Subroutine inputs: called CC=1 SCAR=0.0
Pod documented
294 45 @{$self->{'inputs'}};
295 }
296  
297  
298  
299 sub find_input
300 {
301 179 ▶ my($self, $selector, $type, $no) = @_;
Subroutine find_input: called CC=25 SCAR=32.5
Pod documented
302 179 ▶ Carp::croak "Invalid index $no"
Branch: 1 branch
BranchTrueFalseTotal
if defined $no and $no < 1 2 177 179
Condition100%3/3
defined $no and $no < 1
!ll&&!rl&&r
104732
MC/DC100%2/2
defined $no and $no < 1
defined $no $no < 1
Truth table100%3/3
A and B
A= defined $noB= $no < 1
ABresult
0X0
100
111
303 if defined $no && $no < 1;
304 177 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 4 173 177
305 4 ▶ warn "find_input called in list context with index specified\n"
Branch: 1 branch
BranchTrueFalseTotal
if defined $no 1 3 4
306 if defined $no;
307 4 my @res;
308 my $c;
309 4 for (@{$self->{'inputs'}}) {
310 11 ▶ if ( defined($selector) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $selector) 9 2 11
311  
312 # an input that explicitly has no name
313 9 ▶ if ( ref($selector) eq 'SCALAR' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $selector eq 'SCALAR') { } 4 5 9
314 next
315 4 ▶ if !defined($$selector) && $_->{name};
Branch: 1 branch
BranchTrueFalseTotal
if not defined $$selector and $_->{"name"} 1 3 4
Condition66%2/3
not defined $$selector and $_->{"name"}
!ll&&!rl&&r
031
MC/DC50%1/2
not defined $$selector and $_->{"name"}
not defined $$selector $_->{"name"}
Truth table66%2/3
not(A) and B
A= not defined $$selectorB= $_->{"name"}
ABresult
0X0
100
111
316 }
317  
318 # an input that does not fit this selector
319 else {
320 next
321 5 ▶ if !$_->selected($selector);
Branch: 1 branch
BranchTrueFalseTotal
unless $_->selected($selector) 3 2 5
322 }
323 }
324 7 ▶ next if $type && $type ne $_->{type};
Branch: 1 branch
BranchTrueFalseTotal
if $type and $type ne $_->{"type"} 0 7 7
Condition66%2/3
$type and $type ne $_->{"type"}
!ll&&!rl&&r
520
MC/DC0%0/2
$type and $type ne $_->{"type"}
$type $type ne $_->{"type"}
Truth table66%2/3
A and B
A= $typeB= $type ne $_->{"type"}
ABresult
0X0
100
111
325 7 $c++;
326 7 ▶ next if $no && $no != $c;
Branch: 1 branch
BranchTrueFalseTotal
if $no and $no != $c 0 7 7
Condition33%1/3
$no and $no != $c
!ll&&!rl&&r
700
MC/DC0%0/2
$no and $no != $c
$no $no != $c
Truth table33%1/3
A and B
A= $noB= $no != $c
ABresult
0X0
100
111
327 7 push(@res, $_);
328 }
329 4 return @res;
330  
331 }
332 else {
333 173 ▶ $no ||= 1;
Condition100%2/2
$no ||= 1
l!l
72101
MC/DC100%1/1
$no ||= 1
$no
Truth table100%2/2
A
A= $no
Aresult
11
00
334 173 for (@{$self->{'inputs'}}) {
335 594 ▶ if ( defined($selector) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $selector) 594 0 594
336  
337 # an input that explicitly has no name
338 594 ▶ if ( ref($selector) eq 'SCALAR' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $selector eq 'SCALAR') { } 19 575 594
339 next
340 19 ▶ if !defined($$selector) && $_->{name};
Branch: 1 branch
BranchTrueFalseTotal
if not defined $$selector and $_->{"name"} 5 14 19
Condition66%2/3
not defined $$selector and $_->{"name"}
!ll&&!rl&&r
0145
MC/DC50%1/2
not defined $$selector and $_->{"name"}
not defined $$selector $_->{"name"}
Truth table66%2/3
not(A) and B
A= not defined $$selectorB= $_->{"name"}
ABresult
0X0
100
111
341 }
342  
343 # an input that does not fit this selector
344 else {
345 next
346 575 ▶ if !$_->selected($selector);
Branch: 1 branch
BranchTrueFalseTotal
unless $_->selected($selector) 419 156 575
347 }
348 }
349 170 ▶ next if $type && $type ne $_->{type};
Branch: 1 branch
BranchTrueFalseTotal
if $type and $type ne $_->{"type"} 8 162 170
Condition100%3/3
$type and $type ne $_->{"type"}
!ll&&!rl&&r
119438
MC/DC100%2/2
$type and $type ne $_->{"type"}
$type $type ne $_->{"type"}
Truth table100%3/3
A and B
A= $typeB= $type ne $_->{"type"}
ABresult
0X0
100
111
350 162 ▶ next if --$no;
Branch: 1 branch
BranchTrueFalseTotal
if --$no 22 140 162
351 140 return $_;
352 }
353 33 return undef;
354 }
355 }
356  
357 sub fixup
358 {
359 38 ▶ my $self = shift;
Subroutine fixup: called CC=2 SCAR=6.9
Pod undocumented
360 38 for (@{$self->{'inputs'}}) {
361 105 $_->fixup;
362 }
363 }
364  
365  
366  
367 sub value
368 {
369 41 ▶ my $self = shift;
Subroutine value: called CC=4 SCAR=18.6
Pod documented
370 41 my $key = shift;
371 41 my $input = $self->find_input($key);
372 41 ▶ unless ($input) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($input) 0 41 41
373 0 ▶ Carp::croak("No such field '$key'") if $self->{strict};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"strict"} 0 0 0
374 0 ▶ return undef unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 0 0
375 0 $input = $self->push_input("text", { name => $key, value => "" });
376 }
377 41 local $Carp::CarpLevel = 1;
378 41 $input->value(@_);
379 }
380  
381  
382 sub param {
383 32 ▶ my $self = shift;
Subroutine param: called CC=21 SCAR=30.5
Pod documented
384 32 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 30 2 32
385 30 my $name = shift;
386 30 my @inputs;
387 30 for ($self->inputs) {
388 186 my $n = $_->name;
389 186 ▶ next if !defined($n) || $n ne $name;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $n or $n ne $name 135 51 186
Condition66%2/3
not defined $n or $n ne $name
l!l&&r!l&&!r
013551
MC/DC50%1/2
not defined $n or $n ne $name
not defined $n $n ne $name
Truth table66%2/3
not(A) or B
A= not defined $nB= $n ne $name
ABresult
1X1
011
000
390 51 push(@inputs, $_);
391 }
392  
393 30 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 8 22 30
394 # set
395 8 ▶ Carp::croak "No '$name' parameter exists" unless @inputs;
Branch: 1 branch
BranchTrueFalseTotal
unless @inputs 0 8 8
396 8 my @v = @_;
397 8 ▶ @v = @{$v[0]} if @v == 1 && ref($v[0]);
Branch: 1 branch
BranchTrueFalseTotal
if @v == 1 and ref $v[0] 3 5 8
Condition100%3/3
@v == 1 and ref $v[0]
!ll&&!rl&&r
233
MC/DC100%2/2
@v == 1 and ref $v[0]
@v == 1 ref $v[0]
Truth table100%3/3
A and B
A= @v == 1B= ref $v[0]
ABresult
0X0
100
111
398 8 ▶ while (@v) {
Branch: 1 branch
BranchTrueFalseTotal
if (@v) 9 6 15
399 9 my $v = shift @v;
400 9 my $err;
401 9 for my $i (0 .. @inputs-1) {
402 16 eval {
403 16 $inputs[$i]->value($v);
404 };
405 16 ▶ unless ($@) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($@) 7 9 16
406 7 undef($err);
407 7 splice(@inputs, $i, 1);
408 7 last;
409 }
410 9 ▶ $err ||= $@;
Condition66%2/3
$err ||= $@
l!l&&r!l&&!r
270
MC/DC0%0/2
$err ||= $@
$err $@
Truth table66%2/3
A ||= B
A= $errB= $@
ABresult
1X1
011
000
411 }
412 9 ▶ Carp::croak $err if $err;
Branch: 1 branch
BranchTrueFalseTotal
if $err 2 7 9
413 }
414  
415 # the rest of the input should be cleared
416 6 for (@inputs) {
417 6 $_->value(undef);
418 }
419 }
420 else {
421 # get
422 22 my @v;
423 22 for (@inputs) {
424 35 ▶ if (defined(my $v = $_->value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $v = $_->value)) 22 13 35
425 22 push(@v, $v);
426 }
427 }
428 22 ▶ return wantarray ? @v : $v[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 15 7 22
429 }
430 }
431 else {
432 # list parameter names
433 2 my @n;
434 my %seen;
435 2 for ($self->inputs) {
436 14 my $n = $_->name;
437 14 ▶ next if !defined($n) || $seen{$n}++;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $n or $seen{$n}++ 6 8 14
Condition66%2/3
not defined $n or $seen{$n}++
l!l&&r!l&&!r
068
MC/DC50%1/2
not defined $n or $seen{$n}++
not defined $n $seen{$n}++
Truth table66%2/3
not(A) or B
A= not defined $nB= $seen{$n}++
ABresult
1X1
011
000
438 8 push(@n, $n);
439 }
440 2 return @n;
441 }
442 }
443  
444  
445  
446 sub try_others
447 {
448 0 ▶ my($self, $cb) = @_;
Subroutine try_others: not called CC=4 SCAR=30.0
Pod documented
449 0 my @try;
450 0 for (@{$self->{'inputs'}}) {
451 0 my @not_tried_yet = $_->other_possible_values;
452 0 ▶ next unless @not_tried_yet;
Branch: 1 branch
BranchTrueFalseTotal
unless @not_tried_yet 0 0 0
453 0 push(@try, [\@not_tried_yet, $_]);
454 }
455 0 ▶ return unless @try;
Branch: 1 branch
BranchTrueFalseTotal
unless @try 0 0 0
456 0 $self->_try($cb, \@try, 0);
457 }
458  
459 sub _try
460 {
461 0 ▶ my($self, $cb, $try, $i) = @_;
Subroutine _try: not called CC=3 SCAR=24.8
462 0 for (@{$try->[$i][0]}) {
463 0 $try->[$i][1]->value($_);
464 0 &$cb($self);
465 0 ▶ $self->_try($cb, $try, $i+1) if $i+1 < @$try;
Branch: 1 branch
BranchTrueFalseTotal
if $i + 1 < @$try 0 0 0
466 }
467 }
468  
469  
470  
471 sub make_request
472 {
473 42 ▶ my $self = shift;
Subroutine make_request: called CC=6 SCAR=18.0
Pod documented
474 42 my $method = uc $self->{'method'};
475 42 my $uri = $self->{'action'};
476 42 my $enctype = $self->{'enctype'};
477 42 my @form = $self->form;
478  
479 42 ▶ my $charset = $self->accept_charset eq "UNKNOWN" ? $self->{default_charset} : $self->accept_charset;
Branch: 1 branch
BranchTrueFalseTotal
$self->accept_charset eq 'UNKNOWN' ? : 38 4 42
480 42 foreach my $fi (@form) {
481 142 ▶ $fi = Encode::encode($charset, $fi) unless ref($fi);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $fi 130 12 142
482 }
483  
484 42 ▶ if ($method eq "GET") {
Branch: 2 branches
BranchTrueFalseTotal
if ($method eq 'GET') { } 21 21 42
elsif ($method eq 'POST') { } 21 0 21
485 21 require HTTP::Request;
486 21 $uri = URI->new($uri, "http");
487 21 $uri->query_form(@form);
488 21 return HTTP::Request->new(GET => $uri);
489 }
490 elsif ($method eq "POST") {
491 21 require HTTP::Request::Common;
492 21 return HTTP::Request::Common::POST($uri, \@form,
493 Content_Type => $enctype);
494 }
495 else {
496 0 Carp::croak("Unknown method '$method'");
497 }
498 }
499  
500  
501  
502 sub click
503 {
504 31 ▶ my $self = shift;
Subroutine click: called CC=8 SCAR=21.5
Pod documented
505 31 my $name;
506 31 ▶ $name = shift if (@_ % 2) == 1; # odd number of arguments
Branch: 1 branch
BranchTrueFalseTotal
if @_ % 2 == 1 0 31 31
507  
508 # try to find first submit button to activate
509 31 for (@{$self->{'inputs'}}) {
510 59 ▶ next unless $_->can("click");
Branch: 1 branch
BranchTrueFalseTotal
unless $_->can("click") 52 7 59
511 7 ▶ next if $name && !$_->selected($name);
Branch: 1 branch
BranchTrueFalseTotal
if $name and not $_->selected($name) 0 7 7
Condition33%1/3
$name and not $_->selected($name)
!ll&&!rl&&r
700
MC/DC0%0/2
$name and not $_->selected($name)
$name not $_->selected($name)
Truth table33%1/3
A and not(B)
A= $nameB= not $_->selected($name)
ABresult
0X0
100
111
512 7 ▶ next if $_->disabled;
Branch: 1 branch
BranchTrueFalseTotal
if $_->disabled 1 6 7
513 6 return $_->click($self, @_);
514 }
515 25 ▶ Carp::croak("No clickable input with name $name") if $name;
Branch: 1 branch
BranchTrueFalseTotal
if $name 0 25 25
516 25 $self->make_request;
517 }
518  
519  
520  
521 sub form
522 {
523 44 ▶ my $self = shift;
Subroutine form: called CC=1 SCAR=0.0
Pod documented
524 44 map { $_->form_name_value($self) } @{$self->{'inputs'}};
525 }
526  
527  
528  
529 sub dump
530 {
531 2 ▶ my $self = shift;
Subroutine dump: called CC=5 SCAR=16.2
Pod documented
532 2 my $method = $self->{'method'};
533 2 my $uri = $self->{'action'};
534 2 my $enctype = $self->{'enctype'};
535 2 my $dump = "$method $uri";
536 2 ▶ $dump .= " ($enctype)"
Branch: 1 branch
BranchTrueFalseTotal
if $enctype ne "application/x-www-form-urlencoded" 0 2 2
537 if $enctype ne "application/x-www-form-urlencoded";
538 $dump .= " [$self->{attr}{name}]"
539 2 ▶ if exists $self->{attr}{name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"attr"}{"name"} 1 1 2
540 2 $dump .= "\n";
541 2 for ($self->inputs) {
542 1 $dump .= " " . $_->dump . "\n";
543 }
544 2 ▶ print STDERR $dump unless defined wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless defined wantarray 0 2 2
545 2 $dump;
546 }
547  
548  
549 #---------------------------------------------------
550 package HTML::Form::Input;
551  
552  
553 sub new
554 {
555 154 ▶ my $class = shift;
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
556 154 my $self = bless {@_}, $class;
557 154 $self;
558 }
559  
560 sub add_to_form
561 {
562 116 ▶ my($self, $form) = @_;
Subroutine add_to_form: called CC=1 SCAR=0.0
Pod undocumented
563 116 push(@{$form->{'inputs'}}, $self);
564 116 $self;
565 }
566  
567 sub strict {
568 10 ▶ my $self = shift;
Subroutine strict: called CC=2 SCAR=6.9
Pod undocumented
569 10 my $old = $self->{strict};
570 10 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 8 2 10
571 8 $self->{strict} = shift;
572 }
573 10 $old;
574 }
575  
576 ▶sub fixup {}
Subroutine fixup: called CC=1 SCAR=0.0
Pod undocumented
577  
578  
579  
580 sub type
581 {
582 145 ▶ shift->{type};
Subroutine type: called CC=1 SCAR=0.0
Pod undocumented
583 }
584  
585  
586 sub name
587 {
588 234 ▶ my $self = shift;
Subroutine name: called CC=2 SCAR=6.9
Pod undocumented
589 234 my $old = $self->{name};
590 234 ▶ $self->{name} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 1 233 234
591 234 $old;
592 }
593  
594 sub id
595 {
596 1 ▶ my $self = shift;
Subroutine id: called CC=2 SCAR=7.0
Pod undocumented
597 1 my $old = $self->{id};
598 1 ▶ $self->{id} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 1 1
599 1 $old;
600 }
601  
602 sub class
603 {
604 1 ▶ my $self = shift;
Subroutine class: called CC=2 SCAR=7.0
Pod undocumented
605 1 my $old = $self->{class};
606 1 ▶ $self->{class} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 1 1
607 1 $old;
608 }
609  
610 sub selected {
611 580 ▶ my($self, $sel) = @_;
Subroutine selected: called CC=6 SCAR=17.9
Pod undocumented
612 580 ▶ return undef unless defined $sel;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $sel 0 580 580
613 580 ▶ my $attr =
Branch: 3 branches
BranchTrueFalseTotal
$sel =~ s/^\^// ? : 6 574 580
$sel =~ s/^#// ? : 20 554 574
$sel =~ s/^\.// ? : 14 540 554
614 $sel =~ s/^\^// ? "name" :
615 $sel =~ s/^#// ? "id" :
616 $sel =~ s/^\.// ? "class" :
617 "name";
618 580 ▶ return 0 unless defined $self->{$attr};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{$attr} 5 575 580
619 575 return $self->{$attr} eq $sel;
620 }
621  
622 sub value
623 {
624 11 ▶ my $self = shift;
Subroutine value: called CC=2 SCAR=6.9
Pod undocumented
625 11 my $old = $self->{value};
626 11 ▶ $self->{value} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 1 10 11
627 11 $old;
628 }
629  
630 sub autocomplete
631 {
632 6 ▶ my $self = shift;
Subroutine autocomplete: called CC=2 SCAR=6.9
Pod undocumented
633 6 my $old = $self->{autocomplete};
634 6 ▶ $self->{autocomplete} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 2 4 6
635 6 $old;
636 }
637  
638  
639 sub possible_values
640 {
641 0 ▶ return;
Subroutine possible_values: not called CC=1 SCAR=6.9
Pod undocumented
642 }
643  
644  
645 sub other_possible_values
646 {
647 0 ▶ return;
Subroutine other_possible_values: not called CC=1 SCAR=6.9
Pod undocumented
648 }
649  
650  
651 sub value_names {
652 return
653 0 ▶}
Subroutine value_names: not called CC=1 SCAR=6.9
Pod undocumented
654  
655  
656 sub readonly {
657 6 ▶ my $self = shift;
Subroutine readonly: called CC=2 SCAR=6.9
Pod undocumented
658 6 my $old = $self->{readonly};
659 6 ▶ $self->{readonly} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 1 5 6
660 6 $old;
661 }
662  
663  
664 sub disabled {
665 60 ▶ my $self = shift;
Subroutine disabled: called CC=2 SCAR=6.9
Pod undocumented
666 60 my $old = $self->{disabled};
667 60 ▶ $self->{disabled} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 1 59 60
668 60 $old;
669 }
670  
671  
672 sub form_name_value
673 {
674 76 ▶ my $self = shift;
Subroutine form_name_value: called CC=4 SCAR=13.9
Pod undocumented
675 76 my $name = $self->{'name'};
676 76 ▶ return unless defined $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 8 68 76
677 68 ▶ return if $self->disabled;
Branch: 1 branch
BranchTrueFalseTotal
if $self->disabled 3 65 68
678 65 my $value = $self->value;
679 65 ▶ return unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 5 60 65
680 60 return ($name => $value);
681 }
682  
683 sub dump
684 {
685 1 ▶ my $self = shift;
Subroutine dump: called CC=16 SCAR=47.6
Pod undocumented
686 1 my $name = $self->name;
687 1 ▶ $name = "<NONAME>" unless defined $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 0 1 1
688 1 my $value = $self->value;
689 1 ▶ $value = "<UNDEF>" unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 1 1
690 1 my $dump = "$name=$value";
691  
692 1 my $type = $self->type;
693  
694 1 ▶ $type .= " disabled" if $self->disabled;
Branch: 1 branch
BranchTrueFalseTotal
if $self->disabled 0 1 1
695 1 ▶ $type .= " readonly" if $self->readonly;
Branch: 1 branch
BranchTrueFalseTotal
if $self->readonly 0 1 1
696 1 ▶ return sprintf "%-30s %s", $dump, "($type)" unless $self->{menu};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"menu"} 1 0 1
697  
698 0 my @menu;
699 0 my $i = 0;
700 0 for (@{$self->{menu}}) {
701 0 my $opt = $_->{value};
702 0 ▶ $opt = "<UNDEF>" unless defined $opt;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opt 0 0 0
703 $opt .= "/$_->{name}"
704 0 ▶ if defined $_->{name} && length $_->{name} && $_->{name} ne $opt;
Branch: 1 branch
BranchTrueFalseTotal
if defined $_->{"name"} and length $_->{"name"} and $_->{"name"} ne $opt 0 0 0
Condition0%0/6
defined $_->{"name"} and length $_->{"name"} and $_->{"name"} ne $opt
!ll&&!rl&&r
000
defined $_->{"name"} and length $_->{"name"}
!ll&&!rl&&r
000
MC/DC0%0/3
defined $_->{"name"} and length $_->{"name"} and $_->{"name"} ne $opt
defined $_->{"name"} length $_->{"name"} $_->{"name"} ne $opt
Truth table0%0/4
A and B and C
A= defined $_->{"name"}B= length $_->{"name"}C= $_->{"name"} ne $opt
ABCresult
0XX0
10X0
1100
1111
705 0 ▶ substr($opt,0,0) = "-" if $_->{disabled};
Branch: 1 branch
BranchTrueFalseTotal
if $_->{"disabled"} 0 0 0
706 0 ▶ if (exists $self->{current} && $self->{current} == $i) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{'current'} and $self->{'current'} == $i) { } 0 0 0
Condition0%0/3
exists $self->{"current"} and $self->{"current"} == $i
!ll&&!rl&&r
000
MC/DC0%0/2
exists $self->{"current"} and $self->{"current"} == $i
exists $self->{"current"} $self->{"current"} == $i
Truth table0%0/3
A and B
A= exists $self->{"current"}B= $self->{"current"} == $i
ABresult
0X0
100
111
707 0 ▶ substr($opt,0,0) = "!" unless $_->{seen};
Branch: 1 branch
BranchTrueFalseTotal
unless $_->{"seen"} 0 0 0
708 0 substr($opt,0,0) = "*";
709 }
710 else {
711 0 ▶ substr($opt,0,0) = ":" if $_->{seen};
Branch: 1 branch
BranchTrueFalseTotal
if $_->{"seen"} 0 0 0
712 }
713 0 push(@menu, $opt);
714 0 $i++;
715 }
716  
717 0 return sprintf "%-30s %-10s %s", $dump, "($type)", "[" . join("|", @menu) . "]";
718 }
719  
720  
721 #---------------------------------------------------
722 package HTML::Form::TextInput;
723 10 @HTML::Form::TextInput::ISA=qw(HTML::Form::Input);
724  
725 #input/text
726 #input/password
727 #input/hidden
728 #textarea
729  
730 sub value
731 {
732 116 ▶ my $self = shift;
Subroutine value: called CC=10 SCAR=23.0
Pod undocumented
733 116 my $old = $self->{value};
734 116 ▶ $old = "" unless defined $old;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $old 36 80 116
735 116 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 24 92 116
736 Carp::croak("Input '$self->{name}' is readonly")
737 24 ▶ if $self->{strict} && $self->{readonly};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"strict"} and $self->{"readonly"} 2 22 24
Condition100%3/3
$self->{"strict"} and $self->{"readonly"}
!ll&&!rl&&r
1842
MC/DC100%2/2
$self->{"strict"} and $self->{"readonly"}
$self->{"strict"} $self->{"readonly"}
Truth table100%3/3
A and B
A= $self->{"strict"}B= $self->{"readonly"}
ABresult
0X0
100
111
738 22 my $new = shift;
739 22 ▶ my $n = exists $self->{maxlength} ? $self->{maxlength} : undef;
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{'maxlength'} ? : 4 18 22
740 Carp::croak("Input '$self->{name}' has maxlength '$n'")
741 22 ▶ if $self->{strict} && defined($n) && defined($new) && length($new) > $n;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"strict"} and defined $n and defined $new and length $new > $n 1 21 22
Condition88%8/9
$self->{"strict"} and defined $n and defined $new and length $new > $n
!ll&&!rl&&r
2011
$self->{"strict"} and defined $n and defined $new
!ll&&!rl&&r
2002
$self->{"strict"} and defined $n
!ll&&!rl&&r
1822
MC/DC75%3/4
$self->{"strict"} and defined $n and defined $new and length $new > $n
$self->{"strict"} defined $n defined $new length $new > $n
Truth table80%4/5
A and B and C and D
A= $self->{"strict"}B= defined $nC= defined $newD= length $new > $n
ABCDresult
0XXX0
10XX0
110X0
11100
11111
742 21 $self->{value} = $new;
743 }
744 113 $old;
745 }
746  
747 #---------------------------------------------------
748 package HTML::Form::IgnoreInput;
749 10 @HTML::Form::IgnoreInput::ISA=qw(HTML::Form::Input);
750  
751 #input/button
752 #input/reset
753  
754 1 ▶sub value { return }
Subroutine value: called CC=1 SCAR=0.0
Pod undocumented
755  
756  
757 #---------------------------------------------------
758 package HTML::Form::ListInput;
759 10 @HTML::Form::ListInput::ISA=qw(HTML::Form::Input);
760  
761 #select/option (val1, val2, ....)
762 #input/radio (undef, val1, val2,...)
763 #input/checkbox (undef, value)
764 #select-multiple/option (undef, value)
765  
766 sub new
767 {
768 93 ▶ my $class = shift;
Subroutine new: called CC=10 SCAR=23.0
Pod undocumented
769 93 my $self = $class->SUPER::new(@_);
770  
771 93 my $value = delete $self->{value};
772 93 my $value_name = delete $self->{value_name};
773 93 my $type = $self->{type};
774  
775 93 ▶ if ($type eq "checkbox") {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq 'checkbox') { } 7 86 93
776 7 ▶ $value = "on" unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 2 5 7
777 $self->{menu} = [
778 7 { value => undef, name => "off", },
779 { value => $value, name => $value_name, },
780 ];
781 7 ▶ $self->{current} = (delete $self->{checked}) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
delete $self->{'checked'} ? : 5 2 7
782 ;
783 }
784 else {
785 $self->{option_disabled}++
786 86 ▶ if $type eq "radio" && delete $self->{disabled};
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "radio" and delete $self->{"disabled"} 4 82 86
Condition100%3/3
$type eq "radio" and delete $self->{"disabled"}
!ll&&!rl&&r
71114
MC/DC100%2/2
$type eq "radio" and delete $self->{"disabled"}
$type eq "radio" delete $self->{"disabled"}
Truth table100%3/3
A and B
A= $type eq "radio"B= delete $self->{"disabled"}
ABresult
0X0
100
111
787 $self->{menu} = [
788 86 {value => $value, name => $value_name},
789 ];
790 86 ▶ my $checked = $self->{checked} || $self->{option_selected};
Condition100%3/3
$self->{'checked'} || $self->{'option_selected'}
l!l&&r!l&&!r
21371
MC/DC100%2/2
$self->{'checked'} || $self->{'option_selected'}
$self->{'checked'} $self->{'option_selected'}
Truth table100%3/3
A || B
A= $self->{'checked'}B= $self->{'option_selected'}
ABresult
1X1
011
000
791 86 delete $self->{checked};
792 86 delete $self->{option_selected};
793 86 ▶ if (exists $self->{multiple}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{'multiple'}) { } 16 70 86
794 16 unshift(@{$self->{menu}}, { value => undef, name => "off"});
795 16 ▶ $self->{current} = $checked ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$checked ? : 2 14 16
796 }
797 else {
798 70 ▶ $self->{current} = 0 if $checked;
Branch: 1 branch
BranchTrueFalseTotal
if $checked 13 57 70
799 }
800 }
801 93 $self;
802 }
803  
804 sub add_to_form
805 {
806 93 ▶ my($self, $form) = @_;
Subroutine add_to_form: called CC=10 SCAR=23.0
Pod undocumented
807 93 my $type = $self->type;
808  
809 93 ▶ return $self->SUPER::add_to_form($form)
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "checkbox" 7 86 93
810 if $type eq "checkbox";
811  
812 86 ▶ if ($type eq "option" && exists $self->{multiple}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "option" and exists $self->{"multiple"}) 16 70 86
Condition100%3/3
$type eq "option" and exists $self->{"multiple"}
!ll&&!rl&&r
155516
MC/DC100%2/2
$type eq "option" and exists $self->{"multiple"}
$type eq "option" exists $self->{"multiple"}
Truth table100%3/3
A and B
A= $type eq "option"B= exists $self->{"multiple"}
ABresult
0X0
100
111
813 16 ▶ $self->{disabled} ||= delete $self->{option_disabled};
Condition100%3/3
$self->{"disabled"} ||= delete $self->{"option_disabled"}
l!l&&r!l&&!r
3310
MC/DC100%2/2
$self->{"disabled"} ||= delete $self->{"option_disabled"}
$self->{"disabled"} delete $self->{"option_disabled"}
Truth table100%3/3
A ||= B
A= $self->{"disabled"}B= delete $self->{"option_disabled"}
ABresult
1X1
011
000
814 16 return $self->SUPER::add_to_form($form);
815 }
816  
817 70 ▶ Carp::croak "Assert" if @{$self->{menu}} != 1;
Branch: 1 branch
BranchTrueFalseTotal
if @{$self->{"menu"};} != 1 0 70 70
818 70 my $m = $self->{menu}[0];
819 70 ▶ $m->{disabled}++ if delete $self->{option_disabled};
Branch: 1 branch
BranchTrueFalseTotal
if delete $self->{"option_disabled"} 9 61 70
820  
821 # if there was no name we have to search for an input that explicitly has
822 # no name either, because otherwise the name attribute would be ignored
823 70 ▶ my $prev = $form->find_input($self->{name} || \undef, $self->{type}, $self->{idx});
Condition100%2/2
$self->{'name'} || \(undef)
l!l
619
MC/DC100%1/1
$self->{'name'} || \(undef)
$self->{'name'}
Truth table100%2/2
A
A= $self->{'name'}
Aresult
11
00
824 70 ▶ return $self->SUPER::add_to_form($form) unless $prev;
Branch: 1 branch
BranchTrueFalseTotal
unless $prev 32 38 70
825  
826 # merge menus
827 38 ▶ $prev->{current} = @{$prev->{menu}} if exists $self->{current};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"current"} 4 34 38
828 38 push(@{$prev->{menu}}, $m);
829 }
830  
831 sub fixup
832 {
833 55 ▶ my $self = shift;
Subroutine fixup: called CC=4 SCAR=13.9
Pod undocumented
834 55 ▶ if ($self->{type} eq "option" && !(exists $self->{current})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"type"} eq "option" and not exists $self->{"current"}) 11 44 55
Condition100%3/3
$self->{"type"} eq "option" and not exists $self->{"current"}
!ll&&!rl&&r
172711
MC/DC100%2/2
$self->{"type"} eq "option" and not exists $self->{"current"}
$self->{"type"} eq "option" not exists $self->{"current"}
Truth table100%3/3
A and not(B)
A= $self->{"type"} eq "option"B= not exists $self->{"current"}
ABresult
0X0
100
111
835 11 $self->{current} = 0;
836 }
837 55 ▶ $self->{menu}[$self->{current}]{seen}++ if exists $self->{current};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"current"} 47 8 55
838 }
839  
840 sub disabled
841 {
842 46 ▶ my $self = shift;
Subroutine disabled: called CC=4 SCAR=13.9
Pod undocumented
843 46 my $type = $self->type;
844  
845 46 ▶ my $old = $self->{disabled} || _menu_all_disabled(@{$self->{menu}});
Condition100%3/3
$self->{'disabled'} || _menu_all_disabled(@{$$self{'menu'};})
l!l&&r!l&&!r
10432
MC/DC100%2/2
$self->{'disabled'} || _menu_all_disabled(@{$$self{'menu'};})
$self->{'disabled'} _menu_all_disabled(@{$$self{'menu'};})
Truth table100%3/3
A || B
A= $self->{'disabled'}B= _menu_all_disabled(@{$$self{'menu'};})
ABresult
1X1
011
000
846 46 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 5 41 46
847 5 my $v = shift;
848 5 $self->{disabled} = $v;
849 5 for (@{$self->{menu}}) {
850 11 $_->{disabled} = $v;
851 }
852 }
853 46 return $old;
854 }
855  
856 sub _menu_all_disabled {
857 36 ▶ for (@_) {
Subroutine _menu_all_disabled: called CC=3 SCAR=11.0
858 38 ▶ return 0 unless $_->{disabled};
Branch: 1 branch
BranchTrueFalseTotal
unless $_->{"disabled"} 32 6 38
859 }
860 4 return 1;
861 }
862  
863 sub value
864 {
865 96 ▶ my $self = shift;
Subroutine value: called CC=27 SCAR=33.0
Pod undocumented
866 96 my $old;
867 96 ▶ $old = $self->{menu}[$self->{current}]{value} if exists $self->{current};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"current"} 92 4 96
868 96 ▶ $old = $self->{value} if exists $self->{value};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"value"} 0 96 96
869 96 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 41 55 96
870 41 my $i = 0;
871 41 my $val = shift;
872 41 my $cur;
873 my $disabled;
874 41 for (@{$self->{menu}}) {
875 79 ▶ if ((defined($val) && defined($_->{value}) && $val eq $_->{value}) ||
Branch: 1 branch
BranchTrueFalseTotal
if (defined $val and defined $_->{"value"} and $val eq $_->{"value"} or not defined $val || defined $_->{"value"}) 27 52 79
Condition100%12/12
defined $val and defined $_->{"value"} and $val eq $_->{"value"} or not defined $val || defined $_->{"value"}
l!l&&r!l&&!r
19852
defined $val and defined $_->{"value"} and $val eq $_->{"value"}
!ll&&!rl&&r
293119
defined $val and defined $_->{"value"}
!ll&&!rl&&r
92050
defined $val || defined $_->{'value'}
l!l&&r!l&&!r
5118
MC/DC100%5/5
defined $val and defined $_->{"value"} and $val eq $_->{"value"} or not defined $val || defined $_->{"value"}
defined $val defined $_->{"value"} $val eq $_->{"value"} defined $val defined $_->{'value'}
Truth table50%5/10
A and B and C or not(D || E)
A= defined $valB= defined $_->{"value"}C= $val eq $_->{"value"}D= defined $valE= defined $_->{'value'}
ABCDEresult
111XX1
0XX001
10X001
110001
0XX1X0
0XX010
10X1X0
10X010
1101X0
110010
876 (!defined($val) && !defined($_->{value}))
877 )
878 {
879 27 $cur = $i;
880 27 $disabled = $_->{disabled};
881 27 ▶ last unless $disabled;
Branch: 1 branch
BranchTrueFalseTotal
unless $disabled 20 7 27
882 }
883 59 $i++;
884 }
885 41 ▶ if (!(defined $cur) || $disabled) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $cur or $disabled) 21 20 41
Condition100%3/3
not defined $cur or $disabled
l!l&&r!l&&!r
14720
MC/DC100%2/2
not defined $cur or $disabled
not defined $cur $disabled
Truth table100%3/3
not(A) or B
A= not defined $curB= $disabled
ABresult
1X1
011
000
886 21 ▶ if (defined $val) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $val) { } 20 1 21
elsif ($self->{'strict'}) { } 1 0 1
887 # try to search among the alternative names as well
888 20 my $i = 0;
889 20 my $cur_ignorecase;
890 20 my $lc_val = lc($val);
891 20 for (@{$self->{menu}}) {
892 43 ▶ if (defined $_->{name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_->{"name"}) 35 8 43
893 35 ▶ if ($val eq $_->{name}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val eq $_->{"name"}) 4 31 35
894 4 $disabled = $_->{disabled};
895 4 $cur = $i;
896 4 ▶ last unless $disabled;
Branch: 1 branch
BranchTrueFalseTotal
unless $disabled 3 1 4
897 }
898 32 ▶ if (!defined($cur_ignorecase) && $lc_val eq lc($_->{name})) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $cur_ignorecase and $lc_val eq lc $_->{"name"}) 3 29 32
Condition100%3/3
not defined $cur_ignorecase and $lc_val eq lc $_->{"name"}
!ll&&!rl&&r
1283
MC/DC100%2/2
not defined $cur_ignorecase and $lc_val eq lc $_->{"name"}
not defined $cur_ignorecase $lc_val eq lc $_->{"name"}
Truth table100%3/3
not(A) and B
A= not defined $cur_ignorecaseB= $lc_val eq lc $_->{"name"}
ABresult
0X0
100
111
899 3 $cur_ignorecase = $i;
900 }
901 }
902 40 $i++;
903 }
904 20 ▶ unless (defined $cur) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $cur) 10 10 20
905 10 $cur = $cur_ignorecase;
906 10 ▶ if (defined $cur) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $cur) { } 2 8 10
elsif ($self->{'strict'}) { } 8 0 8
907 2 $disabled = $self->{menu}[$cur]{disabled};
908 }
909 elsif ($self->{strict}) {
910 8 my $n = $self->name;
911 8 Carp::croak("Illegal value '$val' for field '$n'");
912 }
913 }
914 }
915 elsif ($self->{strict}) {
916 1 my $n = $self->name;
917 1 Carp::croak("The '$n' field can't be unchecked");
918 }
919 }
920 32 ▶ if ($self->{strict} && $disabled) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"strict"} and $disabled) 7 25 32
Condition100%3/3
$self->{"strict"} and $disabled
!ll&&!rl&&r
6197
MC/DC100%2/2
$self->{"strict"} and $disabled
$self->{"strict"} $disabled
Truth table100%3/3
A and B
A= $self->{"strict"}B= $disabled
ABresult
0X0
100
111
921 7 my $n = $self->name;
922 7 Carp::croak("The value '$val' has been disabled for field '$n'");
923 }
924 25 ▶ if (defined $cur) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cur) { } 25 0 25
925 25 $self->{current} = $cur;
926 25 $self->{menu}[$cur]{seen}++;
927 25 delete $self->{value};
928 }
929 else {
930 0 $self->{value} = $val;
931 0 delete $self->{current};
932 }
933 }
934 80 $old;
935 }
936  
937  
938 sub check
939 {
940 1 ▶ my $self = shift;
Subroutine check: called CC=1 SCAR=0.0
Pod undocumented
941 1 $self->{current} = 1;
942 1 $self->{menu}[1]{seen}++;
943 }
944  
945 sub possible_values
946 {
947 2 ▶ my $self = shift;
Subroutine possible_values: called CC=1 SCAR=0.0
Pod undocumented
948 2 map $_->{value}, grep !$_->{disabled}, @{$self->{menu}};
949 }
950  
951 sub other_possible_values
952 {
953 3 ▶ my $self = shift;
Subroutine other_possible_values: called CC=2 SCAR=6.9
Pod undocumented
954 3 ▶ map $_->{value}, grep !$_->{seen} && !$_->{disabled}, @{$self->{menu}};
Condition100%3/3
$_->{'seen'} || $_->{'disabled'}
l!l&&r!l&&!r
412
MC/DC100%2/2
$_->{'seen'} || $_->{'disabled'}
$_->{'seen'} $_->{'disabled'}
Truth table100%3/3
A || B
A= $_->{'seen'}B= $_->{'disabled'}
ABresult
1X1
011
000
955 }
956  
957 sub value_names {
958 7 ▶ my $self = shift;
Subroutine value_names: called CC=3 SCAR=11.0
Pod undocumented
959 7 my @names;
960 7 for (@{$self->{menu}}) {
961 10 my $n = $_->{name};
962 10 ▶ $n = $_->{value} unless defined $n;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $n 1 9 10
963 10 push(@names, $n);
964 }
965 7 @names;
966 }
967  
968  
969 #---------------------------------------------------
970 package HTML::Form::SubmitInput;
971 10 @HTML::Form::SubmitInput::ISA=qw(HTML::Form::Input);
972  
973 #input/image
974 #input/submit
975  
976  
977 sub click
978 {
979 6 ▶ my($self,$form,$x,$y) = @_;
Subroutine click: called CC=4 SCAR=13.9
Pod undocumented
980 6 ▶ for ($x, $y) { $_ = 1 unless defined; }
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_ 12 0 12
981 6 local($self->{clicked}) = [$x,$y];
982 6 ▶ local($self->{value}) = "" unless defined $self->value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->value 3 3 6
983 6 return $form->make_request;
984 }
985  
986 sub form_name_value
987 {
988 7 ▶ my $self = shift;
Subroutine form_name_value: called CC=2 SCAR=6.9
Pod undocumented
989 7 ▶ return unless $self->{clicked};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"clicked"} 3 4 7
990 4 return $self->SUPER::form_name_value(@_);
991 }
992  
993  
994 #---------------------------------------------------
995 package HTML::Form::ImageInput;
996 10 @HTML::Form::ImageInput::ISA=qw(HTML::Form::SubmitInput);
997  
998 sub form_name_value
999 {
1000 2 ▶ my $self = shift;
Subroutine form_name_value: called CC=5 SCAR=16.4
Pod undocumented
1001 2 my $clicked = $self->{clicked};
1002 2 ▶ return unless $clicked;
Branch: 1 branch
BranchTrueFalseTotal
unless $clicked 0 2 2
1003 2 ▶ return if $self->{disabled};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"disabled"} 0 2 2
1004 2 my $name = $self->{name};
1005 2 ▶ $name = (defined($name) && length($name)) ? "$name." : "";
Branch: 1 branch
BranchTrueFalseTotal
defined $name && length $name ? : 1 1 2
Condition66%2/3
defined $name and length $name
!ll&&!rl&&r
101
MC/DC50%1/2
defined $name and length $name
defined $name length $name
Truth table66%2/3
A and B
A= defined $nameB= length $name
ABresult
0X0
100
111
1006 2 return ("${name}x" => $clicked->[0],
1007 "${name}y" => $clicked->[1]
1008 );
1009 }
1010  
1011 #---------------------------------------------------
1012 package HTML::Form::FileInput;
1013 10 @HTML::Form::FileInput::ISA=qw(HTML::Form::TextInput);
1014  
1015  
1016 sub file {
1017 32 ▶ my $self = shift;
Subroutine file: called CC=1 SCAR=0.0
Pod undocumented
1018 32 $self->value(@_);
1019 }
1020  
1021  
1022 sub filename {
1023 16 ▶ my $self = shift;
Subroutine filename: called CC=3 SCAR=11.0
Pod undocumented
1024 16 my $old = $self->{filename};
1025 16 ▶ $self->{filename} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 3 13 16
1026 16 ▶ $old = $self->file unless defined $old;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $old 13 3 16
1027 16 $old;
1028 }
1029  
1030  
1031 sub content {
1032 15 ▶ my $self = shift;
Subroutine content: called CC=2 SCAR=6.9
Pod undocumented
1033 15 my $old = $self->{content};
1034 15 ▶ $self->{content} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 2 13 15
1035 15 $old;
1036 }
1037  
1038  
1039 sub headers {
1040 13 ▶ my $self = shift;
Subroutine headers: called CC=3 SCAR=11.4
Pod undocumented
1041 13 ▶ my $old = $self->{headers} || [];
Condition50%1/2
$self->{'headers'} || []
l!l
013
MC/DC0%0/1
$self->{'headers'} || []
$self->{'headers'}
Truth table50%1/2
A
A= $self->{'headers'}
Aresult
11
00
1042 13 ▶ $self->{headers} = [@_] if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 13 13
1043 13 @$old;
1044 }
1045  
1046 sub form_name_value {
1047 14 ▶ my($self, $form) = @_;
Subroutine form_name_value: called CC=11 SCAR=24.2
Pod undocumented
1048 14 ▶ return $self->SUPER::form_name_value($form)
Branch: 1 branch
BranchTrueFalseTotal
if $form->method ne "POST" or $form->enctype ne "multipart/form-data" 1 13 14
Condition66%2/3
$form->method ne "POST" or $form->enctype ne "multipart/form-data"
l!l&&r!l&&!r
0113
MC/DC50%1/2
$form->method ne "POST" or $form->enctype ne "multipart/form-data"
$form->method ne "POST" $form->enctype ne "multipart/form-data"
Truth table66%2/3
A or B
A= $form->method ne "POST"B= $form->enctype ne "multipart/form-data"
ABresult
1X1
011
000
1049 if $form->method ne "POST" ||
1050 $form->enctype ne "multipart/form-data";
1051  
1052 13 my $name = $self->name;
1053 13 ▶ return unless defined $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 0 13 13
1054 13 ▶ return if $self->{disabled};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"disabled"} 0 13 13
1055  
1056 13 my $file = $self->file;
1057 13 my $filename = $self->filename;
1058 13 my @headers = $self->headers;
1059 13 my $content = $self->content;
1060 13 my %headers = @headers;
1061 13 ▶ if (defined $content || grep m/^Content$/i, keys %headers) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $content or grep /^Content$/i, keys %headers) { } 2 11 13
elsif (not defined $file or length $file == 0) { } 1 10 11
Condition66%2/3
defined $content or grep /^Content$/i, keys %headers
l!l&&r!l&&!r
2011
MC/DC50%1/2
defined $content or grep /^Content$/i, keys %headers
defined $content grep /^Content$/i, keys %headers
Truth table66%2/3
A or B
A= defined $contentB= grep /^Content$/i, keys %headers
ABresult
1X1
011
000
1062 2 ▶ $filename = $file unless defined $filename;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $filename 0 2 2
1063 2 $file = undef;
1064 2 unshift(@headers, "Content" => $content);
1065 }
1066 ▶ elsif (!defined($file) || length($file) == 0) {
Condition66%2/3
not defined $file or length $file == 0
l!l&&r!l&&!r
0110
MC/DC50%1/2
not defined $file or length $file == 0
not defined $file length $file == 0
Truth table66%2/3
not(A) or B
A= not defined $fileB= length $file == 0
ABresult
1X1
011
000
1067 1 return;
1068 }
1069  
1070 # legacy (this used to be the way to do it)
1071 12 ▶ if (ref($file) eq "ARRAY") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $file eq "ARRAY") 7 5 12
1072 7 my $f = shift @$file;
1073 7 my $fn = shift @$file;
1074 7 push(@headers, @$file);
1075 7 $file = $f;
1076 7 $filename = $fn;
1077 }
1078  
1079 12 return ($name => [$file, $filename, @headers]);
1080 }
1081  
1082 package HTML::Form::KeygenInput;
1083 10 @HTML::Form::KeygenInput::ISA=qw(HTML::Form::Input);
1084  
1085 sub challenge {
1086 1 ▶ my $self = shift;
Subroutine challenge: called CC=1 SCAR=0.0
Pod undocumented
1087 1 return $self->{challenge};
1088 }
1089  
1090 sub keytype {
1091 1 ▶ my $self = shift;
Subroutine keytype: called CC=2 SCAR=7.2
Pod undocumented
1092 1 ▶ return lc($self->{keytype} || 'rsa');
Condition50%1/2
$self->{'keytype'} || 'rsa'
l!l
01
MC/DC0%0/1
$self->{'keytype'} || 'rsa'
$self->{'keytype'}
Truth table50%1/2
A
A= $self->{'keytype'}
Aresult
11
00
1093 }
1094  
1095 10 1;
1096  
1097 __END__
↑ Summary