| 1 | ##---------------------------------------------------------------------------- | ||
| 2 | ## HTML Object - ~/lib/HTML/Object/Attribute.pm | ||
| 3 | ## Version v0.3.0 | ||
| 4 | ## Copyright(c) 2022 DEGUEST Pte. Ltd. | ||
| 5 | ## Author: Jacques Deguest <jack@deguest.jp> | ||
| 6 | ## Created 2021/04/22 | ||
| 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::Attribute; | ||
| 15 | BEGIN | ||
| 16 | { | ||
| 17 | use strict; | ||
| 18 | use warnings; | ||
| 19 | warnings::register_categories( 'HTML::Object' ); | ||
| 20 | use parent qw( Module::Generic ); | ||
| 21 | use vars qw( $VERSION ); | ||
| 22 | use HTML::Object::Literal; | ||
| 23 | use HTML::Object::Number; | ||
| 24 | use Wanted; | ||
| 25 | our $VERSION = 'v0.3.0'; | ||
| 26 | }; | ||
| 27 | |||
| 28 | use strict; | ||
| 29 | use warnings; | ||
| 30 | |||
| 31 | sub init | ||
| 32 | { | ||
| 33 | my $self = shift( @_ ); | ||
| 34 | my $name; | ||
| 35 | # HTML::Object::Attribute->new( 'id' ); | ||
| 36 | # HTML::Object::Attribute( 'id', value => 'hello', element => $e ); | ||
| 37 | # HTML::Object::Attribute( 'id', { value => 'hello', element => $e } ); | ||
| 38 | if( ( scalar( @_ ) == 1 && ( !ref( $_[0] ) || overload::Method( $_[0], '""' ) ) ) || | ||
| 39 | ( ( @_ % 2 ) && ( !ref( $_[0] ) || overload::Method( $_[0], '""' ) ) ) || | ||
| 40 | ( scalar( @_ ) == 2 && ( !ref( $_[0] ) || overload::Method( $_[0], '""' ) ) && ref( $_[1] ) eq 'HASH' ) ) | ||
| 41 | { | ||
| 42 | $name = shift( @_ ); | ||
| 43 | } | ||
| 44 | $self->{element} = ''; | ||
| 45 | $self->{name} = $name; | ||
| 46 | $self->{rank} = ''; | ||
| 47 | $self->{value} = ''; | ||
| 48 | $self->{_init_strict_use_sub} = 1; | ||
| 49 | $self->{_exception_class} = 'HTML::Object::Exception'; | ||
| 50 | $self->SUPER::init( @_ ) || return( $self->pass_error ); | ||
| 51 | return( $self ); | ||
| 52 | } | ||
| 53 | |||
| 54 | sub element { return( shift->_set_get_object_without_init( 'element', 'HTML::Object::Element', @_ ) ); } | ||
| 55 | |||
| 56 | sub name { return( shift->_set_get_scalar_as_object( 'name', @_ ) ); } | ||
| 57 | |||
| 58 | sub rank { return( shift->_set_get_number_as_object( 'rank', @_ ) ); } | ||
| 59 | |||
| 60 | # sub value { return( shift->_set_get_scalar_as_object( 'value', @_ ) ); } | ||
| 61 | sub value | ||
| 62 | { | ||
| 63 | my $self = shift( @_ ); | ||
| 64 | if( @_ ) | ||
| 65 | { | ||
| 66 | my $v = shift( @_ ); | ||
| 67 | $v =~ s/^[[:blank:]\h]+|[[:blank:]\h]+$//g; | ||
| 68 | return( $self->_set_get_scalar_as_object( 'value', $v ) ); | ||
| 69 | } | ||
| 70 | return( $self->_set_get_scalar_as_object( 'value' ) ); | ||
| 71 | } | ||
| 72 | |||
| 73 | |||
| 74 | 1; | ||
| 75 | # NOTE: POD | ||
| 76 | __END__ | ||
| 77 | |||
| 78 | =encoding utf8 | ||
| 79 | |||
| 80 | =head1 NAME | ||
| 81 | |||
| 82 | HTML::Object::Attribute - HTML Object Element Attribute Class | ||
| 83 | |||
| 84 | =head1 SYNOPSIS | ||
| 85 | |||
| 86 | use HTML::Object::Attribute; | ||
| 87 | my $attr = HTML::Object::Attribute->new( 'id' ); | ||
| 88 | $attr->value = "hello"; | ||
| 89 | |||
| 90 | =head1 VERSION | ||
| 91 | |||
| 92 | v0.3.0 | ||
| 93 | |||
| 94 | =head1 DESCRIPTION | ||
| 95 | |||
| 96 | This class represent an element attribute. it is used as part of L<HTML::Object>, and also contains methods to interface with L<HTML::Object::XPath> | ||
| 97 | |||
| 98 | =head1 CONSTRUCTOR | ||
| 99 | |||
| 100 | =head2 new | ||
| 101 | |||
| 102 | Creates a new C<HTML::Object::Attribute> objects. | ||
| 103 | |||
| 104 | It may also take an hash like arguments, that also are method of the same name. | ||
| 105 | |||
| 106 | my $attr = HTML::Object::Attribute->new( 'id' ); | ||
| 107 | # or | ||
| 108 | my $attr = HTML::Object::Attribute->new( name => 'id' ); | ||
| 109 | |||
| 110 | =head1 PROPERTIES | ||
| 111 | |||
| 112 | =head2 nodeValue | ||
| 113 | |||
| 114 | This returns or sets the value of the current node. | ||
| 115 | |||
| 116 | For document, element or collection, this returns C<undef> and for attribute, text or comment, this returns the objct value. | ||
| 117 | |||
| 118 | See L<for more information|https://developer.mozilla.org/en-US/docs/Web/API/Node/nodeValue> | ||
| 119 | |||
| 120 | =head1 METHODS | ||
| 121 | |||
| 122 | =head2 element | ||
| 123 | |||
| 124 | Returns the L<HTML::Object::Element> object to which this attribute belongs. | ||
| 125 | |||
| 126 | =head2 getAttributes | ||
| 127 | |||
| 128 | Returns an L<array object|Module::Generic::Array> of the related element's attributes as L<HTML::Object::Attribute> objects. | ||
| 129 | |||
| 130 | =head2 getLocalName | ||
| 131 | |||
| 132 | Returns the attribute name. | ||
| 133 | |||
| 134 | =head2 getName | ||
| 135 | |||
| 136 | Returns the attribute name. | ||
| 137 | |||
| 138 | =head2 getNextSibling | ||
| 139 | |||
| 140 | Returns the next attribute object, or C<undef>. | ||
| 141 | |||
| 142 | =head2 getParentNode | ||
| 143 | |||
| 144 | Returns the parent L<HTML::Object::Element> object. | ||
| 145 | |||
| 146 | =head2 getPreviousSibling | ||
| 147 | |||
| 148 | Returns the previous attribute object, or C<undef>. | ||
| 149 | |||
| 150 | =head2 getValue | ||
| 151 | |||
| 152 | Returns the attribute value. | ||
| 153 | |||
| 154 | =head2 isAttributeNode | ||
| 155 | |||
| 156 | Always returns true. | ||
| 157 | |||
| 158 | =head2 is_inside | ||
| 159 | |||
| 160 | Provided with an L<HTML::Object::Element> and this will return true if this attribute is inside it, or false otherwise. | ||
| 161 | |||
| 162 | =head2 lineage | ||
| 163 | |||
| 164 | Add the parent element to our lineage. See L<HTML::Object::Element/lineage> | ||
| 165 | |||
| 166 | =head2 localName | ||
| 167 | |||
| 168 | Read-only. | ||
| 169 | |||
| 170 | A string representing the local part of the qualified name of the attribute. | ||
| 171 | |||
| 172 | This is the same as L</getName>, because this interface does not use xml C<prefix> | ||
| 173 | |||
| 174 | See L<for more information|https://developer.mozilla.org/en-US/docs/Web/API/Attr/localName> | ||
| 175 | |||
| 176 | =head2 name | ||
| 177 | |||
| 178 | Set or get the attribute name. | ||
| 179 | |||
| 180 | Normally, under JavaScript, this is read-only, but under perl you can change it. Still be careful. | ||
| 181 | |||
| 182 | See L<for more information|https://developer.mozilla.org/en-US/docs/Web/API/Attr/name> | ||
| 183 | |||
| 184 | =head2 namespaceURI | ||
| 185 | |||
| 186 | Read-only | ||
| 187 | |||
| 188 | A string representing the URI of the namespace of the attribute, or C<undef> if there is no namespace. | ||
| 189 | |||
| 190 | This actually always return C<undef>, because this interface does not use xml C<prefix> | ||
| 191 | |||
| 192 | See L<for more information|https://developer.mozilla.org/en-US/docs/Web/API/Attr/namespaceURI> | ||
| 193 | |||
| 194 | =head2 nodeValue | ||
| 195 | |||
| 196 | This returns or sets the value of the current element. | ||
| 197 | |||
| 198 | See L<for more information|https://developer.mozilla.org/en-US/docs/Web/API/Node/nodeValue> | ||
| 199 | |||
| 200 | =head2 ownerElement | ||
| 201 | |||
| 202 | Returns the L<element object|HTML::Object::Element> to which this attribute object belongs. | ||
| 203 | |||
| 204 | See L<for more information|https://developer.mozilla.org/en-US/docs/Web/API/Attr/ownerElement> | ||
| 205 | |||
| 206 | =head2 prefix | ||
| 207 | |||
| 208 | Read-only. | ||
| 209 | |||
| 210 | This always return C<undef>, because this interface does not use xml C<prefix> | ||
| 211 | |||
| 212 | Normally, under JavaScript, this would return a string representing the namespace prefix of the attribute, or c<undef> if a namespace without prefix or no namespace are specified. | ||
| 213 | |||
| 214 | See L<for more information|https://developer.mozilla.org/en-US/docs/Web/API/Attr/prefix> | ||
| 215 | |||
| 216 | =head2 rank | ||
| 217 | |||
| 218 | Set or get the attribute rank. This returns a L<number object|Module::Generic::Number> | ||
| 219 | |||
| 220 | =head2 string_value | ||
| 221 | |||
| 222 | This is an alias for L</value> | ||
| 223 | |||
| 224 | =head2 to_boolean | ||
| 225 | |||
| 226 | Returns the attribute value as a L<boolean|Module::Generic::Boolean> | ||
| 227 | |||
| 228 | =head2 to_literal | ||
| 229 | |||
| 230 | Returns the attribute value as a L<litteral|HTML::Object::Litteral> | ||
| 231 | |||
| 232 | =head2 to_number | ||
| 233 | |||
| 234 | Returns the attribute value as a L<number|Module::Generic::Number> | ||
| 235 | |||
| 236 | =head2 toString | ||
| 237 | |||
| 238 | Returns a stringification of this attribute such as C<attribute="value"> | ||
| 239 | |||
| 240 | =head2 value | ||
| 241 | |||
| 242 | Set or get the value of this attribute as a L<scalar object|Module::Generic::Scalar>. For example: | ||
| 243 | |||
| 244 | $attr->value( "hello" ); | ||
| 245 | |||
| 246 | See L<for more information|https://developer.mozilla.org/en-US/docs/Web/API/Attr/value> | ||
| 247 | |||
| 248 | =head1 AUTHOR | ||
| 249 | |||
| 250 | Jacques Deguest E<lt>F<jack@deguest.jp>E<gt> | ||
| 251 | |||
| 252 | =head1 SEE ALSO | ||
| 253 | |||
| 254 | L<HTML::Object>, L<HTML::Object::Attribute>, L<HTML::Object::Boolean>, L<HTML::Object::Closing>, L<HTML::Object::Collection>, L<HTML::Object::Comment>, L<HTML::Object::Declaration>, L<HTML::Object::Document>, L<HTML::Object::Element>, L<HTML::Object::Exception>, L<HTML::Object::Literal>, L<HTML::Object::Number>, L<HTML::Object::Root>, L<HTML::Object::Space>, L<HTML::Object::Text>, L<HTML::Object::XQuery> | ||
| 255 | |||
| 256 | L<https://developer.mozilla.org/en-US/docs/Web/API/Attr> | ||
| 257 | |||
| 258 | L<Mozilla reference|https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes> | ||
| 259 | |||
| 260 | L<W3C standard on attributes|https://html.spec.whatwg.org/multipage/syntax.html#attributes-2> | ||
| 261 | |||
| 262 | =head1 COPYRIGHT & LICENSE | ||
| 263 | |||
| 264 | Copyright (c) 2021 DEGUEST Pte. Ltd. | ||
| 265 | |||
| 266 | All rights reserved | ||
| 267 | |||
| 268 | This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. | ||
| 269 | |||
| 270 | =cut |