lib/CGI/FormBuilder/Template/Builtin.pm

filter: Statementstmt 94.9% 131 / 138 Branchbran 74.0% 74 / 100 Conditioncond 47.6% 10 / 21 MC/DCmcdc 21.4% 3 / 14 Subroutinesub 100% 8 / 8 Podpod 0.0% 0 / 3 totaltotal 79.5% 226 / 284 SCAR 43.5
CC
60
Cov
83%
prepare
76.8
new
3.3
BEGIN
1.0
CRAP 77.7
« lib/CGI/FormBuilder/Source/File.pm ↑ Summary blib/lib/CGI/FormBuilder/Field/static.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 package CGI::FormBuilder::Template::Builtin;
8  
9 =head1 NAME
10  
11 CGI::FormBuilder::Template::Builtin - Builtin HTML rendering
12  
13 =head1 SYNOPSIS
14  
15 my $form = CGI::FormBuilder->new;
16 $form->render;
17  
18 =cut
19  
20 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 4 ▶no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 4 ▶use CGI::FormBuilder::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27  
28 4 our $VERSION = '3.10';
29  
30 sub new {
31 58 ▶ my $self = shift;
Subroutine new: called CC=3 SCAR=12.0
Pod undocumented
32 58 ▶ my $class = ref($self) || $self;
Condition33%1/3
ref $self || $self
l!l&&r!l&&!r
0580
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
33 58 ▶ my %opt = @_ if @_ > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 1 0 58 58
34 58 return bless \%opt, $class;
35 }
36  
37 sub prepare {
38 58 ▶ my $self = shift;
Subroutine prepare: called CC=58 SCAR=43.4
Pod undocumented
39 58 my $form = shift;
40  
41 58 my @html = (); # joined with newline
42  
43 # Opening CGI/title gunk
44 58 my $hd = $form->header;
45 58 ▶ if (defined $hd) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $hd) 14 44 58
46 14 push @html, $form->dtd, htmltag('head');
47 14 ▶ push @html, htmltag('title') . $form->title . htmltag('/title')
Branch: 1 branch
BranchTrueFalseTotal
if $form->title 14 0 14
48 if $form->title;
49  
50 # stylesheet path if specified
51 14 ▶ if ($form->{stylesheet} && $form->{stylesheet} ne 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($form->{"stylesheet"} and $form->{"stylesheet"} ne 1) 4 10 14
Condition100%3/3
$form->{"stylesheet"} and $form->{"stylesheet"} ne 1
!ll&&!rl&&r
734
MC/DC100%2/2
$form->{"stylesheet"} and $form->{"stylesheet"} ne 1
$form->{"stylesheet"} $form->{"stylesheet"} ne 1
Truth table100%3/3
A and B
A= $form->{"stylesheet"}B= $form->{"stylesheet"} ne 1
ABresult
0X0
100
111
52 # user-specified path
53 push @html, htmltag('link', { rel => 'stylesheet',
54 type => 'text/css',
55 4 href => $form->{stylesheet} });
56 }
57 }
58  
59 # JavaScript validate/head functions
60 58 my $js = $form->script;
61 58 ▶ push @html, $js if $js;
Branch: 1 branch
BranchTrueFalseTotal
if $js 14 44 58
62  
63 # Opening HTML if so requested
64 58 my $font = $form->font;
65 58 ▶ my $fcls = $font ? htmltag('/font') : '';
Branch: 1 branch
BranchTrueFalseTotal
$font ? : 0 58 58
66 58 ▶ if (defined $hd) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $hd) 14 44 58
67 14 push @html, htmltag('/head'), $form->body;
68 14 ▶ push @html, $font if $font;
Branch: 1 branch
BranchTrueFalseTotal
if $font 0 14 14
69 14 ▶ push @html, htmltag('h3') . $form->title . htmltag('/h3')
Branch: 1 branch
BranchTrueFalseTotal
if $form->title 14 0 14
70 if $form->title;
71 }
72  
73 # Include warning if noscript
74 58 ▶ push @html, $form->noscript if $js;
Branch: 1 branch
BranchTrueFalseTotal
if $js 14 44 58
75  
76 # Put id's around state tags if so required
77 58 my($stid, $keid);
78 58 ▶ if (my $fn = $form->name) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $fn = $form->name) 8 50 58
79 8 $stid = tovar("${fn}$form->{statename}");
80 8 $keid = tovar("${fn}$form->{extraname}");
81 }
82  
83 # Begin form
84 58 my $txt = $form->text;
85 58 ▶ push @html, $txt if $txt;
Branch: 1 branch
BranchTrueFalseTotal
if $txt 14 44 58
86 58 push @html, $form->start;
87  
88 # Put id's around state tags if they exist
89 58 ▶ if (my $st = $form->statetags) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $st = $form->statetags) 58 0 58
90 58 push @html,
91 $form->div(id => $form->idname($form->statename),
92 class => $form->class($form->statename)) .
93 $st . htmltag('/div');
94 }
95 58 ▶ if (my $ke = $form->keepextras) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ke = $form->keepextras) 4 54 58
96 4 push @html,
97 $form->div(id => $form->idname($form->extraname),
98 class => $form->class($form->extraname)) .
99 $ke . htmltag('/div');
100 }
101  
102 # Render hidden fields first
103 58 my @unhidden;
104 58 for my $field ($form->fieldlist) {
105 169 ▶ push(@unhidden, $field), next if $field->type ne 'hidden';
Branch: 1 branch
BranchTrueFalseTotal
if $field->type ne "hidden" 167 2 169
106 2 push @html, $field->tag; # no label/etc for hidden fields
107 }
108  
109 # Support fieldset => 'name' to organize by fieldset on the fly
110 58 my $legend = $form->fieldsets;
111  
112 # Get table stuff and reused calls
113 58 my $table = $form->table(id => $form->idname($form->bodyname), class => $form->class);
114 58 my $tabn = 1;
115 58 ▶ push @html, $table if $table;
Branch: 1 branch
BranchTrueFalseTotal
if $table 56 2 58
116  
117 # Render regular fields in table
118 58 my $lastset;
119 58 for my $field (@unhidden) {
120 167 ▶ if (my $set = $field->fieldset) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $set = $field->fieldset) { } 9 158 167
elsif ($lastset) { } 1 157 158
121 # hooks (hack?) for fieldsets
122 9 ▶ if ($set ne $lastset) {
Branch: 1 branch
BranchTrueFalseTotal
if ($set ne $lastset) 4 5 9
123 # close any open tables/fieldsets
124 4 ▶ if ($lastset) {
Branch: 2 branches
BranchTrueFalseTotal
if ($lastset) { } 3 1 4
elsif ($table) { } 1 0 1
125 3 ▶ push @html, htmltag('/table') if $table;
Branch: 1 branch
BranchTrueFalseTotal
if $table 3 0 3
126 3 push @html, htmltag('/fieldset');
127 3 push @html, htmltag('/div');
128 } elsif ($table) {
129 # Catch in case we have an empty table - ie the previous
130 # element is just <table>. This workaround is needed
131 # in case the user wants to mix fields with/without
132 # fieldsets in the same form
133 1 ▶ if ($html[-1] =~ /^<table\b/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($html[-1] =~ /^<table\b/) { } 1 0 1
134 1 pop @html;
135 } else {
136 # close non-fieldset table
137 0 push @html, htmltag('/table');
138 }
139 }
140  
141 # Wrap fieldset in a <div> to allow jquery #tabs
142 4 push @html, $form->div(id => $form->idname($form->tabname.$tabn++),
143 class => $form->class($form->tabname));
144  
145 4 (my $sn = lc $set) =~ s/\W+/_/g;
146 4 push @html, htmltag('fieldset', id => $form->idname("_$sn"),
147 class => $form->class('_set'));
148 push @html, htmltag('legend') . ($legend->{$set}||$set) . htmltag('/legend')
149 4 ▶ if defined $legend->{$set};
Branch: 1 branch
BranchTrueFalseTotal
if defined $legend->{$set} 4 0 4
Condition33%1/3
$legend->{$set} || $set
l!l&&r!l&&!r
400
MC/DC0%0/2
$legend->{$set} || $set
$legend->{$set} $set
Truth table33%1/3
A || B
A= $legend->{$set}B= $set
ABresult
1X1
011
000
150  
151 # Wrap fields in a table
152 4 ▶ push @html, $form->table if $table;
Branch: 1 branch
BranchTrueFalseTotal
if $table 4 0 4
153  
154 4 $lastset = $set;
155 }
156 } elsif ($lastset) {
157 # ended <fieldset> defs before form has ended
158 # remaining fields are not in a fieldset
159 1 ▶ push @html, htmltag('/table') if $table;
Branch: 1 branch
BranchTrueFalseTotal
if $table 1 0 1
160 1 push @html, htmltag('/fieldset');
161 1 push @html, htmltag('/div');
162 1 ▶ push @html, $table if $table;
Branch: 1 branch
BranchTrueFalseTotal
if $table 1 0 1
163 1 undef $lastset; # avoid dup </fieldset> below
164 }
165  
166 167 debug 2, "render: attacking normal field '$field'";
167 167 ▶ next if $field->static > 1 && ! $field->tag_value; # skip missing static vals
Branch: 1 branch
BranchTrueFalseTotal
if $field->static > 1 and not $field->tag_value 0 167 167
Condition33%1/3
$field->static > 1 and not $field->tag_value
!ll&&!rl&&r
16700
MC/DC0%0/2
$field->static > 1 and not $field->tag_value
$field->static > 1 not $field->tag_value
Truth table33%1/3
A and not(B)
A= $field->static > 1B= not $field->tag_value
ABresult
0X0
100
111
168  
169 167 ▶ if ($table) {
Branch: 1 branch
BranchTrueFalseTotal
if ($table) { } 165 2 167
170 165 push @html, $form->tr(id => $form->idname("_$field", $form->rowname));
171  
172 165 my $cl = $form->class($form->labelname);
173 165 my $row = ' ' . $form->td(id => $form->idname("_$field", $form->labelname),
174 class => $cl) . $font;
175 165 ▶ if ($field->invalid) {
Branch: 2 branches
BranchTrueFalseTotal
if ($field->invalid) { } 2 163 165
elsif ($field->required and not $field->static) { } 34 129 163
176 2 $row .= $form->invalid_tag($field->label);
177 ▶ } elsif ($field->required && ! $field->static) {
Condition66%2/3
$field->required and not $field->static
!ll&&!rl&&r
129034
MC/DC50%1/2
$field->required and not $field->static
$field->required not $field->static
Truth table66%2/3
A and not(B)
A= $field->requiredB= not $field->static
ABresult
0X0
100
111
178 34 $row .= $form->required_tag($field->label);
179 } else {
180 129 $row .= $field->label;
181 }
182 165 $row .= $fcls . htmltag('/td');
183 165 push @html, $row;
184  
185 # tag plus optional errors and/or comments
186 165 $row = '';
187 165 ▶ if ($field->invalid) {
Branch: 1 branch
BranchTrueFalseTotal
if ($field->invalid) 2 163 165
188 2 $row .= ' ' . $field->message;
189 }
190 165 ▶ if ($field->comment) {
Branch: 1 branch
BranchTrueFalseTotal
if ($field->comment) 11 154 165
191 11 ▶ $row .= ' ' . $field->comment unless $field->static;
Branch: 1 branch
BranchTrueFalseTotal
unless $field->static 11 0 11
192 }
193 165 $row = $field->tag . $row;
194 165 $cl = $form->class($form->{fieldname});
195 165 push @html, (' ' . $form->td(id => $form->idname("_$field", $form->fieldname),
196 class => $cl) . $font
197 . $row . $fcls . htmltag('/td'));
198 165 push @html, htmltag('/tr');
199 } else {
200 # no table
201 2 my $row = $font;
202 2 ▶ if ($field->invalid) {
Branch: 2 branches
BranchTrueFalseTotal
if ($field->invalid) { } 0 2 2
elsif ($field->required and not $field->static) { } 0 2 2
203 0 $row .= $form->invalid_tag($field->label);
204 ▶ } elsif ($field->required && ! $field->static) {
Condition33%1/3
$field->required and not $field->static
!ll&&!rl&&r
200
MC/DC0%0/2
$field->required and not $field->static
$field->required not $field->static
Truth table33%1/3
A and not(B)
A= $field->requiredB= not $field->static
ABresult
0X0
100
111
205 0 $row .= $form->required_tag($field->label);
206 } else {
207 2 $row .= $field->label;
208 }
209 2 $row .= $fcls;
210 2 push @html, $row;
211 2 push @html, $field->tag;
212 2 ▶ push @html, $field->message if $field->invalid;
Branch: 1 branch
BranchTrueFalseTotal
if $field->invalid 0 2 2
213 2 ▶ push @html, $field->comment if $field->comment;
Branch: 1 branch
BranchTrueFalseTotal
if $field->comment 0 2 2
214 2 ▶ push @html, '<br />' if $form->linebreaks;
Branch: 1 branch
BranchTrueFalseTotal
if $form->linebreaks 0 2 2
215 }
216 }
217  
218 # Close fieldset before [Submit] if using fieldsets
219 58 ▶ if ($lastset) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lastset) 0 58 58
220 0 ▶ push @html, htmltag('/table') if $table;
Branch: 1 branch
BranchTrueFalseTotal
if $table 0 0 0
221 0 push @html, htmltag('/fieldset');
222 0 push @html, htmltag('/div');
223 0 undef $table; # avoid dup </table> below
224 }
225  
226 # Throw buttons in a colspan
227 58 my $buttons = $form->reset . $form->submit;
228 58 ▶ if ($buttons) {
Branch: 1 branch
BranchTrueFalseTotal
if ($buttons) 55 3 58
229 55 my $row = '';
230 55 ▶ if ($table) {
Branch: 1 branch
BranchTrueFalseTotal
if ($table) { } 53 2 55
231 53 my $c = $form->class($form->{submitname});
232 53 ▶ my %a = $c ? () : (align => 'center');
Branch: 1 branch
BranchTrueFalseTotal
$c ? : 9 44 53
233 53 $row .= $form->tr(id => $form->idname($form->submitname, $form->rowname)) . "\n "
234 . $form->td(id => $form->idname($form->submitname, $form->fieldname),
235 class => $c, colspan => 2, %a) . $font;
236 } else {
237 # wrap in a <div> for fieldsets
238 2 $row .= $form->div(id => $form->idname('_controls'),
239 class => $form->class('_controls'));
240 }
241 55 $row .= $buttons;
242 55 ▶ if ($table) {
Branch: 1 branch
BranchTrueFalseTotal
if ($table) { } 53 2 55
243 53 ▶ $row .= htmltag('/font') if $font;
Branch: 1 branch
BranchTrueFalseTotal
if $font 0 53 53
244 53 ▶ $row .= htmltag('/td') . "\n" . htmltag('/tr') if $table;
Branch: 1 branch
BranchTrueFalseTotal
if $table 53 0 53
245 } else {
246 2 $row .= htmltag('/div');
247 }
248 55 push @html, $row;
249 }
250  
251 # Properly nest closing tags
252 58 ▶ push @html, htmltag('/table') if $table;
Branch: 1 branch
BranchTrueFalseTotal
if $table 56 2 58
253 58 push @html, htmltag('/form'); # $form->end
254 58 ▶ push @html, htmltag('/font') if $font && defined $hd;
Branch: 1 branch
BranchTrueFalseTotal
if $font and defined $hd 0 58 58
Condition33%1/3
$font and defined $hd
!ll&&!rl&&r
5800
MC/DC0%0/2
$font and defined $hd
$font defined $hd
Truth table33%1/3
A and B
A= $fontB= defined $hd
ABresult
0X0
100
111
255 58 ▶ push @html, htmltag('/body'),htmltag('/html') if defined $hd;
Branch: 1 branch
BranchTrueFalseTotal
if defined $hd 14 44 58
256  
257 # Always return scalar since print() is a list function
258 58 return $self->{output} = join("\n", @html) . "\n"
259 }
260  
261 sub render {
262 58 ▶ my $self = shift;
Subroutine render: called CC=1 SCAR=0.0
Pod undocumented
263 58 return $self->{output};
264 }
265  
266 4 1;
267 __END__
« lib/CGI/FormBuilder/Source/File.pm ↑ Summary blib/lib/CGI/FormBuilder/Field/static.pm »