lib/HTML/FormFu/Role/Element/Input.pm

filter: Statementstmt 94.6% 123 / 130 Branchbran 65.3% 34 / 52 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 21 / 21 Podpod n/a totaltotal 86.0% 179 / 208 CC 32 SCAR 16.5
CC
32
Cov
85%
__ANON__
10.1
_parse_option_hashref
9.2
datalist_options
6.3
CRAP 5.2
« lib/HTML/FormFu/Constraint/Range.pm ↑ Summary lib/HTML/FormFu/Attribute.pm »
1 342 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2  
3 package HTML::FormFu::Role::Element::Input;
4 342 $HTML::FormFu::Role::Element::Input::VERSION = '2.09';
5 # ABSTRACT: Role for input fields
6  
7 342 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 342 with 'HTML::FormFu::Role::Element::Field',
10 'HTML::FormFu::Role::Element::FieldMethods' =>
11 { -excludes => 'nested_name' },
12 'HTML::FormFu::Role::Element::Coercible';
13  
14 342 ▶use HTML::FormFu::Util qw( literal xml_escape );
Subroutine BEGIN: called CC=1 SCAR=0.0
15 342 ▶use Clone ();
Subroutine BEGIN: called CC=1 SCAR=0.0
16 342 ▶use List::Util 1.33 qw( none );
Subroutine BEGIN: called CC=1 SCAR=0.0
17 342 ▶use Scalar::Util qw( reftype );
Subroutine BEGIN: called CC=1 SCAR=0.0
18 342 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 342 ▶use HTML::FormFu::Attribute qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
21 mk_attr_accessors
22 mk_attr_bool_accessors
23 342 );
24 342 ▶use HTML::FormFu::Constants qw( $EMPTY_STR );
Subroutine BEGIN: called CC=1 SCAR=0.0
25 342 ▶use HTML::FormFu::Util qw( process_attrs xml_escape );
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 342 has field_type => ( is => 'rw', );
28  
29 342 has datalist_id => ( is => 'rw' );
30  
31 has _datalist_options => (
32 is => 'rw',
33 429 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
34 342 lazy => 1,
35 isa => 'ArrayRef',
36 );
37  
38 342 __PACKAGE__->mk_attr_accessors( qw(
39 alt autocomplete
40 checked maxlength
41 pattern placeholder
42 size
43 ) );
44  
45 342 __PACKAGE__->mk_attr_bool_accessors( qw(
46 autofocus
47 multiple
48 required
49 ) );
50  
51 342 my @ALLOWED_OPTION_KEYS = qw(
52 value
53 value_xml
54 value_loc
55 label
56 label_xml
57 label_loc
58 );
59  
60 1 ▶sub datalist_options {
Subroutine datalist_options: called CC=6 SCAR=18.5
61 1 my ( $self, $arg ) = @_;
62 1 my ( @options, @new );
63  
64 1 ▶ return $self->_datalist_options if @_ == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 1 0 1 1
65  
66 1 ▶ croak "datalist_options argument must be a single array-ref" if @_ > 2;
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 2 0 1 1
67  
68 1 ▶ if ( defined $arg ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $arg) 1 0 1
69 1 ▶ croak "datalist_options argument must be an array-ref"
Branch: 1 branch
BranchTrueFalseTotal
if builtin::reftype($arg) ne "ARRAY" 0 1 1
70 if reftype($arg) ne 'ARRAY';
71  
72 1 @options = @$arg;
73  
74 1 for my $item (@options) {
75 4 push @new, $self->_parse_option($item);
76 }
77 }
78  
79 1 $self->_datalist_options( \@new );
80  
81 1 return $self;
82 }
83  
84 4 ▶sub _parse_option {
Subroutine _parse_option: called CC=3 SCAR=11.3
85 4 my ( $self, $item ) = @_;
86  
87 4 ▶ if ( reftype($item) eq 'HASH' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (builtin::reftype($item) eq 'HASH') { } 2 2 4
elsif (builtin::reftype($item) eq 'ARRAY') { } 2 0 2
88 2 return $self->_parse_option_hashref($item);
89 }
90 elsif ( reftype($item) eq 'ARRAY' ) {
91 return {
92 2 value => $item->[0],
93 label => $item->[1],
94 };
95 }
96 else {
97 0 croak "each datalist_options argument must be a hash-ref or array-ref";
98 }
99 }
100  
101 2 ▶sub _parse_option_hashref {
Subroutine _parse_option_hashref: called CC=8 SCAR=22.2
102 2 my ( $self, $item ) = @_;
103  
104 # sanity check options
105 2 my @keys = keys %$item;
106  
107 2 for my $key (@keys) {
108 croak "unknown option argument: '$key'"
109 4 ▶ if none { $key eq $_ } @ALLOWED_OPTION_KEYS;
Subroutine __ANON__: called CC=1 SCAR=0.2
Branch: 1 branch
BranchTrueFalseTotal
if &none(sub { $key eq $_; } , @ALLOWED_OPTION_KEYS) 0 4 4
110 }
111  
112 2 ▶ if ( defined $item->{label_xml} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $item->{'label_xml'}) { } 0 2 2
elsif (defined $item->{'label_loc'}) { } 1 1 2
113 0 $item->{label} = literal( $item->{label_xml} );
114 }
115 elsif ( defined $item->{label_loc} ) {
116 1 $item->{label} = $self->form->localize( $item->{label_loc} );
117 }
118  
119 2 ▶ if ( defined $item->{value_xml} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $item->{'value_xml'}) { } 1 1 2
elsif (defined $item->{'value_loc'}) { } 0 1 1
120 1 $item->{value} = literal( $item->{value_xml} );
121 }
122 elsif ( defined $item->{value_loc} ) {
123 0 $item->{value} = $self->form->localize( $item->{value_loc} );
124 }
125  
126 2 ▶ if ( !defined $item->{value} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $item->{"value"}) 0 2 2
127 0 $item->{value} = '';
128 }
129  
130 2 return $item;
131 }
132  
133 1 ▶sub datalist_values {
Subroutine datalist_values: called CC=4 SCAR=14.1
134 1 my ( $self, $arg ) = @_;
135  
136 1 ▶ croak "datalist_values argument must be a single array-ref of values"
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 2 0 1 1
137 if @_ > 2;
138  
139 1 my @values;
140  
141 1 ▶ if ( defined $arg ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $arg) 1 0 1
142 1 ▶ croak "datalist_values argument must be an array-ref"
Branch: 1 branch
BranchTrueFalseTotal
if builtin::reftype($arg) ne "ARRAY" 0 1 1
143 if reftype($arg) ne 'ARRAY';
144  
145 1 @values = @$arg;
146 }
147  
148 1 my @new = map { { value => $_, label => ucfirst $_, } } @values;
149  
150 1 $self->_datalist_options( \@new );
151  
152 1 return $self;
153 }
154  
155 744 ▶around prepare_id => sub {
Subroutine __ANON__: called CC=8 SCAR=23.2
156 744 my ( $orig, $self, $render ) = @_;
157  
158 744 $self->$orig($render);
159  
160 744 ▶ return if !@{ $self->_datalist_options };
Branch: 1 branch
BranchTrueFalseTotal
unless @{$self->_datalist_options;} 742 2 744
161  
162 2 ▶ if ( defined $render->{datalist_id} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $render->{'datalist_id'}) { } 0 2 2
elsif (defined $self->auto_datalist_id and length $self->auto_datalist_id) { } 2 0 2
163 0 $render->{attributes}{list} = $render->{datalist_id};
164 }
165 ▶ elsif ( defined $self->auto_datalist_id
Condition33%1/3
defined $self->auto_datalist_id and length $self->auto_datalist_id
!ll&&!rl&&r
002
MC/DC0%0/2
defined $self->auto_datalist_id and length $self->auto_datalist_id
defined $self->auto_datalist_id length $self->auto_datalist_id
Truth table33%1/3
A and B
A= defined $self->auto_datalist_idB= length $self->auto_datalist_id
ABresult
0X0
100
111
166 && length $self->auto_datalist_id )
167 {
168 2 ▶ my $form_name
Branch: 1 branch
BranchTrueFalseTotal
defined $self->form->id ? : 2 0 2
169 = defined $self->form->id
170 ? $self->form->id
171 : $EMPTY_STR;
172  
173 my $field_name
174 = defined $render->{nested_name}
175 ? $render->{nested_name}
176 2 ▶ : $EMPTY_STR;
Branch: 1 branch
BranchTrueFalseTotal
defined $render->{'nested_name'} ? : 2 0 2
177  
178 2 my %string = (
179 f => $form_name,
180 n => $field_name,
181 );
182  
183 2 my $id = $self->auto_datalist_id;
184 2 $id =~ s/%([fn])/$string{$1}/g;
185  
186 2 ▶ if ( defined( my $count = $self->repeatable_count ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $count = $self->repeatable_count)) 0 2 2
187 0 $id =~ s/%r/$count/g;
188 }
189  
190 2 $render->{attributes}{list} = $id;
191 }
192 else {
193 0 croak
194 "either 'datalist_id' or 'auto_datalist_id' must be set when using a datalist";
195 }
196  
197 2 return;
198 342 };
199  
200 744 ▶around render_data_non_recursive => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
201 744 my ( $orig, $self, $args ) = @_;
202  
203 744 ▶ my $render = $self->$orig(
Branch: 1 branch
BranchTrueFalseTotal
$args ? : 0 744 744
204 { field_type => $self->field_type,
205 placeholder => $self->placeholder,
206 $args ? %$args : (),
207 } );
208  
209 744 ▶ if ( @{ $self->_datalist_options } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->_datalist_options;}) 2 742 744
210 2 $render->{datalist_options} = Clone::clone( $self->_datalist_options );
211 }
212  
213 744 $self->_quote_options( $render->{datalist_options} );
214  
215 744 return $render;
216 342 };
217  
218 744 ▶sub _quote_options {
Subroutine _quote_options: called CC=2 SCAR=6.9
219 744 my ( $self, $options ) = @_;
220  
221 744 foreach my $opt (@$options) {
222 7 $opt->{label} = xml_escape( $opt->{label} );
223 7 $opt->{value} = xml_escape( $opt->{value} );
224 }
225 }
226  
227 587 ▶sub _string_field {
Subroutine _string_field: called CC=5 SCAR=16.1
228 587 my ( $self, $render ) = @_;
229  
230 587 my $html = "";
231  
232 587 ▶ if ( $render->{datalist_options} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($render->{"datalist_options"}) 2 585 587
233 2 $html .= sprintf qq{<datalist id="%s">\n}, $render->{attributes}{list};
234 2 for my $option ( @{ $render->{datalist_options} } ) {
235 $html .= sprintf qq{<option value="%s">%s</option>\n},
236 $option->{value},
237 7 $option->{label};
238 }
239 2 $html .= sprintf qq{</datalist>\n};
240 }
241  
242 587 $html .= "<input";
243  
244 587 ▶ if ( defined $render->{nested_name} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $render->{"nested_name"}) 581 6 587
245 581 $html .= sprintf qq{ name="%s"}, $render->{nested_name};
246 }
247  
248 587 $html .= sprintf qq{ type="%s"}, $render->{field_type};
249  
250 587 ▶ if ( defined $render->{value} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $render->{"value"}) 363 224 587
251 363 $html .= sprintf qq{ value="%s"}, $render->{value};
252 }
253  
254 587 $html .= sprintf "%s />", process_attrs( $render->{attributes} );
255  
256 587 return $html;
257 }
258  
259 532 ▶around clone => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
260 532 my ( $orig, $self ) = @_;
261  
262 532 my $clone = $self->$orig(@_);
263  
264 532 $clone->_datalist_options( Clone::clone( $self->_datalist_options ) );
265  
266 532 return $clone;
267 342 };
268  
269 342 1;
270  
271 __END__
« lib/HTML/FormFu/Constraint/Range.pm ↑ Summary lib/HTML/FormFu/Attribute.pm »