| 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__ | ||
| 92 | |||
| 93 | =encoding utf-8 | ||
| 94 | |||
| 95 | =head1 NAME | ||
| 96 | |||
| 97 | HTML::Object::DOM::Element::Label - HTML Object DOM Label Class | ||
| 98 | |||
| 99 | =head1 SYNOPSIS | ||
| 100 | |||
| 101 | use HTML::Object::DOM::Element::Label; | ||
| 102 | my $label = HTML::Object::DOM::Element::Label->new || | ||
| 103 | die( HTML::Object::DOM::Element::Label->error, "\n" ); | ||
| 104 | |||
| 105 | =head1 VERSION | ||
| 106 | |||
| 107 | v0.2.0 | ||
| 108 | |||
| 109 | =head1 DESCRIPTION | ||
| 110 | |||
| 111 | This interface gives access to properties specific to <label> elements. It inherits methods and properties from the base L<HTML::Object::Element> interface. | ||
| 112 | |||
| 113 | =head1 INHERITANCE | ||
| 114 | |||
| 115 | +-----------------------+ +---------------------------+ +-------------------------+ +----------------------------+ +-----------------------------------+ | ||
| 116 | | HTML::Object::Element | --> | HTML::Object::EventTarget | --> | HTML::Object::DOM::Node | --> | HTML::Object::DOM::Element | --> | HTML::Object::DOM::Element::Label | | ||
| 117 | +-----------------------+ +---------------------------+ +-------------------------+ +----------------------------+ +-----------------------------------+ | ||
| 118 | |||
| 119 | =head1 PROPERTIES | ||
| 120 | |||
| 121 | Inherits properties from its parent L<HTML::Object::DOM::Element> | ||
| 122 | |||
| 123 | =head2 control | ||
| 124 | |||
| 125 | Read-only. | ||
| 126 | |||
| 127 | Is a L<HTML::Object::Element> representing the control with which the label is associated. It returns C<undef> if the C<for> attribute has no id set, or no associated element could be found in the DOM. | ||
| 128 | |||
| 129 | See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLLabelElement/control> | ||
| 130 | |||
| 131 | =head2 form | ||
| 132 | |||
| 133 | Read-only. | ||
| 134 | |||
| 135 | Is a L<HTML::Object::DOM::Element::Form> object representing the form with which the labeled control is associated, or C<undef> if there is no associated control, or if that control is not associated with a form. In other words, this is just a shortcut for: | ||
| 136 | |||
| 137 | $e->control->form | ||
| 138 | |||
| 139 | See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLLabelElement/form> | ||
| 140 | |||
| 141 | =head2 htmlFor | ||
| 142 | |||
| 143 | Is a string containing the ID of the labeled control. This reflects the for attribute. | ||
| 144 | |||
| 145 | Example: | ||
| 146 | |||
| 147 | <label for="inputId">Enter your name</label> | ||
| 148 | my $label = $doc->getElementsByTagName( 'label' )->first; | ||
| 149 | say( "ID is: ", $label->htmlFor ); | ||
| 150 | |||
| 151 | See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLLabelElement/htmlFor> | ||
| 152 | |||
| 153 | =head1 METHODS | ||
| 154 | |||
| 155 | Inherits methods from its parent L<HTML::Object::DOM::Element> | ||
| 156 | |||
| 157 | =head1 AUTHOR | ||
| 158 | |||
| 159 | Jacques Deguest E<lt>F<jack@deguest.jp>E<gt> | ||
| 160 | |||
| 161 | =head1 SEE ALSO | ||
| 162 | |||
| 163 | L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLLabelElement>, L<Mozilla documentation on label element|https://developer.mozilla.org/en-US/docs/Web/HTML/Element/label>, L<W3C specificatins|https://html.spec.whatwg.org/multipage/forms.html#htmllabelelement> | ||
| 164 | |||
| 165 | =head1 COPYRIGHT & LICENSE | ||
| 166 | |||
| 167 | Copyright(c) 2021 DEGUEST Pte. Ltd. | ||
| 168 | |||
| 169 | All rights reserved | ||
| 170 | |||
| 171 | This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. | ||
| 172 | |||
| 173 | =cut |