| 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__ |