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

filter: Statementstmt 0.0% 0 / 600 Branchbran 0.0% 0 / 260 Conditioncond 0.0% 0 / 246 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 78 Podpod 0.0% 0 / 68 totaltotal 0.0% 0 / 1252 CC 131 SCAR 51.2
CC
131
Cov
0%
compareDocumentPosition
812.0
replaceChild
306.0
appendChild
182.0
CRAP 166.9
1 ##----------------------------------------------------------------------------
2 ## HTML Object - ~/lib/HTML/Object/DOM/Node.pm
3 ## Version v0.3.0_1
4 ## Copyright(c) 2025 DEGUEST Pte. Ltd.
5 ## Author: Jacques Deguest <jack@deguest.jp>
6 ## Created 2021/12/13
7 ## Modified 2026/03/29
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::Node;
15 BEGIN
16 {
17 use strict;
18 use warnings;
19 warnings::register_categories( 'HTML::Object' );
20 use parent qw( HTML::Object::EventTarget );
21 use vars qw( @EXPORT $XP $VERSION );
22 use Wanted;
23 use constant {
24 DOCUMENT_POSITION_IDENTICAL => 0,
25 DOCUMENT_POSITION_DISCONNECTED => 1,
26 DOCUMENT_POSITION_PRECEDING => 2,
27 DOCUMENT_POSITION_FOLLOWING => 4,
28 DOCUMENT_POSITION_CONTAINS => 8,
29 DOCUMENT_POSITION_CONTAINED_BY => 16,
30 DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC => 32,
31 XML_DEFAULT_NAMESPACE => 'http://www.w3.org/XML/1998/namespace',
32 ELEMENT_NODE => 1,
33 ATTRIBUTE_NODE => 2,
34 TEXT_NODE => 3,
35 CDATA_SECTION_NODE => 4,
36 # Deprecated
37 ENTITY_REFERENCE_NODE => 5,
38 # Deprecated
39 ENTITY_NODE => 6,
40 PROCESSING_INSTRUCTION_NODE => 7,
41 COMMENT_NODE => 8,
42 DOCUMENT_NODE => 9,
43 DOCUMENT_TYPE_NODE => 10,
44 DOCUMENT_FRAGMENT_NODE => 11,
45 NOTATION_NODE => 12,
46 SPACE_NODE => 13,
47 };
48 our @EXPORT = qw(
49 DOCUMENT_POSITION_IDENTICAL
50 DOCUMENT_POSITION_DISCONNECTED
51 DOCUMENT_POSITION_PRECEDING
52 DOCUMENT_POSITION_FOLLOWING
53 DOCUMENT_POSITION_CONTAINS
54 DOCUMENT_POSITION_CONTAINED_BY
55 DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC
56  
57 ELEMENT_NODE
58 ATTRIBUTE_NODE
59 TEXT_NODE
60 CDATA_SECTION_NODE
61 ENTITY_REFERENCE_NODE
62 ENTITY_NODE
63 PROCESSING_INSTRUCTION_NODE
64 COMMENT_NODE
65 DOCUMENT_NODE
66 DOCUMENT_TYPE_NODE
67 DOCUMENT_FRAGMENT_NODE
68 NOTATION_NODE
69 SPACE_NODE
70 );
71 use overload (
72 '==' => \&isSameNode,
73 'eq' => \&isSameNode,
74 fallback => 1,
75 );
76 our $XP;
77 our $VERSION = 'v0.3.0_1';
78 };
79  
80 use strict;
81 use warnings;
82  
83 sub init
84 {
85 my $self = shift( @_ );
86 $self->{_init_strict_use_sub} = 1;
87 $self->HTML::Object::Element::init( @_ ) || return( $self->pass_error );
88 return( $self );
89 }
90  
91 sub appendChild
92 {
93 my $self = shift( @_ );
94 return( $self->error({
95 message => sprintf( "At least 1 arguments is required, but only %d provided.", scalar( @_ ) ),
96 class => 'HTML::Object::TypeError',
97 }) ) if( scalar( @_ ) < 1 );
98 my $new = shift( @_ );
99 my $new_parent = $new->parent;
100 my $parent = $self->parent;
101 # We use 'nodes' rather than 'children' so this works well with HTML::Object::DOM::Document
102 my $nodes = $self->nodes;
103 if( $new_parent &&
104 !$self->_is_a( $new_parent => 'HTML::Object::DOM::Document' ) &&
105 !$self->_is_a( $new_parent => 'HTML::Object::DOM::DocumentFragment' ) &&
106 !$self->_is_a( $new_parent => 'HTML::Object::DOM::Element' ) )
107 {
108 return( $self->error({
109 message => "Node's parent is not an HTML::Object::DOM::Document, HTML::Object::DOM::DocumentFragment or HTML::Object::DOM::Element object.",
110 class => 'HTML::Object::HierarchyRequestError',
111 }) );
112 }
113 # All other conditions below are strictly similar to replaceChild()
114 elsif( !$self->_is_a( $new => 'HTML::Object::DOM::DocumentFragment' ) &&
115 !$self->_is_a( $new => 'HTML::Object::DOM::Declaration' ) &&
116 !$self->_is_a( $new => 'HTML::Object::DOM::Element' ) &&
117 !$self->_is_a( $new => 'HTML::Object::DOM::CharacterData' ) )
118 {
119 return( $self->error({
120 message => "New node is not an HTML::Object::DOM::DocumentFragment, HTML::Object::DOM::Declaration, HTML::Object::DOM::Element or HTML::Object::DOM::CharacterData object.",
121 class => 'HTML::Object::HierarchyRequestError',
122 }) );
123 }
124 elsif( $self->lineage->has( $new ) )
125 {
126 return( $self->error({
127 message => "New node provided is an ancestor of the current node.",
128 class => 'HTML::Object::HierarchyRequestError',
129 }) );
130 }
131 elsif( ( $self->_is_a( $new => 'HTML::Object::DOM::Text' ) || $self->_is_a( $new => 'HTML::Object::DOM::Space' ) ) &&
132 $self->_is_a( $new_parent => 'HTML::Object::DOM::Document' ) )
133 {
134 return( $self->error({
135 message => "New node is a HTML::Object::DOM::Text or HTML::Object::DOM::Space node and its parent is a HTML::Object::DOM::Document node.",
136 class => 'HTML::Object::HierarchyRequestError',
137 }) );
138 }
139 elsif( $self->isa( 'HTML::Object::DOM::Declaration' ) && !$self->_is_a( $parent => 'HTML::Object::DOM::Document' ) )
140 {
141 return( $self->error({
142 message => "Current node is a DocumentType, but its parent is not an HTML::Object::DOM::Document object.",
143 class => 'HTML::Object::HierarchyRequestError',
144 }) );
145 }
146 elsif( $self->_is_a( $parent => 'HTML::Object::DOM::Document' ) &&
147 $self->_is_a( $new => 'HTML::Object::DOM::DocumentFragment' ) &&
148 ( $new->childElementCount > 1 || $new->children->grep(sub{ $self->_is_a( $_ => 'HTML::Object::DOM::Text' ) })->length ) )
149 {
150 return( $self->error({
151 message => "Current node parent is a HTML::Object::DOM::Document object and new node is a HTML::Object::DOM::DocumentFragment object that has either more than 1 element or has a HTML::Object::DOM::Text node.",
152 class => 'HTML::Object::HierarchyRequestError',
153 }) );
154 }
155 # This is different from replaceChild()
156 elsif( $self->_is_a( $parent => 'HTML::Object::DOM::Document' ) &&
157 $parent->childElementCount > 0 &&
158 $self->_is_a( $new => 'HTML::Object::DOM::Element' ) )
159 {
160 return( $self->error({
161 message => "Attempting to replace a child element in a Document with another non HTML-tag element. Document can have only one Element: the HTML-tag element.",
162 class => 'HTML::Object::HierarchyRequestError',
163 }) );
164 }
165 # If the node to append is a doctype and the curent last node is an element, this would put a doctype after an element, which is forbidden
166 elsif( $self->_is_a( $new => 'HTML::Object::DOM::Declaration' ) &&
167 $self->_is_a( $nodes->last, 'HTML::Object::DOM::Element' ) )
168 {
169 return( $self->error({
170 message => "The last node is an element. Appending the DocumentType would place it after.",
171 class => 'HTML::Object::HierarchyRequestError',
172 }) );
173 }
174  
175 $new->detach;
176 my $new_array = $self->new_array( $self->_is_a( $new => 'HTML::Object::DOM::DocumentFragment' ) ? $new->children : $new );
177 $new_array->foreach(sub
178 {
179 return(1) if( !$self->_is_a( $_ => 'HTML::Object::DOM::Node' ) );
180 $_->parent( $self );
181 });
182 $nodes->push( $new_array->list );
183 $self->reset(1);
184 if( $self->_is_a( $new => 'HTML::Object::DOM::DocumentFragment' ) )
185 {
186 $new->children->reset;
187 }
188 return( $new );
189 }
190  
191 sub appendNodes
192 {
193 my $self = shift( @_ );
194 my $children = $self->children;
195 foreach my $this ( @_ )
196 {
197 if( $self->_is_a( $this => 'HTML::Object::DOM::Node' ) )
198 {
199 $this->parent( $self );
200 $children->push( $this );
201 }
202 }
203 return( $self );
204 }
205  
206 # Note: Property
207 # Example: <base href="https://www.example.com/">
208 sub baseURI : lvalue { return( shift->_set_get_callback({
209 get => sub
210 {
211 my $self = shift( @_ );
212 my $root = $self->root;
213 return( $self->new_null ) if( !$root );
214 return( $self->new_null ) if( !$root->can( 'uri' ) );
215 return( $root->uri ) if( $root->uri );
216 my $nodes = $self->findNode( 'base' );
217 return( $self->new_null ) if( $nodes->is_empty );
218 my $node = $nodes->first;
219 return( $self->new_null ) if( !$node );
220 return( $self->new_null ) if( !$node->attributes->has( 'href' ) );
221 my $uri = $node->attributes->get( 'href' );
222 return( $self->new_null ) if( !defined( $uri ) || !CORE::length( "$uri" ) );
223 return( $self->_set_get_uri( 'uri', $uri ) );
224 },
225 set => sub
226 {
227 my $self = shift( @_ );
228 my $uri = shift( @_ );
229 my $root = $self->root;
230 return( $self->new_null ) if( !$root );
231 my $nodes = $root->findNode( 'base' );
232 my $base;
233 if( $nodes->is_empty )
234 {
235 $base = $root->createElement( 'base' ) || return( $self->error( $root->pass_error ) );
236 my $head = $root->findNode( 'head' )->first ||
237 return( $self->error( "No base uri can be set, because there is no head element in this document." ) );
238 $head->appendChild( $base );
239 }
240 else
241 {
242 $base = $nodes->first;
243 }
244 $base->href( $uri ) || return( $self->error( $base->pass_error ) );
245 return( $uri );
246 },
247 }, @_ ) ); }
248  
249 sub childNodes { return( shift->children ); }
250  
251 sub cloneNode
252 {
253 my $self = shift( @_ );
254 my $clone = $self->clone;
255 $clone->parent( undef );
256 return( $clone );
257 }
258  
259 sub compareDocumentPosition
260 {
261 my $self = shift( @_ );
262 my $elem = shift( @_ ) || return( $self->error( "No element was provided to append." ) );
263 return( $self->error( "Element provided (", overload::StrVal( $elem ), ") is actually not an HTML element." ) ) if( !$self->_is_a( $elem => 'HTML::Object::Element' ) );
264 # 0 - Elements are identical.
265 # -> DOCUMENT_POSITION_IDENTICAL
266 # 1 - No relationship, both nodes are in different documents or different trees in the same document.
267 # -> DOCUMENT_POSITION_DISCONNECTED
268 # 2 - The specified node precedes the current node.
269 # otherNode precedes the node in either a pre-order depth-first traversal of a tree containing both (e.g., as an ancestor or previous sibling or a descendant of a previous sibling or previous sibling of an ancestor) or (if they are disconnected) in an arbitrary but consistent ordering.
270 # -> DOCUMENT_POSITION_PRECEDING
271 # 4 - The specified node follows the current node.
272 # The otherNode follows the node in either a pre-order depth-first traversal of a tree containing both (e.g., as a descendant or following sibling or a descendant of a following sibling or following sibling of an ancestor) or (if they are disconnected) in an arbitrary but consistent ordering.
273 # -> DOCUMENT_POSITION_FOLLOWING
274 # 8 - The otherNode is an ancestor of / contains the current node.
275 # -> DOCUMENT_POSITION_CONTAINS
276 # 16 - The otherNode is a descendant of / contained by the node.
277 # -> DOCUMENT_POSITION_CONTAINED_BY
278 # 32 - The specified node and the current node have no common container node or the two nodes are different attributes of the same node.
279 # -> DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC
280  
281 # "If the two nodes being compared are the same node, then no flags are set on the return."
282 # <https://www.w3.org/TR/DOM-Level-3-Core/core.html#DocumentPosition>
283 return(0) if( Scalar::Util::refaddr( $self ) eq Scalar::Util::refaddr( $elem ) );
284 # Current object and other element are both attributes of the same element (ownerElement)
285 # "If neither of the two determining node is a child node and nodeType is the same for both determining nodes, then an implementation-dependent order between the determining nodes is returned."
286 # <https://www.w3.org/TR/DOM-Level-3-Core/core.html#DocumentPosition>
287 if( $self->nodeType == ATTRIBUTE_NODE && $elem->nodeType == ATTRIBUTE_NODE )
288 {
289 # 2 attributes of the same node
290 return(32) if( $self->ownerElement && $self->ownerElement eq $elem->ownerElement );
291 }
292  
293 my $parent = $self->parent;
294 my $parent2 = $elem->parent;
295 # "If neither of the two determining node is a child node and one determining node has a greater value of nodeType than the other, then the corresponding node precedes the other."
296 # <https://www.w3.org/TR/DOM-Level-3-Core/core.html#DocumentPosition>
297 if( !$parent && !$parent2 )
298 {
299 return( $self->nodeType < $elem->nodeType ? DOCUMENT_POSITION_FOLLOWING : DOCUMENT_POSITION_PRECEDING );
300 }
301  
302 my $root = $self->root;
303 my $root2 = $elem->root;
304 # Both elements are in different documents
305 if( $root ne $root2 )
306 {
307 return( DOCUMENT_POSITION_DISCONNECTED );
308 }
309  
310 my $bit = 0;
311 my $lineage = $self->lineage;
312 my $lineage2 = $elem->lineage;
313 my $prev_siblings = $self->left;
314 my $next_siblings = $self->right;
315 my $seen = {};
316 my $crawl;
317 $crawl = sub
318 {
319 my $kid = shift( @_ );
320 my $addr = Scalar::Util::refaddr( $kid );
321 return if( ++$seen->{ $addr } > 1 );
322 my $children = $kid->children;
323 foreach( @$children )
324 {
325 if( $_->can( 'eid' ) &&
326 defined( $_->eid ) &&
327 defined( $elem->eid ) &&
328 $_->eid eq $elem->eid )
329 {
330 return( $_ );
331 }
332 if( my $e = $crawl->( $_ ) )
333 {
334 return( $e );
335 }
336 }
337 return;
338 };
339  
340 # Check if our parent is among the other element's parents
341 my $parent_pos = $lineage2->pos( $parent );
342 # Check if the other element's parent is among our parents
343 my $parent2_pos = $lineage->pos( $parent2 );
344 # Then check their position, if found
345 if( defined( $parent_pos ) && defined( $parent2_pos ) )
346 {
347 if( $parent_pos > $parent2_pos )
348 {
349 $bit |= DOCUMENT_POSITION_FOLLOWING;
350 }
351 elsif( $parent2_pos > $parent_pos )
352 {
353 $bit |= DOCUMENT_POSITION_PRECEDING;
354 }
355 else
356 {
357 # Nodes share the same parent, and thus are siblings
358 }
359 }
360 elsif( defined( $parent_pos ) )
361 {
362 $bit |= DOCUMENT_POSITION_FOLLOWING;
363 }
364 elsif( defined( $parent2_pos ) )
365 {
366 $bit |= DOCUMENT_POSITION_PRECEDING;
367 }
368 # Otherwise neither our parent or the other's parent is in either lineage
369 else
370 {
371 # Neither our parent, nor the other's parent is in either lineage.
372 }
373  
374 if( $lineage->intersection( $lineage2 )->is_empty &&
375 $lineage2->intersection( $lineage )->is_empty )
376 {
377 $bit |= DOCUMENT_POSITION_DISCONNECTED;
378 }
379 else
380 {
381 # Our lineage or the other or both lineages intersect
382 }
383 # Check for the other node in:
384 # 1) ancestor
385 # 2) previous sibling
386 # 3) descendant of previous sibling
387 # 4) previous sibling of an ancestor
388  
389 # "If one of the nodes being compared contains the other node, then the container precedes the contained node, and reversely the contained node follows the container."
390 # <https://www.w3.org/TR/DOM-Level-3-Core/core.html#DocumentPosition>
391 if( $lineage->has( $elem ) )
392 {
393 $bit |= DOCUMENT_POSITION_PRECEDING;
394 $bit |= DOCUMENT_POSITION_CONTAINS;
395 }
396 # check previous sibling
397 elsif( $prev_siblings->has( $elem ) )
398 {
399 $bit |= DOCUMENT_POSITION_PRECEDING;
400 }
401 else
402 {
403 # check for descendant of previous sibling
404 $seen = {};
405 foreach( @$prev_siblings )
406 {
407 if( my $e = $crawl->( $_ ) )
408 {
409 $bit |= DOCUMENT_POSITION_PRECEDING;
410 last;
411 }
412 }
413  
414 # no luck so far. Checking previous sibling of an ancestor
415 if( !( $bit & DOCUMENT_POSITION_PRECEDING ) )
416 {
417 # Go through each ancestor
418 foreach( @$lineage )
419 {
420 # then get its previous siblings
421 my $ancestor_siblings = $_->left;
422 # and check if the other element is one of them
423 if( $ancestor_siblings && $ancestor_siblings->has( $elem ) )
424 {
425 $bit |= DOCUMENT_POSITION_PRECEDING;
426 last;
427 }
428 }
429 }
430 }
431  
432 # still no luck, check
433 # 1) descendants
434 # 2) following sibling
435 # 3) a descendant of a following sibling
436 # 4) following sibling of an ancestor
437 $seen = {};
438 # e.g. a child or an attribute of our element:
439 # "when comparing an element against its own attribute or child, the element node precedes its attribute node and its child node, which both follow it."
440 # <https://www.w3.org/TR/DOM-Level-3-Core/core.html#DocumentPosition>
441 if( $lineage2->has( $self ) )
442 {
443 $bit |= ( DOCUMENT_POSITION_CONTAINED_BY | DOCUMENT_POSITION_FOLLOWING )
444 }
445 # Now check for following siblings
446 elsif( $next_siblings->has( $elem ) )
447 {
448 $bit |= DOCUMENT_POSITION_FOLLOWING;
449 }
450 # "If one of the nodes being compared contains the other node, then the container precedes the contained node, and reversely the contained node follows the container."
451 # <https://www.w3.org/TR/DOM-Level-3-Core/core.html#DocumentPosition>
452 # We look deeper than our direct child
453 elsif( my $e = $crawl->( $self ) )
454 {
455 $bit |= ( DOCUMENT_POSITION_CONTAINED_BY | DOCUMENT_POSITION_FOLLOWING )
456 }
457 # check for a descendant of a following sibling
458 else
459 {
460 $seen = {};
461 foreach( @$next_siblings )
462 {
463 if( my $e = $crawl->( $_ ) )
464 {
465 $bit |= DOCUMENT_POSITION_FOLLOWING;
466 last;
467 }
468 }
469  
470 # no luck so far. Checking previous sibling of an ancestor
471 if( !( $bit & DOCUMENT_POSITION_FOLLOWING ) )
472 {
473 # Go through each ancestor
474 foreach( @$lineage )
475 {
476 # then get its following siblings
477 my $ancestor_siblings = $_->right;
478 # and check if the other element is one of them
479 if( $ancestor_siblings && $ancestor_siblings->has( $elem ) )
480 {
481 $bit |= DOCUMENT_POSITION_PRECEDING;
482 last;
483 }
484 }
485 }
486 }
487 return( $bit );
488 }
489  
490 sub contains
491 {
492 my $self = shift( @_ );
493 my $elem = shift( @_ ) || return( $self->error( "No element was provided to append." ) );
494 return( $self->error( "Element provided (", overload::StrVal( $elem ), ") is actually not an HTML element." ) ) if( !$self->_is_a( $elem => 'HTML::Object::Element' ) );
495 # Object and comparison object are the same
496 return(1) if( Scalar::Util::refaddr( $self ) eq Scalar::Util::refaddr( $elem ) );
497 my $found = 0;
498 my $seen = {};
499 my $traverse;
500 $traverse = sub
501 {
502 my $e = shift( @_ );
503 return if( !defined( $e ) || !CORE::length( $e ) );
504 my $addr = Scalar::Util::refaddr( $e );
505 return if( CORE::exists( $seen->{ $addr } ) );
506 $seen->{ $addr }++;
507 $e->children->foreach(sub
508 {
509 if( $_->eid eq $elem->eid )
510 {
511 $found++;
512 return(1);
513 }
514 return( $traverse->( $_ ) );
515 });
516 };
517 $traverse->( $self );
518 return( $found );
519 }
520  
521 # Takes a selector; or
522 # Element object
523 # We alias find to findNode(), because find(9 get redefined by HTML::Object::XQuery and
524 # we need internally to continue to rely on this method
525 sub find { return( shift->findNode( @_ ) ); }
526  
527 sub find_xpath
528 {
529 my( $self, $path ) = @_;
530 return( $self->xp->find( $path, $self ) );
531 }
532  
533 sub findNode
534 {
535 my $self = shift( @_ );
536 my $this = shift( @_ );
537 my $opts = $self->_get_args_as_hash( @_ );
538 my $results = $self->new_array;
539  
540 if( ref( $this ) && $self->_is_object( $this ) && $this->isa( 'HTML::Object::DOM::Element' ) )
541 {
542 my $a = $self->new_array( [ $this ] );
543 my $lookup;
544 $lookup = sub
545 {
546 my $kids = shift( @_ );
547 $kids->foreach(sub
548 {
549 my $child = shift( @_ );
550 $a->foreach(sub
551 {
552 my $candidate = shift( @_ );
553 if( $child->eid eq $candidate->eid )
554 {
555 $results->push( $child );
556 # We've added this child. Move to next child.
557 return(1);
558 }
559 });
560 if( $child->children->length > 0 )
561 {
562 $lookup->( $child->children );
563 }
564 });
565 };
566 # Wether this is a collection or just an element object, we check our children
567 $lookup->( $self->children );
568 }
569 # I am expecting an xpath value
570 else
571 {
572 if( ref( $this ) &&
573 (
574 !overload::Overloaded( $this ) ||
575 ( overload::Overloaded( $this ) && !overload::Method( $this, '""' ) )
576 ) )
577 {
578 return( $self->error( "I was expecting an xpath string, but instead I got '$this'." ) );
579 }
580 my $xpath = $self->_xpath_value( $this, $opts ) || return( $self->pass_error );
581 # $self->children->foreach(sub
582 # {
583 # my $child = shift( @_ );
584 # return(1) if( !$child->isElementNode );
585 # # Propagate debug value
586 # $child->debug( $self->debug );
587 # try
588 # {
589 # my @nodes = $child->findnodes( $xpath );
590 # # $self->messagef( 4, "%d nodes found under child element '$child' whose tag is '%s' -> '%s'", scalar( @nodes ), $child->tag, join( "', '", map( overload::StrVal( $_ ), @nodes ) ) );
591 # $results->push( @nodes );
592 # }
593 # catch( $e )
594 # {
595 # warn( "Error while calling findnodes on element id \"", $_->id, "\" and tag \"", $_->tag, "\": $e\n" );
596 # }
597 # });
598 # try-catch
599 local $@;
600 eval
601 {
602 my @nodes = $self->findnodes( $xpath );
603 $results->push( @nodes );
604 };
605 if( $@ )
606 {
607 warn( "Error while calling findnodes on element with tag \"", ( $self->tag // '' ), "\": $@\n" );
608 }
609 }
610 return( $results );
611 }
612  
613 sub findnodes
614 {
615 my( $self, $path ) = @_;
616 return( $self->xp->findnodes( $path, $self ) );
617 }
618  
619 sub findnodes_as_string
620 {
621 my( $self, $path ) = @_;
622 return( $self->xp->findnodes_as_string( $path, $self ) );
623 }
624  
625 sub findnodes_as_strings
626 {
627 my( $self, $path ) = @_;
628 return( $self->xp->findnodes_as_strings( $path, $self ) );
629 }
630  
631 sub findvalue
632 {
633 my( $self, $path ) = @_;
634 return( $self->xp->findvalue( $path, $self ) );
635 }
636  
637 sub findvalues
638 {
639 my( $self, $path ) = @_;
640 return( $self->xp->findvalues( $path, $self ) );
641 }
642  
643 # Note: Property
644 sub firstChild { return( shift->nodes->first ); }
645  
646 sub getAttributes
647 {
648 my $self = shift( @_ );
649 my $rank = 0;
650 my $a = $self->attributes_sequence->map(sub
651 {
652 return( $self->new_attribute(
653 name => $_,
654 element => $self,
655 rank => $rank++,
656 value => $self->attributes->get( $_ ),
657 ) );
658 });
659 return( wantarray() ? $a->list : $a );
660 }
661  
662 sub getChildNodes
663 {
664 my $self = shift( @_ );
665 my $nodes = $self->nodes;
666 return( wantarray() ? $nodes->list : $nodes );
667 }
668  
669 sub getElementById { return; }
670  
671 # sub getFirstChild { return; }
672 sub getFirstChild { return( shift->nodes->first ); }
673  
674 # sub getLastChild { return; }
675 sub getLastChild { return( shift->nodes->last ); }
676  
677 sub getName { return; }
678  
679 sub getNextSibling { return( shift->nextSibling ); }
680  
681 sub getParentNode { return( shift->parent ); }
682  
683 sub getPreviousSibling { return( shift->previousSibling ); }
684  
685 sub getRootNode
686 {
687 my $self = shift( @_ );
688 # The parent of root is a HTML::Object::Root
689 # that helps getting the tree to mimic a DOM tree
690 # return( $self->root->getParentNode );
691 return( $self->root );
692 }
693  
694 sub hasChildNodes { return( !shift->nodes->is_empty ); }
695  
696 sub insertAfter
697 {
698 my $self = shift( @_ );
699 my $e = shift( @_ ) || return( $self->error( "No node was provided to insert." ) );
700 return( $self->error({
701 message => "Node provided (" . overload::StrVal( $e ) . ") is not an HTML::Object::DOM::Node.",
702 class => 'HTML::Object::TypeError',
703 }) ) if( !$self->_is_a( $e => 'HTML::Object::DOM::Node' ) );
704 my $refNode = shift( @_ );
705 my $nodes = $self->nodes;
706 my $pos;
707 if( !defined( $refNode ) )
708 {
709 $pos = $nodes->size;
710 }
711 else
712 {
713 return( $self->error({
714 message => "Reference node provided (" . overload::StrVal( $refNode ) . ") is not an HTML::Object::DOM::Node.",
715 class => 'HTML::Object::TypeError',
716 }) ) if( !$self->_is_a( $refNode => 'HTML::Object::DOM::Node' ) );
717 $pos = $nodes->pos( $refNode );
718 return( $self->error({
719 message => "Reference node provided (" . overload::StrVal( $refNode ) . ") is not among the document nodes.",
720 class => 'HTML::Object::HierarchyRequestError',
721 }) ) if( !defined( $pos ) );
722 return( $self->error( "Somehow, position for the reference node returned $pos, but I expected an integer equal or above 0" ) ) if( $pos < 0 );
723 }
724 my $list = $self->new_array( $self->_is_a( $e => 'HTML::Object::DOM::DocumentFragment' ) ? $e->children : $e );
725 $nodes->splice( $pos + 1, 0, $list->list );
726 $list->foreach(sub
727 {
728 $_->detach;
729 $_->parent( $self );
730 });
731 $self->reset(1);
732 if( $self->_is_a( $e => 'HTML::Object::DOM::DocumentFragment' ) )
733 {
734 $e->children->reset;
735 }
736 return( $e );
737 }
738  
739 sub insertBefore
740 {
741 my $self = shift( @_ );
742 my $e = shift( @_ ) || return( $self->error( "No node was provided to insert." ) );
743 return( $self->error({
744 message => "Node provided (" . overload::StrVal( $e ) . ") is not an HTML::Object::DOM::Node.",
745 class => 'HTML::Object::TypeError',
746 }) ) if( !$self->_is_a( $e => 'HTML::Object::DOM::Node' ) );
747 my $refNode = shift( @_ );
748 my $nodes = $self->nodes;
749 my $pos;
750 if( !defined( $refNode ) )
751 {
752 $pos = $nodes->length; # set the position to size + 1 to make it equivalent to a push()
753 }
754 else
755 {
756 return( $self->error({
757 message => "Reference node provided (" . overload::StrVal( $refNode ) . ") is not an HTML::Object::DOM::Node.",
758 class => 'HTML::Object::TypeError',
759 }) ) if( !$self->_is_a( $refNode => 'HTML::Object::DOM::Node' ) );
760 $pos = $nodes->pos( $refNode );
761 return( $self->error({
762 message => "Reference node provided (" . overload::StrVal( $refNode ) . ") is not among the document nodes.",
763 class => 'HTML::Object::HierarchyRequestError',
764 }) ) if( !defined( $pos ) );
765 return( $self->error( "Somehow, position for the reference node returned $pos, but I expected an integer equal or above 0" ) ) if( $pos < 0 );
766 }
767 my $list = $self->new_array( $self->_is_a( $e => 'HTML::Object::DOM::DocumentFragment' ) ? $e->children : $e );
768 $nodes->splice( $pos, 0, $list->list );
769 $list->foreach(sub
770 {
771 $_->detach;
772 $_->parent( $self );
773 });
774 $self->reset(1);
775 if( $self->_is_a( $e => 'HTML::Object::DOM::DocumentFragment' ) )
776 {
777 $e->children->reset;
778 }
779 return( $e );
780 }
781  
782 sub isAttributeNode { return(0); }
783  
784 sub isCommentNode { return(0); }
785  
786 # Note: Property
787 sub isConnected
788 {
789 my $self = shift( @_ );
790 my $root = $self->root;
791 return( $self->false ) if( !$root );
792 return( $root->isa( 'HTML::Object::Document' ) ? $self->true : $self->false );
793 }
794  
795 sub isDefaultNamespace
796 {
797 my $self = shift( @_ );
798 my $uri = shift( @_ );
799 return( $self->error( "No namespace URI was provided to check." ) ) if( !defined( $uri ) );
800 if( $self->tag eq 'svg' )
801 {
802 return( $self->attributes->get( 'xmlns' ) eq $uri );
803 }
804 else
805 {
806 return( $uri eq "" );
807 }
808 }
809  
810 sub isEqualNode
811 {
812 my $self = shift( @_ );
813 my $e = shift( @_ ) || return( $self->error( "No html element was provided to check for equality." ) );
814 return( $self->error( "Element provided (", overload::StrVal( $e ), ") is not an HTML::Object::Element." ) ) if( !$self->_is_a( $e => 'HTML::Object::Element' ) );
815 return(0) if( $self->nodeType != $e->nodeType );
816 return(0) if( $self->children->length != $e->children->length );
817 return(0) if( $self->attributes_sequence->join( ',' ) ne $e->attributes_sequence->join( ',' ) );
818 my $failed = 0;
819 $self->attributes_sequence->foreach(sub
820 {
821 my $v1 = $self->attributes->get( $_ );
822 my $v2 = $e->attributes->get( $_ );
823 if( $v1 ne $v2 )
824 {
825 $failed++;
826 return(0);
827 }
828 });
829 return(0) if( $failed );
830 return(1);
831 }
832  
833 sub isElementNode { return(0); }
834  
835 sub isNamespaceNode { return(0); }
836  
837 sub isPINode { return(0); }
838  
839 sub isProcessingInstructionNode { return(0); }
840  
841 sub isSameNode
842 {
843 return(0) if( !defined( $_[1] ) );
844 if( !$_[0]->_is_a( $_[1] => 'HTML::Object::DOM::Node' ) )
845 {
846 warn( "Value provided ($_[1]) is not a reference.\n" ) if( $_[0]->_warnings_is_enabled );
847 return(0);
848 }
849 return( Scalar::Util::refaddr( $_[0] ) eq Scalar::Util::refaddr( $_[1] ) );
850 }
851  
852 sub isTextNode { return(0); }
853  
854 # Node: Property
855 sub lastChild { return( shift->nodes->last ); }
856  
857 sub lookupNamespaceURI
858 {
859 my $self = shift( @_ );
860 my $prefix = shift( @_ ) || return( '' );
861 return( XML_DEFAULT_NAMESPACE ) if( lc( $prefix ) eq 'xml' );
862 return( '' );
863 }
864  
865 sub lookupPrefix { return; }
866  
867 sub new_closing
868 {
869 my $self = shift( @_ );
870 $self->_load_class( 'HTML::Object::DOM::Closing' ) || return( $self->pass_error );
871 my $e = HTML::Object::DOM::Closing->new( @_ ) ||
872 return( $self->pass_error( HTML::Object::DOM::Closing->error ) );
873 return( $e );
874 }
875  
876 sub new_comment
877 {
878 my $self = shift( @_ );
879 $self->_load_class( 'HTML::Object::DOM::Comment' ) || return( $self->pass_error );
880 my $e = HTML::Object::DOM::Comment->new( @_ ) ||
881 return( $self->pass_error( HTML::Object::DOM::Comment->error ) );
882 return( $e );
883 }
884  
885 sub new_element
886 {
887 my $self = shift( @_ );
888 $self->_load_class( 'HTML::Object::DOM::Element' ) || return( $self->pass_error );
889 my $e = HTML::Object::DOM::Element->new( @_ ) ||
890 return( $self->pass_error( HTML::Object::DOM::Element->error ) );
891 return( $e );
892 }
893  
894 sub new_parser
895 {
896 my $self = shift( @_ );
897 $self->_load_class( 'HTML::Object::DOM' ) || return( $self->pass_error );
898 my $p = HTML::Object::DOM->new( debug => $self->debug ) ||
899 return( $self->pass_error( HTML::Object::DOM->error ) );
900 return( $p );
901 }
902  
903 sub new_text
904 {
905 my $self = shift( @_ );
906 $self->_load_class( 'HTML::Object::DOM::Text' ) || return( $self->pass_error );
907 my $e = HTML::Object::DOM::Text->new( @_ ) ||
908 return( $self->pass_error( HTML::Object::DOM::Text->error ) );
909 return( $e );
910 }
911  
912 # Note: Property
913 sub nextSibling { return( shift->right->first ); }
914  
915 # Note: Property
916 sub nodeName
917 {
918 my $self = shift( @_ );
919 my $class = ref( $self ) || $self;
920 my $map =
921 {
922 Comment => '#comment',
923 Document => '#document',
924 DocumentFragment => '#documentFragment',
925 Space => '#space',
926 Text => '#text',
927 };
928 my $type = [split( /::/, $class )]->[-1];
929 return( CORE::exists( $map->{ $type } ) ? $map->{ $type } : $self->tag );
930 }
931  
932 # For any nodes except for Document, nodes are the same as children
933 sub nodes { return( shift->children( @_ ) ); }
934  
935 # ELEMENT_NODE 1
936 # ATTRIBUTE_NODE 2
937 # TEXT_NODE 3
938 # CDATA_SECTION_NODE 4
939 # PROCESSING_INSTRUCTION_NODE 7
940 # COMMENT_NODE 8
941 # DOCUMENT_NODE 9
942 # DOCUMENT_TYPE_NODE 10
943 # DOCUMENT_FRAGMENT_NODE 11
944 # <https://developer.mozilla.org/en-US/docs/Web/API/Node/nodeType>
945 # Note: Property
946 sub nodeType
947 {
948 my $self = shift( @_ );
949 my $map =
950 [
951 # Document also inherits from Element, so we test it first
952 'HTML::Object::DOM::Document' => DOCUMENT_NODE,
953 'HTML::Object::DOM::Element' => ELEMENT_NODE,
954 'HTML::Object::DOM::Attribute' => ATTRIBUTE_NODE,
955 'HTML::Object::DOM::Text' => TEXT_NODE,
956 # Nothing for CData (4) or Processing Instruction (7)
957 'HTML::Object::DOM::Comment' => COMMENT_NODE,
958 'HTML::Object::DOM::Declaration' => DOCUMENT_TYPE_NODE,
959 'HTML::Object::DOM::DocumentFragment' => DOCUMENT_FRAGMENT_NODE,
960 # We treat space separately, but the DOM normally treats it as text
961 'HTML::Object::DOM::Space' => SPACE_NODE,
962 ];
963  
964 # return( $map->{ [split( /::/, ( ref( $self ) || $self ) )]->[-1] } );
965 for( my $i = 0; $i < scalar( @$map ); $i += 2 )
966 {
967 my $class = $map->[$i];
968 my $const = $map->[$i + 1];
969 if( $self->isa( $class ) )
970 {
971 return( $const );
972 }
973 }
974 return;
975 }
976  
977 # Note: Property
978 sub nodeValue { return; }
979  
980 sub normalize
981 {
982 my $self = shift( @_ );
983 my $process;
984 $process = sub
985 {
986 my $e = shift( @_ );
987 my $new = $self->new_array;
988 my $found = '';
989 $e->children->foreach(sub
990 {
991 my $kid = shift( @_ );
992 # text
993 if( $kid->nodeType == 3 )
994 {
995 if( $found )
996 {
997 # merge it with previous text element and skip it
998 $found->value->append( $kid->value->scalar );
999 # next
1000 return(1);
1001 }
1002 # We found a text element. Save it and merge it with siblings, if any
1003 else
1004 {
1005 $found = $kid;
1006 }
1007 }
1008 else
1009 {
1010 $found = '';
1011 }
1012 $new->push( $kid );
1013 });
1014 # Set the new children elements array object
1015 $e->children( $new );
1016 };
1017 $process->( $self );
1018 return( $self );
1019 }
1020  
1021 # Note: Property
1022 sub ownerDocument
1023 {
1024 my $self = shift( @_ );
1025 my $root = $self->root;
1026 return( $root );
1027 }
1028  
1029 sub parent { return( shift->_set_get_object_without_init({
1030 field => 'parent',
1031 weaken => 1,
1032 }, 'HTML::Object::DOM::Node', @_ ) ); }
1033  
1034 # Note: Property
1035 sub parentNode { return( shift->parent ); }
1036  
1037 # Note: Property
1038 # "Returns an Element that is the parent of this node. If the node has no parent, or if that parent is not an Element, this property returns null."
1039 sub parentElement
1040 {
1041 my $self = shift( @_ );
1042 my $parent = $self->parent;
1043 return( $parent ) if( defined( $parent ) && $self->_is_a( $parent => 'HTML::Object::DOM::Element' ) );
1044 return;
1045 }
1046  
1047 # Note: Property
1048 # The last element of our siblings, is our first element on our left
1049 sub previousSibling { return( shift->left->last ); }
1050  
1051 sub removeChild
1052 {
1053 my $self = shift( @_ );
1054 my $elem = shift( @_ ) || return( $self->error({
1055 message => "No element was provided to remove.",
1056 class => 'HTML::Object::TypeError'
1057 }) );
1058 return( $self->error({
1059 message => "Element provided (" . overload::StrVal( $elem ) . ") is actually not an HTML element.",
1060 class => 'HTML::Object::TypeError'
1061 }) ) if( !$self->_is_a( $elem => 'HTML::Object::Element' ) );
1062 my $nodes = $self->nodes;
1063 my $pos = $nodes->pos( $elem );
1064 return( $self->error({
1065 message => "Node to remove was not found among the current object's children.",
1066 class => 'HTML::Object::NotFoundError',
1067 }) ) if( !defined( $pos ) );
1068 $nodes->splice( $pos, 1 );
1069 $elem->parent( undef );
1070 # Remove the closing tag also, if there are any.
1071 if( my $close = $elem->close_tag )
1072 {
1073 if( defined( $pos = $nodes->pos( $close ) ) )
1074 {
1075 $nodes->splice( $pos, 1 );
1076 }
1077 }
1078 $self->reset(1);
1079 return( $elem );
1080 }
1081  
1082 sub replaceChild
1083 {
1084 my $self = shift( @_ );
1085 return( $self->error({
1086 message => sprintf( "At least 2 arguments are required, but only %d provided.", scalar( @_ ) ),
1087 class => 'HTML::Object::TypeError',
1088 }) ) if( scalar( @_ ) < 2 );
1089 my( $new, $old ) = @_;
1090 my $new_parent = $new->parent;
1091 my $old_parent = $old->parent;
1092 my $parent = $self->parent;
1093 if( !$parent )
1094 {
1095 return( $self->error({
1096 message => "Current node does not have any parent.",
1097 class => 'HTML::Object::HierarchyRequestError',
1098 }) );
1099 }
1100 elsif( !$old_parent )
1101 {
1102 return( $self->error({
1103 message => "Old node provided does not have any parent",
1104 class => 'HTML::Object::HierarchyRequestError',
1105 }) );
1106 }
1107 elsif( $old_parent ne $self )
1108 {
1109 return( $self->error({
1110 message => "Old node parent is not the current node.",
1111 class => 'HTML::Object::NotFoundError',
1112 }) );
1113 }
1114 elsif( !$self->_is_a( $old_parent => 'HTML::Object::DOM::Document' ) &&
1115 !$self->_is_a( $old_parent => 'HTML::Object::DOM::DocumentFragment' ) &&
1116 !$self->_is_a( $old_parent => 'HTML::Object::DOM::Element' ) )
1117 {
1118 return( $self->error({
1119 message => "Old node's parent is not an HTML::Object::DOM::Document, HTML::Object::DOM::DocumentFragment or HTML::Object::DOM::Element object.",
1120 class => 'HTML::Object::HierarchyRequestError',
1121 }) );
1122 }
1123 elsif( !$self->_is_a( $new => 'HTML::Object::DOM::DocumentFragment' ) &&
1124 !$self->_is_a( $new => 'HTML::Object::DOM::Declaration' ) &&
1125 !$self->_is_a( $new => 'HTML::Object::DOM::Element' ) &&
1126 !$self->_is_a( $new => 'HTML::Object::DOM::CharacterData' ) )
1127 {
1128 return( $self->error({
1129 message => "New node is not an HTML::Object::DOM::DocumentFragment, HTML::Object::DOM::Declaration, HTML::Object::DOM::Element or HTML::Object::DOM::CharacterData object.",
1130 class => 'HTML::Object::HierarchyRequestError',
1131 }) );
1132 }
1133 elsif( $self->lineage->has( $new ) )
1134 {
1135 return( $self->error({
1136 message => "New node provided is an ancestor of the current node.",
1137 class => 'HTML::Object::HierarchyRequestError',
1138 }) );
1139 }
1140 elsif( ( $self->_is_a( $new => 'HTML::Object::DOM::Text' ) || $self->_is_a( $new => 'HTML::Object::DOM::Space' ) ) &&
1141 $self->_is_a( $new_parent => 'HTML::Object::DOM::Document' ) )
1142 {
1143 return( $self->error({
1144 message => "New node is a HTML::Object::DOM::Text or HTML::Object::DOM::Space node and its parent is a HTML::Object::DOM::Document node.",
1145 class => 'HTML::Object::HierarchyRequestError',
1146 }) );
1147 }
1148 elsif( $self->isa( 'HTML::Object::DOM::Declaration' ) && !$self->_is_a( $parent => 'HTML::Object::DOM::Document' ) )
1149 {
1150 return( $self->error({
1151 message => "Current node is a DocumentType, but its parent is not an HTML::Object::DOM::Document object.",
1152 class => 'HTML::Object::HierarchyRequestError',
1153 }) );
1154 }
1155 elsif( $self->_is_a( $parent => 'HTML::Object::DOM::Document' ) &&
1156 $self->_is_a( $new => 'HTML::Object::DOM::DocumentFragment' ) &&
1157 ( $new->childElementCount > 1 || $new->children->grep(sub{ $self->_is_a( $_ => 'HTML::Object::DOM::Text' ) })->length ) )
1158 {
1159 return( $self->error({
1160 message => "Current node parent is a HTML::Object::DOM::Document object and new node is a HTML::Object::DOM::DocumentFragment object that has either more than 1 element or has a HTML::Object::DOM::Text node.",
1161 class => 'HTML::Object::HierarchyRequestError',
1162 }) );
1163 }
1164 elsif( $self->_is_a( $parent => 'HTML::Object::DOM::Document' ) &&
1165 $parent->childElementCount > 0 &&
1166 $self->_is_a( $new => 'HTML::Object::DOM::Element' ) &&
1167 # Non-standard addition:
1168 # replacement is not forbidden if the user replace an element that is not an HTML element by an HTML element and there is no HTML element yet or
1169 # the user replace an HTML element by another HTML element
1170 !(
1171 ( $self->_is_a( $old => 'HTML::Object::DOM::Element' ) &&
1172 !$self->_is_a( $old => 'HTML::Object::DOM::Element::HTML' ) &&
1173 $parent->children->grep(sub{ $self->_is_a( $_ => 'HTML::Object::DOM::Element::HTML' ) })->is_empty &&
1174 $self->_is_a( $new => 'HTML::Object::DOM::Element::HTML' ) ) ||
1175 ( $self->_is_a( $old => 'HTML::Object::DOM::Element::HTML' ) &&
1176 $self->_is_a( $new => 'HTML::Object::DOM::Element::HTML' ) )
1177 ) )
1178 {
1179 return( $self->error({
1180 message => "Attempting to replace a child element in a Document with another non HTML-tag element. Document can have only one Element: the HTML-tag element.",
1181 class => 'HTML::Object::HierarchyRequestError',
1182 }) );
1183 }
1184 elsif( $self->_is_a( $new => 'HTML::Object::DOM::Element' ) &&
1185 $self->_is_a( $old->previousSibling => 'HTML::Object::DOM::Declaration' ) )
1186 {
1187 return( $self->error({
1188 message => "Attempting to add an Element object before a DocumentType object",
1189 class => 'HTML::Object::HierarchyRequestError',
1190 }) );
1191 }
1192 # We use 'nodes' rather than 'children' so this works well with HTML::Object::DOM::Document
1193 my $nodes = $self->nodes;
1194 my $newPos = $nodes->pos( $new );
1195 my $oldPos = $nodes->pos( $old );
1196 if( defined( $newPos ) && !defined( $oldPos ) )
1197 {
1198 return( $self->error({
1199 message => "New child already has this parent and old child does not. Please check the order of replaceChild's arguments.",
1200 class => 'HTML::Object::NotFoundError',
1201 }) );
1202 }
1203 elsif( !defined( $oldPos ) )
1204 {
1205 return( $self->error({
1206 message => "Child to be replaced is not a child of this node",
1207 class => 'HTML::Object::NotFoundError',
1208 }) );
1209 }
1210 $new->detach;
1211 my $new_array = $self->new_array( $self->_is_a( $new => 'HTML::Object::DOM::DocumentFragment' ) ? $new->children : $new );
1212 $new_array->foreach(sub
1213 {
1214 return(1) if( !$self->_is_a( $_ => 'HTML::Object::DOM::Node' ) );
1215 $_->parent( $self );
1216 });
1217 $nodes->splice( $oldPos, 1, $new_array->list );
1218 $old->parent( undef );
1219 $self->reset(1);
1220 return( $old );
1221 }
1222  
1223 sub textContent : lvalue { return( shift->_set_get_callback({
1224 get => sub
1225 {
1226 my $self = shift( @_ );
1227 return if( $self->isa( 'HTML::Object::DOM::Document' ) );
1228 unless( $self->isa( 'HTML::Object::DOM::Comment' ) ||
1229 $self->isa( 'HTML::Object::DOM::Text' ) ||
1230 $self->isa( 'HTML::Object::DOM::Element' ) )
1231 {
1232 return;
1233 }
1234 my $str = $self->as_text;
1235 $str =~ s,<br[[:blank:]]*/>\n,\n,gs;
1236 $str =~ s/\&gt;/>/gs;
1237 $str =~ s/\&lt;/</gs;
1238 return( $str );
1239 },
1240 set => sub
1241 {
1242 my $self = shift( @_ );
1243 my $ctx = $_;
1244 my $arg = shift( @_ );
1245 return if( $self->isa( 'HTML::Object::DOM::Document' ) );
1246 my $dummy;
1247 if( $self->isa( 'HTML::Object::DOM::Comment' ) ||
1248 $self->isa( 'HTML::Object::DOM::Text' ) ||
1249 $self->isa( 'HTML::Object::DOM::Element' ) )
1250 {
1251 if( $self->isa( 'HTML::Object::DOM::Comment' ) ||
1252 $self->isa( 'HTML::Object::DOM::Text' ) )
1253 {
1254 $self->value( $arg );
1255 }
1256 else
1257 {
1258 my $val = "$arg";
1259 $val =~ s/</\&lt;/gs;
1260 $val =~ s/>/\&gt;/gs;
1261 $val =~ s,\n,<br />\n,gs;
1262 my $e = $self->new_text( value => $val, parent => $self );
1263 $self->children->set( $e );
1264 }
1265 $self->reset(1);
1266 $dummy = 1;
1267 return( $dummy );
1268 }
1269 return( $dummy );
1270 },
1271 }, @_ ) ); }
1272  
1273 sub trigger
1274 {
1275 my $self = shift( @_ );
1276 my $type = shift( @_ );
1277 return( $self->error({
1278 message => "No event type was provided to trigger.",
1279 class => 'HTML::Object::SyntaxError',
1280 }) ) if( !defined( $type ) || !CORE::length( "$type" ) );
1281 return( $self->error({
1282 message => "Event type provided \"$type\" contains illegal characters. Only alphanuneric, underscore (\"_\") and dash (\"-\") are allowed",
1283 class => 'HTML::Object::TypeError',
1284 }) ) if( $type !~ /^\w[\w\-]*$/ );
1285 require HTML::Object::Event;
1286 my $evt = HTML::Object::Event->new( $type, @_ ) ||
1287 return( $self->pass_error( HTML::Object::Event->error ) );
1288 return( $self->dispatchEvent( $evt ) );
1289 }
1290  
1291 sub xp
1292 {
1293 my $self = shift( @_ );
1294 unless( $XP )
1295 {
1296 $self->_load_class( 'HTML::Object::XPath' ) || return( $self->pass_error );
1297 $XP = HTML::Object::XPath->new;
1298 }
1299 # $XP->debug( $self->debug );
1300 return( $XP );
1301 }
1302  
1303 sub _list_to_nodes
1304 {
1305 my $self = shift( @_ );
1306 my $list = $self->new_array;
1307 my $p = $self->new_parser;
1308 my $prev;
1309 foreach( @_ )
1310 {
1311 if( $self->_is_a( $_ => 'HTML::Object::DOM::Node' ) )
1312 {
1313 if( $self->_is_a( $_ => 'HTML::Object::DOM::DocumentFragment' ) )
1314 {
1315 my $kids = $_->children->clone;
1316 $_->children->reset;
1317 foreach my $kid ( @$kids )
1318 {
1319 $kid->detach;
1320 }
1321 $list->push( $kids->list );
1322 }
1323 else
1324 {
1325 $list->push( $_ );
1326 $_->detach;
1327 }
1328 undef( $prev );
1329 }
1330 # HTML string
1331 elsif( !ref( $_ ) || ( ref( $_ ) && overload::Method( $_, '""' ) ) )
1332 {
1333 if( $self->looks_like_html( $_ ) )
1334 {
1335 my $doc = $p->parse_data( $_ ) ||
1336 return( $self->pass_error({ class => 'HTML::Object::TypeError' }) );
1337 $list->push( $doc->children->list ) if( !$doc->children->is_empty );
1338 undef( $prev );
1339 }
1340 else
1341 {
1342 if( defined( $prev ) && $self->_is_a( $prev => 'HTML::Object::DOM::Text' ) )
1343 {
1344 $prev->value->append( $_ );
1345 }
1346 else
1347 {
1348 my $e = $self->new_text( value => $_ );
1349 $list->push( $e );
1350 $prev = $e;
1351 }
1352 }
1353 }
1354 else
1355 {
1356 return( $self->error({
1357 message => "Unsupported data provided (" . overload::StrVal( $_ ) . ").",
1358 class => 'HTML::Object::TypeError',
1359 }) );
1360 }
1361 }
1362 return( $list );
1363 }
1364  
1365 sub _xpath_value
1366 {
1367 my $self = shift( @_ );
1368 my $this = shift( @_ );
1369 my $opts = $self->_get_args_as_hash( @_ );
1370 if( ref( $this ) )
1371 {
1372 return( $$this );
1373 }
1374 else
1375 {
1376 $self->_load_class( 'HTML::Selector::XPath' ) ||
1377 return( $self->pass_error );
1378 # try-catch
1379 local $@;
1380 my $rv = eval
1381 {
1382 return( HTML::Selector::XPath::selector_to_xpath( $this, %$opts ) );
1383 };
1384 if( $@ )
1385 {
1386 return( $self->error( "Bad selector \"$this\": $@" ) );
1387 }
1388 return( $rv );
1389 }
1390 }
1391  
1392 1;
1393 # NOTE: POD
1394 __END__