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

filter: Statementstmt 0.0% 0 / 157 Branchbran 0.0% 0 / 50 Conditioncond 0.0% 0 / 46 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 28 Podpod 0.0% 0 / 21 totaltotal 0.0% 0 / 302 CC 26 SCAR 36.0
CC
26
Cov
0%
add
132.0
selectedIndex
42.0
remove
20.0
CRAP 36.7
1 ##----------------------------------------------------------------------------
2 ## HTML Object - ~/lib/HTML/Object/DOM/Element/Select.pm
3 ## Version v0.2.0
4 ## Copyright(c) 2021 DEGUEST Pte. Ltd.
5 ## Author: Jacques Deguest <jack@deguest.jp>
6 ## Created 2021/12/23
7 ## Modified 2022/09/18
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::Select;
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( :select );
23 our $VERSION = 'v0.2.0';
24 };
25  
26 use strict;
27 use warnings;
28  
29 sub init
30 {
31 my $self = shift( @_ );
32 $self->{_init_strict_use_sub} = 1;
33 $self->SUPER::init( @_ ) || return( $self->pass_error );
34 $self->{tag} = 'select' if( !CORE::length( "$self->{tag}" ) );
35 $self->{options} = [];
36 $self->{_select_reset} = 1;
37 my $callback = sub
38 {
39 my $def = shift( @_ );
40 # Our children were modified from outside our package.
41 # We need to check if it affects our rows and reset the cache accordingly
42 unless( $def->{caller}->[0] eq ref( $self ) ||
43 $def->{caller}->[0] eq 'HTML::Object::DOM::Element::Select' )
44 {
45 $self->reset(1);
46 }
47 return(1);
48 };
49 $self->children->callback( add => $callback );
50 $self->children->callback( remove => $callback );
51 return( $self );
52 }
53  
54 sub add
55 {
56 my $self = shift( @_ );
57 return( $self->error({
58 message => sprintf( "At least 1 argument is required, but only %d was provided.", scalar( @_ ) ),
59 class => 'HTML::Object::SyntaxError',
60 }) ) if( scalar( @_ ) < 1 );
61 my( $elem, $pos ) = @_;
62 return( $self->error({
63 message => "Element provided is neither a HTML::Object::DOM::Element::Option object nor a HTML::Object::DOM::Element::OptGroup object.",
64 class => 'HTML::Object::TypeError',
65 }) ) if( !$self->_is_a( $elem => 'HTML::Object::DOM::Element::Option' ) && !$self->_is_a( $elem => 'HTML::Object::DOM::Element::OptGroup' ) );
66 my $options = $self->options;
67 my $size = $options->size;
68 if( defined( $pos ) && CORE::length( $pos ) )
69 {
70 if( !$self->_is_a( $elem => 'HTML::Object::DOM::Element::Option' ) &&
71 !$self->_is_a( $elem => 'HTML::Object::DOM::Element::OptGroup' ) &&
72 !$self->_is_integer( $pos ) )
73 {
74 return( $self->error({
75 message => "The offset position before which to insert the element, if provided, must be an object or an integer, but I got '" . overload::StrVal( $pos ) . "'.",
76 class => 'HTML::Object::TypeError',
77 }) );
78 }
79 elsif( $self->_is_a( $elem => 'HTML::Object::DOM::Element::Option' ) ||
80 $self->_is_a( $elem => 'HTML::Object::DOM::Element::OptGroup' ) )
81 {
82 # Check if it is an ancestor, as per the specifications
83 my $lineage = $self->lineage;
84 my $lineagePos = $lineage->pos( $elem );
85 if( defined( $lineagePos ) )
86 {
87 return( $self->error({
88 message => "Element to be added to this select element is an ancestor.",
89 class => 'HTML::Object::HierarchyRequestError',
90 }) );
91 }
92
93 my $tmpPos = $options->pos( $elem );
94 if( defined( $tmpPos ) )
95 {
96 $pos = $tmpPos;
97 }
98 else
99 {
100 undef( $pos );
101 }
102 }
103 else
104 {
105 if( $pos < 0 )
106 {
107 $pos = $pos + $size;
108 }
109 elsif( $pos > $size )
110 {
111 undef( $pos );
112 }
113 }
114 }
115 $elem->detach;
116 $elem->parent( $self );
117 my $children = $self->children;
118 if( defined( $pos ) && CORE::length( $pos ) )
119 {
120 my $kid = $options->index( $pos );
121 my $real_pos = $children->pos( $kid );
122 $children->splice( $real_pos, 0, $elem );
123 }
124 else
125 {
126 $children->push( $elem );
127 }
128 $self->reset(1);
129 return( $self );
130 }
131  
132 # Note: property autofocus
133 sub autofocus : lvalue { return( shift->_set_get_property( 'autofocus', @_ ) ); }
134  
135 sub blur { return; }
136  
137 # Note: method checkValidity is inherited
138  
139 # Note: property disabled is inherited
140  
141 sub focus { return; }
142  
143 # Note: property form read-only is inherited
144  
145 sub item { return( shift->options->index( @_ ) ); }
146  
147 # Note: property labels read-only is inherited
148  
149 # Note: property length
150 sub length : lvalue { return( shift->_set_get_property( 'length', @_ ) ); }
151  
152 # Note: property multiple
153 sub multiple : lvalue { return( shift->_set_get_property( 'multiple', @_ ) ); }
154  
155 # Note: property name is inherited
156  
157 # Note: method namedItem is inherited from HTML::Object::DOM::Collection
158 sub namedItem { return( shift->options->namedItem( @_ ) ); }
159  
160 sub onchange : lvalue { return( shift->on( 'change', @_ ) ); }
161  
162 sub oninput : lvalue { return( shift->on( 'input', @_ ) ); }
163  
164 # Note: property options read-only
165 # sub options { return( shift->_set_get_object( 'options', 'HTML::Object::DOM::Element::OptionsCollection', @_ ) ); }
166 sub options
167 {
168 my $self = shift( @_ );
169 return( $self->{_select_options} ) if( $self->{_select_options} && !$self->_is_select_reset );
170 my $list = $self->children->grep(sub{ $self->_is_a( $_ => 'HTML::Object::DOM::Element::Option' ) });
171 # The content of the collection is refreshed, but the collection object itself does not change, so the user can poll it
172 unless( $self->{_select_options} )
173 {
174 $self->_load_class( 'HTML::Object::DOM::Element::OptionsCollection' ) || return( $self->pass_error );
175 $self->{_select_options} = HTML::Object::DOM::Element::OptionsCollection->new ||
176 return( $self->pass_error( HTML::Object::DOM::Element::OptionsCollection->error ) );
177 }
178 $self->{_select_options}->set( $list );
179 $self->_remove_select_reset;
180 return( $self->{_select_options} );
181 }
182  
183 sub remove
184 {
185 my $self = shift( @_ );
186 my $pos = shift( @_ );
187 return( $self->error({
188 message => "Index value provided ($pos) is not an integer",
189 class => 'HTML::Object::IndexSizeError',
190 }) ) if( !defined( $pos ) || !CORE::length( "$pos" ) || !$self->_is_integer( $pos ) );
191 my $options = $self->options;
192 return( $self->error({
193 message => "Index value provided ($pos) is greater than the zero-based total number of options.",
194 class => 'HTML::Object::IndexSizeError',
195 }) ) if( $pos > $options->size );
196 my $elem = $options->index( $pos );
197 my $real_pos = $self->children->pos( $elem );
198 return( $self->error({
199 message => "Could not find the real index position of element found at offset '$pos'.",
200 class => 'HTML::Object::HierarchyRequestError',
201 }) ) if( !defined( $real_pos ) );
202 $self->children->splice( $real_pos, 1 );
203 $elem->parent( undef );
204 $self->reset(1);
205 return( $elem );
206 }
207  
208 # Note: method reportValidity is inherited
209  
210 # Note: property required is inherited
211  
212 sub reset
213 {
214 my $self = shift( @_ );
215 if( scalar( @_ ) )
216 {
217 $self->_reset_select;
218 # Force the rebuilding of the collection of selected options
219 CORE::delete( $self->{_selected_options} );
220 CORE::delete( $self->{selectedindex} );
221 return( $self->SUPER::reset( @_ ) );
222 }
223 return( $self );
224 }
225  
226 # Note: property selectedIndex
227 # This is called by the HTML::Object::DOM::Element::Option when selected
228 # sub selectedIndex : lvalue { return( shift->_set_get_number( 'selectedindex', @_ ) ); }
229 sub selectedIndex : lvalue { return( shift->_lvalue({
230 set => sub
231 {
232 my( $self, $val ) = @_;
233 return( $self->error({
234 message => "Index value provided is not an integer.",
235 class => 'HTML::Object::TypeError',
236 }) ) if( !$self->_is_integer( $val ) );
237 my $options = $self->options;
238 return( $self->error({
239 message => "Index value provided is greater than the zero-based total number (" . $options->size . ") of options available.",
240 class => 'HTML::Object::IndexSizeError',
241 }) ) if( $val > $options->size );
242 $self->{selectedindex} = $val;
243 my $elem = $options->index( $val );
244 return( $self->error({
245 message => "Somehow, the element found at position $val is not an HTML::Object::DOM::Element::Option object.",
246 class => 'HTML::Object::HierarchyRequestError',
247 }) ) if( !$self->_is_a( $elem => 'HTML::Object::DOM::Element::Option' ) );
248 $elem->defaultSelected = 1;
249 return( $elem );
250 },
251 get => sub
252 {
253 my $self = shift( @_ );
254 return( $self->{selectedindex} ) if( $self->{selectedindex} && !$self->_is_select_reset );
255 # Get all options
256 my $options = $self->options;
257 # Get all selected ones; could be empty
258 my $selected = $self->selectedOptions;
259 return if( $selected->is_empty );
260 # Get the first selected one
261 my $elem = $selected->index(0);
262 # Find its index position among all options
263 my $pos = $options->pos( $elem );
264 # and return it.
265 return( $self->{selectedindex} = $pos );
266 },
267 }, @_ ) ); }
268  
269 # Note: property selectedOptions read-only
270 sub selectedOptions
271 {
272 my $self = shift( @_ );
273 return( $self->{_selected_options} ) if( $self->{_selected_options} && !$self->_is_select_reset );
274 my $list = $self->options->filter(sub{ $_->defaultSelected });
275 $self->children->for(sub
276 {
277 my( $i, $elem ) = @_;
278
279 });
280 # The content of the collection is refreshed, but the collection object itself does not change, so the user can poll it
281 unless( $self->{_selected_options} )
282 {
283 $self->_load_class( 'HTML::Object::DOM::Collection' ) || return( $self->pass_error );
284 $self->{_selected_options} = HTML::Object::DOM::Collection->new ||
285 return( $self->pass_error( HTML::Object::DOM::Collection->error ) );
286 }
287 $self->{_selected_options}->set( $list );
288 $self->_remove_select_reset;
289 return( $self->{_selected_options} );
290 }
291  
292 # Note: method setCustomValidity is inherited
293  
294 # Note: property size
295 sub size : lvalue { return( shift->_set_get_property( 'size', @_ ) ); }
296  
297 # Note: property type read-only is inherited
298  
299 # Note: property validationMessage read-only is inherited
300  
301 # Note: property validity read-only is inherited
302  
303 # Note: property value is inherited
304  
305 # Note: property willValidate read-only is inherited
306  
307 sub _is_select_reset { return( CORE::length( shift->{_select_reset} ) ); }
308  
309 sub _remove_select_reset { return( CORE::delete( shift->{_select_reset} ) ); }
310  
311 sub _reset_select
312 {
313 my $self = shift( @_ );
314 $self->{_select_reset}++;
315 # Force it to recompute
316 $self->options;
317 return( $self );
318 }
319  
320 1;
321 # NOTE: POD
322 __END__
323  
324 =encoding utf-8
325  
326 =head1 NAME
327  
328 HTML::Object::DOM::Element::Select - HTML Object DOM Select Class
329  
330 =head1 SYNOPSIS
331  
332 use HTML::Object::DOM::Element::Select;
333 my $select = HTML::Object::DOM::Element::Select->new ||
334 die( HTML::Object::DOM::Element::Select->error, "\n" );
335  
336 =head1 VERSION
337  
338 v0.2.0
339  
340 =head1 DESCRIPTION
341  
342 This interface represents a C<<select>> HTML Element. These elements also share all of the properties and methods of other HTML elements via the L<HTML::Object::DOM::Element> interface.
343  
344 =head1 INHERITANCE
345  
346 +-----------------------+ +---------------------------+ +-------------------------+ +----------------------------+ +------------------------------------+
347 | HTML::Object::Element | --> | HTML::Object::EventTarget | --> | HTML::Object::DOM::Node | --> | HTML::Object::DOM::Element | --> | HTML::Object::DOM::Element::Select |
348 +-----------------------+ +---------------------------+ +-------------------------+ +----------------------------+ +------------------------------------+
349  
350 =head1 PROPERTIES
351  
352 Inherits properties from its parent L<HTML::Object::DOM::Element>
353  
354 =head2 autofocus
355  
356 A boolean value reflecting the autofocus L<HTML attribute|HTML::Object::DOM::Attribute>, which indicates whether the control should have input focus when the page loads, unless the user overrides it, for example by typing in a different control. Only one form-associated element in a document can have this attribute specified.
357  
358 Example:
359  
360 <select id="mySelect" autofocus>
361 <option>Option 1</option>
362 <option>Option 2</option>
363 </select>
364  
365 # Check if the autofocus attribute on the <select>
366 my $hasAutofocus = $doc->getElementById('mySelect')->autofocus;
367  
368 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLSelectElement/autofocus>
369  
370 =head2 disabled
371  
372 A boolean value reflecting the disabled L<HTML attribute|HTML::Object::DOM::Attribute>, which indicates whether the control is disabled. If it is disabled, it does not accept clicks.
373  
374 Example:
375  
376 <label>
377 Allow drinks?
378 <input id="allow-drinks" type="checkbox" />
379 </label>
380  
381 <label for="drink-select">Drink selection:</label>
382 <select id="drink-select" disabled>
383 <option value="1">Water</option>
384 <option value="2">Beer</option>
385 <option value="3">Pepsi</option>
386 <option value="4">Whisky</option>
387 </select>
388  
389 my $allowDrinksCheckbox = $doc->getElementById( 'allow-drinks' );
390 my $drinkSelect = $doc->getElementById( 'drink-select' );
391  
392 $allowDrinksCheckbox->addEventListener( change => sub
393 {
394 if( $event->target->checked )
395 {
396 $drinkSelect->disabled = 0; # false
397 }
398 else
399 {
400 $drinkSelect->disabled = 1; # true
401 }
402 }, { capture => 0});
403  
404 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLSelectElement/disabled>
405  
406 =head2 form
407  
408 Read-only.
409  
410 An L<HTML::Object::DOM::Element::Form> referencing the form that this element is associated with. If the element is not associated with of a <form> element, then it returns C<undef>.
411  
412 Example:
413  
414 <form id="pet-form">
415 <label for="pet-select">Choose a pet</label>
416 <select name="pets" id="pet-select">
417 <option value="dog">Dog</option>
418 <option value="cat">Cat</option>
419 <option value="parrot">Parrot</option>
420 </select>
421  
422 <button type="submit">Submit</button>
423 </form>
424  
425 <label for="lunch-select">Choose your lunch</label>
426 <select name="lunch" id="lunch-select">
427 <option value="salad">Salad</option>
428 <option value="sandwich">Sandwich</option>
429 </select>
430  
431 my $petSelect = $doc->getElementById( 'pet-select' );
432 my $petForm = $petSelect->form; # <form id="pet-form">
433  
434 my $lunchSelect = $doc->getElementById( 'lunch-select' );
435 my $lunchForm = $lunchSelect->form; # undef
436  
437 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLSelectElement/form>
438  
439 =head2 labels
440  
441 Read-only.
442  
443 A L<HTML::Object::DOM::NodeList> of L<label elements|HTML::Object::DOM::Element::Label> associated with the element.
444  
445 Example:
446  
447 <label id="label1" for="test">Label 1</label>
448 <select id="test">
449 <option value="1">Option 1</option>
450 <option value="2">Option 2</option>
451 </select>
452 <label id="label2" for="test">Label 2</label>
453  
454 window->addEventListener( DOMContentLoaded => sub
455 {
456 my $select = $doc->getElementById( 'test' );
457 for( my $i = 0; $i < $select->labels->length; $i++ )
458 {
459 say( $select->labels->[$i]->textContent ); # "Label 1" and "Label 2"
460 }
461 });
462  
463 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLSelectElement/labels>
464  
465 =head2 length
466  
467 An unsigned long that reflects the number of L<option elements|HTML::Object::DOM::Element::Option> in this select element.
468  
469 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLSelectElement/length>
470  
471 =head2 multiple
472  
473 A boolean value reflecting the multiple L<HTML attribute|HTML::Object::DOM::Attribute>, which indicates whether multiple items can be selected.
474  
475 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLSelectElement/multiple>
476  
477 =head2 name
478  
479 A string reflecting the name L<HTML attribute|HTML::Object::DOM::Attribute>, containing the name of this control used by servers and DOM search functions.
480  
481 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLSelectElement/name>
482  
483 =head2 options
484  
485 Read-only.
486  
487 An L<OptionsCollection|HTML::Object::DOM::Element::OptionsCollection> representing the set of L<option elements|HTML::Object::DOM::Element::Option> contained by this element.
488  
489 Example:
490  
491 <label for="test">Label</label>
492 <select id="test">
493 <option value="1">Option 1</option>
494 <option value="2">Option 2</option>
495 </select>
496  
497 window->addEventListener( DOMContentLoaded => sub
498 {
499 my $select = $doc->getElementById( 'test' );
500 for( my $i = 0; $i < $select->options->length; $i++ )
501 {
502 say( $select->options->[$i]->label ); # "Option 1" and "Option 2"
503 }
504 });
505  
506 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLSelectElement/options>
507  
508 =head2 required
509  
510 A boolean value reflecting the required L<HTML attribute|HTML::Object::DOM::Attribute>, which indicates whether the user is required to select a value before submitting the form.
511  
512 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLSelectElement/required>
513  
514 =head2 selectedIndex
515  
516 A long reflecting the index of the first selected L<option element|HTML::Object::DOM::Element::Option>. The value C<undef> indicates no element is selected.
517  
518 Example:
519  
520 <p id="p">selectedIndex: 0</p>
521 <select id="select">
522 <option selected>Option A</option>
523 <option>Option B</option>
524 <option>Option C</option>
525 <option>Option D</option>
526 <option>Option E</option>
527 </select>
528  
529 my $selectElem = $doc->getElementById('select');
530 my $pElem = $doc->getElementById('p');
531  
532 # When a new <option> is selected
533 $selectElem->addEventListener( change => sub
534 {
535 my $index = $selectElem->selectedIndex;
536 # Add that data to the <p>
537 $pElem->innerHTML = 'selectedIndex: ' . $index;
538 })
539  
540 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLSelectElement/selectedIndex>
541  
542 =head2 selectedOptions
543  
544 Read-only.
545  
546 An L<Collection|HTML::Object::DOM::Collection> representing the set of L<option elements|HTML::Object::DOM::Element::Option> that are selected.
547  
548 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLSelectElement/selectedOptions>
549  
550 =head2 size
551  
552 A long reflecting the size L<HTML attribute|HTML::Object::DOM::Attribute>, which contains the number of visible items in the control. The default is 1, unless multiple is true, in which case it is 4.
553  
554 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLSelectElement/size>
555  
556 =head2 type
557  
558 Read-only.
559  
560 A string represeting the form control's type. When multiple is true, it returns C<select-multiple>; otherwise, it returns C<select-one>.
561  
562 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLSelectElement/type>
563  
564 =head2 validationMessage
565  
566 Read-only.
567  
568 A string representing a localized message that describes the validation constraints that the control does not satisfy (if any). This attribute is the empty string if the control is not a candidate for constraint validation (willValidate is false), or it satisfies its constraints.
569  
570 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLSelectElement/validationMessage>
571  
572 =head2 validity
573  
574 Read-only.
575  
576 A L<ValidityState|HTML::Object::DOM::ValidityState> object reflecting the validity state that this control is in.
577  
578 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLSelectElement/validity>
579  
580 =head2 value
581  
582 A string reflecting the value of the form control. Returns the value property of the first selected option element if there is one, otherwise the empty string.
583  
584 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLSelectElement/value>
585  
586 =head2 willValidate
587  
588 Read-only.
589  
590 A boolean value that indicates whether the button is a candidate for constraint validation. It is false if any conditions bar it from constraint validation.
591  
592 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLSelectElement/willValidate>
593  
594 =head1 METHODS
595  
596 Inherits methods from its parent L<HTML::Object::DOM::Element>
597  
598 =head2 add
599  
600 Provided with an C<item> and optionally an position offset C<before> which to insert the item and this adds an element to the collection of option elements for this select element.
601  
602 =over 4
603  
604 =item C<item> is an L<Option element|HTML::Object::DOM::Element::Option> or L<OptGroup element|HTML::Object::DOM::Element::OptGroup>
605  
606 =item C<before> is optional and an element of the collection, or an index of type long, representing the item should be inserted before. If this parameter is C<undef> (or the index does not exist), the new element is appended to the end of the collection.
607  
608 =back
609  
610 Example:
611  
612 my $sel = $doc->createElement( 'select' );
613 my $opt1 = $doc->createElement( 'option' );
614 my $opt2 = $doc->createElement( 'option' );
615  
616 $opt1->value = 1;
617 $opt1->text = "Option: Value 1";
618  
619 $opt2->value = 2;
620 $opt2->text = "Option: Value 2";
621  
622 # No second argument; no 'before' argument
623 $sel->add( $opt1, undef );
624 # Equivalent to above
625 $sel->add( $opt2 );
626  
627 Produces the following, conceptually:
628  
629 <select>
630 <option value="1">Option: Value 1</option>
631 <option value="2">Option: Value 2</option>
632 </select>
633  
634 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLSelectElement/add>
635  
636 =head2 blur
637  
638 Under perl, of course, this does nothing.
639  
640 Under JavaScript, this removes the input focus from this element. This method is now implemented on L<HTML::Object::DOM::Element>.
641  
642 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLSelectElement/blur>
643  
644 =head2 checkValidity
645  
646 Checks whether the element has any constraints and whether it satisfies them. If the element fails its constraints, the browser fires a cancelable invalid event at the element (and returns false).
647  
648 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLSelectElement/checkValidity>
649  
650 =head2 focus
651  
652 Under perl, of course, this does nothing.
653  
654 Under JavaScript, this gives input focus to this element. This method is now implemented on L<HTML::Object::DOM::Element>.
655  
656 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLSelectElement/focus>
657  
658 =head2 item
659  
660 Gets an item from the options collection for this L<select element|HTML::Object::DOM::Select> by providing a zero-based index position.
661  
662 Example:
663  
664 <select id="myFormControl">
665 <option id="o1">Opt 1</option>
666 <option id="o2">Opt 2</option>
667 </select>
668  
669 # Returns the OptionElement representing #o2
670 my $sel = $doc->getElementById( 'myFormControl' );
671 my $elem1 = $sel->item(1); # Opt 2
672  
673 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLSelectElement/item>
674  
675 =head2 namedItem
676  
677 Gets the item in the options collection with the specified name. The name string can match either the id or the name attribute of an option node.
678  
679 Example:
680  
681 <select id="myFormControl">
682 <option id="o1">Opt 1</option>
683 <option id="o2">Opt 2</option>
684 </select>
685  
686 my $elem1 = $doc->getElementById( 'myFormControl' )->namedItem( 'o1' ); # Returns the OptionElement representing #o1
687  
688 This is, in effect, a shortcut for C<$select->options->namedItem>
689  
690 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLSelectElement/namedItem>
691  
692 =head2 remove
693  
694 Removes the element at the specified index (zero-based) from the options collection for this select element and return it.
695  
696 Example:
697  
698 <select id="existingList" name="existingList">
699 <option value="1">Option: Value 1</option>
700 <option value="2">Option: Value 2</option>
701 <option value="3">Option: Value 3</option>
702 </select>
703  
704 my $sel = $doc->getElementById( 'existingList' );
705 my $removed = $sel->remove(1);
706  
707 HTML is now:
708  
709 <select id="existingList" name="existingList">
710 <option value="1">Option: Value 1</option>
711 <option value="3">Option: Value 3</option>
712 </select>
713  
714 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLSelectElement/remove>
715  
716 =head2 reportValidity
717  
718 This method reports the problems with the constraints on the element, if any, to the user. If there are problems, it fires a cancelable invalid event at the element, and returns false; if there are no problems, it returns true.
719  
720 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLSelectElement/reportValidity>
721  
722 =head2 setCustomValidity
723  
724 Sets the custom validity message for the selection element to the specified message. Use the empty string to indicate that the element does not have a custom validity error.
725  
726 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLSelectElement/setCustomValidity>
727  
728 =head1 EVENTS
729  
730 Event listeners for those events can also be found by prepending C<on> before the event type:
731  
732 C<click> event listeners can be set also with C<onclick> method:
733  
734 $e->onclick(sub{ # do something });
735 # or as an lvalue method
736 $e->onclick = sub{ # do something };
737  
738 Note that, under perl, almost no event are fired, but you can trigger them yourself.
739  
740 =head2 change
741  
742 Fires when the user selects an option, but since there is no user interaction, this event is fired when the C<selectedIndex> value changes, which you can change yourself.
743  
744 Example:
745  
746 <label>Choose an ice cream flavor:
747 <select class="ice-cream" name="ice-cream">
748 <option value="">Select One …</option>
749 <option value="chocolate">Chocolate</option>
750 <option value="sardine">Sardine</option>
751 <option value="vanilla">Vanilla</option>
752 </select>
753 </label>
754  
755 <div class="result"></div>
756  
757 my $selectElement = $doc->querySelector('.ice-cream');
758  
759 $selectElement->addEventListener( change => sub
760 {
761 my $result = $doc->querySelector( '.$result' );
762 $result->textContent = "You like " . $event->target->value;
763 });
764  
765 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/change_event>
766  
767 =head2 input
768  
769 Fires when the value of an <input>, <select>, or <textarea> element has been changed.
770  
771 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/input_event>
772  
773 =head2 reset
774  
775 Reset the cache flag so that some data will be recomputed. The cache is design to avoid doing useless computing repeatedly when there is no change of data.
776  
777 =head1 AUTHOR
778  
779 Jacques Deguest E<lt>F<jack@deguest.jp>E<gt>
780  
781 =head1 SEE ALSO
782  
783 L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLSelectElement>, L<Mozilla documentation on select element|https://developer.mozilla.org/en-US/docs/Web/HTML/Element/select>
784  
785 =head1 COPYRIGHT & LICENSE
786  
787 Copyright(c) 2021 DEGUEST Pte. Ltd.
788  
789 All rights reserved
790  
791 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
792  
793 =cut