DOM/Attr.pm

filter: Statementstmt 97.7% 133 / 136 Branchbran 90.0% 54 / 60 Conditioncond 78.5% 22 / 28 MC/DCmcdc 44.4% 8 / 18 Subroutinesub 100% 33 / 33 Podpod 80.0% 12 / 15 totaltotal 90.3% 262 / 290 SCAR 37.7
CC
43
Cov
93%
replaceChild
14.2
_modified
7.0
value
7.0
CRAP 43.6
« DOM/Element/Table.pm ↑ Summary DOM/CharacterData.pm »
1 package HTML::DOM::Attr;
2  
3 25 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 # attribute constants (array elems)
6 BEGIN{
7 25 ▶ my $x;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 25 %constants
9 = map +($_=>$x++), qw[
10 _doc _elem _name _val _list _styl
11 ]
12 }
13 25 ▶use constant 1.03 \%constants;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 # after compilation:
15 25 delete @{__PACKAGE__."::"}{ keys %constants, 'constants' };
16  
17 25 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 # The internal fields are:
20 # _doc # owner document
21 # _elem # owner element
22 # _name
23 # _val # actually contains an array with one element, so
24 # _list # node list # that nodelists can work efficiently
25 # _styl # style obj
26  
27  
28 use overload fallback => 1,
29 75 ▶ '""' => sub { shift->value },
Subroutine __ANON__: called CC=1 SCAR=0.0
30 25 ▶ 'bool' => sub{1};
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
31  
32 25 use HTML::DOM::Exception qw'NOT_FOUND_ERR NO_MODIFICATION_ALLOWED_ERR
33 25 ▶ HIERARCHY_REQUEST_ERR ';
Subroutine BEGIN: called CC=1 SCAR=0.0
34 25 ▶use HTML::DOM::Node 'ATTRIBUTE_NODE';
Subroutine BEGIN: called CC=1 SCAR=0.0
35 25 ▶use Scalar::Util qw'weaken blessed refaddr';
Subroutine BEGIN: called CC=1 SCAR=0.0
36  
37 25 require HTML::DOM::EventTarget;
38 25 require HTML::DOM::NodeList;
39  
40 25 our @ISA = 'HTML::DOM::EventTarget';
41  
42 25 our $VERSION = '0.057';
43  
44 # -------- NON-DOM AND PRIVATE METHODS -------- #
45  
46 sub new { # @_[1..2] contains the nayme & vallew
47 # ~~~ INVALID_CHARACTER_ERR is meant to be raised if the specified name contains an invalid character.
48 343 ▶ my @self;
Subroutine new: called CC=2 SCAR=6.9
Pod undocumented
49 343 ▶ @self[_name,_val] = ($_[1],[defined$_[2]?$_[2]:'']);
Branch: 1 branch
BranchTrueFalseTotal
defined $_[2] ? : 299 44 343
50 # value should be an empty
51 343 bless \@self, shift; # string, not undef
52 }
53  
54  
55  
56 sub _set_ownerDocument {
57 364 ▶ weaken ($_[0][_doc] = $_[1]);
Subroutine _set_ownerDocument: called CC=1 SCAR=0.0
58 }
59  
60 sub _element { # This is like ownerElement, except that it lets you set it.
61 389 ▶ if(@_ > 1) {
Subroutine _element: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) 364 25 389
62 364 my $old = $_[0][_elem];
63 364 weaken ($_[0][_elem] = $_[1]);
64 364 return $old
65 }
66 25 $_[0][_elem];
67 }
68  
69 sub DOES {
70 1 ▶ return !0 if $_[1] eq 'HTML::DOM::Node';
Subroutine DOES: called CC=3 SCAR=18.4
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
if $_[1] eq "HTML::DOM::Node" 1 0 1
71 0 ▶ eval { shift->SUPER::DOES(@_) } || !1
Branch: 1 branch
BranchTrueFalseTotal
eval { do { (shift())->SUPER::DOES(@_) } } or 0 0 0
72 }
73  
74 sub _value { # returns the value as it is, whether it is a node or scalar
75 24 ▶ $_[0][_val][0];
Subroutine _value: called CC=1 SCAR=0.0
76 }
77  
78 sub _val_as_node { # turns the attribute's value into a text node if it is
79 # not one already and returns it
80 26 ▶ my $val = $_[0][_val][0];
Subroutine _val_as_node: called CC=4 SCAR=13.9
81 defined blessed $val && $val->isa('HTML::DOM::Text')
82 ? $val
83 26 ▶ : do {
Branch: 1 branch
BranchTrueFalseTotal
defined builtin::blessed($val) && $val->isa('HTML::DOM::Text') ? : 13 13 26
Condition66%2/3
defined builtin::blessed($val) and $val->isa("HTML::DOM::Text")
!ll&&!rl&&r
13013
MC/DC50%1/2
defined builtin::blessed($val) and $val->isa("HTML::DOM::Text")
defined builtin::blessed($val) $val->isa("HTML::DOM::Text")
Truth table66%2/3
A and B
A= defined builtin::blessed($val)B= $val->isa("HTML::DOM::Text")
ABresult
0X0
100
111
84 13 ▶ my $val = $_[0][_val][0] =
Branch: 1 branch
BranchTrueFalseTotal
$_[0][5] ? : 2 11 13
85 $_[0]->ownerDocument->createTextNode(
86 $_[0][_styl] ? $_[0][_styl]->cssText : $val
87 );
88 13 weaken($val->{_parent}=($_[0]));
89 13 $val
90 }
91 }
92  
93 # ~~~ Should I make this public? This actually allows a style object to be
94 # attached to any attr node, not just a style attr. Is this useful?
95 # (Actually, it would be problematic for event attributes, unless some-
96 # one really wants to run css code :-)
97 sub style {
98 39 ▶ my $self = shift;
Subroutine style: called CC=3 SCAR=11.0
Pod undocumented
99 39 ▶ $self->[_styl] ||= do{
Condition66%2/3
$self->[5] ||= do { require CSS::DOM::Style, my $ret = CSS::DOM::Style::parse(my $val = $self->value); $ret->modification_handler(my $cref = sub { if (ref(my $text = $self->_value)) { $text->attr("text", (shift())->cssText); } ; $self->_modified; } ); builtin::weaken($self); my $css_code = $ret->cssText; if ($val ne $css_code) { &$cref($ret); } ; $ret }
l!l&&r!l&&!r
12270
MC/DC0%0/2
$self->[5] ||= do { require CSS::DOM::Style, my $ret = CSS::DOM::Style::parse(my $val = $self->value); $ret->modification_handler(my $cref = sub { if (ref(my $text = $self->_value)) { $text->attr("text", (shift())->cssText); } ; $self->_modified; } ); builtin::weaken($self); my $css_code = $ret->cssText; if ($val ne $css_code) { &$cref($ret); } ; $ret }
$self->[5] do { require CSS::DOM::Style, my $ret = CSS::DOM::Style::parse(my $val = $self->value); $ret->modification_handler(my $cref = sub { if (ref(my $text = $self->_value)) { $text->attr("text", (shift())->cssText); } ; $self->_modified; } ); builtin::weaken($self); my $css_code = $ret->cssText; if ($val ne $css_code) { &$cref($ret); } ; $ret }
Truth table66%2/3
A ||= B
A= $self->[5]B= do { require CSS::DOM::Style, my $ret = CSS::DOM::Style::parse(my $val = $self->value); $ret->modification_handler(my $cref = sub { if (ref(my $text = $self->_value)) { $text->attr("text", (shift())->cssText); } ; $self->_modified; } ); builtin::weaken($self); my $css_code = $ret->cssText; if ($val ne $css_code) { &$cref($ret); } ; $ret }
ABresult
1X1
011
000
100 require CSS::DOM::Style,
101 27 my $ret = CSS::DOM::Style::parse(my $val = $self->value);
102 $ret->modification_handler(my $cref = sub {
103 13 ▶ if(ref(my $text = $self->_value)) {
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if (ref(my $text = $self->_value)) 1 12 13
104 # We can’t use ->data here because it will
105 # trigger chardatamodified (see sub new),
106 # which sets cssText, which calls this.
107 1 $text->attr('text', shift->cssText)
108 }
109 13 $self->_modified;
110 27 });
111 27 weaken $self;
112 27 my $css_code = $ret->cssText;
113 27 ▶ if($val ne $css_code) { &$cref($ret) }
Branch: 1 branch
BranchTrueFalseTotal
if ($val ne $css_code) 5 22 27
114 27 $ret;
115 };
116 }
117  
118 sub _modified {
119 276 ▶ my $self = shift;
Subroutine _modified: called CC=7 SCAR=19.5
120 276 my ($old_val,$new) = @_;
121 276 ▶ my $element = $self->[_elem] || return;
Condition100%2/2
$self->[1] || (return)
l!l
26412
MC/DC100%1/1
$self->[1] || (return)
$self->[1]
Truth table100%2/2
A
A= $self->[1]
Aresult
11
00
122 264 ▶ defined $new or $new = value $self;
Branch: 1 branch
BranchTrueFalseTotal
defined $new or $new = $self->value 13 251 264
123 264 ▶ if ($self->[_name] =~ /^on(.*)/is
Branch: 1 branch
BranchTrueFalseTotal
if ($self->[2] =~ /^on(.*)/is and my $listener_maker = $self->ownerDocument->event_attr_handler) 2 262 264
Condition100%3/3
$self->[2] =~ /^on(.*)/is and my $listener_maker = $self->ownerDocument->event_attr_handler
!ll&&!rl&&r
26112
MC/DC0%0/2
$self->[2] =~ /^on(.*)/is and my $listener_maker = $self->ownerDocument->event_attr_handler
$self->[2] =~ /^on(.*)/is my $listener_maker = $self->ownerDocument->event_attr_handler
Truth table33%1/3
A and B
A= $self->[2] =~ /^on(.*)/isB= my $listener_maker = $self->ownerDocument->event_attr_handler
ABresult
0X0
100
111
124 and my $listener_maker = $self->ownerDocument
125 ->event_attr_handler
126 ) {
127 2 my $eavesdropper = &$listener_maker(
128 $element, my $evt_name = lc $1, $new
129 );
130 2 ▶ defined $eavesdropper
Branch: 1 branch
BranchTrueFalseTotal
defined $eavesdropper and $element->event_handler($evt_name, $eavesdropper) 2 0 2
131 and $element->event_handler(
132 $evt_name, $eavesdropper
133 );
134 }
135  
136 $element->trigger_event(
137 264 ▶ DOMAttrModified =>
Branch: 1 branch
BranchTrueFalseTotal
defined $old_val ? : 251 13 264
138 attr_name => $self->[_name],
139 attr_change_type => 1,
140 prev_value => defined $old_val?$old_val:$new,
141 new_value => $new,
142 rel_node => $self,
143 )
144 }
145  
146 sub _text_node_modified {
147 6 ▶ my $self = shift;
Subroutine _text_node_modified: called CC=2 SCAR=6.9
148 6 ▶ if($$self[_styl]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->[5]) { } 1 5 6
149 1 $$self[_styl]->cssText(shift->newValue)
150 }
151 else {
152 5 $self->_modified($_[0]->prevValue,$_[0]->newValue);
153 }
154 }
155  
156  
157 # ----------- ATTR-ONLY METHODS ---------- #
158  
159 sub name {
160 564 ▶ $_[0][_name];
Subroutine name: called CC=1 SCAR=0.0
Pod documented
161 }
162  
163 sub value {
164 938 ▶ if(my $style = $_[0][_styl]) {
Subroutine value: called CC=7 SCAR=19.5
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if (my $style = $_[0][5]) 22 916 938
165 22 shift;
166 22 return $style->cssText(@_);
167 }
168 916 ▶ if(@_ > 1){
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) 255 661 916
169 255 my $old = $_[0][_val][0];
170 255 ▶ if(ref $old) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $old) { } 1 254 255
elsif ((my $new_val = $_[0][3][0] = "$_[1]") ne $old) { } 252 2 254
171 1 $old = $old->data;
172 1 $_[0][_val][0]->data($_[1]);
173 # ~~~ Can we combine these two statements by using data’s retval?
174 }
175 elsif((my $new_val = $_[0][_val][0] = "$_[1]") ne $old) {
176 252 ▶ if($_[0]->get_event_listeners(
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0]->get_event_listeners('DOMCharacterDataModified')) { } 1 251 252
177 'DOMCharacterDataModified'
178 )) {
179 1 $_[0]->firstChild->trigger_event(
180 'DOMCharacterDataModified',
181 prev_value => $old,
182 new_value => $new_val
183 )
184 }
185 else {
186 251 $_[0]->_modified($old,$new_val);
187 }
188 }
189 255 return $old;
190 }
191 661 my $val = $_[0][_val][0];
192 661 ▶ ref $val ? $val->data : $val;
Branch: 1 branch
BranchTrueFalseTotal
ref $val ? : 19 642 661
193 }
194  
195 sub specified {
196 24 ▶ my $attr=shift;
Subroutine specified: called CC=2 SCAR=6.9
Pod documented
197 24 ▶ ($$attr[_elem]||return 1)->_attr_specified($$attr[_name]);
Condition100%2/2
$attr->[1] || (return 1)
l!l
213
MC/DC100%1/1
$attr->[1] || (return 1)
$attr->[1]
Truth table100%2/2
A
A= $attr->[1]
Aresult
11
00
198 }
199  
200 sub ownerElement { # ~~~ If the attr is detached, is _element currently
201 # erased as it should be?
202 shift->_element || ()
203 6 ▶}
Subroutine ownerElement: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
(shift())->_element or () 4 2 6
204  
205 # ------------------ NODE METHODS ------------ #
206  
207 25 *nodeName = \&name;
208 25 *nodeValue = \&value;
209 25 *nodeType =\&ATTRIBUTE_NODE;
210  
211 # These all return null
212 *previousSibling = *nextSibling = *attributes = *parentNode = *prefix =
213 *namespaceURI = *localName = *normalize
214 25 ▶ = sub {};
Subroutine __ANON__: called CC=1 SCAR=0.0
215  
216 sub childNodes {
217 12 ▶ wantarray ? $_[0]->_val_as_node :(
Subroutine childNodes: called CC=3 SCAR=11.1
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 6 6 12
Condition66%2/3
$_[0][4] ||= "HTML::DOM::NodeList"->new($_[0][3])
l!l&&r!l&&!r
240
MC/DC0%0/2
$_[0][4] ||= "HTML::DOM::NodeList"->new($_[0][3])
$_[0][4] "HTML::DOM::NodeList"->new($_[0][3])
Truth table66%2/3
A ||= B
A= $_[0][4]B= "HTML::DOM::NodeList"->new($_[0][3])
ABresult
1X1
011
000
218 $_[0]->_val_as_node,
219 $_[0][_list] ||= HTML::DOM::NodeList->new($_[0][_val])
220 );
221 }
222  
223 25 *firstChild = *lastChild = \&_val_as_node;
224  
225 34 ▶sub ownerDocument { $_[0][_doc] }
Subroutine ownerDocument: called CC=1 SCAR=0.0
Pod documented
226  
227 sub insertBefore {
228 3 ▶ die HTML::DOM::Exception->new(NO_MODIFICATION_ALLOWED_ERR,
Subroutine insertBefore: called CC=1 SCAR=0.0
Pod documented
229 'The list of child nodes of an attribute cannot be modified');
230 }
231  
232 sub replaceChild {
233 11 ▶ my($self,$new_node,$old_node) = @_;
Subroutine replaceChild: called CC=14 SCAR=26.5
Pod documented
234 11 my $val = $self->_value;
235 11 ▶ die HTML::DOM::Exception->new(NOT_FOUND_ERR,
Branch: 1 branch
BranchTrueFalseTotal
if not ref $val or $old_node != $val 1 10 11
Condition66%2/3
not ref $val or $old_node != $val
l!l&&r!l&&!r
0110
MC/DC50%1/2
not ref $val or $old_node != $val
not ref $val $old_node != $val
Truth table66%2/3
not(A) or B
A= not ref $valB= $old_node != $val
ABresult
1X1
011
000
236 'The node passed to replaceChild is not a child of this attribute')
237 if !ref $val || $old_node != $val;
238 10 ▶ if(defined blessed $new_node and
Branch: 1 branch
BranchTrueFalseTotal
if (defined builtin::blessed($new_node) and $new_node->isa("HTML::DOM::DocumentFragment")) 3 7 10
Condition66%2/3
defined builtin::blessed($new_node) and $new_node->isa("HTML::DOM::DocumentFragment")
!ll&&!rl&&r
073
MC/DC50%1/2
defined builtin::blessed($new_node) and $new_node->isa("HTML::DOM::DocumentFragment")
defined builtin::blessed($new_node) $new_node->isa("HTML::DOM::DocumentFragment")
Truth table66%2/3
A and B
A= defined builtin::blessed($new_node)B= $new_node->isa("HTML::DOM::DocumentFragment")
ABresult
0X0
100
111
239 isa $new_node 'HTML::DOM::DocumentFragment') {
240 3 ▶ (($new_node) = $new_node->childNodes) != 1 and
Branch: 1 branch
BranchTrueFalseTotal
(($new_node) = $new_node->childNodes) != 1 and die "HTML::DOM::Exception"->new(3, "The document fragment passed to replaceChild does not have exactly one child node") 1 2 3
241 die HTML::DOM::Exception->new(HIERARCHY_REQUEST_ERR,
242 'The document fragment passed to replaceChild ' .
243 'does not have exactly one child node');
244 }
245 9 ▶ die HTML::DOM::Exception->new(HIERARCHY_REQUEST_ERR,
Branch: 1 branch
BranchTrueFalseTotal
unless defined builtin::blessed($new_node) and $new_node->isa("HTML::DOM::Text") 2 7 9
Condition66%2/3
defined builtin::blessed($new_node) and $new_node->isa("HTML::DOM::Text")
!ll&&!rl&&r
027
MC/DC50%1/2
defined builtin::blessed($new_node) and $new_node->isa("HTML::DOM::Text")
defined builtin::blessed($new_node) $new_node->isa("HTML::DOM::Text")
Truth table66%2/3
A and B
A= defined builtin::blessed($new_node)B= $new_node->isa("HTML::DOM::Text")
ABresult
0X0
100
111
246 'The node passed to replaceChild is not a text node')
247 if !defined blessed $new_node ||
248 !$new_node->isa('HTML::DOM::Text');
249  
250 7 $old_node->trigger_event('DOMNodeRemoved',
251 rel_node => $self);
252 7 ▶ my $in_doc = $self->[_elem] && $self->[_elem]->is_inside(
Condition100%3/3
$self->[1] && $self->[1]->is_inside($self->[0])
!ll&&!rl&&r
151
MC/DC100%2/2
$self->[1] && $self->[1]->is_inside($self->[0])
$self->[1] $self->[1]->is_inside($self->[0])
Truth table100%3/3
A && B
A= $self->[1]B= $self->[1]->is_inside($self->[0])
ABresult
0X0
100
111
253 $self->[_doc]
254 );
255 7 ▶ if($in_doc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($in_doc) 1 6 7
256 1 $old_node->trigger_event('DOMNodeRemovedFromDocument')
257 }
258 7 my $old_parent = $new_node->parent;
259 7 ▶ $old_parent and $new_node->trigger_event('DOMNodeRemoved',
Branch: 1 branch
BranchTrueFalseTotal
$old_parent and $new_node->trigger_event("DOMNodeRemoved", "rel_node", $old_parent) 2 5 7
260 rel_node => $old_parent);
261 7 ▶ if($new_node->is_inside($self->[_doc])){
Branch: 1 branch
BranchTrueFalseTotal
if ($new_node->is_inside($self->[0])) { } 0 7 7
262 0 $new_node->trigger_event('DOMNodeRemovedFromDocument')
263 }
264 else {
265 # Even if it’s already the same document, it’s actually
266 # quicker just to set it than to check first.
267 7 $new_node->_set_ownerDocument( $self->[_doc] );
268 }
269  
270 7 ($_[0][_val][0] = $new_node)->detach;
271 7 weaken($new_node->{_parent}=($self));
272 7 $old_node->parent(undef);
273  
274 7 $new_node->trigger_event('DOMNodeInserted', rel_node => $self);
275 7 ▶ if($in_doc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($in_doc) 1 6 7
276 1 $new_node->trigger_event('DOMNodeInsertedIntoDocument')
277 }
278 $_->trigger_event('DOMSubtreeModified')
279 7 for grep defined, $old_parent, $self;
280 7 $self->_modified($old_node->data, $new_node->data);
281  
282 7 $old_node;
283 }
284  
285  
286 25 *removeChild = *appendChild = \&insertBefore;
287  
288 1 ▶sub hasChildNodes { 1 }
Subroutine hasChildNodes: called CC=1 SCAR=0.0
Pod documented
289  
290 sub cloneNode {
291 # ~~~ The spec. is not clear as to what should be done with an
292 # Attr’s child node when it is cloned shallowly. I’m here fol-
293 # lowing the behaviour of Safari and Firefox, which both ignore
294 # the ‘deep’ option.
295 4 ▶ my($self,$deep) = @_;
Subroutine cloneNode: called CC=2 SCAR=6.9
Pod documented
296 4 my $clone = bless [@$self], ref $self;
297 4 weaken $$clone[_doc];
298 4 delete $$clone[$_] for _elem, _list;
299 4 $$clone[_val] = ["$$clone[_val][0]"]; # copy the single-elem array
300 # that ->[_val] contains,
301 # flattening it in order effec-
302 # tively to clone it.
303 4 $clone;
304 }
305  
306 1 ▶sub hasAttributes { !1 }
Subroutine hasAttributes: called CC=1 SCAR=0.0
Pod documented
307  
308 sub isSupported {
309 2 ▶ my $self = shift;
Subroutine isSupported: called CC=2 SCAR=7.1
Pod documented
310 2 ▶ return !1 if $_[0] =~ /events\z/i;
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] =~ /events\z/i 0 2 2
311 2 $HTML::DOM::Implementation::it->hasFeature(@_)
312 }
313  
314  
315 25 1
316  
317 __END__
« DOM/Element/Table.pm ↑ Summary DOM/CharacterData.pm »