lib/CGI/FormBuilder/Template/Div.pm

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