The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

HTML::Object::DOM::CharacterData - HTML Object Character Data Class

SYNOPSIS

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

VERSION

    v0.2.0

DESCRIPTION

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.

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.

data

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

length

Read-only.

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

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.

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

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.

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

METHODS

This interface also inherits methods from its parents, Node and EventTarget.

after

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:

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

appendData

Provided with some string and this appends the given string to the "data" string; when this method returns, data contains the concatenated string.

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

before

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"

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

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.

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

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.

    <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

remove

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:

See for more information

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.

    <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

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.

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

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.

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.

See for more information

AUTHOR

Jacques Deguest <jack@deguest.jp>

SEE ALSO

Mozilla documentation

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.