File Coverage

blib/lib/Tags/HTML/Element/Input.pm
Criterion Covered Total %
statement 37 42 88.1
branch 6 8 75.0
condition 4 6 66.6
subroutine 9 10 90.0
pod n/a
total 56 66 84.8


line stmt bran cond sub pod time code
1             package Tags::HTML::Element::Input;
2              
3 6     6   303940 use base qw(Tags::HTML);
  6         15  
  6         3900  
4 6     6   43412 use strict;
  6         12  
  6         133  
5 6     6   31 use warnings;
  6         12  
  6         403  
6              
7 6     6   32 use Error::Pure qw(err);
  6         12  
  6         294  
8 6     6   31 use Scalar::Util qw(blessed);
  6         13  
  6         254  
9 6     6   3404 use Tags::HTML::Element::Utils qw(tags_boolean tags_label tags_value);
  6         19  
  6         143  
10              
11             our $VERSION = 0.15;
12              
13             sub _cleanup {
14 0     0   0 my $self = shift;
15              
16 0         0 delete $self->{'_input'};
17              
18 0         0 return;
19             }
20              
21             sub _init {
22 7     7   1008709 my ($self, $input) = @_;
23              
24             # Check input.
25 7 100 66     107 if (! defined $input
      66        
26             || ! blessed($input)
27             || ! $input->isa('Data::HTML::Element::Input')) {
28              
29 1         11 err "Input object must be a 'Data::HTML::Element::Input' instance.";
30             }
31              
32 6         17 $self->{'_input'} = $input;
33              
34 6         37 return;
35             }
36              
37             # Process 'Tags'.
38             sub _process {
39 4     4   35 my $self = shift;
40              
41 4 50       9 if (! $self->{'_input'}) {
42 0         0 return;
43             }
44              
45             $self->{'tags'}->put(
46             tags_label($self, $self->{'_input'}),
47             ['b', 'input'],
48             tags_boolean($self, $self->{'_input'}, 'autofocus'),
49             tags_value($self, $self->{'_input'}, 'css_class', 'class'),
50             tags_value($self, $self->{'_input'}, 'type'),
51             tags_value($self, $self->{'_input'}, 'id'),
52             tags_value($self, $self->{'_input'}, 'name'),
53             tags_value($self, $self->{'_input'}, 'value'),
54             tags_boolean($self, $self->{'_input'}, 'checked'),
55             tags_value($self, $self->{'_input'}, 'placeholder'),
56             tags_value($self, $self->{'_input'}, 'size'),
57             tags_boolean($self, $self->{'_input'}, 'readonly'),
58             tags_boolean($self, $self->{'_input'}, 'disabled'),
59             tags_value($self, $self->{'_input'}, 'min'),
60             tags_value($self, $self->{'_input'}, 'max'),
61             tags_value($self, $self->{'_input'}, 'step'),
62 4         14 tags_value($self, $self->{'_input'}, 'onclick'),
63             ['e', 'input'],
64             );
65              
66 4         463 return;
67             }
68              
69             sub _process_css {
70 2     2   44 my $self = shift;
71              
72 2 50       10 if (! $self->{'_input'}) {
73 0         0 return;
74             }
75              
76 2         6 my $css_class = '';
77 2         14 my $css_required = '.';
78 2 100       12 if (defined $self->{'_input'}->css_class) {
79 1         16 $css_class = '.'.$self->{'_input'}->css_class;
80 1         23 $css_required .= $self->{'_input'}->css_class.'-';
81             }
82 2         22 $css_required .= 'required';
83              
84 2         126 $self->{'css'}->put(
85             ['s', 'input'.$css_class.'[type=submit]:hover'],
86             ['d', 'background-color', '#45a049'],
87             ['e'],
88              
89             ['s', 'input'.$css_class.'[type=submit]'],
90             ['d', 'width', '100%'],
91             ['d', 'background-color', '#4CAF50'],
92             ['d', 'color', 'white'],
93             ['d', 'padding', '14px 20px'],
94             ['d', 'margin', '8px 0'],
95             ['d', 'border', 'none'],
96             ['d', 'border-radius', '4px'],
97             ['d', 'cursor', 'pointer'],
98             ['e'],
99              
100             ['s', 'input'.$css_class.'[type=submit][disabled=disabled]'],
101             ['d', 'background-color', '#888888'],
102             ['e'],
103              
104             ['s', 'input'.$css_class.'[type=text]'],
105             ['s', 'input'.$css_class.'[type=date]'],
106             ['s', 'input'.$css_class.'[type=number]'],
107             ['s', 'input'.$css_class.'[type=email]'],
108             ['s', 'input'.$css_class.'[type=checkbox]'],
109             ['d', 'width', '100%'],
110             ['d', 'padding', '12px 20px'],
111             ['d', 'margin', '8px 0'],
112             ['d', 'display', 'inline-block'],
113             ['d', 'border', '1px solid #ccc'],
114             ['d', 'border-radius', '4px'],
115             ['d', 'box-sizing', 'border-box'],
116             ['e'],
117              
118             ['s', 'input'.$css_class.'[type=button]'],
119             ['d', 'width', '100%'],
120             ['d', 'background-color', '#4CAF50'],
121             ['d', 'color', 'white'],
122             ['d', 'padding', '14px 20px'],
123             ['d', 'margin', '8px 0'],
124             ['d', 'border', 'none'],
125             ['d', 'border-radius', '4px'],
126             ['d', 'cursor', 'pointer'],
127             ['e'],
128              
129             ['s', 'input'.$css_class.'[type=button]:hover'],
130             ['d', 'background-color', '#45a049'],
131             ['e'],
132              
133             ['s', $css_required],
134             ['d', 'color', 'red'],
135             ['e'],
136             );
137              
138 2         2855 return;
139             }
140              
141             1;
142              
143             __END__
144              
145             =pod
146              
147             =encoding utf8
148              
149             =head1 NAME
150              
151             Tags::HTML::Element::Input - Tags helper for HTML input element.
152              
153             =head1 SYNOPSIS
154              
155             use Tags::HTML::Element::Input;
156              
157             my $obj = Tags::HTML::Element::Input->new(%params);
158             $obj->cleanup;
159             $obj->init($input);
160             $obj->prepare;
161             $obj->process;
162             $obj->process_css;
163              
164             =head1 METHODS
165              
166             =head2 C<new>
167              
168             my $obj = Tags::HTML::Element::Input->new(%params);
169              
170             Constructor.
171              
172             =over 8
173              
174             =item * C<css>
175              
176             'CSS::Struct::Output' object for L<process_css> processing.
177              
178             Default value is undef.
179              
180             =item * C<tags>
181              
182             'Tags::Output' object.
183              
184             Default value is undef.
185              
186             =back
187              
188             =head2 C<cleanup>
189              
190             $obj->cleanup;
191              
192             Cleanup internal structures.
193              
194             Returns undef.
195              
196             =head2 C<init>
197              
198             $obj->init($input);
199              
200             Initialize object.
201              
202             Accepted C<$input> is L<Data::HTML::Element::Input>.
203              
204             Returns undef.
205              
206             =head2 C<prepare>
207              
208             $obj->prepare;
209              
210             Process initialization before page run.
211              
212             Do nothing in this object.
213              
214             Returns undef.
215              
216             =head2 C<process>
217              
218             $obj->process;
219              
220             Process L<Tags> structure to output.
221              
222             Returns undef.
223              
224             =head2 C<process_css>
225              
226             $obj->process_css;
227              
228             Process L<CSS::Struct> structure for output.
229              
230             Returns undef.
231              
232             =head1 ERRORS
233              
234             new():
235             From Tags::HTML::new():
236             Parameter 'css' must be a 'CSS::Struct::Output::*' class.
237             Parameter 'tags' must be a 'Tags::Output::*' class.
238             init():
239             Input object must be a 'Data::HTML::Element::Input' instance.
240              
241             process():
242             From Tags::HTML::process():
243             Parameter 'tags' isn't defined.
244              
245             process_css():
246             From Tags::HTML::process_css():
247             Parameter 'css' isn't defined.
248              
249             =head1 EXAMPLE
250              
251             =for comment filename=create_and_print_input.pl
252              
253             use strict;
254             use warnings;
255              
256             use CSS::Struct::Output::Indent;
257             use Data::HTML::Element::Input;
258             use Tags::HTML::Element::Input;
259             use Tags::Output::Indent;
260              
261             # Object.
262             my $css = CSS::Struct::Output::Indent->new;
263             my $tags = Tags::Output::Indent->new(
264             'xml' => 1,
265             );
266             my $obj = Tags::HTML::Element::Input->new(
267             'css' => $css,
268             'tags' => $tags,
269             );
270              
271             # Data object for input.
272             my $input = Data::HTML::Element::Input->new(
273             'css_class' => 'form-input',
274             );
275              
276             # Initialize.
277             $obj->init($input);
278              
279             # Process input.
280             $obj->process;
281             $obj->process_css;
282              
283             # Print out.
284             print "HTML:\n";
285             print $tags->flush;
286             print "\n\n";
287             print "CSS:\n";
288             print $css->flush;
289              
290             # Output:
291             # HTML:
292             # <input class="form-input" type="text" />
293             #
294             # CSS:
295             # input.form-input[type=submit]:hover {
296             # background-color: #45a049;
297             # }
298             # input.form-input[type=submit] {
299             # width: 100%;
300             # background-color: #4CAF50;
301             # color: white;
302             # padding: 14px 20px;
303             # margin: 8px 0;
304             # border: none;
305             # border-radius: 4px;
306             # cursor: pointer;
307             # }
308             # input.form-input {
309             # width: 100%;
310             # padding: 12px 20px;
311             # margin: 8px 0;
312             # display: inline-block;
313             # border: 1px solid #ccc;
314             # border-radius: 4px;
315             # box-sizing: border-box;
316             # }
317              
318             =head1 DEPENDENCIES
319              
320             L<Class::Utils>,
321             L<Error::Pure>,
322             L<Scalar::Util>,
323             L<Tags::HTML>,
324             L<Tags::HTML::Element::Utils>.
325              
326             =head1 REPOSITORY
327              
328             L<https://github.com/michal-josef-spacek/Tags-HTML-Element>
329              
330             =head1 AUTHOR
331              
332             Michal Josef Špaček L<mailto:skim@cpan.org>
333              
334             L<http://skim.cz>
335              
336             =head1 LICENSE AND COPYRIGHT
337              
338             © 2022-2024 Michal Josef Špaček
339              
340             BSD 2-Clause License
341              
342             =head1 VERSION
343              
344             0.15
345              
346             =cut