File Coverage

blib/lib/Data/HTML/Element/Input.pm
Criterion Covered Total %
statement 61 61 100.0
branch 24 24 100.0
condition n/a
subroutine 12 12 100.0
pod 0 1 0.0
total 97 98 98.9


line stmt bran cond sub pod time code
1             package Data::HTML::Element::Input;
2              
3 10     10   174764 use strict;
  10         23  
  10         425  
4 10     10   57 use warnings;
  10         29  
  10         772  
5              
6 10     10   5608 use Error::Pure qw(err);
  10         141633  
  10         371  
7 10     10   1078 use List::Util 1.33 qw(none);
  10         248  
  10         1553  
8 10     10   5281 use Mo qw(build is);
  10         6729  
  10         62  
9 10     10   23473 use Mo::utils 0.26 qw(check_bool check_number);
  10         35090  
  10         272  
10 10     10   8042 use Mo::utils::CSS 0.02 qw(check_css_class);
  10         121000  
  10         300  
11 10     10   1122 use Readonly;
  10         39  
  10         760  
12 10     10   65 use Scalar::Util qw(looks_like_number);
  10         22  
  10         9043  
13              
14             Readonly::Array our @TYPES => qw(button checkbox color date datetime-local
15             email file hidden image month number password radio range reset search
16             submit tel text time url week);
17             Readonly::Array our @STEP_TYPES => qw(date datetime-local month number range time week);
18              
19             our $VERSION = 0.17;
20              
21             has autofocus => (
22             is => 'ro',
23             );
24              
25             has checked => (
26             is => 'ro',
27             );
28              
29             has css_class => (
30             is => 'ro',
31             );
32              
33             has disabled => (
34             is => 'ro',
35             );
36              
37             has id => (
38             is => 'ro',
39             );
40              
41             has label => (
42             is => 'ro',
43             );
44              
45             has max => (
46             is => 'ro',
47             );
48              
49             has min => (
50             is => 'ro',
51             );
52              
53             has name => (
54             is => 'ro',
55             );
56              
57             has onclick => (
58             is => 'ro',
59             );
60              
61             has placeholder => (
62             is => 'ro',
63             );
64              
65             has readonly => (
66             is => 'ro',
67             );
68              
69             has required => (
70             is => 'ro',
71             );
72              
73             has size => (
74             is => 'ro',
75             );
76              
77             has step => (
78             is => 'ro',
79             );
80              
81             has value => (
82             is => 'ro',
83             );
84              
85             has type => (
86             is => 'ro',
87             );
88              
89             sub BUILD {
90 29     29 0 2903426 my $self = shift;
91              
92             # Check autofocus.
93 29 100       196 if (! defined $self->{'autofocus'}) {
94 28         78 $self->{'autofocus'} = 0;
95             }
96 29         142 check_bool($self, 'autofocus');
97              
98             # Check checked.
99 29 100       673 if (! defined $self->{'checked'}) {
100 27         70 $self->{'checked'} = 0;
101             }
102 29         84 check_bool($self, 'checked');
103              
104             # Check CSS class.
105 29         480 check_css_class($self, 'css_class');
106              
107             # Check disabled.
108 29 100       387 if (! defined $self->{'disabled'}) {
109 27         67 $self->{'disabled'} = 0;
110             }
111 29         87 check_bool($self, 'disabled');
112              
113             # Check max.
114 29         481 check_number($self, 'max');
115              
116             # Check min.
117 29         345 check_number($self, 'min');
118              
119             # Check readonly.
120 29 100       292 if (! defined $self->{'readonly'}) {
121 28         74 $self->{'readonly'} = 0;
122             }
123 29         92 check_bool($self, 'readonly');
124              
125             # Check required.
126 29 100       436 if (! defined $self->{'required'}) {
127 26         67 $self->{'required'} = 0;
128             }
129 29         94 check_bool($self, 'required');
130              
131             # Check size.
132 29         413 check_number($self, 'size');
133              
134             # Check step.
135 29 100       321 if (defined $self->{'step'}) {
136             # Value 'any' is valid in step.
137 5 100       24 if ($self->{'step'} ne 'any') {
138 3 100       17 if (! looks_like_number($self->{'step'})) {
139             err "Parameter 'step' must be a number or 'any' string.",
140 1         5 'Value', $self->{'step'},
141             ;
142             }
143             }
144             }
145              
146             # Check type.
147 28 100       87 if (! defined $self->{'type'}) {
148 22         176 $self->{'type'} = 'text';
149             }
150 28 100   514   281 if (none { $self->{'type'} eq $_ } @TYPES) {
  514         3575  
151 1         19 err "Parameter 'type' has bad value.";
152             }
153              
154             # Check step and type.
155 27 100       428 if (defined $self->{'step'}) {
156 4 100   20   22 if (none { $self->type eq $_ } @STEP_TYPES) {
  20         234  
157 1         15 err "Parameter 'step' is not valid for defined type.",
158             'Type', $self->type,
159             ;
160             }
161             }
162              
163 26         144 return;
164             }
165              
166             1;
167              
168             __END__
169              
170             =pod
171              
172             =encoding utf8
173              
174             =head1 NAME
175              
176             Data::HTML::Element::Input - Data object for HTML input element.
177              
178             =head1 SYNOPSIS
179              
180             use Data::HTML::Element::Input;
181              
182             my $obj = Data::HTML::Element::Input->new(%params);
183             my $autofocus = $obj->autofocus;
184             my $checked = $obj->checked;
185             my $css_class = $obj->css_class;
186             my $disabled = $obj->disabled;
187             my $id = $obj->id;
188             my $label = $obj->label;
189             my $max = $obj->max;
190             my $min = $obj->min;
191             my $name = $obj->name;
192             my $onclick = $obj->onclick;
193             my $placeholder = $obj->placeholder;
194             my $readonly = $obj->readonly;
195             my $required = $obj->required;
196             my $size = $obj->size;
197             my $step = $obj->step;
198             my $value = $obj->value;
199             my $type = $obj->type;
200              
201             =head1 METHODS
202              
203             =head2 C<new>
204              
205             my $obj = Data::HTML::Element::Input->new(%params);
206              
207             Constructor.
208              
209             =over 8
210              
211             =item * C<autofocus>
212              
213             Autofocus flag.
214              
215             Default value is 0.
216              
217             =item * C<checked>
218              
219             Checked flag.
220              
221             Default value is 0.
222              
223             =item * C<css_class>
224              
225             Input CSS class.
226              
227             Default value is undef.
228              
229             =item * C<disabled>
230              
231             Disabled flag.
232              
233             Default value is 0.
234              
235             =item * C<id>
236              
237             Input identifier.
238              
239             Default value is undef.
240              
241             =item * C<label>
242              
243             Input label.
244              
245             Default value is undef.
246              
247             =item * C<max>
248              
249             Input maximum value.
250              
251             Default value is undef.
252              
253             =item * C<min>
254              
255             Input minimum value.
256              
257             Default value is undef.
258              
259             =item * C<name>
260              
261             Input name.
262              
263             Default value is undef.
264              
265             =item * C<onclick>
266              
267             OnClick code.
268              
269             Default value is undef.
270              
271             =item * C<placeholder>
272              
273             Input placeholder.
274              
275             Default value is undef.
276              
277             =item * C<readonly>
278              
279             Readonly flag.
280              
281             Default value is 0.
282              
283             =item * C<required>
284              
285             Required flag.
286              
287             Default value is 0.
288              
289             =item * C<size>
290              
291             Input width in characters.
292              
293             Default value is undef.
294              
295             =item * C<step>
296              
297             Input step.
298              
299             Value is number or 'any' string.
300              
301             Default value is undef.
302              
303             =item * C<value>
304              
305             Input value.
306              
307             Default value is undef.
308              
309             =item * C<type>
310              
311             Input type.
312              
313             Possible value are:
314              
315             =over
316              
317             =item * button
318              
319             =item * checkbox
320              
321             =item * color
322              
323             =item * date
324              
325             =item * datetime-local
326              
327             =item * email
328              
329             =item * file
330              
331             =item * hidden
332              
333             =item * image
334              
335             =item * month
336              
337             =item * number
338              
339             =item * password
340              
341             =item * radio
342              
343             =item * range
344              
345             =item * reset
346              
347             =item * search
348              
349             =item * submit
350              
351             =item * tel
352              
353             =item * text
354              
355             =item * time
356              
357             =item * url
358              
359             =item * week
360              
361             =back
362              
363             =back
364              
365             Returns instance of object.
366              
367             =head2 C<autofocus>
368              
369             my $autofocus = $obj->autofocus;
370              
371             Get input autofocus flag.
372              
373             Returns bool value (1/0).
374              
375             =head2 C<checked>
376              
377             my $checked = $obj->checked;
378              
379             Get input checked flag.
380              
381             Returns bool value (1/0).
382              
383             =head2 C<css_class>
384              
385             my $css_class = $obj->css_class;
386              
387             Get CSS class for input.
388              
389             Returns string.
390              
391             =head2 C<disabled>
392              
393             my $disabled = $obj->disabled;
394              
395             Get input disabled flag.
396              
397             Returns bool value (1/0).
398              
399             =head2 C<id>
400              
401             my $id = $obj->id;
402              
403             Get input identifier.
404              
405             Returns string.
406              
407             =head2 C<label>
408              
409             my $label = $obj->label;
410              
411             Get input label.
412              
413             Returns string.
414              
415             =head2 C<max>
416              
417             my $max = $obj->max;
418              
419             Get input max value.
420              
421             Returns number.
422              
423             =head2 C<min>
424              
425             my $min = $obj->min;
426              
427             Get input min value.
428              
429             Returns number.
430              
431             =head2 C<name>
432              
433             my $name = $obj->name;
434              
435             Get input name value.
436              
437             Returns string.
438              
439             =head2 C<onclick>
440              
441             my $onclick = $obj->onclick;
442              
443             Get OnClick code.
444              
445             Returns string.
446              
447             =head2 C<placeholder>
448              
449             my $placeholder = $obj->placeholder;
450              
451             Get input placeholder.
452              
453             Returns string.
454              
455             =head2 C<readonly>
456              
457             my $readonly = $obj->readonly;
458              
459             Get input readonly flag.
460              
461             Returns bool value (1/0).
462              
463             =head2 C<required>
464              
465             my $required = $obj->required;
466              
467             Get input required flag.
468              
469             Returns bool value (1/0).
470              
471             =head2 C<size>
472              
473             my $size = $obj->size;
474              
475             Get input size.
476              
477             Returns number.
478              
479             =head2 C<step>
480              
481             my $step = $obj->step;
482              
483             Get input step.
484              
485             Returns number of 'any' string.
486              
487             =head2 C<value>
488              
489             my $value = $obj->value;
490              
491             Get input value.
492              
493             Returns string.
494              
495             =head2 C<type>
496              
497             my $type = $obj->type;
498              
499             Get input type.
500              
501             Returns string.
502              
503             =head1 ERRORS
504              
505             new():
506             Parameter 'autofocus' must be a bool (0/1).
507             Value: %s
508             Parameter 'checked' must be a bool (0/1).
509             Value: %s
510             Parameter 'css_class' has bad CSS class name.
511             Value: %s
512             Parameter 'css_class' has bad CSS class name (number on begin).
513             Value: %s
514             Parameter 'disabled' must be a bool (0/1).
515             Value: %s
516             Parameter 'max' must be a number.
517             Value: %s
518             Parameter 'min' must be a number.
519             Value: %s
520             Parameter 'readonly' must be a bool (0/1).
521             Value: %s
522             Parameter 'required' must be a bool (0/1).
523             Value: %s
524             Parameter 'size' must be a number.
525             Value: %s
526             Parameter 'step' is not valid for defined type.
527             Type: %s
528             Parameter 'step' must be a number or 'any' string.
529             Value: %s
530             Parameter 'type' has bad value.
531              
532             =head1 EXAMPLE
533              
534             =for comment filename=input_text.pl
535              
536             use strict;
537             use warnings;
538              
539             use Data::HTML::Element::Input;
540              
541             my $obj = Data::HTML::Element::Input->new(
542             'autofocus' => 1,
543             'css_class' => 'input',
544             'id' => 'address',
545             'label' => 'Customer address',
546             'placeholder' => 'Place address',
547             'type' => 'text',
548             );
549              
550             # Print out.
551             print 'Id: '.$obj->id."\n";
552             print 'Type: '.$obj->type."\n";
553             print 'CSS class: '.$obj->css_class."\n";
554             print 'Label: '.$obj->label."\n";
555             print 'Autofocus: '.$obj->autofocus."\n";
556             print 'Placeholder: '.$obj->placeholder."\n";
557              
558             # Output:
559             # Id: address
560             # Type: text
561             # CSS class: input
562             # Label: Customer address
563             # Autofocus: 1
564             # Placeholder: Place address
565              
566             =head1 DEPENDENCIES
567              
568             L<Error::Pure>,
569             L<List::Util>,
570             L<Mo>,
571             L<Mo::utils>,
572             L<Mo::utils::CSS>,
573             L<Readonly>,
574             L<Scalar::Util>.
575              
576             =head1 REPOSITORY
577              
578             L<https://github.com/michal-josef-spacek/Data-HTML-Element>
579              
580             =head1 AUTHOR
581              
582             Michal Josef Špaček L<mailto:skim@cpan.org>
583              
584             L<http://skim.cz>
585              
586             =head1 LICENSE AND COPYRIGHT
587              
588             © 2022-2024 Michal Josef Špaček
589              
590             BSD 2-Clause License
591              
592             =head1 VERSION
593              
594             0.17
595              
596             =cut