File Coverage

blib/lib/Tags/HTML/Element/Option.pm
Criterion Covered Total %
statement 40 41 97.5
branch 7 8 87.5
condition 6 6 100.0
subroutine 10 10 100.0
pod n/a
total 63 65 96.9


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