blib/lib/HTML/Object/DOM/Text.pm untested

filter: Statementstmt 0.0% 0 / 131 Branchbran 0.0% 0 / 32 Conditioncond 0.0% 0 / 22 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 34 Podpod 0.0% 0 / 28 totaltotal 0.0% 0 / 247 CC 17 SCAR 25.7
CC
17
Cov
0%
replaceWholeText
42.0
splitText
42.0
isEqualNode
12.0
CRAP 13.1
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 use strict;
18 use warnings;
19 warnings::register_categories( 'HTML::Object' );
20 use parent qw( HTML::Object::Text HTML::Object::DOM::CharacterData );
21 use vars qw( $VERSION );
22 our $VERSION = 'v0.2.1_1';
23 };
24  
25 use strict;
26 use warnings;
27  
28 sub init
29 {
30 my $self = shift( @_ );
31 $self->{_init_strict_use_sub} = 1;
32 $self->HTML::Object::Text::init( @_ ) || return( $self->pass_error );
33 return( $self );
34 }
35  
36 sub assignedSlot { return( shift->_set_get_object_without_init( 'assignedslot', 'HTML::Object::DOM::Element::Slot', @_ ) ); }
37  
38 sub getAttributes { return( wantarray() ? () : [] ); }
39  
40 sub getChildNodes { return( wantarray() ? () : [] ); }
41  
42 sub getElementById { return; }
43  
44 sub getFirstChild { return; }
45  
46 sub getLastChild { return; }
47  
48 sub getParentNode { return( shift->parent ); }
49  
50 # Inherited
51 # sub getNextSibling;
52  
53 # Inherited
54 # sub getPreviousSibling;
55  
56 sub getRootNode { return( shift->parent->getRootNode ); }
57  
58 sub getValue { return( shift->value ); }
59  
60 sub is_inside
61 {
62 my( $text, $node ) = @_;
63 return( $text->parent->is_inside( $node ) );
64 }
65  
66 sub isAttributeNode { return(0); }
67  
68 sub isCommentNode { return(0); }
69  
70 sub isElementNode { return(0); }
71  
72 sub isEqualNode
73 {
74 my $self = shift( @_ );
75 my $e = shift( @_ ) || return( $self->error( "No html element was provided to insert." ) );
76 return( $self->error( "Element provided (", overload::StrVal( $e ), ") is not an HTML::Object::Element." ) ) if( !$self->_is_a( $e => 'HTML::Object::Element' ) );
77 return(0) if( !$self->_is_a( $e => 'HTML::Object::Text' ) );
78 return( $self->value eq $e->value );
79 }
80  
81 sub isNamespaceNode { return(0); }
82  
83 sub isPINode { return(0); }
84  
85 sub isProcessingInstructionNode { return(0); }
86  
87 sub isTextNode { return(1); }
88  
89 # Note: Property
90 sub nodeValue : lvalue { return( shift->_set_get_lvalue( 'value', @_ ) ); }
91  
92 sub parent { return( shift->_set_get_object_without_init({
93 field => 'parent',
94 weaken => 1,
95 }, 'HTML::Object::DOM::Node', @_ ) ); }
96  
97 sub replaceWholeText
98 {
99 my $self = shift( @_ );
100 my $content = shift( @_ );
101 return( $self->error({
102 message => "Content provided is a reference that cannot be stringified.",
103 class => 'HTML::Object::TypeError',
104 }) ) if( ref( $content ) && !overload::Method( $content, '""' ) );
105 my $prev = $self->left;
106 my $next = $self->right;
107 my $parent = $self->parent;
108 if( !$parent )
109 {
110 $self->value( "$content" );
111 return( $self );
112 }
113 my $siblings = $parent->children;
114 my $pos = $siblings->pos( $self );
115 return( $self->error({
116 message => "I could not find this text node among its parent's children.",
117 class => 'HTML::Object::HierarchyRequestError',
118 }) ) if( !defined( $pos ) );
119 my $start = $pos;
120 $prev->reverse->foreach(sub
121 {
122 if( !$self->_is_a( $_ => 'HTML::Object::DOM::Text' ) &&
123 !$self->_is_a( $_ => 'HTML::Object::DOM::Space' ) )
124 {
125 return(1);
126 }
127 $start--;
128 });
129 my $last = $pos;
130 $next->foreach(sub
131 {
132 if( !$self->_is_a( $_ => 'HTML::Object::DOM::Text' ) &&
133 !$self->_is_a( $_ => 'HTML::Object::DOM::Space' ) )
134 {
135 return(1);
136 }
137 $last++;
138 });
139 my $removed = $siblings->splice( $start, ( ( $last - $start ) + 1 ), $self );
140 $_->parent( undef ) for( @$removed );
141 $self->parent( $parent );
142 $self->value( "$content" );
143 $self->reset(1);
144 return( $self );
145 }
146  
147 sub splitText
148 {
149 my $self = shift( @_ );
150 my $offset = shift( @_ );
151 return( $self->error({
152 message => "Offset value provided ($offset) is not an integer.",
153 class => 'HTML::Object::TypeError',
154 }) ) if( !$self->_is_integer( $offset ) );
155 my $value = $self->value;
156 my $size = $value->length;
157 return( $self->error({
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 if( $offset < 0 )
162 {
163 $offset = ( $offset + $size );
164 # For example, in the unlikely scenario where the negative offset is nth time the size of the text
165 while( $offset < 0 && abs( $offset ) > $size )
166 {
167 $offset = ( $offset + $size );
168 }
169 }
170 my $part1 = $value->substr( 0, $offset );
171 my $part2 = $value->substr( $offset );
172 my $new = $self->new( value => $part2 );
173 my $parent = $self->parent;
174 if( $parent )
175 {
176 $new->parent( $parent );
177 my $siblings = $parent->children;
178 my $pos = $siblings->pos( $self );
179 return( $self->error({
180 message => "Unable to find our text element among our parent's children.",
181 class => 'HTML::Object::HierarchyRequestError',
182 }) ) if( !defined( $pos ) );
183 $siblings->splice( $pos + 1, 0, $new );
184 $self->reset(1);
185 }
186 $self->value( $part1 );
187 return( $new );
188 }
189  
190 sub string_value { return( shift->value ); }
191  
192 sub toString { return( shift->value ); }
193  
194 sub wholeText { return( shift->_get_adjacent_nodes->map(sub{ $_->value })->join( '' )->scalar ); }
195  
196 sub _get_adjacent_nodes
197 {
198 my $self = shift( @_ );
199 my $prev = $self->left;
200 my $next = $self->right;
201 my $res = $self->new_array( $self );
202 $prev->reverse->foreach(sub
203 {
204 if( !$self->_is_a( $_ => 'HTML::Object::DOM::Text' ) &&
205 !$self->_is_a( $_ => 'HTML::Object::DOM::Space' ) )
206 {
207 return(1);
208 }
209 $res->unshift( $_ );
210 });
211 $next->foreach(sub
212 {
213 if( !$self->_is_a( $_ => 'HTML::Object::DOM::Text' ) &&
214 !$self->_is_a( $_ => 'HTML::Object::DOM::Space' ) )
215 {
216 return(1);
217 }
218 $res->push( $_ );
219 });
220 return( $res );
221 }
222  
223 1;
224 # NOTE: POD
225 __END__