File Coverage

blib/lib/Data/HTML/Element/Button.pm
Criterion Covered Total %
statement 55 55 100.0
branch 18 18 100.0
condition n/a
subroutine 13 13 100.0
pod 0 1 0.0
total 86 87 98.8


line stmt bran cond sub pod time code
1             package Data::HTML::Element::Button;
2              
3 15     15   220928 use strict;
  15         35  
  15         537  
4 15     15   70 use warnings;
  15         28  
  15         979  
5              
6 15     15   7662 use Data::HTML::Element::Utils qw(check_data check_data_type);
  15         78  
  15         400  
7 15     15   1223 use Error::Pure qw(err);
  15         31  
  15         921  
8 15     15   82 use List::Util 1.33 qw(none);
  15         227  
  15         1037  
9 15     15   7747 use Mo qw(build default is);
  15         12126  
  15         133  
10 15     15   55489 use Mo::utils 0.06 qw(check_array check_bool check_required);
  15         298  
  15         1236  
11 15     15   8331 use Mo::utils::CSS 0.02 qw(check_css_class);
  15         180883  
  15         384  
12 15     15   1475 use Readonly;
  15         34  
  15         10077  
13              
14             Readonly::Array our @DATA_TYPES => qw(plain tags);
15             Readonly::Array our @ENCTYPES => (
16             'application/x-www-form-urlencoded',
17             'multipart/form-data',
18             'text/plain',
19             );
20             Readonly::Array our @FORM_METHODS => qw(get post);
21             Readonly::Array our @TYPES => qw(button reset submit);
22              
23             our $VERSION = 0.17;
24              
25             has autofocus => (
26             ro => 1,
27             );
28              
29             has css_class => (
30             ro => 1,
31             );
32              
33             has data => (
34             default => [],
35             ro => 1,
36             );
37              
38             has data_type => (
39             ro => 1,
40             );
41              
42             has disabled => (
43             ro => 1,
44             );
45              
46             has form => (
47             ro => 1,
48             );
49              
50             has form_action => (
51             ro => 1,
52             );
53              
54             has form_enctype => (
55             ro => 1,
56             );
57              
58             has form_method => (
59             ro => 1,
60             );
61              
62             has form_no_validate => (
63             ro => 1,
64             );
65              
66             has id => (
67             ro => 1,
68             );
69              
70             has name => (
71             ro => 1,
72             );
73              
74             has pop_over_target => (
75             ro => 1,
76             );
77              
78             has type => (
79             ro => 1,
80             );
81              
82             has value => (
83             ro => 1,
84             );
85              
86             sub BUILD {
87 38     38 0 4483099 my $self = shift;
88              
89             # Check autofocus.
90 38 100       241 if (! defined $self->{'autofocus'}) {
91 34         115 $self->{'autofocus'} = 0;
92             }
93 38         201 check_bool($self, 'autofocus');
94              
95             # Check CSS class.
96 37         927 check_css_class($self, 'css_class');
97              
98             # Check data type.
99 37         573 check_data_type($self);
100              
101             # Check data based on type.
102 36         322 check_data($self);
103              
104             # Check disabled.
105 33 100       96 if (! defined $self->{'disabled'}) {
106 30         74 $self->{'disabled'} = 0;
107             }
108 33         102 check_bool($self, 'disabled');
109              
110             # Check form_enctype.
111 33 100       571 if (defined $self->{'form_enctype'}) {
112 4 100   10   17 if (none { $self->{'form_enctype'} eq $_ } @ENCTYPES) {
  10         44  
113             err "Parameter 'form_enctype' has bad value.",
114 1         7 'Value', $self->{'form_enctype'},
115             ;
116             }
117             }
118              
119             # Check form_method.
120 32 100       164 if (! defined $self->{'form_method'}) {
121 28         78 $self->{'form_method'} = 'get';
122             }
123 32 100   34   183 if (none { $self->{'form_method'} eq $_ } @FORM_METHODS) {
  34         251  
124 1         6 err "Parameter 'form_method' has bad value.";
125             }
126              
127             # Check form_action.
128             # TODO
129              
130             # Check form_no_validate.
131 31 100       325 if (! defined $self->{'form_no_validate'}) {
132 28         81 $self->{'form_no_validate'} = 0;
133             }
134 31         93 check_bool($self, 'form_no_validate');
135              
136             # Check type.
137 30 100       508 if (! defined $self->{'type'}) {
138 26         65 $self->{'type'} = 'button';
139             }
140 30 100   37   169 if (none { $self->{'type'} eq $_ } @TYPES) {
  37         263  
141 1         6 err "Parameter 'type' has bad value.";
142             }
143              
144 29         307 return;
145             }
146              
147             1;
148              
149             __END__
150              
151             =pod
152              
153             =encoding utf8
154              
155             =head1 NAME
156              
157             Data::HTML::Element::Button - Data object for HTML button element.
158              
159             =head1 SYNOPSIS
160              
161             use Data::HTML::Element::Button;
162              
163             my $obj = Data::HTML::Element::Button->new(%params);
164             my $autofocus = $obj->autofocus;
165             my $css_class = $obj->css_class;
166             my $data = $obj->data;
167             my $data_type = $obj->data_type;
168             my $disabled = $obj->disabled;
169             my $form = $obj->form;
170             my $form_action = $obj->form_action;
171             my $form_enctype = $obj->form_enctype;
172             my $form_method = $obj->form_method;
173             my $form_no_validate = $obj->form_no_validate;
174             my $id = $obj->id;
175             my $name = $obj->name;
176             my $pop_over_target = $obj->pop_over_target;
177             my $type = $obj->type;
178             my $value = $obj->value;
179              
180             =head1 METHODS
181              
182             =head2 C<new>
183              
184             my $obj = Data::HTML::Element::Button->new(%params);
185              
186             Constructor.
187              
188             =over 8
189              
190             =item * C<autofocus>
191              
192             Button autofocus flag.
193              
194             Default value is 0.
195              
196             =item * C<css_class>
197              
198             Button CSS class.
199              
200             Default value is undef.
201              
202             =item * C<data>
203              
204             Button data content. It's reference to array.
205              
206             Data type of data is described in 'data_type' parameter.
207              
208             Default value is [].
209              
210             =item * C<data_type>
211              
212             Button data type for content.
213              
214             Possible value are: cb plain tags
215              
216             The 'cb' content is code reference.
217             The 'plain' content are string(s).
218             The 'tags' content is structure described in L<Tags>.
219              
220             Default value is 'plain'.
221              
222             =item * C<disabled>
223              
224             Button autofocus flag.
225              
226             Default value is 0.
227              
228             =item * C<form>
229              
230             Button form id.
231              
232             Default value is undef.
233              
234             =item * C<form_action>
235              
236             Button form action URL.
237              
238             Default value is undef.
239              
240             =item * C<form_enctype>
241              
242             Button form encoding.
243             It's valuable for 'submit' type.
244              
245             Possible values are: application/x-www-form-urlencoded multipart/form-data text/plain
246              
247             Default value is undef.
248              
249             =item * C<form_method>
250              
251             Button form method.
252             It's valuable for 'submit' type.
253              
254             Possible values are: get post
255              
256             Default value is 'get'.
257              
258             =item * C<form_no_validate>
259              
260             Button formnovalidate flag.
261              
262             Default value is 0.
263              
264             =item * C<id>
265              
266             Button identifier.
267              
268             Default value is undef.
269              
270             =item * C<name>
271              
272             Button name.
273              
274             Default value is undef.
275              
276             =item * C<pop_over_target>
277              
278             Button pop over target id.
279              
280             Default value is undef.
281              
282             =item * C<type>
283              
284             Button element type.
285              
286             Possible types: button reset submit
287              
288             Default value is 'button'.
289              
290             =item * C<value>
291              
292             Button value.
293              
294             Default value is undef.
295              
296             =back
297              
298             Returns instance of object.
299              
300             =head2 C<autofocus>
301              
302             my $autofocus = $obj->autofocus;
303              
304             Get button autofocus flag.
305              
306             Returns bool value (1/0).
307              
308             =head2 C<css_class>
309              
310             my $css_class = $obj->css_class;
311              
312             Get CSS class for button.
313              
314             Returns string.
315              
316             =head2 C<data>
317              
318             my $data = $obj->data;
319              
320             Get data inside button element.
321              
322             Returns reference to array.
323              
324             =head2 C<data_type>
325              
326             my $data_type = $obj->data_type;
327              
328             Get button data type.
329              
330             Returns string.
331              
332             =head2 C<disabled>
333              
334             my $disabled = $obj->disabled;
335              
336             Get button disabled flag.
337              
338             Returns bool value (1/0).
339              
340             =head2 C<form>
341              
342             my $form = $obj->form;
343              
344             Get button form id.
345              
346             Returns string.
347              
348             =head2 C<form_action>
349              
350             my $form_action = $obj->form_action;
351              
352             Get button form action URL.
353              
354             Returns string.
355              
356             =head2 C<form_enctype>
357              
358             my $form_enctype = $obj->form_enctype;
359              
360             Get button form enctype.
361              
362             Returns string.
363              
364             =head2 C<form_method>
365              
366             my $form_method = $obj->form_method;
367              
368             Get button form method.
369              
370             Returns string.
371              
372             =head2 C<form_no_validate>
373              
374             my $form_no_validate = $obj->form_no_validate;
375              
376             Get formnovalidate flag.
377              
378             Returns bool value (1/0).
379              
380             =head2 C<id>
381              
382             my $id = $obj->id;
383              
384             Get button identifier.
385              
386             Returns string.
387              
388             =head2 C<name>
389              
390             my $name = $obj->name;
391              
392             Get button name.
393              
394             Returns string.
395              
396             =head2 C<pop_over_target>
397              
398             my $pop_over_target = $obj->pop_over_target;
399              
400             Get button pop over target id.
401              
402             Returns string.
403              
404             =head2 C<type>
405              
406             my $type = $obj->type;
407              
408             Get button type.
409              
410             Returns string.
411              
412             =head2 C<value>
413              
414             my $value = $obj->value;
415              
416             Get button value.
417              
418             Returns string.
419              
420             =head1 ERRORS
421              
422             new():
423             Parameter 'autofocus' must be a bool (0/1).
424             Value: %s
425             Parameter 'css_class' has bad CSS class name.
426             Value: %s
427             Parameter 'css_class' has bad CSS class name (number on begin).
428             Value: %s
429             Parameter 'data' must be a array.
430             Value: %s
431             Reference: %s
432             Parameter 'data' in 'plain' mode must contain reference to array with scalars.
433             Parameter 'data' in 'tags' mode must contain reference to array with references to array with Tags structure.
434             Parameter 'data_type' has bad value.
435             Parameter 'disabled' must be a bool (0/1).
436             Value: %s
437             Parameter 'form_enctype' has bad value.
438             Value: %s
439             Parameter 'form_method' has bad value.
440             Parameter 'formnovalidate' must be a bool (0/1).
441             Value: %s
442             Parameter 'type' has bad value.
443              
444             =head1 EXAMPLE1
445              
446             =for comment filename=button_default.pl
447              
448             use strict;
449             use warnings;
450              
451             use Data::HTML::Element::Button;
452              
453             my $obj = Data::HTML::Element::Button->new;
454              
455             # Print out.
456             print 'Data type: '.$obj->data_type."\n";
457             print 'Form method: '.$obj->form_method."\n";
458             print 'Type: '.$obj->type."\n";
459              
460             # Output:
461             # Data type: plain
462             # Form method: get
463             # Type: button
464              
465             =head1 EXAMPLE2
466              
467             =for comment filename=button_tags.pl
468              
469             use strict;
470             use warnings;
471              
472             use Data::HTML::Element::Button;
473             use Tags::Output::Raw;
474              
475             my $obj = Data::HTML::Element::Button->new(
476             # Tags(3pm) structure.
477             'data' => [
478             ['b', 'span'],
479             ['d', 'Button'],
480             ['e', 'span'],
481             ],
482             'data_type' => 'tags',
483             );
484              
485             my $tags = Tags::Output::Raw->new;
486              
487             # Serialize data to output.
488             $tags->put(@{$obj->data});
489             my $data = $tags->flush(1);
490              
491             # Print out.
492             print 'Data (serialized): '.$data."\n";
493             print 'Data type: '.$obj->data_type."\n";
494             print 'Form method: '.$obj->form_method."\n";
495             print 'Type: '.$obj->type."\n";
496              
497             # Output:
498             # Data (serialized): <span>Button</span>
499             # Data type: tags
500             # Form method: get
501             # Type: button
502              
503             =head1 EXAMPLE3
504              
505             =for comment filename=button_plain.pl
506              
507             use strict;
508             use warnings;
509              
510             use Data::HTML::Element::Button;
511              
512             my $obj = Data::HTML::Element::Button->new(
513             # Plain content.
514             'data' => [
515             'Button',
516             ],
517             'data_type' => 'plain',
518             );
519              
520             # Serialize data to output.
521             my $data = join ' ', @{$obj->data};
522              
523             # Print out.
524             print 'Data: '.$data."\n";
525             print 'Data type: '.$obj->data_type."\n";
526             print 'Form method: '.$obj->form_method."\n";
527             print 'Type: '.$obj->type."\n";
528              
529             # Output:
530             # Data: Button
531             # Data type: plain
532             # Form method: get
533             # Type: button
534              
535             =head1 DEPENDENCIES
536              
537             L<Data::HTML::Element::Utils>,
538             L<Error::Pure>,
539             L<List::Util>,
540             L<Mo>,
541             L<Mo::utils>,
542             L<Mo::utils::CSS>,
543             L<Readonly>.
544              
545             =head1 REPOSITORY
546              
547             L<https://github.com/michal-josef-spacek/Data-HTML-Element>
548              
549             =head1 AUTHOR
550              
551             Michal Josef Špaček L<mailto:skim@cpan.org>
552              
553             L<http://skim.cz>
554              
555             =head1 LICENSE AND COPYRIGHT
556              
557             © 2022-2024 Michal Josef Špaček
558              
559             BSD 2-Clause License
560              
561             =head1 VERSION
562              
563             0.17
564              
565             =cut