HTML::O...terData(3)

πŸ“¦ version 0.6.0, in alpine 3.24

🌸 NAME 🌸

HTML::Object::DOM::CharacterData β€” HTML Object Character Data Class. This is the HTML::O...terData(3) page, bestie, so buckle up πŸ’–


πŸ› οΈ SYNOPSIS πŸ› οΈ

use parent qw( HTML::Object::DOM::CharacterData );
# then implements additional properties and methods

🏷️ VERSION 🏷️

v0.2.0

🌈 DESCRIPTION 🌈

The CharacterData abstract interface is the whole vibe for a Node object that contains characters ✨ It is an abstract interface, which means there are literally not any objects of type CharacterData: it gets implemented by other interfaces like Text, Comment, and Space, and those ones are not abstract. No cap.

It inherits from Node.


🧬 INHERITANCE 🧬

+-----------------------+     +---------------------------+     +-------------------------+     +----------------------------------+
| HTML::Object::Element | --> | HTML::Object::EventTarget | --> | HTML::Object::DOM::Node | --> | HTML::Object::DOM::CharacterData |
+-----------------------+     +---------------------------+     +-------------------------+     +----------------------------------+

πŸ’… PROPERTIES πŸ’…

This interface also inherits properties from its parents Node and EventTarget. Iconic inheritance behavior, honestly πŸ‘‘

πŸ“¦ data πŸ“¦

It is a string representing the textual data contained in this object. Simple, but make it fashion πŸ’«

Example:

<!-- This is an html comment !-->
<output id="Result"></output>

my $comment = $doc->body->childNodes->[1];
my $output = $doc->getElementById('Result');
$output->value = $comment->data;
# output content would now be: This is an html comment !

Setting the content of a text node using data

<span>Result: </span>Not set.

my $span = $doc->getElementsByTagName('span')->[0];
my $textnode = $span->nextSibling;
$textnode->data = "This text has been set using textnode.data."

See for more information: https://developer.mozilla.org/en-US/docs/Web/API/CharacterData/data

πŸ“ length πŸ“

Read-only, bestie 🫢

Returns a number representing the size of the string contained in the object.

Example:

Length of the string in the <code>Text</code> node: <output></output>

use HTML::Object::DOM::Text;
my $output = $doc->getElementsByTagName('output')->[0];
my $textnode = HTML::Object::DOM::Text->new("This text has been set using textnode.data.");

$output->value = $textnode->length;
# Length of the string in the Text node: 43

See for more information: https://developer.mozilla.org/en-US/docs/Web/API/CharacterData/length

πŸ¦‹ nextElementSibling πŸ¦‹

Read-only πŸ’…

Returns the first Element that follows this node, and is a sibling, or undef if this node was the last one in its parent's children list. It's giving "who's next?" energy.

Example:

TEXT
<div id="div-01">Here is div-01</div>
TEXT2
<div id="div-02">Here is div-02</div>
<pre>Here is the result area</pre>

# Initially, set node to the Text node with `TEXT`
my $node = $doc->getElementById('div-01')->previousSibling;
my $result = "Next element siblings of TEXT:\n";
while( $node )
{
    $result .= $node->nodeName + "\n";
    # The first node is a CharacterData, the others Element objects
    $node = $node->nextElementSibling;
}
$doc->getElementsByTagName('pre')->[0]->textContent = $result;

would produce:

TEXT
Here is div-01
TEXT2
Here is div-02

Next element siblings of TEXT:
#text
DIV
DIV
PRE
SCRIPT

See for more information: https://developer.mozilla.org/en-US/docs/Web/API/CharacterData/nextElementSibling

🌸 previousElementSibling 🌸

Read-only πŸŽ€

Returns the first Element that precedes this node, and is a sibling, or undef if this node was the first one in its parent's children list. Lowkey the "look back" bestie of the sibling world.

Example:

<div id="div-01">Here is div-01</div>
TEXT
<div id="div-02">Here is div-02</div>
SOME TEXT
<div id="div-03">Here is div-03</div>
<pre>Result</pre>

# Initially set node to the Text node with `SOME TEXT`
my $node = $doc->getElementById('div-02')->nextSibling;
my $result = "Previous element siblings of SOME TEXT:\n";
while( $node )
{
    $result .= $node->nodeName + "\n";
    $node = $node->previousElementSibling;
}
$doc->getElementsByTagName('pre')->[0]->textContent = $result;

would produce:

Here is div-01
TEXT
Here is div-02
SOME TEXT
Here is div-03

Previous element siblings of SOME TEXT:
#text
DIV
DIV

See for more information: https://developer.mozilla.org/en-US/docs/Web/API/CharacterData/previousElementSibling

πŸ’… METHODS πŸ’…

okay so this interface also inherits methods from its parents, Node and EventTarget β€” the whole family tree is showing up, and honestly? iconic 🫢

πŸ’– after πŸ’–

Inserts a set of Node objects or strings into the children list of the CharacterData's parent, right after the CharacterData object. Just after it. No cap.

Strings get inserted as Text nodes β€” the string is literally handed over as an argument to the HTML::Object::DOM::Text constructor, bestie ✨

It returns an HTML::Object::HierarchyRequestError error when the new nodes cannot be inserted at the specified point in the hierarchy, that is if one of these conditions is met:

  • If the insertion of one of the added node would lead to a cycle, that is if one of them is an ancestor of this CharacterData node.
  • If one of the added node is not a HTML::Object::DOM::DocumentFragment, an HTML::Object::DOM::Element, or a HTML::Object::DOM::CharacterData.
  • If this CharacterData node is actually a Text node, and its parent is a Document.
  • If the parent of this CharacterData node is a Document and one of the nodes to insert is a DocumentFragment with more than one Element child, or that has a Text child.

Example:

my $h1TextNode = $doc->getElementsByTagName('h1')->[0]->firstChild;
$h1TextNode->after(" #h1");

$h1TextNode->parentElement->childNodes;
# NodeList [#text "CharacterData.after()", #text " #h1"]

say $h1TextNode->data;
# "CharacterData.after()"

See for more information <https://developer.mozilla.org/en-US/docs/Web/API/CharacterData/after>

🌸 appendData 🌸

Provided with some string, and this appends the given string to the data string; when this method returns, data contains the concatenated string. It's giving "add it to the end and slay" πŸ’…

Example:

<span>Result: </span>A text

my $span = $doc->getElementsByTagName("span")->[0];
my $textnode = $span->nextSibling;
$textnode->appendData(" - appended text.");
# span now contains:
# Result: A text - appended text.

See for more information <https://developer.mozilla.org/en-US/docs/Web/API/CharacterData/appendData>

πŸ¦‹ before πŸ¦‹

Inserts a set of Node objects or strings in the children list of the CharacterData's parent, just before the CharacterData object. Same energy as after, just... before. Main character positioning honestly ✨

It returns the same error as after

Example:

my $h1TextNode = $doc->getElementsByTagName('h1')->[0]->firstChild;
$h1TextNode->before("h1# ");
$h1TextNode->parentElement->childNodes;
# NodeList [#text "h1# ", #text "CharacterData.before()"]

say $h1TextNode.data;
# "CharacterData.before()"

See for more information <https://developer.mozilla.org/en-US/docs/Web/API/CharacterData/before>

πŸŽ€ deleteData πŸŽ€

Provided with an offset value as an integer and an amount as a length, and this removes the specified amount of characters, starting at the specified offset, from the data string; when this method returns, data contains the shortened string. bye-bye characters, you had a good run 😭

Example:

<span>Result: </span>A long string.

my $span = $doc->getElementsByTagName("span")->[0];
my $textnode = $span->nextSibling;
$textnode->deleteData(1, 5);
# span now contains:
# Result: A string.

See for more information <https://developer.mozilla.org/en-US/docs/Web/API/CharacterData/deleteData>

🧚 insertData 🧚

Provided with an offset value as an integer and a string, and this inserts the specified characters, at the specified offset, in the data string; when this method returns, data contains the modified string. Sneaking text into the middle like it's a group chat message at 2am πŸ’€

<span>Result: </span>A string.

my $span = $doc->getElementsByTagName("span")->[0];
my $textnode = $span->nextSibling;
$textnode->insertData(2, "long ");
# span now contains:
# Result: A long string.

See for more information <https://developer.mozilla.org/en-US/docs/Web/API/CharacterData/insertData>

πŸ’œ remove πŸ’œ

Removes the object from its parent children list. Just straight-up yeeting it out of the chat, no explanation needed πŸ™ˆ

<span>Result: </span>A long string.

my $span = $doc->getElementsByTagName("span")->[0];
my $textnode = $span->nextSibling;
# Removes the text
$textnode->remove();
# span now contains:
# Result:

See for more information <https://developer.mozilla.org/en-US/docs/Web/API/CharacterData/remove>

πŸ”₯ replaceData πŸ”₯

$e->replaceData($offset, $count, $data);

Provided with an offset value as an integer, an amount as a length, and a string, and this replaces the specified amount of characters, starting at the specified offset, with the specified string; when this method returns, data contains the modified string. Out with the old text, in with the new β€” makeover era πŸ’…

<span>Result: </span>A long string.

my $span = $doc->getElementsByTagName("span")->[0];
my $textnode = $span->nextSibling;

$textnode->replaceData(2, 4, "replaced");
# span now contains:
# Result: A replaced string.

See for more information <https://developer.mozilla.org/en-US/docs/Web/API/CharacterData/replaceData>

⭐ replaceWith ⭐

$e->replaceWith( $node1, 'some text', $node2 );

Provided with a list of node objects or strings, and this replaces the characters in the children list of its parent with the supplied set of node objects or strings. It's giving "swap the whole vibe" energy ✨

This returns an HTML::Object::HierarchyRequestError when the node cannot be inserted at the specified point in the hierarchy.

Example:

<p id="myText">Some text</p>

my $text = $doc->getElementById('myText')->firstChild;
my $em = $doc->createElement("em");
$em->textContent = "Italic text";
# Replace `Some text` by `Italic text`
$text->replaceWith( $em );

See for more information <https://developer.mozilla.org/en-US/docs/Web/API/CharacterData/replaceWith>

🌈 substringData 🌈

Provided with an offset value as an integer, an amount as a length, and this returns a string containing the part of data of the specified length and starting at the specified offset. Basically it's doing a little peek-a-boo slice of your text πŸ‘‘

This is effectively similar to perl's substr in perlfunc

Returns a new scalar object

It returns a HTML::Object::IndexSizeError error if offset + amount is larger than the length of the contained data. So like, don't ask for more characters than exist β€” the math has to math πŸ’€

See for more information <https://developer.mozilla.org/en-US/docs/Web/API/CharacterData/substringData>

🌸 AUTHOR 🌸

Jacques Deguest β€” jack@deguest.jp

πŸ¦‹ SEE ALSO πŸ¦‹

Mozilla documentation <https://developer.mozilla.org/en-US/docs/Web/API/CharacterData>

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 the whole page, bestie 🫢 brought to you by perl v5.42.2, dated 2026-02-15, under the manpage name HTML::O...terData(3) β€” go forth and manipulate those character nodes responsibly πŸ’–βœ¨

πŸ“¦ where this page comes from