lib/CGI/FormBuilder/Field/text.pm

filter: Statementstmt 88.3% 68 / 77 Branchbran 61.7% 21 / 34 Conditioncond 54.1% 13 / 24 MC/DCmcdc 40.0% 6 / 15 Subroutinesub 100% 8 / 8 Podpod 50.0% 1 / 2 totaltotal 73.1% 117 / 160 SCAR 36.8
CC
28
Cov
76%
tag
51.8
script
3.0
BEGIN
1.0
CRAP 39.5
« blib/lib/CGI/FormBuilder/Template/Text.pm ↑ Summary lib/CGI/FormBuilder/Field/select.pm »
1  
2 ###########################################################################
3 # Copyright (c) Nate Wiger http://nateware.com. All Rights Reserved.
4 # Please visit http://formbuilder.org for tutorials, support, and examples.
5 ###########################################################################
6  
7 # The majority of this module's methods (including new) are
8 # inherited directly from ::base, since they involve things
9 # which are common, such as parameter parsing. The only methods
10 # that are individual to different fields are those that affect
11 # the rendering, such as script() and tag()
12  
13 package CGI::FormBuilder::Field::text;
14  
15 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 6 ▶no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 6 ▶use CGI::FormBuilder::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 6 ▶use CGI::FormBuilder::Field;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 6 ▶use base 'CGI::FormBuilder::Field';
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23  
24 6 our $VERSION = '3.10';
25  
26 sub script {
27 218 ▶ my $self = shift;
Subroutine script: called CC=3 SCAR=11.0
Pod undocumented
28 218 my $name = $self->name;
29  
30 # The way script() works is slightly backwards: First the
31 # type-specific JS DOM code is generated, then this is
32 # passed as a string to Field->jsfield, which wraps this
33 # in the generic handling.
34  
35 # Holders for different parts of JS code
36 218 my $jsfunc = '';
37 218 my $jsfield = tovar($name);
38 218 my $close_brace = '';
39 218 my $in = indent(my $idt = 1); # indent
40  
41 218 my $alertstr = escapejs($self->jsmessage); # handle embedded '
42 218 $alertstr .= '\n';
43  
44 218 ▶ if ($self->growable) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->growable) { } 2 216 218
45 # special handling for growable, have to dynamically
46 # find out how many have been created
47 2 $jsfunc .= <<EOJS;
48 // $name: growable text or file box
49 var $jsfield = null;
50 var entered_$jsfield = 0;
51 var i = 0;
52 while (1) {
53 var growel = document.getElementById('$jsfield'+'_'+i);
54 if (growel == null) break; // last element
55 $jsfield = growel.value;
56 entered_$jsfield++;
57 i++;
58 EOJS
59  
60 2 $close_brace = <<EOJS;
61  
62 } // while $name
63 EOJS
64  
65 # required?
66 2 ▶ $close_brace .= <<EOJS if $self->required;
Branch: 1 branch
BranchTrueFalseTotal
if $self->required 0 2 2
67 if (! entered_$jsfield) {
68 alertstr += '$alertstr';
69 invalid++;
70 }
71 EOJS
72 # indent the very last if/else tests so they're in the while loop
73 2 $in = indent($idt += 1);
74  
75 } else {
76  
77 # get value from text or other straight input
78 # at least this part makes some sense
79 216 $jsfunc .= <<EOJS;
80 // $name: standard text, hidden, password, or textarea box
81 var $jsfield = form.elements['$name'].value;
82 EOJS
83  
84 }
85  
86 218 return $self->jsfield($jsfunc, $close_brace, $in);
87 }
88  
89 6 *render = \&tag;
90 sub tag {
91 240 ▶ local $^W = 0; # -w sucks
Subroutine tag: called CC=26 SCAR=39.5
Pod documented
92 240 my $self = shift;
93 240 my $attr = $self->attr;
94  
95 240 my $jspre = $self->{_form}->jsprefix;
96  
97 240 my $tag = '';
98 240 my @value = $self->tag_value; # sticky is different in <tag>
99 240 my @opt = $self->options;
100 240 debug 2, "my(@opt) = \$field->options";
101  
102 # Add in our "Other:" option if applicable
103 240 ▶ push @opt, [$self->othername, $self->{_form}{messages}->form_other_default]
Branch: 1 branch
BranchTrueFalseTotal
if $self->other 0 240 240
104 if $self->other;
105  
106 240 debug 2, "$self->{name}: generating $attr->{type} input type";
107  
108 # We iterate over each value - this is the only reliable
109 # way to handle multiple form values of the same name
110 # (i.e., multiple <input> or <hidden> fields)
111 240 ▶ @value = (undef) unless @value; # this creates a single-element array
Branch: 1 branch
BranchTrueFalseTotal
unless @value 157 83 240
112  
113 # growable handling
114 240 my $count = 0; # for tracking the size of growable fields
115 240 my $limit; # for providing (optional) limits to growable fields
116 my $at_limit; # have we reached the limit of a growable field?
117 240 ▶ if ($self->growable && $self->growable ne 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->growable and $self->growable ne 1) 0 240 240
Condition66%2/3
$self->growable and $self->growable ne 1
!ll&&!rl&&r
23640
MC/DC0%0/2
$self->growable and $self->growable ne 1
$self->growable $self->growable ne 1
Truth table66%2/3
A and B
A= $self->growableB= $self->growable ne 1
ABresult
0X0
100
111
118 0 $limit = $self->growable;
119 }
120  
121 240 for my $value (@value) {
122 242 ▶ if ($limit && $count == $limit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($limit and $count == $limit) 0 242 242
Condition33%1/3
$limit and $count == $limit
!ll&&!rl&&r
24200
MC/DC0%0/2
$limit and $count == $limit
$limit $count == $limit
Truth table33%1/3
A and B
A= $limitB= $count == $limit
ABresult
0X0
100
111
123 0 belch "Number of supplied values (" . @value . ")"
124 . " for '$attr->{name}' exceeds growable limit $limit - discarding excess";
125 0 $at_limit = 1;
126 0 last;
127 }
128
129 # setup the value
130 242 $attr->{value} = $value; # override
131 242 ▶ delete $attr->{value} unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 157 85 242
132  
133 242 ▶ if ($self->growable && $self->javascript) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->growable and $self->javascript) 2 240 242
Condition100%3/3
$self->growable and $self->javascript
!ll&&!rl&&r
23822
MC/DC100%2/2
$self->growable and $self->javascript
$self->growable $self->javascript
Truth table100%3/3
A and B
A= $self->growableB= $self->javascript
ABresult
0X0
100
111
134 # the inputs in growable fields need a unique id for fb_grow()
135 2 $attr->{id} = tovar("$attr->{name}_$count");
136 2 $count++;
137 }
138  
139 # render the tag
140 242 $tag .= htmltag('input', $attr);
141  
142 #
143 # If we have options, lookup the label instead of the true value.
144 # This code is reached if a field is marked 'static', since
145 # that is still rendered from ::text (here) but as a 'hidden'
146 # field. These options would be leftover from a former select
147 # or radio group that is now shown on a confirm() screen. Got it?
148 #
149 242 for (@opt) {
150 0 my($o,$n) = optval($_);
151 0 ▶ if ($o eq $value) {
Branch: 1 branch
BranchTrueFalseTotal
if ($o eq $value) 0 0 0
152 0 ▶ $n ||= $attr->{labels}{$o} || ($self->nameopts ? toname($o) : $o);
Branch: 1 branch
BranchTrueFalseTotal
$self->nameopts ? : 0 0 0
Condition0%0/6
$n ||= $attr->{"labels"}{$o} || ($self->nameopts ? &toname($o) : $o)
l!l&&r!l&&!r
000
$attr->{'labels'}{$o} || ($self->nameopts ? &toname($o) : $o)
l!l&&r!l&&!r
000
MC/DC0%0/3
$n ||= $attr->{"labels"}{$o} || ($self->nameopts ? &toname($o) : $o)
$n $attr->{'labels'}{$o} ($self->nameopts ? &toname($o) : $o)
Truth table0%0/4
A ||= B || C
A= $nB= $attr->{'labels'}{$o}C= ($self->nameopts ? &toname($o) : $o)
ABCresult
1XX1
01X1
0011
0000
153 0 $value = $n;
154 0 last;
155 }
156 }
157  
158 242 ▶ if ($self->growable && $self->javascript) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->growable and $self->javascript) { } 2 240 242
Condition100%3/3
$self->growable and $self->javascript
!ll&&!rl&&r
23822
MC/DC100%2/2
$self->growable and $self->javascript
$self->growable $self->javascript
Truth table100%3/3
A and B
A= $self->growableB= $self->javascript
ABresult
0X0
100
111
159 # put linebreaks between the input tags in growable fields
160 # this puts the "Additonal [label]" button on the same line
161 # as the last input tag
162 2 ▶ $tag .= '<br />' unless $count == @value;
Branch: 1 branch
BranchTrueFalseTotal
unless $count == @value 0 2 2
163 } else {
164 240 ▶ $tag .= '<br />' if $self->linebreaks;
Branch: 1 branch
BranchTrueFalseTotal
if $self->linebreaks 0 240 240
165 }
166 }
167 # check to see if we just hit the limit
168 240 ▶ $at_limit = 1 if $limit && $count == $limit;
Branch: 1 branch
BranchTrueFalseTotal
if $limit and $count == $limit 0 240 240
Condition33%1/3
$limit and $count == $limit
!ll&&!rl&&r
24000
MC/DC0%0/2
$limit and $count == $limit
$limit $count == $limit
Truth table33%1/3
A and B
A= $limitB= $count == $limit
ABresult
0X0
100
111
169  
170 # add the "Additional [label]" button
171 240 ▶ if ($self->growable && $self->javascript) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->growable and $self->javascript) 2 238 240
Condition100%3/3
$self->growable and $self->javascript
!ll&&!rl&&r
23622
MC/DC100%2/2
$self->growable and $self->javascript
$self->growable $self->javascript
Truth table100%3/3
A and B
A= $self->growableB= $self->javascript
ABresult
0X0
100
111
172 $tag .= ' ' . htmltag('input',
173 id => $self->growname,
174 type => 'button',
175 onclick => "${jspre}grow('$attr->{name}')",
176 2 ▶ value => sprintf($self->{_form}{messages}->form_grow_default, $self->label),
Branch: 1 branch
BranchTrueFalseTotal
$at_limit ? : 0 2 2
177 ( $at_limit ? ( disabled => 'disabled') : () ),
178 );
179 }
180  
181 # add an additional tag for our _other field
182 240 ▶ $tag .= ' ' . $self->othertag if $self->other;
Branch: 1 branch
BranchTrueFalseTotal
if $self->other 0 240 240
183  
184 240 debug 2, "$self->{name}: generated tag = $tag";
185 240 return $tag; # always return scalar tag
186 }
187  
188 6 1;
189  
190 __END__
« blib/lib/CGI/FormBuilder/Template/Text.pm ↑ Summary lib/CGI/FormBuilder/Field/select.pm »