File Coverage

blib/lib/Tags/HTML/Element/Form.pm
Criterion Covered Total %
statement 60 60 100.0
branch 13 16 81.2
condition 6 6 100.0
subroutine 12 12 100.0
pod 1 1 100.0
total 92 95 96.8


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