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

filter: Statementstmt 0.0% 0 / 1042 Branchbran 0.0% 0 / 420 Conditioncond 0.0% 0 / 283 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 139 Podpod 0.0% 0 / 126 totaltotal 0.0% 0 / 2010 CC 211 SCAR 49.7
CC
211
Cov
0%
_set_get_uri_property
870.0
_set_get_property
462.0
outerHTML
272.0
CRAP 144.7
1 ##----------------------------------------------------------------------------
2 ## HTML Object - ~/lib/HTML/Object/DOM/Element.pm
3 ## Version v0.4.0
4 ## Copyright(c) 2024 DEGUEST Pte. Ltd.
5 ## Author: Jacques Deguest <jack@deguest.jp>
6 ## Created 2021/12/13
7 ## Modified 2025/10/16
8 ## All rights reserved
9 ##
10 ##
11 ## This program is free software; you can redistribute it and/or modify it
12 ## under the same terms as Perl itself.
13 ##----------------------------------------------------------------------------
14 package HTML::Object::DOM::Element;
15 BEGIN
16 {
17 use strict;
18 use warnings;
19 warnings::register_categories( 'HTML::Object' );
20 use HTML::Object::DOM::Node qw( TEXT_NODE COMMENT_NODE );
21 use parent qw( HTML::Object::DOM::Node );
22 use vars qw( @EXPORT_OK $VERSION );
23 use HTML::Object::Exception;
24 use Scalar::Util ();
25 use URI;
26 use Wanted;
27 our @EXPORT_OK = qw(
28 DOCUMENT_POSITION_IDENTICAL DOCUMENT_POSITION_DISCONNECTED
29 DOCUMENT_POSITION_PRECEDING DOCUMENT_POSITION_FOLLOWING DOCUMENT_POSITION_CONTAINS
30 DOCUMENT_POSITION_CONTAINED_BY DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC
31 );
32 our $VERSION = 'v0.4.0';
33 };
34  
35 use strict;
36 use warnings;
37  
38 sub init
39 {
40 my $self = shift( @_ );
41 $self->{contentEditable} = 1;
42 $self->{_init_strict_use_sub} = 1;
43 $self->SUPER::init( @_ ) || return( $self->pass_error );
44 # internal trigger, essentially to be triggered when an attribute is being updated
45 # so the TokenList object can be updated
46 $self->{_internal_attribute_callbacks} = {} if( !exists( $self->{_internal_attribute_callbacks} ) || ref( $self->{_internal_attribute_callbacks} ) ne 'HASH' );
47 $self->{_internal_attribute_callbacks}->{class} = sub
48 {
49 my( $this, $val ) = @_;
50 my $list;
51 return if( !( $list = $this->{_class_list} ) );
52 $list->update( $val );
53 };
54 return( $self );
55 }
56  
57 # Note: accessKey -> property
58 sub accessKey : lvalue { return( shift->_set_get_property( 'accesskey', @_ ) ); }
59  
60 # Note: accessKeyLabel -> property
61 sub accessKeyLabel : lvalue { return( shift->_set_get_property( 'accessKeyLabel', @_ ) ); }
62  
63 sub after
64 {
65 my $self = shift( @_ );
66 return( $self ) if( !scalar( @_ ) );
67 my $parent = $self->parent;
68 return( $self->error( "No parent set for this element, so you cannot set this \"after\" method." ) ) if( !$parent );
69 my $pos = $parent->children->pos( $self );
70 # If a HTML::Object::DOM::DocumentFragment object is provided, its children are
71 # copied to the list and its own children array is emptied.
72 my $list = $self->_get_from_list_of_elements_or_html( @_ );
73 $list->foreach(sub
74 {
75 $_->parent( $parent );
76 $parent->children->splice( $pos + 1, 0, $_ );
77 $pos++;
78 # Required, because $pos++ as the last execution in this anon sub somehow returns defined and false
79 return(1);
80 });
81 $parent->reset(1);
82 return( $self );
83 }
84  
85 sub append
86 {
87 my $self = shift( @_ );
88 return( $self ) if( !scalar( @_ ) );
89 # If a HTML::Object::DOM::DocumentFragment object is provided, its children are
90 # copied to the list and its own children array is emptied.
91 my $list = $self->_get_from_list_of_elements_or_html( @_ );
92 my $children = $self->children;
93 $list->foreach(sub
94 {
95 $_->parent( $self );
96 $children->push( $_ );
97 });
98 $self->reset(1);
99 return( $list );
100 }
101  
102 sub assignedSlot { return; }
103  
104 sub attachInternals { return; }
105  
106 # Note: attributeStyleMap -> property
107 sub attributeStyleMap : lvalue { return( shift->_set_get_property( 'style', @_ ) ); }
108  
109 sub before
110 {
111 my $self = shift( @_ );
112 return( $self ) if( !scalar( @_ ) );
113 my $parent = $self->parent;
114 return( $self->error( "No parent set for this element, so you cannot set this \"before\" method." ) ) if( !$parent );
115 my $pos = $parent->children->pos( $self );
116 $pos--;
117 # If a HTML::Object::DOM::DocumentFragment object is provided, its children are
118 # copied to the list and its own children array is emptied.
119 my $list = $self->_get_from_list_of_elements_or_html( @_ );
120 $list->foreach(sub
121 {
122 $_->parent( $parent );
123 $parent->children->splice( $pos + 1, 0, $_ );
124 $pos++;
125 });
126 $parent->reset(1);
127 return( $self );
128 }
129  
130 sub blur { return; }
131  
132 # NOTE: HTML element property read-pnly
133 sub childElementCount { return( $_[0]->children->grep(sub{ $_[0]->_isa( $_ => 'HTML::Object::DOM::Element' ) })->length ); }
134  
135 # NOTE: HTML element property
136 # <https://developer.mozilla.org/en-US/docs/Web/API/Element/classList>
137 sub classList
138 {
139 my $self = shift( @_ );
140 unless( $self->{_class_list} )
141 {
142 my $classes = $self->attr( 'class' );
143 require HTML::Object::TokenList;
144 $self->{_class_list} = HTML::Object::TokenList->new( $classes, element => $self, attribute => 'class' ) ||
145 return( $self->pass_error( HTML::Object::TokenList->error ) );
146 }
147 return( $self->{_class_list} );
148 }
149  
150 # NOTE: Property
151 sub className : lvalue { return( shift->_set_get_callback({
152 get => sub
153 {
154 my $self = shift( @_ );
155 return( $self->new_scalar( $self->attr( 'class' ) ) );
156 },
157 set => sub
158 {
159 my $self = shift( @_ );
160 my $arg = shift( @_ );
161 $self->attr( class => $arg );
162 $self->reset(1);
163 return( $self->new_scalar( $arg ) );
164 }
165 }, @_ ) ); }
166  
167 sub clientHeight { return; }
168  
169 sub clientLeft { return; }
170  
171 sub clientTop { return; }
172  
173 sub clientWidth { return; }
174  
175 sub click { return( shift->trigger( 'click' ) ); }
176  
177 # TODO: closest: expand the support for xpath
178 sub closest
179 {
180 my $self = shift( @_ );
181 # Right now, only support a tag name.
182 my $what = shift( @_ ) || return( $self->error( "No value provided to find ancestor." ) );
183 $what = lc( $what );
184 my $lineage = $self->lineage;
185 my $result = $lineage->grep(sub{ $_->tag eq $what });
186 return( $result->first );
187 }
188  
189 # Taken from HTML::TreeBuilder::XPpath
190 sub cmp
191 {
192 my( $a, $b ) = @_;
193 # comparison with the root (in $b, or processed in HTML::Object::Root)
194 return( -1 ) if( $b->isa( 'HTML::Object::Root' ) );
195  
196 return(0) if( $a->eid eq $b->eid );
197 # easy cases
198 return( 0 ) if( $a == $b );
199 # a starts after b
200 return( 1 ) if( $a->is_inside( $b ) );
201 # a starts before b
202 return( -1 ) if( $b->is_inside( $a ) );
203  
204 # lineage does not include the element itself
205 my $a_pile = $a->lineage->unshift( $a );
206 my $b_pile = $b->lineage->unshift( $b );
207 # $a->debug(4);
208
209 # the 2 elements are not in the same twig
210 unless( $a_pile->last == $b_pile->last )
211 {
212 warn( "2 nodes not in the same pile: " . ref( $a ) . " - " . ref( $b ) . "\n" ) if( warnings::enabled( 'HTML::Object' ) );
213 # print "a: ", $a->string_value, "\nb: ", $b->string_value, "\n";
214 return;
215 }
216  
217 # find the first non common ancestors (they are siblings)
218 my $a_anc = $a_pile->pop;
219 my $b_anc = $b_pile->pop;
220  
221 while( $a_anc == $b_anc )
222 {
223 $a_anc = $a_pile->pop;
224 $b_anc = $b_pile->pop;
225 }
226  
227 if( defined( $a_anc->rank ) && defined( $b_anc->rank ) )
228 {
229 return( $a_anc->rank <=> $b_anc->rank );
230 }
231 else
232 {
233 # from there move left and right and figure out the order
234 my( $a_prev, $a_next, $b_prev, $b_next ) = ( $a_anc, $a_anc, $b_anc, $b_anc );
235 while()
236 {
237 $a_prev = $a_prev->getPreviousSibling || return( -1 );
238 return( 1 ) if( $a_prev == $b_anc );
239 $a_next = $a_next->getNextSibling || return( 1 );
240 return( -1 ) if( $a_next == $b_anc );
241 $b_prev = $b_prev->getPreviousSibling || return( 1 );
242 return( -1 ) if( $b_prev == $a_next );
243 $b_next = $b_next->getNextSibling || return( -1 );
244 return( 1 ) if( $b_next == $a_prev );
245 }
246 }
247 }
248  
249 # Note: contentEditable -> property
250 sub contentEditable : lvalue { return( shift->_set_get_property( 'contentEditable', @_ ) ); }
251  
252 # NOTE: dataset -> property
253 sub dataset
254 {
255 my $self = shift( @_ );
256 return( $self->{_data_map} ) if( $self->{_data_map} );
257 $self->_load_class( 'HTML::Object::ElementDataMap' ) ||
258 return( $self->pass_error );
259 my $map = HTML::Object::ElementDataMap->new( $self ) ||
260 return( $self->pass_error( HTML::Object::ElementDataMap->error ) );
261 return( $self->{_data_map} = $map );
262 }
263  
264 # Note: dir -> property
265 sub dir : lvalue { return( shift->_set_get_property( 'dir', @_ ) ); }
266  
267 # Note: draggable -> property
268 sub draggable : lvalue { return( shift->_set_get_property( { attribute => 'draggable', is_boolean => 1 }, @_ ) ); }
269  
270 # Note: enterKeyHint -> property
271 sub enterKeyHint : lvalue { return( shift->_set_get_property( 'enterKeyHint', @_ ) ); }
272  
273 sub firstElementChild
274 {
275 my $self = shift( @_ );
276 my $children = $self->children;
277 my $elem;
278 $children->foreach(sub
279 {
280 if( $_->isa( 'HTML::Object::DOM::Element' ) )
281 {
282 $elem = $_;
283 return(0);
284 }
285 return(1);
286 });
287 return( $self->new_null ) if( !defined( $elem ) );
288 return( $elem );
289 }
290  
291 sub focus { return; }
292  
293 sub getAttribute { return( shift->attributes->get( shift( @_ ) ) ); }
294  
295 # We return a clone version to be safe, since we rely on this, so if this get messed up
296 # things will go awry
297 sub getAttributeNames { return( shift->attributes_sequence->clone ); }
298  
299 sub getAttributeNode
300 {
301 my $self = shift( @_ );
302 my $name = shift( @_ ) || return;
303 # new_null is a nifty method inherited from Module::Generic
304 # It returns the right value based on the caller's expectation
305 return( $self->new_null ) if( !$self->attributes->exists( $name ) );
306 my $val = $self->attributes->get( $name );
307 my $att = $self->new_attribute( name => $name, value => $val, element => $self );
308 return( $att );
309 }
310  
311 sub getAttributeNodeNS { return; }
312  
313 sub getAttributeNS { return; }
314  
315 # Note: method getAttributes is inherited
316  
317 # Note: method getChildNodes is inherited
318  
319 sub getElementById
320 {
321 my( $self, $id ) = @_;
322 return( $self->error( "No id was provided to get its corresponding element." ) ) if( !defined( $id ) || !CORE::length( $id ) );
323 return( $self->look_down( id => $id )->first );
324 }
325  
326 sub getElementsByClassName
327 {
328 my $self = shift( @_ );
329 my @args = ();
330 if( scalar( @_ ) == 1 )
331 {
332 @args = split( /[[:blank:]\h]+/, $_[0] );
333 }
334 else
335 {
336 @args = @_;
337 }
338 my $results = $self->new_array;
339 my $test = $self->new_array( \@args )->unique(1);
340 my $totalClassRequired = $test->length->scalar;
341 # Nothing to do somehow
342 return( $results ) if( !$totalClassRequired );
343
344 my $seen = {};
345 my $crawl;
346 $crawl = sub
347 {
348 my $kid = shift( @_ );
349 $kid->children->foreach(sub
350 {
351 my $e = shift( @_ );
352 # Avoid looping
353 my $addr = Scalar::Util::refaddr( $e );
354 return(1) if( CORE::exists( $seen->{ $addr } ) );
355 $seen->{ $addr }++;
356 if( $e->attributes->exists( 'class' ) )
357 {
358 my $val = $self->new_scalar( $e->attributes->get( 'class' ) );
359 $val->trim( qr/[[:blank:]\h]+/ );
360 if( !$val->is_empty )
361 {
362 my $classes = $val->split( qr/[[:blank:]\h]+/ );
363 my $found = 0;
364 $test->foreach(sub
365 {
366 $found++ if( $classes->has( $_ ) );
367 });
368 $results->push( $e ) if( $found == $totalClassRequired );
369 }
370 }
371 $crawl->( $e );
372 # Always return true
373 return(1);
374 });
375 };
376 $crawl->( $self );
377 return( $results );
378 }
379  
380 sub getElementsByTagName
381 {
382 my $self = shift( @_ );
383 my $name = shift( @_ );
384 my $results = $self->new_array;
385 # Nothing to do somehow
386 return( $self->error( "No name was provided for getElementsByTagName()" ) ) if( !defined( $name ) || !CORE::length( "$name" ) );
387
388 my $seen = {};
389 my $crawl;
390 $crawl = sub
391 {
392 my $kid = shift( @_ );
393 $kid->children->foreach(sub
394 {
395 my $e = shift( @_ );
396 # Avoid looping
397 my $addr = Scalar::Util::refaddr( $e );
398 return(1) if( CORE::exists( $seen->{ $addr } ) );
399 $seen->{ $addr }++;
400 if( $e->tag eq $name )
401 {
402 $results->push( $e );
403 }
404 $crawl->( $e );
405 # Always return true
406 return(1);
407 });
408 };
409 $crawl->( $self );
410 return( $results );
411 }
412  
413 # Credits John Resig
414 # <https://johnresig.com/blog/comparing-document-position/>
415 # Original by PPK quirksmode.org
416 sub getElementsByTagNames
417 {
418 my $self = shift( @_ );
419 my $this;
420 my $results = $self->new_array;
421 if( scalar( @_ ) == 1 && !ref( $_[0] ) )
422 {
423 $this = [split( /[[:blank:]\h]+/, $this )];
424 }
425 elsif( scalar( @_ ) == 1 && $self->_is_array( $this ) )
426 {
427 # Good as-is
428 }
429 # list of elements
430 elsif( scalar( @_ ) > 1 )
431 {
432 $this = [@_];
433 }
434 else
435 {
436 return( $results );
437 }
438
439 my $tags = $self->new_array( $this );
440 $tags->foreach(sub
441 {
442 my $elems = $self->getElementsByTagName( $_ );
443 $results->push( $elems->list ) if( !$elems->is_empty );
444 });
445 $results->unique(1);
446 return( $results );
447 }
448  
449 # sub getFirstChild { return( shift->children->first ); }
450 # Note: method getFirstChild is inherited
451  
452 # Note: method getLastChild is inherited
453  
454 sub getLocalName
455 {
456 my $self = shift( @_ );
457 ( my $name = $self->tag ) =~ s{^.*:}{};
458 return( $name );
459 }
460  
461 sub getName { return( shift->tag ); }
462  
463 # sub getNextSibling { return( shift->right->first ); }
464 # Note: method getNextSibling is inherited
465  
466 sub getNodePath
467 {
468 my $self = shift( @_ );
469 my $a;
470 my $init;
471 if( @_ )
472 {
473 $a = shift( @_ );
474 }
475 else
476 {
477 $a = $self->new_array;
478 $init = 1;
479 }
480 return if( $self->isa( 'HTML::Object::Text' ) || $self->isa( 'HTML::Object::Comment' ) || $self->isa( 'HTML::Object::Declaration' ) );
481 my $tag = $self->tag;
482 my $parent = $self->parent;
483 if( !defined( $parent ) )
484 {
485 return( $a ) if( !defined( $tag ) || $tag CORE::eq '_document' );
486 $a->unshift( $tag );
487 return( $a );
488 }
489 my $nth = 0;
490 my $pos = 0;
491 $parent->children->foreach(sub
492 {
493 if( $_->tag CORE::eq $self->tag )
494 {
495 $nth++;
496 if( $_->eid CORE::eq $self->eid )
497 {
498 $pos = $nth;
499 }
500 }
501 # Continue to the next one
502 return(1);
503 });
504 $a->unshift( $nth > 1 ? "${tag}\[${pos}\]" : $tag );
505 return( $parent->getNodePath( $a ) ) unless( $init );
506 my $xpath = '/' . $a->join( '/' );
507 return( $xpath );
508 }
509  
510 sub getParentNode
511 {
512 my $self = shift( @_ );
513 return( $self->parent || $self->new_root( root => $self ) );
514 }
515  
516 # Note: getPreviousSibling is inherited
517  
518 sub getValue
519 {
520 my $self = shift( @_ );
521 # return( $self->text ) if( $self->isCommentNode );
522 return( $self->value ) if( $self->isCommentNode );
523 return( $self->as_text );
524 }
525  
526 sub hasAttribute { return( shift->attributes->has( shift( @_ ) ) ); }
527  
528 sub hasAttributes { return( !shift->attributes->is_empty ); }
529  
530 # Note: hidden -> property
531 sub hidden : lvalue { return( shift->_set_get_property( { attribute => 'hidden', is_boolean => 1 }, @_ ) ); }
532  
533 sub hidePopover { return; }
534  
535 # Note: inert -> property
536 sub inert : lvalue { return( shift->_set_get_property( { attribute => 'inert', is_boolean => 1 }, @_ ) ); }
537  
538 sub innerHTML : lvalue { return( shift->_set_get_callback({
539 get => sub
540 {
541 my $self = shift( @_ );
542 # Create a new document, because we want to use the document object as_string function which produce a string of its children, and no need to reproduce it here
543 my $doc = $self->new_document;
544 $doc->children( $self->children );
545 return( $doc->as_string );
546 },
547 set => sub
548 {
549 my $self = shift( @_ );
550 my $this = shift( @_ );
551 my $children;
552 if( !ref( $this ) ||
553 ( ref( $this ) && overload::Overloaded( $this ) && overload::Method( $this, '""' ) ) )
554 {
555 my $p = $self->new_parser;
556 my $res = $p->parse_data( "$this" ) ||
557 die( "Error while parsing html data provided: " . $p->error );
558 $children = $res->children;
559 }
560 # We are provided with an element, so we set it as our inner html
561 elsif( $self->_is_a( $this => 'HTML::Object::Element' ) )
562 {
563 # If a HTML::Object::DOM::DocumentFragment object is provided, its children are
564 # copied to the list and its own children array is emptied.
565 if( $self->_is_a( $this => 'HTML::Object::DOM::DocumentFragment' ) )
566 {
567 $children = $this->children->clone;
568 $this->children->reset;
569 }
570 else
571 {
572 my $child = $this->clone;
573 $children = $self->new_array( $child );
574 }
575 }
576 else
577 {
578 die( "I was expecting some html data in replacement of html for this element \"" . $self->tag . "\", but instead got '" . ( CORE::length( $this ) > 1024 ? ( CORE::substr( $this, 0, 1024 ) . '...' ) : $this ) . "'." );
579 }
580
581 $children->foreach(sub
582 {
583 $_->parent( $self );
584 });
585 $self->children( $children );
586 $self->reset(1);
587 return(1);
588 }
589 }, @_ ) ); }
590  
591 # Note: innerText -> property
592 sub innerText : lvalue { return( shift->_set_get_callback({
593 get => sub
594 {
595 my $self = shift( @_ );
596 # Create a new document, because we want to use the document object as_string function which produce a string of its children, and no need to reproduce it here
597 my $txt = $self->as_trimmed_text;
598 $txt =~ s,<br[[:blank:]]*/>\n,\n,gs;
599 $txt =~ s/\&gt;/>/gs;
600 $txt =~ s/\&lt;/</gs;
601 my $obj = $self->new_scalar( \$txt );
602 return( $obj );
603 },
604 set => sub
605 {
606 my $self = shift( @_ );
607 my $this = shift( @_ );
608 my $children;
609 # We are provided with an element, so we set it as our inner html
610 if( $self->_is_a( $this => 'HTML::Object::DOM::Text' ) )
611 {
612 my $copy = $this->clone;
613 my $val = $copy->value;
614 $val =~ s/</\&lt;/gs;
615 $val =~ s/>/\&gt;/gs;
616 $val =~ s,\n,<br />\n,gs;
617 $copy->value( $val );
618 $children = $self->new_array( $copy );
619 }
620 elsif( !ref( $this ) ||
621 ( ref( $this ) && overload::Overloaded( $this ) && overload::Method( $this, '""' ) ) )
622 {
623 # $this =~ s,\n,<br />\n,gs;
624 my $val = "$this";
625 $val =~ s/</\&lt;/gs;
626 $val =~ s/>/\&gt;/gs;
627 $val =~ s,\n,<br />\n,gs;
628 my $txt = $self->new_text( value => $val ) || die( $self->error );
629 $children = $self->new_array( $txt );
630 }
631 else
632 {
633 die( "I was expecting some text data in replacement of html for this element \"" . $self->tag . "\", but instead got '" . ( CORE::length( $this ) > 1024 ? ( CORE::substr( $this, 0, 1024 ) . '...' ) : $this ) . "'." );
634 }
635
636 $children->foreach(sub
637 {
638 $_->parent( $self );
639 });
640 $self->children( $children );
641 $self->reset(1);
642 return(1);
643 }
644 }, @_ ) ); }
645  
646 # Note: inputMode -> property
647 sub inputMode : lvalue { return( shift->_set_get_property( 'inputMode', @_ ) ); }
648  
649 sub insertAdjacentElement
650 {
651 my $self = shift( @_ );
652 my( $pos, $elem ) = @_;
653 return( $self->error({
654 message => 'No position was provided',
655 code => 500,
656 class => 'HTML::Object::SyntaxError',
657 }) ) if( !defined( $pos ) || !CORE::length( "$pos" ) );
658 # Return error if the element provided is either undefined , or an empty string
659 return( $self->error({
660 message => "No element was provided.",
661 code => 500,
662 class => 'HTML::Object::TypeError',
663 }) ) if( !defined( $elem ) || ( !ref( $elem ) && !CORE::length( $elem ) ) );
664 $pos = lc( "$pos" );
665 # Error if the position string provided is of an unknown value.
666 return( $self->error({
667 message => "Position provided \"$pos\" is not a recognised value. Use beforebegin, afterbegin, beforeend or afterend",
668 code => 500,
669 class => 'HTML::Object::SyntaxError',
670 }) ) if( $pos !~ /^(?:beforebegin|afterbegin|beforeend|afterend)$/ );
671 # Error if the element value provided is not an element object.
672 return( $self->error({
673 message => "Element provided (" . overload::StrVal( $elem ) . ") is not an HTML::Object::DOM::Element object.",
674 code => 500,
675 class => 'HTML::Object::TypeError',
676 }) ) if( !$self->_is_a( $elem => 'HTML::Object::DOM::Element' ) );
677 my $parent = $self->parent;
678 return( $self->error({
679 message => "Current object has no parent, so the provided element cannot be inserted before or after it.",
680 code => 500,
681 class => 'HTML::Object::HierarchyRequestError',
682 }) ) if( !$parent && ( $pos eq 'beforebegin' || $pos eq 'afterend' ) );
683 if( $pos eq 'beforebegin' )
684 {
685 my $offset = $parent->children->pos( $self );
686 if( !defined( $offset ) )
687 {
688 return( $self->error({
689 message => "The current element (" . overload::StrVal( $self ) . ") could not be found in its parent element (" . overload::StrVal( $parent ) . ") whose tag is \"" . $parent->tag . "\".",
690 code => 500,
691 class => 'HTML::Object::HierarchyRequestError',
692 }) );
693 }
694 else
695 {
696 $parent->splice( $offset, 0, $elem );
697 }
698 }
699 elsif( $pos eq 'beforeend' )
700 {
701 $self->children->push( $elem );
702 }
703 elsif( $pos eq 'afterbegin' )
704 {
705 $self->children->unshift( $elem );
706 }
707 elsif( $pos eq 'afterend' )
708 {
709 my $offset = $parent->children->pos( $self );
710 if( !defined( $offset ) )
711 {
712 return( $self->error({
713 message => "The current element (" . overload::StrVal( $self ) . ") could not be found in its parent element (" . overload::StrVal( $parent ) . ") whose tag is \"" . $parent->tag . "\".",
714 code => 500,
715 class => 'HTML::Object::HierarchyRequestError',
716 }) );
717 }
718 $parent->splice( ++$offset, 0, $elem );
719 }
720 return( $elem );
721 }
722  
723 sub insertAdjacentHTML
724 {
725 my $self = shift( @_ );
726 my( $pos, $html ) = @_;
727 return( $self->error({
728 message => 'No position was provided',
729 code => 500,
730 class => 'HTML::Object::SyntaxError',
731 }) ) if( !defined( $pos ) || !CORE::length( "$pos" ) );
732 # Return error if the element provided is either undefined , or an empty string
733 return( $self->error({
734 message => "No html string was provided to insert.",
735 code => 500,
736 class => 'HTML::Object::TypeError',
737 }) ) if( !defined( $html ) || !CORE::length( "$html" ) );
738 return( $self->error({
739 message => "A reference (" . ref( $html ) . ") was provided instead of an HTML string.",
740 code => 500,
741 class => 'HTML::Object::TypeError',
742 }) ) if( ref( $html ) && !overload::Method( $html, '""' ) );
743 $html = "$html";
744 my $p = $self->new_parser || return( $self->pass_error );
745 my $doc = $p->parse_data( $html ) || return( $self->pass_error( $p->error ) );
746 my $parent = $self->parent;
747 return( $self->error({
748 message => "Current object has no parent, so the provided html nodes cannot be inserted before or after it.",
749 code => 500,
750 class => 'HTML::Object::HierarchyRequestError',
751 }) ) if( !$parent && ( $pos eq 'beforebegin' || $pos eq 'afterend' ) );
752 if( $pos eq 'beforebegin' )
753 {
754 my $offset = $parent->children->pos( $self );
755 if( !defined( $offset ) )
756 {
757 return( $self->error({
758 message => "The current element (" . overload::StrVal( $self ) . ") could not be found in its parent element (" . overload::StrVal( $parent ) . ") whose tag is \"" . $parent->tag . "\".",
759 code => 500,
760 class => 'HTML::Object::HierarchyRequestError',
761 }) );
762 }
763 $doc->children->foreach(sub
764 {
765 my $elem = shift( @_ );
766 $elem->parent( $parent );
767 $parent->children->splice( $offset, 0, $elem );
768 $offset++;
769 });
770 }
771 elsif( $pos eq 'beforeend' )
772 {
773 $doc->children->foreach(sub
774 {
775 my $elem = shift( @_ );
776 $elem->parent( $self );
777 $self->children->push( $elem );
778 });
779 }
780 elsif( $pos eq 'afterbegin' )
781 {
782 my $offset = -1;
783 $doc->children->foreach(sub
784 {
785 my $elem = shift( @_ );
786 $elem->parent( $self );
787 $self->children->splice( ++$offset, 0, $elem );
788 });
789 # $self->children->unshift( $elem );
790 }
791 elsif( $pos eq 'afterend' )
792 {
793 my $offset = $parent->children->pos( $self );
794 if( !defined( $offset ) )
795 {
796 return( $self->error({
797 message => "The current element (" . overload::StrVal( $self ) . ") could not be found in its parent element (" . overload::StrVal( $parent ) . ") whose tag is \"" . $parent->tag . "\".",
798 code => 500,
799 class => 'HTML::Object::HierarchyRequestError',
800 }) );
801 }
802 $doc->children->foreach(sub
803 {
804 my $elem = shift( @_ );
805 $elem->parent( $parent );
806 $parent->children->splice( ++$offset, 0, $elem );
807 });
808 }
809 return( $doc->children );
810 }
811  
812 sub insertAdjacentText
813 {
814 my $self = shift( @_ );
815 my $pos = shift( @_ );
816 return( $self->error({
817 message => 'No position was provided',
818 code => 500,
819 class => 'HTML::Object::SyntaxError',
820 }) ) if( !defined( $pos ) || !CORE::length( "$pos" ) );
821 my $text;
822 if( !scalar( @_ ) ||
823 ( scalar( @_ ) == 1 && !defined( $_[0] ) ) ||
824 ( scalar( @_ ) > 1 && !CORE::length( $text = join( '', @_ ) ) ) )
825 {
826 return( $self->error({
827 message => "No text was provided.",
828 code => 500,
829 class => 'HTML::Object::TypeError',
830 }) );
831 }
832 return( $self->error({
833 message => "A reference (" . ref( $text ) . ") was provided instead of an text string.",
834 code => 500,
835 class => 'HTML::Object::TypeError',
836 }) ) if( ref( $text ) && !overload::Method( $text, '""' ) );
837 my $node = $self->new_text( value => "$text" );
838 my $parent = $self->parent;
839 return( $self->error({
840 message => "Current object has no parent, so the provided text cannot be inserted before or after it.",
841 code => 500,
842 class => 'HTML::Object::HierarchyRequestError',
843 }) ) if( !$parent && ( $pos eq 'beforebegin' || $pos eq 'afterend' ) );
844 if( $pos eq 'beforebegin' )
845 {
846 my $offset = $parent->children->pos( $self );
847 if( !defined( $offset ) )
848 {
849 return( $self->error({
850 message => "The current element (" . overload::StrVal( $self ) . ") could not be found in its parent element (" . overload::StrVal( $parent ) . ") whose tag is \"" . $parent->tag . "\".",
851 code => 500,
852 class => 'HTML::Object::HierarchyRequestError',
853 }) );
854 }
855 else
856 {
857 $parent->splice( $offset, 0, $node );
858 }
859 }
860 elsif( $pos eq 'beforeend' )
861 {
862 $self->children->push( $node );
863 }
864 elsif( $pos eq 'afterbegin' )
865 {
866 $self->children->unshift( $node );
867 }
868 elsif( $pos eq 'afterend' )
869 {
870 my $offset = $parent->children->pos( $self );
871 if( !defined( $offset ) )
872 {
873 return( $self->error({
874 message => "The current element (" . overload::StrVal( $self ) . ") could not be found in its parent element (" . overload::StrVal( $parent ) . ") whose tag is \"" . $parent->tag . "\".",
875 code => 500,
876 class => 'HTML::Object::HierarchyRequestError',
877 }) );
878 }
879 $parent->splice( ++$offset, 0, $node );
880 }
881 return( $node );
882 }
883  
884 sub is_inside
885 {
886 my $self = shift( @_ );
887 return( 0 ) if( !scalar( @_ ) );
888 my @elems = @_;
889 my @literals = ();
890 for( my $i = 0; $i < scalar( @elems ); $i++ )
891 {
892 return( $self->error( "The element provided (", overload::StrVal( $elems[$i] ), ") is not an HTML::Object::Element object." ) ) if( ref( $elems[$i] ) && ( !$self->_is_object( $elems[$i] ) || !$elems[$i]->isa( 'HTML::Object::Element' ) ) );
893 push( @literals, splice( @elems, $i, 1 ) ) if( !ref( $elems[$i] ) );
894 }
895 # We need to ensure the literals provided, if any, are in lowercase
896 # @$lit{ @literals } = (1) x scalar( @literals );
897 my $lit = +{ map( lc( $_ ), @literals ) };
898 my $obj = +{ map{ $_->eid => 1 } @elems };
899 my $parent = $self;
900 # Check if ourself for any of our parent are a match of any of the element given
901 while( $parent )
902 {
903 return( 1 ) if( exists( $obj->{ $parent->eid } ) || exists( $lit->{ $parent->tag } ) );
904 $parent = $parent->parent;
905 }
906 return( 0 );
907 }
908  
909 sub isAttributeNode { return(0); }
910  
911 sub isCommentNode { return( shift->tag CORE::eq '_comment' ? 1 : 0 ); }
912  
913 sub isContentEditable
914 {
915 my $self = shift( @_ );
916 return( $self->contentEditable ? $self->true : $self->false );
917 }
918  
919 sub isElementNode { return( shift->tag->substr( 0, 1 ) CORE::eq '_' ? 0 : 1 ); }
920  
921 sub isNamespaceNode { return(0); }
922  
923 sub isPINode { return(0); }
924  
925 sub isProcessingInstructionNode { return( shift->tag CORE::eq '_pi' ? 1 : 0 ); }
926  
927 sub isTextNode { return( shift->tag CORE::eq '_text' ? 1 : 0 ); }
928  
929 # Note: lang -> property
930 sub lang : lvalue { return( shift->_set_get_property( 'lang', @_ ) ); }
931  
932 sub lastElementChild
933 {
934 my $self = shift( @_ );
935 my $children = $self->children;
936 my $elem;
937 $children->reverse->foreach(sub
938 {
939 if( $_->isa( 'HTML::Object::DOM::Element' ) )
940 {
941 $elem = $_;
942 return(0);
943 }
944 return(1);
945 });
946 return( $self->new_null ) if( !defined( $elem ) );
947 return( $elem );
948 }
949  
950 sub localName { return( shift->getName ); }
951  
952 sub matches
953 {
954 my $self = shift( @_ );
955 my $selector = shift( @_ );
956 my $opts = $self->_get_args_as_hash( @_ );
957 my $params = {};
958 # The only supported parameter by HTML::Selector::XPath
959 $params->{root} = CORE::delete( $opts->{root} ) if( CORE::exists( $opts->{root} ) );
960 if( !$self->{_xp} )
961 {
962 $self->_load_class( 'HTML::Object::XPath' ) ||
963 return( $self->pass_error );
964 my $xp = HTML::Object::XPath->new;
965 $self->{_xp} = $xp;
966 }
967 $self->_load_class( 'HTML::Selector::XPath', { version => '0.20' } ) ||
968 return( $self->pass_error );
969 my $xpath;
970 # try-catch
971 local $@;
972 eval
973 {
974 my $sel = HTML::Selector::XPath->new( $selector, %$params );
975 $xpath = $sel->to_xpath( %$params );
976 };
977 if( $@ )
978 {
979 return( $self->error( "Error trying to get the xpath value for selector \"$selector\": $@" ) );
980 }
981 my $xp = $self->{_xp};
982 $self->message( 4, "Calling xp->matches for xpath '$xpath' with context '", $self->as_string, "'" );
983 return( $xp->matches( $self, $xpath, $self ) );
984 }
985  
986 sub namespaceURI { return; }
987  
988 sub new_attribute
989 {
990 my $self = shift( @_ );
991 $self->_load_class( 'HTML::Object::DOM::Attribute' ) || return( $self->pass_error );
992 my $att = HTML::Object::DOM::Attribute->new( @_ ) ||
993 return( $self->pass_error( HTML::Object::DOM::Attribute->error ) );
994 return( $att );
995 }
996  
997 sub new_closing
998 {
999 my $self = shift( @_ );
1000 $self->_load_class( 'HTML::Object::DOM::Closing' ) || return( $self->pass_error );
1001 my $e = HTML::Object::DOM::Closing->new( @_ ) ||
1002 return( $self->pass_error( HTML::Object::DOM::Closing->error ) );
1003 return( $e );
1004 }
1005  
1006 sub new_collection_elements
1007 {
1008 my $self = shift( @_ );
1009 $self->_load_class( 'HTML::Object::DOM::Collection' ) || return( $self->pass_error );
1010 my $e = HTML::Object::DOM::Collection->new( @_ ) ||
1011 return( $self->pass_error( HTML::Object::DOM::Collection->error ) );
1012 return( $e );
1013 }
1014  
1015 sub new_comment
1016 {
1017 my $self = shift( @_ );
1018 $self->_load_class( 'HTML::Object::DOM::Comment' ) || return( $self->pass_error );
1019 my $e = HTML::Object::DOM::Comment->new( @_ ) ||
1020 return( $self->pass_error( HTML::Object::DOM::Comment->error ) );
1021 return( $e );
1022 }
1023  
1024 sub new_document
1025 {
1026 my $self = shift( @_ );
1027 $self->_load_class( 'HTML::Object::DOM::Document' ) || return( $self->pass_error );
1028 my $e = HTML::Object::DOM::Document->new( debug => $self->debug ) ||
1029 return( $self->pass_error( HTML::Object::DOM::Document->error ) );
1030 return( $e );
1031 }
1032  
1033 sub new_element
1034 {
1035 my $self = shift( @_ );
1036 my $tag = shift( @_ ) || return( $self->error( "No tag was provided to create an element." ) );
1037 my $dict = HTML::Object->get_definition( $tag ) || return( $self->pass_error( HTML::Object->error ) );
1038 my $e = HTML::Object::DOM::Element->new({
1039 is_empty => $dict->{is_empty},
1040 tag => $dict->{tag},
1041 debug => $self->debug,
1042 }) || return( $self->pass_error( HTML::Object::DOM::Element->error ) );
1043 return( $e );
1044 }
1045  
1046 sub new_nodelist
1047 {
1048 my $self = shift( @_ );
1049 $self->_load_class( 'HTML::Object::DOM::NodeList' ) || return( $self->pass_error );
1050 my $list = HTML::Object::DOM::NodeList->new( @_ ) ||
1051 return( $self->pass_error( HTML::Object::DOM::NodeList->error ) );
1052 return( $list );
1053 }
1054  
1055 sub new_parser
1056 {
1057 my $self = shift( @_ );
1058 $self->_load_class( 'HTML::Object::DOM' ) || return( $self->pass_error );
1059 my $p = HTML::Object::DOM->new( debug => $self->debug ) ||
1060 return( $self->pass_error( HTML::Object::DOM->error ) );
1061 return( $p );
1062 }
1063  
1064 sub new_space
1065 {
1066 my $self = shift( @_ );
1067 $self->_load_class( 'HTML::Object::DOM::Space' ) || return( $self->pass_error );
1068 my $e = HTML::Object::DOM::Space->new( @_ ) ||
1069 return( $self->pass_error( HTML::Object::DOM::Space->error ) );
1070 return( $e );
1071 }
1072  
1073 sub new_text
1074 {
1075 my $self = shift( @_ );
1076 $self->_load_class( 'HTML::Object::DOM::Text' ) || return( $self->pass_error );
1077 my $e = HTML::Object::DOM::Text->new( @_ ) ||
1078 return( $self->pass_error( HTML::Object::DOM::Text->error ) );
1079 return( $e );
1080 }
1081  
1082 sub nextElementSibling
1083 {
1084 my $self = shift( @_ );
1085 my $all = $self->right;
1086 for( my $i = 0; $i < scalar( @$all ); $i++ )
1087 {
1088 return( $all->[$i] ) if( $self->_is_a( $all->[$i] => 'HTML::Object::DOM::Element' ) );
1089 }
1090 return( $self->new_null );
1091 }
1092  
1093 # Note: noModule -> property
1094 sub noModule : lvalue { return( shift->_set_get_property( { attribute => 'noModule', is_boolean => 1 }, @_ ) ); }
1095  
1096 # Note: nonce -> property
1097 sub nonce : lvalue { return( shift->_set_get_property( 'nonce', @_ ) ); }
1098  
1099 # Note: offsetHeight -> property
1100 sub offsetHeight : lvalue { return( shift->_set_get_property( 'offsetheight', @_ ) ); }
1101  
1102 # Note: offsetLeft -> property
1103 sub offsetLeft : lvalue { return( shift->_set_get_property( 'offsetleft', @_ ) ); }
1104  
1105 # Note: offsetParent -> property
1106 sub offsetParent { return( shift->parent( @_ ) ); }
1107  
1108 # Note: offsetTop -> property
1109 sub offsetTop : lvalue { return( shift->_set_get_property( 'offsettop', @_ ) ); }
1110  
1111 # Note: offsetWidth -> property
1112 sub offsetWidth : lvalue { return( shift->_set_get_property( 'offsetwidth', @_ ) ); }
1113  
1114 sub onerror : lvalue { return( shift->_set_get_code( '_error_handler', @_ ) ); }
1115  
1116 # Note: Property
1117 sub outerHTML : lvalue { return( shift->_set_get_callback({
1118 get => sub
1119 {
1120 my $self = shift( @_ );
1121 return( $self->as_string );
1122 },
1123 set => sub
1124 {
1125 my $self = shift( @_ );
1126 my $this = shift( @_ );
1127 my $children;
1128 my $pos;
1129 my $parent = $self->parent;
1130 $pos = $parent->children->pos( $self ) if( $parent );
1131 my $dummy;
1132 if( !ref( $this ) ||
1133 ( ref( $this ) && overload::Overloaded( $this ) && overload::Method( $this, '""' ) ) )
1134 {
1135 # User provided an empty string, so we just remove the element
1136 if( !CORE::length( $this ) )
1137 {
1138 if( defined( $pos ) )
1139 {
1140 $parent->children->splice( $pos, 0 );
1141 # If this element has a closing tag in the dom, we remove it too
1142 if( my $close = $self->close_tag )
1143 {
1144 $parent->children->remove( $close );
1145 $close->parent( undef );
1146 }
1147 $self->parent( undef );
1148 $self->parent->reset(1);
1149 $dummy = 1;
1150 }
1151 # Fallback
1152 else
1153 {
1154 $dummy = 0;
1155 }
1156 return( $dummy );
1157 }
1158 else
1159 {
1160 my $p = $self->new_parser;
1161 my $res = $p->parse_data( "$this" ) ||
1162 return( $self->error( "Error while parsing html data provided: ", $p->error ) );
1163 $children = $res->children;
1164 if( !$children->is_empty && defined( $pos ) )
1165 {
1166 $children->foreach(sub
1167 {
1168 $_->parent( $parent );
1169 });
1170 $parent->children->splice( $pos, 1, $children->list );
1171 # If this element has a closing tag in the dom, we remove it too
1172 if( my $close = $self->close_tag )
1173 {
1174 $parent->children->remove( $close );
1175 $close->parent( undef );
1176 }
1177 $parent->reset(1);
1178 $dummy = 1;
1179 }
1180 else
1181 {
1182 $dummy = 0;
1183 }
1184 return( $dummy );
1185 }
1186 }
1187 # We are provided with an element, so we set it as our inner html
1188 elsif( $self->_is_a( $this => 'HTML::Object::Element' ) )
1189 {
1190 # If a HTML::Object::DOM::DocumentFragment object is provided, its children are
1191 # copied to the list and its own children array is emptied.
1192 if( $self->_is_a( $this => 'HTML::Object::DOM::DocumentFragment' ) )
1193 {
1194 my $copy = $this->children->clone;
1195 $this->children->reset;
1196 if( defined( $pos ) )
1197 {
1198 $parent->children->splice( $pos, 1, $copy->list );
1199 $copy->children->foreach(sub
1200 {
1201 $_->parent( $parent );
1202 });
1203 # The element itself is being replace, so we remove out own parent
1204 $self->parent->reset(1);
1205 $self->parent( undef );
1206 # If this element has a closing tag in the dom, we remove it too
1207 if( my $close = $self->close_tag )
1208 {
1209 $parent->children->remove( $close );
1210 $close->parent( undef );
1211 }
1212 $dummy = 1;
1213 }
1214 else
1215 {
1216 $dummy = 0;
1217 }
1218 }
1219 else
1220 {
1221 my $child = $this->clone;
1222 if( defined( $pos ) )
1223 {
1224 $parent->children->splice( $pos, 1, $child );
1225 # Add the closing tag if any
1226 if( my $close = $child->close_tag )
1227 {
1228 $parent->children->splice( $pos + 1, 0, $close );
1229 # $parent->children->splice( 2, 0, $close );
1230 }
1231 $child->parent( $parent );
1232 # The element itself is being replace, so we remove out own parent
1233 $self->parent->reset(1);
1234 $self->parent( undef );
1235 # If this element has a closing tag in the dom, we remove it too
1236 if( my $close = $self->close_tag )
1237 {
1238 $parent->children->remove( $close );
1239 $close->parent( undef );
1240 }
1241 $dummy = 1;
1242 }
1243 else
1244 {
1245 $dummy = 0;
1246 }
1247 }
1248 return( $dummy );
1249 }
1250 else
1251 {
1252 die( "I was expecting some html data in replacement of html for this element \"" . $self->tag . "\", but instead got '" . ( CORE::length( $this ) > 1024 ? ( CORE::substr( $this, 0, 1024 ) . '...' ) : $this ) . "'." );
1253 }
1254 }
1255 }, @_ ) ); }
1256  
1257 # Note: outerText -> property
1258 sub outerText : lvalue { return( shift->_set_get_callback({
1259 get => sub
1260 {
1261 my $self = shift( @_ );
1262 # Create a new document, because we want to use the document object as_string function which produce a string of its children, and no need to reproduce it here
1263 my $txt = $self->as_trimmed_text;
1264 my $obj = $self->new_scalar( \$txt );
1265 return( $obj );
1266 },
1267 set => sub
1268 {
1269 my $self = shift( @_ );
1270 my $this = shift( @_ );
1271 my $element;
1272 # We are provided with an element, so we set it as our inner html
1273 if( $self->_is_a( $this => 'HTML::Object::DOM::Text' ) )
1274 {
1275 $element = $this->clone;
1276 }
1277 elsif( !ref( $this ) ||
1278 ( ref( $this ) && overload::Overloaded( $this ) && overload::Method( $this, '""' ) ) )
1279 {
1280 $this =~ s,\n,<br />\n,gs;
1281 $element = $self->new_text( value => $this ) || die( $self->error );
1282 }
1283 else
1284 {
1285 die( "I was expecting some text data in replacement of html for this element \"" . $self->tag . "\", but instead got '" . ( CORE::length( $this ) > 1024 ? ( CORE::substr( $this, 0, 1024 ) . '...' ) : $this ) . "'." );
1286 }
1287
1288 my $parent = $self->parent;
1289 my $pos = $parent->children->pos( $self );
1290 if( !defined( $pos ) )
1291 {
1292 die( "Unable to find the current element among its parent's children." );
1293 }
1294
1295 $element->parent( $parent );
1296 $parent->splice( $pos, 1, $element );
1297 $self->parent( undef() );
1298 $parent->reset(1);
1299 my $dummy = 'dummy';
1300 return( $dummy );
1301 }
1302 }, @_ ) ); }
1303  
1304 # Note: popover -> property
1305 sub popover : lvalue { return( shift->_set_get_callback({
1306 get => sub
1307 {
1308 my $self = shift( @_ );
1309 my $val = $self->_set_get_property( 'popover' );
1310 return( $val ) if( defined( $val ) );
1311 return( $self->root->_set_get_property( 'popover' ) );
1312 },
1313 set => sub
1314 {
1315 my $self = shift( @_ );
1316 my $arg = shift( @_ );
1317 $self->_set_get_property( 'popover', $arg );
1318 return( $arg );
1319 }
1320 }, @_ ) ); }
1321  
1322 sub prefix { return; }
1323  
1324 sub prepend
1325 {
1326 my $self = shift( @_ );
1327 return( $self->error({
1328 message => "No data to prepend was provided.",
1329 code => 500,
1330 class => 'HTML::Object::SyntaxError',
1331 }) ) if( !scalar( @_ ) );
1332 # If a HTML::Object::DOM::DocumentFragment object is provided, its children are
1333 # copied to the list and its own children array is emptied.
1334 my $list = $self->_get_from_list_of_elements_or_html( @_ );
1335 my $children = $self->children;
1336 my $pos = -1;
1337 $list->foreach(sub
1338 {
1339 $_->parent( $self );
1340 $children->splice( ++$pos, 0, $_ );
1341 });
1342 $self->reset(1);
1343 return( $list );
1344 }
1345  
1346 sub previousElementSibling
1347 {
1348 my $self = shift( @_ );
1349 my $all = $self->left->reverse;
1350 for( my $i = 0; $i < scalar( @$all ); $i++ )
1351 {
1352 return( $all->[$i] ) if( $self->_is_a( $all->[$i] => 'HTML::Object::DOM::Element' ) );
1353 }
1354 return( $self->new_null );
1355 }
1356  
1357 # Note: properties -> property experimental
1358 sub properties { return( shift->new_array ); }
1359  
1360 sub querySelector
1361 {
1362 my $self = shift( @_ );
1363 my @sels = @_;
1364 return( $self->error({
1365 message => "No CSS selector was provided to query.",
1366 code => 500,
1367 class => 'HTML::Object::SyntaxError',
1368 }) ) if( !scalar( @sels ) );
1369
1370 foreach my $sel ( @sels )
1371 {
1372 my $results = $self->findNode( $sel, { root => '.' } ) ||
1373 return( $self->pass_error({ class => 'HTML::Object::SyntaxError' }) );
1374 return( $results->first ) if( !$results->is_empty );
1375 }
1376 return( $self->new_null );
1377 }
1378  
1379 sub querySelectorAll
1380 {
1381 my $self = shift( @_ );
1382 my @sels = @_;
1383 return( $self->error({
1384 message => "No CSS selector was provided to query.",
1385 code => 500,
1386 class => 'HTML::Object::SyntaxError',
1387 }) ) if( !scalar( @sels ) );
1388
1389 my $results = $self->new_array;
1390 foreach my $sel ( @sels )
1391 {
1392 my $elems = $self->findNode( $sel, { root => './' } ) ||
1393 return( $self->pass_error({ class => 'HTML::Object::SyntaxError' }) );
1394 $results->push( $elems->list ) if( !$elems->is_empty );
1395 }
1396 $results->unique(1);
1397 return( $results );
1398 }
1399  
1400 sub remove
1401 {
1402 my $self = shift( @_ );
1403 my $parent = $self->parent;
1404 return( $self->error({
1405 message => "This element has no parent, and thus cannot be removed.",
1406 code => 500,
1407 class => 'HTML::Object::HierarchyRequestError',
1408 }) ) if( !$parent );
1409 my $pos = $parent->children->pos( $self );
1410 return( $self->error({
1411 message => "This element could not be found among its parent's children.",
1412 code => 500,
1413 class => 'HTML::Object::HierarchyRequestError',
1414 }) ) if( !defined( $pos ) );
1415 $parent->children->splice( $pos, 1 );
1416 $parent->reset(1);
1417 return( $self->true );
1418 }
1419  
1420 sub removeAttribute
1421 {
1422 my $self = shift( @_ );
1423 my $name = shift( @_ );
1424 return( $self->error({
1425 message => "No attribute name was provided.",
1426 code => 500,
1427 class => 'HTML::Object::SyntaxError',
1428 }) ) if( !defined( $name ) || !CORE::length( $name ) );
1429 if( $self->attributes->has( $name ) )
1430 {
1431 $self->attributes->remove( $name );
1432 $self->attributes_sequence->remove( $name );
1433 return( $self );
1434 }
1435 return;
1436 }
1437  
1438 sub removeAttributeNode
1439 {
1440 my $self = shift( @_ );
1441 my $node = shift( @_ ) || return( $self->error({
1442 message => "No attribute node was provided to remove.",
1443 code => 500,
1444 class => 'HTML::Object::SyntaxError',
1445 }) );
1446 return( $self->error({
1447 message => "Object provided is not an attribute node.",
1448 code => 500,
1449 class => 'HTML::Object::SyntaxError',
1450 }) ) if( !$self->_is_a( $node => 'HTML::Object::DOM::Attribute' ) );
1451 my $name = $node->name;
1452 return( $self->error({
1453 message => "Attribute node provided has no name value.",
1454 code => 500,
1455 class => 'HTML::Object::SyntaxError',
1456 }) ) if( $name->is_empty );
1457 return( $self->removeAttribute( $name ) );
1458 }
1459  
1460 sub removeAttributeNS { return; }
1461  
1462 sub replaceChildren
1463 {
1464 my $self = shift( @_ );
1465 my $results = $self->new_array;
1466 my $children = $self->children;
1467 if( !scalar( @_ ) )
1468 {
1469 $results->push( $children->list );
1470 $children->reset;
1471 $results->foreach(sub
1472 {
1473 $_->parent( undef );
1474 });
1475 return( $results );
1476 }
1477 my $new = $self->_list_to_nodes( @_ ) || return( $self->pass_error({ class => 'HTML::Object::SyntaxError' }) );
1478
1479 # We take some care to keep the same original array, so that if it is used or
1480 # referenced elsewhere it continues to be valid, as a 'live' array of (new) elements
1481 $children->foreach(sub
1482 {
1483 $_->parent( undef() );
1484 });
1485 $results->push( $children->list );
1486 # We empty it, and pu the new content inside
1487 $children->reset;
1488
1489 $new->foreach(sub
1490 {
1491 $_->parent( $self );
1492 $children->push( $_ );
1493 });
1494 # Return the old set
1495 return( $results );
1496 }
1497  
1498 sub replaceWith
1499 {
1500 my $self = shift( @_ );
1501 return( $self->error({
1502 message => "No data was provided to replace this element.",
1503 code => 500,
1504 class => 'HTML::Object::SyntaxError',
1505 }) ) if( !scalar( @_ ) );
1506 my $parent = $self->parent;
1507 return( $self->error({
1508 message => "Current object does not have a parent",
1509 code => 500,
1510 class => 'HTML::Object::HierarchyRequestError',
1511 }) ) if( !$parent );
1512 my $new = $self->_list_to_nodes( @_ ) || return( $self->pass_error({ class => 'HTML::Object::SyntaxError' }) );
1513 my $pos = $parent->children->pos( $self );
1514 $parent->children->splice( $pos, 1, $new->list );
1515 $new->foreach(sub
1516 {
1517 $_->parent( $parent );
1518 });
1519 return( $new );
1520 }
1521  
1522 sub scrollHeight { return; }
1523  
1524 sub scrollLeft { return; }
1525  
1526 sub scrollTop { return; }
1527  
1528 sub scrollWidth { return; }
1529  
1530 sub setAttribute
1531 {
1532 my $self = shift( @_ );
1533 my( $name, $value ) = @_;
1534 return( $self->error({
1535 message => "No attribute name was provided.",
1536 code => 500,
1537 class => 'HTML::Object::SyntaxError',
1538 }) ) if( !defined( $name ) || !CORE::length( $name ) );
1539
1540 # Inherited from HTML::Object::Element
1541 if( !$self->is_valid_attribute( $name ) )
1542 {
1543 return( $self->error({
1544 message => "Attribute name provided \"$name\" contains illegal characters.",
1545 code => 500,
1546 class => 'HTML::Object::InvalidCharacterError',
1547 }) );
1548 }
1549 $name = lc( $name );
1550
1551 if( !defined( $value ) )
1552 {
1553 return( $self->removeAttribute( $name ) );
1554 }
1555 $self->attributes->set( $name => $value );
1556 $self->attributes_sequence->push( $name ) if( !$self->attributes_sequence->has( $name ) );
1557 return( $self );
1558 }
1559  
1560 sub setAttributeNode
1561 {
1562 my $self = shift( @_ );
1563 my $att = shift( @_ );
1564 return( $self->error({
1565 message => "No attribute name was provided.",
1566 code => 500,
1567 class => 'HTML::Object::SyntaxError',
1568 }) ) if( !defined( $att ) );
1569 return( $self->error({
1570 message => "Attribute node provided (", overload::StrVal( $att ), ") is not actually an HTML::Object::DOM::Attribute object.",
1571 code => 500,
1572 class => 'HTML::Object::TypeError',
1573 }) ) if( !$self->_is_a( $att => 'HTML::Object::DOM::Attribute' ) );
1574 return( $self->error({
1575 message => "Attribute node object provided has no attribute name set.",
1576 code => 500,
1577 class => 'HTML::Object::SyntaxError',
1578 }) ) if( !$att->name->defined || $att->name->is_empty );
1579 my $old = $self->getAttributeNode( $att->name );
1580 $self->setAttribute( $att->name, $att->value ) || return( $self->pass_error );
1581 return( $old );
1582 }
1583  
1584 # setHTML is a mutator only
1585 sub setHTML : lvalue { return( shift->_set_get_callback({
1586 set => sub
1587 {
1588 my $self = shift( @_ );
1589 my $this = shift( @_ );
1590 if( !defined( $this ) || !CORE::length( $this ) )
1591 {
1592 die( "No html provided." );
1593 }
1594
1595 my $children;
1596 if( $self->_is_a( $this => 'HTML::Object::Element' ) )
1597 {
1598 if( $self->_is_a( $this => 'HTML::Object::DOM::DocumentFragment' ) )
1599 {
1600 $children = $this->children->clone;
1601 $this->children->reset;
1602 # DocumentFragment children are not cloned, but moved as per the specification
1603 $children->foreach(sub
1604 {
1605 $_->detach;
1606 });
1607 }
1608 else
1609 {
1610 my $clone = $this->clone;
1611 $children = $self->new_array( $clone );
1612 $children->push( $clone->close_tag ) if( $clone->close_tag );
1613 }
1614 }
1615 else
1616 {
1617 if( ref( $this ) && ( !$self->_is_object( $this ) || ( $self->_is_object( $this ) && !overload::Method( $this, '""' ) ) ) )
1618 {
1619 die( "I was expecting some HTML data, but got '" . overload::StrVal( $this ) . "'" );
1620 }
1621 my $p = $self->new_parser;
1622 my $res = $p->parse_data( "$this" ) ||
1623 die( "Error while parsing html data provided: " . $p->error );
1624 $children = $res->children;
1625 }
1626 $children->foreach(sub
1627 {
1628 $_->parent( $self );
1629 });
1630 $self->children( $children );
1631 return( $self );
1632 }
1633 }, @_ ) ); }
1634  
1635 sub shadowRoot { return; }
1636  
1637 sub showPopover { return; }
1638  
1639 # Note: spellcheck -> property
1640 sub spellcheck : lvalue { return( shift->_set_get_property( { attribute => 'spellcheck', is_boolean => 1 }, @_ ) ); }
1641  
1642 sub string_value
1643 {
1644 my $self = shift( @_ );
1645 my $type = $self->nodeType;
1646 if( $type == TEXT_NODE || $type == COMMENT_NODE )
1647 {
1648 return( $self->value );
1649 }
1650 else
1651 {
1652 return( $self->as_text );
1653 }
1654 }
1655  
1656 # Note: style -> property
1657 sub style { return( shift->new_hash ); }
1658  
1659 # Note: property
1660 sub tabIndex : lvalue { return( shift->_set_get_property( 'tabindex', @_ ) ); }
1661  
1662 sub tagName { return( shift->getName ); }
1663  
1664 # Note: title -> property
1665 sub title : lvalue { return( shift->_set_get_property( 'title', @_ ) ); }
1666  
1667 sub to_number
1668 {
1669 my $self = shift( @_ );
1670 return( $self->new_number( $self->as_text ) );
1671 }
1672  
1673 # Based on the polyfill provided by Mozilla at:
1674 # <https://developer.mozilla.org/en-US/docs/Web/API/Element/toggleAttribute>
1675 # because, otherwise, as of 2021-12-15, the description of the use of 'force' is cryptic
1676 sub toggleAttribute
1677 {
1678 my $self = shift( @_ );
1679 my( $name, $force ) = @_;
1680 return( $self->error({
1681 message => "No attribute name was provided.",
1682 code => 500,
1683 class => 'HTML::Object::SyntaxError',
1684 }) ) if( !defined( $name ) || !CORE::length( $name ) );
1685
1686 # Inherited from HTML::Object::Element
1687 if( !$self->is_valid_attribute( $name ) )
1688 {
1689 return( $self->error({
1690 message => "Attribute name provided \"$name\" contains illegal characters.",
1691 code => 500,
1692 class => 'HTML::Object::InvalidCharacterError',
1693 }) );
1694 }
1695 $name = lc( $name );
1696 if( $self->attribute->has( $name ) )
1697 {
1698 return( $self->true ) if( defined( $force ) && $force );
1699 $self->removeAttribute( $name );
1700 return( $self->false );
1701 }
1702 return( $self->false ) if( defined( $force ) && !$force );
1703 $self->setAttribute( $name => '' );
1704 return( $self->true );
1705 }
1706  
1707 sub togglePopover { return; }
1708  
1709 sub toString { return( shift->as_string ); }
1710  
1711 # Note: translate -> property
1712 sub translate : lvalue { return( shift->_set_get_property( { attribute => 'translate', is_boolean => 1 }, @_ ) ); }
1713  
1714 # Used by HTML::Object::DOM::Element::*
1715 sub _get_parent_form { return( shift->closest( 'form' ) ); }
1716  
1717 # Note: moved _list_to_nodes to HTML::Object::DOM::Node to make it also available to HTML::Object::DOM::Declaration
1718  
1719 # Used by HTML::Object::DOM::Element::Anchor and HTML::Object::DOM::Element::Area
1720 sub _set_get_anchor_uri
1721 {
1722 my $self = shift( @_ );
1723 my $link = $self->href;
1724 # We constantly get a new URI object, because the value of the href attribute may have been altered by other means
1725 # try-catch
1726 local $@;
1727 my $rv = eval
1728 {
1729 return( $link ) if( $self->_is_a( $link => 'URI' ) );
1730 return( ( defined( $link ) && CORE::length( "$link" ) ) ? URI->new( $link ) : URI->new );
1731 };
1732 if( $@ )
1733 {
1734 return( $self->error( "Unable to create a URI object from \"$link\" (", overload::StrVal( $link ), "): $@" ) );
1735 }
1736 return( $rv );
1737 }
1738  
1739 sub _set_get_form_attribute : lvalue
1740 {
1741 my $self = shift( @_ );
1742 my $attr = shift( @_ );
1743  
1744 return( $self->_set_get_callback({
1745 get => sub
1746 {
1747 my $form = $self->_get_parent_form;
1748 if( !defined( $form ) )
1749 {
1750 return;
1751 }
1752 my $code = $form->can( $attr );
1753 if( !defined( $code ) )
1754 {
1755 die( "Form object has no method \"$attr\"." );
1756 }
1757
1758 my $rv = $code->( $form );
1759 return( $rv );
1760 },
1761 set => sub
1762 {
1763 my $arg = shift( @_ );
1764 my $ctx = $_;
1765 my $form = $self->_get_parent_form;
1766 if( !defined( $form ) )
1767 {
1768 return;
1769 }
1770 my $code = $form->can( $attr );
1771 if( !defined( $code ) )
1772 {
1773 die( "Form object has no method \"$attr\"." );
1774 }
1775
1776 $code->( $form, $arg );
1777 return( $arg ) if( $ctx->{assign} );
1778 return( $self );
1779 },
1780 }, @_ ) );
1781 }
1782  
1783 # _set_get_property has been moved up in HTML::Object::Element
1784 # Note: private method to set or get attribute as an lvalue method for DOM properties in HTML::Object::DOM::Element::* and also for some DOM List abstract class like HTML::Object::DOM::List
1785 sub _set_get_property : lvalue
1786 {
1787 my $self = shift( @_ );
1788 my $attr = shift( @_ );
1789
1790 my $def = {};
1791 # If the $attr parameter is an hash reference, it is used to provide more information
1792 # such as whether this property is a boolean
1793 if( ref( $attr ) eq 'HASH' )
1794 {
1795 $def = $attr;
1796 $attr = $def->{attribute};
1797 }
1798 $def->{is_boolean} //= 0;
1799
1800 return( $self->_set_get_callback({
1801 get => sub
1802 {
1803 my $self = shift( @_ );
1804 if( $def->{is_datetime} )
1805 {
1806 my $val = $self->attr( $attr );
1807 # try-catch
1808 local $@;
1809 my $rv = eval
1810 {
1811 my $dt = $self->_parse_timestamp( $val );
1812 return( $self->pass_error ) if( !defined( $dt ) );
1813 return( $dt );
1814 };
1815 if( $@ )
1816 {
1817 return( $self->error( "Unable to parse datetime value \"$val\": $@" ) );
1818 }
1819 return( $rv );
1820 }
1821 elsif( $def->{is_number} )
1822 {
1823 my $val = $self->attr( $attr );
1824 return if( !$self->_is_number( $val ) );
1825 if( $val =~ /^(\d{1,10})(?:\.\d+)?$/ )
1826 {
1827 my $dt = $self->_parse_timestamp( $val );
1828 return( $dt ) if( ref( $dt ) );
1829 }
1830 return( $self->new_number( $val ) );
1831 }
1832 elsif( $def->{is_uri} )
1833 {
1834 my $val = $self->attr( $attr );
1835 # We constantly get a new URI object, because the value of the href attribute may have been altered by other means
1836 # try-catch
1837 local $@;
1838 my $rv = eval
1839 {
1840 return( $val ) if( $self->_is_a( $val => 'URI' ) );
1841 return if( !defined( $val ) );
1842 return( $val ) if( !CORE::length( "$val" ) );
1843 return( URI->new( "$val" ) );
1844 };
1845 if( $@ )
1846 {
1847 return( $self->error( "Unable to create a URI object from \"$val\" (", overload::StrVal( $val ), "): $@" ) );
1848 }
1849 return( $rv );
1850 }
1851 elsif( $def->{callback} && ref( $def->{callback} ) eq 'CODE' )
1852 {
1853 return( $def->{callback}->( $self, $attr ) );
1854 }
1855 return( $self->attr( $attr ) );
1856 },
1857 set => sub
1858 {
1859 my $self = shift( @_ );
1860 my $arg = shift( @_ );
1861 if( $def->{is_boolean} )
1862 {
1863 # Any true value works, even in the web browser
1864 if( $arg )
1865 {
1866 # it is ok to set an empty value
1867 $self->attr( $attr => '' );
1868 }
1869 else
1870 {
1871 # Passing undef implies it will be removed. See HTML::Object::Element
1872 $self->attr( $attr => undef );
1873 }
1874 }
1875 elsif( $def->{is_datetime} )
1876 {
1877 $self->attr( $attr => "$arg" );
1878 }
1879 # form target
1880 elsif( $def->{is_uri} )
1881 {
1882 # try-catch
1883 local $@;
1884 eval
1885 {
1886 my $uri = URI->new( $arg );
1887 $self->attr( $attr => $uri );
1888 };
1889 if( $@ )
1890 {
1891 die( "Unable to create an URI with \"$arg\": $@" );
1892 }
1893 }
1894 # Used for <option>
1895 elsif( $def->{callback} && ref( $def->{callback} ) eq 'CODE' )
1896 {
1897 $def->{callback}->( $self, $attr => $arg );
1898 }
1899 else
1900 {
1901 $self->attr( $attr => $arg );
1902 }
1903 $self->reset(1);
1904 return( $arg );
1905 }
1906 }, @_ ) );
1907 }
1908  
1909 # Used by HTML::Object::DOM::Element::Anchor and HTML::Object::DOM::Element::Area
1910 sub _set_get_uri_property : lvalue
1911 {
1912 my $self = shift( @_ );
1913 my $prop = shift( @_ );
1914 my $uri = $self->_set_get_anchor_uri;
1915 my $map =
1916 {
1917 hash => 'fragment',
1918 # URI's host_port is tolerant just like DOM's host is. Even if no port is provided, it will not complain
1919 host => 'host_port',
1920 hostname => 'host',
1921 pathname => 'path',
1922 password => 'userinfo',
1923 protocol => 'scheme',
1924 search => 'query',
1925 username => 'userinfo',
1926 };
1927
1928 return( $self->_set_get_callback({
1929 get => sub
1930 {
1931 my $self = shift( @_ );
1932 # If there is an URI, we use it as a alue storage
1933 # It is convenient and let the user modify it directly if he wants.
1934 if( ref( $uri ) )
1935 {
1936 my $meth = exists( $map->{ $prop } ) ? $map->{ $prop } : $prop;
1937 my $code = $uri->can( $meth );
1938 # User trying to access URI method like host port, etc on a generic URI
1939 # which is ok for method like path, query, fragment
1940 # So we convert what would otherwise be an error into an undef returned, meaning no value
1941 if( !defined( $code ) )
1942 {
1943 if( $uri->isa( 'URI::_generic' ) )
1944 {
1945 return( $self->{ $prop } );
1946 }
1947 else
1948 {
1949 return( $self->error( "URI object has no method \"$meth\"." ) );
1950 }
1951 }
1952 # try-catch
1953 local $@;
1954 my $val = eval
1955 {
1956 $code->( $uri );
1957 };
1958 if( $@ )
1959 {
1960 die( "Unable to get value for URI method \"${prop}\": $@" );
1961 }
1962 # We assign the value from the URI method in case, the user would have modified the URI object directly
1963 # We need to stay synchronised.
1964 if( $prop eq 'username' || $prop eq 'password' )
1965 {
1966 if( defined( $val ) )
1967 {
1968 @$self{qw( username password )} = split( /:/, $val, 2 );
1969 }
1970 else
1971 {
1972 $self->{username} = undef;
1973 $self->{password} = undef;
1974 }
1975 return( $self->{ $prop } );
1976 }
1977 # We add back the colon, because URI stores the scheme without it, but our 'protocol' method returns the scheme with it.
1978 elsif( $prop eq 'protocol' )
1979 {
1980 $val .= ':' if( defined( $val ) );
1981 }
1982 elsif( $prop eq 'hash' )
1983 {
1984 substr( $val, 0, 0, '#' ) if( defined( $val ) );
1985 }
1986 elsif( $prop eq 'search' )
1987 {
1988 substr( $val, 0, 0, '?' ) if( defined( $val ) );
1989 }
1990 return( $self->{ $prop } = $val );
1991 }
1992 return( $self->{ $prop } );
1993 },
1994 set => sub
1995 {
1996 my $self = shift( @_ );
1997 my $arg = shift( @_ );
1998 $self->{ $prop } = $arg;
1999 if( ref( $uri ) )
2000 {
2001 my $uri_class = ref( $uri ); # URI::https or maybe URI::_generic ?
2002 if( $prop eq 'username' || $prop eq 'password' )
2003 {
2004 no warnings 'uninitialized';
2005 $arg = join( ':', @$self{ qw( username password ) } );
2006 }
2007 elsif( $prop eq 'protocol' )
2008 {
2009 # Remove the trailing colon, because URI scheme method takes it without it
2010 $arg =~ s/\:$//;
2011 }
2012 elsif( $prop eq 'hash' )
2013 {
2014 $arg =~ s/^\#//;
2015 }
2016 elsif( $prop eq 'search' )
2017 {
2018 $arg =~ s/^\?//;
2019 }
2020 my $meth = exists( $map->{ $prop } ) ? $map->{ $prop } : $prop;
2021 my $code = $uri->can( $meth );
2022 # User trying to access URI method like host port, etc on a generic URI
2023 # which is ok for method like path, query, fragment
2024 # So we convert what would otherwise be an error into an undef returned, meaning no value
2025 if( !defined( $code ) )
2026 {
2027 if( $uri->isa( 'URI::_generic' ) )
2028 {
2029 return( $self->{ $prop } );
2030 }
2031 else
2032 {
2033 return( $self->error( "URI object has no method \"$meth\"." ) );
2034 }
2035 }
2036 # try-catch
2037 local $@;
2038 eval
2039 {
2040 $code->( $uri, $arg );
2041 # If the URI object was generic and we switched it to a non-generic one by setting the schem
2042 # We also set other properties if we have them
2043 if( $prop eq 'protocol' && $uri_class eq 'URI::_generic' )
2044 {
2045 if( $self->{hostname} )
2046 {
2047 $uri->host_port( $self->{hostname} );
2048 }
2049 elsif( $self->{host} || $self->{port} )
2050 {
2051 $uri->host( $self->{host} ) if( $self->{host} );
2052 $uri->port( $self->{port} ) if( $self->{port} );
2053 }
2054 if( $self->{username} || $self->{password} )
2055 {
2056 $uri->userinfo( join( ':', @$self{qw( username password )} ) );
2057 }
2058 }
2059 $self->attr( href => $uri );
2060 };
2061 if( $@ )
2062 {
2063 die( "Unable to set value \"${arg}\" for URI method \"${prop}\": $@" );
2064 }
2065 }
2066 $self->reset(1);
2067 $self->{ $prop } = $arg;
2068 $self->attr( href => $uri );
2069 return( $arg );
2070 }
2071 }, @_ ) );
2072 }
2073  
2074 1;
2075 # NOTE: POD
2076 __END__
2077  
2078 =encoding utf-8
2079  
2080 =head1 NAME
2081  
2082 HTML::Object::DOM::Element - HTML Object
2083  
2084 =head1 SYNOPSIS
2085  
2086 use HTML::Object::DOM::Element;
2087 my $this = HTML::Object::DOM::Element->new ||
2088 die( HTML::Object::DOM::Element->error );
2089  
2090 =head1 VERSION
2091  
2092 v0.4.0
2093  
2094 =head1 DESCRIPTION
2095  
2096 This module represents an HTML element and contains also all the methods for L<DOM nodes|https://developer.mozilla.org/en-US/docs/Web/API/Node>. It is inherited by all other element objects in a L<document|HTML::Object::Document>.
2097  
2098 If you want to get the children elements (i.e. children excluding text, comments and space) from any given tag, you want to do something like this:
2099  
2100 use HTML::Object::DOM ':node'; # Load up the node related constants
2101 # This will return an array object of HTML::Object::DOM::Element objects only.
2102 my $array_ref_of_elements = $parent_element->children->grep(sub{ $_->nodeType == ELEMENT_NODE });
2103  
2104 This module inherits from L<HTML::Object::Node> and is extended by L<HTML::Object::XQuery>
2105  
2106 =head1 INHERITANCE
2107  
2108 +-----------------------+ +---------------------------+ +-------------------------+ +----------------------------+
2109 | HTML::Object::Element | --> | HTML::Object::EventTarget | --> | HTML::Object::DOM::Node | --> | HTML::Object::DOM::Element |
2110 +-----------------------+ +---------------------------+ +-------------------------+ +----------------------------+
2111  
2112 =head1 PROPERTIES
2113  
2114 All the following properties can be used as lvalue method as well as regular method. For example with L</baseURI>
2115  
2116 =head2 accessKey
2117  
2118 A string representing the access key assigned to the element.
2119  
2120 See L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/accessKey> for more information.
2121  
2122 =head2 accessKeyLabel
2123  
2124 my $label = $element->accessKeyLabel;
2125  
2126 my $btn = $document->getElementById("btn1");
2127 my $shortcutLabel = $btn->accessKeyLabel || $btn->accessKey;
2128 $btn->title .= " [" . uc( $shortcutLabel ) . "]";
2129  
2130 Read-only
2131  
2132 Returns a string containing the element's assigned access key.
2133  
2134 See L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/accessKeyLabel> for more information.
2135  
2136 =head2 attributeStyleMap
2137  
2138 Sets or gets the C<style> attribute.
2139  
2140 Normally, this is read-only, and represents a C<StylePropertyMap> representing the declarations of the element's style attribute.
2141  
2142 See L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/attributeStyleMap> for more information.
2143  
2144 =head2 baseURI
2145  
2146 # Get the base uri, if any
2147 my $uri = $e->baseURI;
2148 $e->baseURI = 'https://example.org/some/where';
2149 # or
2150 $e->baseURI( 'https://example.org/some/where' );
2151  
2152 Read-only
2153  
2154 This returns an L<URI> object representing the base URL of the document containing the Node, if any.
2155  
2156 =head2 childElementCount
2157  
2158 Read-only
2159  
2160 Returns the number of child elements of this element.
2161  
2162 =head2 childNodes
2163  
2164 Read-only
2165  
2166 This returns an L<array object|Module::Generic::Array> containing all the children of this node (including elements, text and comments). This list being live means that if the children of the Node change, the L<list object|Module::Generic::Array> is automatically updated.
2167  
2168 =head2 classList
2169  
2170 The C<classList> property is a read-only property that returns a live L<HTML::Object::TokenList> collection of the class attributes of the element. This can then be used to manipulate the class list.
2171  
2172 Using classList is a convenient alternative to accessing an element's list of classes as a space-delimited string via L</className>
2173  
2174 It returns a L<HTML::Object::TokenList> object representing the contents of the element's class attribute. If the class attribute is not set or empty, it returns an empty L<HTML::Object::TokenList>, i.e. a L<HTML::Object::TokenList> with the L<length|HTML::Object::TokenList/length> property equal to 0.
2175  
2176 Although the classList property itself is read-only, you can modify its associated L<HTML::Object::TokenList> using the L<add()|HTML::Object::TokenList/add>, L<remove()|HTML::Object::TokenList/add>, L<replace()|HTML::Object::TokenList/add>, and L<toggle()|HTML::Object::TokenList/add> methods.
2177  
2178 For example:
2179  
2180 my $div = $doc->createElement('div');
2181 # use the classList API to remove and add classes
2182 $div->classList->remove("foo");
2183 $div->classList->add("anotherclass");
2184 say $div->outerHTML; # <div class="anotherclass"></div>
2185 # if visible is set remove it, otherwise add it
2186 $div->classList->toggle("visible");
2187 $div->classList->contains("foo");
2188 # add or remove multiple classes
2189 $div->classList->add("foo", "bar", "baz");
2190 $div->classList->remove("foo", "bar", "baz");
2191 # replace class "foo" with class "bar"
2192 $div->classList->replace("foo", "bar");
2193  
2194 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/Element/classList>
2195  
2196 =head2 className
2197  
2198 Set or get the element class.
2199  
2200 Returns a string representing the class of the element.
2201  
2202 This method is an lvalue method, so you can assign value like this:
2203  
2204 $e->className = "my-class";
2205  
2206 See L<for more information|https://developer.mozilla.org/en-US/docs/Web/API/Element/className>
2207  
2208 =head2 clientHeight
2209  
2210 Read-only.
2211  
2212 This always return C<undef> since this has no meaning under perl.
2213  
2214 Normally, under JavaScript, this would return a number representing the inner height of the element.
2215  
2216 See L<for more information|https://developer.mozilla.org/en-US/docs/Web/API/Element/clientHeight>
2217  
2218 =head2 clientLeft
2219  
2220 This always return C<undef> since this has no meaning under perl.
2221  
2222 Normally, under JavaScript, this would return a number representing the width of the left border of the element.
2223  
2224 See L<for more information|https://developer.mozilla.org/en-US/docs/Web/API/Element/clientLeft>
2225  
2226 =head2 clientTop
2227  
2228 This always return C<undef> since this has no meaning under perl.
2229  
2230 Normally, under JavaScript, this would return a number representing the width of the top border of the element.
2231  
2232 See L<for more information|https://developer.mozilla.org/en-US/docs/Web/API/Element/clientTop>
2233  
2234 =head2 clientWidth
2235  
2236 This always return C<undef> since this has no meaning under perl.
2237  
2238 Normally, under JavaScript, this would return a number representing the inner width of the element.
2239  
2240 See L<for more information|https://developer.mozilla.org/en-US/docs/Web/API/Element/clientWidth>
2241  
2242 =head2 contentEditable
2243  
2244 Set or get the boolean value where true means the element is editable and a value of false means it is not. Defautls to true.
2245  
2246 $e->contentEditable = 0; # turn off content editability
2247  
2248 See L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/contentEditable> for more information.
2249  
2250 =head2 dataset
2251  
2252 <div id="user" data-id="1234567890" data-user="carinaanand" data-date-of-birth>
2253 Carina Anand
2254 </div>
2255  
2256 var $el = $document->getElementById('user');
2257  
2258 # $el->id eq 'user';
2259 # $el->dataset->id eq '1234567890';
2260 # $el->dataset->user eq 'carinaanand';
2261 # $el->dataset->dateOfBirth eq '';
2262  
2263 # set a data attribute
2264 $el->dataset->dateOfBirth = "1960-10-03";
2265 # <div id="user" data-id="1234567890" data-user="carinaanand" data-date-of-birth="1960-10-03">Carina Anand</div>
2266  
2267 Read-only
2268  
2269 Returns an L<HTML::Object::ElementDataMap> object with which script can read and write the element's custom data attributes (data-*).
2270  
2271 The attribute name begins with data-. It can contain only letters, numbers, dashes (-), periods (.), colons (:), and underscores (_). Any ASCII capital letters (A to Z) are converted to lowercase.
2272  
2273 Name conversion
2274  
2275 dash-style to camelCase conversion
2276  
2277 A custom data attribute name is transformed to a key for the DOMStringMap entry by the following:
2278  
2279 =over 4
2280  
2281 =item 1. Lowercase all ASCII capital letters (A to Z);
2282  
2283 =item 2. Remove the prefix data- (including the dash);
2284  
2285 =item 3. For any dash (U+002D) followed by an ASCII lowercase letter a to z, remove the dash and uppercase the letter;
2286  
2287 =item 4. Other characters (including other dashes) are left unchanged.
2288  
2289 =back
2290  
2291 camelCase to dash-style conversion
2292  
2293 The opposite transformation, which maps a key to an attribute name, uses the following:
2294  
2295 =over 4
2296  
2297 =item 1. Restriction: Before transformation, a dash must not be immediately followed by an ASCII lowercase letter a to z;
2298  
2299 =item 2. Add the data- prefix;
2300  
2301 =item 3. Add a dash before any ASCII uppercase letter A to Z, then lowercase the letter;
2302  
2303 =item 4. Other characters are left unchanged.
2304  
2305 =back
2306  
2307 For example, a data-abc-def attribute corresponds to C<<$dataset->abcDef>>.
2308  
2309 See L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/dataset> for more information.
2310  
2311 =head2 dir
2312  
2313 my $parg = $document->getElementById("para1");
2314 $parg->dir = "rtl";
2315 # change the text direction on a paragraph identified as "para1"
2316  
2317 A string, reflecting the dir global attribute, representing the directionality of the element. Possible values are:
2318  
2319 =over 4
2320  
2321 =item * C<ltr>
2322  
2323 for left-to-right;
2324  
2325 =item * C<rtl>
2326  
2327 for right-to-left;
2328  
2329 =item * C<auto>
2330  
2331 for specifying that the direction of the element must be determined based on the contents of the element.
2332  
2333 =back
2334  
2335 See L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/dir> for more information.
2336  
2337 =head2 draggable
2338  
2339 A boolean value indicating if the element can be dragged.
2340  
2341 See L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/draggable> for more information.
2342  
2343 =head2 enterKeyHint
2344  
2345 A string defining what action label (or icon) to present for the enter key on virtual keyboards.
2346  
2347 See L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/enterKeyHint> for more information.
2348  
2349 =head2 firstChild
2350  
2351 Read-only
2352  
2353 This returns an element representing the first direct child element of the element, or C<undef> if the element has no child.
2354  
2355 =head2 firstElementChild
2356  
2357 Read-only.
2358  
2359 It returns the first child element of this element.
2360  
2361 See L<for more information|https://developer.mozilla.org/en-US/docs/Web/API/Element/firstElementChild>
2362  
2363 =head2 hidden
2364  
2365 A string or boolean value reflecting the value of the element's hidden attribute.
2366  
2367 See L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/hidden> for more information.
2368  
2369 =head2 id
2370  
2371 Set or get an string representing the id of the element.
2372  
2373 # Set it as a regular method
2374 $e->id( 'hello' );
2375 # Set it as a lvalue method
2376 $e->id = 'hello';
2377 # Retrieve it
2378 my $id = $e->id;
2379  
2380 See L<for more information|https://developer.mozilla.org/en-US/docs/Web/API/Element/id>
2381  
2382 =head2 innerHTML
2383  
2384 Set or get the element's content. This returns a string representing the markup of the element's content.
2385  
2386 Se L<for more information|https://developer.mozilla.org/en-US/docs/Web/API/Element/innerHTML>
2387  
2388 =head2 inert
2389  
2390 A boolean value indicating whether the user agent must act as though the given node is absent for the purposes of user interaction events, in-page text searches (C<find in page>), and text selection.
2391  
2392 See L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/inert> for more information.
2393  
2394 =head2 innerText
2395  
2396 Represents the rendered text content of a node and its descendants. As a getter, it approximates the text the user would get if they highlighted the contents of the element with the cursor and then copied it to the clipboard. This returns a L<string object|Module::Generic::Scalar>. As a setter, it replaces the content inside the selected element, with either a L<text object|HTML::Object::DOM::Text> or by a string converting any line breaks into C<<br />> elements.
2397  
2398 When setting some values, this method will ensure that HTML characters are escaped, namely: C<< < >>, C<< > >> and new lines are preceded by the C<< <br /> >> tag.
2399  
2400 When the value is retrieved, this is reversed.
2401  
2402 See L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/innerText> for more information.
2403  
2404 See also L<HTML::Object::DOM::Node/textContent>, L<HTML::Object::Element/as_text> and L<HTML::Object::XQuery/text>
2405  
2406 =head2 inputMode
2407  
2408 A string value reflecting the value of the element's inputmode attribute.
2409  
2410 See L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/inputMode> for more information.
2411  
2412 =head2 isConnected
2413  
2414 Returns a boolean indicating whether or not the element is connected (directly or indirectly) to the context object, i.e. the L<Document object|HTML::Object::Document> in the case of the normal DOM.
2415  
2416 See L<for more information|https://developer.mozilla.org/en-US/docs/Web/API/Node/isConnected>
2417  
2418 =head2 isContentEditable
2419  
2420 <p id="firstParagraph">Uneditable Paragraph</p>
2421 <p id="secondParagraph" contenteditable="true">Editable Paragraph</p>
2422  
2423 my $firstParagraph = $document->getElementById("firstParagraph");
2424 my $secondParagraph = $document->getElementById("secondParagraph");
2425  
2426 Read-only
2427  
2428 Returns a L<boolean value|HTML::Object::Boolean> indicating whether or not the content of the element can be edited. Use L<contentEditable> to change the value.
2429  
2430 See L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/isContentEditable> for more information.
2431  
2432 =head2 lang
2433  
2434 A string representing the language of an element's attributes, text, and element contents.
2435  
2436 See L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/lang> for more information.
2437  
2438 =head2 lastChild
2439  
2440 Read-only
2441  
2442 This returns an element representing the last direct child element of the element, or C<undef> if the element has no child.
2443  
2444 See L<for more information|https://developer.mozilla.org/en-US/docs/Web/API/Node/lastChild>
2445  
2446 =head2 lastElementChild
2447  
2448 Read-only
2449  
2450 Returns the last child element of this element, if any at all.
2451  
2452 See L<for more information|https://developer.mozilla.org/en-US/docs/Web/API/Element/lastElementChild>
2453  
2454 =head2 localName
2455  
2456 Read-only
2457  
2458 A string representing the local part of the qualified name of the element. This is basically the tag name. This has a special meaning only when using xml, which we do not. So this is just an alias to L</getName>
2459  
2460 See L<for more information|https://developer.mozilla.org/en-US/docs/Web/API/Element/localName>
2461  
2462 =head2 namespaceURI
2463  
2464 Read-only
2465  
2466 The namespace URI of the element, or C<undef> if it is no namespace.
2467  
2468 This always return C<undef>, because as HTML, we do not deal with namespace, which is used primarily under xml.
2469  
2470 See L<for more information|https://developer.mozilla.org/en-US/docs/Web/API/Element/namespaceURI>
2471  
2472 =head2 nextElementSibling
2473  
2474 Read-only
2475  
2476 Is an L<Element|HTML::Object::Element>, the element immediately following the given one in the tree, or C<undef> if there's no sibling node.
2477  
2478 See L<for more information|https://developer.mozilla.org/en-US/docs/Web/API/Element/nextElementSibling>
2479  
2480 =head2 nextSibling
2481  
2482 Read-only
2483  
2484 This returns an element representing the next element in the tree, or C<undef> if there is not such element.
2485  
2486 The next node could also be a whitespace or a text. If you want to get the next element and not just any node, use L<nextElementSibling|HTML::Object::DOM/nextElementSibling> instead.
2487  
2488 =head2 nodeName
2489  
2490 Read-only
2491  
2492 This returns a string containing the name of the element. The structure of the name will differ with the element type. E.g. An L<HTML Element|HTML::Object::Element> will contain the name of the corresponding tag, like 'audio' for an HTML audio element, a L<Text|HTML::Object::Text> element will have the '#text' string, or a L<Document|HTML::Object::Document> element will have the '#document' string.
2493  
2494 =head2 nodeType
2495  
2496 Read-only
2497  
2498 This returns an integer representing the type of the element. Possible values are:
2499  
2500 =over 4
2501  
2502 =item 1. element node
2503  
2504 =item 2. attribute node
2505  
2506 =item 3. text node
2507  
2508 =item 4. CDATA section node
2509  
2510 =item 5. unused
2511  
2512 =item 6. unsued
2513  
2514 =item 7. processing instruction node
2515  
2516 =item 8. comment node
2517  
2518 =item 9. document node
2519  
2520 =item 10. document type node
2521  
2522 =item 11. document fragment node
2523  
2524 =back
2525  
2526 See L<for more information|https://developer.mozilla.org/en-US/docs/Web/API/Node/nodeType>
2527  
2528 =head2 nodeValue
2529  
2530 This returns or sets the value of the current node.
2531  
2532 For document, element or collection, this returns C<undef> and for attribute, text or comment, this sets or returns the objct value.
2533  
2534 See L<for more information|https://developer.mozilla.org/en-US/docs/Web/API/Node/nodeValue>
2535  
2536 =head2 noModule
2537  
2538 A boolean value indicating whether an import script can be executed in user agents that support module scripts.
2539  
2540 See L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/noModule> for more information.
2541  
2542 =head2 nonce
2543  
2544 This returns nothing.
2545  
2546 Normally, under JavaScript, this would return the cryptographic number used once that is used by Content Security Policy to determine whether a given fetch will be allowed to proceed.
2547  
2548 See L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/nonce> for more information.
2549  
2550 =head2 offsetHeight
2551  
2552 Sets or gets the property C<offsetheight>.
2553  
2554 Normally, under JavaScript, this would be read-only and return a double containing the height of an element, relative to the layout.
2555  
2556 See L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/offsetHeight> for more information.
2557  
2558 =head2 offsetLeft
2559  
2560 Sets or gets the property C<offsetleft>.
2561  
2562 Normally, under JavaScript, this would be read-only and return a double, the distance from this element's left border to its offsetParent's left border.
2563  
2564 See L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/offsetLeft> for more information.
2565  
2566 =head2 offsetParent
2567  
2568 Sets or gets the property C<offsetparent>.
2569  
2570 Normally, under JavaScript, this would be read-only and return Element that is the element from which all offset calculations are currently computed.
2571  
2572 See L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/offsetParent> for more information.
2573  
2574 =head2 offsetTop
2575  
2576 Sets or gets the property C<offsettop>.
2577  
2578 Normally, under JavaScript, this would be read-only and return a double, the distance from this element's top border to its offsetParent's top border.
2579  
2580 See L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/offsetTop> for more information.
2581  
2582 =head2 offsetWidth
2583  
2584 Sets or gets the property C<offsetwidth>.
2585  
2586 Normally, under JavaScript, this would be read-only and return a double containing the width of an element, relative to the layout.
2587  
2588 See L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/offsetWidth> for more information.
2589  
2590 =head2 outerHTML
2591  
2592 Returns a string representing the markup of the element including its content.
2593 When used as a setter, replaces the element with nodes parsed from the given string.
2594  
2595 See L<for more information|https://developer.mozilla.org/en-US/docs/Web/API/Element/outerHTML>
2596  
2597 =head2 outerText
2598  
2599 Represents the rendered text content of a node and its descendants.
2600  
2601 As a getter, it is the same as L</innerText> (it represents the rendered text content of an element and its descendants).
2602  
2603 As a setter, it replaces the selected node and its contents with the given value, converting any line breaks into C<<br />> elements.
2604  
2605 See L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/outerText> for more information.
2606  
2607 =head2 ownerDocument
2608  
2609 Read-only
2610  
2611 This returns the L<Document|HTML::Object::Document> that this element belongs to. If the element is itself a document, returns C<undef>.
2612  
2613 See L<for more information|https://developer.mozilla.org/en-US/docs/Web/API/Node/ownerDocument>
2614  
2615 =head2 parentNode
2616  
2617 Read-only
2618  
2619 This returns an element that is the parent of this element. If there is no such element, like if this element is the top of the tree or if does not participate in a tree, this property returns C<undef>.
2620  
2621 See L<for more information|https://developer.mozilla.org/en-US/docs/Web/API/Node/parentNode>
2622  
2623 =head2 parentElement
2624  
2625 Read-only
2626  
2627 This returns an element that is the parent of this element. If the element has no parent, or if that parent is not an Element, this property returns C<undef>.
2628  
2629 See L<for more information|https://developer.mozilla.org/en-US/docs/Web/API/Node/parentElement>
2630  
2631 =head2 part
2632  
2633 This always returns C<undef>, because it has no meaning under perl.
2634  
2635 Normally, under JavaScript, this would be a part that represents the part identifier(s) of the element (i.e. set using the part attribute), returned as a DOMTokenList.
2636  
2637 See L<for more information|https://developer.mozilla.org/en-US/docs/Web/API/Element/outerHTML>
2638  
2639 =head2 popover
2640  
2641 Experimental
2642  
2643 Gets and sets an element's popover state via JavaScript (C<auto> or C<manual>), and can be used for feature detection. Reflects the value of the popover global HTML attribute.
2644  
2645 If no value are set, this will return the one of the HTML attribute.
2646  
2647 See L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/popover> for more information.
2648  
2649 =head2 prefix
2650  
2651 Read-only
2652  
2653 This always return C<undef>
2654  
2655 Normally, for xml documents, this would be a string representing the namespace prefix of the element, or C<undef> if no prefix is specified.
2656  
2657 See L<for more information|https://developer.mozilla.org/en-US/docs/Web/API/Element/prefix>
2658  
2659 =head2 previousElementSibling
2660  
2661 Read-only
2662  
2663 Returns an Element, the element immediately preceding the given one in the tree, or C<undef> if there is no sibling element.
2664  
2665 See L<for more information|https://developer.mozilla.org/en-US/docs/Web/API/Element/previousElementSibling>
2666  
2667 =head2 previousSibling
2668  
2669 Read-only
2670  
2671 This returns a element representing the previous element in the tree, or C<undef> if there is not such element.
2672  
2673 The previous node could also be a whitespace or a text. If you want to get the previous element and not just any node, use L<previousElementSibling|HTML::Object::DOM/previousElementSibling> instead.
2674  
2675 See L<for more information|https://developer.mozilla.org/en-US/docs/Web/API/Node/previousSibling>
2676  
2677 =head2 properties
2678  
2679 This does nothing, but return an empty L<array object|Module::Generic::Array>
2680  
2681 See L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/properties> for more information.
2682  
2683 =head2 scrollHeight
2684  
2685 Read-only
2686  
2687 This always return C<undef> as this is not applicable under perl.
2688  
2689 Normally, under JavaScript, this would return a number representing the scroll view height of an element.
2690  
2691 See L<for more information|https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollHeight>
2692  
2693 =head2 scrollLeft
2694  
2695 This always return C<undef> as this is not applicable under perl.
2696  
2697 Normally, under JavaScript, this would set or return a number representing the left scroll offset of the element.
2698  
2699 See L<for more information|https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollLeft>
2700  
2701 =head2 scrollTop
2702  
2703 This always return C<undef> as this is not applicable under perl.
2704  
2705 Normally, under JavaScript, this would set or return a number representing number of pixels the top of the document is scrolled vertically.
2706  
2707 See L<for more information|https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollTop>
2708  
2709 =head2 scrollWidth
2710  
2711 Read-only
2712  
2713 This always return C<undef> as this is not applicable under perl.
2714  
2715 Normally, under JavaScript, this would return a number representing the scroll view width of the element.
2716  
2717 See L<for more information|https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollWidth>
2718  
2719 =head2 setHTML
2720  
2721 Parses and sanitizes a string of HTML and inserts into the DOM as a subtree of the element.
2722  
2723 See L<for more information|https://developer.mozilla.org/en-US/docs/Web/API/Element/setHTML>
2724  
2725 =head2 shadowRoot
2726  
2727 Read-only
2728  
2729 Always returns C<undef>
2730  
2731 Normally, under JavaScript, this would return the open shadow root that is hosted by the element, or null if no open shadow root is present.
2732  
2733 See L<for more information|https://developer.mozilla.org/en-US/docs/Web/API/Element/shadowRoot>
2734  
2735 =head2 spellcheck
2736  
2737 A boolean value that controls spell-checking. It is present on all HTML elements, though it doesn't have an effect on all of them.
2738  
2739 See L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/spellcheck> for more information.
2740  
2741 =head2 style
2742  
2743 This does nothing, but return a new empty L<hash object|Module::Generic::Hash>
2744  
2745 Normally, this would set or get A C<CSSStyleDeclaration> representing the declarations of the element's style attribute.
2746  
2747 See L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/style> for more information.
2748  
2749 =head2 tabIndex
2750  
2751 The tabIndex property represents the tab order of the current element.
2752  
2753 Tab order is as follows:
2754  
2755 =over
2756  
2757 =item 1. Elements with a positive tabIndex. Elements that have identical tabIndex values should be navigated in the order they appear. Navigation proceeds from the lowest tabIndex to the highest tabIndex.
2758  
2759 =item 2. Elements that do not support the tabIndex attribute or support it and assign tabIndex to 0, in the order they appear.
2760  
2761 =back
2762  
2763 Elements that are disabled do not participate in the tabbing order.
2764  
2765 Values do not need to be sequential, nor must they begin with any particular value. They may even be negative, though each browser trims very large values.
2766  
2767 See L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/tabIndex>
2768  
2769 =head2 tagName
2770  
2771 Read-only. This is merely an alias for L</getName>
2772  
2773 This returns a string with the name of the tag for the given element.
2774  
2775 See L<for more information|https://developer.mozilla.org/en-US/docs/Web/API/Element/tagName>
2776  
2777 =head2 textContent
2778  
2779 This returns or sets the textual content of an element and all its descendants.
2780  
2781 Example:
2782  
2783 <div id="divA">This is <span>some</span> text!</div>
2784  
2785 my $text = $doc->getElementById('divA')->textContent;
2786 # The text variable is now: 'This is some text!'
2787
2788 $doc->getElementById('divA')->textContent = 'This text is different!';
2789 # The HTML for divA is now:
2790 # <div id="divA">This text is different!</div>
2791  
2792 See L<for more information|https://developer.mozilla.org/en-US/docs/Web/API/Node/textContent>
2793  
2794 =head2 title
2795  
2796 A string containing the text that appears in a popup box when mouse is over the element.
2797  
2798 See L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/title> for more information.
2799  
2800 =head2 translate
2801  
2802 A boolean value representing the translation.
2803  
2804 See L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/translate> for more information.
2805  
2806 =head1 METHODS
2807  
2808 =head2 addEventListener
2809  
2810 Registers an event handler to a specific event type on the element. This is inherited from L<HTML::Object::EventTarget>
2811  
2812 See L<HTML::Object::EventTarget/addEventListener> for more information.
2813  
2814 =head2 after
2815  
2816 Inserts a list of L<element|HTML::Object::Element> or HTML string in the L<children|/children> list of the L<element|HTML::Object::Element>'s parent, just after the L<element|HTML::Object::Element>.
2817  
2818 For example:
2819  
2820 Inserting an element:
2821  
2822 my $container = $doc->createElement("div");
2823 my $p = $doc->createElement("p");
2824 $container->appendChild( $p );
2825 my $span = $doc->createElement("span");
2826  
2827 $p->after( $span );
2828  
2829 say( $container->outerHTML );
2830 # "<div><p></p><span></span></div>"
2831  
2832 Inserting an element and text
2833  
2834 my $container = $doc->createElement("div");
2835 my $p = $doc->createElement("p");
2836 $container->appendChild( $p );
2837 my $span = $doc->createElement("span");
2838  
2839 $p->after( $span, "Text" );
2840  
2841 say( $container->outerHTML );
2842 # "<div><p></p><span></span>Text</div>"
2843  
2844 See L<for more information|https://developer.mozilla.org/en-US/docs/Web/API/Element/after>
2845  
2846 =for Pod::Coverage ajax
2847  
2848 =head2 append
2849  
2850 Inserts a set of L<element|HTML::Object::Element> objects or HTML strings after the last child of the L<element|HTML::Object::Element>.
2851  
2852 It returns the objects thus inserted as an L<array object|Module::Generic::Array>.
2853  
2854 Differences from L</appendChild>:
2855  
2856 =over 4
2857  
2858 =item 1. L</append> allows you to also append HTML strings, whereas L</appendChild> only accepts L<element|HTML::Object::Element> objects.
2859  
2860 =item 2. L</append> returns the current L<element|HTML::Object::Element> object, whereas L</appendChild> returns the appended L<element|HTML::Object::Element> object.
2861  
2862 =item 3. L</append> can append several L<element|HTML::Object::Element> and strings, whereas L</appendChild> can only append one L<element|HTML::Object::Element>.
2863  
2864 =back
2865  
2866 See L<for more information|https://developer.mozilla.org/en-US/docs/Web/API/Element/append>
2867  
2868 =head2 appendChild
2869  
2870 Adds the specified child L<element|HTML::Object::Element> argument as the last child to the current L<element|HTML::Object::Element>. If the argument referenced an existing L<element|HTML::Object::Element> on the DOM tree, the element will be detached from its current position and attached at the new position.
2871  
2872 Returns the appended object.
2873  
2874 See L<for more information|https://developer.mozilla.org/en-US/docs/Web/API/Node/appendChild>
2875  
2876 =for assignedSlot
2877  
2878 =head2 attachInternals
2879  
2880 This does nothing.
2881  
2882 Normally, under JavaScript, this would set or return an C<ElementInternals> object, and enables a custom element to participate in HTML forms.
2883  
2884 See L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/attachInternals> for more information.
2885  
2886 =head2 before
2887  
2888 Inserts a set of L<element|HTML::Object::Element> or HTML strings in the L<children|/children> list of the L<element|HTML::Object::Element>'s parent, just before the L<element|HTML::Object::Element>.
2889  
2890 For example:
2891  
2892 my $container = $doc->createElement("div");
2893 my $p = $doc->createElement("p");
2894 $container->appendChild( $p );
2895 my $span = $doc->createElement("span");
2896  
2897 $p->before(span);
2898  
2899 say( $container->outerHTML );
2900 # "<div><span></span><p></p></div>"
2901  
2902 See L<for more information|https://developer.mozilla.org/en-US/docs/Web/API/Element/before>
2903  
2904 =head2 blur
2905  
2906 This does nothing.
2907  
2908 Normally, under JavaScript, this would remove keyboard focus from the currently focused element.
2909  
2910 See L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/blur> for more information.
2911  
2912 =for Pod::Coverage clearQueue
2913  
2914 =head2 click
2915  
2916 Sends a mouse click event to the element.
2917  
2918 See L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/click> for more information.
2919  
2920 =head2 cloneNode
2921  
2922 Clone an element, and optionally, all of its contents. By default, it clones the content of the element.
2923  
2924 Returns the element cloned.
2925  
2926 =head2 closest
2927  
2928 Returns the L<element|HTML::Object::Element> which is the closest ancestor of the current L<element|HTML::Object::Element> (or the current L<element|HTML::Object::Element> itself) which matches the selectors given in parameter.
2929  
2930 For example:
2931  
2932 <article>
2933 <div id="div-01">Here is div-01
2934 <div id="div-02">Here is div-02
2935 <div id="div-03">Here is div-03</div>
2936 </div>
2937 </div>
2938 </article>
2939  
2940 my $el = $doc->getElementById('div-03');
2941 my $r1 = $el->closest("#div-02");
2942 # returns the element with the id C<div-02>
2943  
2944 my $r2 = $el->closest("div div");
2945 # returns the closest ancestor which is a div in div, here it is the C<div-03> itself
2946  
2947 my $r3 = $el->closest("article > div");
2948 # returns the closest ancestor which is a div and has a parent article, here it is the C<div-01>
2949  
2950 my $r4 = $el->closest(":not(div)");
2951 # returns the closest ancestor which is not a div, here it is the outmost article
2952  
2953 See L<for more information|https://developer.mozilla.org/en-US/docs/Web/API/Element/closest>
2954  
2955 =for cmp
2956  
2957 =head2 compareDocumentPosition
2958  
2959 Compares the position of the current element against another element in any other document.
2960  
2961 my $head = $doc->head;
2962 my $body = $doc->body;
2963  
2964 if( $head->compareDocumentPosition( $body ) & HTML::Object::Element->Node.DOCUMENT_POSITION_FOLLOWING )
2965 {
2966 say( 'Well-formed document' );
2967 }
2968 else
2969 {
2970 say( '<head> is not before <body>' );
2971 }
2972  
2973 See L<for more information|https://developer.mozilla.org/en-US/docs/Web/API/Node/compareDocumentPosition>
2974  
2975 =head2 contains
2976  
2977 Returns true or false value indicating whether or not an element is a descendant of the calling element.
2978  
2979 See L<for more information|https://developer.mozilla.org/en-US/docs/Web/API/Node/contains>
2980  
2981 =for Pod::Coverage contents
2982  
2983 =for css
2984  
2985 =for css_cache_check
2986  
2987 =for css_cache_store
2988  
2989 =for data
2990  
2991 =for Pod::Coverage dequeue
2992  
2993 =head2 dispatchEvent
2994  
2995 Dispatches an event to this element in the DOM and returns a boolean value that indicates whether no handler canceled the event.
2996  
2997 This is inherited from L<HTML::Object::EventTarget>
2998
2999 See L<HTML::Object::EventTarget/dispatchEvent> for more information.
3000  
3001 =for each
3002  
3003 =for empty
3004  
3005 =for eq
3006  
3007 =for even
3008  
3009 =for exists
3010  
3011 =head2 focus
3012  
3013 This does nothing.
3014  
3015 Normally, under JavaScript, this would make the element the current keyboard focus.
3016  
3017 See L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/focus> for more information.
3018  
3019 =head2 getAttribute
3020  
3021 Retrieves the value of the named attribute from the current node and returns it as a string.
3022  
3023 Example:
3024  
3025 my $parser = HTML::Object::DOM->new;
3026 my $doc = $parser->parse_data( q{<div id="div1">Hi Champ!</div>} );
3027  
3028 # in a console
3029 my $div1 = $doc->getElementById('div1');
3030 # => <div id="div1">Hi Champ!</div>
3031  
3032 my $exampleAttr = $div1->getAttribute('id');
3033 # => "div1"
3034  
3035 my $align = $div1->getAttribute('align');
3036 # => null
3037  
3038 See L<for more information|https://developer.mozilla.org/en-US/docs/Web/API/Element/getAttribute>
3039  
3040 =head2 getAttributeNames
3041  
3042 Returns an L<array object|Module::Generic::Array> of attribute names from the current element.
3043  
3044 Example:
3045  
3046 <div id="hello" class="opened" data-status="ok"></div>
3047  
3048 my $div = $doc->getElementById( 'hello' );
3049 my $arr = $div->getAttributeNames; # id class data-status
3050 $arr->foreach(sub
3051 {
3052 say $_;
3053 });
3054 # would print:
3055 # id
3056 # class
3057 # data-status
3058  
3059 See L<for more information|https://developer.mozilla.org/en-US/docs/Web/API/Element/getAttributeNames>
3060  
3061 =head2 getAttributeNode
3062  
3063 Retrieves the node representation of the named attribute from the current node and returns it as an Attr.
3064  
3065 Example:
3066  
3067 # html: <div id="top" />
3068 my $t = $doc->getElementById("top");
3069 my $idAttr = $t->getAttributeNode("id");
3070 say( $idAttr->value eq "top" ); # 1
3071  
3072 See L<for more information|https://developer.mozilla.org/en-US/docs/Web/API/Element/getAttributeNode>
3073  
3074 =head2 getAttributeNodeNS
3075  
3076 This always returns C<undef> since there is no support for namespace.
3077  
3078 Retrieves the node representation of the attribute with the specified name and namespace, from the current node and returns it as an Attr.
3079  
3080 See L<for more information|https://developer.mozilla.org/en-US/docs/Web/API/Element/getAttributeNodeNS>
3081  
3082 =head2 getAttributeNS
3083  
3084 This always returns C<undef> since there is no support for namespace.
3085  
3086 Retrieves the value of the attribute with the specified namespace and name from the current node and returns it as a string.
3087  
3088 See L<for more information|https://developer.mozilla.org/en-US/docs/Web/API/Element/getAttributeNS>
3089  
3090 =for Pod::Coverage getDataJson
3091  
3092 =head2 getElementsByClassName
3093  
3094 Provided with a space-delimited list of classes, or a list of classes, and this returns an L<array object|Module::Generic::Array> that contains all descendants of the current element that possess the list of classes given in the parameter.
3095  
3096 Example:
3097  
3098 my $array = $element->getElementsByClassName('test');
3099  
3100 This example finds all elements that have a class of C<test>, which are also a descendant of the element that has the id of C<main>:
3101  
3102 my $array = $doc->getElementById('main')->getElementsByClassName('test');
3103  
3104 To find elements whose class lists include both the C<red> and C<test> classes:
3105  
3106 $element->getElementsByClassName('red test');
3107  
3108 or, equivalently:
3109  
3110 $element->getElementsByClassName('red', 'test');
3111  
3112 Inspecting the results:
3113  
3114 my $matches = $element->getElementsByClassName('colorbox');
3115  
3116 for( my $i=0; $i<$matches->length; $i++ )
3117 {
3118 $matches->[$i]->classList->remove('colorbox');
3119 $matches->get($i)->classList->add('hueframe');
3120 }
3121  
3122 or, somewhat more streamlined:
3123  
3124 $matches->foreach(sub
3125 {
3126 $_->classList->remove('colorbox');
3127 $_->classList->add('hueframe');
3128 });
3129  
3130 See L<for more information|https://developer.mozilla.org/en-US/docs/Web/API/Element/getElementsByClassName>
3131  
3132 =head2 getElementsByTagName
3133  
3134 Provided with a tag name, and this returns an L<array object|Module::Generic::Array> containing all descendant elements, of a particular tag name, from the current element.
3135  
3136 The special string C<*> represents all elements.
3137  
3138 Example:
3139  
3140 # Check the status of each data cell in a table
3141 my $cells = $doc->getElementById('forecast-table')->getElementsByTagName('td');
3142  
3143 $cells->foreach(sub
3144 {
3145 my $cell = shift( @_ ); $_ is available too
3146 my $status = $cell->getAttribute('data-status');
3147 if( $status === 'open' )
3148 {
3149 # Grab the data
3150 }
3151 });
3152  
3153 All descendants of the specified element are searched, but not the element itself.
3154  
3155 See L<for more information|https://developer.mozilla.org/en-US/docs/Web/API/Element/getElementsByTagName>
3156  
3157 =head2 getElementsByTagNameNS
3158  
3159 This always returns C<undef> since there is no support for namespace.
3160  
3161 Normally, under JavaScript, this would return a live HTMLCollection containing all descendant elements, of a particular tag name and namespace, from the current element.
3162  
3163 See L<for more information|https://developer.mozilla.org/en-US/docs/Web/API/Element/getElementsByTagNameNS>
3164  
3165 =head2 getElementsByTagNames
3166  
3167 Provided with a space-separated string of tag names, or an array reference of tag names or a list of tag names, and this will return an L<array object|Module::Generic::Array> of descendant elements matching those tag names.
3168  
3169 This is a non-standard method, courtesy of L<John Resig|https://johnresig.com/blog/comparing-document-position/#postcomment>
3170  
3171 =for Pod::Coverage getJSON
3172  
3173 =for getLocalName
3174  
3175 =head2 getNextSibling
3176  
3177 This non-standard method is an alias for the property L</nextSibling>
3178  
3179 =for getNodePath
3180  
3181 =head2 getPreviousSibling
3182  
3183 This non-standard method is an alias for the property L</previousSibling>
3184  
3185 =head2 getRootNode
3186  
3187 Returns the context object's root which optionally includes the shadow root if it is available.
3188  
3189 See L<for more information|https://developer.mozilla.org/en-US/docs/Web/API/Node/getRootNode>
3190  
3191 =for getValue
3192  
3193 =head2 hasAttribute
3194  
3195 Provided with an attribute name and this returns a boolean value indicating if the element has the specified attribute or not.
3196  
3197 Example:
3198  
3199 my $foo = $doc->getElementById("foo");
3200 if( $foo->hasAttribute("bar") )
3201 {
3202 # do something
3203 }
3204  
3205 See L<for more information|https://developer.mozilla.org/en-US/docs/Web/API/Element/hasAttribute>
3206  
3207 =head2 hasAttributeNS
3208  
3209 This always returns C<undef> since there is no support for namespace.
3210  
3211 Returns a boolean value indicating if the element has the specified attribute, in the specified namespace, or not.
3212  
3213 =head2 hasAttributes
3214  
3215 Returns a boolean value indicating if the element has one or more HTML attributes present.
3216  
3217 Example:
3218  
3219 my $foo = $doc->getElementById('foo');
3220 if( $foo->hasAttributes() )
3221 {
3222 # Do something with '$foo->attributes'
3223 }
3224  
3225 See L<for more information|https://developer.mozilla.org/en-US/docs/Web/API/Element/hasAttributes>
3226  
3227 =head2 hasChildNodes
3228  
3229 Normally, under JavaScript, this would return a boolean value indicating whether or not the element has any child elements.
3230  
3231 See L<for more information|https://developer.mozilla.org/en-US/docs/Web/API/Node/hasChildNodes>
3232  
3233 =for hasClass
3234  
3235 =for hide
3236  
3237 =head2 hidePopover
3238  
3239 Experimental
3240  
3241 This does nothing.
3242  
3243 Normally, under JavaScript, this would hide a popover element by removing it from the top layer and styling it with display: none.
3244  
3245 See L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/hidePopover> for more information.
3246  
3247 =for html
3248  
3249 =for index
3250  
3251 =head2 insertAdjacentElement
3252  
3253 Provided with a C<position> and an L<element|HTML::Object::DOM::Element> and this inserts a given L<element|HTML::Object::DOM::Element> node at a given C<position> relative to the L<element|HTML::Object::DOM::Element> it is invoked upon.
3254  
3255 It returns the element that was inserted, or C<undef>, if the insertion failed.
3256  
3257 It returns a C<HTML::Object::SyntaxError> error if the C<position> specified is not a recognised value.
3258  
3259 It returns a C<HTML::Object::TypeError> error if the C<element> specified is not a valid element.
3260  
3261 THe C<position> can be any one of (case insensitive)
3262  
3263 =over 4
3264  
3265 =item C<beforebegin>
3266  
3267 Before the targetElement itself.
3268  
3269 =item C<afterbegin>
3270  
3271 Just inside the targetElement, before its first child.
3272  
3273 =item C<beforeend>
3274  
3275 Just inside the targetElement, after its last child.
3276  
3277 =item C<afterend>
3278  
3279 After the targetElement itself.
3280  
3281 =back
3282  
3283 <!-- beforebegin -->
3284 <p>
3285 <!-- afterbegin -->
3286 foo
3287 <!-- beforeend -->
3288 </p>
3289 <!-- afterend -->
3290  
3291 See L<for more information|https://developer.mozilla.org/en-US/docs/Web/API/Element/insertAdjacentElement>
3292  
3293 =head2 insertAdjacentHTML
3294  
3295 Provided with a C<position> and an L<element|HTML::Object::DOM::Element> and this parses the text as HTML and inserts the resulting nodes into the tree in the position given.
3296  
3297 This takes the same C<position> parameter as L</insertAdjacentElement>
3298  
3299 It returns the newly created objects from parsing the html data and that were inserted, as an L<array object|Module::Generic::Array>, or C<undef>, if the insertion failed.
3300  
3301 It returns a C<HTML::Object::SyntaxError> error if the C<position> specified is not a recognised value.
3302  
3303 It returns a C<HTML::Object::TypeError> error if the C<element> specified is not a valid element.
3304  
3305 Example:
3306  
3307 # <div id="one">one</div>
3308 my $d1 = $doc->getElementById('one');
3309 $d1->insertAdjacentHTML('afterend', q{<div id="two">two</div>});
3310  
3311 # At this point, the new structure is:
3312 # <div id="one">one</div><div id="two">two</div>
3313  
3314 See L<for more information|https://developer.mozilla.org/en-US/docs/Web/API/Element/insertAdjacentHTML>
3315  
3316 =head2 insertAdjacentText
3317  
3318 Provided with a C<position> and a C<text> string, or a list of C<text> string that will be concatenated, and this inserts the given C<text> at the given C<position> relative to the element it is invoked upon.
3319  
3320 This takes the same C<position> parameter as L</insertAdjacentElement>
3321  
3322 It returns the newly created text L<node|HTML::Object::DOM::Node> that was inserted, or C<undef>, if the insertion failed.
3323  
3324 It returns a C<HTML::Object::SyntaxError> error if the C<position> specified is not a recognised value.
3325  
3326 It returns a C<HTML::Object::TypeError> error if the C<element> specified is not a valid element.
3327  
3328 Example:
3329  
3330 $beforeBtn->addEventListener( click => sub
3331 {
3332 $para->insertAdjacentText('afterbegin',$textInput->value);
3333 });
3334  
3335 $afterBtn->addEventListener( click => sub
3336 {
3337 $para->insertAdjacentText('beforeend',$textInput->value);
3338 });
3339  
3340 Or
3341  
3342 $para->insertAdjacentText( beforesend => 'Some chunks', 'of', 'text to insert' );
3343  
3344 Or, more simply:
3345  
3346 $para->insertAdjacentText( beforesend => qw( Some chunks of text to insert ) );
3347  
3348 But, the following would fail since there is no data provided:
3349  
3350 $para->insertAdjacentText( beforesend => '' );
3351 $para->insertAdjacentText( beforesend => undef );
3352 $para->insertAdjacentText( beforesend => '', '', '' );
3353  
3354 So you have to make sure the data submitted is not zero length.
3355  
3356 See L<for more information|https://developer.mozilla.org/en-US/docs/Web/API/Element/insertAdjacentText>
3357  
3358 =head2 insertBefore
3359  
3360 Inserts an element before the reference element as a child of a specified parent element.
3361  
3362 See L<for more information|https://developer.mozilla.org/en-US/docs/Web/API/Node/insertBefore>
3363  
3364 =for isa_collection
3365  
3366 =for isa_element
3367  
3368 =for Pod::Coverage isArray
3369  
3370 =head2 isDefaultNamespace
3371  
3372 Accepts a namespace URI as an argument and returns a boolean value with a value of true if the namespace is the default namespace on the given element or false if not.
3373  
3374 See L<for more information|https://developer.mozilla.org/en-US/docs/Web/API/Node/isDefaultNamespace>
3375  
3376 =head2 isEqualNode
3377  
3378 Returns a boolean value which indicates whether or not two elements are of the same type and all their defining data points match.
3379  
3380 Two elements are equal when they have the same type, defining characteristics (this would be their ID, number of children, and so forth), its attributes match, and so on. The specific set of data points that must match varies depending on the types of the elements.
3381  
3382 See L<for more information|https://developer.mozilla.org/en-US/docs/Web/API/Node/isEqualNode>
3383  
3384 =head2 isSameNode
3385  
3386 Returns a boolean value indicating whether or not the two elements are the same (that is, they reference the same object).
3387  
3388 See L<for more information|https://developer.mozilla.org/en-US/docs/Web/API/Node/isSameNode>
3389  
3390 =for length
3391  
3392 =for load
3393  
3394 =head2 lookupNamespaceURI
3395  
3396 Accepts a prefix and returns the namespace URI associated with it on the given element if found (and C<undef> if not). Supplying C<undef> for the prefix will return the default namespace.
3397  
3398 This always return an empty string and C<http://www.w3.org/XML/1998/namespace> if the prefix is C<xml>
3399  
3400 See L<for more information|https://developer.mozilla.org/en-US/docs/Web/API/Node/lookupNamespaceURI>
3401  
3402 =head2 lookupPrefix
3403  
3404 This always returns C<undef>.
3405  
3406 Returns a string containing the prefix for a given namespace URI, if present, and C<undef> if not.
3407  
3408 See L<for more information|https://developer.mozilla.org/en-US/docs/Web/API/Node/lookupPrefix>
3409  
3410 =for map
3411  
3412 =head2 matches
3413  
3414 Provided with a CSS C<selector> string, and an optional hash or hash reference, and this returns a boolean value indicating whether or not the element would be selected by the specified C<selector> string.
3415  
3416 Example:
3417  
3418 <ul id="birds">
3419 <li>Orange-winged parrot</li>
3420 <li class="endangered">Philippine eagle</li>
3421 <li>Great white pelican</li>
3422 </ul>
3423  
3424 my $birds = $doc->getElementsByTagName('li');
3425  
3426 for( my $i = 0; $i < $birds->length; $i++ )
3427 {
3428 if( $birds->[$i]->matches('.endangered') )
3429 {
3430 say('The ' + $birds->[i]->textContent + ' is endangered!');
3431 }
3432 }
3433 # The Philippine eagle is endangered!
3434  
3435 See L<for more information|https://developer.mozilla.org/en-US/docs/Web/API/Element/matches>
3436  
3437 =for name
3438  
3439 =head2 new_attribute
3440  
3441 Returns a new L<HTML::Object::DOM::Attribute> object, passing it whatever arguments were provided and return the newly instantiated object.
3442  
3443 If an error occurred, this returns C<undef> and sets an L<error|Module::Generic/error>
3444  
3445 =head2 new_closing
3446  
3447 Returns a new L<HTML::Object::DOM::Closing> object, passing it whatever arguments were provided and return the newly instantiated object.
3448  
3449 If an error occurred, this returns C<undef> and sets an L<error|Module::Generic/error>
3450  
3451 =for Pod::Coverage new_collection
3452  
3453 =head2 new_collection_elements
3454  
3455 Returns a new L<HTML::Object::DOM::Collection> object, passing it whatever arguments were provided and return the newly instantiated object.
3456  
3457 If an error occurred, this returns C<undef> and sets an L<error|Module::Generic/error>
3458  
3459 =head2 new_comment
3460  
3461 Returns a new L<HTML::Object::DOM::Comment> object, passing it whatever arguments were provided and return the newly instantiated object.
3462  
3463 If an error occurred, this returns C<undef> and sets an L<error|Module::Generic/error>
3464  
3465 =head2 new_document
3466  
3467 Returns a new L<HTML::Object::DOM::Document> object, passing it whatever arguments were provided and return the newly instantiated object.
3468  
3469 If an error occurred, this returns C<undef> and sets an L<error|Module::Generic/error>
3470  
3471 =head2 new_element
3472  
3473 Returns a new L<HTML::Object::DOM::Element> object, passing it whatever arguments were provided and return the newly instantiated object.
3474  
3475 If an error occurred, this returns C<undef> and sets an L<error|Module::Generic/error>
3476  
3477 =head2 new_nodelist
3478  
3479 Returns a new L<HTML::Object::DOM::NodeList> object, passing it whatever arguments were provided and return the newly instantiated object.
3480  
3481 If an error occurred, this returns C<undef> and sets an L<error|Module::Generic/error>
3482  
3483 =head2 new_parser
3484  
3485 Returns a new L<HTML::Object::DOM> object, passing it whatever arguments were provided and return the newly instantiated object.
3486  
3487 If an error occurred, this returns C<undef> and sets an L<error|Module::Generic/error>
3488  
3489 =for new_root
3490  
3491 =head2 new_space
3492  
3493 Returns a new L<HTML::Object::DOM::Space> object, passing it whatever arguments were provided and return the newly instantiated object.
3494  
3495 If an error occurred, this returns C<undef> and sets an L<error|Module::Generic/error>
3496  
3497 =head2 new_text
3498  
3499 Returns a new L<HTML::Object::DOM::Text> object, passing it whatever arguments were provided and return the newly instantiated object.
3500  
3501 If an error occurred, this returns C<undef> and sets an L<error|Module::Generic/error>
3502  
3503 =head2 normalize
3504  
3505 Clean up all the text elements under this element (merge adjacent, remove empty).
3506  
3507 See L<for more information|https://developer.mozilla.org/en-US/docs/Web/API/Node/normalize>
3508  
3509 =for odd
3510  
3511 =head2 prepend
3512  
3513 Provided with a list of L<node objects|HTML::Object::DOM::Node> (this includes L<element objects|HTML::Object::DOM::Element>) or C<text>, or a mixture of them and this inserts them before the first child of the L<element|HTML::Object::DOM::Element>.
3514  
3515 It returns the objects thus inserted as an L<array object|Module::Generic::Array>.
3516  
3517 It returns a C<HTML::Object::HierarchyRequestError> error if the objects cannot be inserted at the specified point into the hierarchy.
3518  
3519 It returns a C<HTML::Object::TypeError> error if any argument provided is neither a text string nor a L<node object|HTML::Object::DOM::Node>.
3520  
3521 It returns a C<HTML::Object::SyntaxError> error if no argument was provided.
3522  
3523 Upon success, it returns an L<array objects|Module::Generic::Array> of the nods thus prepended, and upon error, it returns undef and sets one of the errors aforementioned.
3524  
3525 Example:
3526  
3527 Prepending an element:
3528  
3529 my $div = $doc->createElement("div");
3530 my $p = $doc->createElement("p");
3531 my $span = $doc->createElement("span");
3532 $div->append($p);
3533 $div->prepend($span);
3534  
3535 # Array object containing <span>, <p>
3536 my $list = $div->childNodes;
3537  
3538 Prepending text
3539  
3540 my $div = $doc->createElement("div");
3541 $div->append("Some text");
3542 $div->prepend("Headline: ");
3543  
3544 # "Headline: Some text"
3545 say( $div->textContent );
3546  
3547 Prepending both an element and some text
3548  
3549 my $div = $doc->createElement("div");
3550 my $p = $doc->createElement("p");
3551 $div->prepend("Some text", $p);
3552  
3553 # Array object containing "Some text", <p>
3554 my $list = $div->childNodes;
3555  
3556 See L<for more information|https://developer.mozilla.org/en-US/docs/Web/API/Element/prepend>
3557  
3558 =for prependTo
3559  
3560 =for promise
3561  
3562 =for prop
3563  
3564 =head2 querySelector
3565  
3566 Provided with a list of CSS C<selector> strings and this returns the first L<element|HTML::Object::DOM::Element> that is a descendant of the L<element|HTML::Object::DOM::Element> on which it is invoked that matches the specified group of selectors.
3567  
3568 If returns a L<smart undef|Module::Generic/new_null> if nothing is found (to differentiate from an error and still treated as false), and C<undef> upon error and sets an L<error|Module::Generic/error>.
3569  
3570 It returns a C<HTML::Object::SyntaxError> error if any of the selector provided is not a valid selector.
3571  
3572 Example:
3573  
3574 <div>
3575 <h6>Page Title</h6>
3576 <div id="parent">
3577 <span>Love is Kind.</span>
3578 <span>
3579 <span>Love is Patient.</span>
3580 </span>
3581 <span>
3582 <span>Love is Selfless.</span>
3583 </span>
3584 </div>
3585 </div>
3586  
3587 my $parentElement = $doc->querySelector('#parent');
3588 # would need to check that $parentElement is not undef here through...
3589 my $allChildren = $parentElement->querySelectorAll(":scope > span");
3590 $allChildren->foreach(sub
3591 {
3592 my $item = shift( @_ );
3593 $item->classList->add("red");
3594 });
3595  
3596 <div>
3597 <h6>Page Title</h6>
3598 <div id="parent">
3599 <span class="red">Love is Kind.</span>
3600 <span class="red">
3601 <span>Love is Patient.</span>
3602 </span>
3603 <span class="red">
3604 <span>Love is Selfless.</span>
3605 </span>
3606 </div>
3607 </div>
3608  
3609 See L<for more information|https://developer.mozilla.org/en-US/docs/Web/API/Element/querySelector>
3610  
3611 =head2 querySelectorAll
3612  
3613 Provided with a list of CSS C<selector> strings and this returns an L<array object|Module::Generic::Array> representing a list of L<elements|HTML::Object::DOM::Element> matching the specified group of C<selectors> which are descendants of the L<element|HTML::Object::DOM::Element> on which the method was called.
3614  
3615 It returns a C<HTML::Object::SyntaxError> error if any of the selector provided is not a valid selector.
3616  
3617 Example:
3618  
3619 my $matches = $myBox->querySelectorAll("p");
3620 my $matches = $myBox->querySelectorAll("div.note, div.alert");
3621  
3622 Get a list of the document's <p> elements whose immediate parent element is a C<div> with the class C<highlighted> and which are located inside a container whose ID is C<test>.
3623  
3624 my $container = $doc->querySelector("#test");
3625 my $matches = $container->querySelectorAll("div.highlighted > p");
3626  
3627 Here with an attribute C<selector>
3628  
3629 my $container = $doc->querySelector("#userlist");
3630 my $matches = $container->querySelectorAll("li[data-active='1']");
3631  
3632 To access the matched element, see L<Module::Generic::Array/foreach> for example:
3633  
3634 $matches->foreach(sub
3635 {
3636 my $elem = shift( @_ ); # $_ is available too
3637 # Do something with $elem
3638
3639 # To satisfy array object's foreach and avoid ending abruptly the loop
3640 return(1);
3641 });
3642  
3643 or
3644  
3645 foreach my $elem ( @$matches )
3646 {
3647 # Do something with $elem
3648 }
3649  
3650 A word of caution on some edge case. While the JavaScript equivalent of C<querySelectorAll> takes a document global view at the CSS selector provided, here in perl, it only matches descendants.
3651  
3652 For example, the following would return 1 in JavaScript while it would return 0 in our implementation.
3653  
3654 <div class="outer">
3655 <div class="select">
3656 <div class="inner"></div>
3657 </div>
3658 </div>
3659  
3660 With JavaScript:
3661  
3662 var select = document.querySelector('.select');
3663 var inner = select.querySelectorAll('.outer .inner');
3664 inner.length; // 1, not 0 !
3665  
3666 With Perl:
3667  
3668 my $select = $doc->querySelector('.select');
3669 my $inner = $select->querySelectorAll('.outer .inner');
3670 $inner->length; // 0, not 1 !!
3671  
3672 Why is that? Because, when JavaScript does it search for the element whose class is C<inner> and who is inside another element whose class is C<outer>, it does not bother JavaScript that C<outer> is a parent of the elment on which C<querySelectorAll> is being called, because it retains only the last part of the selector, i.e. C<inner>.
3673  
3674 In perl, there is no such elaborate CSS engine that would allow us this level of granularity, and thus it would look for C<.outer .inner> below C<select> and since there are none, it would return C<0>
3675  
3676 See L<for more information|https://developer.mozilla.org/en-US/docs/Web/API/Element/querySelectorAll>
3677  
3678 =for rank
3679  
3680 =head2 remove
3681  
3682 Removes the element from the children list of its parent.
3683  
3684 It returns true upon success, and upon error, it returns C<undef> and sets an L<error|Module::Generic/error>
3685  
3686 It returns an C<HTML::Object::HierarchyRequestError> if the L<element|HTML::Object::DOM::Element> does not have any parent, like so:
3687  
3688 my $div = $doc->createElement( 'div' );
3689 $div->remove; # Error returned !
3690  
3691 Example:
3692  
3693 <div id="div-01">Here is div-01</div>
3694 <div id="div-02">Here is div-02</div>
3695 <div id="div-03">Here is div-03</div>
3696  
3697 my $el = $doc->getElementById('div-02');
3698 $el->remove(); # Removes the div with the 'div-02' id
3699  
3700 See L<for more information|https://developer.mozilla.org/en-US/docs/Web/API/Element/remove>
3701  
3702 =for removeAttr
3703  
3704 =head2 removeAttribute
3705  
3706 Provided with an attribute C<name> and this removes the attribute with the specified C<name> from the L<element|HTML::Object::DOM::Element>.
3707  
3708 It returns true upon success or false otherwise.
3709  
3710 It returns an C<HTML::Object::SyntaxError> if no attribute name was provided.
3711  
3712 Example:
3713  
3714 # Given: <div id="div1" align="left" width="200px">
3715 $doc->getElementById("div1")->removeAttribute("align");
3716 # Now: <div id="div1" width="200px">
3717  
3718 See L<for more information|https://developer.mozilla.org/en-US/docs/Web/API/Element/removeAttribute>
3719  
3720 =head2 removeAttributeNode
3721  
3722 Provided with an L<attribute node|HTML::Object::DOM::Attribute> and this removes the node representation of the named attribute from the current node. This is similar to L</removeAttribute>, except that L</removeAttribute> takes a string as attribute name, while L</removeAttributeNode> takes an L<attribute object|HTML::Object::DOM::Attribute>.
3723  
3724 Upon error, it returns C<undef> and sets an L<error|Module::Generic/error>.
3725  
3726 It returns an C<HTML::Object::SyntaxError> if the value provided is not an L<attribute object|HTML::Object::DOM::Attribute>.
3727  
3728 Example:
3729  
3730 # Given: <div id="top" align="center" />
3731 my $d = $doc->getElementById("top");
3732 my $d_align = $d->getAttributeNode("align");
3733 $d->removeAttributeNode( $d_align ); # <-- passing the attribute object
3734 # align is now removed: <div id="top" />
3735  
3736 See L<for more information|https://developer.mozilla.org/en-US/docs/Web/API/Element/removeAttributeNode>
3737  
3738 =head2 removeAttributeNS
3739  
3740 This always return C<undef> since there is no support for namespace.
3741  
3742 Under JavaScript, this would remove the attribute with the specified name and namespace, from the current node.
3743  
3744 See L<for more information|https://developer.mozilla.org/en-US/docs/Web/API/Element/removeAttributeNS>
3745  
3746 =head2 removeChild
3747  
3748 Removes a child element from the current element, which must be a child of the current element.
3749  
3750 See L<for more information|https://developer.mozilla.org/en-US/docs/Web/API/Node/removeChild>
3751  
3752 =for removeClass
3753  
3754 =for Pod::Coverage removeData
3755  
3756 =head2 removeEventListener
3757  
3758 Removes an event listener from the element. This is inherited from L<HTML::Object::EventTarget>
3759
3760 See L<HTML::Object::EventTarget/removeEventListener> for more information.
3761  
3762 =head2 replaceChild
3763  
3764 Replaces one child element of the current one with the second one given in parameter.
3765  
3766 See L<for more information|https://developer.mozilla.org/en-US/docs/Web/API/Node/replaceChild>
3767  
3768 =head2 replaceChildren
3769  
3770 Replaces the existing children of a L<Node|HTML::Object::DOM::Node> with a specified new set of children.
3771 These can be HTML strings or L<node objects|HTML::Object::DOM::Node>.
3772  
3773 It returns an L<array object|Module::Generic::Array> of the replaced or removed children. Note that those replaced children will have their parent value set to C<undef>.
3774  
3775 You can call it on a node without any argument specified to remove all of its children:
3776  
3777 $myNode->replaceChildren();
3778  
3779 This method also enables you to easily transfer nodes between elements since each new nodes provided will be detached from their previous parent and re-attached under the current element.
3780  
3781 See L<for more information|https://developer.mozilla.org/en-US/docs/Web/API/Element/replaceChildren>
3782  
3783 =head2 replaceWith
3784  
3785 Replaces the element in the children list of its parent with a set of Node or DOMString objects.
3786  
3787 This method replaces the current L<element|HTML::Object::DOM::Element> in the children list of its parent with a set of L<node|HTML::Object::DOM::Node> or strings. Strings that look like HTML are parsed and added as L<HTML::Object::DOM::Element> and other text strings are inserted as equivalent L<Text nodes|HTML::Object::DOM::Text>.
3788  
3789 This returns a C<HTML::Object::HierarchyRequestError> when a L<node|HTML::Object::DOM::Node> provided cannot be inserted at the specified point in the hierarchy.
3790  
3791 It returns an L<array object|Module::Generic::Array> of the newly inserted nodes.
3792  
3793 Example:
3794  
3795 my $div = $doc->createElement("div");
3796 my $p = $doc->createElement("p");
3797 $div->appendChild( $p );
3798 # Now div is: <div><p></p></div>
3799 my $span = $doc->createElement("span");
3800  
3801 $p->replaceWith( $span );
3802  
3803 say( $div->outerHTML );
3804 # "<div><span></span></div>"
3805  
3806 See L<for more information|https://developer.mozilla.org/en-US/docs/Web/API/Element/replaceWith>
3807  
3808 =for set_namespace
3809  
3810 =head2 setAttribute
3811  
3812 Provided with a C<name> and a C<value> and this sets the value of an attribute on the specified L<element|HTML::Object::DOM::Element>. If the attribute already exists, the value is updated; otherwise a new attribute is added with the specified name and value.
3813  
3814 To get the current value of an attribute, use L</getAttribute>; to remove an attribute, use L</removeAttribute>.
3815  
3816 Contrary to the original JavaScript equivalent, providing a C<value> with C<undef> results in removing the attribute altogether.
3817  
3818 It returns the current element upon success and upon error, it returns C<undef> and sets an L<error|Module::Generic/error>
3819  
3820 It returns an C<HTML::Object::InvalidCharacterError> object when the attribute name provided contains illegal characters.
3821  
3822 It returns an C<HTML::Object::SyntaxError> object when no attribute name was provided.
3823  
3824 Example:
3825  
3826 Set attributes on a button
3827  
3828 <button>Hello World</button>
3829  
3830 my $b = $doc->querySelector("button");
3831  
3832 $b->setAttribute("name", "helloButton");
3833 $b->setAttribute("disabled", "");
3834 # <button name="helloButton" disabled="">Hello World</button>
3835  
3836 To remove an attribute (same as calling L</removeAttribute>)
3837  
3838 $b->setAttribute("disabled", undef);
3839  
3840 See L<for more information|https://developer.mozilla.org/en-US/docs/Web/API/Element/setAttribute>
3841  
3842 =head2 setAttributeNode
3843  
3844 Provided with an L<attribute object|HTML::Object::DOM::Attribute> and this sets, or possibly replace, the node representation of the named attribute from the current node.
3845  
3846 If a previous L<attribute|HTML::Object::DOM::Attribute> existed, it will be replaced and returned.
3847  
3848 Example:
3849  
3850 <div id="one" align="left">one</div>
3851 <div id="two">two</div>
3852  
3853 my $d1 = $doc->getElementById('one');
3854 my $d2 = $doc->getElementById('two');
3855 my $a = $d1->getAttributeNode('align');
3856  
3857 $d2->setAttributeNode( $a.cloneNode($true) );
3858  
3859 # Returns: 'left'
3860 say( $d2->attributes->get( 'align' )->value );
3861 # or
3862 say( $d2->attributes->{align}->value );
3863  
3864 See L<for more information|https://developer.mozilla.org/en-US/docs/Web/API/Element/setAttributeNode>
3865  
3866 =head2 setAttributeNodeNS
3867  
3868 This always return C<undef> since namespace is not supported.
3869  
3870 Under JavaScript, this would set the L<node|HTML::Object::DOM::Attribute> representation of the attribute with the specified C<name> and C<namespace>, from the current node.
3871  
3872 See L<for more information|https://developer.mozilla.org/en-US/docs/Web/API/Element/setAttributeNS>
3873  
3874 =for show
3875  
3876 =head2 showPopover
3877  
3878 Experimental
3879  
3880 This does nothing.
3881  
3882 Normally, under JavaScript, this would show a popover element by adding it to the top layer and removing display: none; from its styles.
3883  
3884 See L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/showPopover> for more information.
3885  
3886 =for Pod::Coverage sourceIndex
3887  
3888 =for string_value
3889  
3890 =for tagname
3891  
3892 =head2 toggleAttribute
3893  
3894 Provided with an attribute name and an optiona C<force> value, and this toggles a boolean attribute, removing it if it is present and adding it if it is not present, on the specified element.
3895  
3896 C<force> is a boolean value to determine whether the attribute should be added or removed, no matter whether the attribute is present or not at the moment.
3897  
3898 It returns C<true> if attribute C<name> is eventually present, and C<false> otherwise.
3899  
3900 It returns an C<HTML::Object::InvalidCharacterError> error if the specified attribute C<name> contains one or more characters which are not valid in attribute names.
3901  
3902 Example:
3903  
3904 To toggle the C<disabled> attribute of an input field
3905  
3906 <input value="text">
3907 <button>toggleAttribute("disabled")</button>
3908  
3909 my $button = $doc->querySelector("button");
3910 my $input = $doc->querySelector("input");
3911  
3912 $button->addEventListener( click => sub
3913 {
3914 $input->toggleAttribute("disabled");
3915 });
3916  
3917 See L<for more information|https://developer.mozilla.org/en-US/docs/Web/API/Element/toggleAttribute>
3918  
3919 =for toggleClass
3920  
3921 =head2 togglePopover
3922  
3923 Experimental
3924  
3925 This does nothing.
3926  
3927 Normally, under JavaScript, this would toggle a popover element between the hidden and showing states.
3928  
3929 See L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/togglePopover> for more information.
3930  
3931 =head2 toString
3932  
3933 Returns a string representation for this element.
3934  
3935 =head2 to_number
3936  
3937 Returns a L<HTML::Object::DOM::Number> object representing the text value of this element.
3938  
3939 =for Pod::Coverage uniqueSort
3940  
3941 =for Pod::Coverage wrap
3942  
3943 =for xq
3944  
3945 =head1 EVENTS
3946  
3947 Listen to these events using L<HTML::Object::EventTarget/addEventListener> or by assigning an event listener to the C<oneventname> property of this interface.
3948  
3949 Under perl, few events are actually "fired" by L<HTML::Object> and L<for the others|https://developer.mozilla.org/en-US/docs/Web/API/Element#events> (also L<here|https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement#events>), nothing prevents you from L<triggering|HTML::Object::EventTarget/dispatchEvent> whatever events you want, even private non-standard ones, and set up listeners for them.
3950  
3951 Below are the ones actually "fired" by L<HTML::Object>.
3952  
3953 =head2 change
3954  
3955 This event is fired when there has been some change to the underlying element. This is also available via the onchange property.
3956  
3957 =head2 error
3958  
3959 This event is fired when an error occurs for this element. This is also available via the onerror property.
3960  
3961 Example:
3962  
3963 $video->onerror = sub
3964 {
3965 say( "Error " . $video->error->code . "; details: " . $video->error->message );
3966 }
3967  
3968 =head1 EXTENDED
3969  
3970 Inheriting from L<HTML::Object::EventTarget> and extended with L<HTML::Object::XQuery>
3971  
3972 See detailed documentation in L<HTML::Object::XQuery>
3973  
3974 =head1 AUTHOR
3975  
3976 Jacques Deguest E<lt>F<jack@deguest.jp>E<gt>
3977  
3978 =head1 SEE ALSO
3979  
3980 L<HTML::Object::DOM>, L<HTML::Object::DOM::Attribute>, L<HTML::Object::DOM::Boolean>, L<HTML::Object::DOM::Closing>, L<HTML::Object::Collection>, L<HTML::Object::DOM::Comment>, L<HTML::Object::DOM::Declaration>, L<HTML::Object::DOM::Document>, L<HTML::Object::DOM::Element>, L<HTML::Object::Exception>, L<HTML::Object::Literal>, L<HTML::Object::DOM::Number>, L<HTML::Object::DOM::Root>, L<HTML::Object::DOM::Space>, L<HTML::Object::DOM::Text>, L<HTML::Object::XQuery>
3981  
3982 L<Mozilla DOM documentation|https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement>, L<Mozilla Element documentation|https://developer.mozilla.org/en-US/docs/Web/API/Element>
3983  
3984 =head1 COPYRIGHT & LICENSE
3985  
3986 Copyright(c) 2021 DEGUEST Pte. Ltd.
3987  
3988 All rights reserved
3989  
3990 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
3991  
3992 =cut