FormBuilder/Base.pm

filter: Statementstmt 100% 54 / 54 Branchbran 83.3% 15 / 18 Conditioncond 80.0% 8 / 10 MC/DCmcdc 66.6% 4 / 6 Subroutinesub 100% 12 / 12 Podpod n/a totaltotal 93.0% 93 / 100 CC 15 SCAR 19.7
CC
15
Cov
94%
_build_element_and_attributes
13.0
__ANON__
2.1
__ANON__
2.1
CRAP 7.2
« FormBuilder.pm ↑ Summary FormBuilder/FieldSet.pm »
1 package HTML::FormBuilder::Base;
2  
3 13 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 13 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 13 ▶use 5.008_005;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 13 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 13 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 13 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 13 our $VERSION = '0.12'; ## VERSION
12  
13 13 our $CLASSES = {
14 fieldset_group => 'fieldset_group',
15 no_stack_field_parent => 'no_stack_field_parent',
16 row_padding => 'row_padding',
17 fieldset_footer => 'fieldset_footer',
18 comment => 'comment',
19 row => 'row',
20 extra_tooltip_container => 'extra_tooltip_container',
21 backbutton => 'backbutton',
22 required_asterisk => 'required_asterisk',
23 inputtrailing => 'inputtrailing',
24 label_column => 'label_column',
25 input_column => 'input_column',
26 hide_mobile => 'hide_mobile'
27 };
28  
29 has classes => (
30 is => 'ro',
31 isa => sub {
32 151 ▶ my $classes = shift;
Subroutine __ANON__: called CC=2 SCAR=7.2
33 151 ▶ croak('classes should be a hashref') unless ref($classes) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $classes eq "HASH" 0 151 151
34 13 });
35  
36 has localize => (
37 is => 'ro',
38 isa => sub {
39 153 ▶ my $localize = shift;
Subroutine __ANON__: called CC=2 SCAR=7.2
40 153 ▶ croak('localize should be a sub') unless ref($localize) eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $localize eq "CODE" 0 153 153
41 },
42 default => sub {
43 5 ▶ return sub { return shift; }
Subroutine __ANON__: called CC=1 SCAR=0.0
44 28 ▶ },
Subroutine __ANON__: called CC=1 SCAR=0.0
45 13 );
46  
47 #####################################################################
48 # Usage : build the html element and its own attributes
49 # Purpose : perform checking and drop unnecessary attributes
50 # Returns : element with its attributes in string
51 # Parameters : $element_tag such as p, input, label and etc
52 # $attributes in HASH ref for example
53 # $attributes = {'id' => 'test', 'name' => 'test', 'class' => 'myclass'}
54 # Comments :
55 # See Also :
56 #####################################################################
57 sub _build_element_and_attributes {
58 275 ▶ my $self = shift;
Subroutine _build_element_and_attributes: called CC=13 SCAR=25.7
59 275 my $element_tag = shift;
60 275 my $attributes = shift;
61 275 ▶ my $content = shift || '';
Condition100%2/2
shift() || ''
l!l
170105
MC/DC100%1/1
shift() || ''
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
62 275 ▶ my $options = shift || {};
Condition100%2/2
shift() || {}
l!l
57218
MC/DC100%1/1
shift() || {}
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
63  
64 #check if the elemen tag is empty
65 275 ▶ return if ($element_tag eq '');
Branch: 1 branch
BranchTrueFalseTotal
if $element_tag eq "" 0 275 275
66  
67 275 my $html;
68 275 $html = '<' . $element_tag;
69 275 foreach my $key (sort keys %{$attributes}) {
70 next
71 if (ref($attributes->{$key}) eq 'HASH'
72 665 ▶ or ref($attributes->{$key}) eq 'ARRAY');
Branch: 1 branch
BranchTrueFalseTotal
if ref $attributes->{$key} eq "HASH" or ref $attributes->{$key} eq "ARRAY" 1 664 665
Condition66%2/3
ref $attributes->{$key} eq "HASH" or ref $attributes->{$key} eq "ARRAY"
l!l&&r!l&&!r
10664
MC/DC50%1/2
ref $attributes->{$key} eq "HASH" or ref $attributes->{$key} eq "ARRAY"
ref $attributes->{$key} eq "HASH" ref $attributes->{$key} eq "ARRAY"
Truth table66%2/3
A or B
A= ref $attributes->{$key} eq "HASH"B= ref $attributes->{$key} eq "ARRAY"
ABresult
1X1
011
000
73  
74 # skip attributes that are not intended for HTML
75 664 ▶ next if ($key =~ /^(?:option|text|hide_required_text|localize|wrap_in_div_class)/i);
Branch: 1 branch
BranchTrueFalseTotal
if $key =~ /^(?:option|text|hide_required_text|localize|wrap_in_div_class)/i 106 558 664
76 558 ▶ if ($attributes->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attributes->{$key}) 530 28 558
77 530 $html .= ' ' . $key . '="' . $attributes->{$key} . '"';
78 }
79 }
80 275 ▶ if ($element_tag eq 'button') {
Branch: 1 branch
BranchTrueFalseTotal
if ($element_tag eq 'button') { } 1 274 275
81 1 $html .= '>' . $attributes->{'value'} . '</' . $element_tag . '>';
82 } else {
83 274 $html .= '>';
84 }
85  
86 275 ▶ if ($options->{required_mark} && !$self->{option}{hide_required_text}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{"required_mark"} and not $self->{"option"}{"hide_required_text"}) 3 272 275
Condition66%2/3
$options->{"required_mark"} and not $self->{"option"}{"hide_required_text"}
!ll&&!rl&&r
27203
MC/DC50%1/2
$options->{"required_mark"} and not $self->{"option"}{"hide_required_text"}
$options->{"required_mark"} not $self->{"option"}{"hide_required_text"}
Truth table66%2/3
A and not(B)
A= $options->{"required_mark"}B= not $self->{"option"}{"hide_required_text"}
ABresult
0X0
100
111
87 3 $html .= qq[<em class="$self->{classes}{required_asterisk}">**</em>];
88 }
89  
90 #close the tag
91 275 my $end_tag = "</$element_tag>";
92  
93 # input needn't close tag
94 275 ▶ if ($element_tag =~ /^(input)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($element_tag =~ /^(input)$/) 52 223 275
95 52 $end_tag = '';
96 }
97 275 return $html . $content . $end_tag;
98 }
99  
100 #####################################################################
101 # Usage : call $self->{option}{localize} to localize a string
102 # Purpose : localize string
103 # Returns : a localized string
104 # Parameters : string
105 # Comments :
106 # See Also : new
107 #####################################################################
108 sub _localize {
109 8 ▶ my $self = shift;
Subroutine _localize: called CC=1 SCAR=0.0
110 8 my $str = shift;
111 8 return $self->localize->($str);
112 }
113  
114 13 1;
115  
116 =head1 NAME
117  
118 HTML::FormBuilder::Base - HTML::FormBuilder and HTML::FormBuilder::Fieldset, HTML::FormBuilder::Field base class
119  
120 =head1 Attributes
121  
122 =head2 classes
123  
124 please refer to L<HTML::FormBuilder>.
125  
126 =head2 localize
127  
128 please refer to L<HTML::FormBuilder>.
129  
130 =head1 AUTHOR
131  
132 Chylli L<mailto:chylli@binary.com>
133  
134 =head1 CONTRIBUTOR
135  
136 Fayland Lam L<mailto:fayland@binary.com>
137  
138 Tee Shuwn Yuan L<mailto:shuwnyuan@binary.com>
139  
140 =head1 COPYRIGHT AND LICENSE
141  
142 =cut
« FormBuilder.pm ↑ Summary FormBuilder/FieldSet.pm »