File Coverage

lib/HTML/Object/DOM/Element/Option.pm
Criterion Covered Total %
statement 79 85 92.9
branch 26 38 68.4
condition 4 9 44.4
subroutine 19 19 100.0
pod 7 7 100.0
total 135 158 85.4


line stmt bran cond sub pod time code
1             ##----------------------------------------------------------------------------
2             ## HTML Object - ~/lib/HTML/Object/DOM/Element/Option.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::Option;
15             BEGIN
16             {
17 2     2   1948 use strict;
  2         5  
  2         108  
18 2     2   26 use warnings;
  2         7  
  2         97  
19 2     2   11 use parent qw( HTML::Object::DOM::Element );
  2         4  
  2         15  
20 2     2   178 use vars qw( $VERSION );
  2         4  
  2         104  
21 2     2   14 use HTML::Object::DOM::Element::Shared qw( :option );
  2         15  
  2         284  
22 2     2   24 use Want;
  2         4  
  2         137  
23 2     2   53 our $VERSION = 'v0.2.0';
24             };
25              
26 2     2   11 use strict;
  2         13  
  2         86  
27 2     2   14 use warnings;
  2         3  
  2         1618  
28              
29             sub init
30             {
31 4     4 1 387 my $self = shift( @_ );
32 4         175 $self->{_init_strict_use_sub} = 1;
33 4 50       30 $self->SUPER::init( @_ ) || return( $self->pass_error );
34 4 50       23 $self->{tag} = 'option' if( !CORE::length( "$self->{tag}" ) );
35 4         44 return( $self );
36             }
37              
38             # Note: property defaultSelected
39             sub defaultSelected : lvalue { return( shift->_set_get_property({
40             attribute => 'selected',
41             callback => sub
42             {
43 12     12   25 my $self = shift( @_ );
44 12         23 my $attr = shift( @_ );
45 12 100       34 if( @_ )
46             {
47 1         2 my $value = shift( @_ );
48             # Whatever true value we received, we change it to empty string so this becomes:
49             # selected=""
50 1 50       6 $value = '' if( defined( $value ) );
51 1         7 $self->attr( $attr => $value );
52 1         8 $self->selected( $value );
53             # Will also reset our parent, if any
54 1         890 $self->reset(1);
55             }
56             else
57             {
58 11 100       41 return( $self->attributes->has( 'selected' ) ? 1 : 0 );
59             }
60             }
61 12     12 1 40863 }, @_ ) ); }
62              
63             # Note: property disabled is inherited
64              
65             # Note: property form read-only inherited
66              
67             # Note: property index read-only
68             sub index
69             {
70 1     1 1 683 my $self = shift( @_ );
71 1         5 my $parent = $self->parent;
72 1 50       28 return if( !$parent );
73 1 50       8 return if( !$parent->can( 'options' ) );
74 1         5 return( $parent->options->pos( $self ) );
75             }
76              
77             # Note: property label read-only
78             sub label : lvalue { return( shift->_lvalue({
79             set => sub
80             {
81 1     1   419 my $self = shift( @_ );
82 1         3 my $val = shift( @_ );
83             # User passed undef, so we remove all text from the option
84 1 50       4 if( !defined( $val ) )
85             {
86 0         0 $self->children->reset;
87 0         0 return( $self );
88             }
89 1 50 33     5 $val = $val->value if( $self->_is_a( $val => 'HTML::Object::DOM::Element::Text' ) || $self->_is_a( $val => 'HTML::Object::DOM::Element::Space' ) );
90 1 50 33     37 return( $self->error({
91             message => "Value provided ($val) is not a string.",
92             class => 'HTML::Object::TypeError',
93             }) ) if( ref( $val ) && !overload::Method( $val, '""' ) );
94 1         5 return( $self->attr( label => "$val" ) );
95             },
96             get => sub
97             {
98 2     2   1106 my $self = shift( @_ );
99 2         9 my $label = $self->attr( 'label' );
100 2 100 66     1124 if( defined( $label ) && CORE::length( "$label" ) )
101             {
102 1         20 return( $label );
103             }
104             else
105             {
106 1         4 return( $self->text );
107             }
108             }
109 3     3 1 3284 }, @_ ) ); }
110              
111             # Note: property selected
112             # Unintuitively enough, the one that affects the 'selected' attribute is the 'defaultSelected' method, not this 'selected' one ! This is just a boolean
113             sub selected : lvalue
114             {
115 2     2 1 915 my $self = shift( @_ );
116 2         5 my $has_arg = 0;
117 2         3 my $arg;
118 2 50       9 if( want( qw( LVALUE ASSIGN ) ) )
119             {
120 0         0 ( $arg ) = want( 'ASSIGN' );
121 0         0 $has_arg++;
122             }
123             else
124             {
125 2 100       207 if( @_ )
126             {
127 1         4 $arg = shift( @_ );
128 1         4 $has_arg++;
129             }
130             }
131             # If a value is provided, this will set the internal boolean value, but will not affect the DOM attribute 'selected'
132 2 100       6 if( $has_arg )
133             {
134 1         9 return( $self->_set_get_boolean( 'selected', $arg ) );
135             }
136             # If no value is provided, we return true if the DOM attribute exists, no matter its value, or false otherwise
137             else
138             {
139 1 50       6 my $val = $self->attributes->has( 'selected' ) ? 1 : 0;
140 1         559 return( $val );
141             }
142             }
143              
144             # Note: property text
145             # textContent is inherited from HTML::Object::DOM::Node and is also an lvalue method
146 5     5 1 2091 sub text : lvalue { return( shift->textContent( @_ ) ); }
147              
148             # Note: property value
149             sub value : lvalue
150             {
151 6     6 1 2400 my $self = shift( @_ );
152 6         12 my $has_arg = 0;
153 6         10 my $arg;
154 6 100       19 if( want( qw( LVALUE ASSIGN ) ) )
155             {
156 2         318 ( $arg ) = want( 'ASSIGN' );
157 2         239 $has_arg++;
158             }
159             else
160             {
161 4 50       592 if( @_ )
162             {
163 0         0 $arg = shift( @_ );
164 0         0 $has_arg++;
165             }
166             }
167 6 100       16 if( $has_arg )
168             {
169 2         11 return( $self->_set_get_property( 'value', $arg ) );
170             }
171             else
172             {
173 4 50       12 return( $self->attributes->has( 'value' ) ? $self->attr( 'value' ) : $self->textContent );
174             }
175             }
176              
177             1;
178             # NOTE: POD
179             __END__
180              
181             =encoding utf-8
182              
183             =head1 NAME
184              
185             HTML::Object::DOM::Element::Option - HTML Object DOM Option Class
186              
187             =head1 SYNOPSIS
188              
189             use HTML::Object::DOM::Element::Option;
190             my $opt = HTML::Object::DOM::Element::Option->new ||
191             die( HTML::Object::DOM::Element::Option->error, "\n" );
192              
193             =head1 VERSION
194              
195             v0.2.0
196              
197             =head1 DESCRIPTION
198              
199             This interface represents C<<option>> elements and inherits all properties and methods of the L<HTML::Object::Element> interface.
200              
201             =head1 INHERITANCE
202              
203             +-----------------------+ +---------------------------+ +-------------------------+ +----------------------------+ +------------------------------------+
204             | HTML::Object::Element | --> | HTML::Object::EventTarget | --> | HTML::Object::DOM::Node | --> | HTML::Object::DOM::Element | --> | HTML::Object::DOM::Element::Option |
205             +-----------------------+ +---------------------------+ +-------------------------+ +----------------------------+ +------------------------------------+
206              
207             =head1 PROPERTIES
208              
209             Inherits properties from its parent L<HTML::Object::DOM::Element>
210              
211             =head2 defaultSelected
212              
213             Has a value of either true or false that shows the initial value of the selected HTML attribute, indicating whether the option is selected by default or not.
214              
215             See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLOptionElement/defaultSelected>
216              
217             =head2 disabled
218              
219             Has a value of either true or false representing the value of the disabled HTML attribute, which indicates that the option is unavailable to be selected. An option can also be disabled if it is a child of an <optgroup> element that is disabled.
220              
221             See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLOptionElement/disabled>
222              
223             =head2 form
224              
225             Read-only.
226              
227             Is a L<HTML::Object::DOM::Element::Form> representing the same value as the form of the corresponding <select> element, if the option is a descendant of a <select> element, or C<undef> if none is found.
228              
229             See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLOptionElement/form>
230              
231             =head2 index
232              
233             Read-only.
234              
235             Is a long representing the position of the option within the list of options it belongs to, in tree-order. If the option is not part of a list of options, like when it is part of the C<datalist> element, the value is C<undef>.
236              
237             See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLOptionElement/index>
238              
239             =head2 label
240              
241             Read-only.
242              
243             Is a string that reflects the value of the label HTML attribute, which provides a label for the option. If this attribute is not specifically set, reading it returns the element's text content.
244              
245             See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLOptionElement/label>
246              
247             =head2 selected
248              
249             Has a value of either true or false that indicates whether the option is currently selected.
250              
251             See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLOptionElement/selected>
252              
253             =head2 text
254              
255             Is a string that contains the text content of the element.
256              
257             See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLOptionElement/text>
258              
259             =head2 value
260              
261             Is a string that reflects the value of the value HTML attribute, if it exists; otherwise reflects value of the Node.textContent property.
262              
263             See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLOptionElement/value>
264              
265             =head1 METHODS
266              
267             Inherits methods from its parent L<HTML::Object::DOM::Element>
268              
269             =head1 AUTHOR
270              
271             Jacques Deguest E<lt>F<jack@deguest.jp>E<gt>
272              
273             =head1 SEE ALSO
274              
275             L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLOptionElement>, L<Mozilla documentation on option element|https://developer.mozilla.org/en-US/docs/Web/HTML/Element/option>
276              
277             =head1 COPYRIGHT & LICENSE
278              
279             Copyright(c) 2021 DEGUEST Pte. Ltd.
280              
281             All rights reserved
282              
283             This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
284              
285             =cut