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