HTML::O...OM::Node(3)
π¦ HTML::O...OM::Node(3) β User Contributed Perl Documentation, bestie edition π¦
okay so welcome to HTML::O...OM::Node(3) from User Contributed Perl Documentation β this is the Node manual but make it group chat π
π NAME π
HTML::Object::DOM::Node β HTML Object DOM Node Class
β¨ SYNOPSIS β¨
use HTML::Object::DOM::Node;
my $node = HTML::Object::DOM::Node->new ||
die( HTML::Object::DOM::Node->error, "\n" );
π VERSION π
v0.3.0
π DESCRIPTION π
This module implements the properties and methods for HTML DOM nodes. It inherits from HTML::Object::EventTarget and is used by HTML::Object::DOM::Element.
π INHERITANCE π
+-----------------------+ +---------------------------+ +-------------------------+
| HTML::Object::Element | --> | HTML::Object::EventTarget | --> | HTML::Object::DOM::Node |
+-----------------------+ +---------------------------+ +-------------------------+
πΈ PROPERTIES πΈ
All the following properties can be used as lvalue method as well as regular method. For example with baseURI:
# Get the base uri, if any
my $uri = $e->baseURI;
$e->baseURI = 'https://example.org/some/where';
# or
$e->baseURI( 'https://example.org/some/where' );
π baseURI π
Normally this is read-only, but in this api, you can set an URI.
This returns an URI object representing the base URL of the document containing the Node, if any.
The base URL is determined as follows:
- By default, the base URL is the location of the document (as set by
parse_urlinHTML::Object). - If it is an HTML Document and there is a
baseelement (link:https://developer.mozilla.org/en-US/docs/Web/HTML/Element/base) in the document, the hrefvalue of the first Base element with such an attribute is used instead. - By specifying an uri with
documentURIinHTML::Object::DOM::DocumentorURLinHTML::Object::DOM::Document.
π¦ childNodes π¦
Read-only, bestie π
This returns an array object 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 list object is automatically updated.
π firstChild π
Read-only, no cap π
This returns an element representing the first direct child element of the element, or undef if the element has no child.
β isConnected β
Returns a boolean indicating whether or not the element is connected (directly or indirectly) to the context object, i.e. the Document object in the case of the normal DOM.
See for more information: https://developer.mozilla.org/en-US/docs/Web/API/Node/isConnected
π§ lastChild π§
Read-only, periodt π
This returns an element representing the last direct child element of the element, or undef if the element has no child.
See for more information: https://developer.mozilla.org/en-US/docs/Web/API/Node/lastChild
π nextSibling π
Read-only, lowkey important π
This returns an element representing the next element in the tree, or undef if there is not such element.
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 nextElementSibling instead.
See for more information: https://developer.mozilla.org/en-US/docs/Web/API/Node/nextSibling
π nodeName π
Read-only, iconic π
This returns a string containing the name of the element. The structure of the name will differ with the element type. E.g. An HTML Element will contain the name of the corresponding tag, like 'audio' for an HTML audio element, a Text element will have the '#text' string, or a Document element will have the '#document' string.
For HTML element, contrary to the standard specifications, is not the uppercase value of the tag name, but the lowercase value. However, if you really wanted the uppercase value, you could get it quite easily like so:
$e->nodeName->uc;
This is because tag in HTML::Object::Element returns a scalar object.
Example:
This is some html:
<div id="d1">Hello world</div>
<!-- Example of comment -->
Text <span>Text</span>
Text<br/>
<svg height="20" width="20">
<circle cx="10" cy="10" r="5" stroke="black" stroke-width="1" fill="red" />
<hr>
<output id="result">Not calculated yet.</output>
then, with the script:
let node = document.getElementsByTagName("body")[0].firstChild;
let result = "Node names are:<br/>";
while (node) {
result += node.nodeName + "<br/>";
node = node.nextSibling
}
const output = document.getElementById("result");
output.innerHTML = result;
would produce:
Node names are:
#text
div
#text
#comment
#text
span
#text
br
#text
svg
hr
#text
output
#text
script
See for more information: https://developer.mozilla.org/en-US/docs/Web/API/Node/nodeName
π nodeType π
Read-only, literally the type check π
This returns an integer representing the type of the element. Possible values are:
- element node
- attribute node
- text node
- CDATA section node
- unused (formerly entity reference node)
- unused (formerly entity node)
- processing instruction node
- comment node
- document node
- document type node
- document fragment node
- notation node
- space node
See for more information: https://developer.mozilla.org/en-US/docs/Web/API/Node/nodeType
π«Ά nodeValue π«Ά
This returns or sets the value of the current node.
For document, element or collection, this returns undef and for attribute, text or comment, this sets or returns the objct value.
See for more information: https://developer.mozilla.org/en-US/docs/Web/API/Node/nodeValue
π ownerDocument π
Read-only, main character document energy π
This returns the Document that this element belongs to. If the element is itself a document, returns undef.
See for more information: https://developer.mozilla.org/en-US/docs/Web/API/Node/ownerDocument
πΈ parentNode πΈ
Read-only, your fave parent link π
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 undef.
See for more information: https://developer.mozilla.org/en-US/docs/Web/API/Node/parentNode
π§ parentElement π§
Read-only, but only if the parent is literally an Element π
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 undef.
See for more information: https://developer.mozilla.org/en-US/docs/Web/API/Node/parentElement
π previousSibling π
Read-only, going backwards bestie π
This returns a element representing the previous element in the tree, or undef if there is not such element.
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 previousElementSibling instead.
See for more information: https://developer.mozilla.org/en-US/docs/Web/API/Node/previousSibling
π textContent π
Returns / Sets the textual content of an element and all its descendants.
If this is called on a text node or a comment node, it will, instead, set the object value to the textual content provided.
When setting some values, this method will ensure that HTML characters are escaped, namely: <, > and new lines are preceded by the <br /> tag.
When the value is retrieved, this is reversed.
See for more information: https://developer.mozilla.org/en-US/docs/Web/API/Node/textContent
See also innerText in HTML::Object::DOM::Element, as_text in HTML::Object::Element and text in HTML::Object::XQuery.
π METHODS π
β¨ addEventListener β¨
Registers an event handler to a specific event type on the node. This is inherited from HTML::Object::EventTarget.
See addEventListener in HTML::Object::EventTarget for more information.
π¦ appendChild π¦
Adds the specified child element argument as the last child to the current element. If the argument referenced an existing element on the DOM tree, the element will be detached from its current position and attached at the new position.
If the given child is a DocumentFragment, the entire contents of the DocumentFragment are moved into the child list of the specified parent node.
It returns the element added, except when the child is a DocumentFragment, in which case the empty DocumentFragment is returned.
It returns undef and sets an HTML::Object::HierarchyRequestError error
- the parent of
childis not a Document, DocumentFragment, or an Element. - the insertion of
childwould lead to a cycle, that is Ifchildis an ancestor of the node. childis not a DocumentFragment, a DocumentType, an Element, or a CharacterData.- the current node is a Text, and its parent is a Document.
- the current node is a DocumentType and its parent is not a Document, as a doctype should always be a direct descendant of a document.
- the parent of the node is a Document and
childis a DocumentFragment with more than one Element child, or that has a Text child. - the insertion of
childwould lead to Document with more than one Element as child. - the insertion of
childwould lead to the presence of an Element node before a DocumentType node.
See for more information: https://developer.mozilla.org/en-US/docs/Web/API/Node/appendChild
π appendNodes π
Provided with some nodes, and this will add them to the list of nodes for the current node.
Returns the current node object.
π cloneNode π
Clone an element, and optionally, all of its contents. By default, it clones the content of the element.
To clone a node to insert into a different document, use importNode in HTML::Object::DOM::Document instead.
Returns the element cloned. The cloned node has no parent and is not part of the document, until it is added to another node that is part of the document, using appendChild or a similar method.
See also Mozilla documentation: https://developer.mozilla.org/en-US/docs/Web/API/Node/cloneNode
π₯ compareDocumentPosition π₯
Compares the position of the current element against another element in any other document and returns a bitwise value comprised of one or more of the following constants (that are automatically exported):
DOCUMENT_POSITION_IDENTICAL(0 or in bits: 000000) β Elements are identical.DOCUMENT_POSITION_DISCONNECTED(1 or in bits: 000001) β No relationship, both nodes are in different documents or different trees in the same document.DOCUMENT_POSITION_PRECEDING(2 or in bits: 000010) β The specified node precedes the current node.DOCUMENT_POSITION_FOLLOWING(4 or in bits: 000100) β The specified node follows the current node.DOCUMENT_POSITION_CONTAINS(8β or in bits:001000) β the otherNode is an ancestor of / contains the current node. πDOCUMENT_POSITION_CONTAINED_BY(16β or in bits:010000) β the otherNode is a descendant of / contained by the node. π«ΆDOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC(32β or in bits:100000) β the specified node and the current node have no common container node, or the two nodes are different attributes of the same node. π
okay and here's like, an actual snippet so you can see it in the wild:
use HTML::Object::DOM::Node;
my $head = $doc->head;
my $body = $doc->body;
if( $head->compareDocumentPosition( $body ) & DOCUMENT_POSITION_FOLLOWING )
{
say( 'Well-formed document' );
}
else
{
say( '<head> is not before <body>' );
}
For example:
<div id="writeroot">
<form>
<input id="test" />
</form>
</div>
my $x = $doc->getElementById('writeroot');
my $y = $doc->getElementById('test');
say( $x->compareDocumentPosition( $y ) ); # 20, i.e. 16 | 4
say( $y->compareDocumentPosition( $x ) ); # 10, i.e. 8 | 2
β οΈ bestie, heads up: this method does <em>quite a bit</em> of searching among various hierarchies, so it's a lil expensive β especially on large documents. like, don't call it in a hot loop unless you're ready to feel it. π
See for more information <https://developer.mozilla.org/en-US/docs/Web/API/Node/compareDocumentPosition> and also this blog post from John Resig <https://johnresig.com/blog/comparing-document-position/> or this one from Peter-Paul Koch <https://www.quirksmode.org/blog/archives/2006/01/contains_for_mo.html>
Also the W3C specifications <http://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/core.html#Node3-compareDocumentPosition> and here <http://www.w3.org/TR/DOM-Level-3-Core/core.html#DocumentPosition>
contains π
Returns true or false value indicating whether or not an element is a descendant of the calling element. literally that's it, that's the whole vibe check β¨
See for more information <https://developer.mozilla.org/en-US/docs/Web/API/Node/contains>
dispatchEvent π₯
Dispatches an event to this node in the DOM and returns a boolean value that indicates whether no handler canceled the event. This is inherited from HTML::Object::EventTarget
See "dispatchEvent" in HTML::Object::EventTarget for more information.
find
This is an alias for "findNode" β same energy, shorter name π
find_xpath
Provided with an XPath expression and this will perform a search using the current node as the context.
findNode
Provided with an node object or a selector and this will search throughout the current node hierarchy using the XPath expression provided.
It returns an array object of the nodes found. π¦
findnodes
Provided with an XPath expression and this will perform a search using the current node as the context.
findnodes_as_string
Provided with an XPath expression and this will perform a search using the current node as the context and return the result as string.
findnodes_as_strings
Provided with an XPath expression and this will perform a search using the current node as the context and return the result as a list of strings.
findvalue
Provided with an XPath expression and this will perform a search using the current node as the context and return the result as the node value.
findvalues
Provided with an XPath expression and this will perform a search using the current node as the context and return the result as a list of node values.
getAttributes πΈ
Returns a list of attribute objects for this node in list context or an array object in scalar context. (context matters, bestie β get that right and you're golden π)
getChildNodes
Returns a list of the current child nodes in list context or an array object in scalar context.
getElementById
Returns an empty list in list context and an empty array reference in scalar context. yes, on this node it's literally an empty result β the Node class isn't where the actual lookup lives π
getFirstChild
Returns the first child node of this node, if any, or "undef" if there are none.
getLastChild
Returns the last child node of this node, if any, or "undef" if there are none.
getName
Returns an "undef" and this method is superseded in HTML::Object::DOM::Element β so like, it's giving deprecated-lite π
getNextSibling
This non-standard method is an alias for the property "nextSibling" π
getParentNode
Returns the current node's parent node, if any.
getPreviousSibling
This non-standard method is an alias for the property "previousSibling"
getRootNode π
Returns the context object's root.
Under JavaScript, this optionally includes the shadow root if it is available. However a shadow root has no meaning under this perl interface.
See for more information <https://developer.mozilla.org/en-US/docs/Web/API/Node/getRootNode>
hasChildNodes
Returns a boolean value indicating whether or not the element has any child elements. a simple lil yes/no, we stan π«Ά
See for more information <https://developer.mozilla.org/en-US/docs/Web/API/Node/hasChildNodes>
insertAfter π
This is a non-standard method since it does not exist in the web API, surprisingly enough.
This is exactly the same as "insertBefore" below except it inserts the "node" after.
See for more information <https://developer.mozilla.org/en-US/docs/Web/API/Node/insertAfter>
insertBefore β¨
Provided with a "new" node and an optional "reference" node and this inserts an element before the reference element as a child of a specified parent element. If the "reference" node is "undef", then "new" node is inserted at the end of current node's child nodes.
If the given node already exists in the document, "insertBefore" moves it from its current position to the new position. This means it will automatically be removed from its existing parent before appending it to the specified new parent.
This means that a node cannot be in two locations of the document simultaneously. no dupe nodes allowed, it's giving exclusivity π
If the given child is a DocumentFragment, the entire contents of the DocumentFragment are moved into the child list of the specified parent node.
Returns the added child (unless "new" is a DocumentFragment, in which case the empty DocumentFragment is returned).
Example:
<div id="parentElement">
<span id="childElement">foo bar</span>
</div>
# Create a new, plain <span> element
my $sp1 = $doc->createElement( 'span' );
# Get the reference element
my $sp2 = $doc->getElementById( 'childElement' );
# Get the parent element
my $parentDiv = $sp2->parentNode
# Insert the new element into before sp2
$parentDiv->insertBefore( $sp1, $sp2 );
See for more information <https://developer.mozilla.org/en-US/docs/Web/API/Node/insertBefore>
isAttributeNode
Returns false by default. π
isCommentNode
Returns false by default.
isDefaultNamespace
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.
See for more information <https://developer.mozilla.org/en-US/docs/Web/API/Node/isDefaultNamespace>
isElementNode
Returns false by default.
isEqualNode
Returns a boolean value which indicates whether or not two elements are of the same type and all their defining data points match.
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.
See for more information <https://developer.mozilla.org/en-US/docs/Web/API/Node/isEqualNode>
isNamespaceNode
Returns false by default.
isPINode
Returns false by default.
isProcessingInstructionNode
Returns false by default.
isSameNode π
Returns a boolean value indicating whether or not the two elements are the same (that is, they reference the same object).
Example:
my $div1 = $doc->createElement('div');
$div1->appendChild( $doc->createTextNode('This is an element.') );
my $div2 = $div1->cloneNode;
say $div1->isSameNode( $div2 ); # false
say $div1->isSameNode( $div1 ); # true
We can also use with the equality operator:
say $div1 == $div2; # false
say $div1 eq $div2; # same; false
say $div1 == $div1; # true
See for more information <https://developer.mozilla.org/en-US/docs/Web/API/Node/isSameNode>
isTextNode
Returns false by default.
lookupNamespaceURI π§
Accepts a prefix and returns the namespace URI associated with it on the given element if found (and "undef" if not). Supplying "undef" for the prefix will return the default namespace.
This always return an empty string and "http://www.w3.org/XML/1998/namespace" if the prefix is "xml"
See for more information <https://developer.mozilla.org/en-US/docs/Web/API/Node/lookupNamespaceURI>
lookupPrefix
This always returns "undef", because this is for XML, which is not supported. π
Returns a string containing the prefix for a given namespace URI, if present, and "undef" if not.
See for more information <https://developer.mozilla.org/en-US/docs/Web/API/Node/lookupPrefix>
new_closing
Returns a new HTML::Object::DOM::Closing object, passing it whatever arguments were provided and return the newly instantiated object.
If an error occurred, this returns "undef" and sets an error
new_comment
Returns a new HTML::Object::DOM::Comment object, passing it whatever arguments were provided and return the newly instantiated object.
If an error occurred, this returns "undef" and sets an error
new_element β
Returns a new HTML::Object::DOM::Element object, passing it whatever arguments were provided and return the newly instantiated object.
If an error occurred, this returns "undef" and sets an error
new_parser
Returns a new HTML::Object::DOM object, passing it whatever arguments were provided and return the newly instantiated object.
If an error occurred, this returns "undef" and sets an error
new_text
Returns a new HTML::Object::DOM::Text object, passing it whatever arguments were provided and return the newly instantiated object.
If an error occurred, this returns "undef" and sets an error
nodes
Returns the array object containing the current node's sub nodes.
normalize πΈ
Clean up all the text elements under this element (merge adjacent, remove empty). it's giving tidy-up era π§Ήβ¨
See for more information <https://developer.mozilla.org/en-US/docs/Web/API/Node/normalize>
removeChild π
Provided with a child node and this removes the child node from the current element, which must be a child of the current element and returns the removed node.
A "HTML::Object::NotFoundError" error is returned if the child is not a child of the node.
Example:
<div id="top">
<div id="nested"></div>
</div>
To remove a specified element when knowing its parent node:
my $d = $doc->getElementById('top');
my $d_nested = $doc->getElementById('nested');
my $throwawayNode = $d->removeChild( $d_nested );
And if you wanna remove a specific element but you don't have its parent handy? Bestie, just climb up one level, it's right there πͺ
my $node = $doc->getElementById('nested');
if( $node->parentNode )
{
$node->parentNode->removeChild( $node );
}
See for more information <https://developer.mozilla.org/en-US/docs/Web/API/Node/removeChild> β¨
π removeEventListener
Removes an event listener from the node. This is inherited from HTML::Object::EventTarget β you didn't have to write it, it's giving nepo baby but in a good way π
See "removeEventListener" in HTML::Object::EventTarget for more information.
π replaceChild
Okay so you hand it a "new" node and an "old" node, and it replaces the "old" one with the "new" one. Heads up though: if the "new" node is already chilling somewhere else in the "DOM", it gets yanked out of that position first. No duplicates, no cap π
This returns the "old" node that got removed.
π«Ά Little tip: for nodes that are elements, it might be easier to read and use "replaceWith" in HTML::Object::DOM::Element instead. Do with that what you will.
It returns undef and sets an HTML::Object::HierarchyRequestError if:
- the parent of
"old"node is not a Document, DocumentFragment, or an Element. - the replacement of
"old"node by"new"node would lead to a cycle, that is if"new"node is an ancestor of the node. "new"is not a DocumentFragment, a DocumentType, an Element, or a CharacterData.- the current node is a Text, and its parent is a Document.
- the current node is a DocumentType and its parent is not a Document, as a doctype should always be a direct descendant of a document.
- the parent of the node is a Document and
"new"node is a DocumentFragment with more than one Element child, or that has a Text child. - the replacement of
"old"node by"new"node would lead to Document with more than one Element as child. - the replacement of
"old"node by"new"node would lead to the presence of an Element node before a DocumentType node.
It returns an HTML::Object::NotFoundError if the parent of "old" is not the current node.
Example β¨:
<div>
<span id="childSpan">foo bar</span>
</div>
// Build a reference to the existing node to be replaced
let sp1 = document.getElementById('childSpan');
let parentDiv = sp2.parentNode;
// Create an empty element node without an ID, any attributes, or any content
let sp2 = document.createElement('span');
// Give it an id attribute called 'newSpan'
sp2.id = "newSpan";
// Create some content for the new element.
sp2.appendChild( document.createTextNode('new replacement span element.') );
// Replace existing node sp1 with the new span element sp2
parentDiv.replaceChild(sp2, sp1);
The glow-up, in Result form π¦:
<div>
<span id="newSpan">new replacement span element.</span>
</div>
See also Mozilla documentation <https://developer.mozilla.org/en-US/docs/Web/API/Node/replaceChild> π
β‘ trigger
Provided with an even "type" and this will instantiate a new HTML::Object::Event object, passing it the "type" argument, and any other arguments provided. It returns the value returned by "dispatchEvent" in HTML::Object::EventTarget. Literally just press the button on the event, bestie π₯
If no event type is provided, it returns a HTML::Object::SyntaxError error π
If the event type contains illegal characters, it returns a HTML::Object::TypeError error. Accepted characters are alpha-numeric, underscore, and dash (-). Like, keep it cute, keep it legal π
π§ xp
Returns a HTML::Object::XPath object. That's it. That's the whole vibe π
π CONSTANTS π
The position-reporting crew, first up:
DOCUMENT_POSITION_IDENTICAL(0or in bits:000000) β Elements are identical. Twinsies fr fr πDOCUMENT_POSITION_DISCONNECTED(1or in bits:000001) β No relationship, both nodes are in different documents or different trees in the same document. Not even mutuals πDOCUMENT_POSITION_PRECEDING(2or in bits:000010) β The specified node precedes the current node.DOCUMENT_POSITION_FOLLOWING(4or in bits:000100) β The specified node follows the current node.DOCUMENT_POSITION_CONTAINS(8or in bits:001000) β The otherNode is an ancestor of / contains the current node.DOCUMENT_POSITION_CONTAINED_BY(16or in bits:010000) β The otherNode is a descendant of / contained by the node.DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC(32or in bits:100000) β The specified node and the current node have no common container node or the two nodes are different attributes of the same node.
And also the following constants β the whole node-type alphabet, iconic behavior honestly β¨
ELEMENT_NODE(1)ATTRIBUTE_NODE(2)TEXT_NODE(3)CDATA_SECTION_NODE(4)ENTITY_REFERENCE_NODE(5)ENTITY_NODE(6)PROCESSING_INSTRUCTION_NODE(7)COMMENT_NODE(8)DOCUMENT_NODE(9)DOCUMENT_TYPE_NODE(10)DOCUMENT_FRAGMENT_NODE(11)NOTATION_NODE(12)SPACE_NODE(13)
π AUTHOR π
Jacques Deguest <jack@deguest.jp> π«Ά
π¦ SEE ALSO π¦
See Mozilla documentation <https://developer.mozilla.org/en-US/docs/Web/API/Node> β
πΈ COPYRIGHT & LICENSE πΈ
Copyright(c) 2021 DEGUEST Pte. Ltd.
All rights reserved
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
And that's a wrap on the whole page, bestie π Go forth and manipulate that DOM responsibly. This lil doc is living its truth as perl v5.42.2, dated 2026-02-15 β so when it says 2026, it's not a typo, it's time travel, and honestly? Main character energy π
β¨π
π¦ where this page comes from
- alpine 3.24: perl-html-object-doc 0.6.0-r1 Β· GPL-1.0-or-later OR Artistic-1.0-Perl
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms