blib/lib/HTML/Object/DOM/Element/Form.pm untested

filter: Statementstmt 0.0% 0 / 54 Branchbran 0.0% 0 / 2 Conditioncond 0.0% 0 / 2 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 19 Podpod 0.0% 0 / 12 totaltotal 0.0% 0 / 89 CC 2 SCAR 8.8
CC
2
Cov
0%
init
6.0
BEGIN
2.0
acceptCharset
2.0
CRAP 2.4
1 ##----------------------------------------------------------------------------
2 ## HTML Object - ~/lib/HTML/Object/DOM/Element/Form.pm
3 ## Version v0.2.1
4 ## Copyright(c) 2022 DEGUEST Pte. Ltd.
5 ## Author: Jacques Deguest <jack@deguest.jp>
6 ## Created 2021/12/23
7 ## Modified 2024/04/30
8 ## All rights reserved
9 ##
10 ##
11 ## This program is free software; you can redistribute it and/or modify it
12 ## under the same terms as Perl itself.
13 ##----------------------------------------------------------------------------
14 package HTML::Object::DOM::Element::Form;
15 BEGIN
16 {
17 use strict;
18 use warnings;
19 warnings::register_categories( 'HTML::Object' );
20 use parent qw( HTML::Object::DOM::Element );
21 use vars qw( $VERSION );
22 use HTML::Object::DOM::Element::Shared qw( :form );
23 our $VERSION = 'v0.2.1';
24 };
25  
26 use strict;
27 use warnings;
28  
29 sub init
30 {
31 my $self = shift( @_ );
32 $self->{checkValidity} = 1;
33 $self->{reportValidity} = 1;
34 $self->{_init_strict_use_sub} = 1;
35 $self->SUPER::init( @_ ) || return( $self->pass_error );
36 $self->{tag} = 'form' if( !CORE::length( "$self->{tag}" ) );
37 return( $self );
38 }
39  
40 # Note: property attribute
41 sub acceptCharset : lvalue { return( shift->_set_get_property( 'accept-charset', @_ ) ); }
42  
43 # Note: property attribute
44 sub action : lvalue { return( shift->_set_get_property( 'action', @_ ) ); }
45  
46 # Note: property accept inherited
47  
48 # Note: property autocapitalize inherited
49  
50 # Note: method checkValidity inherited
51  
52 # Note: property read-only
53 # Same as in HTML::Object::DOM::FieldSet
54 sub elements
55 {
56 my $self = shift( @_ );
57 my $children = $self->children;
58 # my $form_elements = $self->new_array( [qw( button datalist fieldset input label legend meter optgroup option output progress select textarea )] );
59 # <https://developer.mozilla.org/en-US/docs/Web/API/HTMLFormElement/elements#value>
60 my $form_elements = $self->new_array( [qw( button fieldset input object output select textarea )] );
61 my $list = $form_elements->as_hash;
62 my $results = $children->grep(sub{ exists( $form_elements->{ $_->tag } ) });
63 my $col = $self->new_collection_elements;
64 $col->push( $results->list );
65 return( $col );
66 }
67  
68 # Note: property
69 sub encoding : lvalue { return( shift->_set_get_property( 'encoding', @_ ) ); }
70  
71 # Note: property
72 sub length : lvalue { return( shift->_set_get_property( 'length', @_ ) ); }
73  
74 # Note: property
75 sub method : lvalue { return( shift->_set_get_property( 'method', @_ ) ); }
76  
77 # Note: property name inherited
78  
79 # Note: property
80 sub noValidate : lvalue { return( shift->_set_get_property( { attribute => 'novalidate', is_boolean => 1 }, @_ ) ); }
81  
82 sub requestSubmit { return( shift->_set_get_object_without_init( 'requestSubmit', 'HTML::Object::DOM::Element', @_ ) ); }
83  
84 # Note: method reportValidity inherited
85  
86 sub submit { return; }
87  
88 # Note: property target
89 sub target : lvalue { return( shift->_set_get_property( { attribute => 'target', is_uri => 1 }, @_ ) ); }
90  
91 1;
92 # NOTE: POD
93 __END__
94  
95 =encoding utf-8
96  
97 =head1 NAME
98  
99 HTML::Object::DOM::Element::Form - HTML Object DOM Form Class
100  
101 =head1 SYNOPSIS
102  
103 use HTML::Object::DOM::Element::Form;
104 my $form = HTML::Object::DOM::Element::Form->new ||
105 die( HTML::Object::DOM::Element::Form->error, "\n" );
106  
107 =head1 VERSION
108  
109 v0.2.1
110  
111 =head1 DESCRIPTION
112  
113 This interface represents a <form> element in the DOM. It allows access to-and, in some cases, modification of-aspects of the form, as well as access to its component elements.
114  
115 =head1 INHERITANCE
116  
117 +-----------------------+ +---------------------------+ +-------------------------+ +----------------------------+ +----------------------------------+
118 | HTML::Object::Element | --> | HTML::Object::EventTarget | --> | HTML::Object::DOM::Node | --> | HTML::Object::DOM::Element | --> | HTML::Object::DOM::Element::Form |
119 +-----------------------+ +---------------------------+ +-------------------------+ +----------------------------+ +----------------------------------+
120  
121 =head1 PROPERTIES
122  
123 Inherits properties from its parent L<HTML::Object::DOM::Element>
124  
125 =head2 acceptCharset
126  
127 A string reflecting the value of the form's accept-charset HTML attribute, representing the character encoding that the server accepts.
128  
129 Example:
130  
131 <form action="/some/where" accept-charset="utf-8">
132 <button>Ok</button>
133 </form>
134  
135 my $inputs = $doc->forms->[0]->acceptCharset; # utf-8
136  
137 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLFormElement/acceptCharset>
138  
139 =head2 action
140  
141 A string reflecting the value of the form's action HTML attribute, containing the URI of a program that processes the information submitted by the form.
142  
143 Example:
144  
145 var $form = $doc->forms->[0];
146 $form->action = '/cgi-bin/publish';
147  
148 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLFormElement/action>
149  
150 =head2 autocomplete
151  
152 A string (C<on> or C<off>) reflecting the value of the form's autocomplete HTML attribute, indicating whether the controls in this form can have their values automatically populated by the browser.
153  
154 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLFormElement/autocomplete>
155  
156 =head2 elements
157  
158 Read-only.
159  
160 A L<collection object|HTML::Object::DOM::Collection> holding all L<form controls|https://developer.mozilla.org/en-US/docs/Web/API/HTMLFormElement/elements> belonging to this form element.
161  
162 Example:
163  
164 <form id="my-form">
165 <input type="text" name="username">
166 <input type="text" name="full-name">
167 <input type="password" name="password">
168 </form>
169  
170 my $inputs = $doc->getElementById("my-form")->elements;
171 my $inputByIndex = $inputs->[0];
172 my $inputByName = $inputs->username;
173  
174 Another example:
175  
176 my $inputs = $doc->getElementById("my-form")->elements;
177  
178 # Iterate over the form controls
179 for( my $i = 0; $i < $inputs->length; $i++ )
180 {
181 if( $inputs->[i]->nodeName == "INPUT" && $inputs->[i]->type == "text" )
182 {
183 # Update text input
184 $inputs->[i]->value->toLocaleUpperCase();
185 }
186 }
187  
188 Another example:
189  
190 my $inputs = $doc->getElementById("my-form")->elements;
191  
192 # Iterate over the form controls
193 for( my $i = 0; $i < $inputs->length; $i++ )
194 {
195 # Disable all form controls
196 $inputs->[$i]->setAttribute( "disabled", "" );
197 }
198  
199 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLFormElement/elements>, L<StackOverflow question|https://stackoverflow.com/questions/34785743/javascript-about-form-elements>
200  
201 =head2 encoding
202  
203 A string reflecting the value of the form's enctype HTML attribute, indicating the type of content that is used to transmit the form to the server. Only specified values can be set. The two properties are synonyms.
204  
205 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLFormElement/encoding>
206  
207 =head2 length
208  
209 Read-only.
210  
211 A long reflecting the number of controls in the form.
212  
213 Example:
214  
215 if( $doc->getElementById('form1')->length > 1 )
216 {
217 # more than one form control here
218 }
219  
220 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLFormElement/length>
221  
222 =head2 method
223  
224 A string reflecting the value of the form's method HTML attribute, indicating the HTTP method used to submit the form. Only specified values can be set.
225  
226 Example:
227  
228 $doc->forms->myform->method = 'post';
229  
230 my $formElement = $doc->createElement("form"); # Create a form
231 $doc->body->appendChild( $formElement );
232 say( $formElement->method ); # 'get'
233  
234 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLFormElement/method>
235  
236 =head2 name
237  
238 A string reflecting the value of the form's name HTML attribute, containing the name of the form.
239  
240 Example:
241  
242 my $string = form->name;
243 form->name = $string;
244  
245 my $form1name = $doc->getElementById('form1').name;
246  
247 if ($form1name != $doc->form->form1) {
248 # Browser does not support this form of reference
249 }
250  
251 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLFormElement/name>
252  
253 =head2 noValidate
254  
255 A boolean value reflecting the value of the form's novalidate HTML attribute, indicating whether the form should not be validated.
256  
257 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLFormElement/noValidate>
258  
259 =head2 target
260  
261 A URL reflecting the value of the form's target HTML attribute, indicating where to display the results received from submitting the form.
262  
263 Example:
264  
265 $myForm->target = $doc->frames->[1]->name;
266  
267 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLFormElement/target>
268  
269 =head1 METHODS
270  
271 Inherits methods from its parent L<HTML::Object::DOM::Element>
272  
273 =head2 checkValidity
274  
275 In perl, this always returns true, or whatever value you would have set.
276  
277 In JavaScript environment, this returns true if the element's child controls are subject to constraint validation and satisfy those constraints; returns false if some controls do not satisfy their constraints. Fires an event named invalid at any control that does not satisfy its constraints; such controls are considered invalid if the event is not canceled. It is up to the programmer to decide how to respond to false.
278  
279 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLFormElement/checkValidity>
280  
281 =head2 reportValidity
282  
283 In perl, this always returns true, or whatever value you would have set.
284  
285 In JavaScript environment, this returns true if the element's child controls satisfy their validation constraints. When false is returned, cancelable invalid events are fired for each invalid child and validation problems are reported to the user.
286  
287 Example:
288  
289 $doc->forms->myform->addEventListener( submit => sub
290 {
291 $doc->forms->myform->reportValidity();
292 }, { capture => 0 });
293  
294 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLFormElement/reportValidity>
295  
296 =head2 requestSubmit
297  
298 Requests that the form be submitted using the specified L<submit button|HTML::Object::DOM::Element> object and its corresponding configuration.
299  
300 Example:
301  
302 my $myForm = $doc->querySelector( 'form' );
303 my $submitButton = $myForm->querySelector( '#main-submit' );
304  
305 if( $myForm->requestSubmit )
306 {
307 if( $submitButton )
308 {
309 $myForm->requestSubmit( $submitButton );
310 }
311 else
312 {
313 $myForm->requestSubmit();
314 }
315 }
316 else
317 {
318 $myForm->submit();
319 }
320  
321 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLFormElement/requestSubmit>
322  
323 =head2 submit
324  
325 This does nothing and returns C<undef> under perl environment.
326  
327 In JavaScript environment, this submits the form to the server.
328  
329 Example:
330  
331 $doc->forms->myform->submit();
332  
333 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLFormElement/submit>
334  
335 =head1 AUTHOR
336  
337 Jacques Deguest E<lt>F<jack@deguest.jp>E<gt>
338  
339 =head1 SEE ALSO
340  
341 L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLFormElement>, L<Mozilla documentation on form element|https://developer.mozilla.org/en-US/docs/Web/HTML/Element/form>
342  
343 =head1 COPYRIGHT & LICENSE
344  
345 Copyright(c) 2021 DEGUEST Pte. Ltd.
346  
347 All rights reserved
348  
349 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
350  
351 =cut