| 1 | ##---------------------------------------------------------------------------- | ||
| 2 | ## HTML Object - ~/lib/HTML/Object/DOM/Element.pm | ||
| 3 | ## Version v0.4.0 | ||
| 4 | ## Copyright(c) 2024 DEGUEST Pte. Ltd. | ||
| 5 | ## Author: Jacques Deguest <jack@deguest.jp> | ||
| 6 | ## Created 2021/12/13 | ||
| 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; | ||
| 15 | BEGIN | ||
| 16 | { | ||
| 17 | use strict; | ||
| 18 | use warnings; | ||
| 19 | warnings::register_categories( 'HTML::Object' ); | ||
| 20 | use HTML::Object::DOM::Node qw( TEXT_NODE COMMENT_NODE ); | ||
| 21 | use parent qw( HTML::Object::DOM::Node ); | ||
| 22 | use vars qw( @EXPORT_OK $VERSION ); | ||
| 23 | use HTML::Object::Exception; | ||
| 24 | use Scalar::Util (); | ||
| 25 | use URI; | ||
| 26 | use Wanted; | ||
| 27 | our @EXPORT_OK = qw( | ||
| 28 | DOCUMENT_POSITION_IDENTICAL DOCUMENT_POSITION_DISCONNECTED | ||
| 29 | DOCUMENT_POSITION_PRECEDING DOCUMENT_POSITION_FOLLOWING DOCUMENT_POSITION_CONTAINS | ||
| 30 | DOCUMENT_POSITION_CONTAINED_BY DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC | ||
| 31 | ); | ||
| 32 | our $VERSION = 'v0.4.0'; | ||
| 33 | }; | ||
| 34 | |||
| 35 | use strict; | ||
| 36 | use warnings; | ||
| 37 | |||
| 38 | sub init | ||
| 39 | { | ||
| 40 | my $self = shift( @_ ); | ||
| 41 | $self->{contentEditable} = 1; | ||
| 42 | $self->{_init_strict_use_sub} = 1; | ||
| 43 | $self->SUPER::init( @_ ) || return( $self->pass_error ); | ||
| 44 | # internal trigger, essentially to be triggered when an attribute is being updated | ||
| 45 | # so the TokenList object can be updated | ||
| 46 | $self->{_internal_attribute_callbacks} = {} if( !exists( $self->{_internal_attribute_callbacks} ) || ref( $self->{_internal_attribute_callbacks} ) ne 'HASH' ); | ||
| 47 | $self->{_internal_attribute_callbacks}->{class} = sub | ||
| 48 | { | ||
| 49 | my( $this, $val ) = @_; | ||
| 50 | my $list; | ||
| 51 | return if( !( $list = $this->{_class_list} ) ); | ||
| 52 | $list->update( $val ); | ||
| 53 | }; | ||
| 54 | return( $self ); | ||
| 55 | } | ||
| 56 | |||
| 57 | # Note: accessKey -> property | ||
| 58 | sub accessKey : lvalue { return( shift->_set_get_property( 'accesskey', @_ ) ); } | ||
| 59 | |||
| 60 | # Note: accessKeyLabel -> property | ||
| 61 | sub accessKeyLabel : lvalue { return( shift->_set_get_property( 'accessKeyLabel', @_ ) ); } | ||
| 62 | |||
| 63 | sub after | ||
| 64 | { | ||
| 65 | my $self = shift( @_ ); | ||
| 66 | return( $self ) if( !scalar( @_ ) ); | ||
| 67 | my $parent = $self->parent; | ||
| 68 | return( $self->error( "No parent set for this element, so you cannot set this \"after\" method." ) ) if( !$parent ); | ||
| 69 | my $pos = $parent->children->pos( $self ); | ||
| 70 | # If a HTML::Object::DOM::DocumentFragment object is provided, its children are | ||
| 71 | # copied to the list and its own children array is emptied. | ||
| 72 | my $list = $self->_get_from_list_of_elements_or_html( @_ ); | ||
| 73 | $list->foreach(sub | ||
| 74 | { | ||
| 75 | $_->parent( $parent ); | ||
| 76 | $parent->children->splice( $pos + 1, 0, $_ ); | ||
| 77 | $pos++; | ||
| 78 | # Required, because $pos++ as the last execution in this anon sub somehow returns defined and false | ||
| 79 | return(1); | ||
| 80 | }); | ||
| 81 | $parent->reset(1); | ||
| 82 | return( $self ); | ||
| 83 | } | ||
| 84 | |||
| 85 | sub append | ||
| 86 | { | ||
| 87 | my $self = shift( @_ ); | ||
| 88 | return( $self ) if( !scalar( @_ ) ); | ||
| 89 | # If a HTML::Object::DOM::DocumentFragment object is provided, its children are | ||
| 90 | # copied to the list and its own children array is emptied. | ||
| 91 | my $list = $self->_get_from_list_of_elements_or_html( @_ ); | ||
| 92 | my $children = $self->children; | ||
| 93 | $list->foreach(sub | ||
| 94 | { | ||
| 95 | $_->parent( $self ); | ||
| 96 | $children->push( $_ ); | ||
| 97 | }); | ||
| 98 | $self->reset(1); | ||
| 99 | return( $list ); | ||
| 100 | } | ||
| 101 | |||
| 102 | sub assignedSlot { return; } | ||
| 103 | |||
| 104 | sub attachInternals { return; } | ||
| 105 | |||
| 106 | # Note: attributeStyleMap -> property | ||
| 107 | sub attributeStyleMap : lvalue { return( shift->_set_get_property( 'style', @_ ) ); } | ||
| 108 | |||
| 109 | sub before | ||
| 110 | { | ||
| 111 | my $self = shift( @_ ); | ||
| 112 | return( $self ) if( !scalar( @_ ) ); | ||
| 113 | my $parent = $self->parent; | ||
| 114 | return( $self->error( "No parent set for this element, so you cannot set this \"before\" method." ) ) if( !$parent ); | ||
| 115 | my $pos = $parent->children->pos( $self ); | ||
| 116 | $pos--; | ||
| 117 | # If a HTML::Object::DOM::DocumentFragment object is provided, its children are | ||
| 118 | # copied to the list and its own children array is emptied. | ||
| 119 | my $list = $self->_get_from_list_of_elements_or_html( @_ ); | ||
| 120 | $list->foreach(sub | ||
| 121 | { | ||
| 122 | $_->parent( $parent ); | ||
| 123 | $parent->children->splice( $pos + 1, 0, $_ ); | ||
| 124 | $pos++; | ||
| 125 | }); | ||
| 126 | $parent->reset(1); | ||
| 127 | return( $self ); | ||
| 128 | } | ||
| 129 | |||
| 130 | sub blur { return; } | ||
| 131 | |||
| 132 | # NOTE: HTML element property read-pnly | ||
| 133 | sub childElementCount { return( $_[0]->children->grep(sub{ $_[0]->_isa( $_ => 'HTML::Object::DOM::Element' ) })->length ); } | ||
| 134 | |||
| 135 | # NOTE: HTML element property | ||
| 136 | # <https://developer.mozilla.org/en-US/docs/Web/API/Element/classList> | ||
| 137 | sub classList | ||
| 138 | { | ||
| 139 | my $self = shift( @_ ); | ||
| 140 | unless( $self->{_class_list} ) | ||
| 141 | { | ||
| 142 | my $classes = $self->attr( 'class' ); | ||
| 143 | require HTML::Object::TokenList; | ||
| 144 | $self->{_class_list} = HTML::Object::TokenList->new( $classes, element => $self, attribute => 'class' ) || | ||
| 145 | return( $self->pass_error( HTML::Object::TokenList->error ) ); | ||
| 146 | } | ||
| 147 | return( $self->{_class_list} ); | ||
| 148 | } | ||
| 149 | |||
| 150 | # NOTE: Property | ||
| 151 | sub className : lvalue { return( shift->_set_get_callback({ | ||
| 152 | get => sub | ||
| 153 | { | ||
| 154 | my $self = shift( @_ ); | ||
| 155 | return( $self->new_scalar( $self->attr( 'class' ) ) ); | ||
| 156 | }, | ||
| 157 | set => sub | ||
| 158 | { | ||
| 159 | my $self = shift( @_ ); | ||
| 160 | my $arg = shift( @_ ); | ||
| 161 | $self->attr( class => $arg ); | ||
| 162 | $self->reset(1); | ||
| 163 | return( $self->new_scalar( $arg ) ); | ||
| 164 | } | ||
| 165 | }, @_ ) ); } | ||
| 166 | |||
| 167 | sub clientHeight { return; } | ||
| 168 | |||
| 169 | sub clientLeft { return; } | ||
| 170 | |||
| 171 | sub clientTop { return; } | ||
| 172 | |||
| 173 | sub clientWidth { return; } | ||
| 174 | |||
| 175 | sub click { return( shift->trigger( 'click' ) ); } | ||
| 176 | |||
| 177 | # TODO: closest: expand the support for xpath | ||
| 178 | sub closest | ||
| 179 | { | ||
| 180 | my $self = shift( @_ ); | ||
| 181 | # Right now, only support a tag name. | ||
| 182 | my $what = shift( @_ ) || return( $self->error( "No value provided to find ancestor." ) ); | ||
| 183 | $what = lc( $what ); | ||
| 184 | my $lineage = $self->lineage; | ||
| 185 | my $result = $lineage->grep(sub{ $_->tag eq $what }); | ||
| 186 | return( $result->first ); | ||
| 187 | } | ||
| 188 | |||
| 189 | # Taken from HTML::TreeBuilder::XPpath | ||
| 190 | sub cmp | ||
| 191 | { | ||
| 192 | my( $a, $b ) = @_; | ||
| 193 | # comparison with the root (in $b, or processed in HTML::Object::Root) | ||
| 194 | return( -1 ) if( $b->isa( 'HTML::Object::Root' ) ); | ||
| 195 | |||
| 196 | return(0) if( $a->eid eq $b->eid ); | ||
| 197 | # easy cases | ||
| 198 | return( 0 ) if( $a == $b ); | ||
| 199 | # a starts after b | ||
| 200 | return( 1 ) if( $a->is_inside( $b ) ); | ||
| 201 | # a starts before b | ||
| 202 | return( -1 ) if( $b->is_inside( $a ) ); | ||
| 203 | |||
| 204 | # lineage does not include the element itself | ||
| 205 | my $a_pile = $a->lineage->unshift( $a ); | ||
| 206 | my $b_pile = $b->lineage->unshift( $b ); | ||
| 207 | # $a->debug(4); | ||
| 208 | |||
| 209 | # the 2 elements are not in the same twig | ||
| 210 | unless( $a_pile->last == $b_pile->last ) | ||
| 211 | { | ||
| 212 | warn( "2 nodes not in the same pile: " . ref( $a ) . " - " . ref( $b ) . "\n" ) if( warnings::enabled( 'HTML::Object' ) ); | ||
| 213 | # print "a: ", $a->string_value, "\nb: ", $b->string_value, "\n"; | ||
| 214 | return; | ||
| 215 | } | ||
| 216 | |||
| 217 | # find the first non common ancestors (they are siblings) | ||
| 218 | my $a_anc = $a_pile->pop; | ||
| 219 | my $b_anc = $b_pile->pop; | ||
| 220 | |||
| 221 | while( $a_anc == $b_anc ) | ||
| 222 | { | ||
| 223 | $a_anc = $a_pile->pop; | ||
| 224 | $b_anc = $b_pile->pop; | ||
| 225 | } | ||
| 226 | |||
| 227 | if( defined( $a_anc->rank ) && defined( $b_anc->rank ) ) | ||
| 228 | { | ||
| 229 | return( $a_anc->rank <=> $b_anc->rank ); | ||
| 230 | } | ||
| 231 | else | ||
| 232 | { | ||
| 233 | # from there move left and right and figure out the order | ||
| 234 | my( $a_prev, $a_next, $b_prev, $b_next ) = ( $a_anc, $a_anc, $b_anc, $b_anc ); | ||
| 235 | while() | ||
| 236 | { | ||
| 237 | $a_prev = $a_prev->getPreviousSibling || return( -1 ); | ||
| 238 | return( 1 ) if( $a_prev == $b_anc ); | ||
| 239 | $a_next = $a_next->getNextSibling || return( 1 ); | ||
| 240 | return( -1 ) if( $a_next == $b_anc ); | ||
| 241 | $b_prev = $b_prev->getPreviousSibling || return( 1 ); | ||
| 242 | return( -1 ) if( $b_prev == $a_next ); | ||
| 243 | $b_next = $b_next->getNextSibling || return( -1 ); | ||
| 244 | return( 1 ) if( $b_next == $a_prev ); | ||
| 245 | } | ||
| 246 | } | ||
| 247 | } | ||
| 248 | |||
| 249 | # Note: contentEditable -> property | ||
| 250 | sub contentEditable : lvalue { return( shift->_set_get_property( 'contentEditable', @_ ) ); } | ||
| 251 | |||
| 252 | # NOTE: dataset -> property | ||
| 253 | sub dataset | ||
| 254 | { | ||
| 255 | my $self = shift( @_ ); | ||
| 256 | return( $self->{_data_map} ) if( $self->{_data_map} ); | ||
| 257 | $self->_load_class( 'HTML::Object::ElementDataMap' ) || | ||
| 258 | return( $self->pass_error ); | ||
| 259 | my $map = HTML::Object::ElementDataMap->new( $self ) || | ||
| 260 | return( $self->pass_error( HTML::Object::ElementDataMap->error ) ); | ||
| 261 | return( $self->{_data_map} = $map ); | ||
| 262 | } | ||
| 263 | |||
| 264 | # Note: dir -> property | ||
| 265 | sub dir : lvalue { return( shift->_set_get_property( 'dir', @_ ) ); } | ||
| 266 | |||
| 267 | # Note: draggable -> property | ||
| 268 | sub draggable : lvalue { return( shift->_set_get_property( { attribute => 'draggable', is_boolean => 1 }, @_ ) ); } | ||
| 269 | |||
| 270 | # Note: enterKeyHint -> property | ||
| 271 | sub enterKeyHint : lvalue { return( shift->_set_get_property( 'enterKeyHint', @_ ) ); } | ||
| 272 | |||
| 273 | sub firstElementChild | ||
| 274 | { | ||
| 275 | my $self = shift( @_ ); | ||
| 276 | my $children = $self->children; | ||
| 277 | my $elem; | ||
| 278 | $children->foreach(sub | ||
| 279 | { | ||
| 280 | if( $_->isa( 'HTML::Object::DOM::Element' ) ) | ||
| 281 | { | ||
| 282 | $elem = $_; | ||
| 283 | return(0); | ||
| 284 | } | ||
| 285 | return(1); | ||
| 286 | }); | ||
| 287 | return( $self->new_null ) if( !defined( $elem ) ); | ||
| 288 | return( $elem ); | ||
| 289 | } | ||
| 290 | |||
| 291 | sub focus { return; } | ||
| 292 | |||
| 293 | sub getAttribute { return( shift->attributes->get( shift( @_ ) ) ); } | ||
| 294 | |||
| 295 | # We return a clone version to be safe, since we rely on this, so if this get messed up | ||
| 296 | # things will go awry | ||
| 297 | sub getAttributeNames { return( shift->attributes_sequence->clone ); } | ||
| 298 | |||
| 299 | sub getAttributeNode | ||
| 300 | { | ||
| 301 | my $self = shift( @_ ); | ||
| 302 | my $name = shift( @_ ) || return; | ||
| 303 | # new_null is a nifty method inherited from Module::Generic | ||
| 304 | # It returns the right value based on the caller's expectation | ||
| 305 | return( $self->new_null ) if( !$self->attributes->exists( $name ) ); | ||
| 306 | my $val = $self->attributes->get( $name ); | ||
| 307 | my $att = $self->new_attribute( name => $name, value => $val, element => $self ); | ||
| 308 | return( $att ); | ||
| 309 | } | ||
| 310 | |||
| 311 | sub getAttributeNodeNS { return; } | ||
| 312 | |||
| 313 | sub getAttributeNS { return; } | ||
| 314 | |||
| 315 | # Note: method getAttributes is inherited | ||
| 316 | |||
| 317 | # Note: method getChildNodes is inherited | ||
| 318 | |||
| 319 | sub getElementById | ||
| 320 | { | ||
| 321 | my( $self, $id ) = @_; | ||
| 322 | return( $self->error( "No id was provided to get its corresponding element." ) ) if( !defined( $id ) || !CORE::length( $id ) ); | ||
| 323 | return( $self->look_down( id => $id )->first ); | ||
| 324 | } | ||
| 325 | |||
| 326 | sub getElementsByClassName | ||
| 327 | { | ||
| 328 | my $self = shift( @_ ); | ||
| 329 | my @args = (); | ||
| 330 | if( scalar( @_ ) == 1 ) | ||
| 331 | { | ||
| 332 | @args = split( /[[:blank:]\h]+/, $_[0] ); | ||
| 333 | } | ||
| 334 | else | ||
| 335 | { | ||
| 336 | @args = @_; | ||
| 337 | } | ||
| 338 | my $results = $self->new_array; | ||
| 339 | my $test = $self->new_array( \@args )->unique(1); | ||
| 340 | my $totalClassRequired = $test->length->scalar; | ||
| 341 | # Nothing to do somehow | ||
| 342 | return( $results ) if( !$totalClassRequired ); | ||
| 343 | |||
| 344 | my $seen = {}; | ||
| 345 | my $crawl; | ||
| 346 | $crawl = sub | ||
| 347 | { | ||
| 348 | my $kid = shift( @_ ); | ||
| 349 | $kid->children->foreach(sub | ||
| 350 | { | ||
| 351 | my $e = shift( @_ ); | ||
| 352 | # Avoid looping | ||
| 353 | my $addr = Scalar::Util::refaddr( $e ); | ||
| 354 | return(1) if( CORE::exists( $seen->{ $addr } ) ); | ||
| 355 | $seen->{ $addr }++; | ||
| 356 | if( $e->attributes->exists( 'class' ) ) | ||
| 357 | { | ||
| 358 | my $val = $self->new_scalar( $e->attributes->get( 'class' ) ); | ||
| 359 | $val->trim( qr/[[:blank:]\h]+/ ); | ||
| 360 | if( !$val->is_empty ) | ||
| 361 | { | ||
| 362 | my $classes = $val->split( qr/[[:blank:]\h]+/ ); | ||
| 363 | my $found = 0; | ||
| 364 | $test->foreach(sub | ||
| 365 | { | ||
| 366 | $found++ if( $classes->has( $_ ) ); | ||
| 367 | }); | ||
| 368 | $results->push( $e ) if( $found == $totalClassRequired ); | ||
| 369 | } | ||
| 370 | } | ||
| 371 | $crawl->( $e ); | ||
| 372 | # Always return true | ||
| 373 | return(1); | ||
| 374 | }); | ||
| 375 | }; | ||
| 376 | $crawl->( $self ); | ||
| 377 | return( $results ); | ||
| 378 | } | ||
| 379 | |||
| 380 | sub getElementsByTagName | ||
| 381 | { | ||
| 382 | my $self = shift( @_ ); | ||
| 383 | my $name = shift( @_ ); | ||
| 384 | my $results = $self->new_array; | ||
| 385 | # Nothing to do somehow | ||
| 386 | return( $self->error( "No name was provided for getElementsByTagName()" ) ) if( !defined( $name ) || !CORE::length( "$name" ) ); | ||
| 387 | |||
| 388 | my $seen = {}; | ||
| 389 | my $crawl; | ||
| 390 | $crawl = sub | ||
| 391 | { | ||
| 392 | my $kid = shift( @_ ); | ||
| 393 | $kid->children->foreach(sub | ||
| 394 | { | ||
| 395 | my $e = shift( @_ ); | ||
| 396 | # Avoid looping | ||
| 397 | my $addr = Scalar::Util::refaddr( $e ); | ||
| 398 | return(1) if( CORE::exists( $seen->{ $addr } ) ); | ||
| 399 | $seen->{ $addr }++; | ||
| 400 | if( $e->tag eq $name ) | ||
| 401 | { | ||
| 402 | $results->push( $e ); | ||
| 403 | } | ||
| 404 | $crawl->( $e ); | ||
| 405 | # Always return true | ||
| 406 | return(1); | ||
| 407 | }); | ||
| 408 | }; | ||
| 409 | $crawl->( $self ); | ||
| 410 | return( $results ); | ||
| 411 | } | ||
| 412 | |||
| 413 | # Credits John Resig | ||
| 414 | # <https://johnresig.com/blog/comparing-document-position/> | ||
| 415 | # Original by PPK quirksmode.org | ||
| 416 | sub getElementsByTagNames | ||
| 417 | { | ||
| 418 | my $self = shift( @_ ); | ||
| 419 | my $this; | ||
| 420 | my $results = $self->new_array; | ||
| 421 | if( scalar( @_ ) == 1 && !ref( $_[0] ) ) | ||
| 422 | { | ||
| 423 | $this = [split( /[[:blank:]\h]+/, $this )]; | ||
| 424 | } | ||
| 425 | elsif( scalar( @_ ) == 1 && $self->_is_array( $this ) ) | ||
| 426 | { | ||
| 427 | # Good as-is | ||
| 428 | } | ||
| 429 | # list of elements | ||
| 430 | elsif( scalar( @_ ) > 1 ) | ||
| 431 | { | ||
| 432 | $this = [@_]; | ||
| 433 | } | ||
| 434 | else | ||
| 435 | { | ||
| 436 | return( $results ); | ||
| 437 | } | ||
| 438 | |||
| 439 | my $tags = $self->new_array( $this ); | ||
| 440 | $tags->foreach(sub | ||
| 441 | { | ||
| 442 | my $elems = $self->getElementsByTagName( $_ ); | ||
| 443 | $results->push( $elems->list ) if( !$elems->is_empty ); | ||
| 444 | }); | ||
| 445 | $results->unique(1); | ||
| 446 | return( $results ); | ||
| 447 | } | ||
| 448 | |||
| 449 | # sub getFirstChild { return( shift->children->first ); } | ||
| 450 | # Note: method getFirstChild is inherited | ||
| 451 | |||
| 452 | # Note: method getLastChild is inherited | ||
| 453 | |||
| 454 | sub getLocalName | ||
| 455 | { | ||
| 456 | my $self = shift( @_ ); | ||
| 457 | ( my $name = $self->tag ) =~ s{^.*:}{}; | ||
| 458 | return( $name ); | ||
| 459 | } | ||
| 460 | |||
| 461 | sub getName { return( shift->tag ); } | ||
| 462 | |||
| 463 | # sub getNextSibling { return( shift->right->first ); } | ||
| 464 | # Note: method getNextSibling is inherited | ||
| 465 | |||
| 466 | sub getNodePath | ||
| 467 | { | ||
| 468 | my $self = shift( @_ ); | ||
| 469 | my $a; | ||
| 470 | my $init; | ||
| 471 | if( @_ ) | ||
| 472 | { | ||
| 473 | $a = shift( @_ ); | ||
| 474 | } | ||
| 475 | else | ||
| 476 | { | ||
| 477 | $a = $self->new_array; | ||
| 478 | $init = 1; | ||
| 479 | } | ||
| 480 | return if( $self->isa( 'HTML::Object::Text' ) || $self->isa( 'HTML::Object::Comment' ) || $self->isa( 'HTML::Object::Declaration' ) ); | ||
| 481 | my $tag = $self->tag; | ||
| 482 | my $parent = $self->parent; | ||
| 483 | if( !defined( $parent ) ) | ||
| 484 | { | ||
| 485 | return( $a ) if( !defined( $tag ) || $tag CORE::eq '_document' ); | ||
| 486 | $a->unshift( $tag ); | ||
| 487 | return( $a ); | ||
| 488 | } | ||
| 489 | my $nth = 0; | ||
| 490 | my $pos = 0; | ||
| 491 | $parent->children->foreach(sub | ||
| 492 | { | ||
| 493 | if( $_->tag CORE::eq $self->tag ) | ||
| 494 | { | ||
| 495 | $nth++; | ||
| 496 | if( $_->eid CORE::eq $self->eid ) | ||
| 497 | { | ||
| 498 | $pos = $nth; | ||
| 499 | } | ||
| 500 | } | ||
| 501 | # Continue to the next one | ||
| 502 | return(1); | ||
| 503 | }); | ||
| 504 | $a->unshift( $nth > 1 ? "${tag}\[${pos}\]" : $tag ); | ||
| 505 | return( $parent->getNodePath( $a ) ) unless( $init ); | ||
| 506 | my $xpath = '/' . $a->join( '/' ); | ||
| 507 | return( $xpath ); | ||
| 508 | } | ||
| 509 | |||
| 510 | sub getParentNode | ||
| 511 | { | ||
| 512 | my $self = shift( @_ ); | ||
| 513 | return( $self->parent || $self->new_root( root => $self ) ); | ||
| 514 | } | ||
| 515 | |||
| 516 | # Note: getPreviousSibling is inherited | ||
| 517 | |||
| 518 | sub getValue | ||
| 519 | { | ||
| 520 | my $self = shift( @_ ); | ||
| 521 | # return( $self->text ) if( $self->isCommentNode ); | ||
| 522 | return( $self->value ) if( $self->isCommentNode ); | ||
| 523 | return( $self->as_text ); | ||
| 524 | } | ||
| 525 | |||
| 526 | sub hasAttribute { return( shift->attributes->has( shift( @_ ) ) ); } | ||
| 527 | |||
| 528 | sub hasAttributes { return( !shift->attributes->is_empty ); } | ||
| 529 | |||
| 530 | # Note: hidden -> property | ||
| 531 | sub hidden : lvalue { return( shift->_set_get_property( { attribute => 'hidden', is_boolean => 1 }, @_ ) ); } | ||
| 532 | |||
| 533 | sub hidePopover { return; } | ||
| 534 | |||
| 535 | # Note: inert -> property | ||
| 536 | sub inert : lvalue { return( shift->_set_get_property( { attribute => 'inert', is_boolean => 1 }, @_ ) ); } | ||
| 537 | |||
| 538 | sub innerHTML : lvalue { return( shift->_set_get_callback({ | ||
| 539 | get => sub | ||
| 540 | { | ||
| 541 | my $self = shift( @_ ); | ||
| 542 | # Create a new document, because we want to use the document object as_string function which produce a string of its children, and no need to reproduce it here | ||
| 543 | my $doc = $self->new_document; | ||
| 544 | $doc->children( $self->children ); | ||
| 545 | return( $doc->as_string ); | ||
| 546 | }, | ||
| 547 | set => sub | ||
| 548 | { | ||
| 549 | my $self = shift( @_ ); | ||
| 550 | my $this = shift( @_ ); | ||
| 551 | my $children; | ||
| 552 | if( !ref( $this ) || | ||
| 553 | ( ref( $this ) && overload::Overloaded( $this ) && overload::Method( $this, '""' ) ) ) | ||
| 554 | { | ||
| 555 | my $p = $self->new_parser; | ||
| 556 | my $res = $p->parse_data( "$this" ) || | ||
| 557 | die( "Error while parsing html data provided: " . $p->error ); | ||
| 558 | $children = $res->children; | ||
| 559 | } | ||
| 560 | # We are provided with an element, so we set it as our inner html | ||
| 561 | elsif( $self->_is_a( $this => 'HTML::Object::Element' ) ) | ||
| 562 | { | ||
| 563 | # If a HTML::Object::DOM::DocumentFragment object is provided, its children are | ||
| 564 | # copied to the list and its own children array is emptied. | ||
| 565 | if( $self->_is_a( $this => 'HTML::Object::DOM::DocumentFragment' ) ) | ||
| 566 | { | ||
| 567 | $children = $this->children->clone; | ||
| 568 | $this->children->reset; | ||
| 569 | } | ||
| 570 | else | ||
| 571 | { | ||
| 572 | my $child = $this->clone; | ||
| 573 | $children = $self->new_array( $child ); | ||
| 574 | } | ||
| 575 | } | ||
| 576 | else | ||
| 577 | { | ||
| 578 | die( "I was expecting some html data in replacement of html for this element \"" . $self->tag . "\", but instead got '" . ( CORE::length( $this ) > 1024 ? ( CORE::substr( $this, 0, 1024 ) . '...' ) : $this ) . "'." ); | ||
| 579 | } | ||
| 580 | |||
| 581 | $children->foreach(sub | ||
| 582 | { | ||
| 583 | $_->parent( $self ); | ||
| 584 | }); | ||
| 585 | $self->children( $children ); | ||
| 586 | $self->reset(1); | ||
| 587 | return(1); | ||
| 588 | } | ||
| 589 | }, @_ ) ); } | ||
| 590 | |||
| 591 | # Note: innerText -> property | ||
| 592 | sub innerText : lvalue { return( shift->_set_get_callback({ | ||
| 593 | get => sub | ||
| 594 | { | ||
| 595 | my $self = shift( @_ ); | ||
| 596 | # Create a new document, because we want to use the document object as_string function which produce a string of its children, and no need to reproduce it here | ||
| 597 | my $txt = $self->as_trimmed_text; | ||
| 598 | $txt =~ s,<br[[:blank:]]*/>\n,\n,gs; | ||
| 599 | $txt =~ s/\>/>/gs; | ||
| 600 | $txt =~ s/\</</gs; | ||
| 601 | my $obj = $self->new_scalar( \$txt ); | ||
| 602 | return( $obj ); | ||
| 603 | }, | ||
| 604 | set => sub | ||
| 605 | { | ||
| 606 | my $self = shift( @_ ); | ||
| 607 | my $this = shift( @_ ); | ||
| 608 | my $children; | ||
| 609 | # We are provided with an element, so we set it as our inner html | ||
| 610 | if( $self->_is_a( $this => 'HTML::Object::DOM::Text' ) ) | ||
| 611 | { | ||
| 612 | my $copy = $this->clone; | ||
| 613 | my $val = $copy->value; | ||
| 614 | $val =~ s/</\</gs; | ||
| 615 | $val =~ s/>/\>/gs; | ||
| 616 | $val =~ s,\n,<br />\n,gs; | ||
| 617 | $copy->value( $val ); | ||
| 618 | $children = $self->new_array( $copy ); | ||
| 619 | } | ||
| 620 | elsif( !ref( $this ) || | ||
| 621 | ( ref( $this ) && overload::Overloaded( $this ) && overload::Method( $this, '""' ) ) ) | ||
| 622 | { | ||
| 623 | # $this =~ s,\n,<br />\n,gs; | ||
| 624 | my $val = "$this"; | ||
| 625 | $val =~ s/</\</gs; | ||
| 626 | $val =~ s/>/\>/gs; | ||
| 627 | $val =~ s,\n,<br />\n,gs; | ||
| 628 | my $txt = $self->new_text( value => $val ) || die( $self->error ); | ||
| 629 | $children = $self->new_array( $txt ); | ||
| 630 | } | ||
| 631 | else | ||
| 632 | { | ||
| 633 | die( "I was expecting some text data in replacement of html for this element \"" . $self->tag . "\", but instead got '" . ( CORE::length( $this ) > 1024 ? ( CORE::substr( $this, 0, 1024 ) . '...' ) : $this ) . "'." ); | ||
| 634 | } | ||
| 635 | |||
| 636 | $children->foreach(sub | ||
| 637 | { | ||
| 638 | $_->parent( $self ); | ||
| 639 | }); | ||
| 640 | $self->children( $children ); | ||
| 641 | $self->reset(1); | ||
| 642 | return(1); | ||
| 643 | } | ||
| 644 | }, @_ ) ); } | ||
| 645 | |||
| 646 | # Note: inputMode -> property | ||
| 647 | sub inputMode : lvalue { return( shift->_set_get_property( 'inputMode', @_ ) ); } | ||
| 648 | |||
| 649 | sub insertAdjacentElement | ||
| 650 | { | ||
| 651 | my $self = shift( @_ ); | ||
| 652 | my( $pos, $elem ) = @_; | ||
| 653 | return( $self->error({ | ||
| 654 | message => 'No position was provided', | ||
| 655 | code => 500, | ||
| 656 | class => 'HTML::Object::SyntaxError', | ||
| 657 | }) ) if( !defined( $pos ) || !CORE::length( "$pos" ) ); | ||
| 658 | # Return error if the element provided is either undefined , or an empty string | ||
| 659 | return( $self->error({ | ||
| 660 | message => "No element was provided.", | ||
| 661 | code => 500, | ||
| 662 | class => 'HTML::Object::TypeError', | ||
| 663 | }) ) if( !defined( $elem ) || ( !ref( $elem ) && !CORE::length( $elem ) ) ); | ||
| 664 | $pos = lc( "$pos" ); | ||
| 665 | # Error if the position string provided is of an unknown value. | ||
| 666 | return( $self->error({ | ||
| 667 | message => "Position provided \"$pos\" is not a recognised value. Use beforebegin, afterbegin, beforeend or afterend", | ||
| 668 | code => 500, | ||
| 669 | class => 'HTML::Object::SyntaxError', | ||
| 670 | }) ) if( $pos !~ /^(?:beforebegin|afterbegin|beforeend|afterend)$/ ); | ||
| 671 | # Error if the element value provided is not an element object. | ||
| 672 | return( $self->error({ | ||
| 673 | message => "Element provided (" . overload::StrVal( $elem ) . ") is not an HTML::Object::DOM::Element object.", | ||
| 674 | code => 500, | ||
| 675 | class => 'HTML::Object::TypeError', | ||
| 676 | }) ) if( !$self->_is_a( $elem => 'HTML::Object::DOM::Element' ) ); | ||
| 677 | my $parent = $self->parent; | ||
| 678 | return( $self->error({ | ||
| 679 | message => "Current object has no parent, so the provided element cannot be inserted before or after it.", | ||
| 680 | code => 500, | ||
| 681 | class => 'HTML::Object::HierarchyRequestError', | ||
| 682 | }) ) if( !$parent && ( $pos eq 'beforebegin' || $pos eq 'afterend' ) ); | ||
| 683 | if( $pos eq 'beforebegin' ) | ||
| 684 | { | ||
| 685 | my $offset = $parent->children->pos( $self ); | ||
| 686 | if( !defined( $offset ) ) | ||
| 687 | { | ||
| 688 | return( $self->error({ | ||
| 689 | message => "The current element (" . overload::StrVal( $self ) . ") could not be found in its parent element (" . overload::StrVal( $parent ) . ") whose tag is \"" . $parent->tag . "\".", | ||
| 690 | code => 500, | ||
| 691 | class => 'HTML::Object::HierarchyRequestError', | ||
| 692 | }) ); | ||
| 693 | } | ||
| 694 | else | ||
| 695 | { | ||
| 696 | $parent->splice( $offset, 0, $elem ); | ||
| 697 | } | ||
| 698 | } | ||
| 699 | elsif( $pos eq 'beforeend' ) | ||
| 700 | { | ||
| 701 | $self->children->push( $elem ); | ||
| 702 | } | ||
| 703 | elsif( $pos eq 'afterbegin' ) | ||
| 704 | { | ||
| 705 | $self->children->unshift( $elem ); | ||
| 706 | } | ||
| 707 | elsif( $pos eq 'afterend' ) | ||
| 708 | { | ||
| 709 | my $offset = $parent->children->pos( $self ); | ||
| 710 | if( !defined( $offset ) ) | ||
| 711 | { | ||
| 712 | return( $self->error({ | ||
| 713 | message => "The current element (" . overload::StrVal( $self ) . ") could not be found in its parent element (" . overload::StrVal( $parent ) . ") whose tag is \"" . $parent->tag . "\".", | ||
| 714 | code => 500, | ||
| 715 | class => 'HTML::Object::HierarchyRequestError', | ||
| 716 | }) ); | ||
| 717 | } | ||
| 718 | $parent->splice( ++$offset, 0, $elem ); | ||
| 719 | } | ||
| 720 | return( $elem ); | ||
| 721 | } | ||
| 722 | |||
| 723 | sub insertAdjacentHTML | ||
| 724 | { | ||
| 725 | my $self = shift( @_ ); | ||
| 726 | my( $pos, $html ) = @_; | ||
| 727 | return( $self->error({ | ||
| 728 | message => 'No position was provided', | ||
| 729 | code => 500, | ||
| 730 | class => 'HTML::Object::SyntaxError', | ||
| 731 | }) ) if( !defined( $pos ) || !CORE::length( "$pos" ) ); | ||
| 732 | # Return error if the element provided is either undefined , or an empty string | ||
| 733 | return( $self->error({ | ||
| 734 | message => "No html string was provided to insert.", | ||
| 735 | code => 500, | ||
| 736 | class => 'HTML::Object::TypeError', | ||
| 737 | }) ) if( !defined( $html ) || !CORE::length( "$html" ) ); | ||
| 738 | return( $self->error({ | ||
| 739 | message => "A reference (" . ref( $html ) . ") was provided instead of an HTML string.", | ||
| 740 | code => 500, | ||
| 741 | class => 'HTML::Object::TypeError', | ||
| 742 | }) ) if( ref( $html ) && !overload::Method( $html, '""' ) ); | ||
| 743 | $html = "$html"; | ||
| 744 | my $p = $self->new_parser || return( $self->pass_error ); | ||
| 745 | my $doc = $p->parse_data( $html ) || return( $self->pass_error( $p->error ) ); | ||
| 746 | my $parent = $self->parent; | ||
| 747 | return( $self->error({ | ||
| 748 | message => "Current object has no parent, so the provided html nodes cannot be inserted before or after it.", | ||
| 749 | code => 500, | ||
| 750 | class => 'HTML::Object::HierarchyRequestError', | ||
| 751 | }) ) if( !$parent && ( $pos eq 'beforebegin' || $pos eq 'afterend' ) ); | ||
| 752 | if( $pos eq 'beforebegin' ) | ||
| 753 | { | ||
| 754 | my $offset = $parent->children->pos( $self ); | ||
| 755 | if( !defined( $offset ) ) | ||
| 756 | { | ||
| 757 | return( $self->error({ | ||
| 758 | message => "The current element (" . overload::StrVal( $self ) . ") could not be found in its parent element (" . overload::StrVal( $parent ) . ") whose tag is \"" . $parent->tag . "\".", | ||
| 759 | code => 500, | ||
| 760 | class => 'HTML::Object::HierarchyRequestError', | ||
| 761 | }) ); | ||
| 762 | } | ||
| 763 | $doc->children->foreach(sub | ||
| 764 | { | ||
| 765 | my $elem = shift( @_ ); | ||
| 766 | $elem->parent( $parent ); | ||
| 767 | $parent->children->splice( $offset, 0, $elem ); | ||
| 768 | $offset++; | ||
| 769 | }); | ||
| 770 | } | ||
| 771 | elsif( $pos eq 'beforeend' ) | ||
| 772 | { | ||
| 773 | $doc->children->foreach(sub | ||
| 774 | { | ||
| 775 | my $elem = shift( @_ ); | ||
| 776 | $elem->parent( $self ); | ||
| 777 | $self->children->push( $elem ); | ||
| 778 | }); | ||
| 779 | } | ||
| 780 | elsif( $pos eq 'afterbegin' ) | ||
| 781 | { | ||
| 782 | my $offset = -1; | ||
| 783 | $doc->children->foreach(sub | ||
| 784 | { | ||
| 785 | my $elem = shift( @_ ); | ||
| 786 | $elem->parent( $self ); | ||
| 787 | $self->children->splice( ++$offset, 0, $elem ); | ||
| 788 | }); | ||
| 789 | # $self->children->unshift( $elem ); | ||
| 790 | } | ||
| 791 | elsif( $pos eq 'afterend' ) | ||
| 792 | { | ||
| 793 | my $offset = $parent->children->pos( $self ); | ||
| 794 | if( !defined( $offset ) ) | ||
| 795 | { | ||
| 796 | return( $self->error({ | ||
| 797 | message => "The current element (" . overload::StrVal( $self ) . ") could not be found in its parent element (" . overload::StrVal( $parent ) . ") whose tag is \"" . $parent->tag . "\".", | ||
| 798 | code => 500, | ||
| 799 | class => 'HTML::Object::HierarchyRequestError', | ||
| 800 | }) ); | ||
| 801 | } | ||
| 802 | $doc->children->foreach(sub | ||
| 803 | { | ||
| 804 | my $elem = shift( @_ ); | ||
| 805 | $elem->parent( $parent ); | ||
| 806 | $parent->children->splice( ++$offset, 0, $elem ); | ||
| 807 | }); | ||
| 808 | } | ||
| 809 | return( $doc->children ); | ||
| 810 | } | ||
| 811 | |||
| 812 | sub insertAdjacentText | ||
| 813 | { | ||
| 814 | my $self = shift( @_ ); | ||
| 815 | my $pos = shift( @_ ); | ||
| 816 | return( $self->error({ | ||
| 817 | message => 'No position was provided', | ||
| 818 | code => 500, | ||
| 819 | class => 'HTML::Object::SyntaxError', | ||
| 820 | }) ) if( !defined( $pos ) || !CORE::length( "$pos" ) ); | ||
| 821 | my $text; | ||
| 822 | if( !scalar( @_ ) || | ||
| 823 | ( scalar( @_ ) == 1 && !defined( $_[0] ) ) || | ||
| 824 | ( scalar( @_ ) > 1 && !CORE::length( $text = join( '', @_ ) ) ) ) | ||
| 825 | { | ||
| 826 | return( $self->error({ | ||
| 827 | message => "No text was provided.", | ||
| 828 | code => 500, | ||
| 829 | class => 'HTML::Object::TypeError', | ||
| 830 | }) ); | ||
| 831 | } | ||
| 832 | return( $self->error({ | ||
| 833 | message => "A reference (" . ref( $text ) . ") was provided instead of an text string.", | ||
| 834 | code => 500, | ||
| 835 | class => 'HTML::Object::TypeError', | ||
| 836 | }) ) if( ref( $text ) && !overload::Method( $text, '""' ) ); | ||
| 837 | my $node = $self->new_text( value => "$text" ); | ||
| 838 | my $parent = $self->parent; | ||
| 839 | return( $self->error({ | ||
| 840 | message => "Current object has no parent, so the provided text cannot be inserted before or after it.", | ||
| 841 | code => 500, | ||
| 842 | class => 'HTML::Object::HierarchyRequestError', | ||
| 843 | }) ) if( !$parent && ( $pos eq 'beforebegin' || $pos eq 'afterend' ) ); | ||
| 844 | if( $pos eq 'beforebegin' ) | ||
| 845 | { | ||
| 846 | my $offset = $parent->children->pos( $self ); | ||
| 847 | if( !defined( $offset ) ) | ||
| 848 | { | ||
| 849 | return( $self->error({ | ||
| 850 | message => "The current element (" . overload::StrVal( $self ) . ") could not be found in its parent element (" . overload::StrVal( $parent ) . ") whose tag is \"" . $parent->tag . "\".", | ||
| 851 | code => 500, | ||
| 852 | class => 'HTML::Object::HierarchyRequestError', | ||
| 853 | }) ); | ||
| 854 | } | ||
| 855 | else | ||
| 856 | { | ||
| 857 | $parent->splice( $offset, 0, $node ); | ||
| 858 | } | ||
| 859 | } | ||
| 860 | elsif( $pos eq 'beforeend' ) | ||
| 861 | { | ||
| 862 | $self->children->push( $node ); | ||
| 863 | } | ||
| 864 | elsif( $pos eq 'afterbegin' ) | ||
| 865 | { | ||
| 866 | $self->children->unshift( $node ); | ||
| 867 | } | ||
| 868 | elsif( $pos eq 'afterend' ) | ||
| 869 | { | ||
| 870 | my $offset = $parent->children->pos( $self ); | ||
| 871 | if( !defined( $offset ) ) | ||
| 872 | { | ||
| 873 | return( $self->error({ | ||
| 874 | message => "The current element (" . overload::StrVal( $self ) . ") could not be found in its parent element (" . overload::StrVal( $parent ) . ") whose tag is \"" . $parent->tag . "\".", | ||
| 875 | code => 500, | ||
| 876 | class => 'HTML::Object::HierarchyRequestError', | ||
| 877 | }) ); | ||
| 878 | } | ||
| 879 | $parent->splice( ++$offset, 0, $node ); | ||
| 880 | } | ||
| 881 | return( $node ); | ||
| 882 | } | ||
| 883 | |||
| 884 | sub is_inside | ||
| 885 | { | ||
| 886 | my $self = shift( @_ ); | ||
| 887 | return( 0 ) if( !scalar( @_ ) ); | ||
| 888 | my @elems = @_; | ||
| 889 | my @literals = (); | ||
| 890 | for( my $i = 0; $i < scalar( @elems ); $i++ ) | ||
| 891 | { | ||
| 892 | return( $self->error( "The element provided (", overload::StrVal( $elems[$i] ), ") is not an HTML::Object::Element object." ) ) if( ref( $elems[$i] ) && ( !$self->_is_object( $elems[$i] ) || !$elems[$i]->isa( 'HTML::Object::Element' ) ) ); | ||
| 893 | push( @literals, splice( @elems, $i, 1 ) ) if( !ref( $elems[$i] ) ); | ||
| 894 | } | ||
| 895 | # We need to ensure the literals provided, if any, are in lowercase | ||
| 896 | # @$lit{ @literals } = (1) x scalar( @literals ); | ||
| 897 | my $lit = +{ map( lc( $_ ), @literals ) }; | ||
| 898 | my $obj = +{ map{ $_->eid => 1 } @elems }; | ||
| 899 | my $parent = $self; | ||
| 900 | # Check if ourself for any of our parent are a match of any of the element given | ||
| 901 | while( $parent ) | ||
| 902 | { | ||
| 903 | return( 1 ) if( exists( $obj->{ $parent->eid } ) || exists( $lit->{ $parent->tag } ) ); | ||
| 904 | $parent = $parent->parent; | ||
| 905 | } | ||
| 906 | return( 0 ); | ||
| 907 | } | ||
| 908 | |||
| 909 | sub isAttributeNode { return(0); } | ||
| 910 | |||
| 911 | sub isCommentNode { return( shift->tag CORE::eq '_comment' ? 1 : 0 ); } | ||
| 912 | |||
| 913 | sub isContentEditable | ||
| 914 | { | ||
| 915 | my $self = shift( @_ ); | ||
| 916 | return( $self->contentEditable ? $self->true : $self->false ); | ||
| 917 | } | ||
| 918 | |||
| 919 | sub isElementNode { return( shift->tag->substr( 0, 1 ) CORE::eq '_' ? 0 : 1 ); } | ||
| 920 | |||
| 921 | sub isNamespaceNode { return(0); } | ||
| 922 | |||
| 923 | sub isPINode { return(0); } | ||
| 924 | |||
| 925 | sub isProcessingInstructionNode { return( shift->tag CORE::eq '_pi' ? 1 : 0 ); } | ||
| 926 | |||
| 927 | sub isTextNode { return( shift->tag CORE::eq '_text' ? 1 : 0 ); } | ||
| 928 | |||
| 929 | # Note: lang -> property | ||
| 930 | sub lang : lvalue { return( shift->_set_get_property( 'lang', @_ ) ); } | ||
| 931 | |||
| 932 | sub lastElementChild | ||
| 933 | { | ||
| 934 | my $self = shift( @_ ); | ||
| 935 | my $children = $self->children; | ||
| 936 | my $elem; | ||
| 937 | $children->reverse->foreach(sub | ||
| 938 | { | ||
| 939 | if( $_->isa( 'HTML::Object::DOM::Element' ) ) | ||
| 940 | { | ||
| 941 | $elem = $_; | ||
| 942 | return(0); | ||
| 943 | } | ||
| 944 | return(1); | ||
| 945 | }); | ||
| 946 | return( $self->new_null ) if( !defined( $elem ) ); | ||
| 947 | return( $elem ); | ||
| 948 | } | ||
| 949 | |||
| 950 | sub localName { return( shift->getName ); } | ||
| 951 | |||
| 952 | sub matches | ||
| 953 | { | ||
| 954 | my $self = shift( @_ ); | ||
| 955 | my $selector = shift( @_ ); | ||
| 956 | my $opts = $self->_get_args_as_hash( @_ ); | ||
| 957 | my $params = {}; | ||
| 958 | # The only supported parameter by HTML::Selector::XPath | ||
| 959 | $params->{root} = CORE::delete( $opts->{root} ) if( CORE::exists( $opts->{root} ) ); | ||
| 960 | if( !$self->{_xp} ) | ||
| 961 | { | ||
| 962 | $self->_load_class( 'HTML::Object::XPath' ) || | ||
| 963 | return( $self->pass_error ); | ||
| 964 | my $xp = HTML::Object::XPath->new; | ||
| 965 | $self->{_xp} = $xp; | ||
| 966 | } | ||
| 967 | $self->_load_class( 'HTML::Selector::XPath', { version => '0.20' } ) || | ||
| 968 | return( $self->pass_error ); | ||
| 969 | my $xpath; | ||
| 970 | # try-catch | ||
| 971 | local $@; | ||
| 972 | eval | ||
| 973 | { | ||
| 974 | my $sel = HTML::Selector::XPath->new( $selector, %$params ); | ||
| 975 | $xpath = $sel->to_xpath( %$params ); | ||
| 976 | }; | ||
| 977 | if( $@ ) | ||
| 978 | { | ||
| 979 | return( $self->error( "Error trying to get the xpath value for selector \"$selector\": $@" ) ); | ||
| 980 | } | ||
| 981 | my $xp = $self->{_xp}; | ||
| 982 | $self->message( 4, "Calling xp->matches for xpath '$xpath' with context '", $self->as_string, "'" ); | ||
| 983 | return( $xp->matches( $self, $xpath, $self ) ); | ||
| 984 | } | ||
| 985 | |||
| 986 | sub namespaceURI { return; } | ||
| 987 | |||
| 988 | sub new_attribute | ||
| 989 | { | ||
| 990 | my $self = shift( @_ ); | ||
| 991 | $self->_load_class( 'HTML::Object::DOM::Attribute' ) || return( $self->pass_error ); | ||
| 992 | my $att = HTML::Object::DOM::Attribute->new( @_ ) || | ||
| 993 | return( $self->pass_error( HTML::Object::DOM::Attribute->error ) ); | ||
| 994 | return( $att ); | ||
| 995 | } | ||
| 996 | |||
| 997 | sub new_closing | ||
| 998 | { | ||
| 999 | my $self = shift( @_ ); | ||
| 1000 | $self->_load_class( 'HTML::Object::DOM::Closing' ) || return( $self->pass_error ); | ||
| 1001 | my $e = HTML::Object::DOM::Closing->new( @_ ) || | ||
| 1002 | return( $self->pass_error( HTML::Object::DOM::Closing->error ) ); | ||
| 1003 | return( $e ); | ||
| 1004 | } | ||
| 1005 | |||
| 1006 | sub new_collection_elements | ||
| 1007 | { | ||
| 1008 | my $self = shift( @_ ); | ||
| 1009 | $self->_load_class( 'HTML::Object::DOM::Collection' ) || return( $self->pass_error ); | ||
| 1010 | my $e = HTML::Object::DOM::Collection->new( @_ ) || | ||
| 1011 | return( $self->pass_error( HTML::Object::DOM::Collection->error ) ); | ||
| 1012 | return( $e ); | ||
| 1013 | } | ||
| 1014 | |||
| 1015 | sub new_comment | ||
| 1016 | { | ||
| 1017 | my $self = shift( @_ ); | ||
| 1018 | $self->_load_class( 'HTML::Object::DOM::Comment' ) || return( $self->pass_error ); | ||
| 1019 | my $e = HTML::Object::DOM::Comment->new( @_ ) || | ||
| 1020 | return( $self->pass_error( HTML::Object::DOM::Comment->error ) ); | ||
| 1021 | return( $e ); | ||
| 1022 | } | ||
| 1023 | |||
| 1024 | sub new_document | ||
| 1025 | { | ||
| 1026 | my $self = shift( @_ ); | ||
| 1027 | $self->_load_class( 'HTML::Object::DOM::Document' ) || return( $self->pass_error ); | ||
| 1028 | my $e = HTML::Object::DOM::Document->new( debug => $self->debug ) || | ||
| 1029 | return( $self->pass_error( HTML::Object::DOM::Document->error ) ); | ||
| 1030 | return( $e ); | ||
| 1031 | } | ||
| 1032 | |||
| 1033 | sub new_element | ||
| 1034 | { | ||
| 1035 | my $self = shift( @_ ); | ||
| 1036 | my $tag = shift( @_ ) || return( $self->error( "No tag was provided to create an element." ) ); | ||
| 1037 | my $dict = HTML::Object->get_definition( $tag ) || return( $self->pass_error( HTML::Object->error ) ); | ||
| 1038 | my $e = HTML::Object::DOM::Element->new({ | ||
| 1039 | is_empty => $dict->{is_empty}, | ||
| 1040 | tag => $dict->{tag}, | ||
| 1041 | debug => $self->debug, | ||
| 1042 | }) || return( $self->pass_error( HTML::Object::DOM::Element->error ) ); | ||
| 1043 | return( $e ); | ||
| 1044 | } | ||
| 1045 | |||
| 1046 | sub new_nodelist | ||
| 1047 | { | ||
| 1048 | my $self = shift( @_ ); | ||
| 1049 | $self->_load_class( 'HTML::Object::DOM::NodeList' ) || return( $self->pass_error ); | ||
| 1050 | my $list = HTML::Object::DOM::NodeList->new( @_ ) || | ||
| 1051 | return( $self->pass_error( HTML::Object::DOM::NodeList->error ) ); | ||
| 1052 | return( $list ); | ||
| 1053 | } | ||
| 1054 | |||
| 1055 | sub new_parser | ||
| 1056 | { | ||
| 1057 | my $self = shift( @_ ); | ||
| 1058 | $self->_load_class( 'HTML::Object::DOM' ) || return( $self->pass_error ); | ||
| 1059 | my $p = HTML::Object::DOM->new( debug => $self->debug ) || | ||
| 1060 | return( $self->pass_error( HTML::Object::DOM->error ) ); | ||
| 1061 | return( $p ); | ||
| 1062 | } | ||
| 1063 | |||
| 1064 | sub new_space | ||
| 1065 | { | ||
| 1066 | my $self = shift( @_ ); | ||
| 1067 | $self->_load_class( 'HTML::Object::DOM::Space' ) || return( $self->pass_error ); | ||
| 1068 | my $e = HTML::Object::DOM::Space->new( @_ ) || | ||
| 1069 | return( $self->pass_error( HTML::Object::DOM::Space->error ) ); | ||
| 1070 | return( $e ); | ||
| 1071 | } | ||
| 1072 | |||
| 1073 | sub new_text | ||
| 1074 | { | ||
| 1075 | my $self = shift( @_ ); | ||
| 1076 | $self->_load_class( 'HTML::Object::DOM::Text' ) || return( $self->pass_error ); | ||
| 1077 | my $e = HTML::Object::DOM::Text->new( @_ ) || | ||
| 1078 | return( $self->pass_error( HTML::Object::DOM::Text->error ) ); | ||
| 1079 | return( $e ); | ||
| 1080 | } | ||
| 1081 | |||
| 1082 | sub nextElementSibling | ||
| 1083 | { | ||
| 1084 | my $self = shift( @_ ); | ||
| 1085 | my $all = $self->right; | ||
| 1086 | for( my $i = 0; $i < scalar( @$all ); $i++ ) | ||
| 1087 | { | ||
| 1088 | return( $all->[$i] ) if( $self->_is_a( $all->[$i] => 'HTML::Object::DOM::Element' ) ); | ||
| 1089 | } | ||
| 1090 | return( $self->new_null ); | ||
| 1091 | } | ||
| 1092 | |||
| 1093 | # Note: noModule -> property | ||
| 1094 | sub noModule : lvalue { return( shift->_set_get_property( { attribute => 'noModule', is_boolean => 1 }, @_ ) ); } | ||
| 1095 | |||
| 1096 | # Note: nonce -> property | ||
| 1097 | sub nonce : lvalue { return( shift->_set_get_property( 'nonce', @_ ) ); } | ||
| 1098 | |||
| 1099 | # Note: offsetHeight -> property | ||
| 1100 | sub offsetHeight : lvalue { return( shift->_set_get_property( 'offsetheight', @_ ) ); } | ||
| 1101 | |||
| 1102 | # Note: offsetLeft -> property | ||
| 1103 | sub offsetLeft : lvalue { return( shift->_set_get_property( 'offsetleft', @_ ) ); } | ||
| 1104 | |||
| 1105 | # Note: offsetParent -> property | ||
| 1106 | sub offsetParent { return( shift->parent( @_ ) ); } | ||
| 1107 | |||
| 1108 | # Note: offsetTop -> property | ||
| 1109 | sub offsetTop : lvalue { return( shift->_set_get_property( 'offsettop', @_ ) ); } | ||
| 1110 | |||
| 1111 | # Note: offsetWidth -> property | ||
| 1112 | sub offsetWidth : lvalue { return( shift->_set_get_property( 'offsetwidth', @_ ) ); } | ||
| 1113 | |||
| 1114 | sub onerror : lvalue { return( shift->_set_get_code( '_error_handler', @_ ) ); } | ||
| 1115 | |||
| 1116 | # Note: Property | ||
| 1117 | sub outerHTML : lvalue { return( shift->_set_get_callback({ | ||
| 1118 | get => sub | ||
| 1119 | { | ||
| 1120 | my $self = shift( @_ ); | ||
| 1121 | return( $self->as_string ); | ||
| 1122 | }, | ||
| 1123 | set => sub | ||
| 1124 | { | ||
| 1125 | my $self = shift( @_ ); | ||
| 1126 | my $this = shift( @_ ); | ||
| 1127 | my $children; | ||
| 1128 | my $pos; | ||
| 1129 | my $parent = $self->parent; | ||
| 1130 | $pos = $parent->children->pos( $self ) if( $parent ); | ||
| 1131 | my $dummy; | ||
| 1132 | if( !ref( $this ) || | ||
| 1133 | ( ref( $this ) && overload::Overloaded( $this ) && overload::Method( $this, '""' ) ) ) | ||
| 1134 | { | ||
| 1135 | # User provided an empty string, so we just remove the element | ||
| 1136 | if( !CORE::length( $this ) ) | ||
| 1137 | { | ||
| 1138 | if( defined( $pos ) ) | ||
| 1139 | { | ||
| 1140 | $parent->children->splice( $pos, 0 ); | ||
| 1141 | # If this element has a closing tag in the dom, we remove it too | ||
| 1142 | if( my $close = $self->close_tag ) | ||
| 1143 | { | ||
| 1144 | $parent->children->remove( $close ); | ||
| 1145 | $close->parent( undef ); | ||
| 1146 | } | ||
| 1147 | $self->parent( undef ); | ||
| 1148 | $self->parent->reset(1); | ||
| 1149 | $dummy = 1; | ||
| 1150 | } | ||
| 1151 | # Fallback | ||
| 1152 | else | ||
| 1153 | { | ||
| 1154 | $dummy = 0; | ||
| 1155 | } | ||
| 1156 | return( $dummy ); | ||
| 1157 | } | ||
| 1158 | else | ||
| 1159 | { | ||
| 1160 | my $p = $self->new_parser; | ||
| 1161 | my $res = $p->parse_data( "$this" ) || | ||
| 1162 | return( $self->error( "Error while parsing html data provided: ", $p->error ) ); | ||
| 1163 | $children = $res->children; | ||
| 1164 | if( !$children->is_empty && defined( $pos ) ) | ||
| 1165 | { | ||
| 1166 | $children->foreach(sub | ||
| 1167 | { | ||
| 1168 | $_->parent( $parent ); | ||
| 1169 | }); | ||
| 1170 | $parent->children->splice( $pos, 1, $children->list ); | ||
| 1171 | # If this element has a closing tag in the dom, we remove it too | ||
| 1172 | if( my $close = $self->close_tag ) | ||
| 1173 | { | ||
| 1174 | $parent->children->remove( $close ); | ||
| 1175 | $close->parent( undef ); | ||
| 1176 | } | ||
| 1177 | $parent->reset(1); | ||
| 1178 | $dummy = 1; | ||
| 1179 | } | ||
| 1180 | else | ||
| 1181 | { | ||
| 1182 | $dummy = 0; | ||
| 1183 | } | ||
| 1184 | return( $dummy ); | ||
| 1185 | } | ||
| 1186 | } | ||
| 1187 | # We are provided with an element, so we set it as our inner html | ||
| 1188 | elsif( $self->_is_a( $this => 'HTML::Object::Element' ) ) | ||
| 1189 | { | ||
| 1190 | # If a HTML::Object::DOM::DocumentFragment object is provided, its children are | ||
| 1191 | # copied to the list and its own children array is emptied. | ||
| 1192 | if( $self->_is_a( $this => 'HTML::Object::DOM::DocumentFragment' ) ) | ||
| 1193 | { | ||
| 1194 | my $copy = $this->children->clone; | ||
| 1195 | $this->children->reset; | ||
| 1196 | if( defined( $pos ) ) | ||
| 1197 | { | ||
| 1198 | $parent->children->splice( $pos, 1, $copy->list ); | ||
| 1199 | $copy->children->foreach(sub | ||
| 1200 | { | ||
| 1201 | $_->parent( $parent ); | ||
| 1202 | }); | ||
| 1203 | # The element itself is being replace, so we remove out own parent | ||
| 1204 | $self->parent->reset(1); | ||
| 1205 | $self->parent( undef ); | ||
| 1206 | # If this element has a closing tag in the dom, we remove it too | ||
| 1207 | if( my $close = $self->close_tag ) | ||
| 1208 | { | ||
| 1209 | $parent->children->remove( $close ); | ||
| 1210 | $close->parent( undef ); | ||
| 1211 | } | ||
| 1212 | $dummy = 1; | ||
| 1213 | } | ||
| 1214 | else | ||
| 1215 | { | ||
| 1216 | $dummy = 0; | ||
| 1217 | } | ||
| 1218 | } | ||
| 1219 | else | ||
| 1220 | { | ||
| 1221 | my $child = $this->clone; | ||
| 1222 | if( defined( $pos ) ) | ||
| 1223 | { | ||
| 1224 | $parent->children->splice( $pos, 1, $child ); | ||
| 1225 | # Add the closing tag if any | ||
| 1226 | if( my $close = $child->close_tag ) | ||
| 1227 | { | ||
| 1228 | $parent->children->splice( $pos + 1, 0, $close ); | ||
| 1229 | # $parent->children->splice( 2, 0, $close ); | ||
| 1230 | } | ||
| 1231 | $child->parent( $parent ); | ||
| 1232 | # The element itself is being replace, so we remove out own parent | ||
| 1233 | $self->parent->reset(1); | ||
| 1234 | $self->parent( undef ); | ||
| 1235 | # If this element has a closing tag in the dom, we remove it too | ||
| 1236 | if( my $close = $self->close_tag ) | ||
| 1237 | { | ||
| 1238 | $parent->children->remove( $close ); | ||
| 1239 | $close->parent( undef ); | ||
| 1240 | } | ||
| 1241 | $dummy = 1; | ||
| 1242 | } | ||
| 1243 | else | ||
| 1244 | { | ||
| 1245 | $dummy = 0; | ||
| 1246 | } | ||
| 1247 | } | ||
| 1248 | return( $dummy ); | ||
| 1249 | } | ||
| 1250 | else | ||
| 1251 | { | ||
| 1252 | die( "I was expecting some html data in replacement of html for this element \"" . $self->tag . "\", but instead got '" . ( CORE::length( $this ) > 1024 ? ( CORE::substr( $this, 0, 1024 ) . '...' ) : $this ) . "'." ); | ||
| 1253 | } | ||
| 1254 | } | ||
| 1255 | }, @_ ) ); } | ||
| 1256 | |||
| 1257 | # Note: outerText -> property | ||
| 1258 | sub outerText : lvalue { return( shift->_set_get_callback({ | ||
| 1259 | get => sub | ||
| 1260 | { | ||
| 1261 | my $self = shift( @_ ); | ||
| 1262 | # Create a new document, because we want to use the document object as_string function which produce a string of its children, and no need to reproduce it here | ||
| 1263 | my $txt = $self->as_trimmed_text; | ||
| 1264 | my $obj = $self->new_scalar( \$txt ); | ||
| 1265 | return( $obj ); | ||
| 1266 | }, | ||
| 1267 | set => sub | ||
| 1268 | { | ||
| 1269 | my $self = shift( @_ ); | ||
| 1270 | my $this = shift( @_ ); | ||
| 1271 | my $element; | ||
| 1272 | # We are provided with an element, so we set it as our inner html | ||
| 1273 | if( $self->_is_a( $this => 'HTML::Object::DOM::Text' ) ) | ||
| 1274 | { | ||
| 1275 | $element = $this->clone; | ||
| 1276 | } | ||
| 1277 | elsif( !ref( $this ) || | ||
| 1278 | ( ref( $this ) && overload::Overloaded( $this ) && overload::Method( $this, '""' ) ) ) | ||
| 1279 | { | ||
| 1280 | $this =~ s,\n,<br />\n,gs; | ||
| 1281 | $element = $self->new_text( value => $this ) || die( $self->error ); | ||
| 1282 | } | ||
| 1283 | else | ||
| 1284 | { | ||
| 1285 | die( "I was expecting some text data in replacement of html for this element \"" . $self->tag . "\", but instead got '" . ( CORE::length( $this ) > 1024 ? ( CORE::substr( $this, 0, 1024 ) . '...' ) : $this ) . "'." ); | ||
| 1286 | } | ||
| 1287 | |||
| 1288 | my $parent = $self->parent; | ||
| 1289 | my $pos = $parent->children->pos( $self ); | ||
| 1290 | if( !defined( $pos ) ) | ||
| 1291 | { | ||
| 1292 | die( "Unable to find the current element among its parent's children." ); | ||
| 1293 | } | ||
| 1294 | |||
| 1295 | $element->parent( $parent ); | ||
| 1296 | $parent->splice( $pos, 1, $element ); | ||
| 1297 | $self->parent( undef() ); | ||
| 1298 | $parent->reset(1); | ||
| 1299 | my $dummy = 'dummy'; | ||
| 1300 | return( $dummy ); | ||
| 1301 | } | ||
| 1302 | }, @_ ) ); } | ||
| 1303 | |||
| 1304 | # Note: popover -> property | ||
| 1305 | sub popover : lvalue { return( shift->_set_get_callback({ | ||
| 1306 | get => sub | ||
| 1307 | { | ||
| 1308 | my $self = shift( @_ ); | ||
| 1309 | my $val = $self->_set_get_property( 'popover' ); | ||
| 1310 | return( $val ) if( defined( $val ) ); | ||
| 1311 | return( $self->root->_set_get_property( 'popover' ) ); | ||
| 1312 | }, | ||
| 1313 | set => sub | ||
| 1314 | { | ||
| 1315 | my $self = shift( @_ ); | ||
| 1316 | my $arg = shift( @_ ); | ||
| 1317 | $self->_set_get_property( 'popover', $arg ); | ||
| 1318 | return( $arg ); | ||
| 1319 | } | ||
| 1320 | }, @_ ) ); } | ||
| 1321 | |||
| 1322 | sub prefix { return; } | ||
| 1323 | |||
| 1324 | sub prepend | ||
| 1325 | { | ||
| 1326 | my $self = shift( @_ ); | ||
| 1327 | return( $self->error({ | ||
| 1328 | message => "No data to prepend was provided.", | ||
| 1329 | code => 500, | ||
| 1330 | class => 'HTML::Object::SyntaxError', | ||
| 1331 | }) ) if( !scalar( @_ ) ); | ||
| 1332 | # If a HTML::Object::DOM::DocumentFragment object is provided, its children are | ||
| 1333 | # copied to the list and its own children array is emptied. | ||
| 1334 | my $list = $self->_get_from_list_of_elements_or_html( @_ ); | ||
| 1335 | my $children = $self->children; | ||
| 1336 | my $pos = -1; | ||
| 1337 | $list->foreach(sub | ||
| 1338 | { | ||
| 1339 | $_->parent( $self ); | ||
| 1340 | $children->splice( ++$pos, 0, $_ ); | ||
| 1341 | }); | ||
| 1342 | $self->reset(1); | ||
| 1343 | return( $list ); | ||
| 1344 | } | ||
| 1345 | |||
| 1346 | sub previousElementSibling | ||
| 1347 | { | ||
| 1348 | my $self = shift( @_ ); | ||
| 1349 | my $all = $self->left->reverse; | ||
| 1350 | for( my $i = 0; $i < scalar( @$all ); $i++ ) | ||
| 1351 | { | ||
| 1352 | return( $all->[$i] ) if( $self->_is_a( $all->[$i] => 'HTML::Object::DOM::Element' ) ); | ||
| 1353 | } | ||
| 1354 | return( $self->new_null ); | ||
| 1355 | } | ||
| 1356 | |||
| 1357 | # Note: properties -> property experimental | ||
| 1358 | sub properties { return( shift->new_array ); } | ||
| 1359 | |||
| 1360 | sub querySelector | ||
| 1361 | { | ||
| 1362 | my $self = shift( @_ ); | ||
| 1363 | my @sels = @_; | ||
| 1364 | return( $self->error({ | ||
| 1365 | message => "No CSS selector was provided to query.", | ||
| 1366 | code => 500, | ||
| 1367 | class => 'HTML::Object::SyntaxError', | ||
| 1368 | }) ) if( !scalar( @sels ) ); | ||
| 1369 | |||
| 1370 | foreach my $sel ( @sels ) | ||
| 1371 | { | ||
| 1372 | my $results = $self->findNode( $sel, { root => '.' } ) || | ||
| 1373 | return( $self->pass_error({ class => 'HTML::Object::SyntaxError' }) ); | ||
| 1374 | return( $results->first ) if( !$results->is_empty ); | ||
| 1375 | } | ||
| 1376 | return( $self->new_null ); | ||
| 1377 | } | ||
| 1378 | |||
| 1379 | sub querySelectorAll | ||
| 1380 | { | ||
| 1381 | my $self = shift( @_ ); | ||
| 1382 | my @sels = @_; | ||
| 1383 | return( $self->error({ | ||
| 1384 | message => "No CSS selector was provided to query.", | ||
| 1385 | code => 500, | ||
| 1386 | class => 'HTML::Object::SyntaxError', | ||
| 1387 | }) ) if( !scalar( @sels ) ); | ||
| 1388 | |||
| 1389 | my $results = $self->new_array; | ||
| 1390 | foreach my $sel ( @sels ) | ||
| 1391 | { | ||
| 1392 | my $elems = $self->findNode( $sel, { root => './' } ) || | ||
| 1393 | return( $self->pass_error({ class => 'HTML::Object::SyntaxError' }) ); | ||
| 1394 | $results->push( $elems->list ) if( !$elems->is_empty ); | ||
| 1395 | } | ||
| 1396 | $results->unique(1); | ||
| 1397 | return( $results ); | ||
| 1398 | } | ||
| 1399 | |||
| 1400 | sub remove | ||
| 1401 | { | ||
| 1402 | my $self = shift( @_ ); | ||
| 1403 | my $parent = $self->parent; | ||
| 1404 | return( $self->error({ | ||
| 1405 | message => "This element has no parent, and thus cannot be removed.", | ||
| 1406 | code => 500, | ||
| 1407 | class => 'HTML::Object::HierarchyRequestError', | ||
| 1408 | }) ) if( !$parent ); | ||
| 1409 | my $pos = $parent->children->pos( $self ); | ||
| 1410 | return( $self->error({ | ||
| 1411 | message => "This element could not be found among its parent's children.", | ||
| 1412 | code => 500, | ||
| 1413 | class => 'HTML::Object::HierarchyRequestError', | ||
| 1414 | }) ) if( !defined( $pos ) ); | ||
| 1415 | $parent->children->splice( $pos, 1 ); | ||
| 1416 | $parent->reset(1); | ||
| 1417 | return( $self->true ); | ||
| 1418 | } | ||
| 1419 | |||
| 1420 | sub removeAttribute | ||
| 1421 | { | ||
| 1422 | my $self = shift( @_ ); | ||
| 1423 | my $name = shift( @_ ); | ||
| 1424 | return( $self->error({ | ||
| 1425 | message => "No attribute name was provided.", | ||
| 1426 | code => 500, | ||
| 1427 | class => 'HTML::Object::SyntaxError', | ||
| 1428 | }) ) if( !defined( $name ) || !CORE::length( $name ) ); | ||
| 1429 | if( $self->attributes->has( $name ) ) | ||
| 1430 | { | ||
| 1431 | $self->attributes->remove( $name ); | ||
| 1432 | $self->attributes_sequence->remove( $name ); | ||
| 1433 | return( $self ); | ||
| 1434 | } | ||
| 1435 | return; | ||
| 1436 | } | ||
| 1437 | |||
| 1438 | sub removeAttributeNode | ||
| 1439 | { | ||
| 1440 | my $self = shift( @_ ); | ||
| 1441 | my $node = shift( @_ ) || return( $self->error({ | ||
| 1442 | message => "No attribute node was provided to remove.", | ||
| 1443 | code => 500, | ||
| 1444 | class => 'HTML::Object::SyntaxError', | ||
| 1445 | }) ); | ||
| 1446 | return( $self->error({ | ||
| 1447 | message => "Object provided is not an attribute node.", | ||
| 1448 | code => 500, | ||
| 1449 | class => 'HTML::Object::SyntaxError', | ||
| 1450 | }) ) if( !$self->_is_a( $node => 'HTML::Object::DOM::Attribute' ) ); | ||
| 1451 | my $name = $node->name; | ||
| 1452 | return( $self->error({ | ||
| 1453 | message => "Attribute node provided has no name value.", | ||
| 1454 | code => 500, | ||
| 1455 | class => 'HTML::Object::SyntaxError', | ||
| 1456 | }) ) if( $name->is_empty ); | ||
| 1457 | return( $self->removeAttribute( $name ) ); | ||
| 1458 | } | ||
| 1459 | |||
| 1460 | sub removeAttributeNS { return; } | ||
| 1461 | |||
| 1462 | sub replaceChildren | ||
| 1463 | { | ||
| 1464 | my $self = shift( @_ ); | ||
| 1465 | my $results = $self->new_array; | ||
| 1466 | my $children = $self->children; | ||
| 1467 | if( !scalar( @_ ) ) | ||
| 1468 | { | ||
| 1469 | $results->push( $children->list ); | ||
| 1470 | $children->reset; | ||
| 1471 | $results->foreach(sub | ||
| 1472 | { | ||
| 1473 | $_->parent( undef ); | ||
| 1474 | }); | ||
| 1475 | return( $results ); | ||
| 1476 | } | ||
| 1477 | my $new = $self->_list_to_nodes( @_ ) || return( $self->pass_error({ class => 'HTML::Object::SyntaxError' }) ); | ||
| 1478 | |||
| 1479 | # We take some care to keep the same original array, so that if it is used or | ||
| 1480 | # referenced elsewhere it continues to be valid, as a 'live' array of (new) elements | ||
| 1481 | $children->foreach(sub | ||
| 1482 | { | ||
| 1483 | $_->parent( undef() ); | ||
| 1484 | }); | ||
| 1485 | $results->push( $children->list ); | ||
| 1486 | # We empty it, and pu the new content inside | ||
| 1487 | $children->reset; | ||
| 1488 | |||
| 1489 | $new->foreach(sub | ||
| 1490 | { | ||
| 1491 | $_->parent( $self ); | ||
| 1492 | $children->push( $_ ); | ||
| 1493 | }); | ||
| 1494 | # Return the old set | ||
| 1495 | return( $results ); | ||
| 1496 | } | ||
| 1497 | |||
| 1498 | sub replaceWith | ||
| 1499 | { | ||
| 1500 | my $self = shift( @_ ); | ||
| 1501 | return( $self->error({ | ||
| 1502 | message => "No data was provided to replace this element.", | ||
| 1503 | code => 500, | ||
| 1504 | class => 'HTML::Object::SyntaxError', | ||
| 1505 | }) ) if( !scalar( @_ ) ); | ||
| 1506 | my $parent = $self->parent; | ||
| 1507 | return( $self->error({ | ||
| 1508 | message => "Current object does not have a parent", | ||
| 1509 | code => 500, | ||
| 1510 | class => 'HTML::Object::HierarchyRequestError', | ||
| 1511 | }) ) if( !$parent ); | ||
| 1512 | my $new = $self->_list_to_nodes( @_ ) || return( $self->pass_error({ class => 'HTML::Object::SyntaxError' }) ); | ||
| 1513 | my $pos = $parent->children->pos( $self ); | ||
| 1514 | $parent->children->splice( $pos, 1, $new->list ); | ||
| 1515 | $new->foreach(sub | ||
| 1516 | { | ||
| 1517 | $_->parent( $parent ); | ||
| 1518 | }); | ||
| 1519 | return( $new ); | ||
| 1520 | } | ||
| 1521 | |||
| 1522 | sub scrollHeight { return; } | ||
| 1523 | |||
| 1524 | sub scrollLeft { return; } | ||
| 1525 | |||
| 1526 | sub scrollTop { return; } | ||
| 1527 | |||
| 1528 | sub scrollWidth { return; } | ||
| 1529 | |||
| 1530 | sub setAttribute | ||
| 1531 | { | ||
| 1532 | my $self = shift( @_ ); | ||
| 1533 | my( $name, $value ) = @_; | ||
| 1534 | return( $self->error({ | ||
| 1535 | message => "No attribute name was provided.", | ||
| 1536 | code => 500, | ||
| 1537 | class => 'HTML::Object::SyntaxError', | ||
| 1538 | }) ) if( !defined( $name ) || !CORE::length( $name ) ); | ||
| 1539 | |||
| 1540 | # Inherited from HTML::Object::Element | ||
| 1541 | if( !$self->is_valid_attribute( $name ) ) | ||
| 1542 | { | ||
| 1543 | return( $self->error({ | ||
| 1544 | message => "Attribute name provided \"$name\" contains illegal characters.", | ||
| 1545 | code => 500, | ||
| 1546 | class => 'HTML::Object::InvalidCharacterError', | ||
| 1547 | }) ); | ||
| 1548 | } | ||
| 1549 | $name = lc( $name ); | ||
| 1550 | |||
| 1551 | if( !defined( $value ) ) | ||
| 1552 | { | ||
| 1553 | return( $self->removeAttribute( $name ) ); | ||
| 1554 | } | ||
| 1555 | $self->attributes->set( $name => $value ); | ||
| 1556 | $self->attributes_sequence->push( $name ) if( !$self->attributes_sequence->has( $name ) ); | ||
| 1557 | return( $self ); | ||
| 1558 | } | ||
| 1559 | |||
| 1560 | sub setAttributeNode | ||
| 1561 | { | ||
| 1562 | my $self = shift( @_ ); | ||
| 1563 | my $att = shift( @_ ); | ||
| 1564 | return( $self->error({ | ||
| 1565 | message => "No attribute name was provided.", | ||
| 1566 | code => 500, | ||
| 1567 | class => 'HTML::Object::SyntaxError', | ||
| 1568 | }) ) if( !defined( $att ) ); | ||
| 1569 | return( $self->error({ | ||
| 1570 | message => "Attribute node provided (", overload::StrVal( $att ), ") is not actually an HTML::Object::DOM::Attribute object.", | ||
| 1571 | code => 500, | ||
| 1572 | class => 'HTML::Object::TypeError', | ||
| 1573 | }) ) if( !$self->_is_a( $att => 'HTML::Object::DOM::Attribute' ) ); | ||
| 1574 | return( $self->error({ | ||
| 1575 | message => "Attribute node object provided has no attribute name set.", | ||
| 1576 | code => 500, | ||
| 1577 | class => 'HTML::Object::SyntaxError', | ||
| 1578 | }) ) if( !$att->name->defined || $att->name->is_empty ); | ||
| 1579 | my $old = $self->getAttributeNode( $att->name ); | ||
| 1580 | $self->setAttribute( $att->name, $att->value ) || return( $self->pass_error ); | ||
| 1581 | return( $old ); | ||
| 1582 | } | ||
| 1583 | |||
| 1584 | # setHTML is a mutator only | ||
| 1585 | sub setHTML : lvalue { return( shift->_set_get_callback({ | ||
| 1586 | set => sub | ||
| 1587 | { | ||
| 1588 | my $self = shift( @_ ); | ||
| 1589 | my $this = shift( @_ ); | ||
| 1590 | if( !defined( $this ) || !CORE::length( $this ) ) | ||
| 1591 | { | ||
| 1592 | die( "No html provided." ); | ||
| 1593 | } | ||
| 1594 | |||
| 1595 | my $children; | ||
| 1596 | if( $self->_is_a( $this => 'HTML::Object::Element' ) ) | ||
| 1597 | { | ||
| 1598 | if( $self->_is_a( $this => 'HTML::Object::DOM::DocumentFragment' ) ) | ||
| 1599 | { | ||
| 1600 | $children = $this->children->clone; | ||
| 1601 | $this->children->reset; | ||
| 1602 | # DocumentFragment children are not cloned, but moved as per the specification | ||
| 1603 | $children->foreach(sub | ||
| 1604 | { | ||
| 1605 | $_->detach; | ||
| 1606 | }); | ||
| 1607 | } | ||
| 1608 | else | ||
| 1609 | { | ||
| 1610 | my $clone = $this->clone; | ||
| 1611 | $children = $self->new_array( $clone ); | ||
| 1612 | $children->push( $clone->close_tag ) if( $clone->close_tag ); | ||
| 1613 | } | ||
| 1614 | } | ||
| 1615 | else | ||
| 1616 | { | ||
| 1617 | if( ref( $this ) && ( !$self->_is_object( $this ) || ( $self->_is_object( $this ) && !overload::Method( $this, '""' ) ) ) ) | ||
| 1618 | { | ||
| 1619 | die( "I was expecting some HTML data, but got '" . overload::StrVal( $this ) . "'" ); | ||
| 1620 | } | ||
| 1621 | my $p = $self->new_parser; | ||
| 1622 | my $res = $p->parse_data( "$this" ) || | ||
| 1623 | die( "Error while parsing html data provided: " . $p->error ); | ||
| 1624 | $children = $res->children; | ||
| 1625 | } | ||
| 1626 | $children->foreach(sub | ||
| 1627 | { | ||
| 1628 | $_->parent( $self ); | ||
| 1629 | }); | ||
| 1630 | $self->children( $children ); | ||
| 1631 | return( $self ); | ||
| 1632 | } | ||
| 1633 | }, @_ ) ); } | ||
| 1634 | |||
| 1635 | sub shadowRoot { return; } | ||
| 1636 | |||
| 1637 | sub showPopover { return; } | ||
| 1638 | |||
| 1639 | # Note: spellcheck -> property | ||
| 1640 | sub spellcheck : lvalue { return( shift->_set_get_property( { attribute => 'spellcheck', is_boolean => 1 }, @_ ) ); } | ||
| 1641 | |||
| 1642 | sub string_value | ||
| 1643 | { | ||
| 1644 | my $self = shift( @_ ); | ||
| 1645 | my $type = $self->nodeType; | ||
| 1646 | if( $type == TEXT_NODE || $type == COMMENT_NODE ) | ||
| 1647 | { | ||
| 1648 | return( $self->value ); | ||
| 1649 | } | ||
| 1650 | else | ||
| 1651 | { | ||
| 1652 | return( $self->as_text ); | ||
| 1653 | } | ||
| 1654 | } | ||
| 1655 | |||
| 1656 | # Note: style -> property | ||
| 1657 | sub style { return( shift->new_hash ); } | ||
| 1658 | |||
| 1659 | # Note: property | ||
| 1660 | sub tabIndex : lvalue { return( shift->_set_get_property( 'tabindex', @_ ) ); } | ||
| 1661 | |||
| 1662 | sub tagName { return( shift->getName ); } | ||
| 1663 | |||
| 1664 | # Note: title -> property | ||
| 1665 | sub title : lvalue { return( shift->_set_get_property( 'title', @_ ) ); } | ||
| 1666 | |||
| 1667 | sub to_number | ||
| 1668 | { | ||
| 1669 | my $self = shift( @_ ); | ||
| 1670 | return( $self->new_number( $self->as_text ) ); | ||
| 1671 | } | ||
| 1672 | |||
| 1673 | # Based on the polyfill provided by Mozilla at: | ||
| 1674 | # <https://developer.mozilla.org/en-US/docs/Web/API/Element/toggleAttribute> | ||
| 1675 | # because, otherwise, as of 2021-12-15, the description of the use of 'force' is cryptic | ||
| 1676 | sub toggleAttribute | ||
| 1677 | { | ||
| 1678 | my $self = shift( @_ ); | ||
| 1679 | my( $name, $force ) = @_; | ||
| 1680 | return( $self->error({ | ||
| 1681 | message => "No attribute name was provided.", | ||
| 1682 | code => 500, | ||
| 1683 | class => 'HTML::Object::SyntaxError', | ||
| 1684 | }) ) if( !defined( $name ) || !CORE::length( $name ) ); | ||
| 1685 | |||
| 1686 | # Inherited from HTML::Object::Element | ||
| 1687 | if( !$self->is_valid_attribute( $name ) ) | ||
| 1688 | { | ||
| 1689 | return( $self->error({ | ||
| 1690 | message => "Attribute name provided \"$name\" contains illegal characters.", | ||
| 1691 | code => 500, | ||
| 1692 | class => 'HTML::Object::InvalidCharacterError', | ||
| 1693 | }) ); | ||
| 1694 | } | ||
| 1695 | $name = lc( $name ); | ||
| 1696 | if( $self->attribute->has( $name ) ) | ||
| 1697 | { | ||
| 1698 | return( $self->true ) if( defined( $force ) && $force ); | ||
| 1699 | $self->removeAttribute( $name ); | ||
| 1700 | return( $self->false ); | ||
| 1701 | } | ||
| 1702 | return( $self->false ) if( defined( $force ) && !$force ); | ||
| 1703 | $self->setAttribute( $name => '' ); | ||
| 1704 | return( $self->true ); | ||
| 1705 | } | ||
| 1706 | |||
| 1707 | sub togglePopover { return; } | ||
| 1708 | |||
| 1709 | sub toString { return( shift->as_string ); } | ||
| 1710 | |||
| 1711 | # Note: translate -> property | ||
| 1712 | sub translate : lvalue { return( shift->_set_get_property( { attribute => 'translate', is_boolean => 1 }, @_ ) ); } | ||
| 1713 | |||
| 1714 | # Used by HTML::Object::DOM::Element::* | ||
| 1715 | sub _get_parent_form { return( shift->closest( 'form' ) ); } | ||
| 1716 | |||
| 1717 | # Note: moved _list_to_nodes to HTML::Object::DOM::Node to make it also available to HTML::Object::DOM::Declaration | ||
| 1718 | |||
| 1719 | # Used by HTML::Object::DOM::Element::Anchor and HTML::Object::DOM::Element::Area | ||
| 1720 | sub _set_get_anchor_uri | ||
| 1721 | { | ||
| 1722 | my $self = shift( @_ ); | ||
| 1723 | my $link = $self->href; | ||
| 1724 | # We constantly get a new URI object, because the value of the href attribute may have been altered by other means | ||
| 1725 | # try-catch | ||
| 1726 | local $@; | ||
| 1727 | my $rv = eval | ||
| 1728 | { | ||
| 1729 | return( $link ) if( $self->_is_a( $link => 'URI' ) ); | ||
| 1730 | return( ( defined( $link ) && CORE::length( "$link" ) ) ? URI->new( $link ) : URI->new ); | ||
| 1731 | }; | ||
| 1732 | if( $@ ) | ||
| 1733 | { | ||
| 1734 | return( $self->error( "Unable to create a URI object from \"$link\" (", overload::StrVal( $link ), "): $@" ) ); | ||
| 1735 | } | ||
| 1736 | return( $rv ); | ||
| 1737 | } | ||
| 1738 | |||
| 1739 | sub _set_get_form_attribute : lvalue | ||
| 1740 | { | ||
| 1741 | my $self = shift( @_ ); | ||
| 1742 | my $attr = shift( @_ ); | ||
| 1743 | |||
| 1744 | return( $self->_set_get_callback({ | ||
| 1745 | get => sub | ||
| 1746 | { | ||
| 1747 | my $form = $self->_get_parent_form; | ||
| 1748 | if( !defined( $form ) ) | ||
| 1749 | { | ||
| 1750 | return; | ||
| 1751 | } | ||
| 1752 | my $code = $form->can( $attr ); | ||
| 1753 | if( !defined( $code ) ) | ||
| 1754 | { | ||
| 1755 | die( "Form object has no method \"$attr\"." ); | ||
| 1756 | } | ||
| 1757 | |||
| 1758 | my $rv = $code->( $form ); | ||
| 1759 | return( $rv ); | ||
| 1760 | }, | ||
| 1761 | set => sub | ||
| 1762 | { | ||
| 1763 | my $arg = shift( @_ ); | ||
| 1764 | my $ctx = $_; | ||
| 1765 | my $form = $self->_get_parent_form; | ||
| 1766 | if( !defined( $form ) ) | ||
| 1767 | { | ||
| 1768 | return; | ||
| 1769 | } | ||
| 1770 | my $code = $form->can( $attr ); | ||
| 1771 | if( !defined( $code ) ) | ||
| 1772 | { | ||
| 1773 | die( "Form object has no method \"$attr\"." ); | ||
| 1774 | } | ||
| 1775 | |||
| 1776 | $code->( $form, $arg ); | ||
| 1777 | return( $arg ) if( $ctx->{assign} ); | ||
| 1778 | return( $self ); | ||
| 1779 | }, | ||
| 1780 | }, @_ ) ); | ||
| 1781 | } | ||
| 1782 | |||
| 1783 | # _set_get_property has been moved up in HTML::Object::Element | ||
| 1784 | # Note: private method to set or get attribute as an lvalue method for DOM properties in HTML::Object::DOM::Element::* and also for some DOM List abstract class like HTML::Object::DOM::List | ||
| 1785 | sub _set_get_property : lvalue | ||
| 1786 | { | ||
| 1787 | my $self = shift( @_ ); | ||
| 1788 | my $attr = shift( @_ ); | ||
| 1789 | |||
| 1790 | my $def = {}; | ||
| 1791 | # If the $attr parameter is an hash reference, it is used to provide more information | ||
| 1792 | # such as whether this property is a boolean | ||
| 1793 | if( ref( $attr ) eq 'HASH' ) | ||
| 1794 | { | ||
| 1795 | $def = $attr; | ||
| 1796 | $attr = $def->{attribute}; | ||
| 1797 | } | ||
| 1798 | $def->{is_boolean} //= 0; | ||
| 1799 | |||
| 1800 | return( $self->_set_get_callback({ | ||
| 1801 | get => sub | ||
| 1802 | { | ||
| 1803 | my $self = shift( @_ ); | ||
| 1804 | if( $def->{is_datetime} ) | ||
| 1805 | { | ||
| 1806 | my $val = $self->attr( $attr ); | ||
| 1807 | # try-catch | ||
| 1808 | local $@; | ||
| 1809 | my $rv = eval | ||
| 1810 | { | ||
| 1811 | my $dt = $self->_parse_timestamp( $val ); | ||
| 1812 | return( $self->pass_error ) if( !defined( $dt ) ); | ||
| 1813 | return( $dt ); | ||
| 1814 | }; | ||
| 1815 | if( $@ ) | ||
| 1816 | { | ||
| 1817 | return( $self->error( "Unable to parse datetime value \"$val\": $@" ) ); | ||
| 1818 | } | ||
| 1819 | return( $rv ); | ||
| 1820 | } | ||
| 1821 | elsif( $def->{is_number} ) | ||
| 1822 | { | ||
| 1823 | my $val = $self->attr( $attr ); | ||
| 1824 | return if( !$self->_is_number( $val ) ); | ||
| 1825 | if( $val =~ /^(\d{1,10})(?:\.\d+)?$/ ) | ||
| 1826 | { | ||
| 1827 | my $dt = $self->_parse_timestamp( $val ); | ||
| 1828 | return( $dt ) if( ref( $dt ) ); | ||
| 1829 | } | ||
| 1830 | return( $self->new_number( $val ) ); | ||
| 1831 | } | ||
| 1832 | elsif( $def->{is_uri} ) | ||
| 1833 | { | ||
| 1834 | my $val = $self->attr( $attr ); | ||
| 1835 | # We constantly get a new URI object, because the value of the href attribute may have been altered by other means | ||
| 1836 | # try-catch | ||
| 1837 | local $@; | ||
| 1838 | my $rv = eval | ||
| 1839 | { | ||
| 1840 | return( $val ) if( $self->_is_a( $val => 'URI' ) ); | ||
| 1841 | return if( !defined( $val ) ); | ||
| 1842 | return( $val ) if( !CORE::length( "$val" ) ); | ||
| 1843 | return( URI->new( "$val" ) ); | ||
| 1844 | }; | ||
| 1845 | if( $@ ) | ||
| 1846 | { | ||
| 1847 | return( $self->error( "Unable to create a URI object from \"$val\" (", overload::StrVal( $val ), "): $@" ) ); | ||
| 1848 | } | ||
| 1849 | return( $rv ); | ||
| 1850 | } | ||
| 1851 | elsif( $def->{callback} && ref( $def->{callback} ) eq 'CODE' ) | ||
| 1852 | { | ||
| 1853 | return( $def->{callback}->( $self, $attr ) ); | ||
| 1854 | } | ||
| 1855 | return( $self->attr( $attr ) ); | ||
| 1856 | }, | ||
| 1857 | set => sub | ||
| 1858 | { | ||
| 1859 | my $self = shift( @_ ); | ||
| 1860 | my $arg = shift( @_ ); | ||
| 1861 | if( $def->{is_boolean} ) | ||
| 1862 | { | ||
| 1863 | # Any true value works, even in the web browser | ||
| 1864 | if( $arg ) | ||
| 1865 | { | ||
| 1866 | # it is ok to set an empty value | ||
| 1867 | $self->attr( $attr => '' ); | ||
| 1868 | } | ||
| 1869 | else | ||
| 1870 | { | ||
| 1871 | # Passing undef implies it will be removed. See HTML::Object::Element | ||
| 1872 | $self->attr( $attr => undef ); | ||
| 1873 | } | ||
| 1874 | } | ||
| 1875 | elsif( $def->{is_datetime} ) | ||
| 1876 | { | ||
| 1877 | $self->attr( $attr => "$arg" ); | ||
| 1878 | } | ||
| 1879 | # form target | ||
| 1880 | elsif( $def->{is_uri} ) | ||
| 1881 | { | ||
| 1882 | # try-catch | ||
| 1883 | local $@; | ||
| 1884 | eval | ||
| 1885 | { | ||
| 1886 | my $uri = URI->new( $arg ); | ||
| 1887 | $self->attr( $attr => $uri ); | ||
| 1888 | }; | ||
| 1889 | if( $@ ) | ||
| 1890 | { | ||
| 1891 | die( "Unable to create an URI with \"$arg\": $@" ); | ||
| 1892 | } | ||
| 1893 | } | ||
| 1894 | # Used for <option> | ||
| 1895 | elsif( $def->{callback} && ref( $def->{callback} ) eq 'CODE' ) | ||
| 1896 | { | ||
| 1897 | $def->{callback}->( $self, $attr => $arg ); | ||
| 1898 | } | ||
| 1899 | else | ||
| 1900 | { | ||
| 1901 | $self->attr( $attr => $arg ); | ||
| 1902 | } | ||
| 1903 | $self->reset(1); | ||
| 1904 | return( $arg ); | ||
| 1905 | } | ||
| 1906 | }, @_ ) ); | ||
| 1907 | } | ||
| 1908 | |||
| 1909 | # Used by HTML::Object::DOM::Element::Anchor and HTML::Object::DOM::Element::Area | ||
| 1910 | sub _set_get_uri_property : lvalue | ||
| 1911 | { | ||
| 1912 | my $self = shift( @_ ); | ||
| 1913 | my $prop = shift( @_ ); | ||
| 1914 | my $uri = $self->_set_get_anchor_uri; | ||
| 1915 | my $map = | ||
| 1916 | { | ||
| 1917 | hash => 'fragment', | ||
| 1918 | # URI's host_port is tolerant just like DOM's host is. Even if no port is provided, it will not complain | ||
| 1919 | host => 'host_port', | ||
| 1920 | hostname => 'host', | ||
| 1921 | pathname => 'path', | ||
| 1922 | password => 'userinfo', | ||
| 1923 | protocol => 'scheme', | ||
| 1924 | search => 'query', | ||
| 1925 | username => 'userinfo', | ||
| 1926 | }; | ||
| 1927 | |||
| 1928 | return( $self->_set_get_callback({ | ||
| 1929 | get => sub | ||
| 1930 | { | ||
| 1931 | my $self = shift( @_ ); | ||
| 1932 | # If there is an URI, we use it as a alue storage | ||
| 1933 | # It is convenient and let the user modify it directly if he wants. | ||
| 1934 | if( ref( $uri ) ) | ||
| 1935 | { | ||
| 1936 | my $meth = exists( $map->{ $prop } ) ? $map->{ $prop } : $prop; | ||
| 1937 | my $code = $uri->can( $meth ); | ||
| 1938 | # User trying to access URI method like host port, etc on a generic URI | ||
| 1939 | # which is ok for method like path, query, fragment | ||
| 1940 | # So we convert what would otherwise be an error into an undef returned, meaning no value | ||
| 1941 | if( !defined( $code ) ) | ||
| 1942 | { | ||
| 1943 | if( $uri->isa( 'URI::_generic' ) ) | ||
| 1944 | { | ||
| 1945 | return( $self->{ $prop } ); | ||
| 1946 | } | ||
| 1947 | else | ||
| 1948 | { | ||
| 1949 | return( $self->error( "URI object has no method \"$meth\"." ) ); | ||
| 1950 | } | ||
| 1951 | } | ||
| 1952 | # try-catch | ||
| 1953 | local $@; | ||
| 1954 | my $val = eval | ||
| 1955 | { | ||
| 1956 | $code->( $uri ); | ||
| 1957 | }; | ||
| 1958 | if( $@ ) | ||
| 1959 | { | ||
| 1960 | die( "Unable to get value for URI method \"${prop}\": $@" ); | ||
| 1961 | } | ||
| 1962 | # We assign the value from the URI method in case, the user would have modified the URI object directly | ||
| 1963 | # We need to stay synchronised. | ||
| 1964 | if( $prop eq 'username' || $prop eq 'password' ) | ||
| 1965 | { | ||
| 1966 | if( defined( $val ) ) | ||
| 1967 | { | ||
| 1968 | @$self{qw( username password )} = split( /:/, $val, 2 ); | ||
| 1969 | } | ||
| 1970 | else | ||
| 1971 | { | ||
| 1972 | $self->{username} = undef; | ||
| 1973 | $self->{password} = undef; | ||
| 1974 | } | ||
| 1975 | return( $self->{ $prop } ); | ||
| 1976 | } | ||
| 1977 | # We add back the colon, because URI stores the scheme without it, but our 'protocol' method returns the scheme with it. | ||
| 1978 | elsif( $prop eq 'protocol' ) | ||
| 1979 | { | ||
| 1980 | $val .= ':' if( defined( $val ) ); | ||
| 1981 | } | ||
| 1982 | elsif( $prop eq 'hash' ) | ||
| 1983 | { | ||
| 1984 | substr( $val, 0, 0, '#' ) if( defined( $val ) ); | ||
| 1985 | } | ||
| 1986 | elsif( $prop eq 'search' ) | ||
| 1987 | { | ||
| 1988 | substr( $val, 0, 0, '?' ) if( defined( $val ) ); | ||
| 1989 | } | ||
| 1990 | return( $self->{ $prop } = $val ); | ||
| 1991 | } | ||
| 1992 | return( $self->{ $prop } ); | ||
| 1993 | }, | ||
| 1994 | set => sub | ||
| 1995 | { | ||
| 1996 | my $self = shift( @_ ); | ||
| 1997 | my $arg = shift( @_ ); | ||
| 1998 | $self->{ $prop } = $arg; | ||
| 1999 | if( ref( $uri ) ) | ||
| 2000 | { | ||
| 2001 | my $uri_class = ref( $uri ); # URI::https or maybe URI::_generic ? | ||
| 2002 | if( $prop eq 'username' || $prop eq 'password' ) | ||
| 2003 | { | ||
| 2004 | no warnings 'uninitialized'; | ||
| 2005 | $arg = join( ':', @$self{ qw( username password ) } ); | ||
| 2006 | } | ||
| 2007 | elsif( $prop eq 'protocol' ) | ||
| 2008 | { | ||
| 2009 | # Remove the trailing colon, because URI scheme method takes it without it | ||
| 2010 | $arg =~ s/\:$//; | ||
| 2011 | } | ||
| 2012 | elsif( $prop eq 'hash' ) | ||
| 2013 | { | ||
| 2014 | $arg =~ s/^\#//; | ||
| 2015 | } | ||
| 2016 | elsif( $prop eq 'search' ) | ||
| 2017 | { | ||
| 2018 | $arg =~ s/^\?//; | ||
| 2019 | } | ||
| 2020 | my $meth = exists( $map->{ $prop } ) ? $map->{ $prop } : $prop; | ||
| 2021 | my $code = $uri->can( $meth ); | ||
| 2022 | # User trying to access URI method like host port, etc on a generic URI | ||
| 2023 | # which is ok for method like path, query, fragment | ||
| 2024 | # So we convert what would otherwise be an error into an undef returned, meaning no value | ||
| 2025 | if( !defined( $code ) ) | ||
| 2026 | { | ||
| 2027 | if( $uri->isa( 'URI::_generic' ) ) | ||
| 2028 | { | ||
| 2029 | return( $self->{ $prop } ); | ||
| 2030 | } | ||
| 2031 | else | ||
| 2032 | { | ||
| 2033 | return( $self->error( "URI object has no method \"$meth\"." ) ); | ||
| 2034 | } | ||
| 2035 | } | ||
| 2036 | # try-catch | ||
| 2037 | local $@; | ||
| 2038 | eval | ||
| 2039 | { | ||
| 2040 | $code->( $uri, $arg ); | ||
| 2041 | # If the URI object was generic and we switched it to a non-generic one by setting the schem | ||
| 2042 | # We also set other properties if we have them | ||
| 2043 | if( $prop eq 'protocol' && $uri_class eq 'URI::_generic' ) | ||
| 2044 | { | ||
| 2045 | if( $self->{hostname} ) | ||
| 2046 | { | ||
| 2047 | $uri->host_port( $self->{hostname} ); | ||
| 2048 | } | ||
| 2049 | elsif( $self->{host} || $self->{port} ) | ||
| 2050 | { | ||
| 2051 | $uri->host( $self->{host} ) if( $self->{host} ); | ||
| 2052 | $uri->port( $self->{port} ) if( $self->{port} ); | ||
| 2053 | } | ||
| 2054 | if( $self->{username} || $self->{password} ) | ||
| 2055 | { | ||
| 2056 | $uri->userinfo( join( ':', @$self{qw( username password )} ) ); | ||
| 2057 | } | ||
| 2058 | } | ||
| 2059 | $self->attr( href => $uri ); | ||
| 2060 | }; | ||
| 2061 | if( $@ ) | ||
| 2062 | { | ||
| 2063 | die( "Unable to set value \"${arg}\" for URI method \"${prop}\": $@" ); | ||
| 2064 | } | ||
| 2065 | } | ||
| 2066 | $self->reset(1); | ||
| 2067 | $self->{ $prop } = $arg; | ||
| 2068 | $self->attr( href => $uri ); | ||
| 2069 | return( $arg ); | ||
| 2070 | } | ||
| 2071 | }, @_ ) ); | ||
| 2072 | } | ||
| 2073 | |||
| 2074 | 1; | ||
| 2075 | # NOTE: POD | ||
| 2076 | __END__ |