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 isEqualNode
67 {
68 my $self = shift( @_ );
69 my $e = shift( @_ ) || return( $self->error( "No html element was provided to insert." ) );
70 return( $self->error( "Element provided (", overload::StrVal( $e ), ") is not an HTML::Object::Element." ) ) if( !$self->_is_a( $e => 'HTML::Object::Element' ) );
71 return(0) if( !$self->_is_a( $e => 'HTML::Object::Text' ) );
72 return( $self->value eq $e->value );
73 }
74  
75 sub isAttributeNode { return(0); }
76  
77 sub isCommentNode { return(0); }
78  
79 sub isElementNode { return(0); }
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__
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