Object/DOM/Element/Option.pm

filter: Statementstmt 93.1% 81 / 87 Branchbran 68.4% 26 / 38 Conditioncond 44.4% 4 / 9 MC/DCmcdc 16.6% 1 / 6 Subroutinesub 100% 19 / 19 Podpod 100% 7 / 7 totaltotal 83.1% 138 / 166 CC 23 SCAR 13.8
CC
23
Cov
83%
__ANON__
9.3
value
5.2
selected
5.2
CRAP 4.0
« Object/DOM/Attribute.pm ↑ Summary Object/DOM/Element/Mod.pm »
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 2 ▶{
Subroutine BEGIN: called CC=1 SCAR=0.0
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' );
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 4 ▶{
Subroutine init: called CC=3 SCAR=11.3
Pod documented
32 4 my $self = shift( @_ );
33 4 $self->{_init_strict_use_sub} = 1;
34 4 ▶ $self->SUPER::init( @_ ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->SUPER::init(@_) or return $self->pass_error 4 0 4
35 4 ▶ $self->{tag} = 'option' if( !CORE::length( "$self->{tag}" ) );
Branch: 1 branch
BranchTrueFalseTotal
unless length qq[$self->{"tag"}] 0 4 4
36 4 return( $self );
37 }
38  
39 # Note: property defaultSelected
40 12 ▶sub defaultSelected : lvalue { return( shift->_set_get_property({
Subroutine defaultSelected: called CC=1 SCAR=0.0
Pod documented
41 attribute => 'selected',
42 callback => sub
43 12 ▶ {
Subroutine __ANON__: called CC=4 SCAR=13.9
44 12 my $self = shift( @_ );
45 12 my $attr = shift( @_ );
46 12 ▶ if( @_ )
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 1 11 12
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
BranchTrueFalseTotal
if defined $value 1 0 1
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
BranchTrueFalseTotal
$self->attributes->has('selected') ? : 4 7 11
60 }
61 }
62 12 }, @_ ) ); }
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 1 ▶{
Subroutine index: called CC=3 SCAR=11.3
Pod documented
71 1 my $self = shift( @_ );
72 1 my $parent = $self->parent;
73 1 ▶ return if( !$parent );
Branch: 1 branch
BranchTrueFalseTotal
unless $parent 0 1 1
74 1 ▶ return if( !$parent->can( 'options' ) );
Branch: 1 branch
BranchTrueFalseTotal
unless $parent->can("options") 0 1 1
75 1 return( $parent->options->pos( $self ) );
76 }
77  
78 # Note: property label read-only
79 3 ▶sub label : lvalue { return( shift->_lvalue({
Subroutine label: called CC=1 SCAR=0.3
Pod documented
80 set => sub
81 1 ▶ {
Subroutine __ANON__: called CC=6 SCAR=22.3
82 1 my $self = shift( @_ );
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
BranchTrueFalseTotal
unless (defined $val) 0 1 1
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
BranchTrueFalseTotal
if $self->_is_a($val, "HTML::Object::DOM::Element::Text") or $self->_is_a($val, "HTML::Object::DOM::Element::Space") 0 1 1
Condition33%1/3
$self->_is_a($val, "HTML::Object::DOM::Element::Text") or $self->_is_a($val, "HTML::Object::DOM::Element::Space")
l!l&&r!l&&!r
001
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")
ABresult
1X1
011
000
91 1 ▶ return( $self->error({
Branch: 1 branch
BranchTrueFalseTotal
if ref $val and not overload::Method($val, "\"\"") 0 1 1
Condition33%1/3
ref $val and not overload::Method($val, "\"\"")
!ll&&!rl&&r
100
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, "\"\"")
ABresult
0X0
100
111
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 2 ▶ {
Subroutine __ANON__: called CC=3 SCAR=11.0
99 2 my $self = shift( @_ );
100 2 my $label = $self->attr( 'label' );
101 2 ▶ if( defined( $label ) && CORE::length( "$label" ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $label and length "$label") { } 1 1 2
Condition66%2/3
defined $label and length "$label"
!ll&&!rl&&r
101
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"
ABresult
0X0
100
111
102 {
103 1 return( $label );
104 }
105 else
106 {
107 1 return( $self->text );
108 }
109 }
110 3 }, @_ ) ); }
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 2 ▶sub selected : lvalue
Subroutine selected: called CC=5 SCAR=16.4
Pod documented
115 {
116 2 my $self = shift( @_ );
117 2 my $has_arg = 0;
118 2 my $arg;
119 2 ▶ if( want( qw( LVALUE ASSIGN ) ) )
Branch: 1 branch
BranchTrueFalseTotal
if (want(('LVALUE', 'ASSIGN'))) { } 0 2 2
120 {
121 0 ( $arg ) = want( 'ASSIGN' );
122 0 $has_arg++;
123 }
124 else
125 {
126 2 ▶ if( @_ )
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 1 1 2
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
BranchTrueFalseTotal
if ($has_arg) { } 1 1 2
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
BranchTrueFalseTotal
$self->attributes->has('selected') ? : 1 0 1
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 6 ▶sub value : lvalue
Subroutine value: called CC=5 SCAR=16.5
Pod documented
151 {
152 6 my $self = shift( @_ );
153 6 my $has_arg = 0;
154 6 my $arg;
155 6 ▶ if( want( qw( LVALUE ASSIGN ) ) )
Branch: 1 branch
BranchTrueFalseTotal
if (want(('LVALUE', 'ASSIGN'))) { } 2 4 6
156 {
157 2 ( $arg ) = want( 'ASSIGN' );
158 2 $has_arg++;
159 }
160 else
161 {
162 4 ▶ if( @_ )
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 4 4
163 {
164 0 $arg = shift( @_ );
165 0 $has_arg++;
166 }
167 }
168 6 ▶ if( $has_arg )
Branch: 1 branch
BranchTrueFalseTotal
if ($has_arg) { } 2 4 6
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
BranchTrueFalseTotal
$self->attributes->has('value') ? : 4 0 4
175 }
176 }
177  
178 2 1;
179 # NOTE: POD
180 __END__
« Object/DOM/Attribute.pm ↑ Summary Object/DOM/Element/Mod.pm »