FormHandler/Widget/Wrapper/Bootstrap3.pm

filter: Statementstmt 74.7% 80 / 107 Branchbran 65.5% 38 / 58 Conditioncond 55.5% 20 / 36 MC/DCmcdc 26.3% 5 / 19 Subroutinesub 88.2% 15 / 17 Podpod 0.0% 0 / 10 totaltotal 63.9% 158 / 247 CC 45 SCAR 27.6
CC
45
Cov
69%
do_prepend_append
72.0
wrap_field
13.4
add_standard_element_classes
9.3
CRAP 15.8
« FormHandler/InitResult.pm ↑ Summary FormHandler/Widget/Field/HorizCheckboxGroup.pm »
1 package HTML::FormHandler::Widget::Wrapper::Bootstrap3;
2 # ABSTRACT: Twitter Bootstrap 3.0 field wrapper
3 6 $HTML::FormHandler::Widget::Wrapper::Bootstrap3::VERSION = '0.410001';
4 6 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 6 ▶use HTML::FormHandler::Render::Util ('process_attrs');
Subroutine BEGIN: called CC=1 SCAR=0.0
7 6 ▶use List::Util 1.33 ('any');
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 6 with 'HTML::FormHandler::Widget::Wrapper::Base';
10  
11  
12 3 ▶sub is_b3 {1}
Subroutine is_b3: called CC=1 SCAR=0.0
Pod undocumented
13  
14 sub build_wrapper_tags {
15 {
16 20 ▶ radio_element_wrapper => 1,
Subroutine build_wrapper_tags: called CC=1 SCAR=0.0
Pod undocumented
17 checkbox_element_wrapper => 1,
18 }
19 }
20  
21 sub wrap_field {
22 23 ▶ my ( $self, $result, $rendered_widget ) = @_;
Subroutine wrap_field: called CC=13 SCAR=26.0
Pod undocumented
23  
24 23 my $output;
25  
26 # create attribute string for wrapper
27 23 ▶ if ( $self->do_wrapper ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->do_wrapper) 20 3 23
28 20 my $attr = $self->wrapper_attributes($result);
29 # no 'control-group' class for Hidden fields, 'form-actions' for submit/reset
30 20 my $div_class = 'form-group';
31 20 unshift @{$attr->{class}}, $div_class;
32 20 my $attr_str = process_attrs( $attr );
33 # wrapper is always a div
34 20 $output .= $self->get_tag('before_wrapper');
35 20 $output .= qq{\n<div$attr_str>};
36 }
37 # render the label
38 23 ▶ $output .= "\n" . $self->do_render_label($result, undef, ['control-label'] )
Branch: 1 branch
BranchTrueFalseTotal
if $self->do_label 16 7 23
39 if $self->do_label;
40 23 $output .= $self->get_tag('before_element');
41  
42 # the controls div; used to have 'controls' class. Now it comes from
43 # the 'element_wrapper_class'. Used for column layout.
44 23 my $ew_attr = $self->element_wrapper_attributes($result);
45 23 my $element_wrapper_attrs = process_attrs( $ew_attr );
46 23 ▶ $output .= qq{\n<div$element_wrapper_attrs>}
Branch: 1 branch
BranchTrueFalseTotal
if $self->do_wrapper 20 3 23
47 unless !$self->do_wrapper;
48  
49 # yet another tag
50 23 $output .= $self->get_tag('before_element_inside_div');
51 # handle input-prepend and input-append
52 23 ▶ if( $self->get_tag('input_prepend') || $self->get_tag('input_append') ||
Branch: 2 branches
BranchTrueFalseTotal
if ($self->get_tag('input_prepend') or $self->get_tag('input_append') or $self->get_tag('input_append_button')) { } 0 23 23
elsif (lc $self->widget eq 'checkbox') { } 7 16 23
Condition33%2/6
$self->get_tag("input_prepend") or $self->get_tag("input_append") or $self->get_tag("input_append_button")
l!l&&r!l&&!r
0023
$self->get_tag("input_prepend") or $self->get_tag("input_append")
l!l&&r!l&&!r
0023
MC/DC0%0/3
$self->get_tag("input_prepend") or $self->get_tag("input_append") or $self->get_tag("input_append_button")
$self->get_tag("input_prepend") $self->get_tag("input_append") $self->get_tag("input_append_button")
Truth table25%1/4
A or B or C
A= $self->get_tag("input_prepend")B= $self->get_tag("input_append")C= $self->get_tag("input_append_button")
ABCresult
1XX1
01X1
0011
0000
53 $self->get_tag('input_append_button') ) {
54 0 $rendered_widget = $self->do_prepend_append($rendered_widget);
55 }
56 elsif( lc $self->widget eq 'checkbox' ) {
57 7 $rendered_widget = $self->wrap_checkbox($result, $rendered_widget, 'b3_label_left')
58 }
59  
60 23 $output .= "\n$rendered_widget";
61 # various 'help-inline' bits: errors, warnings
62 23 ▶ unless( $self->get_tag('no_errors') ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->get_tag("no_errors")) 23 0 23
63 $output .= qq{\n<span class="help-block">$_</span>}
64 23 for $result->all_errors;
65 23 $output .= qq{\n<span class="help-block">$_</span>} for $result->all_warnings;
66 }
67 # extra after element stuff
68 23 $output .= $self->get_tag('after_element');
69 # close element_wrapper 'control' div
70 23 ▶ $output .= '</div>' unless !$self->do_wrapper;
Branch: 1 branch
BranchTrueFalseTotal
if $self->do_wrapper 20 3 23
71 # close wrapper
72 23 ▶ if ( $self->do_wrapper ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->do_wrapper) 20 3 23
73 20 $output .= "\n</div>";
74 20 $output .= $self->get_tag('after_wrapper');
75 }
76 23 return "$output";
77 }
78  
79 # don't render label for checkboxes
80 sub do_render_label {
81 16 ▶ my ( $self ) = @_;
Subroutine do_render_label: called CC=2 SCAR=6.9
Pod undocumented
82  
83 16 ▶ return '' if $self->type_attr eq 'checkbox';
Branch: 1 branch
BranchTrueFalseTotal
if $self->type_attr eq "checkbox" 3 13 16
84 13 HTML::FormHandler::Widget::Wrapper::Base::do_render_label(@_);
85 }
86  
87 sub add_standard_element_classes {
88 17 ▶ my ( $self, $result, $class ) = @_;
Subroutine add_standard_element_classes: called CC=8 SCAR=22.3
Pod undocumented
89 17 ▶ push @$class, 'has-error' if $result->has_errors;
Branch: 1 branch
BranchTrueFalseTotal
if $result->has_errors 2 15 17
90 17 ▶ push @$class, 'has-warning' if $result->has_warnings;
Branch: 1 branch
BranchTrueFalseTotal
if $result->has_warnings 0 17 17
91 17 ▶ push @$class, 'disabled' if $self->disabled;
Branch: 1 branch
BranchTrueFalseTotal
if $self->disabled 0 17 17
92 17 ▶ push @$class, 'form-control'
Branch: 1 branch
BranchTrueFalseTotal
if $self->html_element eq "select" or $self->html_element eq "textarea" or $self->type_attr eq "text" or $self->type_attr eq "password" 7 10 17
Condition55%5/9
$self->html_element eq "select" or $self->html_element eq "textarea" or $self->type_attr eq "text" or $self->type_attr eq "password"
l!l&&r!l&&!r
7010
$self->html_element eq "select" or $self->html_element eq "textarea" or $self->type_attr eq "text"
l!l&&r!l&&!r
0710
$self->html_element eq "select" or $self->html_element eq "textarea"
l!l&&r!l&&!r
0017
MC/DC25%1/4
$self->html_element eq "select" or $self->html_element eq "textarea" or $self->type_attr eq "text" or $self->type_attr eq "password"
$self->html_element eq "select" $self->html_element eq "textarea" $self->type_attr eq "text" $self->type_attr eq "password"
Truth table40%2/5
A or B or C or D
A= $self->html_element eq "select"B= $self->html_element eq "textarea"C= $self->type_attr eq "text"D= $self->type_attr eq "password"
ABCDresult
1XXX1
01XX1
001X1
00011
00000
93 if $self->html_element eq 'select' || $self->html_element eq 'textarea' ||
94 $self->type_attr eq 'text' || $self->type_attr eq 'password';
95 }
96  
97 sub add_standard_wrapper_classes {
98 20 ▶ my ( $self, $result, $class ) = @_;
Subroutine add_standard_wrapper_classes: called CC=4 SCAR=14.2
Pod undocumented
99 20 ▶ push @$class, 'has-error' if ( $result->has_error_results || $result->has_errors );
Branch: 1 branch
BranchTrueFalseTotal
if $result->has_error_results or $result->has_errors 2 18 20
Condition66%2/3
$result->has_error_results or $result->has_errors
l!l&&r!l&&!r
0218
MC/DC50%1/2
$result->has_error_results or $result->has_errors
$result->has_error_results $result->has_errors
Truth table66%2/3
A or B
A= $result->has_error_resultsB= $result->has_errors
ABresult
1X1
011
000
100 20 ▶ push @$class, 'has-warning' if $result->has_warnings;
Branch: 1 branch
BranchTrueFalseTotal
if $result->has_warnings 0 20 20
101 # TODO: has-success?
102 }
103  
104 sub add_standard_label_classes {
105 13 ▶ my ( $self, $result, $class ) = @_;
Subroutine add_standard_label_classes: called CC=4 SCAR=14.8
Pod undocumented
106 13 ▶ if ( my $classes = $self->form->get_tag('layout_classes') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $classes = $self->form->get_tag("layout_classes")) 9 4 13
107 9 my $label_class = $classes->{label_class};
108 9 ▶ if ( $label_class && not any { $_ =~ /^col\-/ } @$class ) {
Subroutine __ANON__: not called CC=1 SCAR=1.7
Branch: 1 branch
BranchTrueFalseTotal
if ($label_class and not &any(sub { $_ =~ /^col\-/; } , @$class)) 9 0 9
Condition33%1/3
$label_class and not &any(sub {
!ll&&!rl&&r
009
MC/DC0%0/2
$label_class and not &any(sub { $_ =~ /^col\-/; } , @$class)
$label_class not &any(sub { $_ =~ /^col\-/; } , @$class)
Truth table33%1/3
A and not(B)
A= $label_classB= not &any(sub { $_ =~ /^col\-/; } , @$class)
ABresult
0X0
100
111
109 9 push @$class, @{$classes->{label_class}};
110 }
111 }
112 }
113  
114 sub add_standard_element_wrapper_classes {
115 23 ▶ my ( $self, $result, $class ) = @_;
Subroutine add_standard_element_wrapper_classes: called CC=8 SCAR=20.9
Pod undocumented
116 23 ▶ if ( my $classes = $self->form->get_tag('layout_classes') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $classes = $self->form->get_tag("layout_classes")) 17 6 23
117 17 ▶ if ( exists $classes->{element_wrapper_class} &&
Branch: 1 branch
BranchTrueFalseTotal
if (exists $classes->{"element_wrapper_class"} and not &any(sub { $_ =~ /^col\-/; } , @$class)) 15 2 17
Condition66%2/3
exists $classes->{"element_wrapper_class"} and not &any(sub {
!ll&&!rl&&r
0215
MC/DC50%1/2
exists $classes->{"element_wrapper_class"} and not &any(sub { $_ =~ /^col\-/; } , @$class)
exists $classes->{"element_wrapper_class"} not &any(sub { $_ =~ /^col\-/; } , @$class)
Truth table66%2/3
A and not(B)
A= exists $classes->{"element_wrapper_class"}B= not &any(sub { $_ =~ /^col\-/; } , @$class)
ABresult
0X0
100
111
118 2 ▶ not any { $_ =~ /^col\-/ } @$class ) {
Subroutine __ANON__: called CC=1 SCAR=0.0
119 15 push @$class, @{$classes->{element_wrapper_class}};
120 }
121 17 ▶ if ( exists $classes->{no_label_element_wrapper_class} &&
Branch: 1 branch
BranchTrueFalseTotal
if (exists $classes->{"no_label_element_wrapper_class"} and !$self->do_label || $self->type_attr eq "checkbox" and not &any(sub { $_ =~ /^col\-.*offset/; } , @$class)) 8 9 17
Condition77%7/9
exists $classes->{"no_label_element_wrapper_class"} and !$self->do_label || $self->type_attr eq "checkbox" and not &any(sub {
!ll&&!rl&&r
908
exists $classes->{"no_label_element_wrapper_class"} and !$self->do_label || $self->type_attr eq "checkbox"
!ll&&!rl&&r
098
not $self->do_label or $self->type_attr eq "checkbox"
l!l&&r!l&&!r
629
MC/DC50%2/4
exists $classes->{"no_label_element_wrapper_class"} and !$self->do_label || $self->type_attr eq "checkbox" and not &any(sub { $_ =~ /^col\-.*offset/; } , @$class)
exists $classes->{"no_label_element_wrapper_class"} not $self->do_label $self->type_attr eq "checkbox" not &any(sub { $_ =~ /^col\-.*offset/; } , @$class)
Truth table50%3/6
A and not(B) or C and not(D)
A= exists $classes->{"no_label_element_wrapper_class"}B= not $self->do_labelC= $self->type_attr eq "checkbox"D= not &any(sub { $_ =~ /^col\-.*offset/; } , @$class)
ABCDresult
0XXX0
100X0
11X00
10100
11X11
10111
122 ( ! $self->do_label || $self->type_attr eq 'checkbox' ) &&
123 8 ▶ not any { $_ =~ /^col\-.*offset/ } @$class ) {
Subroutine __ANON__: called CC=1 SCAR=0.0
124 8 push @$class, @{$classes->{no_label_element_wrapper_class}};
125 }
126 }
127 }
128  
129 sub wrap_checkbox {
130 7 ▶ my ( $self, $result, $rendered_widget ) = @_;
Subroutine wrap_checkbox: called CC=5 SCAR=16.7
Pod undocumented
131  
132 # use the regular label
133 7 ▶ my $label = $self->option_label || $self->label;
Condition33%1/3
$self->option_label || $self->label
l!l&&r!l&&!r
070
MC/DC0%0/2
$self->option_label || $self->label
$self->option_label $self->label
Truth table33%1/3
A || B
A= $self->option_labelB= $self->label
ABresult
1X1
011
000
134 7 ▶ $label = $self->get_tag('label_no_filter') ? $self->_localize($label) : $self->html_filter($self->_localize($label));
Branch: 1 branch
BranchTrueFalseTotal
$self->get_tag('label_no_filter') ? : 0 7 7
135 7 my $id = $self->id;
136 7 my $for = qq{ for="$id"};
137  
138 # return unwrapped checkbox with 'checkbox-inline'
139 7 ▶ return qq{<label class="checkbox-inline" $for>$rendered_widget\n$label\n</label>}
Branch: 1 branch
BranchTrueFalseTotal
if $self->get_tag("inline") 3 4 7
140 if( $self->get_tag('inline') );
141  
142 # return wrapped checkbox, either on left or right
143 4 ▶ return qq{<div class="checkbox"><label$for>\n$label\n$rendered_widget</label></div>}
Branch: 1 branch
BranchTrueFalseTotal
if $self->get_tag("label_left") 0 4 4
144 if( $self->get_tag('label_left') );
145 4 return qq{<div class="checkbox"><label$for>$rendered_widget\n$label\n</label></div>};
146 }
147  
148 sub do_prepend_append {
149 0 ▶ my ( $self, $rendered_widget ) = @_;
Subroutine do_prepend_append: not called CC=8 SCAR=42.8
Pod undocumented
150  
151 0 my @class;
152 0 ▶ if( my $ip_tag = $self->get_tag('input_prepend' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ip_tag = $self->get_tag("input_prepend")) 0 0 0
153 0 $rendered_widget = qq{<span class="input-group-addon">$ip_tag</span>$rendered_widget};
154 0 push @class, 'input-group';
155 }
156 0 ▶ if ( my $ia_tag = $self->get_tag('input_append' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ia_tag = $self->get_tag("input_append")) 0 0 0
157 0 $rendered_widget = qq{$rendered_widget<span class="input-group-addon">$ia_tag</span>};
158 0 push @class, 'input-group';
159 }
160 0 ▶ if ( my $iab_tag = $self->get_tag('input_append_button') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $iab_tag = $self->get_tag("input_append_button")) 0 0 0
161 0 my $iab_element_attr_tag = $self->get_tag('input_append_button_element_attr');
162  
163 0 my ($btn_class, $attr);
164 0 ▶ if (ref $iab_element_attr_tag eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $iab_element_attr_tag eq "HASH") 0 0 0
165 0 ▶ $btn_class = ref $iab_element_attr_tag->{class} eq 'ARRAY' ? shift @{$iab_element_attr_tag->{class}} : $iab_element_attr_tag->{class};
Branch: 1 branch
BranchTrueFalseTotal
ref $iab_element_attr_tag->{'class'} eq 'ARRAY' ? : 0 0 0
166 0 $attr = process_attrs( $iab_element_attr_tag );
167 }
168  
169 0 ▶ my @buttons = ref $iab_tag eq 'ARRAY' ? @$iab_tag : ($iab_tag);
Branch: 1 branch
BranchTrueFalseTotal
ref $iab_tag eq 'ARRAY' ? : 0 0 0
170 0 my $group = qq{<span class="input-group-btn">};
171 0 foreach my $btn ( @buttons ) {
172 0 $group .= qq{<button type="button" class="btn $btn_class"$attr>$btn</button>};
173 }
174 0 $group .= qq{</span>};
175 0 $rendered_widget = qq{$rendered_widget$group};
176 0 push @class, 'input-group';
177 }
178 0 my $attr = process_attrs( { class => \@class } );
179 0 $rendered_widget =
180 qq{<div$attr>
181 $rendered_widget
182 </div>};
183 0 return $rendered_widget;
184 }
185  
186 6 1;
187  
188 __END__
189  
190 =pod
191  
192 =encoding UTF-8
193  
194 =head1 NAME
195  
196 HTML::FormHandler::Widget::Wrapper::Bootstrap3 - Twitter Bootstrap 3.0 field wrapper
197  
198 =head1 VERSION
199  
200 version 0.410001
201  
202 =head1 SYNOPSIS
203  
204 Wrapper to implement Bootstrap 3.0 style form element rendering.
205  
206 =head1 DESCRIPTION
207  
208 Differences from the Bootstrap 2.0 wrapper:
209  
210 The wrapper class is 'form-group' instead of 'control-group'
211 The div wrapping the form element does not
212 have the 'controls' class. Used for sizing css classes.
213 Input prepend & append use different classes
214 The input elements have a 'form-control' class
215  
216 Tags supported:
217  
218 label_no_filter -- don't html filter the label
219 label_after -- useful for putting a colon, or other trailing formatting
220 before_element -- insert tag before input, outside element's control div
221 before_element_inside_div -- insert tag before input element, inside control div
222 input_prepend -- for Bootstrap 'input-prepend' class
223 input_append -- for Bootstrap 'input-append' class
224 input_append_button -- 'input-append' with button instead of span
225 no_errors -- don't append error to field rendering
226 after_element -- insert tag after input element
227  
228 =head1 AUTHOR
229  
230 FormHandler Contributors - see HTML::FormHandler
231  
232 =head1 COPYRIGHT AND LICENSE
233  
234 This software is copyright (c) 2017 by Gerda Shank.
235  
236 This is free software; you can redistribute it and/or modify it under
237 the same terms as the Perl 5 programming language system itself.
238  
239 =cut
« FormHandler/InitResult.pm ↑ Summary FormHandler/Widget/Field/HorizCheckboxGroup.pm »