HTML::Object::DOM::CharacterData - HTML Object Character Data Class
use parent qw( HTML::Object::DOM::CharacterData ); # then implements additional properties and methods
v0.2.0
The CharacterData abstract interface represents a Node object that contains characters. This is an abstract interface, meaning there are not any objects of type CharacterData: it is implemented by other interfaces like Text, Comment, Space which are not abstract.
CharacterData
It inherits from Node
+-----------------------+ +---------------------------+ +-------------------------+ +----------------------------------+ | HTML::Object::Element | --> | HTML::Object::EventTarget | --> | HTML::Object::DOM::Node | --> | HTML::Object::DOM::CharacterData | +-----------------------+ +---------------------------+ +-------------------------+ +----------------------------------+
This interface also inherits properties from its parents Node and EventTarget.
Is a string representing the textual data contained in this object.
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
Read-only.
Returns a number representing the size of the string contained in the object.
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
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.
undef
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
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.
<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;
Here is div-01 TEXT Here is div-02 SOME TEXT Here is div-03 Previous element siblings of SOME TEXT: #text DIV DIV
This interface also inherits methods from its parents, Node and EventTarget.
Inserts a set of Node objects or strings in the children list of the CharacterData's parent, just after the CharacterData object.
Strings are inserted as Text nodes; the string is being passed as argument to the HTML::Object::DOM::Text constructor.
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 the following conditions is met:
HTML::Object::HierarchyRequestError
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.
my $h1TextNode = $doc->getElementsByTagName('h1')->[0]->firstChild; $h1TextNode->after(" #h1"); $h1TextNode->parentElement->childNodes; # NodeList [#text "CharacterData.after()", #text " #h1"] say $h1TextNode->data; # "CharacterData.after()"
Provided with some string and this appends the given string to the "data" string; when this method returns, data contains the concatenated string.
<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.
Inserts a set of Node objects or strings in the children list of the CharacterData's parent, just before the CharacterData object.
It returns the same error as "after"
my $h1TextNode = $doc->getElementsByTagName('h1')->[0]->firstChild; $h1TextNode->before("h1# "); $h1TextNode->parentElement->childNodes; # NodeList [#text "h1# ", #text "CharacterData.before()"] say $h1TextNode.data; # "CharacterData.before()"
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.
offset
amount
<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.
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.
<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.
Removes the object from its parent children list.
<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:
$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.
string
<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.
$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.
This returns an HTML::Object::HierarchyRequestError when the node cannot be inserted at the specified point in the hierarchy.
<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 );
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.
length
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.
HTML::Object::IndexSizeError
Jacques Deguest <jack@deguest.jp>
Mozilla documentation
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.
To install HTML::Object, copy and paste the appropriate command in to your terminal.
cpanm
cpanm HTML::Object
CPAN shell
perl -MCPAN -e shell install HTML::Object
For more information on module installation, please visit the detailed CPAN module installation guide.