| 1 | ##---------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 2 | ## HTML Object - ~/lib/HTML/Object/DOM/Text.pm | ||||||||||||||||||||||||||||
| 3 | ## Version v0.2.1_1 | ||||||||||||||||||||||||||||
| 4 | ## Copyright(c) 2024 DEGUEST Pte. Ltd. | ||||||||||||||||||||||||||||
| 5 | ## Author: Jacques Deguest <jack@deguest.jp> | ||||||||||||||||||||||||||||
| 6 | ## Created 2021/12/13 | ||||||||||||||||||||||||||||
| 7 | ## Modified 2026/03/29 | ||||||||||||||||||||||||||||
| 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::Text; | ||||||||||||||||||||||||||||
| 15 | BEGIN | ||||||||||||||||||||||||||||
| 16 | { | ||||||||||||||||||||||||||||
| 17 | 33 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | 33 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | 33 | ▶ | warnings::register_categories( 'HTML::Object' ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | 33 | ▶ | use parent qw( HTML::Object::Text HTML::Object::DOM::CharacterData ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | 33 | ▶ | use vars qw( $VERSION ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 22 | 33 | our $VERSION = 'v0.2.1_1'; | |||||||||||||||||||||||||||
| 23 | }; | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 33 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 26 | 33 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | sub init | ||||||||||||||||||||||||||||
| 29 | { | ||||||||||||||||||||||||||||
| 30 | 143 | ▶ | my $self = shift( @_ ); | ||||||||||||||||||||||||||
|
Subroutine
init: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 31 | 143 | $self->{_init_strict_use_sub} = 1; | |||||||||||||||||||||||||||
| 32 | 143 | ▶ | $self->HTML::Object::Text::init( @_ ) || return( $self->pass_error ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 33 | 143 | return( $self ); | |||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 0 | ▶ | sub assignedSlot { return( shift->_set_get_object_without_init( 'assignedslot', 'HTML::Object::DOM::Element::Slot', @_ ) ); } | ||||||||||||||||||||||||||
|
Subroutine
assignedSlot: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 0 | ▶ | sub getAttributes { return( wantarray() ? () : [] ); } | ||||||||||||||||||||||||||
|
Subroutine
getAttributes: not called CC=2 SCAR=17.9
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 134 | ▶ | sub getChildNodes { return( wantarray() ? () : [] ); } | ||||||||||||||||||||||||||
|
Subroutine
getChildNodes: called CC=2 SCAR=6.9
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 0 | ▶ | sub getElementById { return; } | ||||||||||||||||||||||||||
|
Subroutine
getElementById: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 0 | ▶ | sub getFirstChild { return; } | ||||||||||||||||||||||||||
|
Subroutine
getFirstChild: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 0 | ▶ | sub getLastChild { return; } | ||||||||||||||||||||||||||
|
Subroutine
getLastChild: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 0 | ▶ | sub getParentNode { return( shift->parent ); } | ||||||||||||||||||||||||||
|
Subroutine
getParentNode: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | # Inherited | ||||||||||||||||||||||||||||
| 51 | # sub getNextSibling; | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | # Inherited | ||||||||||||||||||||||||||||
| 54 | # sub getPreviousSibling; | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 0 | ▶ | sub getRootNode { return( shift->parent->getRootNode ); } | ||||||||||||||||||||||||||
|
Subroutine
getRootNode: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 0 | ▶ | sub getValue { return( shift->value ); } | ||||||||||||||||||||||||||
|
Subroutine
getValue: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | sub is_inside | ||||||||||||||||||||||||||||
| 61 | { | ||||||||||||||||||||||||||||
| 62 | 0 | ▶ | my( $text, $node ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
is_inside: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 63 | 0 | return( $text->parent->is_inside( $node ) ); | |||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | sub isEqualNode | ||||||||||||||||||||||||||||
| 67 | { | ||||||||||||||||||||||||||||
| 68 | 0 | ▶ | my $self = shift( @_ ); | ||||||||||||||||||||||||||
|
Subroutine
isEqualNode: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||
| 69 | 0 | ▶ | my $e = shift( @_ ) || return( $self->error( "No html element was provided to insert." ) ); | ||||||||||||||||||||||||||
|
Condition0%0/2
shift @_ || (return $self->error('No html element was provided to insert.'))
MC/DC0%0/1
shift @_ || (return $self->error('No html element was provided to insert.'))
shift @_
Truth table0%0/2
A
A= shift @_
| |||||||||||||||||||||||||||||
| 70 | 0 | ▶ | return( $self->error( "Element provided (", overload::StrVal( $e ), ") is not an HTML::Object::Element." ) ) if( !$self->_is_a( $e => 'HTML::Object::Element' ) ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 71 | 0 | ▶ | return(0) if( !$self->_is_a( $e => 'HTML::Object::Text' ) ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | 0 | return( $self->value eq $e->value ); | |||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 0 | ▶ | sub isAttributeNode { return(0); } | ||||||||||||||||||||||||||
|
Subroutine
isAttributeNode: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 0 | ▶ | sub isCommentNode { return(0); } | ||||||||||||||||||||||||||
|
Subroutine
isCommentNode: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 106 | ▶ | sub isElementNode { return(0); } | ||||||||||||||||||||||||||
|
Subroutine
isElementNode: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 0 | ▶ | sub isNamespaceNode { return(0); } | ||||||||||||||||||||||||||
|
Subroutine
isNamespaceNode: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 0 | ▶ | sub isPINode { return(0); } | ||||||||||||||||||||||||||
|
Subroutine
isPINode: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 0 | ▶ | sub isProcessingInstructionNode { return(0); } | ||||||||||||||||||||||||||
|
Subroutine
isProcessingInstructionNode: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 0 | ▶ | sub isTextNode { return(1); } | ||||||||||||||||||||||||||
|
Subroutine
isTextNode: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | # Note: Property | ||||||||||||||||||||||||||||
| 90 | 1 | ▶ | sub nodeValue : lvalue { return( shift->_set_get_lvalue( 'value', @_ ) ); } | ||||||||||||||||||||||||||
|
Subroutine
nodeValue: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | sub parent { return( shift->_set_get_object_without_init({ | ||||||||||||||||||||||||||||
| 93 | 373 | ▶ | field => 'parent', | ||||||||||||||||||||||||||
|
Subroutine
parent: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 94 | weaken => 1, | ||||||||||||||||||||||||||||
| 95 | }, 'HTML::Object::DOM::Node', @_ ) ); } | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | sub replaceWholeText | ||||||||||||||||||||||||||||
| 98 | { | ||||||||||||||||||||||||||||
| 99 | 1 | ▶ | my $self = shift( @_ ); | ||||||||||||||||||||||||||
|
Subroutine
replaceWholeText: called CC=6 SCAR=19.7
Pod
documented
| |||||||||||||||||||||||||||||
| 100 | 1 | my $content = shift( @_ ); | |||||||||||||||||||||||||||
| 101 | 1 | ▶ | return( $self->error({ | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
ref $content and not overload::Method($content, "\"\"")
MC/DC0%0/2
ref $content and not overload::Method($content, "\"\"")
ref $content
not overload::Method($content, "\"\"")
Truth table33%1/3
A and not(B)
A= ref $contentB= not overload::Method($content, "\"\"")
| |||||||||||||||||||||||||||||
| 102 | message => "Content provided is a reference that cannot be stringified.", | ||||||||||||||||||||||||||||
| 103 | class => 'HTML::Object::TypeError', | ||||||||||||||||||||||||||||
| 104 | }) ) if( ref( $content ) && !overload::Method( $content, '""' ) ); | ||||||||||||||||||||||||||||
| 105 | 1 | my $prev = $self->left; | |||||||||||||||||||||||||||
| 106 | 1 | my $next = $self->right; | |||||||||||||||||||||||||||
| 107 | 1 | my $parent = $self->parent; | |||||||||||||||||||||||||||
| 108 | 1 | ▶ | if( !$parent ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 109 | { | ||||||||||||||||||||||||||||
| 110 | 0 | $self->value( "$content" ); | |||||||||||||||||||||||||||
| 111 | 0 | return( $self ); | |||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | 1 | my $siblings = $parent->children; | |||||||||||||||||||||||||||
| 114 | 1 | my $pos = $siblings->pos( $self ); | |||||||||||||||||||||||||||
| 115 | 1 | ▶ | return( $self->error({ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 116 | message => "I could not find this text node among its parent's children.", | ||||||||||||||||||||||||||||
| 117 | class => 'HTML::Object::HierarchyRequestError', | ||||||||||||||||||||||||||||
| 118 | }) ) if( !defined( $pos ) ); | ||||||||||||||||||||||||||||
| 119 | 1 | my $start = $pos; | |||||||||||||||||||||||||||
| 120 | $prev->reverse->foreach(sub | ||||||||||||||||||||||||||||
| 121 | { | ||||||||||||||||||||||||||||
| 122 | 1 | ▶ | if( !$self->_is_a( $_ => 'HTML::Object::DOM::Text' ) && | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=14.2
Branch: 1 branch
Condition33%1/3
$self->_is_a($_, "HTML::Object::DOM::Text") or $self->_is_a($_, "HTML::Object::DOM::Space")
MC/DC0%0/2
$self->_is_a($_, "HTML::Object::DOM::Text") or $self->_is_a($_, "HTML::Object::DOM::Space")
$self->_is_a($_, "HTML::Object::DOM::Text")
$self->_is_a($_, "HTML::Object::DOM::Space")
Truth table33%1/3
A or B
A= $self->_is_a($_, "HTML::Object::DOM::Text")B= $self->_is_a($_, "HTML::Object::DOM::Space")
| |||||||||||||||||||||||||||||
| 123 | !$self->_is_a( $_ => 'HTML::Object::DOM::Space' ) ) | ||||||||||||||||||||||||||||
| 124 | { | ||||||||||||||||||||||||||||
| 125 | 0 | return(1); | |||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | 1 | $start--; | |||||||||||||||||||||||||||
| 128 | 1 | }); | |||||||||||||||||||||||||||
| 129 | 1 | my $last = $pos; | |||||||||||||||||||||||||||
| 130 | $next->foreach(sub | ||||||||||||||||||||||||||||
| 131 | { | ||||||||||||||||||||||||||||
| 132 | 1 | ▶ | if( !$self->_is_a( $_ => 'HTML::Object::DOM::Text' ) && | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=14.2
Branch: 1 branch
Condition33%1/3
$self->_is_a($_, "HTML::Object::DOM::Text") or $self->_is_a($_, "HTML::Object::DOM::Space")
MC/DC0%0/2
$self->_is_a($_, "HTML::Object::DOM::Text") or $self->_is_a($_, "HTML::Object::DOM::Space")
$self->_is_a($_, "HTML::Object::DOM::Text")
$self->_is_a($_, "HTML::Object::DOM::Space")
Truth table33%1/3
A or B
A= $self->_is_a($_, "HTML::Object::DOM::Text")B= $self->_is_a($_, "HTML::Object::DOM::Space")
| |||||||||||||||||||||||||||||
| 133 | !$self->_is_a( $_ => 'HTML::Object::DOM::Space' ) ) | ||||||||||||||||||||||||||||
| 134 | { | ||||||||||||||||||||||||||||
| 135 | 0 | return(1); | |||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||
| 137 | 1 | $last++; | |||||||||||||||||||||||||||
| 138 | 1 | }); | |||||||||||||||||||||||||||
| 139 | 1 | my $removed = $siblings->splice( $start, ( ( $last - $start ) + 1 ), $self ); | |||||||||||||||||||||||||||
| 140 | 1 | $_->parent( undef ) for( @$removed ); | |||||||||||||||||||||||||||
| 141 | 1 | $self->parent( $parent ); | |||||||||||||||||||||||||||
| 142 | 1 | $self->value( "$content" ); | |||||||||||||||||||||||||||
| 143 | 1 | $self->reset(1); | |||||||||||||||||||||||||||
| 144 | 1 | return( $self ); | |||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | sub splitText | ||||||||||||||||||||||||||||
| 148 | { | ||||||||||||||||||||||||||||
| 149 | 1 | ▶ | my $self = shift( @_ ); | ||||||||||||||||||||||||||
|
Subroutine
splitText: called CC=8 SCAR=23.6
Pod
documented
| |||||||||||||||||||||||||||||
| 150 | 1 | my $offset = shift( @_ ); | |||||||||||||||||||||||||||
| 151 | 1 | ▶ | return( $self->error({ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 152 | message => "Offset value provided ($offset) is not an integer.", | ||||||||||||||||||||||||||||
| 153 | class => 'HTML::Object::TypeError', | ||||||||||||||||||||||||||||
| 154 | }) ) if( !$self->_is_integer( $offset ) ); | ||||||||||||||||||||||||||||
| 155 | 1 | my $value = $self->value; | |||||||||||||||||||||||||||
| 156 | 1 | my $size = $value->length; | |||||||||||||||||||||||||||
| 157 | 1 | ▶ | return( $self->error({ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 158 | message => "Offset value provided ($offset) is higher than the size of the string (" . $value->length . ")", | ||||||||||||||||||||||||||||
| 159 | class => 'HTML::Object::IndexSizeError', | ||||||||||||||||||||||||||||
| 160 | }) ) if( $offset > $size ); | ||||||||||||||||||||||||||||
| 161 | 1 | ▶ | if( $offset < 0 ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 162 | { | ||||||||||||||||||||||||||||
| 163 | 0 | $offset = ( $offset + $size ); | |||||||||||||||||||||||||||
| 164 | # For example, in the unlikely scenario where the negative offset is nth time the size of the text | ||||||||||||||||||||||||||||
| 165 | 0 | ▶ | while( $offset < 0 && abs( $offset ) > $size ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$offset < 0 and abs $offset > $size
MC/DC0%0/2
$offset < 0 and abs $offset > $size
$offset < 0
abs $offset > $size
Truth table0%0/3
A and B
A= $offset < 0B= abs $offset > $size
| |||||||||||||||||||||||||||||
| 166 | { | ||||||||||||||||||||||||||||
| 167 | 0 | $offset = ( $offset + $size ); | |||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||
| 170 | 1 | my $part1 = $value->substr( 0, $offset ); | |||||||||||||||||||||||||||
| 171 | 1 | my $part2 = $value->substr( $offset ); | |||||||||||||||||||||||||||
| 172 | 1 | my $new = $self->new( value => $part2 ); | |||||||||||||||||||||||||||
| 173 | 1 | my $parent = $self->parent; | |||||||||||||||||||||||||||
| 174 | 1 | ▶ | if( $parent ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 175 | { | ||||||||||||||||||||||||||||
| 176 | 1 | $new->parent( $parent ); | |||||||||||||||||||||||||||
| 177 | 1 | my $siblings = $parent->children; | |||||||||||||||||||||||||||
| 178 | 1 | my $pos = $siblings->pos( $self ); | |||||||||||||||||||||||||||
| 179 | 1 | ▶ | return( $self->error({ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 180 | message => "Unable to find our text element among our parent's children.", | ||||||||||||||||||||||||||||
| 181 | class => 'HTML::Object::HierarchyRequestError', | ||||||||||||||||||||||||||||
| 182 | }) ) if( !defined( $pos ) ); | ||||||||||||||||||||||||||||
| 183 | 1 | $siblings->splice( $pos + 1, 0, $new ); | |||||||||||||||||||||||||||
| 184 | 1 | $self->reset(1); | |||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||
| 186 | 1 | $self->value( $part1 ); | |||||||||||||||||||||||||||
| 187 | 1 | return( $new ); | |||||||||||||||||||||||||||
| 188 | } | ||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | 56 | ▶ | sub string_value { return( shift->value ); } | ||||||||||||||||||||||||||
|
Subroutine
string_value: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | 0 | ▶ | sub toString { return( shift->value ); } | ||||||||||||||||||||||||||
|
Subroutine
toString: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | 2 | ▶ | sub wholeText { return( shift->_get_adjacent_nodes->map(sub{ $_->value })->join( '' )->scalar ); } | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Subroutine
wholeText: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | sub _get_adjacent_nodes | ||||||||||||||||||||||||||||
| 197 | { | ||||||||||||||||||||||||||||
| 198 | 1 | ▶ | my $self = shift( @_ ); | ||||||||||||||||||||||||||
|
Subroutine
_get_adjacent_nodes: called CC=1 SCAR=1.3
| |||||||||||||||||||||||||||||
| 199 | 1 | my $prev = $self->left; | |||||||||||||||||||||||||||
| 200 | 1 | my $next = $self->right; | |||||||||||||||||||||||||||
| 201 | 1 | my $res = $self->new_array( $self ); | |||||||||||||||||||||||||||
| 202 | $prev->reverse->foreach(sub | ||||||||||||||||||||||||||||
| 203 | { | ||||||||||||||||||||||||||||
| 204 | 0 | ▶ | if( !$self->_is_a( $_ => 'HTML::Object::DOM::Text' ) && | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
Branch: 1 branch
Condition0%0/3
$self->_is_a($_, "HTML::Object::DOM::Text") or $self->_is_a($_, "HTML::Object::DOM::Space")
MC/DC0%0/2
$self->_is_a($_, "HTML::Object::DOM::Text") or $self->_is_a($_, "HTML::Object::DOM::Space")
$self->_is_a($_, "HTML::Object::DOM::Text")
$self->_is_a($_, "HTML::Object::DOM::Space")
Truth table0%0/3
A or B
A= $self->_is_a($_, "HTML::Object::DOM::Text")B= $self->_is_a($_, "HTML::Object::DOM::Space")
| |||||||||||||||||||||||||||||
| 205 | !$self->_is_a( $_ => 'HTML::Object::DOM::Space' ) ) | ||||||||||||||||||||||||||||
| 206 | { | ||||||||||||||||||||||||||||
| 207 | 0 | return(1); | |||||||||||||||||||||||||||
| 208 | } | ||||||||||||||||||||||||||||
| 209 | 0 | $res->unshift( $_ ); | |||||||||||||||||||||||||||
| 210 | 1 | }); | |||||||||||||||||||||||||||
| 211 | $next->foreach(sub | ||||||||||||||||||||||||||||
| 212 | { | ||||||||||||||||||||||||||||
| 213 | 1 | ▶ | if( !$self->_is_a( $_ => 'HTML::Object::DOM::Text' ) && | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=14.2
Branch: 1 branch
Condition33%1/3
$self->_is_a($_, "HTML::Object::DOM::Text") or $self->_is_a($_, "HTML::Object::DOM::Space")
MC/DC0%0/2
$self->_is_a($_, "HTML::Object::DOM::Text") or $self->_is_a($_, "HTML::Object::DOM::Space")
$self->_is_a($_, "HTML::Object::DOM::Text")
$self->_is_a($_, "HTML::Object::DOM::Space")
Truth table33%1/3
A or B
A= $self->_is_a($_, "HTML::Object::DOM::Text")B= $self->_is_a($_, "HTML::Object::DOM::Space")
| |||||||||||||||||||||||||||||
| 214 | !$self->_is_a( $_ => 'HTML::Object::DOM::Space' ) ) | ||||||||||||||||||||||||||||
| 215 | { | ||||||||||||||||||||||||||||
| 216 | 0 | return(1); | |||||||||||||||||||||||||||
| 217 | } | ||||||||||||||||||||||||||||
| 218 | 1 | $res->push( $_ ); | |||||||||||||||||||||||||||
| 219 | 1 | }); | |||||||||||||||||||||||||||
| 220 | 1 | return( $res ); | |||||||||||||||||||||||||||
| 221 | } | ||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||
| 223 | 33 | 1; | |||||||||||||||||||||||||||
| 224 | # NOTE: POD | ||||||||||||||||||||||||||||
| 225 | __END__ | ||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||
| 227 | =encoding utf-8 | ||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||
| 229 | =head1 NAME | ||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||
| 231 | HTML::Object::DOM::Text - HTML Object DOM Text Class | ||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||
| 233 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||
| 235 | use HTML::Object::DOM::Text; | ||||||||||||||||||||||||||||
| 236 | my $text = HTML::Object::DOM::Text->new( value => $some_text ) || | ||||||||||||||||||||||||||||
| 237 | die( HTML::Object::DOM::Text->error, "\n" ); | ||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||
| 239 | =head1 VERSION | ||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||
| 241 | v0.2.1_1 | ||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||
| 243 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||
| 245 | It inherits from L<HTML::Object::Text> and L<HTML::Object::DOM::CharacterData> | ||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||
| 247 | =head1 INHERITANCE | ||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||
| 249 | +-----------------------+ +---------------------------+ +-------------------------+ +----------------------------------+ +-------------------------+ | ||||||||||||||||||||||||||||
| 250 | | HTML::Object::Element | --> | HTML::Object::EventTarget | --> | HTML::Object::DOM::Node | --> | HTML::Object::DOM::CharacterData | --> | HTML::Object::DOM::Text | | ||||||||||||||||||||||||||||
| 251 | +-----------------------+ +---------------------------+ +-------------------------+ +----------------------------------+ +-------------------------+ | ||||||||||||||||||||||||||||
| 252 | | ^ | ||||||||||||||||||||||||||||
| 253 | | | | ||||||||||||||||||||||||||||
| 254 | v | | ||||||||||||||||||||||||||||
| 255 | +-----------------------+ | | ||||||||||||||||||||||||||||
| 256 | | HTML::Object::Text | -----------------------------------------------------------------------------------------------------------------+ | ||||||||||||||||||||||||||||
| 257 | +-----------------------+ | ||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||
| 259 | =head1 PROPERTIES | ||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||
| 261 | =head2 assignedSlot | ||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||
| 263 | Normally this is a read-only property, but under perl, you can set or get a L<HTML::Object::DOM::Element::Slot> object associated with the element. | ||||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||
| 265 | See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/Text/assignedSlot> | ||||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||
| 267 | =head2 nodeValue | ||||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||
| 269 | Sets or gets the text value for this element. | ||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||
| 271 | =head2 wholeText | ||||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||
| 273 | The read-only C<wholeText> property of the L<HTML::Object::DOM::Text> interface returns the full text of all L<Text|HTML::Object::DOM::Text> nodes logically adjacent to the node. The text is concatenated in document order. This allows specifying any text node and obtaining all adjacent text as a single string. | ||||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||
| 275 | It returns a string with the concanated text. | ||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||
| 277 | Example: | ||||||||||||||||||||||||||||
| 278 | |||||||||||||||||||||||||||||
| 279 | <p id="favy">I like <span class="maybe-not">Shochu</span>, Dorayaki and Natto-gohan.</p> | ||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||
| 281 | $doc->getElementsByTagName('span')->[0]->remove; | ||||||||||||||||||||||||||||
| 282 | # Now paragraph contains 2 text nodes: | ||||||||||||||||||||||||||||
| 283 | # 'I like ' | ||||||||||||||||||||||||||||
| 284 | # ', Dorayaki and Natto-gohan.' | ||||||||||||||||||||||||||||
| 285 | say $doc->getElementById('favy')->getFirstChild->wholeText; | ||||||||||||||||||||||||||||
| 286 | # I like , Dorayaki and Natto-gohan. | ||||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||
| 288 | See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/Text/wholeText> | ||||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||
| 290 | =head1 METHODS | ||||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||
| 292 | =head2 getAttributes | ||||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||
| 294 | Returns an empty list in list context, or an empty array reference in scalar context. | ||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||
| 296 | =head2 getChildNodes | ||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||
| 298 | Returns an empty list in list context, or an empty array reference in scalar context. | ||||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||
| 300 | =head2 getElementById | ||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||
| 302 | Returns an empty list in list context, or C<undef> in scalar context. | ||||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||
| 304 | =head2 getFirstChild | ||||||||||||||||||||||||||||
| 305 | |||||||||||||||||||||||||||||
| 306 | Returns an empty list in list context, or C<undef> in scalar context. | ||||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||
| 308 | =head2 getLastChild | ||||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||
| 310 | Returns an empty list in list context, or C<undef> in scalar context. | ||||||||||||||||||||||||||||
| 311 | |||||||||||||||||||||||||||||
| 312 | =head2 getParentNode | ||||||||||||||||||||||||||||
| 313 | |||||||||||||||||||||||||||||
| 314 | Returns the parent node, if any. | ||||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||
| 316 | =head2 getRootNode | ||||||||||||||||||||||||||||
| 317 | |||||||||||||||||||||||||||||
| 318 | Returns the L<root node|HTML::Object::DOM::Document> | ||||||||||||||||||||||||||||
| 319 | |||||||||||||||||||||||||||||
| 320 | =head2 getValue | ||||||||||||||||||||||||||||
| 321 | |||||||||||||||||||||||||||||
| 322 | Returns the text value of this comment, i.e. the text between C<<!--> and C<-->> | ||||||||||||||||||||||||||||
| 323 | |||||||||||||||||||||||||||||
| 324 | =head2 is_inside | ||||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||
| 326 | Provided with a node, this will return true if it is inside this text's parent or false otherwise. | ||||||||||||||||||||||||||||
| 327 | |||||||||||||||||||||||||||||
| 328 | =head2 isAttributeNode | ||||||||||||||||||||||||||||
| 329 | |||||||||||||||||||||||||||||
| 330 | Returns false. | ||||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||
| 332 | =head2 isCommentNode | ||||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||
| 334 | Returns true. | ||||||||||||||||||||||||||||
| 335 | |||||||||||||||||||||||||||||
| 336 | =head2 isElementNode | ||||||||||||||||||||||||||||
| 337 | |||||||||||||||||||||||||||||
| 338 | Returns false. | ||||||||||||||||||||||||||||
| 339 | |||||||||||||||||||||||||||||
| 340 | =head2 isEqualNode | ||||||||||||||||||||||||||||
| 341 | |||||||||||||||||||||||||||||
| 342 | Provided with another element object, and this returns true if both text element are the same, or false otherwise. | ||||||||||||||||||||||||||||
| 343 | |||||||||||||||||||||||||||||
| 344 | =head2 isNamespaceNode | ||||||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||||||
| 346 | Returns false. | ||||||||||||||||||||||||||||
| 347 | |||||||||||||||||||||||||||||
| 348 | =head2 isPINode | ||||||||||||||||||||||||||||
| 349 | |||||||||||||||||||||||||||||
| 350 | Returns false. | ||||||||||||||||||||||||||||
| 351 | |||||||||||||||||||||||||||||
| 352 | =head2 isProcessingInstructionNode | ||||||||||||||||||||||||||||
| 353 | |||||||||||||||||||||||||||||
| 354 | Returns false. | ||||||||||||||||||||||||||||
| 355 | |||||||||||||||||||||||||||||
| 356 | =head2 isTextNode | ||||||||||||||||||||||||||||
| 357 | |||||||||||||||||||||||||||||
| 358 | Returns true. | ||||||||||||||||||||||||||||
| 359 | |||||||||||||||||||||||||||||
| 360 | =head2 parent | ||||||||||||||||||||||||||||
| 361 | |||||||||||||||||||||||||||||
| 362 | Set or get this text's parent L<node|HTML::Object::DOM::Node> | ||||||||||||||||||||||||||||
| 363 | |||||||||||||||||||||||||||||
| 364 | =head2 replaceWholeText | ||||||||||||||||||||||||||||
| 365 | |||||||||||||||||||||||||||||
| 366 | This method of the L<Text|HTML::Object::DOM::Text> interface replaces the text of the node and all of its logically adjacent text nodes with the specified C<text>. The replaced nodes are removed, except the current node. | ||||||||||||||||||||||||||||
| 367 | |||||||||||||||||||||||||||||
| 368 | It returns the current node with the newly C<text> set. | ||||||||||||||||||||||||||||
| 369 | |||||||||||||||||||||||||||||
| 370 | Example: | ||||||||||||||||||||||||||||
| 371 | |||||||||||||||||||||||||||||
| 372 | <p id="favy">I like apple,<span class="and"> and</span> orange,<span class="and"> and</span> kaki</p> | ||||||||||||||||||||||||||||
| 373 | $doc->getElementsByTagName('span')->foreach(sub | ||||||||||||||||||||||||||||
| 374 | { | ||||||||||||||||||||||||||||
| 375 | $_->remove; | ||||||||||||||||||||||||||||
| 376 | }); | ||||||||||||||||||||||||||||
| 377 | # Now text is: I like apple, orange, kaki | ||||||||||||||||||||||||||||
| 378 | # which are 3 text nodes | ||||||||||||||||||||||||||||
| 379 | # Take the 2nd one (for example) and set a new text for it and its adjacent siblings | ||||||||||||||||||||||||||||
| 380 | $doc->getElementById('favy')->getChildNodes->[1]->replaceWholeText( 'I like fruits' ); | ||||||||||||||||||||||||||||
| 381 | # Now the whole chunk has become: | ||||||||||||||||||||||||||||
| 382 | # <p id="favy">I like fruits</p> | ||||||||||||||||||||||||||||
| 383 | |||||||||||||||||||||||||||||
| 384 | See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/Text/replaceWholeText> | ||||||||||||||||||||||||||||
| 385 | |||||||||||||||||||||||||||||
| 386 | =head2 splitText | ||||||||||||||||||||||||||||
| 387 | |||||||||||||||||||||||||||||
| 388 | Provided with an C<offset> position and this method breaks the L<Text|HTML::Object::DOM::Text> node into two nodes at the specified C<offset>, keeping both nodes in the tree as siblings. | ||||||||||||||||||||||||||||
| 389 | |||||||||||||||||||||||||||||
| 390 | After the split, the current node contains all the content up to the specified offset point, and a newly created node of the same type contains the remaining text. The newly created node is returned to the caller. If the original node had a parent, the new node is inserted as the next sibling of the original node. If the offset is equal to the length of the original node, the newly created node has no data. | ||||||||||||||||||||||||||||
| 391 | |||||||||||||||||||||||||||||
| 392 | It returns the newly created L<Text|HTML::Object::DOM::Text> node that contains the text after the specified offset point. | ||||||||||||||||||||||||||||
| 393 | |||||||||||||||||||||||||||||
| 394 | It returns an C<HTML::Object::IndexSizeError> if the specified C<offset> is greater than the size of the node's text. | ||||||||||||||||||||||||||||
| 395 | |||||||||||||||||||||||||||||
| 396 | Example: | ||||||||||||||||||||||||||||
| 397 | |||||||||||||||||||||||||||||
| 398 | <p>foobar</p> | ||||||||||||||||||||||||||||
| 399 | |||||||||||||||||||||||||||||
| 400 | my $p = $doc->getElementsByTagName('p')->first; | ||||||||||||||||||||||||||||
| 401 | # Get contents of <p> as a text node | ||||||||||||||||||||||||||||
| 402 | my $foobar = $p->firstChild; | ||||||||||||||||||||||||||||
| 403 | |||||||||||||||||||||||||||||
| 404 | # Split 'foobar' into two text nodes, 'foo' and 'bar', | ||||||||||||||||||||||||||||
| 405 | # and save 'bar' as a const | ||||||||||||||||||||||||||||
| 406 | my $bar = $foobar->splitText(3); | ||||||||||||||||||||||||||||
| 407 | |||||||||||||||||||||||||||||
| 408 | # Create a <u> element containing ' new content ' | ||||||||||||||||||||||||||||
| 409 | my $u = $doc->createElement('u'); | ||||||||||||||||||||||||||||
| 410 | $u->appendChild( $doc->createTextNode( ' new content ' ) ); | ||||||||||||||||||||||||||||
| 411 | # Add <u> before 'bar' | ||||||||||||||||||||||||||||
| 412 | $p->insertBefore( $u, $bar ); | ||||||||||||||||||||||||||||
| 413 | # The result is: <p>foo<u> new content </u>bar</p> | ||||||||||||||||||||||||||||
| 414 | |||||||||||||||||||||||||||||
| 415 | See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/Text/splitText> | ||||||||||||||||||||||||||||
| 416 | |||||||||||||||||||||||||||||
| 417 | =head2 string_value | ||||||||||||||||||||||||||||
| 418 | |||||||||||||||||||||||||||||
| 419 | Returns the content of the comment as a string. | ||||||||||||||||||||||||||||
| 420 | |||||||||||||||||||||||||||||
| 421 | =head2 toString | ||||||||||||||||||||||||||||
| 422 | |||||||||||||||||||||||||||||
| 423 | Returns the content of the comment as a string. | ||||||||||||||||||||||||||||
| 424 | |||||||||||||||||||||||||||||
| 425 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 426 | |||||||||||||||||||||||||||||
| 427 | Jacques Deguest E<lt>F<jack@deguest.jp>E<gt> | ||||||||||||||||||||||||||||
| 428 | |||||||||||||||||||||||||||||
| 429 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 430 | |||||||||||||||||||||||||||||
| 431 | L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/Text> | ||||||||||||||||||||||||||||
| 432 | |||||||||||||||||||||||||||||
| 433 | =head1 COPYRIGHT & LICENSE | ||||||||||||||||||||||||||||
| 434 | |||||||||||||||||||||||||||||
| 435 | Copyright(c) 2021 DEGUEST Pte. Ltd. | ||||||||||||||||||||||||||||
| 436 | |||||||||||||||||||||||||||||
| 437 | All rights reserved | ||||||||||||||||||||||||||||
| 438 | |||||||||||||||||||||||||||||
| 439 | This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 440 | |||||||||||||||||||||||||||||
| 441 | =cut | ||||||||||||||||||||||||||||