File Coverage

blib/lib/Tags/HTML/Element/Button.pm
Criterion Covered Total %
statement 41 41 100.0
branch 8 8 100.0
condition 6 6 100.0
subroutine 10 10 100.0
pod n/a
total 65 65 100.0


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