lib/HTML/Object/DOM/CharacterData.pm

filter: Statementstmt 14.9% 22 / 147 Branchbran 0.0% 0 / 72 Conditioncond 0.0% 0 / 52 MC/DCmcdc 0.0% 0 / 31 Subroutinesub 33.3% 8 / 24 Podpod 100% 13 / 13 totaltotal 12.6% 43 / 339 CC 59 SCAR 43.5
CC
59
Cov
8%
_sanity_check_for_before_after
240.0
replaceData
90.0
deleteData
72.0
CRAP 77.6
« lib/HTML/Object/Element.pm ↑ Summary blib/lib/HTML/Object/XPath/Expr.pm »
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 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::DOM::Node );
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.0';
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 # 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 0 ▶ my $self = shift( @_ );
Subroutine after: not called CC=4 SCAR=30.0
Pod documented
33 0 ▶ my $parent = $self->parent || return;
Condition0%0/2
$self->parent || (return)
l!l
00
MC/DC0%0/1
$self->parent || (return)
$self->parent
Truth table0%0/2
A
A= $self->parent
Aresult
11
00
34 0 my $pos = $parent->children->pos( $self );
35 0 ▶ return( $self->error({
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pos 0 0 0
36 message => "This CharacterData object cannot be found in its parent node.",
37 class => 'HTML::Object::HierarchyRequestError',
38 }) ) if( !defined( $pos ) );
39 0 my $list = $self->_get_from_list_of_elements_or_html( @_ );
40 0 ▶ $self->_sanity_check_for_before_after( $list ) ||
Branch: 1 branch
BranchTrueFalseTotal
$self->_sanity_check_for_before_after($list) or return $self->pass_error 0 0 0
41 return( $self->pass_error );
42
43 0 $parent->children->splice( $pos + 1, 0, $list->list );
44 $list->foreach(sub
45 {
46 0 ▶ $_->parent( $parent );
Subroutine __ANON__: not called CC=1 SCAR=6.9
47 0 });
48 0 $parent->reset(1);
49 0 return( $self );
50 }
51  
52 0 ▶sub appendData { return( shift->value->append( @_ ) ); }
Subroutine appendData: not called CC=1 SCAR=6.9
Pod documented
53  
54 sub before
55 {
56 0 ▶ my $self = shift( @_ );
Subroutine before: not called CC=4 SCAR=30.0
Pod documented
57 0 ▶ my $parent = $self->parent || return;
Condition0%0/2
$self->parent || (return)
l!l
00
MC/DC0%0/1
$self->parent || (return)
$self->parent
Truth table0%0/2
A
A= $self->parent
Aresult
11
00
58 0 my $pos = $parent->children->pos( $self );
59 0 ▶ return( $self->error({
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pos 0 0 0
60 message => "This CharacterData object cannot be found in its parent node.",
61 class => 'HTML::Object::HierarchyRequestError',
62 }) ) if( !defined( $pos ) );
63 0 my $list = $self->_get_from_list_of_elements_or_html( @_ );
64 0 ▶ $self->_sanity_check_for_before_after( $list ) ||
Branch: 1 branch
BranchTrueFalseTotal
$self->_sanity_check_for_before_after($list) or return $self->pass_error 0 0 0
65 return( $self->pass_error );
66
67 $list->foreach(sub
68 {
69 0 ▶ $parent->children->splice( $pos, 0, $_ );
Subroutine __ANON__: not called CC=1 SCAR=6.9
70 0 $_->parent( $parent );
71 0 $pos++;
72 0 });
73 0 $parent->reset(1);
74 0 return( $self );
75 }
76  
77 # Note: property
78 2 ▶sub data : lvalue { return( shift->_set_get_lvalue( 'value', @_ ) ); }
Subroutine data: called CC=1 SCAR=0.0
Pod documented
79  
80 sub deleteData
81 {
82 0 ▶ my $self = shift( @_ );
Subroutine deleteData: not called CC=8 SCAR=42.8
Pod documented
83 0 my $offset = shift( @_ );
84 0 my $len = shift( @_ );
85 0 ▶ $offset = "$offset" if( ref( $offset ) && overload::Method( $offset, '""' ) );
Branch: 1 branch
BranchTrueFalseTotal
if ref $offset and overload::Method($offset, "\"\"") 0 0 0
Condition0%0/3
ref $offset and overload::Method($offset, "\"\"")
!ll&&!rl&&r
000
MC/DC0%0/2
ref $offset and overload::Method($offset, "\"\"")
ref $offset overload::Method($offset, "\"\"")
Truth table0%0/3
A and B
A= ref $offsetB= overload::Method($offset, "\"\"")
ABresult
0X0
100
111
86 0 ▶ $len = "$len" if( ref( $len ) && overload::Method( $len, '""' ) );
Branch: 1 branch
BranchTrueFalseTotal
if ref $len and overload::Method($len, "\"\"") 0 0 0
Condition0%0/3
ref $len and overload::Method($len, "\"\"")
!ll&&!rl&&r
000
MC/DC0%0/2
ref $len and overload::Method($len, "\"\"")
ref $len overload::Method($len, "\"\"")
Truth table0%0/3
A and B
A= ref $lenB= overload::Method($len, "\"\"")
ABresult
0X0
100
111
87 0 ▶ return( $self->error({
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_integer($offset) 0 0 0
88 message => "Offset value provided \"$offset\" is not an integer.",
89 class => 'HTML::Object::TypeError',
90 }) ) if( !$self->_is_integer( $offset ) );
91 0 ▶ return( $self->error({
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_integer($len) 0 0 0
92 message => "Length value provided \"$len\" is not an integer.",
93 class => 'HTML::Object::TypeError',
94 }) ) if( !$self->_is_integer( $len ) );
95 0 $offset = int( $offset );
96 0 $len = int( $len );
97 0 ▶ return( $self->error({
Branch: 1 branch
BranchTrueFalseTotal
if $offset > $self->value->length 0 0 0
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 0 $self->value->substr( $offset, $len, '' );
102 0 $self->reset(1);
103 0 return( $self );
104 }
105  
106 sub insertData
107 {
108 0 ▶ my $self = shift( @_ );
Subroutine insertData: not called CC=7 SCAR=40.3
Pod documented
109 0 my $offset = shift( @_ );
110 0 my $str = shift( @_ );
111 # Nothing to do, stop here
112 0 ▶ return( $self ) if( !defined( $str ) || !CORE::length( "$str" ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $str and CORE::length "$str" 0 0 0
Condition0%0/3
defined $str and CORE::length "$str"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $str and CORE::length "$str"
defined $str CORE::length "$str"
Truth table0%0/3
A and B
A= defined $strB= CORE::length "$str"
ABresult
0X0
100
111
113 0 ▶ $offset = "$offset" if( ref( $offset ) && overload::Method( $offset, '""' ) );
Branch: 1 branch
BranchTrueFalseTotal
if ref $offset and overload::Method($offset, "\"\"") 0 0 0
Condition0%0/3
ref $offset and overload::Method($offset, "\"\"")
!ll&&!rl&&r
000
MC/DC0%0/2
ref $offset and overload::Method($offset, "\"\"")
ref $offset overload::Method($offset, "\"\"")
Truth table0%0/3
A and B
A= ref $offsetB= overload::Method($offset, "\"\"")
ABresult
0X0
100
111
114 0 $str = "$str";
115 0 ▶ return( $self->error({
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_integer($offset) 0 0 0
116 message => "Offset value provided \"$offset\" is not an integer.",
117 class => 'HTML::Object::TypeError',
118 }) ) if( !$self->_is_integer( $offset ) );
119 0 $offset = int( $offset );
120 0 ▶ return( $self->error({
Branch: 1 branch
BranchTrueFalseTotal
if $offset > $self->value->length 0 0 0
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 0 $self->value->substr( $offset, 0, $str );
125 0 $self->reset(1);
126 0 return( $self );
127 }
128  
129 # Note: property
130 0 ▶sub length { return( shift->value->length ); }
Subroutine length: not called CC=1 SCAR=6.9
Pod documented
131  
132 # Note: property
133 sub nextElementSibling
134 {
135 0 ▶ my $self = shift( @_ );
Subroutine nextElementSibling: not called CC=3 SCAR=24.8
Pod documented
136 0 ▶ my $parent = $self->parent || return;
Condition0%0/2
$self->parent || (return)
l!l
00
MC/DC0%0/1
$self->parent || (return)
$self->parent
Truth table0%0/2
A
A= $self->parent
Aresult
11
00
137 0 my $pos = $parent->children->pos( $self );
138 0 ▶ return( $self->error({
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pos 0 0 0
139 message => "This CharacterData object cannot be found in its parent node.",
140 class => 'HTML::Object::HierarchyRequestError',
141 }) ) if( !defined( $pos ) );
142 0 return( $parent->children->index( $pos + 1 ) );
143 }
144  
145 # Note: property
146 sub previousElementSibling
147 {
148 0 ▶ my $self = shift( @_ );
Subroutine previousElementSibling: not called CC=4 SCAR=30.0
Pod documented
149 0 ▶ my $parent = $self->parent || return;
Condition0%0/2
$self->parent || (return)
l!l
00
MC/DC0%0/1
$self->parent || (return)
$self->parent
Truth table0%0/2
A
A= $self->parent
Aresult
11
00
150 0 my $pos = $parent->children->pos( $self );
151 0 ▶ return( $self->error({
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pos 0 0 0
152 message => "This CharacterData object cannot be found in its parent node.",
153 class => 'HTML::Object::HierarchyRequestError',
154 }) ) if( !defined( $pos ) );
155 0 ▶ return if( $pos <= 0 );
Branch: 1 branch
BranchTrueFalseTotal
if $pos <= 0 0 0 0
156 0 return( $parent->children->index( $pos - 1 ) );
157 }
158  
159 sub remove
160 {
161 0 ▶ my $self = shift( @_ );
Subroutine remove: not called CC=3 SCAR=24.8
Pod documented
162 0 ▶ my $parent = $self->parent || return;
Condition0%0/2
$self->parent || (return)
l!l
00
MC/DC0%0/1
$self->parent || (return)
$self->parent
Truth table0%0/2
A
A= $self->parent
Aresult
11
00
163 0 my $pos = $parent->children->pos( $self );
164 0 ▶ return( $self->error({
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pos 0 0 0
165 message => "This CharacterData object cannot be found in its parent node.",
166 class => 'HTML::Object::HierarchyRequestError',
167 }) ) if( !defined( $pos ) );
168 0 $parent->children->splice( $pos, 1 );
169 0 $parent->reset(1);
170 0 return( $self );
171 }
172  
173 sub replaceData
174 {
175 0 ▶ my $self = shift( @_ );
Subroutine replaceData: not called CC=9 SCAR=45.0
Pod documented
176 0 my $offset = shift( @_ );
177 0 my $len = shift( @_ );
178 0 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 0 ▶ $str //= '';
Condition0%0/2
$str //= ""
l!l
00
MC/DC0%0/1
$str //= ""
$str
Truth table0%0/2
A
A= $str
Aresult
11
00
182 0 ▶ $offset = "$offset" if( ref( $offset ) && overload::Method( $offset, '""' ) );
Branch: 1 branch
BranchTrueFalseTotal
if ref $offset and overload::Method($offset, "\"\"") 0 0 0
Condition0%0/3
ref $offset and overload::Method($offset, "\"\"")
!ll&&!rl&&r
000
MC/DC0%0/2
ref $offset and overload::Method($offset, "\"\"")
ref $offset overload::Method($offset, "\"\"")
Truth table0%0/3
A and B
A= ref $offsetB= overload::Method($offset, "\"\"")
ABresult
0X0
100
111
183 0 ▶ $len = "$len" if( ref( $len ) && overload::Method( $len, '""' ) );
Branch: 1 branch
BranchTrueFalseTotal
if ref $len and overload::Method($len, "\"\"") 0 0 0
Condition0%0/3
ref $len and overload::Method($len, "\"\"")
!ll&&!rl&&r
000
MC/DC0%0/2
ref $len and overload::Method($len, "\"\"")
ref $len overload::Method($len, "\"\"")
Truth table0%0/3
A and B
A= ref $lenB= overload::Method($len, "\"\"")
ABresult
0X0
100
111
184 0 ▶ return( $self->error({
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_integer($offset) 0 0 0
185 message => "Offset value provided \"$offset\" is not an integer.",
186 class => 'HTML::Object::TypeError',
187 }) ) if( !$self->_is_integer( $offset ) );
188 0 ▶ return( $self->error({
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_integer($len) 0 0 0
189 message => "Length value provided \"$len\" is not an integer.",
190 class => 'HTML::Object::TypeError',
191 }) ) if( !$self->_is_integer( $len ) );
192 0 $offset = int( $offset );
193 0 $len = int( $len );
194 0 $str = "$str";
195 0 ▶ return( $self->error({
Branch: 1 branch
BranchTrueFalseTotal
if $offset > $self->value->length 0 0 0
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 0 $self->value->substr( $offset, $len, $str );
200 0 $self->reset(1);
201 0 return( $self );
202 }
203  
204 sub replaceWith
205 {
206 0 ▶ my $self = shift( @_ );
Subroutine replaceWith: not called CC=4 SCAR=30.0
Pod documented
207 0 ▶ my $parent = $self->parent || return( $self->error({
Condition0%0/2
$self->parent || (return $self->error({'message', 'This CharacterData has no parent.', 'class', 'HTML::Object::HierarchyRequestError'}))
l!l
00
MC/DC0%0/1
$self->parent || (return $self->error({'message', 'This CharacterData has no parent.', 'class', 'HTML::Object::HierarchyRequestError'}))
$self->parent
Truth table0%0/2
A
A= $self->parent
Aresult
11
00
208 message => "This CharacterData has no parent.",
209 class => 'HTML::Object::HierarchyRequestError',
210 }) );
211 0 my $pos = $parent->children->pos( $self );
212 0 my $list = _get_from_list_of_elements_or_html( @_ );
213 0 ▶ return( $self->error({
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pos 0 0 0
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 0 ▶ return( $self ) if( $list->is_empty );
Branch: 1 branch
BranchTrueFalseTotal
if $list->is_empty 0 0 0
219 0 $parent->children->splice( $pos, 1, $list->list );
220 $list->foreach(sub
221 {
222 0 ▶ $_->parent( $parent );
Subroutine __ANON__: not called CC=1 SCAR=6.9
223 0 });
224 0 $parent->reset(1);
225 0 return( $self );
226 }
227  
228 sub substringData
229 {
230 0 ▶ my $self = shift( @_ );
Subroutine substringData: not called CC=8 SCAR=42.8
Pod documented
231 0 my $offset = shift( @_ );
232 0 my $len = shift( @_ );
233 0 ▶ $offset = "$offset" if( ref( $offset ) && overload::Method( $offset, '""' ) );
Branch: 1 branch
BranchTrueFalseTotal
if ref $offset and overload::Method($offset, "\"\"") 0 0 0
Condition0%0/3
ref $offset and overload::Method($offset, "\"\"")
!ll&&!rl&&r
000
MC/DC0%0/2
ref $offset and overload::Method($offset, "\"\"")
ref $offset overload::Method($offset, "\"\"")
Truth table0%0/3
A and B
A= ref $offsetB= overload::Method($offset, "\"\"")
ABresult
0X0
100
111
234 0 ▶ $len = "$len" if( ref( $len ) && overload::Method( $len, '""' ) );
Branch: 1 branch
BranchTrueFalseTotal
if ref $len and overload::Method($len, "\"\"") 0 0 0
Condition0%0/3
ref $len and overload::Method($len, "\"\"")
!ll&&!rl&&r
000
MC/DC0%0/2
ref $len and overload::Method($len, "\"\"")
ref $len overload::Method($len, "\"\"")
Truth table0%0/3
A and B
A= ref $lenB= overload::Method($len, "\"\"")
ABresult
0X0
100
111
235 0 ▶ return( $self->error({
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_integer($offset) 0 0 0
236 message => "Offset value provided \"$offset\" is not an integer.",
237 class => 'HTML::Object::TypeError',
238 }) ) if( !$self->_is_integer( $offset ) );
239 0 ▶ return( $self->error({
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_integer($len) 0 0 0
240 message => "Length value provided \"$len\" is not an integer.",
241 class => 'HTML::Object::TypeError',
242 }) ) if( !$self->_is_integer( $len ) );
243 0 $offset = int( $offset );
244 0 $len = int( $len );
245 0 ▶ return( $self->error({
Branch: 1 branch
BranchTrueFalseTotal
if $offset > $self->value->length 0 0 0
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 0 return( $self->value->substr( $offset, $len ) );
250 }
251  
252 sub _sanity_check_for_before_after
253 {
254 0 ▶ my $self = shift( @_ );
Subroutine _sanity_check_for_before_after: not called CC=15 SCAR=54.8
255 0 ▶ my $list = shift( @_ ) || return( $self->error( "No list of nodes was provided." ) );
Condition0%0/2
shift @_ || (return $self->error('No list of nodes was provided.'))
l!l
00
MC/DC0%0/1
shift @_ || (return $self->error('No list of nodes was provided.'))
shift @_
Truth table0%0/2
A
A= shift @_
Aresult
11
00
256 0 ▶ return( $self->error( "I was expecting an array object, but instead got '$list'." ) ) if( !$self->_is_a( $list => 'Module::Generic::Array' ) );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_a($list, "Module::Generic::Array") 0 0 0
257 0 my $parent = $self->parent;
258 0 my $lineage = $self->lineage;
259 0 ▶ if( !$list->intersect( $lineage )->is_empty )
Branch: 1 branch
BranchTrueFalseTotal
unless ($list->intersect($lineage)->is_empty) 0 0 0
260 {
261 0 return( $self->error({
262 message => "One of the nodes provided is an ancestor of this CharacterData",
263 class => 'HTML::Object::HierarchyRequestError',
264 }) );
265 }
266 0 foreach( @$list )
267 {
268 0 ▶ if( !$self->_is_a( $_ => 'HTML::Object::DOM::DocumentFragment' ) &&
Branch: 2 branches
BranchTrueFalseTotal
if (not $self->_is_a($_, 'HTML::Object::DOM::DocumentFragment') || $self->_is_a($_, 'HTML::Object::DOM::Element') || $self->_is_a($_, 'HTML::Object::DOM::CharacterData')) { } 0 0 0
elsif ($self->_is_a($_, 'HTML::Object::DOM::DocumentFragment')) { } 0 0 0
Condition0%0/6
$self->_is_a($_, 'HTML::Object::DOM::DocumentFragment') || $self->_is_a($_, 'HTML::Object::DOM::Element') || $self->_is_a($_, 'HTML::Object::DOM::CharacterData')
l!l&&r!l&&!r
000
$self->_is_a($_, "HTML::Object::DOM::DocumentFragment") or $self->_is_a($_, "HTML::Object::DOM::Element")
l!l&&r!l&&!r
000
MC/DC0%0/3
$self->_is_a($_, 'HTML::Object::DOM::DocumentFragment') || $self->_is_a($_, 'HTML::Object::DOM::Element') || $self->_is_a($_, 'HTML::Object::DOM::CharacterData')
$self->_is_a($_, "HTML::Object::DOM::DocumentFragment") $self->_is_a($_, "HTML::Object::DOM::Element") $self->_is_a($_, 'HTML::Object::DOM::CharacterData')
Truth table0%0/4
A or B || C
A= $self->_is_a($_, "HTML::Object::DOM::DocumentFragment")B= $self->_is_a($_, "HTML::Object::DOM::Element")C= $self->_is_a($_, 'HTML::Object::DOM::CharacterData')
ABCresult
1XX1
01X1
0011
0000
269 !$self->_is_a( $_ => 'HTML::Object::DOM::Element' ) &&
270 !$self->_is_a( $_ => 'HTML::Object::DOM::CharacterData' ) )
271 {
272 0 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 0 my $kids = $_->children;
280 0 my $count = 0;
281 0 foreach my $node ( @$kids )
282 {
283 0 ▶ ++$count if( $node->isa( 'HTML::Object::DOM::Element' ) );
Branch: 1 branch
BranchTrueFalseTotal
if $node->isa("HTML::Object::DOM::Element") 0 0 0
284 0 ▶ if( $count > 1 || $node->isa( 'HTML::Object::DOM::Text' ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($count > 1 or $node->isa("HTML::Object::DOM::Text")) 0 0 0
Condition0%0/3
$count > 1 or $node->isa("HTML::Object::DOM::Text")
l!l&&r!l&&!r
000
MC/DC0%0/2
$count > 1 or $node->isa("HTML::Object::DOM::Text")
$count > 1 $node->isa("HTML::Object::DOM::Text")
Truth table0%0/3
A or B
A= $count > 1B= $node->isa("HTML::Object::DOM::Text")
ABresult
1X1
011
000
285 {
286 0 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 0 ▶ if( $self->isa( 'HTML::Object::DOM::Text' ) && $self->_is_a( $parent => 'HTML::Object::DOM::Document' ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->isa("HTML::Object::DOM::Text") and $self->_is_a($parent, "HTML::Object::DOM::Document")) 0 0 0
Condition0%0/3
$self->isa("HTML::Object::DOM::Text") and $self->_is_a($parent, "HTML::Object::DOM::Document")
!ll&&!rl&&r
000
MC/DC0%0/2
$self->isa("HTML::Object::DOM::Text") and $self->_is_a($parent, "HTML::Object::DOM::Document")
$self->isa("HTML::Object::DOM::Text") $self->_is_a($parent, "HTML::Object::DOM::Document")
Truth table0%0/3
A and B
A= $self->isa("HTML::Object::DOM::Text")B= $self->_is_a($parent, "HTML::Object::DOM::Document")
ABresult
0X0
100
111
295 {
296 0 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 0 return(1);
302 }
303  
304 33 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
« lib/HTML/Object/Element.pm ↑ Summary blib/lib/HTML/Object/XPath/Expr.pm »