| 1 | ##---------------------------------------------------------------------------- | ||
| 2 | ## HTML Object - ~/lib/HTML/Object/DOM/CharacterData.pm | ||
| 3 | ## Version v0.2.0 | ||
| 4 | ## Copyright(c) 2021 DEGUEST Pte. Ltd. | ||
| 5 | ## Author: Jacques Deguest <jack@deguest.jp> | ||
| 6 | ## Created 2021/12/20 | ||
| 7 | ## Modified 2022/09/18 | ||
| 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::CharacterData; | ||
| 15 | BEGIN | ||
| 16 | { | ||
| 17 | use strict; | ||
| 18 | use warnings; | ||
| 19 | warnings::register_categories( 'HTML::Object' ); | ||
| 20 | use parent qw( HTML::Object::DOM::Node ); | ||
| 21 | use vars qw( $VERSION ); | ||
| 22 | our $VERSION = 'v0.2.0'; | ||
| 23 | }; | ||
| 24 | |||
| 25 | use strict; | ||
| 26 | use warnings; | ||
| 27 | |||
| 28 | # There is no constructor, because this is just an abstract interface to be inherited from by HTML::Object::DOM::Text, HTML::Object::DOM::Comment or HTML::Object::DOM::Space | ||
| 29 | |||
| 30 | sub after | ||
| 31 | { | ||
| 32 | my $self = shift( @_ ); | ||
| 33 | my $parent = $self->parent || return; | ||
| 34 | my $pos = $parent->children->pos( $self ); | ||
| 35 | return( $self->error({ | ||
| 36 | message => "This CharacterData object cannot be found in its parent node.", | ||
| 37 | class => 'HTML::Object::HierarchyRequestError', | ||
| 38 | }) ) if( !defined( $pos ) ); | ||
| 39 | my $list = $self->_get_from_list_of_elements_or_html( @_ ); | ||
| 40 | $self->_sanity_check_for_before_after( $list ) || | ||
| 41 | return( $self->pass_error ); | ||
| 42 | |||
| 43 | $parent->children->splice( $pos + 1, 0, $list->list ); | ||
| 44 | $list->foreach(sub | ||
| 45 | { | ||
| 46 | $_->parent( $parent ); | ||
| 47 | }); | ||
| 48 | $parent->reset(1); | ||
| 49 | return( $self ); | ||
| 50 | } | ||
| 51 | |||
| 52 | sub appendData { return( shift->value->append( @_ ) ); } | ||
| 53 | |||
| 54 | sub before | ||
| 55 | { | ||
| 56 | my $self = shift( @_ ); | ||
| 57 | my $parent = $self->parent || return; | ||
| 58 | my $pos = $parent->children->pos( $self ); | ||
| 59 | return( $self->error({ | ||
| 60 | message => "This CharacterData object cannot be found in its parent node.", | ||
| 61 | class => 'HTML::Object::HierarchyRequestError', | ||
| 62 | }) ) if( !defined( $pos ) ); | ||
| 63 | my $list = $self->_get_from_list_of_elements_or_html( @_ ); | ||
| 64 | $self->_sanity_check_for_before_after( $list ) || | ||
| 65 | return( $self->pass_error ); | ||
| 66 | |||
| 67 | $list->foreach(sub | ||
| 68 | { | ||
| 69 | $parent->children->splice( $pos, 0, $_ ); | ||
| 70 | $_->parent( $parent ); | ||
| 71 | $pos++; | ||
| 72 | }); | ||
| 73 | $parent->reset(1); | ||
| 74 | return( $self ); | ||
| 75 | } | ||
| 76 | |||
| 77 | # Note: property | ||
| 78 | sub data : lvalue { return( shift->_set_get_lvalue( 'value', @_ ) ); } | ||
| 79 | |||
| 80 | sub deleteData | ||
| 81 | { | ||
| 82 | my $self = shift( @_ ); | ||
| 83 | my $offset = shift( @_ ); | ||
| 84 | my $len = shift( @_ ); | ||
| 85 | $offset = "$offset" if( ref( $offset ) && overload::Method( $offset, '""' ) ); | ||
| 86 | $len = "$len" if( ref( $len ) && overload::Method( $len, '""' ) ); | ||
| 87 | return( $self->error({ | ||
| 88 | message => "Offset value provided \"$offset\" is not an integer.", | ||
| 89 | class => 'HTML::Object::TypeError', | ||
| 90 | }) ) if( !$self->_is_integer( $offset ) ); | ||
| 91 | return( $self->error({ | ||
| 92 | message => "Length value provided \"$len\" is not an integer.", | ||
| 93 | class => 'HTML::Object::TypeError', | ||
| 94 | }) ) if( !$self->_is_integer( $len ) ); | ||
| 95 | $offset = int( $offset ); | ||
| 96 | $len = int( $len ); | ||
| 97 | return( $self->error({ | ||
| 98 | message => "Offset value is greater than the size of the CharacterData object.", | ||
| 99 | class => 'HTML::Object::IndexSizeError', | ||
| 100 | }) ) if( $offset > $self->value->length ); | ||
| 101 | $self->value->substr( $offset, $len, '' ); | ||
| 102 | $self->reset(1); | ||
| 103 | return( $self ); | ||
| 104 | } | ||
| 105 | |||
| 106 | sub insertData | ||
| 107 | { | ||
| 108 | my $self = shift( @_ ); | ||
| 109 | my $offset = shift( @_ ); | ||
| 110 | my $str = shift( @_ ); | ||
| 111 | # Nothing to do, stop here | ||
| 112 | return( $self ) if( !defined( $str ) || !CORE::length( "$str" ) ); | ||
| 113 | $offset = "$offset" if( ref( $offset ) && overload::Method( $offset, '""' ) ); | ||
| 114 | $str = "$str"; | ||
| 115 | return( $self->error({ | ||
| 116 | message => "Offset value provided \"$offset\" is not an integer.", | ||
| 117 | class => 'HTML::Object::TypeError', | ||
| 118 | }) ) if( !$self->_is_integer( $offset ) ); | ||
| 119 | $offset = int( $offset ); | ||
| 120 | return( $self->error({ | ||
| 121 | message => "Offset value is greater than the size of the CharacterData object.", | ||
| 122 | class => 'HTML::Object::IndexSizeError', | ||
| 123 | }) ) if( $offset > $self->value->length ); | ||
| 124 | $self->value->substr( $offset, 0, $str ); | ||
| 125 | $self->reset(1); | ||
| 126 | return( $self ); | ||
| 127 | } | ||
| 128 | |||
| 129 | # Note: property | ||
| 130 | sub length { return( shift->value->length ); } | ||
| 131 | |||
| 132 | # Note: property | ||
| 133 | sub nextElementSibling | ||
| 134 | { | ||
| 135 | my $self = shift( @_ ); | ||
| 136 | my $parent = $self->parent || return; | ||
| 137 | my $pos = $parent->children->pos( $self ); | ||
| 138 | return( $self->error({ | ||
| 139 | message => "This CharacterData object cannot be found in its parent node.", | ||
| 140 | class => 'HTML::Object::HierarchyRequestError', | ||
| 141 | }) ) if( !defined( $pos ) ); | ||
| 142 | return( $parent->children->index( $pos + 1 ) ); | ||
| 143 | } | ||
| 144 | |||
| 145 | # Note: property | ||
| 146 | sub previousElementSibling | ||
| 147 | { | ||
| 148 | my $self = shift( @_ ); | ||
| 149 | my $parent = $self->parent || return; | ||
| 150 | my $pos = $parent->children->pos( $self ); | ||
| 151 | return( $self->error({ | ||
| 152 | message => "This CharacterData object cannot be found in its parent node.", | ||
| 153 | class => 'HTML::Object::HierarchyRequestError', | ||
| 154 | }) ) if( !defined( $pos ) ); | ||
| 155 | return if( $pos <= 0 ); | ||
| 156 | return( $parent->children->index( $pos - 1 ) ); | ||
| 157 | } | ||
| 158 | |||
| 159 | sub remove | ||
| 160 | { | ||
| 161 | my $self = shift( @_ ); | ||
| 162 | my $parent = $self->parent || return; | ||
| 163 | my $pos = $parent->children->pos( $self ); | ||
| 164 | return( $self->error({ | ||
| 165 | message => "This CharacterData object cannot be found in its parent node.", | ||
| 166 | class => 'HTML::Object::HierarchyRequestError', | ||
| 167 | }) ) if( !defined( $pos ) ); | ||
| 168 | $parent->children->splice( $pos, 1 ); | ||
| 169 | $parent->reset(1); | ||
| 170 | return( $self ); | ||
| 171 | } | ||
| 172 | |||
| 173 | sub replaceData | ||
| 174 | { | ||
| 175 | my $self = shift( @_ ); | ||
| 176 | my $offset = shift( @_ ); | ||
| 177 | my $len = shift( @_ ); | ||
| 178 | my $str = shift( @_ ); | ||
| 179 | # It is ok to be provided an undefined value, but we convert it to an empty string | ||
| 180 | # to prevent perl from triggering warnings. | ||
| 181 | $str //= ''; | ||
| 182 | $offset = "$offset" if( ref( $offset ) && overload::Method( $offset, '""' ) ); | ||
| 183 | $len = "$len" if( ref( $len ) && overload::Method( $len, '""' ) ); | ||
| 184 | return( $self->error({ | ||
| 185 | message => "Offset value provided \"$offset\" is not an integer.", | ||
| 186 | class => 'HTML::Object::TypeError', | ||
| 187 | }) ) if( !$self->_is_integer( $offset ) ); | ||
| 188 | return( $self->error({ | ||
| 189 | message => "Length value provided \"$len\" is not an integer.", | ||
| 190 | class => 'HTML::Object::TypeError', | ||
| 191 | }) ) if( !$self->_is_integer( $len ) ); | ||
| 192 | $offset = int( $offset ); | ||
| 193 | $len = int( $len ); | ||
| 194 | $str = "$str"; | ||
| 195 | return( $self->error({ | ||
| 196 | message => "Offset value is greater than the size of the CharacterData object.", | ||
| 197 | class => 'HTML::Object::IndexSizeError', | ||
| 198 | }) ) if( $offset > $self->value->length ); | ||
| 199 | $self->value->substr( $offset, $len, $str ); | ||
| 200 | $self->reset(1); | ||
| 201 | return( $self ); | ||
| 202 | } | ||
| 203 | |||
| 204 | sub replaceWith | ||
| 205 | { | ||
| 206 | my $self = shift( @_ ); | ||
| 207 | my $parent = $self->parent || return( $self->error({ | ||
| 208 | message => "This CharacterData has no parent.", | ||
| 209 | class => 'HTML::Object::HierarchyRequestError', | ||
| 210 | }) ); | ||
| 211 | my $pos = $parent->children->pos( $self ); | ||
| 212 | my $list = _get_from_list_of_elements_or_html( @_ ); | ||
| 213 | return( $self->error({ | ||
| 214 | message => "This CharacterData object cannot be found in its parent node.", | ||
| 215 | class => 'HTML::Object::HierarchyRequestError', | ||
| 216 | }) ) if( !defined( $pos ) ); | ||
| 217 | # There is nothing to do | ||
| 218 | return( $self ) if( $list->is_empty ); | ||
| 219 | $parent->children->splice( $pos, 1, $list->list ); | ||
| 220 | $list->foreach(sub | ||
| 221 | { | ||
| 222 | $_->parent( $parent ); | ||
| 223 | }); | ||
| 224 | $parent->reset(1); | ||
| 225 | return( $self ); | ||
| 226 | } | ||
| 227 | |||
| 228 | sub substringData | ||
| 229 | { | ||
| 230 | my $self = shift( @_ ); | ||
| 231 | my $offset = shift( @_ ); | ||
| 232 | my $len = shift( @_ ); | ||
| 233 | $offset = "$offset" if( ref( $offset ) && overload::Method( $offset, '""' ) ); | ||
| 234 | $len = "$len" if( ref( $len ) && overload::Method( $len, '""' ) ); | ||
| 235 | return( $self->error({ | ||
| 236 | message => "Offset value provided \"$offset\" is not an integer.", | ||
| 237 | class => 'HTML::Object::TypeError', | ||
| 238 | }) ) if( !$self->_is_integer( $offset ) ); | ||
| 239 | return( $self->error({ | ||
| 240 | message => "Length value provided \"$len\" is not an integer.", | ||
| 241 | class => 'HTML::Object::TypeError', | ||
| 242 | }) ) if( !$self->_is_integer( $len ) ); | ||
| 243 | $offset = int( $offset ); | ||
| 244 | $len = int( $len ); | ||
| 245 | return( $self->error({ | ||
| 246 | message => "Offset value is greater than the size of the CharacterData object.", | ||
| 247 | class => 'HTML::Object::IndexSizeError', | ||
| 248 | }) ) if( $offset > $self->value->length ); | ||
| 249 | return( $self->value->substr( $offset, $len ) ); | ||
| 250 | } | ||
| 251 | |||
| 252 | sub _sanity_check_for_before_after | ||
| 253 | { | ||
| 254 | my $self = shift( @_ ); | ||
| 255 | my $list = shift( @_ ) || return( $self->error( "No list of nodes was provided." ) ); | ||
| 256 | return( $self->error( "I was expecting an array object, but instead got '$list'." ) ) if( !$self->_is_a( $list => 'Module::Generic::Array' ) ); | ||
| 257 | my $parent = $self->parent; | ||
| 258 | my $lineage = $self->lineage; | ||
| 259 | if( !$list->intersect( $lineage )->is_empty ) | ||
| 260 | { | ||
| 261 | return( $self->error({ | ||
| 262 | message => "One of the nodes provided is an ancestor of this CharacterData", | ||
| 263 | class => 'HTML::Object::HierarchyRequestError', | ||
| 264 | }) ); | ||
| 265 | } | ||
| 266 | foreach( @$list ) | ||
| 267 | { | ||
| 268 | if( !$self->_is_a( $_ => 'HTML::Object::DOM::DocumentFragment' ) && | ||
| 269 | !$self->_is_a( $_ => 'HTML::Object::DOM::Element' ) && | ||
| 270 | !$self->_is_a( $_ => 'HTML::Object::DOM::CharacterData' ) ) | ||
| 271 | { | ||
| 272 | return( $self->error({ | ||
| 273 | message => "A node provided (" . overload::StrVal( $_ ) . ") is neither a DocumentFragment, an Element nor a CharacterData object.", | ||
| 274 | class => 'HTML::Object::HierarchyRequestError', | ||
| 275 | }) ); | ||
| 276 | } | ||
| 277 | elsif( $self->_is_a( $_ => 'HTML::Object::DOM::DocumentFragment' ) ) | ||
| 278 | { | ||
| 279 | my $kids = $_->children; | ||
| 280 | my $count = 0; | ||
| 281 | foreach my $node ( @$kids ) | ||
| 282 | { | ||
| 283 | ++$count if( $node->isa( 'HTML::Object::DOM::Element' ) ); | ||
| 284 | if( $count > 1 || $node->isa( 'HTML::Object::DOM::Text' ) ) | ||
| 285 | { | ||
| 286 | return( $self->error({ | ||
| 287 | message => "More than one Element node was provided or a Text node was provided.", | ||
| 288 | class => 'HTML::Object::HierarchyRequestError', | ||
| 289 | }) ); | ||
| 290 | } | ||
| 291 | } | ||
| 292 | } | ||
| 293 | } | ||
| 294 | if( $self->isa( 'HTML::Object::DOM::Text' ) && $self->_is_a( $parent => 'HTML::Object::DOM::Document' ) ) | ||
| 295 | { | ||
| 296 | return( $self->error({ | ||
| 297 | message => "This CharacterData object is a Text object, and our parent is a Document.", | ||
| 298 | class => 'HTML::Object::HierarchyRequestError', | ||
| 299 | }) ); | ||
| 300 | } | ||
| 301 | return(1); | ||
| 302 | } | ||
| 303 | |||
| 304 | 1; | ||
| 305 | # NOTE: POD | ||
| 306 | __END__ | ||
| 307 | |||
| 308 | =encoding utf-8 | ||
| 309 | |||
| 310 | =head1 NAME | ||
| 311 | |||
| 312 | HTML::Object::DOM::CharacterData - HTML Object Character Data Class | ||
| 313 | |||
| 314 | =head1 SYNOPSIS | ||
| 315 | |||
| 316 | use parent qw( HTML::Object::DOM::CharacterData ); | ||
| 317 | # then implements additional properties and methods | ||
| 318 | |||
| 319 | =head1 VERSION | ||
| 320 | |||
| 321 | v0.2.0 | ||
| 322 | |||
| 323 | =head1 DESCRIPTION | ||
| 324 | |||
| 325 | The C<CharacterData> abstract interface represents a L<Node object|HTML::Object::DOM::Node> that contains characters. This is an abstract interface, meaning there are not any objects of type CharacterData: it is implemented by other interfaces like L<Text|HTML::Object::DOM::Text>, L<Comment|HTML::Object::DOM::Comment>, L<Space|HTML::Object::DOM::Space> which are not abstract. | ||
| 326 | |||
| 327 | It inherits from L<Node|HTML::Object::DOM::Node> | ||
| 328 | |||
| 329 | =head1 INHERITANCE | ||
| 330 | |||
| 331 | +-----------------------+ +---------------------------+ +-------------------------+ +----------------------------------+ | ||
| 332 | | HTML::Object::Element | --> | HTML::Object::EventTarget | --> | HTML::Object::DOM::Node | --> | HTML::Object::DOM::CharacterData | | ||
| 333 | +-----------------------+ +---------------------------+ +-------------------------+ +----------------------------------+ | ||
| 334 | |||
| 335 | =head1 PROPERTIES | ||
| 336 | |||
| 337 | This interface also inherits properties from its parents L<Node|HTML::Object::DOM::Node> and L<EventTarget|HTML::Object::EventTarget>. | ||
| 338 | |||
| 339 | =head2 data | ||
| 340 | |||
| 341 | Is a string representing the textual data contained in this object. | ||
| 342 | |||
| 343 | Example: | ||
| 344 | |||
| 345 | <!-- This is an html comment !--> | ||
| 346 | <output id="Result"></output> | ||
| 347 | |||
| 348 | my $comment = $doc->body->childNodes->[1]; | ||
| 349 | my $output = $doc->getElementById('Result'); | ||
| 350 | $output->value = $comment->data; | ||
| 351 | # output content would now be: This is an html comment ! | ||
| 352 | |||
| 353 | Setting the content of a text node using data | ||
| 354 | |||
| 355 | <span>Result: </span>Not set. | ||
| 356 | |||
| 357 | my $span = $doc->getElementsByTagName('span')->[0]; | ||
| 358 | my $textnode = $span->nextSibling; | ||
| 359 | $textnode->data = "This text has been set using textnode.data." | ||
| 360 | |||
| 361 | See L<for more information|https://developer.mozilla.org/en-US/docs/Web/API/CharacterData/data> | ||
| 362 | |||
| 363 | =head2 length | ||
| 364 | |||
| 365 | Read-only. | ||
| 366 | |||
| 367 | Returns a L<number|Module::Generic::Number> representing the size of the string contained in the object. | ||
| 368 | |||
| 369 | Example: | ||
| 370 | |||
| 371 | Length of the string in the <code>Text</code> node: <output></output> | ||
| 372 | |||
| 373 | use HTML::Object::DOM::Text; | ||
| 374 | my $output = $doc->getElementsByTagName('output')->[0]; | ||
| 375 | my $textnode = HTML::Object::DOM::Text->new("This text has been set using textnode.data."); | ||
| 376 | |||
| 377 | $output->value = $textnode->length; | ||
| 378 | # Length of the string in the Text node: 43 | ||
| 379 | |||
| 380 | See L<for more information|https://developer.mozilla.org/en-US/docs/Web/API/CharacterData/length> | ||
| 381 | |||
| 382 | =head2 nextElementSibling | ||
| 383 | |||
| 384 | Read-only. | ||
| 385 | |||
| 386 | Returns the first Element that follows this node, and is a sibling, or C<undef> if this node was the last one in its parent's children list. | ||
| 387 | |||
| 388 | Example: | ||
| 389 | |||
| 390 | TEXT | ||
| 391 | <div id="div-01">Here is div-01</div> | ||
| 392 | TEXT2 | ||
| 393 | <div id="div-02">Here is div-02</div> | ||
| 394 | <pre>Here is the result area</pre> | ||
| 395 | |||
| 396 | # Initially, set node to the Text node with `TEXT` | ||
| 397 | my $node = $doc->getElementById('div-01')->previousSibling; | ||
| 398 | my $result = "Next element siblings of TEXT:\n"; | ||
| 399 | while( $node ) | ||
| 400 | { | ||
| 401 | $result .= $node->nodeName + "\n"; | ||
| 402 | # The first node is a CharacterData, the others Element objects | ||
| 403 | $node = $node->nextElementSibling; | ||
| 404 | } | ||
| 405 | $doc->getElementsByTagName('pre')->[0]->textContent = $result; | ||
| 406 | |||
| 407 | would produce: | ||
| 408 | |||
| 409 | TEXT | ||
| 410 | Here is div-01 | ||
| 411 | TEXT2 | ||
| 412 | Here is div-02 | ||
| 413 | |||
| 414 | Next element siblings of TEXT: | ||
| 415 | #text | ||
| 416 | DIV | ||
| 417 | DIV | ||
| 418 | PRE | ||
| 419 | SCRIPT | ||
| 420 | |||
| 421 | See L<for more information|https://developer.mozilla.org/en-US/docs/Web/API/CharacterData/nextElementSibling> | ||
| 422 | |||
| 423 | =head2 previousElementSibling | ||
| 424 | |||
| 425 | Read-only. | ||
| 426 | |||
| 427 | Returns the first Element that precedes this node, and is a sibling, or C<undef> if this node was the first one in its parent's children list. | ||
| 428 | |||
| 429 | Example: | ||
| 430 | |||
| 431 | <div id="div-01">Here is div-01</div> | ||
| 432 | TEXT | ||
| 433 | <div id="div-02">Here is div-02</div> | ||
| 434 | SOME TEXT | ||
| 435 | <div id="div-03">Here is div-03</div> | ||
| 436 | <pre>Result</pre> | ||
| 437 | |||
| 438 | # Initially set node to the Text node with `SOME TEXT` | ||
| 439 | my $node = $doc->getElementById('div-02')->nextSibling; | ||
| 440 | my $result = "Previous element siblings of SOME TEXT:\n"; | ||
| 441 | while( $node ) | ||
| 442 | { | ||
| 443 | $result .= $node->nodeName + "\n"; | ||
| 444 | $node = $node->previousElementSibling; | ||
| 445 | } | ||
| 446 | $doc->getElementsByTagName('pre')->[0]->textContent = $result; | ||
| 447 | |||
| 448 | would produce: | ||
| 449 | |||
| 450 | Here is div-01 | ||
| 451 | TEXT | ||
| 452 | Here is div-02 | ||
| 453 | SOME TEXT | ||
| 454 | Here is div-03 | ||
| 455 | |||
| 456 | Previous element siblings of SOME TEXT: | ||
| 457 | #text | ||
| 458 | DIV | ||
| 459 | DIV | ||
| 460 | |||
| 461 | See L<for more information|https://developer.mozilla.org/en-US/docs/Web/API/CharacterData/previousElementSibling> | ||
| 462 | |||
| 463 | =head1 METHODS | ||
| 464 | |||
| 465 | This interface also inherits methods from its parents, L<Node|HTML::Object::DOM::Node> and L<EventTarget|HTML::Object::EventTarget>. | ||
| 466 | |||
| 467 | =head2 after | ||
| 468 | |||
| 469 | Inserts a set of L<Node objects|HTML::Object::DON::Node> or strings in the children list of the C<CharacterData>'s parent, just after the C<CharacterData> object. | ||
| 470 | |||
| 471 | Strings are inserted as L<Text nodes|HTML::Object::DOM::Text>; the string is being passed as argument to the L<HTML::Object::DOM::Text> constructor. | ||
| 472 | |||
| 473 | It returns an C<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: | ||
| 474 | |||
| 475 | =over 4 | ||
| 476 | |||
| 477 | =item * 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 C<CharacterData> node. | ||
| 478 | |||
| 479 | =item * If one of the added node is not a L<HTML::Object::DOM::DocumentFragment>, an L<HTML::Object::DOM::Element>, or a L<HTML::Object::DOM::CharacterData>. | ||
| 480 | |||
| 481 | =item * If this L<CharacterData node|HTML::Object::DOM::CharacterData> is actually a L<Text|HTML::Object::DOM::Text> node, and its parent is a L<Document|HTML::Object::DOM::Document>. | ||
| 482 | |||
| 483 | =item * If the parent of this L<CharacterData node|HTML::Object::DOM::CharacterData> is a L<Document|HTML::Object::DOM::Document> and one of the nodes to insert is a L<DocumentFragment|HTML::Object::DOM::DocumentFragment> with more than one L<Element|HTML::Object::DOM::Element> child, or that has a L<Text|HTML::Object::DOM::Text> child. | ||
| 484 | |||
| 485 | =back | ||
| 486 | |||
| 487 | Example: | ||
| 488 | |||
| 489 | my $h1TextNode = $doc->getElementsByTagName('h1')->[0]->firstChild; | ||
| 490 | $h1TextNode->after(" #h1"); | ||
| 491 | |||
| 492 | $h1TextNode->parentElement->childNodes; | ||
| 493 | # NodeList [#text "CharacterData.after()", #text " #h1"] | ||
| 494 | |||
| 495 | say $h1TextNode->data; | ||
| 496 | # "CharacterData.after()" | ||
| 497 | |||
| 498 | See L<for more information|https://developer.mozilla.org/en-US/docs/Web/API/CharacterData/after> | ||
| 499 | |||
| 500 | =head2 appendData | ||
| 501 | |||
| 502 | Provided with some string and this appends the given string to the L</data> string; when this method returns, data contains the concatenated string. | ||
| 503 | |||
| 504 | Example: | ||
| 505 | |||
| 506 | <span>Result: </span>A text | ||
| 507 | |||
| 508 | my $span = $doc->getElementsByTagName("span")->[0]; | ||
| 509 | my $textnode = $span->nextSibling; | ||
| 510 | $textnode->appendData(" - appended text."); | ||
| 511 | # span now contains: | ||
| 512 | # Result: A text - appended text. | ||
| 513 | |||
| 514 | See L<for more information|https://developer.mozilla.org/en-US/docs/Web/API/CharacterData/appendData> | ||
| 515 | |||
| 516 | =head2 before | ||
| 517 | |||
| 518 | Inserts a set of L<Node objects|HTML::Object::DON::Node> or strings in the children list of the C<CharacterData>'s parent, just before the C<CharacterData> object. | ||
| 519 | |||
| 520 | It returns the same error as L</after> | ||
| 521 | |||
| 522 | Example: | ||
| 523 | |||
| 524 | my $h1TextNode = $doc->getElementsByTagName('h1')->[0]->firstChild; | ||
| 525 | $h1TextNode->before("h1# "); | ||
| 526 | $h1TextNode->parentElement->childNodes; | ||
| 527 | # NodeList [#text "h1# ", #text "CharacterData.before()"] | ||
| 528 | |||
| 529 | say $h1TextNode.data; | ||
| 530 | # "CharacterData.before()" | ||
| 531 | |||
| 532 | See L<for more information|https://developer.mozilla.org/en-US/docs/Web/API/CharacterData/before> | ||
| 533 | |||
| 534 | =head2 deleteData | ||
| 535 | |||
| 536 | Provided with an C<offset> value as an integer and an C<amount> as a length, and this removes the specified C<amount> of characters, starting at the specified C<offset>, from the L</data> string; when this method returns, data contains the shortened string. | ||
| 537 | |||
| 538 | Example: | ||
| 539 | |||
| 540 | <span>Result: </span>A long string. | ||
| 541 | |||
| 542 | my $span = $doc->getElementsByTagName("span")->[0]; | ||
| 543 | my $textnode = $span->nextSibling; | ||
| 544 | $textnode->deleteData(1, 5); | ||
| 545 | # span now contains: | ||
| 546 | # Result: A string. | ||
| 547 | |||
| 548 | See L<for more information|https://developer.mozilla.org/en-US/docs/Web/API/CharacterData/deleteData> | ||
| 549 | |||
| 550 | =head2 insertData | ||
| 551 | |||
| 552 | Provided with an C<offset> value as an integer and a string, and this inserts the specified characters, at the specified C<offset>, in the L</data> string; when this method returns, data contains the modified string. | ||
| 553 | |||
| 554 | <span>Result: </span>A string. | ||
| 555 | |||
| 556 | my $span = $doc->getElementsByTagName("span")->[0]; | ||
| 557 | my $textnode = $span->nextSibling; | ||
| 558 | $textnode->insertData(2, "long "); | ||
| 559 | # span now contains: | ||
| 560 | # Result: A long string. | ||
| 561 | |||
| 562 | See L<for more information|https://developer.mozilla.org/en-US/docs/Web/API/CharacterData/insertData> | ||
| 563 | |||
| 564 | =head2 remove | ||
| 565 | |||
| 566 | Removes the object from its parent children list. | ||
| 567 | |||
| 568 | <span>Result: </span>A long string. | ||
| 569 | |||
| 570 | my $span = $doc->getElementsByTagName("span")->[0]; | ||
| 571 | my $textnode = $span->nextSibling; | ||
| 572 | # Removes the text | ||
| 573 | $textnode->remove(); | ||
| 574 | # span now contains: | ||
| 575 | # Result: | ||
| 576 | |||
| 577 | See L<for more information|https://developer.mozilla.org/en-US/docs/Web/API/CharacterData/remove> | ||
| 578 | |||
| 579 | =head2 replaceData | ||
| 580 | |||
| 581 | $e->replaceData($offset, $count, $data); | ||
| 582 | |||
| 583 | Provided with an C<offset> value as an integer, an C<amount> as a length, and a string, and this replaces the specified C<amount> of characters, starting at the specified C<offset>, with the specified C<string>; when this method returns, data contains the modified string. | ||
| 584 | |||
| 585 | <span>Result: </span>A long string. | ||
| 586 | |||
| 587 | my $span = $doc->getElementsByTagName("span")->[0]; | ||
| 588 | my $textnode = $span->nextSibling; | ||
| 589 | |||
| 590 | $textnode->replaceData(2, 4, "replaced"); | ||
| 591 | # span now contains: | ||
| 592 | # Result: A replaced string. | ||
| 593 | |||
| 594 | See L<for more information|https://developer.mozilla.org/en-US/docs/Web/API/CharacterData/replaceData> | ||
| 595 | |||
| 596 | =head2 replaceWith | ||
| 597 | |||
| 598 | $e->replaceWith( $node1, 'some text', $node2 ); | ||
| 599 | |||
| 600 | Provided with a list of L<node objects|HTML::Object::DOM::Node> or strings and this replaces the characters in the children list of its parent with the supplied set of L<node objects|HTML::Object::DOM::Node> or strings. | ||
| 601 | |||
| 602 | This returns an C<HTML::Object::HierarchyRequestError> when the node cannot be inserted at the specified point in the hierarchy. | ||
| 603 | |||
| 604 | Example: | ||
| 605 | |||
| 606 | <p id="myText">Some text</p> | ||
| 607 | |||
| 608 | my $text = $doc->getElementById('myText')->firstChild; | ||
| 609 | my $em = $doc->createElement("em"); | ||
| 610 | $em->textContent = "Italic text"; | ||
| 611 | # Replace `Some text` by `Italic text` | ||
| 612 | $text->replaceWith( $em ); | ||
| 613 | |||
| 614 | See L<for more information|https://developer.mozilla.org/en-US/docs/Web/API/CharacterData/replaceWith> | ||
| 615 | |||
| 616 | =head2 substringData | ||
| 617 | |||
| 618 | Provided with an C<offset> value as an integer, an C<amount> as a length, and this returns a string containing the part of L</data> of the specified C<length> and starting at the specified C<offset>. | ||
| 619 | |||
| 620 | This is effectively similar to perl's L<perlfunc/substr> | ||
| 621 | |||
| 622 | Returns a new L<scalar object|Module::Generic::Scalar> | ||
| 623 | |||
| 624 | It returns a C<HTML::Object::IndexSizeError> error if C<offset> + C<amount> is larger than the length of the contained data. | ||
| 625 | |||
| 626 | See L<for more information|https://developer.mozilla.org/en-US/docs/Web/API/CharacterData/substringData> | ||
| 627 | |||
| 628 | =head1 AUTHOR | ||
| 629 | |||
| 630 | Jacques Deguest E<lt>F<jack@deguest.jp>E<gt> | ||
| 631 | |||
| 632 | =head1 SEE ALSO | ||
| 633 | |||
| 634 | L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/CharacterData> | ||
| 635 | |||
| 636 | =head1 COPYRIGHT & LICENSE | ||
| 637 | |||
| 638 | Copyright(c) 2021 DEGUEST Pte. Ltd. | ||
| 639 | |||
| 640 | All rights reserved | ||
| 641 | |||
| 642 | This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. | ||
| 643 | |||
| 644 | =cut |