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

filter: Statementstmt 0.0% 0 / 54 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 16 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 98 CC 6 SCAR 19.3
CC
6
Cov
0%
control
20.0
init
6.0
form
6.0
CRAP 6.9
1 ##----------------------------------------------------------------------------
2 ## HTML Object - ~/lib/HTML/Object/DOM/Element/Label.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::Label;
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( :label );
23 our $VERSION = 'v0.2.0';
24 };
25  
26 use strict;
27 use warnings;
28  
29 sub init
30 {
31 my $self = shift( @_ );
32 $self->{_init_strict_use_sub} = 1;
33 $self->SUPER::init( @_ ) || return( $self->pass_error );
34 $self->{tag} = 'label' if( !CORE::length( "$self->{tag}" ) );
35 return( $self );
36 }
37  
38 # Note: property read-only
39 sub control
40 {
41 my $self = shift( @_ );
42 my $id = $self->htmlFor;
43 # return if( !defined( $id ) || !CORE::length( "$id" ) );
44 if( defined( $id ) && CORE::length( "$id" ) )
45 {
46 my $root = $self->root;
47 my $elem = $root->look_down( id => $id )->first;
48 return if( !defined( $elem ) || !ref( $elem ) );
49 return( $elem );
50 }
51 # "If the for attribute is not specified, but the label element has a labelable element descendant, then the first such descendant in tree order is the label element's labeled control."
52 # <https://html.spec.whatwg.org/multipage/forms.html#htmllabelelement>
53 else
54 {
55 my $elems = $self->look_down( _tag => qr/(?:button|input|meter|output|progress|select|textarea)/ );
56 my $elem;
57 $elems->foreach(sub
58 {
59 my $tag = $_->tag;
60 if( $tag ne 'input' ||
61 ( $tag eq 'input' && lc( $_->attr( 'type' ) // '' ) ne 'hidden' ) )
62 {
63 $elem = $_;
64 return(0);
65 }
66 });
67 return( $elem );
68 }
69 }
70  
71 # Note: property form is NOT inherited, because this is the 'form' value of the associated control, if any.
72 {
73 no warnings 'redefine';
74 sub form
75 {
76 my $self = shift( @_ );
77 my $elem = $self->control;
78 return if( !$self->_is_a( $elem => 'HTML::Object::DOM::Element' ) );
79 return( $elem->form );
80 }
81 }
82  
83 # Note: property
84 # labelable elements:
85 # "button, input (if the type attribute is not in the Hidden state) meter, output, progress, select, textarea, form-associated custom elements"
86 # <https://html.spec.whatwg.org/multipage/forms.html#category-label>
87 sub htmlFor : lvalue { return( shift->_set_get_property( 'for', @_ ) ); }
88  
89 1;
90 # NOTE: POD
91 __END__