blib/lib/HTML/Object/DOM/Document.pm untested

filter: Statementstmt 0.0% 0 / 513 Branchbran 0.0% 0 / 144 Conditioncond 0.0% 0 / 145 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 140 Podpod 0.0% 0 / 125 totaltotal 0.0% 0 / 1067 CC 73 SCAR 28.5
CC
73
Cov
0%
_set_get_on_signal
132.0
replaceChildren
72.0
getElementById
42.0
CRAP 17.2
1 ##----------------------------------------------------------------------------
2 ## HTML Object - ~/lib/HTML/Object/DOM/Document.pm
3 ## Version v0.3.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::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.0';
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 # method cloneNode is inherited from HTML::Object::DOM::Node
208  
209 sub close { return( shift->{_closed} = 1 ); }
210  
211 sub compatMode : lvalue { return; }
212  
213 # read-only
214 # e.g.:
215 # <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
216 sub contentType
217 {
218 my $self = shift( @_ );
219 my $results = $self->findNode( 'meta' ) || return( $self->pass_error );
220 require Module::Generic::HeaderValue;
221 my $default = 'text/html';
222 foreach my $e ( $results->list )
223 {
224 if( $e->attributes->has( 'http-equiv' ) &&
225 $e->attributes->get( 'http-equiv' ) eq 'Content-Type' &&
226 $e->attributes->has( 'content' ) )
227 {
228 my $type = $e->attributes->get( 'content' );
229 next if( !defined( $type ) || !CORE::length( $type ) );
230 my $hv = Module::Generic::HeaderValue->new_from_header( $type );
231 return( $hv->value->first // $default );
232 }
233 }
234 return( $default );
235 }
236  
237 sub cookie : lvalue { return( shift->_set_get_lvalue( '_cookie', @_ ) ); }
238  
239 sub createAttribute
240 {
241 my $self = shift( @_ );
242 my $name = shift( @_ );
243 $name = lc( $name ) if( defined( $name ) );
244 return( $self->error({
245 message => "The attribute provided \"$name\" contains illegal characters.",
246 class => 'HTML::Object::SyntaxError',
247 }) ) if( !$self->is_valid_attribute( $name ) );
248 require HTML::Object::DOM::Attribute;
249 my $att = HTML::Object::DOM::Attribute->new( $name, @_ ) ||
250 return( $self->pass_error( HTML::Object::DOM::Attribute->error ) );
251 return( $att );
252 }
253  
254 sub createAttributeNS { return; }
255  
256 sub createCDATASection { return; }
257  
258 # TODO Create pod: https://developer.mozilla.org/en-US/docs/Web/API/Document/createComment
259 sub createComment
260 {
261 my $self = shift( @_ );
262 my $data;
263 $data = join( '', @_ ) if( @_ );
264 return( $self->new_comment( value => $data ) );
265 }
266  
267 sub createDocumentFragment
268 {
269 my $self = shift( @_ );
270 require HTML::Object::DOM::DocumentFragment;
271 my $frag = HTML::Object::DOM::DocumentFragment->new( @_ ) ||
272 return( $self->pass_error( HTML::Object::DOM::DocumentFragment->error ) );
273 return( $frag );
274 }
275  
276 sub createElement
277 {
278 my $self = shift( @_ );
279 my $tag = shift( @_ ) || return( $self->error( "No tag was provided to create an element." ) );
280 my $opts = $self->_get_args_as_hash( @_ );
281 my $p = HTML::Object::DOM->new;
282 my $def = $p->get_definition( $tag );
283 # Even if the tag does not exist, the default behaviour is to accept whatever was
284 # provided and assume it is a non-empty tag
285 if( !defined( $def ) )
286 {
287 $def =
288 {
289 is_empty => 0,
290 is_inline => 0,
291 };
292 }
293
294 $opts->{debug} = $self->debug;
295 @$opts{qw( is_empty is_inline )} = @$def{qw( is_empty is_inline )};
296 $opts->{tag} = $tag;
297
298 my $e;
299 $def->{class} //= '';
300 if( $def->{class} )
301 {
302 $e = $p->new_special( $def->{class} => $opts );
303 }
304 else
305 {
306 $e = $p->new_element( $opts );
307 }
308 $e->close;
309 return( $e );
310 }
311  
312 sub createElementNS { return; }
313  
314 # TODO: createEntityReference
315 sub createEntityReference { return; }
316  
317 # TODO createEvent https://developer.mozilla.org/en-US/docs/Web/API/Document/createEvent
318 sub createEvent
319 {
320 my $self = shift( @_ );
321 my $type = shift( @_ ) || return( $self->error({
322 message => 'No event type was provided',
323 class => 'HTML::Objct::SyntaxError',
324 }) );
325 $self->_load_class( 'HTML::Object::Event' ) || return( $self->pass_error );
326 my $evt = HTML::Object::Event->new( $type, @_ ) ||
327 return( $self->pass_error( HTML::Object::Event->error ) );
328 return( $evt );
329 }
330  
331 # TODO: createExpression
332 sub createExpression
333 {
334 my $self = shift( @_ );
335 my $eval;
336 unless( $eval = $self->{_xpath_eval} )
337 {
338 $self->_load_class( 'HTML::Object::DOM::XPathEvaluator' ) || return( $self->pass_error );
339 $self->{_xpath_eval} = $eval = HTML::Object::DOM::XPathEvaluator->new ||
340 return( $self->pass_error( HTML::Object::DOM::XPathEvaluator->error ) );
341 }
342 my $expr = $eval->createExpression( @_ );
343 return( $self->pass_error( $eval->error ) ) if( !defined( $expr ) );
344 return( $expr );
345 }
346  
347 # TODO: createNSResolver
348 sub createNSResolver { return; }
349  
350 # TODO: createNodeIterator
351 sub createNodeIterator
352 {
353 my $self = shift( @_ );
354 $self->_load_class( 'HTML::Object::DOM::NodeIterator' ) || return( $self->pass_error );
355 my $iterator = HTML::Object::DOM::NodeIterator->new( @_ );
356 return( $self->pass_error( HTML::Object::DOM::NodeIterator->error ) ) if( !defined( $iterator ) );
357 return( $iterator );
358 }
359  
360 sub createProcessingInstruction { return; }
361  
362 # TODO: createRange
363 sub createRange { return; }
364  
365 sub createTextNode
366 {
367 my $self = shift( @_ );
368 my $txt = @_ == 1 ? shift( @_ ) : join( '', @_ );
369 $self->_load_class( 'HTML::Object::DOM::Text' ) || return( $self->pass_error );
370 my $node = HTML::Object::DOM::Text->new( value => $txt ) ||
371 return( $self->pass_error( HTML::Object::DOM::Text->error ) );
372 return( $node );
373 }
374  
375 sub createTouch { return; }
376  
377 sub createTouchList { return; }
378  
379 sub createTreeWalker
380 {
381 my $self = shift( @_ );
382 $self->_load_class( 'HTML::Object::DOM::TreeWalker' ) || return( $self->pass_error );
383 my $crawler = HTML::Object::DOM::TreeWalker->new( @_ );
384 return( $self->pass_error( HTML::Object::DOM::TreeWalker->error ) ) if( !defined( $crawler ) );
385 return( $crawler );
386 }
387  
388 sub currentScript { return( shift->_set_get_object_lvalue( 'currentscript', 'HTML::Object::DOM::Element', @_ ) ); }
389  
390 sub defaultView : lvalue { return( shift->_set_get_object_lvalue( 'defaultview', 'HTML::Object::DOM::Window', @_ ) ); }
391  
392 sub designMode : lvalue { return; }
393  
394 sub dir : lvalue { return( shift->_set_get_lvalue( '_dir' ) ); }
395  
396 # Note: property doctype read-only
397 sub doctype : lvalue
398 {
399 my $self = shift( @_ );
400 return( $self->declaration );
401 }
402  
403 # Note: property documentElement read-only
404 sub documentElement : lvalue
405 {
406 my $self = shift( @_ );
407 if( @_ )
408 {
409 warn( "This property \"documentElement\" is read-only.\n" ) if( $self->_is_warnings_enabled( 'HTML::Object' ) );
410 }
411 my $html;
412 # It should be the first one, but let's not assume anything
413 my $children = $self->children;
414 foreach( @$children )
415 {
416 if( $_->tag eq 'html' )
417 {
418 $html = $_;
419 last;
420 }
421 }
422 if( !$html && want( 'OBJECT' ) )
423 {
424 require Module::Generic::Null;
425 return( Module::Generic::Null->new( wants => 'OBJECT' ) );
426 }
427 return( $html );
428 }
429  
430 # Note: property documentURI read-only
431 sub documentURI : lvalue { return( shift->_set_get_uri( 'uri', @_ ) ); }
432  
433 sub elementFromPoint { return; }
434  
435 sub elementsFromPoint { return; }
436  
437 # Note: property read-only embeds
438 sub embeds
439 {
440 my $self = shift( @_ );
441 my $results = $self->findNode( 'embed' ) || return( $self->pass_error );
442 return( $self->new_collection_elements( $results ) );
443 }
444  
445 sub enableStyleSheetsForSet { return; }
446  
447 sub evaluate
448 {
449 my $self = shift( @_ );
450 my $eval;
451 unless( $eval = $self->{_xpath_eval} )
452 {
453 $self->_load_class( 'HTML::Object::DOM::XPathEvaluator' ) || return( $self->pass_error );
454 $self->{_xpath_eval} = $eval = HTML::Object::DOM::XPathEvaluator->new ||
455 return( $self->pass_error( HTML::Object::DOM::XPathEvaluator->error ) );
456 }
457 return( $eval->evaluate( @_ ) );
458 }
459  
460 sub execCommand { return; }
461  
462 sub exitPictureInPicture { return; }
463  
464 sub exitPointerLock { return; }
465  
466 sub featurePolicy : lvalue { return; }
467  
468 # Note: firstChild returns its values from the nodes array, not the children one; is inherited
469  
470 # Note: method firstElementChild is inherited from HTML::Object::DOM::Element
471  
472 # Note: property read-only fonts
473 sub fonts { return; }
474  
475 # Note: property read-only forms
476 sub forms
477 {
478 my $self = shift( @_ );
479 my $results = $self->findNode( 'form' ) || return( $self->pass_error );
480 return( $self->new_collection_elements( $results ) );
481 }
482  
483 sub fullscreenElement : lvalue { return; }
484  
485 sub getAnimations { return; }
486  
487 sub getBoxQuads { return; }
488  
489 sub getElementById
490 {
491 my $self = shift( @_ );
492 my $id = shift( @_ );
493 return if( !defined( $id ) || !CORE::length( $id ) );
494 my $seen = {};
495 my $crawl;
496 $crawl = sub
497 {
498 my $elem = shift( @_ );
499 my $addr = Scalar::Util::refaddr( $elem );
500 return if( CORE::exists( $seen->{ $addr } ) );
501 # Not a true element, such as text, comment
502 return if( $elem->tag->substr( 0, 1 ) eq '_' && !$self->_is_a( $elem => 'HTML::Object::Document' ) );
503 $seen->{ $addr }++;
504 if( $elem->attributes->has( 'id' ) &&
505 $elem->attributes->get( 'id' ) eq $id )
506 {
507 return( $elem );
508 }
509 my $e;
510 $elem->children->foreach(sub
511 {
512 my $this = shift( @_ );
513 if( my $found = $crawl->( $this ) )
514 {
515 $e = $found;
516 # last
517 return(0);
518 }
519 return(1);
520 });
521 return( $e );
522 };
523 return( $crawl->( $self ) );
524 }
525  
526 # Note: method getElementsByClassName is inherited from HTML::Object::DOM::Element
527  
528 sub getElementsByName
529 {
530 my $self = shift( @_ );
531 my $name = shift( @_ );
532 return( $self->error( "No value was provided for getElementsByName()" ) ) if( !defined( $name ) || !CORE::length( "$name" ) );
533 my $results = $self->look_down( name => $name ) || return( $self->pass_error );
534 my $list = $self->new_nodelist( $results ) || return( $self->pass_error );
535 return( $list );
536 }
537  
538 # Note: method getElementsByTagName is inherited from HTML::Object::DOM::Element
539  
540 sub getElementsByTagNameNS { return; }
541  
542 sub getRootNode { return( shift->root ); }
543  
544 sub getSelection { return; }
545  
546 # Note: method hasChildNodes is inherited
547  
548 sub hasFocus { return(1); }
549  
550 sub hasStorageAccess { return; }
551  
552 # Note: property head read-only
553 sub head : lvalue
554 {
555 my $self = shift( @_ );
556 my $results = $self->findNode( 'head' ) || return( $self->pass_error );
557 my $head = $results->first;
558 if( !$head && want( 'OBJECT' ) )
559 {
560 require Module::Generic::Null;
561 rreturn( Module::Generic::Null->new( wants => 'OBJECT' ) );
562 }
563 return( $head );
564 }
565  
566 # Note: property hidden read-only
567 sub hidden : lvalue { return; }
568  
569 # Note: property images read-only
570 sub images : lvalue
571 {
572 my $self = shift( @_ );
573 my $results = $self->findNode( 'img' ) || return( $self->pass_error );
574 return( $self->new_collection_elements( $results ) );
575 }
576  
577 # Note: property implementation read-only
578 sub implementation
579 {
580 my $self = shift( @_ );
581 unless( $self->{_implementation} )
582 {
583 $self->_load_class( 'HTML::Object::DOM::Implementation' ) || return( $self->pass_error );
584 $self->{_implementation} = HTML::Object::DOM::Implementation->new( debug => $self->debug ) ||
585 return( $self->pass_error( HTML::Object::DOM::Implementation->error ) );
586 }
587 return( $self->{_implementation} );
588 }
589  
590 sub importNode { return( shift->adoptNode( @_ ) ); }
591  
592 # Note: method insertAfter is inherited
593  
594 # Note: method insertBefore is inherited
595  
596 # Note: property lastChild is inherited
597  
598 # Note: method lastElementChild is inherited from HTML::Object::DOM::Element
599  
600 sub lastModified : lvalue { return( shift->_set_get_datetime( '_last_modified' ) ); }
601  
602 # Note: property links read-only
603 sub links : lvalue
604 {
605 my $self = shift( @_ );
606 my $results = $self->findNode( 'a' ) || return( $self->pass_error );
607 return( $results );
608 }
609  
610 sub location : lvalue { return( shift->_set_get_uri( 'uri', @_ ) ); }
611  
612 sub mozSetImageElement { return; }
613  
614 # Note: property mozSyntheticDocument
615 sub mozSyntheticDocument : lvalue { return( shift->_set_get_boolean( 'mozsyntheticdocument', @_ ) ); }
616  
617 sub nextSibling { return( shift->new_null ); }
618  
619 # See also children for its alter ego
620 sub nodes { return( shift->_init_nodes ); }
621  
622 # Note: Property
623 # Should be undef for document
624 # <https://developer.mozilla.org/en-US/docs/Web/API/Node/nodeValue>
625 sub nodeValue { return; }
626  
627 # Note: this is merely an alias to HTML::Object::DOM::Node->normalize
628 sub normalizeDocument { return( shift->normalize( @_ ) ); }
629  
630 # Note: Property
631 sub onabort : lvalue { return( shift->_set_get_on_signal( [qw( ABRT TERM INT )], @_ ) ); }
632  
633 sub onafterscriptexecute : lvalue { return( shift->on( 'afterscriptexecute', @_ ) ); }
634  
635 sub onbeforescriptexecute : lvalue { return( shift->on( 'beforescriptexecute', @_ ) ); }
636  
637 sub oncopy : lvalue { return( shift->on( 'copy', @_ ) ); }
638  
639 sub oncut : lvalue { return( shift->on( 'cut', @_ ) ); }
640  
641 sub onerror : lvalue { return( shift->_set_get_on_signal( [qw( __WARN__ )], @_ ) ); }
642  
643 sub onfullscreenchange : lvalue { return( shift->on( 'fullscreenchange', @_ ) ); }
644  
645 sub onfullscreenerror : lvalue { return( shift->on( 'fullscreenerror', @_ ) ); }
646  
647 sub onload : lvalue { return( shift->_set_get_code( 'onload', @_ ) ); }
648  
649 sub onpaste : lvalue { return( shift->on( 'paste', @_ ) ); }
650  
651 # sub onreadystatechange : lvalue { return( shift->_set_get_code( 'onreadystatechange', @_ ) ); }
652 sub onreadystatechange : lvalue { return( shift->on( 'readystatechange', @_ ) ); }
653  
654 sub onscroll : lvalue { return( shift->on( 'scroll', @_ ) ); }
655  
656 sub onselectionchange : lvalue { return( shift->on( 'selectionchange', @_ ) ); }
657  
658 sub onvisibilitychange : lvalue { return( shift->on( 'visibilitychange', @_ ) ); }
659  
660 sub onwheel : lvalue { return( shift->on( 'wheel', @_ ) ); }
661  
662 sub open
663 {
664 my $self = shift( @_ );
665 # Already opened
666 return( $self ) if( !$self->{_closed} );
667 my $doc = $self->_list_to_nodes( q{<html><head><title></title></head><body></body></html>} ) ||
668 return( $self->pass_error );
669 # Remove the DTD
670 $self->declaration( undef );
671 my $html = $doc->children->first;
672 return( $self->error( "Cannot find the top <html> element in our own standard document for document->open!" ) ) if( !$html );
673 return( $self->error( "HTML top element found is not a <html> element" ) ) if( $html->tag ne 'html' );
674 $html->parent( $self );
675 $self->children->reset;
676 $self->children->push( $html );
677 $self->reset(1);
678 return( $self );
679 }
680  
681 # Note: property ownerDocument inherited from HTML::Object::DOM::Node
682  
683 sub parentElement { return( shift->new_null ); }
684  
685 sub parentNode { return( shift->new_null ); }
686  
687 sub pictureInPictureElement : lvalue { return( shift->_set_get_object_lvalue( 'pictureinpictureelement', 'HTML::Object::DOM::Element', @_ ) ); }
688  
689 sub pictureInPictureEnabled : lvalue { return( shift->_set_get_boolean( 'pictureinpictureenabled', @_ ) ); }
690  
691 # Note: property read-only plugins
692 sub plugins { return( shift->new_collection_elements ); }
693  
694 sub pointerLockElement : lvalue { return( shift->_set_get_object_lvalue( 'pointerlockelement', 'HTML::Object::DOM::Element', @_ ) ); }
695  
696 sub prepend
697 {
698 my $self = shift( @_ );
699 return( $self ) if( !scalar( @_ ) );
700 # If a HTML::Object::DOM::DocumentFragment object is provided, its children are
701 # copied to the list and its own children array is emptied.
702 my $list = $self->_check_list_of_nodes_or_text( @_ ) || return( $self->pass_error );
703 my $nodes = $self->nodes;
704 $self->reset(1) if( !$list->is_empty );
705 $list->foreach(sub
706 {
707 $_->parent( $self );
708 });
709 $nodes->unshift( $list->list );
710 return( $self );
711 }
712  
713 sub previousSibling { return( shift->new_null ); }
714  
715 sub queryCommandEnabled { return(0); }
716  
717 sub queryCommandIndeterm { return(1); }
718  
719 sub queryCommandState { return(0); }
720  
721 sub queryCommandSupported { return(0); }
722  
723 sub queryCommandValue { return; }
724  
725 # Note: method querySelector is inherited from HTML::Object::DOM::Element
726  
727 # Note: method querySelectorAll is inherited from HTML::Object::DOM::Element
728  
729 sub readyState : lvalue { return( shift->_set_get_lvalue( 'readyState', @_ ) ); }
730  
731 sub referrer : lvalue { return( shift->_set_get_uri( 'referrer', @_ ) ); }
732  
733 sub releaseCapture { return( shift->defaultView->releaseCapture( @_ ) ); }
734  
735 sub releaseEvents { return( shift->defaultView->releaseEvents( @_ ) ); }
736  
737 # Note: method removeChild is inherited
738  
739 # Note: replaceChild is inherited
740  
741 # Note: the Document replaceChildren() method is different from the HTML::Object::DOM::Element replaceChildren()
742 sub replaceChildren
743 {
744 my $self = shift( @_ );
745 if( !scalar( @_ ) )
746 {
747 $self->nodes->reset;
748 return( $self );
749 }
750 my $list = $self->_get_from_list_of_elements_or_html( @_ );
751 return( $list ) if( $list->is_empty );
752 my $html;
753 foreach my $e ( @$list )
754 {
755 if( $self->_is_a( $e => 'HTML::Object::DOM::Element' ) )
756 {
757 if( $e->tag eq 'html' )
758 {
759 if( defined( $html ) )
760 {
761 return( $self->error({
762 message => "You have already provided an <html> element. You can only provide one.",
763 class => 'HTML::Object::HierarchyRequestError',
764 }) );
765 }
766 else
767 {
768 $html = $e;
769 }
770 }
771 else
772 {
773 return( $self->error({
774 message => "Only the <html> element is allowed inside the Document.",
775 class => 'HTML::Object::HierarchyRequestError',
776 }) );
777 }
778 }
779 }
780 return( $self->error({
781 message => "You have not provided any <html> element. A document must have at least one <html> element.",
782 class => 'HTML::Object::HierarchyRequestError',
783 }) ) if( !defined( $html ) );
784 return( $self ) if( $list->is_empty );
785 my $nodes = $self->nodes;
786 $nodes->reset;
787 $list->foreach(sub
788 {
789 $_->detach;
790 $_->parent( $self );
791 });
792 $nodes->push( $list->list );
793 $self->children->reset;
794 $self->children->push( $html );
795 return( $self );
796 }
797  
798 sub requestStorageAccess { return; }
799  
800 # Note: property scripts read-only
801 sub scripts : lvalue
802 {
803 my $self = shift( @_ );
804 my $results = $self->findNode( 'script' ) || return( $self->pass_error );
805 return( $results );
806 }
807  
808 # Note: property scrollingElement read-only
809 sub scrollingElement : lvalue { return( shift->_set_get_object_lvalue( 'scrollingelement', 'HTML::Object::DOM::Element', @_ ) ); }
810  
811 sub string_value { return; }
812  
813 # Note: property styleSheets read-only
814 sub styleSheets : lvalue
815 {
816 my $self = shift( @_ );
817 my $list = $self->findNode( 'link[rel="stylesheet"]' ) || return( $self->pass_error );
818 my $results2 = $self->findNode( 'stylesheet' ) || return( $self->pass_error );
819 $list->merge( $results2 );
820 return( $list );
821 }
822  
823 # Note: property timeline read-only
824 sub timeline : lvalue { return; }
825  
826 sub title : lvalue { return( shift->_set_get_callback({
827 get => sub
828 {
829 my $self = shift( @_ );
830 my $results = $self->findNode( 'title' ) || die( $self->error );
831 return if( $results->is_empty );
832 return( $results->first->text );
833 },
834 set => sub
835 {
836 my $self = shift( @_ );
837 my $arg = shift( @_ );
838 my $results = $self->findNode( 'title' ) || die( $self->error );
839 my $e;
840 if( $results->is_empty )
841 {
842 my $head_results = $self->findNode( 'head' ) || die( $self->error );
843 if( $head_results->is_empty )
844 {
845 die( "Could not find the <head> tag to set as parent of the missing <title>. Something is seriously wrong." );
846 }
847 my $head = $head_results->first;
848 $e = $self->createElement( 'title' );
849 $e->close;
850 $head->push( $e );
851 $e->parent( $head );
852 }
853 else
854 {
855 $e = $results->first;
856 }
857
858 return( $e->text( $arg ) );
859 },
860 }, @_ ) ); }
861  
862 sub URL : lvalue { return( shift->_set_get_uri( 'uri', @_ ) ); }
863  
864 # Note: property visibilityState read-only
865 sub visibilityState : lvalue { return( shift->_set_get_scalar_as_object( 'visibilitystate', @_ ) ); }
866  
867 sub write
868 {
869 my $self = shift( @_ );
870 if( $self->{_closed} )
871 {
872 $self->open() || return( $self->pass_error );
873 }
874 my $body = $self->body || return( $self->pass_error );
875 # Using _list_to_nodes from HTML::Object::DOM::Node
876 my $list = $self->_list_to_nodes( @_ );
877 $list->foreach(sub
878 {
879 $_->parent( $body );
880 });
881 $body->children->push( $list->list );
882 return( $list );
883 }
884  
885 sub writeln
886 {
887 my $self = shift( @_ );
888 my @args = @_;
889 for( @args )
890 {
891 next if( ref( $_ ) && !overload::Method( $_, '""' ) );
892 $_ .= "\n" unless( /\n$/ );
893 }
894 return( $self->write( @args ) );
895 }
896  
897 sub _check_list_of_nodes_or_text
898 {
899 my $self = shift( @_ );
900 # If a HTML::Object::DOM::DocumentFragment object is provided, its children are
901 # copied to the list and its own children array is emptied.
902 my $list = $self->_get_from_list_of_elements_or_html( @_ );
903 return( $list ) if( $list->is_empty );
904 my $html = $self->documentElement;
905 foreach my $e ( @$list )
906 {
907 if( $self->_is_a( $e => 'HTML::Object::DOM::Element' ) )
908 {
909 # There can be only one element in the document: the <html> element.
910 if( ref( $html ) )
911 {
912 return( $self->error({
913 message => "Cannot have more than one Element child of a Document and it must be an <html> element.",
914 class => 'HTML::Object::HierarchyRequestError',
915 }) );
916 }
917 elsif( $e->tag ne 'html' )
918 {
919 return( $self->error({
920 message => "Only the <html> element is allowed inside the Document.",
921 class => 'HTML::Object::HierarchyRequestError',
922 }) );
923 }
924 }
925 }
926 return( $list );
927 }
928  
929 sub _init_nodes
930 {
931 my $self = shift( @_ );
932 unless( CORE::exists( $self->{nodes} ) )
933 {
934 my $children = $self->_set_get_object_array_object( 'children', 'HTML::Object::Element' );
935 $self->{nodes} = $children;
936 my $html = $children->grep(sub{ $self->_is_a( $_ => 'HTML::Object::DOM::Node' ) && $_->tag eq 'html' })->first;
937 # We have a document with an <html> tag, we change the children to be an array with just the <html> element
938 # and we set nodes to be the array containing all top nodes.
939 # Maybe, we should change that structural design in the future to make it better
940 if( $html )
941 {
942 my $new_children = $self->new_array( $html );
943 $self->_set_get_object_array_object( 'children', 'HTML::Object::Element', $new_children );
944 }
945 }
946 return( $self->{nodes} );
947 }
948  
949 sub _set_get_on_signal : lvalue
950 {
951 my $self = shift( @_ );
952 my $sigs = shift( @_ ) || return;
953 return if( ref( $sigs ) ne 'ARRAY' );
954 my $has_arg = 0;
955 my $arg;
956 if( want( qw( LVALUE ASSIGN ) ) )
957 {
958 ( $arg ) = want( 'ASSIGN' );
959 $has_arg = 'assign';
960 }
961 else
962 {
963 @_ = () if( scalar( @_ ) == 1 && !defined( $_[0] ) );
964 if( @_ )
965 {
966 $arg = shift( @_ );
967 $has_arg++;
968 }
969 }
970 # To empty it, it must be set to an empty string
971 if( $has_arg && defined( $arg ) )
972 {
973 my $code = $arg;
974 if( ref( $code ) ne 'CODE' )
975 {
976 my $error = "Value provided is not a code reference (reference to a subroutine or anonymous subroutine).";
977 if( $has_arg eq 'assign' )
978 {
979 $self->error({ message => $error, class => 'HTML::Object::TypeError' });
980 return( $self->{__lvalue_error} = undef );
981 }
982 return( $self->error({ message => $error, class => 'HTML::Object::TypeError' }) ) if( want( 'LVALUE' ) );
983 Wanted::rreturn( $self->error({ message => $error, class => 'HTML::Object::TypeError' }) );
984 }
985 foreach my $sig ( @$sigs )
986 {
987 $SIG{ $sig } = sub
988 {
989 my $type;
990 if( $sig eq '__WARN__' || $sig eq '__DIE__' )
991 {
992 ( $type = $sig ) =~ s/^__([A-Z]+)__$/$1/;
993 }
994 else
995 {
996 $type = shift( @_ );
997 }
998 my $event = HTML::Object::ErrorEvent->new( $type, @_ );
999 $code->( $event );
1000 };
1001 }
1002 }
1003 my $dummy = 1;
1004 return( $dummy ) if( want( 'LVALUE' ) );
1005 Wanted::rreturn( $dummy );
1006 }
1007  
1008 1;
1009 # NOTE: POD
1010 __END__
1011  
1012 =encoding utf-8
1013  
1014 =head1 NAME
1015  
1016 HTML::Object::DOM::Document - HTML Object DOM Document Class
1017  
1018 =head1 SYNOPSIS
1019  
1020 use HTML::Object::DOM::Document;
1021 my $doc = HTML::Object::DOM::Document->new ||
1022 die( HTML::Object::DOM::Document->error, "\n" );
1023  
1024 =head1 VERSION
1025  
1026 v0.3.0
1027  
1028 =head1 DESCRIPTION
1029  
1030 This module represents an HTML document. It inherits from L<HTML::Object::Document> and L<HTML::Object::DOM::Element>
1031  
1032 It is the top object in the hierarchy and thus has no parent. It should contain only one child, the C<html> element, and has one associated L<element|HTML::Object::DOM::Element>, the L<doctype|/doctype>, which can also be accessed with L</declaration>
1033  
1034 =head1 INHERITANCE
1035  
1036 +------------------------+ +---------------------------+ +-------------------------+ +----------------------------+ +-----------------------------+
1037 | HTML::Object::Element | --> | HTML::Object::EventTarget | --> | HTML::Object::DOM::Node | --> | HTML::Object::DOM::Element | --> | HTML::Object::DOM::Document |
1038 +------------------------+ +---------------------------+ +-------------------------+ +----------------------------+ +-----------------------------+
1039 | ^
1040 | |
1041 v |
1042 +------------------------+ |
1043 | HTML::Object::Document | -----------------------------------------------------------------------------------------------------------+
1044 +------------------------+
1045  
1046 =head1 PROPERTIES
1047  
1048 =head2 activeElement
1049  
1050 Normally this returns C<undef> under perl, but you can set it to whatever L<element object|HTML::Object::DOM::Element> you want.
1051  
1052 Under JavaScript, this returns the L<element|HTML::Object::DOM::Element> that currently has focus.
1053  
1054 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/Document/activeElement>
1055  
1056 =head2 body
1057  
1058 Returns the C<body> L<node object|HTML::Object::Element> of the current document.
1059  
1060 Example:
1061  
1062 # Given this HTML: <body id="oldBodyElement"></body>
1063 say($doc->body->id); # "oldBodyElement"
1064  
1065 my $aNewBodyElement = $doc->createElement("body");
1066  
1067 $aNewBodyElement->id = "newBodyElement";
1068 $doc->body = $aNewBodyElement;
1069 say($doc->body->id); # "newBodyElement"
1070  
1071 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/Document/body>
1072  
1073 =head2 characterSet
1074  
1075 This is read-only.
1076  
1077 Returns the character set being used by the document. This is always C<utf-8>
1078  
1079 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/Document/characterSet>
1080  
1081 =head2 childElementCount
1082  
1083 This is read-only.
1084  
1085 Returns the number of child elements of the current document. This being the top document, it typically contains only 1 element, the C<<html>> tag.
1086  
1087 The total number of children is not the same as the number of child nodes.
1088  
1089 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/Document/childElementCount>
1090  
1091 =head2 children
1092  
1093 This is read-only.
1094  
1095 Returns the child elements of the current document, as an L<array object|Module::Generic::Array>.
1096  
1097 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/Document/children>
1098  
1099 =head2 compatMode
1100  
1101 Returns C<undef> since this is not relevant under perl.
1102  
1103 Normally, it would indicate whether the document is rendered in quirks or strict mode.
1104  
1105 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/Document/compatMode>
1106  
1107 =head2 contentType
1108  
1109 This is read-only.
1110  
1111 Returns the Content-Type from the MIME Header of the current document.
1112  
1113 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/Document/contentType>
1114  
1115 =head2 cookie
1116  
1117 Set or get a semicolon-separated list of the cookies for that document or sets a single cookie.
1118  
1119 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/Document/cookie>
1120  
1121 =head2 currentScript
1122  
1123 Under perl, this does not nor return do anything special, but you can set yourself an L<HTML::Object::DOM::Element>.
1124  
1125 Normally, under JavaScript, this returns the C<currentScript> property returns the <script> element whose script is currently being processed and is not a JavaScript module.
1126  
1127 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/Document/currentScript>
1128  
1129 =head2 defaultView
1130  
1131 This returns a reference to the L<window object|HTML::Object::DOM::Window>.
1132  
1133 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/Document/defaultView>
1134  
1135 =head2 designMode
1136  
1137 This always returns true under perl.
1138  
1139 Under the web, with JavaScript, this would get/set the ability to edit the whole document.
1140  
1141 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/Document/designMode>
1142  
1143 =head2 dir
1144  
1145 Get or set directionality (rtl/ltr) of the document.
1146  
1147 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/Document/dir>
1148  
1149 =head2 doctype
1150  
1151 This is read-only.
1152  
1153 Returns the Document Type Definition (DTD) L<object|HTML::Object::Element> of the current document.
1154  
1155 Example:
1156  
1157 my $doctypeObj = $doc->doctype;
1158  
1159 say(
1160 "doctype->name: " . $doctypeObj->name . "\n" +
1161 "doctype->internalSubset: " . $doctypeObj->internalSubset . "\n" +
1162 "doctype->publicId: " . $doctypeObj->publicId . "\n" +
1163 "doctype->systemId: " . $doctypeObj->systemId
1164 );
1165  
1166 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/Document/doctype>
1167  
1168 =head2 documentElement
1169  
1170 This is read-only.
1171  
1172 Returns the L<element|HTML::Object::DOM::Element> that is a direct child of the document. For HTML documents, this is normally the L<HTML Element|HTML::Object::Element> object representing the document's C<<html>> element.
1173  
1174 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/Document/documentElement>
1175  
1176 =head2 documentURI
1177  
1178 Set or get the document location as a string, if any.
1179  
1180 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/Document/documentURI>
1181  
1182 =head2 embeds
1183  
1184 This is read-only.
1185  
1186 Returns a list, as an L<html collection object|HTML::Object::DOM::Collection>, of the embedded <embed> elements within the current document.
1187  
1188 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/Document/embeds>
1189  
1190 =head2 featurePolicy
1191  
1192 Returns C<undef> since this is not relevant under perl.
1193  
1194 Normally, under JavaScript, this would return the FeaturePolicy interface which provides a simple API for introspecting the feature policies applied to a specific document.
1195  
1196 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/Document/featurePolicy>
1197  
1198 =head2 firstElementChild
1199  
1200 This is read-only.
1201  
1202 Returns the first child L<element|HTML::Object::DOM::Element> of the current document.
1203  
1204 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/Document/firstElementChild>
1205  
1206 =head2 fonts
1207  
1208 Returns C<undef> since this is not relevant under perl.
1209  
1210 Normally, it would return the C<FontFaceSet> interface of the current document.
1211  
1212 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/Document/fonts>
1213  
1214 =head2 forms
1215  
1216 This is read-only.
1217  
1218 Returns a list, as an L<html collection object|HTML::Object::DOM::Collection>, of the <form> elements within the current document.
1219  
1220 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/Document/forms>
1221  
1222 =head2 fullscreenElement
1223  
1224 Returns C<undef> since this is not relevant under perl.
1225  
1226 Normally, it would return the element that is currently in full screen mode for this document.
1227  
1228 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/Document/fullscreenElement>
1229  
1230 =head2 head
1231  
1232 This is read-only.
1233  
1234 Returns the L<<head> element|HTML::Object::DOM::Head> of the current document, or C<undef> if there is none.
1235  
1236 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/Document/head>
1237  
1238 =head2 hidden
1239  
1240 Returns C<undef> since this is not relevant under perl.
1241  
1242 Normally, it would return a boolean value indicating if the page is considered hidden or not.
1243  
1244 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/Document/hidden>
1245  
1246 =head2 images
1247  
1248 This is read-only.
1249  
1250 Returns a list, as an L<html collection object|HTML::Object::DOM::Collection>, of the images in the current document.
1251  
1252 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/Document/images>
1253  
1254 =head2 implementation
1255  
1256 This returns the L<DOM implementation object|HTML::Object::DOM::Implementation> associated with the current document.
1257  
1258 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/Document/implementation>
1259  
1260 =head2 lastElementChild
1261  
1262 This is read-only.
1263  
1264 Returns the last child L<element|HTML::Object::DOM::Element> of the current document, which is the root <html> element, the only child of the document.
1265  
1266 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/Document/lastElementChild>
1267  
1268 =head2 lastModified
1269  
1270 This is read-only.
1271  
1272 Returns the date on which the document was last modified, if any, as a L<DateTime> object.
1273  
1274 This value exists if the document was read from a file, and it would contain the file last modification time.
1275  
1276 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/Document/lastModified>
1277  
1278 =head2 links
1279  
1280 This is read-only.
1281  
1282 Returns a list, as an L<html collection object|HTML::Object::DOM::Collection>, of all the hyperlinks in the document.
1283  
1284 Example:
1285  
1286 my $links = $doc->$links;
1287 for( my $i = 0; $i < $links->length; $i++ )
1288 {
1289 my $linkHref = $doc->createTextNode( $links->[$i]->href );
1290 my $lineBreak = $doc->createElement("br");
1291 $doc->body->appendChild( $linkHref );
1292 $doc->body->appendChild( $lineBreak );
1293 }
1294  
1295 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/Document/links>
1296  
1297 =head2 location
1298  
1299 Set or get the URI of the current document. This is the same as L</documentURI>
1300  
1301 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/Document/location>
1302  
1303 =head2 mozSyntheticDocument
1304  
1305 Normally this is returns C<undef> under perl, byt you can set whatever boolean value you want.
1306  
1307 Under JavaScript, this returns a boolean that is true only if this document is synthetic, such as a standalone image, video, audio file, or the like.
1308  
1309 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/Document/mozSyntheticDocument>
1310  
1311 =head2 nodeValue
1312  
1313 This returns or sets the value of the current node.
1314  
1315 For document, element or collection, this returns C<undef> and for attribute, text or comment, this returns the objct value.
1316  
1317 See L<for more information|https://developer.mozilla.org/en-US/docs/Web/API/Node/nodeValue>
1318  
1319 =head2 ownerDocument
1320  
1321 Read-only. This always returns C<undef> since this is the top element. This is inherited from L<HTML::Object::DOM::Node>
1322  
1323 =head2 pictureInPictureElement
1324  
1325 Normally this returns C<undef> under perl, but you can set whatever L<element|HTML::Object::DOM::Element> object you want.
1326  
1327 Under JavaScript, this returns C<undef> since this is not relevant under perl.
1328  
1329 Normally, under JavaScript, this would return the Element currently being presented in picture-in-picture mode in this document.
1330  
1331 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/Document/pictureInPictureElement>
1332  
1333 =head2 pictureInPictureEnabled
1334  
1335 Normally this returns C<undef> under perl, but you can set whatever boolean value you want.
1336  
1337 Under JavaScript, this would return true if the picture-in-picture feature is enabled, and false otherwise.
1338  
1339 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/Document/pictureInPictureEnabled>
1340  
1341 =head2 plugins
1342  
1343 Returns always an empty L<collection object|HTML::Object::DOM::Collection> since this is not relevant under perl.
1344  
1345 Normally, under JavaScript, this would return a list, as a collection, of the available plugins.
1346  
1347 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/Document/plugins>
1348  
1349 =head2 pointerLockElement
1350  
1351 Normally this returns C<undef> under perl, but you can set whatever L<element|HTML::Object::DOM::Element> object you want.
1352  
1353 Under JavaScript, this would return the element set as the target for mouse events while the pointer is locked. null if lock is pending, pointer is unlocked, or if the target is in another document.
1354  
1355 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/Document/pointerLockElement>
1356  
1357 =head2 readyState
1358  
1359 This is read-only.
1360  
1361 Returns loading status of the document. This always returns a true value.
1362  
1363 The readyState of a document can be one of following:
1364  
1365 =over 4
1366  
1367 =item B<loading>
1368  
1369 The document is still loading.
1370  
1371 =item B<interactive>
1372  
1373 The document has finished loading and the document has been parsed.
1374  
1375 There is actually no distinction with the following C<complete> state.
1376  
1377 =item B<complete>
1378  
1379 The document and all its resources have finished loading. The state indicates that the load event is about to fire.
1380  
1381 This is actually the same state as C<interactive>
1382  
1383 =back
1384  
1385 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/Document/readyState>
1386  
1387 =head2 referrer
1388  
1389 Set or get the URI of the page that linked to this page.
1390  
1391 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/Document/referrer>
1392  
1393 =head2 scripts
1394  
1395 This is read-only.
1396  
1397 Returns all the L<<script> elements|HTML::Object::DOM::Element::Script> on the document, as a L<collection object|HTML::Object::DOM::Collection>.
1398  
1399 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/Document/scripts>
1400  
1401 =head2 scrollingElement
1402  
1403 Although this is meaningless under perl, you can set or get an L<element object|HTML::Object::DOM::Element> that scrolls the document.
1404  
1405 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/Document/scrollingElement>
1406  
1407 =head2 styleSheets
1408  
1409 This is read-only.
1410  
1411 Returns a list, as an L<array object|Module::Generic::Array>, of L<CSS StyleSheet element objects|HTML::Object::Element> for stylesheets explicitly linked into, or embedded in a document.
1412  
1413 Contrary to the original JavaScript equivalent, this does not return C<CSSStyleSheet> objects, because it would be potentially heavy to parse each css in C<link> and C<style> tags. You could do this yourself by using L<CSS::Object> and looping through each L<element object|HTML::Object::Element> returned. For example:
1414  
1415 $doc->styleSheets->foreach(sub
1416 {
1417 my $e = shift( @_ );
1418 if( $e->tag eq 'link' )
1419 {
1420 my $resp = $ua->get( $e->attr( 'href' ) );
1421 die( $resp->message ) if( $resp->is_error );
1422 my $style = $resp->decoded_content;
1423 my $css = CSS::Object->new;
1424 $css->read_string( $style );
1425 $css->rules->foreach(sub
1426 {
1427 my $rule = shift( @_ );
1428 # more processing
1429 });
1430 }
1431 elsif( $e->tag eq 'style' )
1432 {
1433 my $css = CSS::Object->new;
1434 $css->read_string( $e->text );
1435 $css->rules->foreach(sub
1436 {
1437 my $rule = shift( @_ );
1438 # more processing
1439 });
1440 }
1441 });
1442  
1443 or you can use L<HTML::Object::XQuery/find> with a xpath selector, such as:
1444  
1445 use HTML::Object::XQuery; # Load jQuery style query functions
1446 # $doc is the HTML::Object::Document returned by HTML::Object->parse
1447 my $collection = $doc->findNode( 'link[rel="https://example.org/css/main.css"]' ) ||
1448 die( $doc->error );
1449 $collection->children->foreach(sub
1450 {
1451 # more processing
1452 });
1453  
1454 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/Document/styleSheets>
1455  
1456 =head2 timeline
1457  
1458 Returns C<undef> since this is not relevant under perl.
1459  
1460 Normally, under JavaScript, this would return timeline as a special instance of DocumentTimeline that is automatically created on page load.
1461  
1462 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/Document/timeline>
1463  
1464 =head2 title
1465  
1466 Set or get the title of the current document.
1467  
1468 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/Document/title>
1469  
1470 =head2 URL
1471  
1472 Set or get the document location as a string.
1473  
1474 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/Document/URL>
1475  
1476 =head2 visibilityState
1477  
1478 Normally this returns C<undef> under perl, but you can set whatever string value you want. This returns a L<scalar object|Module::Generic::Scalar>
1479  
1480 Under JavaScript, this would return a string denoting the visibility state of the document. Possible values are visible, hidden, prerender, and unloaded.
1481  
1482 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/Document/visibilityState>
1483  
1484 =head1 METHODS
1485  
1486 =head2 adoptNode
1487  
1488 L</adoptNode> transfers a L<node|HTML::Object::Element> from another document into the method's L<document|HTML::Object::Document>. The adopted L<node|HTML::Object::Element> and its subtree is removed from its original document (if any), and its ownerDocument is changed to the current document. The L<node|HTML::Object::Element> can then be inserted into the current L<document|HTML::Object::Document>.
1489  
1490 Before they can be inserted into the current document, nodes from external documents should either be:
1491  
1492 =over 4
1493  
1494 =item * cloned using L</importNode>; or
1495  
1496 =item * adopted using L</adoptNode>.
1497  
1498 =back
1499  
1500 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/Document/adoptNode>
1501  
1502 =head2 append
1503  
1504 Inserts a set of Node objects or string objects after the last child of the document.
1505  
1506 This is inherited from L<HTML::Object::DOM::Element>
1507  
1508 Example:
1509  
1510 my $html = $doc->createElement( 'html' );
1511 $doc->append( $html );
1512 # HierarchyRequestError: The operation would yield an incorrect node tree.
1513  
1514 # Also
1515  
1516 my $doc = HTML::Object::DOM::Document->new;
1517 my $html = $doc->createElement( 'html');
1518 $doc->append( $html );
1519  
1520 $doc->children; # HTMLCollection [<html>]
1521  
1522 =head2 appendChild
1523  
1524 Provided with one or more nodes and this will add them to the list of this document's children.
1525  
1526 =head2 as_string
1527  
1528 Returns a string representation of this document and all its hierarchy.
1529  
1530 =head2 captureEvents
1531  
1532 See L<HTML::Object::DOM::Window/captureEvents>.
1533  
1534 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/Document/captureEvents>
1535  
1536 =head2 caretRangeFromPoint
1537  
1538 Always returns C<undef> under perl.
1539  
1540 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/Document/caretRangeFromPoint>
1541  
1542 =head2 caretPositionFromPoint
1543  
1544 Returns C<undef> since this is not relevant under perl.
1545  
1546 Normally, under JavaScript, this would return a CaretPosition object containing the DOM node containing the caret, and caret's character offset within that node.
1547  
1548 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/Document/caretPositionFromPoint>
1549  
1550 =head2 childNodes
1551  
1552 Returns an L<array object|Module::Generic::Array> of the document child nodes.
1553  
1554 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/Node/childNodes>
1555  
1556 =head2 close
1557  
1558 The C<close()> method finishes writing to a document, opened with L</open>.
1559  
1560 Example:
1561  
1562 # Open a document to write to it
1563 $doc->open();
1564  
1565 # Write the content of the document
1566 $doc->write("<p>The one and only content.</p>");
1567  
1568 # Close the document
1569 $doc->close();
1570  
1571 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/Document/close>
1572  
1573 =head2 contentType
1574  
1575 =head2 createAttribute
1576  
1577 Provided with an argument and this creates a new L<attribute object|HTML::Object::Attribute> and returns it.
1578  
1579 The attribute name is converted to lowercase.
1580  
1581 Example:
1582  
1583 my $node = $doc->getElementById( 'div1' );
1584 my $a = $doc->createAttribute( 'my_attrib' );
1585 $a->value = 'newVal';
1586 $node->setAttributeNode( $a );
1587 say( $node->getAttribute( 'my_attrib' ) ); # "newVal"
1588  
1589 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/Document/createAttribute>
1590  
1591 =head2 createAttributeNS
1592  
1593 Returns C<undef> since this is not relevant under perl.
1594  
1595 Normally, under JavaScript, this would create a new attribute node in a given namespace and returns it.
1596  
1597 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/Document/createAttributeNS>
1598  
1599 =head2 createCDATASection
1600  
1601 Returns C<undef> since this is not relevant under perl.
1602  
1603 Normally, under JavaScript, this would create a new CDATA node and returns it.
1604  
1605 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/Document/createCDATASection>
1606  
1607 =head2 createComment
1608  
1609 Creates a new comment node and returns it.
1610  
1611 Example:
1612  
1613 my $comment = $doc->createComment( 'This is a not-so-secret comment in your document' );
1614 $doc->getElementsByTagName( 'div' )->[0]->appendChild( $comment );
1615 say( $doc->as_string );
1616 # <div><!--This is a not-so-secret $comment in your document--></div>
1617  
1618 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/Document/createComment>
1619  
1620 =head2 createDocumentFragment
1621  
1622 This create a new L<HTML::Object::DOM::DocumentFragment> object, passing its C<new()> constructor whatever argument was provided.
1623  
1624 It returns the newly instantiated object.
1625  
1626 L<Document fragments|HTML::Object::DOM::DocumentFragment> are L<DOM Node|HTML::Object::DOM::Node> objects which are never part of the main L<DOM tree|HTML::Object::DOM::Document>. The usual use case is to create the document fragment, append elements to the document fragment and then append the document fragment to the L<DOM tree|HTML::Object::DOM::Document>. In the L<DOM tree|HTML::Object::DOM::Document>, the document fragment is replaced by all its children.
1627  
1628 Since the L<document fragment|HTML::Object::DOM::DocumentFragment> is not part of the main L<DOM tree|HTML::Object::DOM::Document>, appending children to it does not affect the main tree.
1629  
1630 Example:
1631  
1632 <ul id="ul"></ul>
1633  
1634 use Module::Generic::Array;
1635 my $element = $doc->getElementById('ul'); # assuming ul exists
1636 my $fragment = $doc->createDocumentFragment();
1637 my $browsers = Module::Generic::Array->new( ['Firefox', 'Chrome', 'Opera',
1638 'Safari', 'Internet Explorer'] );
1639  
1640 $browsers->foreach(sub
1641 {
1642 my $browser = shift( @_ );
1643 my $li = $doc->createElement('li');
1644 $li->textContent = $browser;
1645 $fragment->appendChild( $li );
1646 });
1647  
1648 $element->appendChild( $fragment );
1649  
1650 would yield:
1651  
1652 =over 4
1653  
1654 =item * Firefox
1655  
1656 =item * Chrome
1657  
1658 =item * Opera
1659  
1660 =item * Safari
1661  
1662 =item * Internet Explorer
1663  
1664 =back
1665  
1666 See L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/Document/createDocumentFragment>
1667  
1668 =head2 createElement
1669  
1670 Provided with a tag name and this creates a new L<HTML::Object::Element> object that is returned.
1671  
1672 This methods sets an L<error|Module::Generic/error> and returns C<undef> upon error.
1673  
1674 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/Document/createElement>
1675  
1676 =head2 createElementNS
1677  
1678 Returns C<undef> since this is not relevant under perl.
1679  
1680 Normally, under JavaScript, this would create a new element with the given tag name and namespace URI.
1681  
1682 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/Document/createElementNS>
1683  
1684 =head2 createEntityReference
1685  
1686 Returns C<undef> since this is not relevant for HTML document. This is used for XML.
1687  
1688 This was used to create a new entity reference object and returns it.
1689  
1690 Example:
1691  
1692 $doc->createEntityReference( '&amp' ); # &
1693  
1694 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/Document/createEntityReference>
1695  
1696 =head2 createEvent
1697  
1698 Creates an event object, passing it whatever arguments were provided, and returns it.
1699  
1700 Example:
1701  
1702 my $event = $doc->createEvent( $type );
1703  
1704 # Create the $event.
1705 my $event = $doc->createEvent( 'click' );
1706  
1707 # Define that the event name is 'build'.
1708 $event->initEvent( build => { bubbles => 1 });
1709  
1710 # Listen for the $event.
1711 $elem->addEventListener( build => sub
1712 {
1713 # e->target matches elem
1714 }, { capture => 0 });
1715  
1716 # Target can be any Element or other EventTarget.
1717 $elem->dispatchEvent( $event );
1718  
1719 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/Document/createEvent>
1720  
1721 =head2 createExpression
1722  
1723 Compiles an L<HTML::Object::XPath::Expr> which can then be used for (repeated) evaluations. It returns a L<HTML::Object::DOM::XPathResult> object.
1724  
1725 Example:
1726  
1727 my $xpathExpr = $doc->createExpression( $xpathText );
1728 my $result = $xpathExpr->evaluate( $contextNode );
1729  
1730 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/Document/createExpression>
1731  
1732 =head2 createNSResolver
1733  
1734 This always returns C<undef> as XML is not used in L<HTML::Object>
1735  
1736 Normally, under JavaScript, this creates an C<XPathNSResolver> object.
1737  
1738 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/Document/createNSResolver>
1739  
1740 =head2 createNodeIterator
1741  
1742 Creates a L<HTML::Object::DOM::NodeIterator> object.
1743  
1744 Example:
1745  
1746 use Module::Generic::Array;
1747 my $nodeIterator = $doc->createNodeIterator(
1748 $doc->body,
1749 NodeFilter->SHOW_ELEMENT,
1750 {
1751 sub acceptNode
1752 {
1753 my $node = shift( @_ );
1754 return( $node->nodeName->toLowerCase() == 'p' ? NodeFilter->FILTER_ACCEPT : NodeFilter->FILTER_REJECT;
1755 }
1756 }
1757 );
1758 my $pars = Module::Generic::Array->new;
1759 my $currentNode;
1760  
1761 while( $currentNode = $nodeIterator->nextNode() )
1762 {
1763 $pars->push( $currentNode );
1764 }
1765  
1766 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/Document/createNodeIterator>
1767  
1768 =head2 createProcessingInstruction
1769  
1770 Creates a new C<ProcessingInstruction> object.
1771  
1772 Example:
1773  
1774 my $doc = HTML::Object::DOM->new->parseFromString('<foo />', 'application/xml');
1775 my $pi = $doc->createProcessingInstruction('xml-stylesheet', 'href="mycss->css" type="text/css"');
1776  
1777 $doc->insertBefore($pi, $doc->firstChild);
1778  
1779 say( $doc->as_string );
1780 # Displays: <?xml-stylesheet href="mycss->css" type="text/css"?><foo/>
1781  
1782 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/Document/createProcessingInstruction>
1783  
1784 =head2 createRange
1785  
1786 This always returns C<undef> under perl.
1787  
1788 Normally, under JavaScript, this creates a C<Range> object.
1789  
1790 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/Document/createRange>
1791  
1792 =head2 createTextNode
1793  
1794 Provided with a text, either as a string, or as a list of strings, and this creates a text node.
1795  
1796 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/Document/createTextNode>
1797  
1798 =head2 createTouch
1799  
1800 This always returns C<undef> under perl.
1801  
1802 Normally, under JavaScript, this creates a Touch object.
1803  
1804 Also, this feature is deprecated.
1805  
1806 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/Document/createTouch>
1807  
1808 =head2 createTouchList
1809  
1810 This always returns C<undef> under perl.
1811  
1812 Normally, under JavaScript, this creates a C<TouchList> object.
1813  
1814 Also, this feature is deprecated.
1815  
1816 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/Document/createTouchList>
1817  
1818 =head2 createTreeWalker
1819  
1820 Creates a C<TreeWalker> object.
1821  
1822 Example:
1823  
1824 use HTML::Object::DOM::NodeFilter qw( :all );
1825 my $treeWalker = $doc->createTreeWalker(
1826 $doc->body,
1827 SHOW_ELEMENT,
1828 sub{ return( FILTER_ACCEPT ); },
1829 );
1830  
1831 my $nodeList = Module::Generic::Array->new;
1832 my $currentNode = $treeWalker->currentNode;
1833  
1834 while( $currentNode )
1835 {
1836 $nodeList->push( $currentNode );
1837 $currentNode = $treeWalker->nextNode();
1838 }
1839  
1840 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/Document/createTreeWalker>
1841  
1842 =head2 elementFromPoint
1843  
1844 This always returns C<undef> under perl.
1845  
1846 Normally, under JavaScript, this returns the topmost element at the specified coordinates.
1847  
1848 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/Document/elementFromPoint>
1849  
1850 =head2 elementsFromPoint
1851  
1852 This always returns C<undef> under perl.
1853  
1854 Normally, under JavaScript, this returns an array of all elements at the specified coordinates.
1855  
1856 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/Document/elementsFromPoint>
1857  
1858 =head2 enableStyleSheetsForSet
1859  
1860 This always returns C<undef> under perl.
1861  
1862 Normally, under JavaScript, this enables the style sheets for the specified style sheet set.
1863  
1864 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/Document/enableStyleSheetsForSet>
1865  
1866 =head2 evaluate
1867  
1868 Evaluates an L<XPath|HTML::Object::XPath> expression.
1869  
1870 Example:
1871  
1872 my $xpathResult = $doc->evaluate(
1873 $xpathExpression,
1874 $contextNode
1875 );
1876  
1877 my $headings = $doc->evaluate( "/html/body//h2", $document );
1878 # Search the document for all h2 elements.
1879 # The result will likely be an unordered node iterator.
1880 my $thisHeading = $headings->iterateNext();
1881 my $alertText = "Level 2 $headings in this document are:\n";
1882 while( $thisHeading )
1883 {
1884 $alertText .= $thisHeading->textContent . "\n";
1885 $thisHeading = $headings->iterateNext();
1886 }
1887 say( $alertText ); # print the text of all h2 elements
1888  
1889 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/Document/evaluate>
1890  
1891 =head2 execCommand
1892  
1893 This does absolutely nothing and always returns C<undef>.
1894  
1895 This is actually a deprecated feature of the web API.
1896  
1897 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/Document/execCommand>
1898  
1899 =head2 exitPictureInPicture
1900  
1901 This always returns C<undef> under perl.
1902  
1903 Normally, under JavaScript, this removes the video from the floating picture-in-picture window back to its original container.
1904  
1905 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/Document/exitPictureInPicture>
1906  
1907 =head2 exitPointerLock
1908  
1909 This always returns C<undef> under perl.
1910  
1911 Normally, under JavaScript, this releases the pointer lock.
1912  
1913 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/Document/exitPointerLock>
1914  
1915 =head2 firstChild
1916  
1917 Returns the first child from the document list of nodes.
1918  
1919 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/Node/firstChild>
1920  
1921 =head2 firstElementChild
1922  
1923 =head2 getElementById
1924  
1925 Provided with an element C<id>, and this method returns the corresponding L<element object|HTML::Object::Element>.
1926  
1927 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/Document/getElementById>
1928  
1929 =head2 getAnimations
1930  
1931 This always returns C<undef> under perl.
1932  
1933 Normally, under JavaScript, this returns an array of all Animation objects currently in effect, whose target elements are descendants of the document.
1934  
1935 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/Document/getAnimations>
1936  
1937 =head2 getBoxQuads
1938  
1939 This always returns C<undef> under perl.
1940  
1941 Normally, under JavaScript, this returns a list of C<DOMQuad> objects representing the CSS fragments of the node.
1942  
1943 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/Document/getBoxQuads>
1944  
1945 =head2 getElementById
1946  
1947 Provided with a string and this returns an element object whose C<id> attribute matches the string specified.
1948  
1949 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/Document/getElementById>
1950  
1951 =head2 getElementsByClassName
1952  
1953 Returns a list of elements with the given class name.
1954  
1955 Example:
1956  
1957 <span class="orange fruit">Orange Fruit</span>
1958 <span class="orange juice">Orange Juice</span>
1959 <span class="apple juice">Apple Juice</span>
1960 <span class="foo bar">Something Random</span>
1961 <textarea id="resultArea" style="width:98%;height:7em"></textarea>
1962  
1963 Another example:
1964  
1965 # getElementsByClassName only selects elements that have both given classes
1966 my $allOrangeJuiceByClass = $doc->getElementsByClassName('orange juice');
1967 my $result = "$doc->getElementsByClassName('orange juice')";
1968 for( my $i=0; $i < $allOrangeJuiceByClass->length; $i++ )
1969 {
1970 $result .= "\n " . $allOrangeJuiceByClass->[$i]->textContent;
1971 }
1972  
1973 # querySelector only selects full complete matches
1974 my $allOrangeJuiceQuery = $doc->querySelectorAll('.orange->juice');
1975 $result += "\n\ndocument->querySelectorAll('.orange->juice')";
1976 for( my $i=0; $i < $allOrangeJuiceQuery->length; $i++ )
1977 {
1978 $result .= "\n " . $allOrangeJuiceQuery->[$i]->textContent;
1979 }
1980  
1981 $doc->getElementById( 'resultArea' )->value = $result;
1982  
1983 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/Document/getElementsByClassName>
1984  
1985 =head2 getElementsByName
1986  
1987 Returns a L<NodeList|HTML::Object::DOM::NodeList> Collection of elements with a given name attribute in the L<document|HTML::Object::DOM::Element>.
1988  
1989 Example:
1990  
1991 <!DOCTYPE html>
1992 <html lang="en">
1993 <head>
1994 <title>Example: using getElementsByName</title>
1995 </head>
1996 <body>
1997 <input type="hidden" name="up" />
1998 <input type="hidden" name="down" />
1999 </body>
2000 </html>
2001  
2002 my $up_names = $doc->getElementsByName( 'up' );
2003 say( $up_names->[0]->tagName ); # displays "input"
2004  
2005 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/Document/getElementsByName>
2006  
2007 =head2 getElementsByTagName
2008  
2009 Returns a L<list|HTML::Object::DOM::Collection> of elements with the given tag name.
2010  
2011 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/Document/getElementsByTagName>
2012  
2013 =head2 getElementsByTagNameNS
2014  
2015 This is merely an alias to L</getElementsByTagName> since there is no support for namespace.
2016  
2017 Normally, under JavaScript, this returns a list of elements with the given tag name and namespace.
2018  
2019 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/Document/getElementsByTagNameNS>
2020  
2021 =head2 getRootNode
2022  
2023 Returns the current object.
2024  
2025 =head2 getSelection
2026  
2027 This always returns C<undef> under perl.
2028  
2029 Normally, under JavaScript, this returns a C<Selection> object representing the range of text selected by the user, or the current position of the caret.
2030  
2031 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/Document/getSelection>
2032  
2033 =head2 hasChildNodes
2034  
2035 Returns true if the document list of nodes is not empty, false otherwise.
2036  
2037 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/Node/hasChildNodes>
2038  
2039 =head2 hasFocus
2040  
2041 Under perl, this always returns true.
2042  
2043 Normally, under JavaScript, this returns a boolean value indicating whether the document or any element inside the document has focus. This method can be used to determine whether the active element in a document has focus.
2044  
2045 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/Document/hasFocus>
2046  
2047 =head2 hasStorageAccess
2048  
2049 This always returns C<undef> under perl.
2050  
2051 Normally, under JavaScript, this returns a C<Promise> that resolves with a boolean value indicating whether the document has access to its first-party storage.
2052  
2053 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/Document/hasStorageAccess>
2054  
2055 =head2 importNode
2056  
2057 Returns a clone of a node from an external document.
2058  
2059 Example:
2060  
2061 my $iframe = $doc->querySelector( 'iframe' );
2062 my $oldNode = $iframe->contentWindow->document->getElementById( 'myNode' );
2063 my $newNode = $doc->importNode( $oldNode, $true );
2064 $doc->getElementById( 'container' )->appendChild( $newNode );
2065  
2066 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/Document/importNode>
2067  
2068 =head2 insertAfter
2069  
2070 Provided with a node and a reference node and this will insert the node after the reference node.
2071  
2072 It returns the current object upon success, or C<undef> upon L<error|Module::Generic/error>
2073  
2074 Surprisingly enough there is no C<insertAfter> method in the web API, only L</insertBefore>
2075  
2076 =head2 insertBefore
2077  
2078 Provided with a node and a reference node and this will insert the node before the reference node.
2079  
2080 It returns the current object upon success, or C<undef> upon L<error|Module::Generic/error>
2081  
2082 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/Node/insertBefore>
2083  
2084 =head2 lastChild
2085  
2086 Returns the last child node from the document list of nodes.
2087  
2088 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/Node/lastChild>
2089  
2090 =head2 mozSetImageElement
2091  
2092 This always returns C<undef> under perl.
2093  
2094 Normally, under JavaScript, this allows you to change the element being used as the background image for a specified element ID.
2095  
2096 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/Document/mozSetImageElement>
2097  
2098 =head2 nextSibling
2099  
2100 Returns a L<smart undef|Module::Generic/new_null>. This means the return value will depend on what the caller expects. In scalar context it will return C<undef> and an empty list in list context, but if this method is chained, it will return a dummy object to avoid the perl error "method called on an undefined value"
2101  
2102 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/Node/nextSibling>
2103  
2104 =head2 normalizeDocument
2105  
2106 Replaces entities, normalizes text nodes, etc.
2107  
2108 This is actually an alias to L<HTML::Object::DOM::Node/normalize>
2109  
2110 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/Document/normalizeDocument>
2111  
2112 =head2 open
2113  
2114 The C<open()> method opens a document for writing and returns the current L<document|HTML::Object::DOM::Document> object.
2115  
2116 This does come with some side effects. For example:
2117  
2118 =over 4
2119  
2120 =item * All event listeners currently registered on the L<document|HTML::Object::DOM::Document>, L<nodes|HTML::Object::DOM::Node> inside the L<document|HTML::Object::DOM::Document>, or the document's L<window|HTML::Object::DOM::Window> are removed.
2121  
2122 =item * All existing L<nodes|HTML::Object::DOM::Node> are removed from the L<document|HTML::Object::DOM::Document>.
2123  
2124 =back
2125  
2126 Example:
2127  
2128 The following simple code opens the document and replaces its content with a number of different HTML fragments, before closing it again.
2129  
2130 my $doc = $parser->parse_data( $html );
2131 $doc->open();
2132 $doc->write("<p>Hello world!</p>");
2133 $doc->write("<p>I am a fish</p>");
2134 $doc->write("<p>The number is 42</p>");
2135 $doc->close();
2136  
2137 An automatic L</open> call happens when L</write> is called after the document has loaded.
2138  
2139 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/Document/open>
2140  
2141 =head2 parentElement
2142  
2143 This interface returns the DOM node's parent Element, or C<undef> if the node either has no parent, or its parent isn't a DOM Element.
2144  
2145 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/Node/parentElement>
2146  
2147 =head2 parentNode
2148  
2149 This returns the parent of the specified node in the DOM tree.
2150  
2151 Document and L<DocumentFragment|HTML::Object::DOM::DocumentFragment> nodes can never have a parent, so C<parentNode> will always return C<undef>.
2152  
2153 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/Node/parentNode>
2154  
2155 =head2 prepend
2156  
2157 Inserts a set of L<Node|HTML::Object::DOM::Node> objects or string objects before the first child of the document.
2158  
2159 Example:
2160  
2161 my $html = $doc->createElement( 'html' );
2162 $doc->prepend( $html );
2163 # HierarchyRequestError: The operation would yield an incorrect node tree.
2164  
2165 Another example:
2166  
2167 my $doc = HTML::Object::DOM::Document->new;
2168 my $html = $doc->createElement( 'html' );
2169 $doc->prepend( $html );
2170  
2171 $doc->children; # HTMLCollection [<$html>]
2172  
2173 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/Document/prepend>
2174  
2175 =head2 previousSibling
2176  
2177 This returns the node immediately preceding the specified one in its parent's childNodes list, or C<undef> if the specified node is the first in that list.
2178  
2179 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/Node/previousSibling>
2180  
2181 =head2 queryCommandEnabled
2182  
2183 This always returns false under perl.
2184  
2185 Normally, under JavaScript, this reports whether or not the specified editor command is enabled by the browser.
2186  
2187 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/Document/queryCommandEnabled>
2188  
2189 =head2 queryCommandIndeterm
2190  
2191 This always returns true under perl.
2192  
2193 Normally, under JavaScript, this returns true if the formatting command is in an indeterminate state on the current range.
2194  
2195 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/Document/queryCommandIndeterm>
2196  
2197 =head2 queryCommandState
2198  
2199 This always returns false under perl.
2200  
2201 Normally, under JavaScript, this returns true if the formatting command has been executed on the current range.
2202  
2203 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/Document/queryCommandState>
2204  
2205 =head2 queryCommandSupported
2206  
2207 This always returns false under perl.
2208  
2209 Normally, under JavaScript, this returns true if the formatting command is supported on the current range.
2210  
2211 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/Document/queryCommandSupported>
2212  
2213 =head2 queryCommandValue
2214  
2215 This always returns C<undef> under perl.
2216  
2217 Normally, under JavaScript, this returns the current value of the current range for a formatting command.
2218  
2219 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/Document/queryCommandValue>
2220  
2221 =head2 querySelector
2222  
2223 Returns the first Element node within the document, in document order, that matches the specified selectors.
2224  
2225 Example:
2226  
2227 <div id="foo\bar"></div>
2228 <div id="foo:bar"></div>
2229  
2230 $doc->querySelector( '#foo\\bar' ); # Match the first div
2231 $doc->querySelector( '#foo\:bar' ); # Match the second div
2232  
2233 my $el = $doc->querySelector(".myclass");
2234  
2235 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelector>
2236  
2237 =head2 querySelectorAll
2238  
2239 Returns a list of all the Element nodes within the document that match the specified selectors.
2240  
2241 Example:
2242  
2243 <div class="outer">
2244 <div class="select">
2245 <div class="inner">
2246 </div>
2247 </div>
2248 </div>
2249  
2250 my $inner = $select->querySelectorAll( '.outer .inner' );
2251 $inner->length; # 1, not 0
2252  
2253 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelectorAll>
2254  
2255 =head2 releaseCapture
2256  
2257 Releases the current mouse capture if it's on an element in this document.
2258  
2259 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/Document/releaseCapture>
2260  
2261 =head2 releaseEvents
2262  
2263 See L<HTML::Object::DOM::Window/releaseEvents>.
2264  
2265 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/Document/releaseEvents>
2266  
2267 =head2 removeChild
2268  
2269 Provided with a node and this removes a child node from the DOM and returns the removed node.
2270  
2271 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/Node/removeChild>
2272  
2273 =head2 replaceChild
2274  
2275 Provided with a C<new> node and and an C<old> node and this will replace the C<old> one by the C<new> one. Note that if the C<new> node is already present somewhere else in the C<DOM>, it is first removed from that position.
2276  
2277 This returns the C<old> node removed.
2278  
2279 See the document in L<HTML::Object::DOM::Node/replaceChild> for more details.
2280  
2281 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/Node/replaceChild>
2282  
2283 =head2 replaceChildren
2284  
2285 Replaces the existing children of a document with a specified new set of children.
2286  
2287 Example:
2288  
2289 $doc->replaceChildren();
2290 $doc->children; # HTMLCollection []
2291  
2292 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/Document/replaceChildren>
2293  
2294 =head2 requestStorageAccess
2295  
2296 This always returns C<undef> under perl.
2297  
2298 Normally, under JavaScript, this returns a C<Promise> that resolves if the access to first-party storage was granted, and rejects if access was denied.
2299  
2300 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/Document/requestStorageAccess>
2301  
2302 =head2 string_value
2303  
2304 This always return C<undef>.
2305  
2306 =head2 write
2307  
2308 Provided with a list of nodes and this will write them to the body of this document. If the document is already closed, such as when it has already loaded, this will call L</open>, which will cause to empty the document.
2309  
2310 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/Document/write>
2311  
2312 =head2 writeln
2313  
2314 Same as with L</write>, except this will add a new line.
2315  
2316 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/Document/writeln>
2317  
2318 =head1 EVENTS
2319  
2320 There is only limited support for events under perl, but you can trigger yourself any event.
2321  
2322 Event listeners for those events can also be found by prepending C<on> before the event type:
2323  
2324 C<click> event listeners can be set also with C<onclick> method:
2325  
2326 $e->onclick(sub{ # do something });
2327 # or as an lvalue method
2328 $e->onclick = sub{ # do something };
2329  
2330 Below are some key ones, but you can see the full lists on the L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/Document#events>
2331  
2332 =head2 DOMContentLoaded
2333  
2334 Fired when the document has been completely loaded and parsed, without waiting for stylesheets, images, and subframes to finish loading.
2335  
2336 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/Document/DOMContentLoaded_event>
2337  
2338 =head2 readystatechange
2339  
2340 Fired when the readyState attribute of a document has changed.
2341  
2342 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/Document/readystatechange_event>
2343  
2344 =head2 visibilitychange
2345  
2346 Fired when the content of a tab has become visible or has been hidden. Also available via the onvisibilitychange property.
2347  
2348 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/Document/visibilitychange_event>
2349  
2350 =head1 EVENT HANDLERS
2351  
2352 Listen to these events using L<HTML::Object::EventTarget/addEventListener> or by assigning an event listener to the C<oneventname> property of this interface.
2353  
2354 Under perl, few events are actually "fired" by L<HTML::Object::DOM::Document> and L<for the others|https://developer.mozilla.org/en-US/docs/Web/API/Element#events>, nothing prevents you from L<triggering|HTML::Object::EventTarget/dispatchEvent> whatever events you want on any L<element|HTML::Object::DOM::Element>, even private non-standard ones, and set up listeners for them.
2355  
2356 Below are the ones actually "fired" by L<HTML::Object>.
2357  
2358 =head2 onabort
2359  
2360 This takes a code reference (a reference to a subroutine or an anonymous subroutine) as its unique argument.
2361  
2362 The C<onabort> property is the L<event|HTML::Object::Event> handler for processing C<abort> events sent to the perl script.
2363  
2364 It returns C<undef> and sets an C<HTML::Object::TypeError> error if the value provided is not a code reference, i.e. a reference to an existing subroutine or an anonymous subroutine.
2365  
2366 It returns true upon success.
2367  
2368 Upon an abort signal (C<ABRT>, C<INT> or C<TERM>), this will execute the code reference, passing it an L<error event object|HTML::Object::ErrorEvent>
2369  
2370 Example:
2371  
2372 use HTML::Object::DOM;
2373 my $p = HTML::Object::DOM->new;
2374 $doc = $p->parse_data( $some_html_string );
2375 $doc->onabort = sub
2376 {
2377 my $event = shift( @_ );
2378 print( "Oh no: ", $event->trace, "\n" );
2379 };
2380 # or
2381 $doc->onabort = \&exception_handler;
2382  
2383 =head2 onafterscriptexecute
2384  
2385 Under perl, no such event is fired, but you can trigger one yourself.
2386  
2387 This property references a function that fires when a static <script> element finishes executing its script. It does not fire if the element is added dynamically, such as with appendChild().
2388  
2389 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/Document/onafterscriptexecute>
2390  
2391 =head2 onbeforescriptexecute
2392  
2393 Under perl, no such event is fired, but you can trigger one yourself.
2394  
2395 This is fired when the code in a <script> element declared in an HTML document is about to start executing. Does not fire if the element is added dynamically, eg with appendChild().
2396  
2397 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/Document/onbeforescriptexecute>
2398  
2399 =head2 oncopy
2400  
2401 Under perl, no such event is fired, but you can trigger one yourself.
2402  
2403 This represents the event handling code for the copy event.
2404  
2405 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/Document/oncopy>
2406  
2407 =head2 oncut
2408  
2409 Under perl, no such event is fired, but you can trigger one yourself.
2410  
2411 This represents the event handling code for the cut event.
2412  
2413 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/Document/oncut>
2414  
2415 =head2 onerror
2416  
2417 The C<onerror> property is an L<event|HTML::Object::Event> handler that processes error events triggered by a L<perlfunc/warn> or L<perlfunc/die>
2418  
2419 Upon an error signal (C<__WARN__>, or C<__DIE__>), this will execute the code reference, passing it an L<error event object|HTML::Object::ErrorEvent>
2420  
2421 Example:
2422  
2423 use HTML::Object::DOM;
2424 my $p = HTML::Object::DOM->new;
2425 $doc = $p->parse_data( $some_html_string );
2426 $doc->onerror = sub
2427 {
2428 my $event = shift( @_ );
2429 print( "Oh no: ", $event->trace, "\n" );
2430 };
2431 # or
2432 $doc->onerror = \&exception_handler;
2433  
2434 =head2 onfullscreenchange
2435  
2436 Under perl, no such event is fired, but you can trigger one yourself.
2437  
2438 This property is an event handler for the fullscreenchange event that is fired immediately before a document transitions into or out of full-screen mode.
2439  
2440 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/Document/onfullscreenchange>
2441  
2442 =head2 onfullscreenerror
2443  
2444 Under perl, no such event is fired, but you can trigger one yourself.
2445  
2446 This property is an event handler for the fullscreenerror event that is sent to the document when it fails to transition into full-screen mode after a prior call to Element.requestFullscreen().
2447  
2448 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/Document/onfullscreenerror>
2449  
2450 =head2 onload
2451  
2452 The C<onload> property is an event handler for the C<load> event that fires when the initial HTML L<document|HTML::Object::DOM::Document> has been completely loaded and parsed. It is fired after the L</readyState> has changed to C<complete>
2453  
2454 Contrary to the JavaScript environment, under perl, there is obviously no window and thus no difference between the JavaScript L<window load|https://developer.mozilla.org/en-US/docs/Web/API/Window/load_event> event and the L<DOMContentLoaded|https://developer.mozilla.org/en-US/docs/Web/API/Document/DOMContentLoaded_event> event
2455  
2456 For example:
2457  
2458 $doc->addEventListener( load => sub
2459 {
2460 say( 'Document fully loaded and parsed' );
2461 });
2462  
2463 sub doSomething
2464 {
2465 say( 'Document loaded' );
2466 }
2467  
2468 # Loading has not finished yet
2469 if( $doc->readyState eq 'loading' )
2470 {
2471 $doc->addEventListener( load => \&doSomething );
2472 }
2473 # 'load' has already fired
2474 else
2475 {
2476 doSomething();
2477 }
2478  
2479 Upon execution, a new L<event|HTML::Object::Event> is passed of type C<readstate> and with the C<detail> property having the following data available:
2480  
2481 =over 4
2482  
2483 =item document
2484  
2485 The L<document object|HTML::Object::DOM::Document>
2486  
2487 =item state
2488  
2489 The state of the document parsing.
2490  
2491 =back
2492  
2493 The event C<target> property is also set to the L<document object|HTML::Object::DOM::Document>.
2494  
2495 =head2 onpaste
2496  
2497 Under perl, no such event is fired, but you can trigger one yourself.
2498  
2499 This property interface is an event handler that processes paste events.
2500  
2501 The paste event fires when the user attempts to paste text.
2502  
2503 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/Document/onpaste>
2504  
2505 =head2 onreadystatechange
2506  
2507 The C<onreadystatechange> property is an event handler for the C<readystatechange> event that is fired when the L</readyState> attribute of a L<document|HTML::Object::DOM::Document> has changed.
2508  
2509 There are 3 state: C<loading>, C<interactive> and C<complete> (which is the same, under perl, as C<loading>)
2510  
2511 This event does not bubble and is not cancelable
2512  
2513 Upon execution, a new L<event|HTML::Object::Event> is passed of type C<readstate> and with the C<detail> property having the following data available:
2514  
2515 =over 4
2516  
2517 =item document
2518  
2519 The L<document object|HTML::Object::DOM::Document>
2520  
2521 =item state
2522  
2523 The state of the document parsing.
2524  
2525 =back
2526  
2527 The event C<target> property is also set to the L<document object|HTML::Object::DOM::Document>.
2528  
2529 See L<for more information|https://developer.mozilla.org/en-US/docs/Web/API/Document/readystatechange_event>
2530  
2531 =head2 onscroll
2532  
2533 Under perl, no such event is fired, but you can trigger one yourself.
2534  
2535 This property interface is an event handler that processes events when the document view or an element has been scrolled, whether by the user, a Web API, or the user agent.
2536  
2537 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/GlobalEventHandlers/onscroll>
2538  
2539 =head2 onselectionchange
2540  
2541 Under perl, no such event is fired, but you can trigger one yourself.
2542  
2543 This is fired when the Selection of a Document is changed. The Selection consists of a starting position and (optionally) a range of HTML nodes from that position. Clicking or starting a selection outside of a text field will generally fire this event.
2544  
2545 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/GlobalEventHandlers/onselectionchange>
2546  
2547 =head2 onvisibilitychange
2548  
2549 Is an event handler representing the code to be called when the visibilitychange event is raised.
2550  
2551 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/Document/onvisibilitychange>
2552  
2553 =head2 onwheel
2554  
2555 Under perl, no such event is fired, but you can trigger one yourself.
2556  
2557 This event is fired when the user rotates the mouse (or other pointing device) wheel.
2558  
2559 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/GlobalEventHandlers/onwheel>
2560  
2561 =head1 AUTHOR
2562  
2563 Jacques Deguest E<lt>F<jack@deguest.jp>E<gt>
2564  
2565 =head1 SEE ALSO
2566  
2567 L<HTML::Object::Document>, L<HTML::Object::DOM::Element>
2568  
2569 L<Mozilla documentation on DOM|https://developer.mozilla.org/en-US/docs/Web/API/Document_Object_Model>, L<Mozilla documentation on Document object|https://developer.mozilla.org/en-US/docs/Web/API/Document>
2570  
2571 =head1 COPYRIGHT & LICENSE
2572  
2573 Copyright(c) 2021 DEGUEST Pte. Ltd.
2574  
2575 All rights reserved
2576  
2577 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
2578  
2579 =cut