| 1 | ##---------------------------------------------------------------------------- | ||
| 2 | ## HTML Object - ~/lib/HTML/Object/DOM/Document.pm | ||
| 3 | ## Version v0.3.1 | ||
| 4 | ## Copyright(c) 2025 DEGUEST Pte. Ltd. | ||
| 5 | ## Author: Jacques Deguest <jack@deguest.jp> | ||
| 6 | ## Created 2021/12/13 | ||
| 7 | ## Modified 2026/07/04 | ||
| 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::Document; | ||
| 15 | BEGIN | ||
| 16 | { | ||
| 17 | use strict; | ||
| 18 | use warnings; | ||
| 19 | warnings::register_categories( 'HTML::Object' ); | ||
| 20 | use parent qw( HTML::Object::Document HTML::Object::DOM::Element ); | ||
| 21 | use vars qw( $VERSION ); | ||
| 22 | use HTML::Object::ErrorEvent; | ||
| 23 | use Scalar::Util (); | ||
| 24 | use Wanted; | ||
| 25 | our $VERSION = 'v0.3.1'; | ||
| 26 | }; | ||
| 27 | |||
| 28 | use strict; | ||
| 29 | use warnings; | ||
| 30 | |||
| 31 | sub init | ||
| 32 | { | ||
| 33 | my $self = shift( @_ ); | ||
| 34 | $self->{_init_strict_use_sub} = 1; | ||
| 35 | $self->HTML::Object::Document::init( @_ ) || return( $self->pass_error ); | ||
| 36 | $self->{onload} = undef; | ||
| 37 | $self->{onreadystatechange} = undef; | ||
| 38 | $self->{visibilitystate} = 'visible'; | ||
| 39 | $self->{_xpath_eval} = undef; | ||
| 40 | # Nodes are special in the document. In other elements, nodes are the same as children | ||
| 41 | # But here no. There can be only 1 element: html, but one can add a doctype or comments | ||
| 42 | # comments added will be part of the children, but not the doctype. So | ||
| 43 | # $doc->children is always equal to 1, but | ||
| 44 | # $doc->childNodes->length would be equal to 2 (including the doctype) | ||
| 45 | # So our real array is nodes, i.e. what would otherwise be children for other elements | ||
| 46 | # $self->{nodes} is automatically created by the read-only method 'nodes' | ||
| 47 | # State of the document for document->open(), document->close() | ||
| 48 | $self->{_closed} = 1; | ||
| 49 | return( $self ); | ||
| 50 | } | ||
| 51 | |||
| 52 | sub activeElement : lvalue { return( shift->_set_get_object_lvalue( 'activeelement', 'HTML::Object::DOM::Element', @_ ) ); } | ||
| 53 | |||
| 54 | sub adoptNode | ||
| 55 | { | ||
| 56 | my $self = shift( @_ ); | ||
| 57 | my $elem = shift( @_ ) || return( $self->error( "No element object was provided to adopt into our document." ) ); | ||
| 58 | return( $self->error( "Element provided ($elem) is not a HTML::Object::Element object." ) ) if( !$self->_is_a( $elem, 'HTML::Object::Element' ) || $self->_is_a( $elem, 'HTML::Object::Collection' ) ); | ||
| 59 | if( $elem->parent ) | ||
| 60 | { | ||
| 61 | $elem->detach; | ||
| 62 | } | ||
| 63 | return( $elem ); | ||
| 64 | } | ||
| 65 | |||
| 66 | # Note: method append is different from HTML::Object::DOM::Element because we need to check what goes in | ||
| 67 | sub append | ||
| 68 | { | ||
| 69 | my $self = shift( @_ ); | ||
| 70 | return( $self ) if( !scalar( @_ ) ); | ||
| 71 | # If a HTML::Object::DOM::DocumentFragment object is provided, its children are | ||
| 72 | # copied to the list and its own children array is emptied. | ||
| 73 | my $list = $self->_check_list_of_nodes_or_text( @_ ) || return( $self->pass_error ); | ||
| 74 | my $nodes = $self->nodes; | ||
| 75 | $self->reset(1) if( !$list->is_empty ); | ||
| 76 | $list->foreach(sub | ||
| 77 | { | ||
| 78 | $_->parent( $self ); | ||
| 79 | }); | ||
| 80 | $nodes->push( $list->list ); | ||
| 81 | return( $self ); | ||
| 82 | } | ||
| 83 | |||
| 84 | # Node: method appendChild is inherited | ||
| 85 | |||
| 86 | sub as_string | ||
| 87 | { | ||
| 88 | my $self = shift( @_ ); | ||
| 89 | my $a = $self->new_array; | ||
| 90 | $self->nodes->foreach(sub | ||
| 91 | { | ||
| 92 | my $e = shift( @_ ); | ||
| 93 | my $v = $e->as_string; | ||
| 94 | $a->push( defined( $v ) ? $v->scalar : $v ); | ||
| 95 | }); | ||
| 96 | return( $a->join( '' ) ); | ||
| 97 | } | ||
| 98 | |||
| 99 | sub body : lvalue { return( shift->_set_get_callback({ | ||
| 100 | get => sub | ||
| 101 | { | ||
| 102 | my $self = shift( @_ ); | ||
| 103 | my $html = $self->documentElement || | ||
| 104 | die( "Unable to find a top HTML tag" ); | ||
| 105 | my $children = $html->children; | ||
| 106 | my $body; | ||
| 107 | foreach( @$children ) | ||
| 108 | { | ||
| 109 | if( $_->tag eq 'body' ) | ||
| 110 | { | ||
| 111 | $body = $_; | ||
| 112 | # End loop | ||
| 113 | last; | ||
| 114 | } | ||
| 115 | } | ||
| 116 | # $body may be undef, and that's ok. A Module::Generic::Null object will be returned instead | ||
| 117 | return( $body ); | ||
| 118 | }, | ||
| 119 | set => sub | ||
| 120 | { | ||
| 121 | my $self = shift( @_ ); | ||
| 122 | my $e = shift( @_ ); | ||
| 123 | my $html = $self->documentElement || | ||
| 124 | die( "Unable to find a top HTML tag" ); | ||
| 125 | my $children = $html->children; | ||
| 126 | if( !$self->_is_a( $e, 'HTML::Object::Element' ) ) | ||
| 127 | { | ||
| 128 | die( "Element provided to set as new body element is not a HTML::Object::Element object." ); | ||
| 129 | } | ||
| 130 | |||
| 131 | my $body; | ||
| 132 | for( my $pos = 0; $pos < scalar( @$children ); $pos++ ) | ||
| 133 | { | ||
| 134 | my $child = $children->[$pos]; | ||
| 135 | if( $child->tag eq 'body' ) | ||
| 136 | { | ||
| 137 | $body = $child; | ||
| 138 | $children->offset( $pos, 1, $e ); | ||
| 139 | # End loop | ||
| 140 | last; | ||
| 141 | } | ||
| 142 | } | ||
| 143 | |||
| 144 | # No body was found, amazingly enough; add it now | ||
| 145 | if( !defined( $body ) ) | ||
| 146 | { | ||
| 147 | $children->push( $e ); | ||
| 148 | } | ||
| 149 | return( $e ); | ||
| 150 | } | ||
| 151 | }, @_ ) ); } | ||
| 152 | |||
| 153 | sub captureEvents { return( shift->defaultView->captureEvents( @_ ) ); } | ||
| 154 | |||
| 155 | sub caretPositionFromPoint { return; } | ||
| 156 | |||
| 157 | sub caretRangeFromPoint { return; } | ||
| 158 | |||
| 159 | # read-only | ||
| 160 | # e.g.: | ||
| 161 | # <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> | ||
| 162 | # <meta charset="utf-8" /> | ||
| 163 | sub characterSet : lvalue | ||
| 164 | { | ||
| 165 | my $self = shift( @_ ); | ||
| 166 | # Get an Module::Generic::Array object | ||
| 167 | my $results = $self->findNode( 'meta' ) || return( $self->pass_error ); | ||
| 168 | require Module::Generic::HeaderValue; | ||
| 169 | foreach my $e ( $results->list ) | ||
| 170 | { | ||
| 171 | if( $e->attributes->has( 'http-equiv' ) && | ||
| 172 | $e->attributes->get( 'http-equiv' ) eq 'Content-Type' && | ||
| 173 | $e->attributes->has( 'content' ) ) | ||
| 174 | { | ||
| 175 | my $type = $e->attributes->get( 'content' ); | ||
| 176 | next if( !defined( $type ) || !CORE::length( $type ) ); | ||
| 177 | my $hv = Module::Generic::HeaderValue->new_from_header( $type ); | ||
| 178 | my $charset = $hv->param( 'charset' ); | ||
| 179 | return( $charset // '' ); | ||
| 180 | } | ||
| 181 | elsif( $e->attributes->has( 'charset' ) ) | ||
| 182 | { | ||
| 183 | return( $e->attributes->get( 'charset' ) // '' ); | ||
| 184 | } | ||
| 185 | } | ||
| 186 | return( '' ); | ||
| 187 | } | ||
| 188 | |||
| 189 | # Note: property childElementCount read-only inherited from HTML::Object::DOM::Element | ||
| 190 | |||
| 191 | # Note: property children read-only | ||
| 192 | sub children | ||
| 193 | { | ||
| 194 | my $self = shift( @_ ); | ||
| 195 | if( @_ ) | ||
| 196 | { | ||
| 197 | return( $self->_set_get_object_array_object( 'children', 'HTML::Object::Element', @_ ) ); | ||
| 198 | } | ||
| 199 | $self->_init_nodes; | ||
| 200 | return( $self->_set_get_object_array_object( 'children', 'HTML::Object::Element' ) ); | ||
| 201 | } | ||
| 202 | |||
| 203 | sub childNodes : lvalue { return( shift->_set_get_array_as_object( 'nodes', @_ ) ); } | ||
| 204 | |||
| 205 | # Note: property children read-only inherited | ||
| 206 | |||
| 207 | sub clone | ||
| 208 | { | ||
| 209 | my $self = shift( @_ ); | ||
| 210 | my $saved_nodes = $self->{nodes}; | ||
| 211 | my $saved_defaultview = $self->{defaultview}; | ||
| 212 | # NOTE: keep the ORIGINAL children list (before SUPER::clone() touches | ||
| 213 | # anything) so we can map each entry to its already-cloned counterpart | ||
| 214 | # below, by object identity rather than by tag name. Some fragments | ||
| 215 | # (like this invoice template, which starts directly at <body> with no | ||
| 216 | # <html> wrapper) never trigger the 'html' narrowing in _init_nodes(), | ||
| 217 | # so 'children' ends up being the SAME full set as 'nodes' — not a | ||
| 218 | # single-element subset as the previous version assumed. | ||
| 219 | my $orig_children = $self->{children}; | ||
| 220 | local $self->{nodes} = undef; | ||
| 221 | local $self->{defaultview} = undef; | ||
| 222 | my $new = $self->SUPER::clone(); | ||
| 223 | |||
| 224 | # NOTE: temporary diagnostic — inventaire de tous les champs de $self, | ||
| 225 | # triés par taille décroissante, pour identifier le(s) champ(s) responsable(s) | ||
| 226 | # du surcoût restant, plutôt que de continuer à deviner des noms un par un. | ||
| 227 | foreach my $k ( sort { total_size( $self->{ $b } ) <=> total_size( $self->{ $a } ) } CORE::keys( %$self ) ) | ||
| 228 | { | ||
| 229 | next unless( ref( $self->{ $k } ) ); | ||
| 230 | } | ||
| 231 | |||
| 232 | # NOTE: defaultView/window represents the execution context, not document | ||
| 233 | # content — the clone reuses the SAME Window instance as the original | ||
| 234 | # rather than cloning it. No identity-mapping needed here (unlike | ||
| 235 | # 'nodes'/'children' above), since there's exactly one window, not a list. | ||
| 236 | $new->{defaultview} = $saved_defaultview if( defined( $saved_defaultview ) ); | ||
| 237 | |||
| 238 | if( defined( $saved_nodes ) && $self->_is_a( $saved_nodes => 'Module::Generic::Array' ) ) | ||
| 239 | { | ||
| 240 | # NOTE: Build a map from original child object address to its | ||
| 241 | # already-cloned counterpart. clone_list(), inside SUPER::clone(), | ||
| 242 | # clones each entry of 'children' in order, so zipping the original | ||
| 243 | # and new lists by index gives us that correspondence cheaply. | ||
| 244 | my %clone_of; | ||
| 245 | if( $self->_is_a( $orig_children => 'Module::Generic::Array' ) && $new->children ) | ||
| 246 | { | ||
| 247 | # $orig_children->list returns an array, not an array reference. | ||
| 248 | my $orig_list = $orig_children; | ||
| 249 | my $new_list = $new->children; | ||
| 250 | for( my $i = 0; $i < scalar( @$orig_list ); $i++ ) | ||
| 251 | { | ||
| 252 | next unless( ref( $orig_list->[ $i ] ) && ref( $new_list->[ $i ] ) ); | ||
| 253 | $clone_of{ Scalar::Util::refaddr( $orig_list->[ $i ] ) } = $new_list->[ $i ]; | ||
| 254 | } | ||
| 255 | } | ||
| 256 | |||
| 257 | my $cloned_nodes = $saved_nodes->map(sub | ||
| 258 | { | ||
| 259 | my $n = shift( @_ ); | ||
| 260 | my $addr = Scalar::Util::refaddr( $n ); | ||
| 261 | # NOTE: already cloned via 'children' — reuse it, do not clone again. | ||
| 262 | return( $clone_of{ $addr } ) if( exists( $clone_of{ $addr } ) ); | ||
| 263 | # Not part of 'children' (e.g. a leading DOCTYPE excluded by | ||
| 264 | # design) — clone_list() never touched it, clone independently. | ||
| 265 | my $c = $n->clone; | ||
| 266 | $c->parent( $new ); | ||
| 267 | return( $c ); | ||
| 268 | }); | ||
| 269 | $new->{nodes} = $cloned_nodes; | ||
| 270 | $self->{nodes} = $saved_nodes; | ||
| 271 | $self->{defaultview} = $saved_defaultview; | ||
| 272 | } | ||
| 273 | |||
| 274 | return( $new ); | ||
| 275 | } | ||
| 276 | |||
| 277 | # method cloneNode is inherited from HTML::Object::DOM::Node | ||
| 278 | |||
| 279 | sub close { return( shift->{_closed} = 1 ); } | ||
| 280 | |||
| 281 | sub compatMode : lvalue { return; } | ||
| 282 | |||
| 283 | # read-only | ||
| 284 | # e.g.: | ||
| 285 | # <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> | ||
| 286 | sub contentType | ||
| 287 | { | ||
| 288 | my $self = shift( @_ ); | ||
| 289 | my $results = $self->findNode( 'meta' ) || return( $self->pass_error ); | ||
| 290 | require Module::Generic::HeaderValue; | ||
| 291 | my $default = 'text/html'; | ||
| 292 | foreach my $e ( $results->list ) | ||
| 293 | { | ||
| 294 | if( $e->attributes->has( 'http-equiv' ) && | ||
| 295 | $e->attributes->get( 'http-equiv' ) eq 'Content-Type' && | ||
| 296 | $e->attributes->has( 'content' ) ) | ||
| 297 | { | ||
| 298 | my $type = $e->attributes->get( 'content' ); | ||
| 299 | next if( !defined( $type ) || !CORE::length( $type ) ); | ||
| 300 | my $hv = Module::Generic::HeaderValue->new_from_header( $type ); | ||
| 301 | return( $hv->value->first // $default ); | ||
| 302 | } | ||
| 303 | } | ||
| 304 | return( $default ); | ||
| 305 | } | ||
| 306 | |||
| 307 | sub cookie : lvalue { return( shift->_set_get_lvalue( '_cookie', @_ ) ); } | ||
| 308 | |||
| 309 | sub createAttribute | ||
| 310 | { | ||
| 311 | my $self = shift( @_ ); | ||
| 312 | my $name = shift( @_ ); | ||
| 313 | $name = lc( $name ) if( defined( $name ) ); | ||
| 314 | return( $self->error({ | ||
| 315 | message => "The attribute provided \"$name\" contains illegal characters.", | ||
| 316 | class => 'HTML::Object::SyntaxError', | ||
| 317 | }) ) if( !$self->is_valid_attribute( $name ) ); | ||
| 318 | require HTML::Object::DOM::Attribute; | ||
| 319 | my $att = HTML::Object::DOM::Attribute->new( $name, @_ ) || | ||
| 320 | return( $self->pass_error( HTML::Object::DOM::Attribute->error ) ); | ||
| 321 | return( $att ); | ||
| 322 | } | ||
| 323 | |||
| 324 | sub createAttributeNS { return; } | ||
| 325 | |||
| 326 | sub createCDATASection { return; } | ||
| 327 | |||
| 328 | # TODO Create pod: https://developer.mozilla.org/en-US/docs/Web/API/Document/createComment | ||
| 329 | sub createComment | ||
| 330 | { | ||
| 331 | my $self = shift( @_ ); | ||
| 332 | my $data; | ||
| 333 | $data = join( '', @_ ) if( @_ ); | ||
| 334 | return( $self->new_comment( value => $data ) ); | ||
| 335 | } | ||
| 336 | |||
| 337 | sub createDocumentFragment | ||
| 338 | { | ||
| 339 | my $self = shift( @_ ); | ||
| 340 | require HTML::Object::DOM::DocumentFragment; | ||
| 341 | my $frag = HTML::Object::DOM::DocumentFragment->new( @_ ) || | ||
| 342 | return( $self->pass_error( HTML::Object::DOM::DocumentFragment->error ) ); | ||
| 343 | return( $frag ); | ||
| 344 | } | ||
| 345 | |||
| 346 | sub createElement | ||
| 347 | { | ||
| 348 | my $self = shift( @_ ); | ||
| 349 | my $tag = shift( @_ ) || return( $self->error( "No tag was provided to create an element." ) ); | ||
| 350 | my $opts = $self->_get_args_as_hash( @_ ); | ||
| 351 | my $p = HTML::Object::DOM->new; | ||
| 352 | my $def = $p->get_definition( $tag ); | ||
| 353 | # Even if the tag does not exist, the default behaviour is to accept whatever was | ||
| 354 | # provided and assume it is a non-empty tag | ||
| 355 | if( !defined( $def ) ) | ||
| 356 | { | ||
| 357 | $def = | ||
| 358 | { | ||
| 359 | is_empty => 0, | ||
| 360 | is_inline => 0, | ||
| 361 | }; | ||
| 362 | } | ||
| 363 | |||
| 364 | $opts->{debug} = $self->debug; | ||
| 365 | @$opts{qw( is_empty is_inline )} = @$def{qw( is_empty is_inline )}; | ||
| 366 | $opts->{tag} = $tag; | ||
| 367 | |||
| 368 | my $e; | ||
| 369 | $def->{class} //= ''; | ||
| 370 | if( $def->{class} ) | ||
| 371 | { | ||
| 372 | $e = $p->new_special( $def->{class} => $opts ); | ||
| 373 | } | ||
| 374 | else | ||
| 375 | { | ||
| 376 | $e = $p->new_element( $opts ); | ||
| 377 | } | ||
| 378 | $e->close; | ||
| 379 | return( $e ); | ||
| 380 | } | ||
| 381 | |||
| 382 | sub createElementNS { return; } | ||
| 383 | |||
| 384 | # TODO: createEntityReference | ||
| 385 | sub createEntityReference { return; } | ||
| 386 | |||
| 387 | # TODO createEvent https://developer.mozilla.org/en-US/docs/Web/API/Document/createEvent | ||
| 388 | sub createEvent | ||
| 389 | { | ||
| 390 | my $self = shift( @_ ); | ||
| 391 | my $type = shift( @_ ) || return( $self->error({ | ||
| 392 | message => 'No event type was provided', | ||
| 393 | class => 'HTML::Objct::SyntaxError', | ||
| 394 | }) ); | ||
| 395 | $self->_load_class( 'HTML::Object::Event' ) || return( $self->pass_error ); | ||
| 396 | my $evt = HTML::Object::Event->new( $type, @_ ) || | ||
| 397 | return( $self->pass_error( HTML::Object::Event->error ) ); | ||
| 398 | return( $evt ); | ||
| 399 | } | ||
| 400 | |||
| 401 | # TODO: createExpression | ||
| 402 | sub createExpression | ||
| 403 | { | ||
| 404 | my $self = shift( @_ ); | ||
| 405 | my $eval; | ||
| 406 | unless( $eval = $self->{_xpath_eval} ) | ||
| 407 | { | ||
| 408 | $self->_load_class( 'HTML::Object::DOM::XPathEvaluator' ) || return( $self->pass_error ); | ||
| 409 | $self->{_xpath_eval} = $eval = HTML::Object::DOM::XPathEvaluator->new || | ||
| 410 | return( $self->pass_error( HTML::Object::DOM::XPathEvaluator->error ) ); | ||
| 411 | } | ||
| 412 | my $expr = $eval->createExpression( @_ ); | ||
| 413 | return( $self->pass_error( $eval->error ) ) if( !defined( $expr ) ); | ||
| 414 | return( $expr ); | ||
| 415 | } | ||
| 416 | |||
| 417 | # TODO: createNSResolver | ||
| 418 | sub createNSResolver { return; } | ||
| 419 | |||
| 420 | # TODO: createNodeIterator | ||
| 421 | sub createNodeIterator | ||
| 422 | { | ||
| 423 | my $self = shift( @_ ); | ||
| 424 | $self->_load_class( 'HTML::Object::DOM::NodeIterator' ) || return( $self->pass_error ); | ||
| 425 | my $iterator = HTML::Object::DOM::NodeIterator->new( @_ ); | ||
| 426 | return( $self->pass_error( HTML::Object::DOM::NodeIterator->error ) ) if( !defined( $iterator ) ); | ||
| 427 | return( $iterator ); | ||
| 428 | } | ||
| 429 | |||
| 430 | sub createProcessingInstruction { return; } | ||
| 431 | |||
| 432 | # TODO: createRange | ||
| 433 | sub createRange { return; } | ||
| 434 | |||
| 435 | sub createTextNode | ||
| 436 | { | ||
| 437 | my $self = shift( @_ ); | ||
| 438 | my $txt = @_ == 1 ? shift( @_ ) : join( '', @_ ); | ||
| 439 | $self->_load_class( 'HTML::Object::DOM::Text' ) || return( $self->pass_error ); | ||
| 440 | my $node = HTML::Object::DOM::Text->new( value => $txt ) || | ||
| 441 | return( $self->pass_error( HTML::Object::DOM::Text->error ) ); | ||
| 442 | return( $node ); | ||
| 443 | } | ||
| 444 | |||
| 445 | sub createTouch { return; } | ||
| 446 | |||
| 447 | sub createTouchList { return; } | ||
| 448 | |||
| 449 | sub createTreeWalker | ||
| 450 | { | ||
| 451 | my $self = shift( @_ ); | ||
| 452 | $self->_load_class( 'HTML::Object::DOM::TreeWalker' ) || return( $self->pass_error ); | ||
| 453 | my $crawler = HTML::Object::DOM::TreeWalker->new( @_ ); | ||
| 454 | return( $self->pass_error( HTML::Object::DOM::TreeWalker->error ) ) if( !defined( $crawler ) ); | ||
| 455 | return( $crawler ); | ||
| 456 | } | ||
| 457 | |||
| 458 | sub currentScript { return( shift->_set_get_object_lvalue( 'currentscript', 'HTML::Object::DOM::Element', @_ ) ); } | ||
| 459 | |||
| 460 | sub defaultView : lvalue { return( shift->_set_get_object_lvalue( 'defaultview', 'HTML::Object::DOM::Window', @_ ) ); } | ||
| 461 | |||
| 462 | sub designMode : lvalue { return; } | ||
| 463 | |||
| 464 | sub dir : lvalue { return( shift->_set_get_lvalue( '_dir' ) ); } | ||
| 465 | |||
| 466 | # Note: property doctype read-only | ||
| 467 | sub doctype : lvalue | ||
| 468 | { | ||
| 469 | my $self = shift( @_ ); | ||
| 470 | return( $self->declaration ); | ||
| 471 | } | ||
| 472 | |||
| 473 | # Note: property documentElement read-only | ||
| 474 | sub documentElement : lvalue | ||
| 475 | { | ||
| 476 | my $self = shift( @_ ); | ||
| 477 | if( @_ ) | ||
| 478 | { | ||
| 479 | warn( "This property \"documentElement\" is read-only.\n" ) if( $self->_is_warnings_enabled( 'HTML::Object' ) ); | ||
| 480 | } | ||
| 481 | my $html; | ||
| 482 | # It should be the first one, but let's not assume anything | ||
| 483 | my $children = $self->children; | ||
| 484 | foreach( @$children ) | ||
| 485 | { | ||
| 486 | if( $_->tag eq 'html' ) | ||
| 487 | { | ||
| 488 | $html = $_; | ||
| 489 | last; | ||
| 490 | } | ||
| 491 | } | ||
| 492 | if( !$html && want( 'OBJECT' ) ) | ||
| 493 | { | ||
| 494 | require Module::Generic::Null; | ||
| 495 | return( Module::Generic::Null->new( wants => 'OBJECT' ) ); | ||
| 496 | } | ||
| 497 | return( $html ); | ||
| 498 | } | ||
| 499 | |||
| 500 | # Note: property documentURI read-only | ||
| 501 | sub documentURI : lvalue { return( shift->_set_get_uri( 'uri', @_ ) ); } | ||
| 502 | |||
| 503 | sub elementFromPoint { return; } | ||
| 504 | |||
| 505 | sub elementsFromPoint { return; } | ||
| 506 | |||
| 507 | # Note: property read-only embeds | ||
| 508 | sub embeds | ||
| 509 | { | ||
| 510 | my $self = shift( @_ ); | ||
| 511 | my $results = $self->findNode( 'embed' ) || return( $self->pass_error ); | ||
| 512 | return( $self->new_collection_elements( $results ) ); | ||
| 513 | } | ||
| 514 | |||
| 515 | sub enableStyleSheetsForSet { return; } | ||
| 516 | |||
| 517 | sub evaluate | ||
| 518 | { | ||
| 519 | my $self = shift( @_ ); | ||
| 520 | my $eval; | ||
| 521 | unless( $eval = $self->{_xpath_eval} ) | ||
| 522 | { | ||
| 523 | $self->_load_class( 'HTML::Object::DOM::XPathEvaluator' ) || return( $self->pass_error ); | ||
| 524 | $self->{_xpath_eval} = $eval = HTML::Object::DOM::XPathEvaluator->new || | ||
| 525 | return( $self->pass_error( HTML::Object::DOM::XPathEvaluator->error ) ); | ||
| 526 | } | ||
| 527 | return( $eval->evaluate( @_ ) ); | ||
| 528 | } | ||
| 529 | |||
| 530 | sub execCommand { return; } | ||
| 531 | |||
| 532 | sub exitPictureInPicture { return; } | ||
| 533 | |||
| 534 | sub exitPointerLock { return; } | ||
| 535 | |||
| 536 | sub featurePolicy : lvalue { return; } | ||
| 537 | |||
| 538 | # Note: firstChild returns its values from the nodes array, not the children one; is inherited | ||
| 539 | |||
| 540 | # Note: method firstElementChild is inherited from HTML::Object::DOM::Element | ||
| 541 | |||
| 542 | # Note: property read-only fonts | ||
| 543 | sub fonts { return; } | ||
| 544 | |||
| 545 | # Note: property read-only forms | ||
| 546 | sub forms | ||
| 547 | { | ||
| 548 | my $self = shift( @_ ); | ||
| 549 | my $results = $self->findNode( 'form' ) || return( $self->pass_error ); | ||
| 550 | return( $self->new_collection_elements( $results ) ); | ||
| 551 | } | ||
| 552 | |||
| 553 | sub fullscreenElement : lvalue { return; } | ||
| 554 | |||
| 555 | sub getAnimations { return; } | ||
| 556 | |||
| 557 | sub getBoxQuads { return; } | ||
| 558 | |||
| 559 | sub getElementById | ||
| 560 | { | ||
| 561 | my $self = shift( @_ ); | ||
| 562 | my $id = shift( @_ ); | ||
| 563 | return if( !defined( $id ) || !CORE::length( $id ) ); | ||
| 564 | my $seen = {}; | ||
| 565 | my $crawl; | ||
| 566 | $crawl = sub | ||
| 567 | { | ||
| 568 | my $elem = shift( @_ ); | ||
| 569 | my $addr = Scalar::Util::refaddr( $elem ); | ||
| 570 | return if( CORE::exists( $seen->{ $addr } ) ); | ||
| 571 | # Not a true element, such as text, comment | ||
| 572 | return if( $elem->tag->substr( 0, 1 ) eq '_' && !$self->_is_a( $elem => 'HTML::Object::Document' ) ); | ||
| 573 | $seen->{ $addr }++; | ||
| 574 | if( $elem->attributes->has( 'id' ) && | ||
| 575 | $elem->attributes->get( 'id' ) eq $id ) | ||
| 576 | { | ||
| 577 | return( $elem ); | ||
| 578 | } | ||
| 579 | my $e; | ||
| 580 | $elem->children->foreach(sub | ||
| 581 | { | ||
| 582 | my $this = shift( @_ ); | ||
| 583 | if( my $found = $crawl->( $this ) ) | ||
| 584 | { | ||
| 585 | $e = $found; | ||
| 586 | # last | ||
| 587 | return(0); | ||
| 588 | } | ||
| 589 | return(1); | ||
| 590 | }); | ||
| 591 | return( $e ); | ||
| 592 | }; | ||
| 593 | return( $crawl->( $self ) ); | ||
| 594 | } | ||
| 595 | |||
| 596 | # Note: method getElementsByClassName is inherited from HTML::Object::DOM::Element | ||
| 597 | |||
| 598 | sub getElementsByName | ||
| 599 | { | ||
| 600 | my $self = shift( @_ ); | ||
| 601 | my $name = shift( @_ ); | ||
| 602 | return( $self->error( "No value was provided for getElementsByName()" ) ) if( !defined( $name ) || !CORE::length( "$name" ) ); | ||
| 603 | my $results = $self->look_down( name => $name ) || return( $self->pass_error ); | ||
| 604 | my $list = $self->new_nodelist( $results ) || return( $self->pass_error ); | ||
| 605 | return( $list ); | ||
| 606 | } | ||
| 607 | |||
| 608 | # Note: method getElementsByTagName is inherited from HTML::Object::DOM::Element | ||
| 609 | |||
| 610 | sub getElementsByTagNameNS { return; } | ||
| 611 | |||
| 612 | sub getRootNode { return( shift->root ); } | ||
| 613 | |||
| 614 | sub getSelection { return; } | ||
| 615 | |||
| 616 | # Note: method hasChildNodes is inherited | ||
| 617 | |||
| 618 | sub hasFocus { return(1); } | ||
| 619 | |||
| 620 | sub hasStorageAccess { return; } | ||
| 621 | |||
| 622 | # Note: property head read-only | ||
| 623 | sub head : lvalue | ||
| 624 | { | ||
| 625 | my $self = shift( @_ ); | ||
| 626 | my $results = $self->findNode( 'head' ) || return( $self->pass_error ); | ||
| 627 | my $head = $results->first; | ||
| 628 | if( !$head && want( 'OBJECT' ) ) | ||
| 629 | { | ||
| 630 | require Module::Generic::Null; | ||
| 631 | rreturn( Module::Generic::Null->new( wants => 'OBJECT' ) ); | ||
| 632 | } | ||
| 633 | return( $head ); | ||
| 634 | } | ||
| 635 | |||
| 636 | # Note: property hidden read-only | ||
| 637 | sub hidden : lvalue { return; } | ||
| 638 | |||
| 639 | # Note: property images read-only | ||
| 640 | sub images : lvalue | ||
| 641 | { | ||
| 642 | my $self = shift( @_ ); | ||
| 643 | my $results = $self->findNode( 'img' ) || return( $self->pass_error ); | ||
| 644 | return( $self->new_collection_elements( $results ) ); | ||
| 645 | } | ||
| 646 | |||
| 647 | # Note: property implementation read-only | ||
| 648 | sub implementation | ||
| 649 | { | ||
| 650 | my $self = shift( @_ ); | ||
| 651 | unless( $self->{_implementation} ) | ||
| 652 | { | ||
| 653 | $self->_load_class( 'HTML::Object::DOM::Implementation' ) || return( $self->pass_error ); | ||
| 654 | $self->{_implementation} = HTML::Object::DOM::Implementation->new( debug => $self->debug ) || | ||
| 655 | return( $self->pass_error( HTML::Object::DOM::Implementation->error ) ); | ||
| 656 | } | ||
| 657 | return( $self->{_implementation} ); | ||
| 658 | } | ||
| 659 | |||
| 660 | sub importNode { return( shift->adoptNode( @_ ) ); } | ||
| 661 | |||
| 662 | # Note: method insertAfter is inherited | ||
| 663 | |||
| 664 | # Note: method insertBefore is inherited | ||
| 665 | |||
| 666 | # Note: property lastChild is inherited | ||
| 667 | |||
| 668 | # Note: method lastElementChild is inherited from HTML::Object::DOM::Element | ||
| 669 | |||
| 670 | sub lastModified : lvalue { return( shift->_set_get_datetime( '_last_modified' ) ); } | ||
| 671 | |||
| 672 | # Note: property links read-only | ||
| 673 | sub links : lvalue | ||
| 674 | { | ||
| 675 | my $self = shift( @_ ); | ||
| 676 | my $results = $self->findNode( 'a' ) || return( $self->pass_error ); | ||
| 677 | return( $results ); | ||
| 678 | } | ||
| 679 | |||
| 680 | sub location : lvalue { return( shift->_set_get_uri( 'uri', @_ ) ); } | ||
| 681 | |||
| 682 | sub mozSetImageElement { return; } | ||
| 683 | |||
| 684 | # Note: property mozSyntheticDocument | ||
| 685 | sub mozSyntheticDocument : lvalue { return( shift->_set_get_boolean( 'mozsyntheticdocument', @_ ) ); } | ||
| 686 | |||
| 687 | sub nextSibling { return( shift->new_null ); } | ||
| 688 | |||
| 689 | # See also children for its alter ego | ||
| 690 | sub nodes { return( shift->_init_nodes ); } | ||
| 691 | |||
| 692 | # Note: Property | ||
| 693 | # Should be undef for document | ||
| 694 | # <https://developer.mozilla.org/en-US/docs/Web/API/Node/nodeValue> | ||
| 695 | sub nodeValue { return; } | ||
| 696 | |||
| 697 | # Note: this is merely an alias to HTML::Object::DOM::Node->normalize | ||
| 698 | sub normalizeDocument { return( shift->normalize( @_ ) ); } | ||
| 699 | |||
| 700 | # Note: Property | ||
| 701 | sub onabort : lvalue { return( shift->_set_get_on_signal( [qw( ABRT TERM INT )], @_ ) ); } | ||
| 702 | |||
| 703 | sub onafterscriptexecute : lvalue { return( shift->on( 'afterscriptexecute', @_ ) ); } | ||
| 704 | |||
| 705 | sub onbeforescriptexecute : lvalue { return( shift->on( 'beforescriptexecute', @_ ) ); } | ||
| 706 | |||
| 707 | sub oncopy : lvalue { return( shift->on( 'copy', @_ ) ); } | ||
| 708 | |||
| 709 | sub oncut : lvalue { return( shift->on( 'cut', @_ ) ); } | ||
| 710 | |||
| 711 | sub onerror : lvalue { return( shift->_set_get_on_signal( [qw( __WARN__ )], @_ ) ); } | ||
| 712 | |||
| 713 | sub onfullscreenchange : lvalue { return( shift->on( 'fullscreenchange', @_ ) ); } | ||
| 714 | |||
| 715 | sub onfullscreenerror : lvalue { return( shift->on( 'fullscreenerror', @_ ) ); } | ||
| 716 | |||
| 717 | sub onload : lvalue { return( shift->_set_get_code( 'onload', @_ ) ); } | ||
| 718 | |||
| 719 | sub onpaste : lvalue { return( shift->on( 'paste', @_ ) ); } | ||
| 720 | |||
| 721 | # sub onreadystatechange : lvalue { return( shift->_set_get_code( 'onreadystatechange', @_ ) ); } | ||
| 722 | sub onreadystatechange : lvalue { return( shift->on( 'readystatechange', @_ ) ); } | ||
| 723 | |||
| 724 | sub onscroll : lvalue { return( shift->on( 'scroll', @_ ) ); } | ||
| 725 | |||
| 726 | sub onselectionchange : lvalue { return( shift->on( 'selectionchange', @_ ) ); } | ||
| 727 | |||
| 728 | sub onvisibilitychange : lvalue { return( shift->on( 'visibilitychange', @_ ) ); } | ||
| 729 | |||
| 730 | sub onwheel : lvalue { return( shift->on( 'wheel', @_ ) ); } | ||
| 731 | |||
| 732 | sub open | ||
| 733 | { | ||
| 734 | my $self = shift( @_ ); | ||
| 735 | # Already opened | ||
| 736 | return( $self ) if( !$self->{_closed} ); | ||
| 737 | my $doc = $self->_list_to_nodes( q{<html><head><title></title></head><body></body></html>} ) || | ||
| 738 | return( $self->pass_error ); | ||
| 739 | # Remove the DTD | ||
| 740 | $self->declaration( undef ); | ||
| 741 | my $html = $doc->children->first; | ||
| 742 | return( $self->error( "Cannot find the top <html> element in our own standard document for document->open!" ) ) if( !$html ); | ||
| 743 | return( $self->error( "HTML top element found is not a <html> element" ) ) if( $html->tag ne 'html' ); | ||
| 744 | $html->parent( $self ); | ||
| 745 | $self->children->reset; | ||
| 746 | $self->children->push( $html ); | ||
| 747 | $self->reset(1); | ||
| 748 | return( $self ); | ||
| 749 | } | ||
| 750 | |||
| 751 | # Note: property ownerDocument inherited from HTML::Object::DOM::Node | ||
| 752 | |||
| 753 | sub parentElement { return( shift->new_null ); } | ||
| 754 | |||
| 755 | sub parentNode { return( shift->new_null ); } | ||
| 756 | |||
| 757 | sub pictureInPictureElement : lvalue { return( shift->_set_get_object_lvalue( 'pictureinpictureelement', 'HTML::Object::DOM::Element', @_ ) ); } | ||
| 758 | |||
| 759 | sub pictureInPictureEnabled : lvalue { return( shift->_set_get_boolean( 'pictureinpictureenabled', @_ ) ); } | ||
| 760 | |||
| 761 | # Note: property read-only plugins | ||
| 762 | sub plugins { return( shift->new_collection_elements ); } | ||
| 763 | |||
| 764 | sub pointerLockElement : lvalue { return( shift->_set_get_object_lvalue( 'pointerlockelement', 'HTML::Object::DOM::Element', @_ ) ); } | ||
| 765 | |||
| 766 | sub prepend | ||
| 767 | { | ||
| 768 | my $self = shift( @_ ); | ||
| 769 | return( $self ) if( !scalar( @_ ) ); | ||
| 770 | # If a HTML::Object::DOM::DocumentFragment object is provided, its children are | ||
| 771 | # copied to the list and its own children array is emptied. | ||
| 772 | my $list = $self->_check_list_of_nodes_or_text( @_ ) || return( $self->pass_error ); | ||
| 773 | my $nodes = $self->nodes; | ||
| 774 | $self->reset(1) if( !$list->is_empty ); | ||
| 775 | $list->foreach(sub | ||
| 776 | { | ||
| 777 | $_->parent( $self ); | ||
| 778 | }); | ||
| 779 | $nodes->unshift( $list->list ); | ||
| 780 | return( $self ); | ||
| 781 | } | ||
| 782 | |||
| 783 | sub previousSibling { return( shift->new_null ); } | ||
| 784 | |||
| 785 | sub queryCommandEnabled { return(0); } | ||
| 786 | |||
| 787 | sub queryCommandIndeterm { return(1); } | ||
| 788 | |||
| 789 | sub queryCommandState { return(0); } | ||
| 790 | |||
| 791 | sub queryCommandSupported { return(0); } | ||
| 792 | |||
| 793 | sub queryCommandValue { return; } | ||
| 794 | |||
| 795 | # Note: method querySelector is inherited from HTML::Object::DOM::Element | ||
| 796 | |||
| 797 | # Note: method querySelectorAll is inherited from HTML::Object::DOM::Element | ||
| 798 | |||
| 799 | sub readyState : lvalue { return( shift->_set_get_lvalue( 'readyState', @_ ) ); } | ||
| 800 | |||
| 801 | sub referrer : lvalue { return( shift->_set_get_uri( 'referrer', @_ ) ); } | ||
| 802 | |||
| 803 | sub releaseCapture { return( shift->defaultView->releaseCapture( @_ ) ); } | ||
| 804 | |||
| 805 | sub releaseEvents { return( shift->defaultView->releaseEvents( @_ ) ); } | ||
| 806 | |||
| 807 | # Note: method removeChild is inherited | ||
| 808 | |||
| 809 | # Note: replaceChild is inherited | ||
| 810 | |||
| 811 | # Note: the Document replaceChildren() method is different from the HTML::Object::DOM::Element replaceChildren() | ||
| 812 | sub replaceChildren | ||
| 813 | { | ||
| 814 | my $self = shift( @_ ); | ||
| 815 | if( !scalar( @_ ) ) | ||
| 816 | { | ||
| 817 | $self->nodes->reset; | ||
| 818 | return( $self ); | ||
| 819 | } | ||
| 820 | my $list = $self->_get_from_list_of_elements_or_html( @_ ); | ||
| 821 | return( $list ) if( $list->is_empty ); | ||
| 822 | my $html; | ||
| 823 | foreach my $e ( @$list ) | ||
| 824 | { | ||
| 825 | if( $self->_is_a( $e => 'HTML::Object::DOM::Element' ) ) | ||
| 826 | { | ||
| 827 | if( $e->tag eq 'html' ) | ||
| 828 | { | ||
| 829 | if( defined( $html ) ) | ||
| 830 | { | ||
| 831 | return( $self->error({ | ||
| 832 | message => "You have already provided an <html> element. You can only provide one.", | ||
| 833 | class => 'HTML::Object::HierarchyRequestError', | ||
| 834 | }) ); | ||
| 835 | } | ||
| 836 | else | ||
| 837 | { | ||
| 838 | $html = $e; | ||
| 839 | } | ||
| 840 | } | ||
| 841 | else | ||
| 842 | { | ||
| 843 | return( $self->error({ | ||
| 844 | message => "Only the <html> element is allowed inside the Document.", | ||
| 845 | class => 'HTML::Object::HierarchyRequestError', | ||
| 846 | }) ); | ||
| 847 | } | ||
| 848 | } | ||
| 849 | } | ||
| 850 | return( $self->error({ | ||
| 851 | message => "You have not provided any <html> element. A document must have at least one <html> element.", | ||
| 852 | class => 'HTML::Object::HierarchyRequestError', | ||
| 853 | }) ) if( !defined( $html ) ); | ||
| 854 | return( $self ) if( $list->is_empty ); | ||
| 855 | my $nodes = $self->nodes; | ||
| 856 | $nodes->reset; | ||
| 857 | $list->foreach(sub | ||
| 858 | { | ||
| 859 | $_->detach; | ||
| 860 | $_->parent( $self ); | ||
| 861 | }); | ||
| 862 | $nodes->push( $list->list ); | ||
| 863 | $self->children->reset; | ||
| 864 | $self->children->push( $html ); | ||
| 865 | return( $self ); | ||
| 866 | } | ||
| 867 | |||
| 868 | sub requestStorageAccess { return; } | ||
| 869 | |||
| 870 | # Note: property scripts read-only | ||
| 871 | sub scripts : lvalue | ||
| 872 | { | ||
| 873 | my $self = shift( @_ ); | ||
| 874 | my $results = $self->findNode( 'script' ) || return( $self->pass_error ); | ||
| 875 | return( $results ); | ||
| 876 | } | ||
| 877 | |||
| 878 | # Note: property scrollingElement read-only | ||
| 879 | sub scrollingElement : lvalue { return( shift->_set_get_object_lvalue( 'scrollingelement', 'HTML::Object::DOM::Element', @_ ) ); } | ||
| 880 | |||
| 881 | sub string_value { return; } | ||
| 882 | |||
| 883 | # Note: property styleSheets read-only | ||
| 884 | sub styleSheets : lvalue | ||
| 885 | { | ||
| 886 | my $self = shift( @_ ); | ||
| 887 | my $list = $self->findNode( 'link[rel="stylesheet"]' ) || return( $self->pass_error ); | ||
| 888 | my $results2 = $self->findNode( 'stylesheet' ) || return( $self->pass_error ); | ||
| 889 | $list->merge( $results2 ); | ||
| 890 | return( $list ); | ||
| 891 | } | ||
| 892 | |||
| 893 | # Note: property timeline read-only | ||
| 894 | sub timeline : lvalue { return; } | ||
| 895 | |||
| 896 | sub title : lvalue { return( shift->_set_get_callback({ | ||
| 897 | get => sub | ||
| 898 | { | ||
| 899 | my $self = shift( @_ ); | ||
| 900 | my $results = $self->findNode( 'title' ) || die( $self->error ); | ||
| 901 | return if( $results->is_empty ); | ||
| 902 | return( $results->first->text ); | ||
| 903 | }, | ||
| 904 | set => sub | ||
| 905 | { | ||
| 906 | my $self = shift( @_ ); | ||
| 907 | my $arg = shift( @_ ); | ||
| 908 | my $results = $self->findNode( 'title' ) || die( $self->error ); | ||
| 909 | my $e; | ||
| 910 | if( $results->is_empty ) | ||
| 911 | { | ||
| 912 | my $head_results = $self->findNode( 'head' ) || die( $self->error ); | ||
| 913 | if( $head_results->is_empty ) | ||
| 914 | { | ||
| 915 | die( "Could not find the <head> tag to set as parent of the missing <title>. Something is seriously wrong." ); | ||
| 916 | } | ||
| 917 | my $head = $head_results->first; | ||
| 918 | $e = $self->createElement( 'title' ); | ||
| 919 | $e->close; | ||
| 920 | $head->push( $e ); | ||
| 921 | $e->parent( $head ); | ||
| 922 | } | ||
| 923 | else | ||
| 924 | { | ||
| 925 | $e = $results->first; | ||
| 926 | } | ||
| 927 | |||
| 928 | return( $e->text( $arg ) ); | ||
| 929 | }, | ||
| 930 | }, @_ ) ); } | ||
| 931 | |||
| 932 | sub URL : lvalue { return( shift->_set_get_uri( 'uri', @_ ) ); } | ||
| 933 | |||
| 934 | # Note: property visibilityState read-only | ||
| 935 | sub visibilityState : lvalue { return( shift->_set_get_scalar_as_object( 'visibilitystate', @_ ) ); } | ||
| 936 | |||
| 937 | sub write | ||
| 938 | { | ||
| 939 | my $self = shift( @_ ); | ||
| 940 | if( $self->{_closed} ) | ||
| 941 | { | ||
| 942 | $self->open() || return( $self->pass_error ); | ||
| 943 | } | ||
| 944 | my $body = $self->body || return( $self->pass_error ); | ||
| 945 | # Using _list_to_nodes from HTML::Object::DOM::Node | ||
| 946 | my $list = $self->_list_to_nodes( @_ ); | ||
| 947 | $list->foreach(sub | ||
| 948 | { | ||
| 949 | $_->parent( $body ); | ||
| 950 | }); | ||
| 951 | $body->children->push( $list->list ); | ||
| 952 | return( $list ); | ||
| 953 | } | ||
| 954 | |||
| 955 | sub writeln | ||
| 956 | { | ||
| 957 | my $self = shift( @_ ); | ||
| 958 | my @args = @_; | ||
| 959 | for( @args ) | ||
| 960 | { | ||
| 961 | next if( ref( $_ ) && !overload::Method( $_, '""' ) ); | ||
| 962 | $_ .= "\n" unless( /\n$/ ); | ||
| 963 | } | ||
| 964 | return( $self->write( @args ) ); | ||
| 965 | } | ||
| 966 | |||
| 967 | sub _check_list_of_nodes_or_text | ||
| 968 | { | ||
| 969 | my $self = shift( @_ ); | ||
| 970 | # If a HTML::Object::DOM::DocumentFragment object is provided, its children are | ||
| 971 | # copied to the list and its own children array is emptied. | ||
| 972 | my $list = $self->_get_from_list_of_elements_or_html( @_ ); | ||
| 973 | return( $list ) if( $list->is_empty ); | ||
| 974 | my $html = $self->documentElement; | ||
| 975 | foreach my $e ( @$list ) | ||
| 976 | { | ||
| 977 | if( $self->_is_a( $e => 'HTML::Object::DOM::Element' ) ) | ||
| 978 | { | ||
| 979 | # There can be only one element in the document: the <html> element. | ||
| 980 | if( ref( $html ) ) | ||
| 981 | { | ||
| 982 | return( $self->error({ | ||
| 983 | message => "Cannot have more than one Element child of a Document and it must be an <html> element.", | ||
| 984 | class => 'HTML::Object::HierarchyRequestError', | ||
| 985 | }) ); | ||
| 986 | } | ||
| 987 | elsif( $e->tag ne 'html' ) | ||
| 988 | { | ||
| 989 | return( $self->error({ | ||
| 990 | message => "Only the <html> element is allowed inside the Document.", | ||
| 991 | class => 'HTML::Object::HierarchyRequestError', | ||
| 992 | }) ); | ||
| 993 | } | ||
| 994 | } | ||
| 995 | } | ||
| 996 | return( $list ); | ||
| 997 | } | ||
| 998 | |||
| 999 | sub _init_nodes | ||
| 1000 | { | ||
| 1001 | my $self = shift( @_ ); | ||
| 1002 | unless( CORE::exists( $self->{nodes} ) ) | ||
| 1003 | { | ||
| 1004 | my $children = $self->_set_get_object_array_object( 'children', 'HTML::Object::Element' ); | ||
| 1005 | $self->{nodes} = $children; | ||
| 1006 | my $html = $children->grep(sub{ $self->_is_a( $_ => 'HTML::Object::DOM::Node' ) && $_->tag eq 'html' })->first; | ||
| 1007 | # We have a document with an <html> tag, we change the children to be an array with just the <html> element | ||
| 1008 | # and we set nodes to be the array containing all top nodes. | ||
| 1009 | # Maybe, we should change that structural design in the future to make it better | ||
| 1010 | if( $html ) | ||
| 1011 | { | ||
| 1012 | my $new_children = $self->new_array( $html ); | ||
| 1013 | $self->_set_get_object_array_object( 'children', 'HTML::Object::Element', $new_children ); | ||
| 1014 | } | ||
| 1015 | } | ||
| 1016 | return( $self->{nodes} ); | ||
| 1017 | } | ||
| 1018 | |||
| 1019 | sub _set_get_on_signal : lvalue | ||
| 1020 | { | ||
| 1021 | my $self = shift( @_ ); | ||
| 1022 | my $sigs = shift( @_ ) || return; | ||
| 1023 | return if( ref( $sigs ) ne 'ARRAY' ); | ||
| 1024 | my $has_arg = 0; | ||
| 1025 | my $arg; | ||
| 1026 | if( want( qw( LVALUE ASSIGN ) ) ) | ||
| 1027 | { | ||
| 1028 | ( $arg ) = want( 'ASSIGN' ); | ||
| 1029 | $has_arg = 'assign'; | ||
| 1030 | } | ||
| 1031 | else | ||
| 1032 | { | ||
| 1033 | @_ = () if( scalar( @_ ) == 1 && !defined( $_[0] ) ); | ||
| 1034 | if( @_ ) | ||
| 1035 | { | ||
| 1036 | $arg = shift( @_ ); | ||
| 1037 | $has_arg++; | ||
| 1038 | } | ||
| 1039 | } | ||
| 1040 | # To empty it, it must be set to an empty string | ||
| 1041 | if( $has_arg && defined( $arg ) ) | ||
| 1042 | { | ||
| 1043 | my $code = $arg; | ||
| 1044 | if( ref( $code ) ne 'CODE' ) | ||
| 1045 | { | ||
| 1046 | my $error = "Value provided is not a code reference (reference to a subroutine or anonymous subroutine)."; | ||
| 1047 | if( $has_arg eq 'assign' ) | ||
| 1048 | { | ||
| 1049 | $self->error({ message => $error, class => 'HTML::Object::TypeError' }); | ||
| 1050 | return( $self->{__lvalue_error} = undef ); | ||
| 1051 | } | ||
| 1052 | return( $self->error({ message => $error, class => 'HTML::Object::TypeError' }) ) if( want( 'LVALUE' ) ); | ||
| 1053 | Wanted::rreturn( $self->error({ message => $error, class => 'HTML::Object::TypeError' }) ); | ||
| 1054 | } | ||
| 1055 | foreach my $sig ( @$sigs ) | ||
| 1056 | { | ||
| 1057 | $SIG{ $sig } = sub | ||
| 1058 | { | ||
| 1059 | my $type; | ||
| 1060 | if( $sig eq '__WARN__' || $sig eq '__DIE__' ) | ||
| 1061 | { | ||
| 1062 | ( $type = $sig ) =~ s/^__([A-Z]+)__$/$1/; | ||
| 1063 | } | ||
| 1064 | else | ||
| 1065 | { | ||
| 1066 | $type = shift( @_ ); | ||
| 1067 | } | ||
| 1068 | my $event = HTML::Object::ErrorEvent->new( $type, @_ ); | ||
| 1069 | $code->( $event ); | ||
| 1070 | }; | ||
| 1071 | } | ||
| 1072 | } | ||
| 1073 | my $dummy = 1; | ||
| 1074 | return( $dummy ) if( want( 'LVALUE' ) ); | ||
| 1075 | Wanted::rreturn( $dummy ); | ||
| 1076 | } | ||
| 1077 | |||
| 1078 | 1; | ||
| 1079 | # NOTE: POD | ||
| 1080 | __END__ |