lib/HTML/Object/DOM/Text.pm

filter: Statementstmt 72.0% 85 / 118 Branchbran 38.8% 14 / 36 Conditioncond 20.0% 4 / 20 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 53.8% 21 / 39 Podpod 100% 26 / 26 totaltotal 59.5% 150 / 252 CC 27 SCAR 16.7
CC
27
Cov
59%
isEqualNode
20.0
__ANON__
12.0
splitText
10.6
CRAP 5.3
« lib/HTML/Object/DOM/XPathResult.pm ↑ Summary blib/lib/HTML/Object/ElementDataMap.pm »
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 36 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 36 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 36 ▶ warnings::register_categories( 'HTML::Object' );
Subroutine BEGIN: called CC=1 SCAR=0.0
20 36 ▶ use parent qw( HTML::Object::Text HTML::Object::DOM::CharacterData );
Subroutine BEGIN: called CC=1 SCAR=0.0
21 36 ▶ use vars qw( $VERSION );
Subroutine BEGIN: called CC=1 SCAR=0.0
22 36 our $VERSION = 'v0.2.1_1';
23 };
24  
25 36 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 36 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 sub init
29 {
30 148 ▶ my $self = shift( @_ );
Subroutine init: called CC=2 SCAR=7.0
Pod documented
31 148 $self->{_init_strict_use_sub} = 1;
32 148 ▶ $self->HTML::Object::Text::init( @_ ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->HTML::Object::Text::init(@_) or return $self->pass_error 0 148 148
33 148 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
BranchTrueFalseTotal
wantarray ? : 0 0 0
39  
40 139 ▶sub getChildNodes { return( wantarray() ? () : [] ); }
Subroutine getChildNodes: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 111 28 139
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 0 ▶sub isAttributeNode { return(0); }
Subroutine isAttributeNode: not called CC=1 SCAR=6.9
Pod documented
67  
68 0 ▶sub isCommentNode { return(0); }
Subroutine isCommentNode: not called CC=1 SCAR=6.9
Pod documented
69  
70 111 ▶sub isElementNode { return(0); }
Subroutine isElementNode: called CC=1 SCAR=0.0
Pod documented
71  
72 sub isEqualNode
73 {
74 0 ▶ my $self = shift( @_ );
Subroutine isEqualNode: not called CC=4 SCAR=30.0
Pod documented
75 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.'))
l!l
00
MC/DC0%0/1
shift @_ || (return $self->error('No html element was provided to insert.'))
shift @_
Truth table0%0/2
A
A= shift @_
Aresult
11
00
76 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
BranchTrueFalseTotal
unless $self->_is_a($e, "HTML::Object::Element") 0 0 0
77 0 ▶ return(0) if( !$self->_is_a( $e => 'HTML::Object::Text' ) );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_a($e, "HTML::Object::Text") 0 0 0
78 0 return( $self->value eq $e->value );
79 }
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 413 ▶ 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
BranchTrueFalseTotal
if ref $content and not overload::Method($content, "\"\"") 0 1 1
Condition33%1/3
ref $content and not overload::Method($content, "\"\"")
!ll&&!rl&&r
100
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, "\"\"")
ABresult
0X0
100
111
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
BranchTrueFalseTotal
unless ($parent) 0 1 1
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
BranchTrueFalseTotal
unless defined $pos 0 1 1
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
BranchTrueFalseTotal
unless ($self->_is_a($_, "HTML::Object::DOM::Text") or $self->_is_a($_, "HTML::Object::DOM::Space")) 0 1 1
Condition33%1/3
$self->_is_a($_, "HTML::Object::DOM::Text") or $self->_is_a($_, "HTML::Object::DOM::Space")
l!l&&r!l&&!r
100
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")
ABresult
1X1
011
000
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
BranchTrueFalseTotal
unless ($self->_is_a($_, "HTML::Object::DOM::Text") or $self->_is_a($_, "HTML::Object::DOM::Space")) 0 1 1
Condition33%1/3
$self->_is_a($_, "HTML::Object::DOM::Text") or $self->_is_a($_, "HTML::Object::DOM::Space")
l!l&&r!l&&!r
100
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")
ABresult
1X1
011
000
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
BranchTrueFalseTotal
unless $self->_is_integer($offset) 0 1 1
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
BranchTrueFalseTotal
if $offset > $size 0 1 1
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
BranchTrueFalseTotal
if ($offset < 0) 0 1 1
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
BranchTrueFalseTotal
if ($offset < 0 and abs $offset > $size) 0 0 0
Condition0%0/3
$offset < 0 and abs $offset > $size
!ll&&!rl&&r
000
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
ABresult
0X0
100
111
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
BranchTrueFalseTotal
if ($parent) 1 0 1
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
BranchTrueFalseTotal
unless defined $pos 0 1 1
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
BranchTrueFalseTotal
unless ($self->_is_a($_, "HTML::Object::DOM::Text") or $self->_is_a($_, "HTML::Object::DOM::Space")) 0 0 0
Condition0%0/3
$self->_is_a($_, "HTML::Object::DOM::Text") or $self->_is_a($_, "HTML::Object::DOM::Space")
l!l&&r!l&&!r
000
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")
ABresult
1X1
011
000
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
BranchTrueFalseTotal
unless ($self->_is_a($_, "HTML::Object::DOM::Text") or $self->_is_a($_, "HTML::Object::DOM::Space")) 0 1 1
Condition33%1/3
$self->_is_a($_, "HTML::Object::DOM::Text") or $self->_is_a($_, "HTML::Object::DOM::Space")
l!l&&r!l&&!r
100
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")
ABresult
1X1
011
000
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 36 1;
224 # NOTE: POD
225 __END__
« lib/HTML/Object/DOM/XPathResult.pm ↑ Summary blib/lib/HTML/Object/ElementDataMap.pm »