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

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