DOM/CharacterData.pm

filter: Statementstmt 100% 111 / 111 Branchbran 88.3% 53 / 60 Conditioncond 71.4% 15 / 21 MC/DCmcdc 16.6% 2 / 12 Subroutinesub 100% 23 / 23 Podpod 80.0% 12 / 15 totaltotal 89.2% 216 / 242 SCAR 36.5
CC
38
Cov
93%
substringData
6.1
substringData16
6.1
desurrogify
6.0
CRAP 38.4
« DOM/Attr.pm ↑ Summary DOM/Collection.pm »
1 package HTML::DOM::CharacterData;
2  
3 # This contains those methods that are shared both by comments and text
4 # nodes.
5  
6 25 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 25 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 25 ▶use HTML::DOM::Exception qw'INDEX_SIZE_ERR';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 25 ▶use Scalar::Util qw'blessed weaken';
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 25 require HTML::DOM::Node;
13  
14 25 our @ISA = 'HTML::DOM::Node';
15 25 our $VERSION = '0.057';
16  
17  
18 sub surrogify($);
19 sub desurrogify($);
20  
21  
22 # ~comment and ~text pseudo-elements (see HTML::Element) store the
23 # character data in the 'text' attribute.
24 sub data {
25 131 ▶ my $old = (my $self = shift)->attr('text');
Subroutine data: called CC=2 SCAR=6.9
Pod documented
26 131 ▶ if(@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 20 111 131
27 20 $self->attr(text => my $strung = "$_[0]");
28 20 $self->_modified($old,$strung);
29 }
30 $old
31 131 }
32  
33 sub length {
34 1 ▶ length $_[0]->attr('text');
Subroutine length: called CC=1 SCAR=0.0
Pod documented
35 }
36  
37 sub length16 {
38 1 ▶ CORE::length surrogify $_[0]->attr('text');
Subroutine length16: called CC=1 SCAR=0.0
Pod documented
39 }
40  
41 sub substringData { # obj, offset, length
42 # Throwing exceptions in these cases is really dumb, but what can I
43 # do? I'm trying to follow standards.
44 7 ▶ my($self,$off,$len) = @_;
Subroutine substringData: called CC=6 SCAR=18.0
Pod documented
45 7 ▶ die HTML::DOM::Exception->new(INDEX_SIZE_ERR,
Branch: 1 branch
BranchTrueFalseTotal
if $off < 0 2 5 7
46 'substringData cannot take a negative offset')
47 if $off <0;
48 5 ▶ die HTML::DOM::Exception->new(INDEX_SIZE_ERR,
Branch: 1 branch
BranchTrueFalseTotal
if $len and $len < 0 0 5 5
Condition66%2/3
$len and $len < 0
!ll&&!rl&&r
230
MC/DC0%0/2
$len and $len < 0
$len $len < 0
Truth table66%2/3
A and B
A= $lenB= $len < 0
ABresult
0X0
100
111
49 'substringData cannot take a negative substring length')
50 if $len && $len <0;
51 5 my $text = $self->attr('text');
52 5 ▶ die HTML::DOM::Exception->new(INDEX_SIZE_ERR,
Branch: 1 branch
BranchTrueFalseTotal
if $off > CORE::length $text 2 3 5
53 "substringData: $off is greater than the length of the text")
54 if $off > CORE::length $text;
55 3 ▶ defined $len ? substr( $text, $off, $len) : substr $text, $off, ;
Branch: 1 branch
BranchTrueFalseTotal
defined $len ? : 2 1 3
56 }
57  
58 sub substringData16 { # obj, offset, length
59 8 ▶ my($self,$off,$len) = @_;
Subroutine substringData16: called CC=6 SCAR=18.0
Pod documented
60 8 ▶ die HTML::DOM::Exception->new(INDEX_SIZE_ERR,
Branch: 1 branch
BranchTrueFalseTotal
if $off < 0 2 6 8
61 'substringData cannot take a negative offset')
62 if $off <0;
63 6 ▶ die HTML::DOM::Exception->new(INDEX_SIZE_ERR,
Branch: 1 branch
BranchTrueFalseTotal
if $len and $len < 0 0 6 6
Condition66%2/3
$len and $len < 0
!ll&&!rl&&r
240
MC/DC0%0/2
$len and $len < 0
$len $len < 0
Truth table66%2/3
A and B
A= $lenB= $len < 0
ABresult
0X0
100
111
64 'substringData cannot take a negative substring length')
65 if $len && $len<0;
66 6 my $text = surrogify $self->attr('text');
67 6 ▶ die HTML::DOM::Exception->new(INDEX_SIZE_ERR,
Branch: 1 branch
BranchTrueFalseTotal
if $off > CORE::length $text 2 4 6
68 "substringData: $off is greater than the length of the text")
69 if $off > CORE::length $text;
70 4 ▶ desurrogify defined $len
Branch: 1 branch
BranchTrueFalseTotal
&desurrogify(defined $len) ? : 3 1 4
71 ? substr($text, $off, $len)
72 : substr $text, $off, ;
73 }
74  
75 sub appendData {
76 2 ▶ my $old = $_[0]->attr(text => my $new = $_[0]->attr('text').$_[1]);
Subroutine appendData: called CC=1 SCAR=0.0
Pod documented
77 2 $_[0]->_modified($old, $new);
78 return # nothing
79 2 }
80  
81 sub insertData { # obj, offset, string to insert
82 4 ▶ my ($self,$off,$insert) = @_;
Subroutine insertData: called CC=3 SCAR=11.0
Pod documented
83 4 ▶ die HTML::DOM::Exception->new(INDEX_SIZE_ERR,
Branch: 1 branch
BranchTrueFalseTotal
if $off < 0 1 3 4
84 'insertData cannot take a negative offset')
85 if $off <0;
86 3 my $text = $self->attr('text');
87 3 ▶ die HTML::DOM::Exception->new(INDEX_SIZE_ERR,
Branch: 1 branch
BranchTrueFalseTotal
if $off > CORE::length $text 1 2 3
88 "insertData: $off is greater than the length of the text")
89 if $off > CORE::length $text;
90 2 substr($text, $off, 0) = $insert;
91 2 my $old = $self->attr(text => $text);
92 2 $self->_modified($old,$text);
93 return # nothing
94 2 }
95  
96 sub insertData16 { # obj, offset, string to insert
97 4 ▶ my ($self,$off,$insert) = @_;
Subroutine insertData16: called CC=3 SCAR=11.0
Pod documented
98 4 ▶ die HTML::DOM::Exception->new(INDEX_SIZE_ERR,
Branch: 1 branch
BranchTrueFalseTotal
if $off < 0 1 3 4
99 'insertData cannot take a negative offset')
100 if $off <0;
101 3 my $text = surrogify $self->attr('text');
102 3 ▶ die HTML::DOM::Exception->new(INDEX_SIZE_ERR,
Branch: 1 branch
BranchTrueFalseTotal
if $off > CORE::length $text 1 2 3
103 "insertData: $off is greater than the length of the text")
104 if $off > CORE::length $text;
105 2 substr($text, $off, 0) = $insert;
106 2 my $old = $self->attr(text => desurrogify $text);
107 2 $self->_modified($old,$text);
108 return # nothing
109 2 }
110  
111 sub deleteData { # obj, offset, length
112 5 ▶ my ($self,$off,$len) = @_;
Subroutine deleteData: called CC=6 SCAR=18.0
Pod documented
113 5 ▶ die HTML::DOM::Exception->new(INDEX_SIZE_ERR,
Branch: 1 branch
BranchTrueFalseTotal
if $off < 0 1 4 5
114 'deleteData cannot take a negative offset')
115 if $off <0;
116 4 ▶ die HTML::DOM::Exception->new(INDEX_SIZE_ERR,
Branch: 1 branch
BranchTrueFalseTotal
if $len and $len < 0 0 4 4
Condition66%2/3
$len and $len < 0
!ll&&!rl&&r
130
MC/DC0%0/2
$len and $len < 0
$len $len < 0
Truth table66%2/3
A and B
A= $lenB= $len < 0
ABresult
0X0
100
111
117 'deleteData cannot take a negative substring length')
118 if $len && $len <0;
119 4 my $text = $self->attr('text');
120 4 ▶ die HTML::DOM::Exception->new(INDEX_SIZE_ERR,
Branch: 1 branch
BranchTrueFalseTotal
if $off > CORE::length $text 1 3 4
121 "deleteData: $off is greater than the length of the text")
122 if $off > CORE::length $text;
123 25 ▶ no warnings; # Silence nonsensical warnings
Subroutine BEGIN: called CC=1 SCAR=0.0
124 3 ▶ undef(defined $len
Branch: 1 branch
BranchTrueFalseTotal
defined $len ? : 2 1 3
125 ? substr( $text, $off, $len)
126 : substr $text, $off, );
127 3 my $old = $_[0]->attr(text => $text);
128 3 $self->_modified($old,$text);
129 return # nothing
130 3 }
131  
132 sub deleteData16 { # obj, offset, length
133 5 ▶ my ($self,$off,$len) = @_;
Subroutine deleteData16: called CC=6 SCAR=18.0
Pod documented
134 5 ▶ die HTML::DOM::Exception->new(INDEX_SIZE_ERR,
Branch: 1 branch
BranchTrueFalseTotal
if $off < 0 1 4 5
135 'deleteData cannot take a negative offset')
136 if $off <0;
137 4 ▶ die HTML::DOM::Exception->new(INDEX_SIZE_ERR,
Branch: 1 branch
BranchTrueFalseTotal
if $len and $len < 0 0 4 4
Condition66%2/3
$len and $len < 0
!ll&&!rl&&r
130
MC/DC0%0/2
$len and $len < 0
$len $len < 0
Truth table66%2/3
A and B
A= $lenB= $len < 0
ABresult
0X0
100
111
138 'deleteData cannot take a negative substring length')
139 if $len && $len <0;
140 4 my $text = surrogify $self->attr('text');
141 4 ▶ die HTML::DOM::Exception->new(INDEX_SIZE_ERR,
Branch: 1 branch
BranchTrueFalseTotal
if $off > CORE::length $text 1 3 4
142 "deleteData: $off is greater than the length of the text")
143 if $off > CORE::length $text;
144 25 ▶ no warnings; # Silence nonsensical warnings
Subroutine BEGIN: called CC=1 SCAR=0.0
145 3 ▶ undef( defined $len
Branch: 1 branch
BranchTrueFalseTotal
defined $len ? : 2 1 3
146 ? substr( $text, $off, $len)
147 : substr $text, $off, );
148 3 my $old = $self->attr(text => desurrogify $text);
149 3 $self->_modified($old,$text);
150 return # nothing
151 3 }
152  
153 sub replaceData { # obj, offset, length, replacement
154 4 ▶ my ($self,$off,$len,$subst) = @_;
Subroutine replaceData: called CC=4 SCAR=13.9
Pod documented
155 4 ▶ die HTML::DOM::Exception->new(INDEX_SIZE_ERR,
Branch: 1 branch
BranchTrueFalseTotal
if $off < 0 1 3 4
156 'replaceData cannot take a negative offset')
157 if $off <0;
158 3 ▶ die HTML::DOM::Exception->new(INDEX_SIZE_ERR,
Branch: 1 branch
BranchTrueFalseTotal
if $len < 0 0 3 3
159 'replaceData cannot take a negative substring length')
160 if $len <0;
161 3 my $text = $self->attr('text');
162 3 ▶ die HTML::DOM::Exception->new(INDEX_SIZE_ERR,
Branch: 1 branch
BranchTrueFalseTotal
if $off > CORE::length $text 1 2 3
163 "replaceData: $off is greater than the length of the text")
164 if $off > CORE::length $text;
165 2 substr($text, $off, $len) = $subst;
166 2 my $old = $self->attr(text => $text);
167 2 $self->_modified($old,$text);
168 return # nothing
169 2 }
170  
171 sub replaceData16 { # obj, offset, length, replacement
172 4 ▶ my ($self,$off,$len,$subst) = @_;
Subroutine replaceData16: called CC=4 SCAR=13.9
Pod undocumented
173 4 ▶ die HTML::DOM::Exception->new(INDEX_SIZE_ERR,
Branch: 1 branch
BranchTrueFalseTotal
if $off < 0 1 3 4
174 'replaceData cannot take a negative offset')
175 if $off <0;
176 3 ▶ die HTML::DOM::Exception->new(INDEX_SIZE_ERR,
Branch: 1 branch
BranchTrueFalseTotal
if $len < 0 0 3 3
177 'replaceData cannot take a negative substring length')
178 if $len <0;
179 3 my $text = surrogify $self->attr('text');
180 3 ▶ die HTML::DOM::Exception->new(INDEX_SIZE_ERR,
Branch: 1 branch
BranchTrueFalseTotal
if $off > CORE::length $text 1 2 3
181 "replaceData: $off is greater than the length of the text")
182 if $off > CORE::length $text;
183 2 substr($text, $off, $len) = $subst;
184 2 my $old = $self->attr(text => desurrogify $text);
185 2 $self->_modified($old,$text);
186 return # nothing
187 2 }
188  
189 sub _modified {
190 36 ▶ my $self = shift;
Subroutine _modified: called CC=2 SCAR=7.2
191 36 ▶ $_[0] eq $_[1] or $self->trigger_event(
Branch: 1 branch
BranchTrueFalseTotal
$_[0] eq $_[1] or $self->trigger_event("DOMCharacterDataModified", "prev_value", $_[0], "new_value", $_[1]) 36 0 36
192 'DOMCharacterDataModified',
193 prev_value => $_[0],
194 new_value => $_[1],
195 );
196 };
197  
198 #------- UTILITY FUNCTIONS ---------#
199  
200 # ~~~ Should these be exported?
201  
202 sub surrogify($) { # copied straight from JE::String
203 17 ▶ my $ret = shift;
Subroutine surrogify: called CC=1 SCAR=0.0
Pod undocumented
204  
205 25 ▶ no warnings 'utf8';
Subroutine BEGIN: called CC=1 SCAR=0.0
206  
207 17 $ret =~ s<([^\0-\x{ffff}])><
208 13 chr((ord($1) - 0x10000) / 0x400 + 0xD800)
209 . chr((ord($1) - 0x10000) % 0x400 + 0xDC00)
210 >eg;
211 17 $ret;
212 }
213  
214 sub desurrogify($) { # copied straight from JE::String (with length changed
215 # to CORE::length)
216 11 ▶ my $ret = shift;
Subroutine desurrogify: called CC=6 SCAR=18.0
Pod undocumented
217 11 my($ord1, $ord2);
218 11 ▶ for(my $n = 0; $n < CORE::length $ret; ++$n) { # really slow
Branch: 1 branch
BranchTrueFalseTotal
if ($n < CORE::length $ret) 56 11 67
219 56 ▶ ($ord1 = ord substr $ret,$n,1) >= 0xd800 and
Branch: 1 branch
BranchTrueFalseTotal
($ord1 = ord substr($ret, $n, 1)) >= 55296 and $ord1 <= 56319 and ($ord2 = ord substr($ret, $n + 1, 1)) >= 56320 and $ord2 <= 57343 and substr($ret, $n, 2) = chr 65536 + ($ord1 - 55296) * 1024 + ($ord2 - 56320) 3 53 56
Condition77%7/9
($ord1 = ord substr($ret, $n, 1)) >= 55296 and $ord1 <= 56319 and ($ord2 = ord substr($ret, $n + 1, 1)) >= 56320 and $ord2 <= 57343
!ll&&!rl&&r
5303
($ord1 = ord substr($ret, $n, 1)) >= 55296 and $ord1 <= 56319 and ($ord2 = ord substr($ret, $n + 1, 1)) >= 56320
!ll&&!rl&&r
5303
($ord1 = ord substr($ret, $n, 1)) >= 55296 and $ord1 <= 56319
!ll&&!rl&&r
5213
MC/DC50%2/4
($ord1 = ord substr($ret, $n, 1)) >= 55296 and $ord1 <= 56319 and ($ord2 = ord substr($ret, $n + 1, 1)) >= 56320 and $ord2 <= 57343
($ord1 = ord substr($ret, $n, 1)) >= 55296 $ord1 <= 56319 ($ord2 = ord substr($ret, $n + 1, 1)) >= 56320 $ord2 <= 57343
Truth table60%3/5
A and B and C and D
A= ($ord1 = ord substr($ret, $n, 1)) >= 55296B= $ord1 <= 56319C= ($ord2 = ord substr($ret, $n + 1, 1)) >= 56320D= $ord2 <= 57343
ABCDresult
0XXX0
10XX0
110X0
11100
11111
220 $ord1 <= 0xdbff and
221 ($ord2 = ord substr $ret,$n+1,1) >= 0xdc00 and
222 $ord2 <= 0xdfff and
223 substr($ret,$n,2) =
224 chr 0x10000 + ($ord1 - 0xD800) * 0x400 + ($ord2 - 0xDC00);
225 }
226  
227 # In perl 5.8.8, if there is a sub on the call stack that was
228 # triggered by the overloading mechanism when the object with the
229 # overloaded operator was passed as the only argument to 'die',
230 # then the following substitution magically calls that subroutine
231 # again with the same arguments, thereby causing infinite
232 # recursion:
233 #
234 # $ret =~ s/([\x{d800}-\x{dbff}])([\x{dc00}-\x{dfff}])/
235 # chr 0x10000 + (ord($1) - 0xD800) * 0x400 +
236 # (ord($2) - 0xDC00)
237 # /ge;
238 #
239 # 5.9.4 still has this bug.
240 # (fixed in 5.9.5--don't know which patch)
241  
242 11 $ret;
243 }
244  
245 5 ▶sub nodeValue { $_[0]->data(@_[1..$#_]); }
Subroutine nodeValue: called CC=1 SCAR=0.0
Pod documented
246  
247  
248 25 1 __END__ 1
249  
250  
251 =head1 NAME
252  
253 HTML::DOM::CharacterData - A base class shared by HTML::DOM::Text and ::Comment
254  
255 =head1 VERSION
256  
257 Version 0.057
258  
259 =head1 DESCRIPTION
260  
261 This class provides those methods that are shared both by comments and text
262 nodes in an HTML DOM tree.
263  
264 =head1 METHODS
265  
266 =head2 Attributes
267  
268 The following DOM attributes are supported:
269  
270 =over 4
271  
272 =item data
273  
274 The textual data that the node contains.
275  
276 =item length
277  
278 The number of characters in C<data>.
279  
280 =item length16
281  
282 A standards-compliant version of C<length> that counts UTF-16 bytes instead
283 of characters.
284  
285 =back
286  
287 =head2 Other Methods
288  
289 =over 4
290  
291 =item substringData ( $offset, $length )
292  
293 Returns a substring of the data. If C<$length> is omitted, all characters
294 from C<$offset> to the end of the data are returned.
295  
296 =item substringData16
297  
298 A UTF-16 version of C<substringData>.
299  
300 =item appendData ( $str )
301  
302 Appends C<$str> to the node's data.
303  
304 =item insertData ( $offset, $str )
305  
306 Inserts C<$str> at the given C<$offset>, which is understood to be the
307 number of Unicode characters from the beginning of the node's data.
308  
309 =item insertData16
310  
311 Like C<insertData>, but C<$offset> is taken to be the number of UTF-16
312 (16-bit) bytes.
313  
314 =item deleteData ( $offset, $length )
315  
316 Deletes the specified data. If C<$length> is omitted, all characters from
317 C<$offset> to the end of the node's data are deleted.
318  
319 =item deleteData16
320  
321 A UTF-16 version of the above.
322  
323 =item replaceData ( $offset, $length, $str )
324  
325 This replaces the substring specified by C<$offset> and C<$length> with
326 C<$str>.
327  
328 =back
329  
330 =head1 SEE ALSO
331  
332 L<HTML::DOM>
333  
334 L<HTML::DOM::Text>
335  
336 L<HTML::DOM::Comment>
« DOM/Attr.pm ↑ Summary DOM/Collection.pm »