File Coverage

blib/lib/Tags/HTML/Element/Select.pm
Criterion Covered Total %
statement 45 45 100.0
branch 8 8 100.0
condition 6 6 100.0
subroutine 11 11 100.0
pod n/a
total 70 70 100.0


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