File Coverage

blib/lib/Tags/HTML/Element/Textarea.pm
Criterion Covered Total %
statement 39 39 100.0
branch 10 10 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::Textarea;
2              
3 7     7   284552 use base qw(Tags::HTML);
  7         15  
  7         3943  
4 7     7   56837 use strict;
  7         15  
  7         150  
5 7     7   27 use warnings;
  7         19  
  7         448  
6              
7 7     7   35 use Error::Pure qw(err);
  7         13  
  7         451  
8 7     7   42 use Scalar::Util qw(blessed);
  7         14  
  7         391  
9 7     7   3888 use Tags::HTML::Element::Utils qw(tags_boolean tags_label tags_value);
  7         24  
  7         162  
10              
11             our $VERSION = 0.15;
12              
13             sub _cleanup {
14 1     1   322911 my $self = shift;
15              
16 1         3 delete $self->{'_textarea'};
17              
18 1         2 return;
19             }
20              
21             sub _init {
22 10     10   1108021 my ($self, $textarea) = @_;
23              
24             # Check textarea.
25 10 100 100     115 if (! defined $textarea
      100        
26             || ! blessed($textarea)
27             || ! $textarea->isa('Data::HTML::Element::Textarea')) {
28              
29 3         87 err "Textarea object must be a 'Data::HTML::Element::Textarea' instance.";
30             }
31              
32 7         22 $self->{'_textarea'} = $textarea;
33              
34 7         18 return;
35             }
36              
37             # Process 'Tags'.
38             sub _process {
39 5     5   5669 my $self = shift;
40              
41 5 100       16 if (! exists $self->{'_textarea'}) {
42 1         4 return;
43             }
44              
45             $self->{'tags'}->put(
46             tags_label($self, $self->{'_textarea'}),
47             ['b', 'textarea'],
48             tags_boolean($self, $self->{'_textarea'}, 'autofocus'),
49             tags_value($self, $self->{'_textarea'}, 'css_class', 'class'),
50             tags_value($self, $self->{'_textarea'}, 'id'),
51             tags_value($self, $self->{'_textarea'}, 'name'),
52             tags_value($self, $self->{'_textarea'}, 'placeholder'),
53             tags_boolean($self, $self->{'_textarea'}, 'readonly'),
54             tags_boolean($self, $self->{'_textarea'}, 'disabled'),
55             tags_boolean($self, $self->{'_textarea'}, 'required'),
56             tags_value($self, $self->{'_textarea'}, 'cols'),
57             tags_value($self, $self->{'_textarea'}, 'rows'),
58             tags_value($self, $self->{'_textarea'}, 'form'),
59             defined $self->{'_textarea'}->value ? (
60 4 100       17 ['d', $self->{'_textarea'}->value],
61             ) : (),
62             ['e', 'textarea'],
63             );
64              
65 4         1103 return;
66             }
67              
68             sub _process_css {
69 3     3   3434 my $self = shift;
70              
71 3 100       13 if (! exists $self->{'_textarea'}) {
72 1         3 return;
73             }
74              
75 2         5 my $css_class = '';
76 2 100       10 if (defined $self->{'_textarea'}->css_class) {
77 1         15 $css_class = '.'.$self->{'_textarea'}->css_class;
78             }
79              
80 2         54 $self->{'css'}->put(
81             ['s', 'textarea'.$css_class],
82             ['d', 'width', '100%'],
83             ['d', 'padding', '12px 20px'],
84             ['d', 'margin', '8px 0'],
85             ['d', 'display', 'inline-block'],
86             ['d', 'border', '1px solid #ccc'],
87             ['d', 'border-radius', '4px'],
88             ['d', 'box-sizing', 'border-box'],
89             ['e'],
90             );
91              
92 2         685 return;
93             }
94              
95             1;
96              
97             __END__
98              
99             =pod
100              
101             =encoding utf8
102              
103             =head1 NAME
104              
105             Tags::HTML::Element::Textarea - Tags helper for HTML textarea element.
106              
107             =head1 SYNOPSIS
108              
109             use Tags::HTML::Element::Textarea;
110              
111             my $obj = Tags::HTML::Element::Textarea->new(%params);
112             $obj->cleanup;
113             $obj->init($textarea);
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::Textarea->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 for L<process> processing.
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 textarea set by L<init>.
149              
150             Returns undef.
151              
152             =head2 C<init>
153              
154             $obj->init($textarea);
155              
156             Process initialization in page run.
157              
158             Accepted C<$textarea> is L<Data::HTML::Element::Textarea>.
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 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 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             Textarea object must be a 'Data::HTML::Element::Textarea' 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_textarea.pl
213              
214             use strict;
215             use warnings;
216              
217             use CSS::Struct::Output::Indent;
218             use Data::HTML::Element::Textarea;
219             use Tags::HTML::Element::Textarea;
220             use Tags::Output::Indent;
221              
222             # Object.
223             my $css = CSS::Struct::Output::Indent->new;
224             my $tags = Tags::Output::Indent->new(
225             'no_simple' => ['textarea'],
226             'preserved' => ['textarea'],
227             'xml' => 1,
228             );
229             my $obj = Tags::HTML::Element::Textarea->new(
230             'css' => $css,
231             'tags' => $tags,
232             );
233              
234             # Data object for textarea.
235             my $textarea = Data::HTML::Element::Textarea->new(
236             'cols' => 5,
237             'css_class' => 'textarea',
238             'id' => 'textarea',
239             'rows' => 10,
240             );
241              
242             # Initialize.
243             $obj->init($textarea);
244              
245             # Process textarea.
246             $obj->process;
247             $obj->process_css;
248              
249             # Print out.
250             print "HTML:\n";
251             print $tags->flush;
252             print "\n\n";
253             print "CSS:\n";
254             print $css->flush;
255              
256             # Output:
257             # HTML:
258             # <textarea class="textarea" id="textarea" cols="5" rows="10"></textarea>
259             #
260             # CSS:
261             # textarea.textarea {
262             # width: 100%;
263             # padding: 12px 20px;
264             # margin: 8px 0;
265             # display: inline-block;
266             # border: 1px solid #ccc;
267             # border-radius: 4px;
268             # box-sizing: border-box;
269             # }
270              
271             =head1 DEPENDENCIES
272              
273             L<Class::Utils>,
274             L<Error::Pure>,
275             L<Scalar::Util>,
276             L<Tags::HTML>,
277             L<Tags::HTML::Element::Utils>.
278              
279             =head1 REPOSITORY
280              
281             L<https://github.com/michal-josef-spacek/Tags-HTML-Element>
282              
283             =head1 AUTHOR
284              
285             Michal Josef Špaček L<mailto:skim@cpan.org>
286              
287             L<http://skim.cz>
288              
289             =head1 LICENSE AND COPYRIGHT
290              
291             © 2022-2024 Michal Josef Špaček
292              
293             BSD 2-Clause License
294              
295             =head1 VERSION
296              
297             0.15
298              
299             =cut