| 1 | ##---------------------------------------------------------------------------- | ||
| 2 | ## HTML Object - ~/lib/HTML/Object/DOM/Attribute.pm | ||
| 3 | ## Version v0.2.0 | ||
| 4 | ## Copyright(c) 2021 DEGUEST Pte. Ltd. | ||
| 5 | ## Author: Jacques Deguest <jack@deguest.jp> | ||
| 6 | ## Created 2021/12/13 | ||
| 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::Attribute; | ||
| 15 | BEGIN | ||
| 16 | { | ||
| 17 | use strict; | ||
| 18 | use warnings; | ||
| 19 | warnings::register_categories( 'HTML::Object' ); | ||
| 20 | use parent qw( HTML::Object::Attribute HTML::Object::DOM::Node ); | ||
| 21 | use vars qw( $VERSION ); | ||
| 22 | our $VERSION = 'v0.2.0'; | ||
| 23 | }; | ||
| 24 | |||
| 25 | use strict; | ||
| 26 | use warnings; | ||
| 27 | |||
| 28 | sub init { return( shift->HTML::Object::Attribute::init( @_ ) ); } | ||
| 29 | |||
| 30 | sub getAttributes | ||
| 31 | { | ||
| 32 | my $self = shift( @_ ); | ||
| 33 | my $elem = $self->element; | ||
| 34 | my $a = $self->new_array; | ||
| 35 | return( wantarray() ? $a->list : $a ) if( !$elem ); | ||
| 36 | return( $elem->getAttributes ); | ||
| 37 | } | ||
| 38 | |||
| 39 | sub getChildNodes { return( wantarray() ? () : [] ); } | ||
| 40 | |||
| 41 | sub getElementById { return; } | ||
| 42 | |||
| 43 | sub getFirstChild { return; } | ||
| 44 | |||
| 45 | sub getLastChild { return; } | ||
| 46 | |||
| 47 | sub getLocalName | ||
| 48 | { | ||
| 49 | my $self = shift( @_ ); | ||
| 50 | ( my $name = $self->name ) =~ s{^.*:}{}; | ||
| 51 | return( $name ); | ||
| 52 | } | ||
| 53 | |||
| 54 | sub getName { return( shift->name ); } | ||
| 55 | |||
| 56 | sub getNextSibling | ||
| 57 | { | ||
| 58 | my $self = shift( @_ ); | ||
| 59 | my $elem = $self->element; | ||
| 60 | return if( !$elem ); | ||
| 61 | my $pos = $elem->attributes_sequence->pos( $self->name ); | ||
| 62 | return if( $pos == $elem->attributes_sequence->size ); | ||
| 63 | my $key = $elem->attributes_sequence->get( $pos - 1 ); | ||
| 64 | my $val = $elem->attributes->get( $key ); | ||
| 65 | return( $self->new( | ||
| 66 | element => $elem, | ||
| 67 | name => $key, | ||
| 68 | rank => ( $pos + 1 ), | ||
| 69 | value => $val, | ||
| 70 | ) ); | ||
| 71 | } | ||
| 72 | |||
| 73 | sub getParentNode { return( shift->element ); } | ||
| 74 | |||
| 75 | # awfully inefficient, but hopefully this is called only for weird (read test-case) queries | ||
| 76 | sub getPreviousSibling | ||
| 77 | { | ||
| 78 | my $self = shift( @_ ); | ||
| 79 | my $elem = $self->element; | ||
| 80 | return if( !$elem ); | ||
| 81 | my $pos = $elem->attributes_sequence->pos( $self->name ); | ||
| 82 | return if( !$pos ); | ||
| 83 | my $key = $elem->attributes_sequence->get( $pos - 1 ); | ||
| 84 | my $val = $elem->attributes->get( $key ); | ||
| 85 | return( $self->new( | ||
| 86 | element => $elem, | ||
| 87 | name => $key, | ||
| 88 | rank => ( $pos - 1 ), | ||
| 89 | value => $val, | ||
| 90 | ) ); | ||
| 91 | } | ||
| 92 | |||
| 93 | sub getRootNode { return( shift->parent->getRootNode ); } | ||
| 94 | |||
| 95 | sub getValue { return( shift->value ); } | ||
| 96 | |||
| 97 | sub isAttributeNode { 1 } | ||
| 98 | |||
| 99 | sub isCommentNode { return(0); } | ||
| 100 | |||
| 101 | sub isElementNode { return(0); } | ||
| 102 | |||
| 103 | sub isNamespaceNode { return(0); } | ||
| 104 | |||
| 105 | sub isPINode { return(0); } | ||
| 106 | |||
| 107 | sub isProcessingInstructionNode { return(0); } | ||
| 108 | |||
| 109 | sub isTextNode { return(0); } | ||
| 110 | |||
| 111 | sub is_inside | ||
| 112 | { | ||
| 113 | my( $self, $node ) = @_; | ||
| 114 | my $e = $self->element; | ||
| 115 | return( ( $e == $node ) || $e->is_inside( $node ) ); | ||
| 116 | } | ||
| 117 | |||
| 118 | # Updated version from HTML::Object::Element's one because HTML::Object::DOM::Attribute | ||
| 119 | # is considered a child of its associated element | ||
| 120 | # This is used in HTML::Object::DOM::Node->compareDocumentPosition | ||
| 121 | sub lineage | ||
| 122 | { | ||
| 123 | my $self = shift( @_ ); | ||
| 124 | my $e = $self->element; | ||
| 125 | return( $self->new_array ) if( !$e ); | ||
| 126 | return( $e->lineage->unshift( $e ) ); | ||
| 127 | } | ||
| 128 | |||
| 129 | sub localName { return( shift->getName ); } | ||
| 130 | |||
| 131 | sub namespaceURI { return; } | ||
| 132 | |||
| 133 | # "The value of Attr.name, that is the qualified name of the attribute." | ||
| 134 | # <https://developer.mozilla.org/en-US/docs/Web/API/Node/nodeName> | ||
| 135 | sub nodeName { return( shift->name ); } | ||
| 136 | |||
| 137 | # Note: Property | ||
| 138 | sub nodeValue : lvalue { return( shift->_set_get_lvalue( 'value', @_ ) ); } | ||
| 139 | |||
| 140 | sub ownerElement { return( shift->element ); } | ||
| 141 | |||
| 142 | sub parent { return( shift->element ); } | ||
| 143 | |||
| 144 | sub prefix { return; } | ||
| 145 | |||
| 146 | sub string_value { return( shift->value ); } | ||
| 147 | |||
| 148 | sub to_boolean { return( shift->value->scalar ? HTML::Object::Boolean->True : HTML::Object::Boolean->False ); } | ||
| 149 | |||
| 150 | sub to_literal { return( HTML::Object::Literal->new( shift->value ) ); } | ||
| 151 | |||
| 152 | sub to_number { return( HTML::Object::Number->new( shift->value ) ); } | ||
| 153 | |||
| 154 | sub toString | ||
| 155 | { | ||
| 156 | my $self = shift( @_ ); | ||
| 157 | return( sprintf( '%s="%s"', $self->name, $self->value ) ); | ||
| 158 | } | ||
| 159 | |||
| 160 | 1; | ||
| 161 | # NOTE: POD | ||
| 162 | __END__ | ||
| 163 | |||
| 164 | =encoding utf-8 | ||
| 165 | |||
| 166 | =head1 NAME | ||
| 167 | |||
| 168 | HTML::Object::DOM::Attribute - HTML Object | ||
| 169 | |||
| 170 | =head1 SYNOPSIS | ||
| 171 | |||
| 172 | use HTML::Object::DOM::Attribute; | ||
| 173 | my $this = HTML::Object::DOM::Attribute->new || die( HTML::Object::DOM::Attribute->error, "\n" ); | ||
| 174 | |||
| 175 | =head1 VERSION | ||
| 176 | |||
| 177 | v0.2.0 | ||
| 178 | |||
| 179 | =head1 DESCRIPTION | ||
| 180 | |||
| 181 | This module implements a DOM node attribute. It inherits from L<HTML::Object::Attribute> and from L<HTML::Object::DOM::Node> | ||
| 182 | |||
| 183 | =head1 INHERITANCE | ||
| 184 | |||
| 185 | +-----------------------+ +---------------------------+ +-------------------------+ +----------------------------+ | ||
| 186 | | HTML::Object::Element | --> | HTML::Object::EventTarget | --> | HTML::Object::DOM::Node | --> | HTML::Object::DOM::Closing | | ||
| 187 | +-----------------------+ +---------------------------+ +-------------------------+ +----------------------------+ | ||
| 188 | | ^ | ||
| 189 | | | | ||
| 190 | v | | ||
| 191 | +-----------------------+ | | ||
| 192 | | HTML::Object::Closing | ------------------------------------------------------------------------+ | ||
| 193 | +-----------------------+ | ||
| 194 | |||
| 195 | =head1 PROPERTIES | ||
| 196 | |||
| 197 | =head2 nodeValue | ||
| 198 | |||
| 199 | Returns the attribute value as a L<scalar object|Module::Generic::Scalar> | ||
| 200 | |||
| 201 | =head1 METHODS | ||
| 202 | |||
| 203 | =head2 getAttributes | ||
| 204 | |||
| 205 | If this attribute has no associated element, this returns an empty list in list context or an empty array reference in scalar context. | ||
| 206 | |||
| 207 | Otherwise, this returns an L<array object|Module::Generic::Array> of attribute objects for the associated element. | ||
| 208 | |||
| 209 | =head2 getChildNodes | ||
| 210 | |||
| 211 | Returns an empty list in list context, or an empty array reference in scalar context. | ||
| 212 | |||
| 213 | =head2 getElementById | ||
| 214 | |||
| 215 | Returns an empty list in list context, or C<undef> in scalar context. | ||
| 216 | |||
| 217 | =head2 getFirstChild | ||
| 218 | |||
| 219 | Returns an empty list in list context, or C<undef> in scalar context. | ||
| 220 | |||
| 221 | =head2 getLastChild | ||
| 222 | |||
| 223 | Returns an empty list in list context, or C<undef> in scalar context. | ||
| 224 | |||
| 225 | =head2 getLocalName | ||
| 226 | |||
| 227 | Returns the attribute name. | ||
| 228 | |||
| 229 | =head2 getName | ||
| 230 | |||
| 231 | Returns the attribute name. | ||
| 232 | |||
| 233 | =head2 getNextSibling | ||
| 234 | |||
| 235 | Returns the next attribute object for the associated element, or C<undef> if there are no associated element, or if this attribute is the last one of the associated element. | ||
| 236 | |||
| 237 | =head2 getParentNode | ||
| 238 | |||
| 239 | Returns the associated element, if any. | ||
| 240 | |||
| 241 | =head2 getPreviousSibling | ||
| 242 | |||
| 243 | Returns the previous attribute object for the associated element, or C<undef> if there are no associated element, or if this attribute is the first one of the associated element. | ||
| 244 | |||
| 245 | =head2 getRootNode | ||
| 246 | |||
| 247 | Returns the root node by calling C<getRootNode> on this attribute parent. | ||
| 248 | |||
| 249 | =head2 getValue | ||
| 250 | |||
| 251 | Returns the attribute value. | ||
| 252 | |||
| 253 | =head2 isAttributeNode | ||
| 254 | |||
| 255 | Returns true. | ||
| 256 | |||
| 257 | =head2 isCommentNode | ||
| 258 | |||
| 259 | Returns false. | ||
| 260 | |||
| 261 | =head2 isElementNode | ||
| 262 | |||
| 263 | Returns false. | ||
| 264 | |||
| 265 | =head2 isEqualNode | ||
| 266 | |||
| 267 | Provided with another attribute object, and this returns true if both attribute object have the same value, or false otherwise. | ||
| 268 | |||
| 269 | =head2 isNamespaceNode | ||
| 270 | |||
| 271 | Returns false. | ||
| 272 | |||
| 273 | =head2 isPINode | ||
| 274 | |||
| 275 | Returns false. | ||
| 276 | |||
| 277 | =head2 isProcessingInstructionNode | ||
| 278 | |||
| 279 | Returns false. | ||
| 280 | |||
| 281 | =head2 isTextNode | ||
| 282 | |||
| 283 | Returns false. | ||
| 284 | |||
| 285 | =head2 is_inside | ||
| 286 | |||
| 287 | Provided with a node and this returns true if this attribute associated element is the same, or if the associated element is inside the provided element. | ||
| 288 | |||
| 289 | =head2 lineage | ||
| 290 | |||
| 291 | Returns the value provided by calling C<lineage> on the associated element and adding it to the lineage, or an empty L<array object> if there is no associated element. | ||
| 292 | |||
| 293 | =head2 localName | ||
| 294 | |||
| 295 | This is just an alias for L</getName> | ||
| 296 | |||
| 297 | =head2 namespaceURI | ||
| 298 | |||
| 299 | Always returns C<undef> | ||
| 300 | |||
| 301 | =head2 nodeName | ||
| 302 | |||
| 303 | Returns the qualified name of the attribute. This actually calls L<HTML::Object::Attribute/name> | ||
| 304 | |||
| 305 | =head2 ownerElement | ||
| 306 | |||
| 307 | This returns the attribute associated element, if any. | ||
| 308 | |||
| 309 | =head2 parent | ||
| 310 | |||
| 311 | This returns the attribute associated element, if any. | ||
| 312 | |||
| 313 | =head2 prefix | ||
| 314 | |||
| 315 | Always returns C<undef> | ||
| 316 | |||
| 317 | =head2 string_value | ||
| 318 | |||
| 319 | Returns the attribute value. | ||
| 320 | |||
| 321 | =head2 to_boolean | ||
| 322 | |||
| 323 | Returns a L<true boolean object|HTML::Object::Boolean> if the attribute value is true, or a L<false boolean object|HTML::Object::Boolean> otherwise. | ||
| 324 | |||
| 325 | =head2 to_literal | ||
| 326 | |||
| 327 | Returns the attribute value. | ||
| 328 | |||
| 329 | =head2 to_number | ||
| 330 | |||
| 331 | Returns a new L<HTML::Object::Number> object based on the attribute value. | ||
| 332 | |||
| 333 | =head2 toString | ||
| 334 | |||
| 335 | Returns a string, such as: C<name="value"> | ||
| 336 | |||
| 337 | =head1 AUTHOR | ||
| 338 | |||
| 339 | Jacques Deguest E<lt>F<jack@deguest.jp>E<gt> | ||
| 340 | |||
| 341 | =head1 SEE ALSO | ||
| 342 | |||
| 343 | L<Mozilla reference|https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes> | ||
| 344 | |||
| 345 | L<W3C standard on attributes|https://html.spec.whatwg.org/multipage/syntax.html#attributes-2> | ||
| 346 | |||
| 347 | =head1 COPYRIGHT & LICENSE | ||
| 348 | |||
| 349 | Copyright(c) 2021 DEGUEST Pte. Ltd. | ||
| 350 | |||
| 351 | All rights reserved | ||
| 352 | |||
| 353 | This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. | ||
| 354 | |||
| 355 | =cut |