| 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 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | 2 | ▶ | warnings::register_categories( 'HTML::Object' ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | 2 | ▶ | use parent qw( HTML::Object::DOM::Element ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | 2 | ▶ | use vars qw( $VERSION ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 22 | 2 | ▶ | use HTML::Object::DOM::Element::Shared qw( :option ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 23 | 2 | ▶ | use Wanted; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 24 | 2 | our $VERSION = 'v0.3.0'; | |||||||||||||||||||||||||||
| 25 | }; | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 28 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | sub init | ||||||||||||||||||||||||||||
| 31 | { | ||||||||||||||||||||||||||||
| 32 | 4 | ▶ | my $self = shift( @_ ); | ||||||||||||||||||||||||||
|
Subroutine
init: called CC=3 SCAR=11.3
Pod
documented
| |||||||||||||||||||||||||||||
| 33 | 4 | $self->{_init_strict_use_sub} = 1; | |||||||||||||||||||||||||||
| 34 | 4 | ▶ | $self->SUPER::init( @_ ) || return( $self->pass_error ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 35 | 4 | ▶ | $self->{tag} = 'option' if( !CORE::length( "$self->{tag}" ) ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 36 | 4 | 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 | 12 | ▶ | my $self = shift( @_ ); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 45 | 12 | my $attr = shift( @_ ); | |||||||||||||||||||||||||||
| 46 | 12 | ▶ | if( @_ ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 47 | { | ||||||||||||||||||||||||||||
| 48 | 1 | my $value = shift( @_ ); | |||||||||||||||||||||||||||
| 49 | # Whatever true value we received, we change it to empty string so this becomes: | ||||||||||||||||||||||||||||
| 50 | # selected="" | ||||||||||||||||||||||||||||
| 51 | 1 | ▶ | $value = '' if( defined( $value ) ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | 1 | $self->attr( $attr => $value ); | |||||||||||||||||||||||||||
| 53 | 1 | $self->selected( $value ); | |||||||||||||||||||||||||||
| 54 | # Will also reset our parent, if any | ||||||||||||||||||||||||||||
| 55 | 1 | $self->reset(1); | |||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | else | ||||||||||||||||||||||||||||
| 58 | { | ||||||||||||||||||||||||||||
| 59 | 11 | ▶ | return( $self->attributes->has( 'selected' ) ? 1 : 0 ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | 12 | ▶ | }, @_ ) ); } | ||||||||||||||||||||||||||
|
Subroutine
defaultSelected: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 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 | 1 | ▶ | my $self = shift( @_ ); | ||||||||||||||||||||||||||
|
Subroutine
index: called CC=3 SCAR=11.3
Pod
documented
| |||||||||||||||||||||||||||||
| 72 | 1 | my $parent = $self->parent; | |||||||||||||||||||||||||||
| 73 | 1 | ▶ | return if( !$parent ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | 1 | ▶ | return if( !$parent->can( 'options' ) ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | 1 | 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 | 1 | ▶ | my $self = shift( @_ ); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=6 SCAR=22.3
| |||||||||||||||||||||||||||||
| 83 | 1 | my $val = shift( @_ ); | |||||||||||||||||||||||||||
| 84 | # User passed undef, so we remove all text from the option | ||||||||||||||||||||||||||||
| 85 | 1 | ▶ | if( !defined( $val ) ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 86 | { | ||||||||||||||||||||||||||||
| 87 | 0 | $self->children->reset; | |||||||||||||||||||||||||||
| 88 | 0 | return( $self ); | |||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | 1 | ▶ | $val = $val->value if( $self->_is_a( $val => 'HTML::Object::DOM::Element::Text' ) || $self->_is_a( $val => 'HTML::Object::DOM::Element::Space' ) ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$self->_is_a($val, "HTML::Object::DOM::Element::Text") or $self->_is_a($val, "HTML::Object::DOM::Element::Space")
MC/DC0%0/2
$self->_is_a($val, "HTML::Object::DOM::Element::Text") or $self->_is_a($val, "HTML::Object::DOM::Element::Space")
$self->_is_a($val, "HTML::Object::DOM::Element::Text")
$self->_is_a($val, "HTML::Object::DOM::Element::Space")
Truth table33%1/3
A or B
A= $self->_is_a($val, "HTML::Object::DOM::Element::Text")B= $self->_is_a($val, "HTML::Object::DOM::Element::Space")
| |||||||||||||||||||||||||||||
| 91 | 1 | ▶ | return( $self->error({ | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
ref $val and not overload::Method($val, "\"\"")
MC/DC0%0/2
ref $val and not overload::Method($val, "\"\"")
ref $val
not overload::Method($val, "\"\"")
Truth table33%1/3
A and not(B)
A= ref $valB= not overload::Method($val, "\"\"")
| |||||||||||||||||||||||||||||
| 92 | message => "Value provided ($val) is not a string.", | ||||||||||||||||||||||||||||
| 93 | class => 'HTML::Object::TypeError', | ||||||||||||||||||||||||||||
| 94 | }) ) if( ref( $val ) && !overload::Method( $val, '""' ) ); | ||||||||||||||||||||||||||||
| 95 | 1 | return( $self->attr( label => "$val" ) ); | |||||||||||||||||||||||||||
| 96 | }, | ||||||||||||||||||||||||||||
| 97 | get => sub | ||||||||||||||||||||||||||||
| 98 | { | ||||||||||||||||||||||||||||
| 99 | 2 | ▶ | my $self = shift( @_ ); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 100 | 2 | my $label = $self->attr( 'label' ); | |||||||||||||||||||||||||||
| 101 | 2 | ▶ | if( defined( $label ) && CORE::length( "$label" ) ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $label and length "$label"
MC/DC50%1/2
defined $label and length "$label"
defined $label
length "$label"
Truth table66%2/3
A and B
A= defined $labelB= length "$label"
| |||||||||||||||||||||||||||||
| 102 | { | ||||||||||||||||||||||||||||
| 103 | 1 | return( $label ); | |||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | else | ||||||||||||||||||||||||||||
| 106 | { | ||||||||||||||||||||||||||||
| 107 | 1 | return( $self->text ); | |||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | 3 | ▶ | }, @_ ) ); } | ||||||||||||||||||||||||||
|
Subroutine
label: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 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 | 2 | ▶ | my $self = shift( @_ ); | ||||||||||||||||||||||||||
|
Subroutine
selected: called CC=5 SCAR=16.4
Pod
documented
| |||||||||||||||||||||||||||||
| 117 | 2 | my $has_arg = 0; | |||||||||||||||||||||||||||
| 118 | 2 | my $arg; | |||||||||||||||||||||||||||
| 119 | 2 | ▶ | if( want( qw( LVALUE ASSIGN ) ) ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 120 | { | ||||||||||||||||||||||||||||
| 121 | 0 | ( $arg ) = want( 'ASSIGN' ); | |||||||||||||||||||||||||||
| 122 | 0 | $has_arg++; | |||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||
| 124 | else | ||||||||||||||||||||||||||||
| 125 | { | ||||||||||||||||||||||||||||
| 126 | 2 | ▶ | if( @_ ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 127 | { | ||||||||||||||||||||||||||||
| 128 | 1 | $arg = shift( @_ ); | |||||||||||||||||||||||||||
| 129 | 1 | $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 | 2 | ▶ | if( $has_arg ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 134 | { | ||||||||||||||||||||||||||||
| 135 | 1 | 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 | 1 | ▶ | my $val = $self->attributes->has( 'selected' ) ? 1 : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 141 | 1 | 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 | 5 | ▶ | sub text : lvalue { return( shift->textContent( @_ ) ); } | ||||||||||||||||||||||||||
|
Subroutine
text: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | # Note: property value | ||||||||||||||||||||||||||||
| 150 | sub value : lvalue | ||||||||||||||||||||||||||||
| 151 | { | ||||||||||||||||||||||||||||
| 152 | 6 | ▶ | my $self = shift( @_ ); | ||||||||||||||||||||||||||
|
Subroutine
value: called CC=5 SCAR=16.5
Pod
documented
| |||||||||||||||||||||||||||||
| 153 | 6 | my $has_arg = 0; | |||||||||||||||||||||||||||
| 154 | 6 | my $arg; | |||||||||||||||||||||||||||
| 155 | 6 | ▶ | if( want( qw( LVALUE ASSIGN ) ) ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 156 | { | ||||||||||||||||||||||||||||
| 157 | 2 | ( $arg ) = want( 'ASSIGN' ); | |||||||||||||||||||||||||||
| 158 | 2 | $has_arg++; | |||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||
| 160 | else | ||||||||||||||||||||||||||||
| 161 | { | ||||||||||||||||||||||||||||
| 162 | 4 | ▶ | if( @_ ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 163 | { | ||||||||||||||||||||||||||||
| 164 | 0 | $arg = shift( @_ ); | |||||||||||||||||||||||||||
| 165 | 0 | $has_arg++; | |||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||
| 168 | 6 | ▶ | if( $has_arg ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 169 | { | ||||||||||||||||||||||||||||
| 170 | 2 | return( $self->_set_get_property( 'value', $arg ) ); | |||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||
| 172 | else | ||||||||||||||||||||||||||||
| 173 | { | ||||||||||||||||||||||||||||
| 174 | 4 | ▶ | return( $self->attributes->has( 'value' ) ? $self->attr( 'value' ) : $self->textContent ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | 2 | 1; | |||||||||||||||||||||||||||
| 179 | # NOTE: POD | ||||||||||||||||||||||||||||
| 180 | __END__ | ||||||||||||||||||||||||||||