lib/XML/DOM/Lite/Node.pm

filter: Statementstmt 72.7% 99 / 136 Branchbran 62.1% 41 / 66 Conditioncond 100% 2 / 2 MC/DCmcdc 100% 1 / 1 Subroutinesub 84.6% 22 / 26 Podpod 0.0% 0 / 23 totaltotal 64.9% 165 / 254 SCAR 43.2
CC
37
Cov
70%
cloneNode
12.0
insertBefore
11.8
documentElement
6.0
CRAP 75.4
« blib/lib/XML/DOM/Lite/Parser.pm ↑ Summary blib/lib/XML/DOM/Lite/Serializer.pm »
1 package XML::DOM::Lite::Node;
2  
3 8 ▶use Scalar::Util qw(weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
4 8 ▶use XML::DOM::Lite::NodeList;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 8 ▶use XML::DOM::Lite::Constants qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 sub new {
8 121 ▶ my ($class, $proto) = @_;
Subroutine new: called CC=4 SCAR=13.9
Pod undocumented
9 121 ▶ unless (UNIVERSAL::isa($proto->{childNodes}, 'XML::DOM::Lite::NodeList')) {
Branch: 1 branch
BranchTrueFalseTotal
unless (UNIVERSAL::isa($proto->{"childNodes"}, "XML::DOM::Lite::NodeList")) 121 0 121
10 $proto->{childNodes} = XML::DOM::Lite::NodeList->new(
11 121 ▶ $proto->{childNodes} || [ ]
Condition100%2/2
$proto->{'childNodes'} || []
l!l
12109
MC/DC100%1/1
$proto->{'childNodes'} || []
$proto->{'childNodes'}
Truth table100%2/2
A
A= $proto->{'childNodes'}
Aresult
11
00
12 );
13 }
14 $proto->{attributes} = XML::DOM::Lite::NodeList->new([ ])
15 121 ▶ unless defined $proto->{attributes};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $proto->{"attributes"} 81 40 121
16  
17 121 my $self = bless $proto, $class;
18 121 return $self;
19 }
20  
21 sub childNodes {
22 137 ▶ my $self = shift; $self->{childNodes} = shift if @_;
Subroutine childNodes: called CC=2 SCAR=7.1
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 137 137
23 137 return $self->{childNodes};
24 }
25  
26 sub parentNode {
27 41 ▶ my $self = shift;
Subroutine parentNode: called CC=2 SCAR=6.9
Pod undocumented
28 41 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 1 40 41
29 1 weaken($self->{parentNode} = shift());
30 } else {
31 40 return $self->{parentNode};
32 }
33 }
34  
35 sub documentElement {
36 0 ▶ weaken($_[0]->{documentElement} = $_[1]) if $_[1]; $_[0]->{documentElement};
Subroutine documentElement: not called CC=2 SCAR=17.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
if $_[1] 0 0 0
37 }
38  
39 sub nodeType {
40 153 ▶ my $self = shift; $self->{nodeType} = shift if @_;
Subroutine nodeType: called CC=2 SCAR=7.1
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 153 153
41 153 $self->{nodeType};
42 }
43  
44 sub nodeName {
45 26 ▶ my $self = shift; $self->{nodeName} = shift if @_;
Subroutine nodeName: called CC=2 SCAR=7.1
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 26 26
46 26 $self->{nodeName};
47 }
48  
49 sub tagName {
50 24 ▶ my $self = shift; $self->{tagName} = shift if @_;
Subroutine tagName: called CC=2 SCAR=7.1
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 24 24
51 24 $self->{tagName};
52 }
53  
54 sub appendChild {
55 78 ▶ my ($self, $node) = @_;
Subroutine appendChild: called CC=4 SCAR=13.9
Pod undocumented
56 78 ▶ if ($node->{parentNode}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->{"parentNode"}) 3 75 78
57 3 $node->{parentNode}->removeChild($node);
58 }
59 78 ▶ unless ($node->nodeType == DOCUMENT_FRAGMENT_NODE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->nodeType == 11) { } 1 77 78
60 77 $node->{parentNode} = $self;
61 77 $self->{childNodes}->insertNode($node);
62 } else {
63 1 ▶ while ($node->childNodes->length) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->childNodes->length) 3 1 4
64 3 $self->appendChild($node->firstChild);
65 }
66 }
67  
68 78 return $node;
69 }
70  
71 sub previousSibling {
72 3 ▶ my $self = shift;
Subroutine previousSibling: called CC=3 SCAR=11.0
Pod undocumented
73 3 ▶ if ($self->parentNode) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->parentNode) 3 0 3
74 3 my $index = $self->parentNode->childNodes->nodeIndex($self);
75 3 ▶ return undef if $index == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $index == 0 1 2 3
76 2 return $self->parentNode->childNodes->[$index - 1];
77 }
78 }
79  
80 sub nextSibling {
81 7 ▶ my $self = shift;
Subroutine nextSibling: called CC=3 SCAR=11.2
Pod undocumented
82 7 ▶ if ($self->parentNode) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->parentNode) 7 0 7
83 7 my $index = $self->parentNode->childNodes->nodeIndex($self);
84 7 ▶ return undef if $index == @{$self->childNodes->length} - 1;
Branch: 1 branch
BranchTrueFalseTotal
if $index == @{$self->childNodes->length;} - 1 0 7 7
85 7 return $self->parentNode->childNodes->[$index + 1];
86 }
87 }
88  
89 sub removeChild {
90 4 ▶ my ($self, $node) = @_;
Subroutine removeChild: called CC=2 SCAR=7.4
Pod undocumented
91 4 ▶ if ($node->parentNode == $self) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->parentNode == $self) { } 4 0 4
92 4 undef($node->{parentNode});
93 4 return $self->childNodes->removeNode($node);
94 } else {
95 0 die "$node is not a child of $self";
96 }
97 }
98  
99 sub insertBefore {
100 1 ▶ my ($self, $node, $refNode) = @_;
Subroutine insertBefore: called CC=7 SCAR=24.7
Pod undocumented
101 1 ▶ die "usage error" unless (scalar(@_) == 3);
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @_ == 3 0 1 1
102 1 ▶ if ($node->parentNode) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->parentNode) 0 1 1
103 0 $node->parentNode->removeChild($node);
104 }
105 1 ▶ if ($node->nodeType == DOCUMENT_FRAGMENT_NODE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->nodeType == 11) 0 1 1
106 0 foreach my $c (@{$node->childNodes}) {
107 0 $self->insertBefore($c, $refNode);
108 }
109 0 return;
110 }
111 1 $node->parentNode($self);
112 1 my $index = $self->childNodes->nodeIndex($refNode);
113 1 ▶ if (defined $index) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $index) { } 1 0 1
114 1 ▶ if ($index <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($index <= 0) { } 0 1 1
115 0 $self->childNodes->insertNode($node, 0);
116 } else {
117 1 $self->childNodes->insertNode($node, $index);
118 }
119 } else {
120 0 die "$refNode is not a child of $self";
121 }
122 }
123  
124 sub replaceChild {
125 0 ▶ my ($self, $node, $refNode) = @_;
Subroutine replaceChild: not called CC=2 SCAR=17.9
Pod undocumented
126 0 ▶ die "usage error" unless (scalar(@_) == 3);
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @_ == 3 0 0 0
127 0 $self->insertBefore($refNode, $node);
128 0 $self->removeChild($refNode);
129 }
130  
131 sub nodeValue {
132 18 ▶ my $self = shift; $self->{nodeValue} = shift if @_;
Subroutine nodeValue: called CC=2 SCAR=7.1
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 18 18
133 18 $self->{nodeValue};
134 }
135  
136 sub attributes {
137 23 ▶ my $self = shift; $self->{attributes} = shift if @_;
Subroutine attributes: called CC=2 SCAR=7.1
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 23 23
138 23 return $self->{attributes};
139 }
140  
141 sub getAttribute {
142 66 ▶ my ($self, $attname) = @_;
Subroutine getAttribute: called CC=3 SCAR=11.0
Pod undocumented
143 66 ▶ for (my $x = 0; $x < $self->{attributes}->length; $x++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x < $self->{"attributes"}->length) 63 39 102
144 return $self->{attributes}->[$x]->{nodeValue}
145 63 ▶ if ($self->{attributes}->[$x]->{nodeName} eq $attname);
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"attributes"}[$x]{"nodeName"} eq $attname 27 36 63
146 }
147 39 return undef;
148 }
149  
150 sub setAttribute {
151 1 ▶ my ($self, $attname, $value) = @_;
Subroutine setAttribute: called CC=3 SCAR=14.2
Pod undocumented
152 1 ▶ for (my $x = 0; $x < $self->{attributes}->length; $x++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x < $self->{"attributes"}->length) 0 1 1
153 0 ▶ if ($self->{attributes}->[$x]->{nodeName} eq $attname) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"attributes"}[$x]{"nodeName"} eq $attname) 0 0 0
154 0 $self->{attributes}->[$x]->{nodeValue} = $value;
155 0 return $value;
156 }
157 }
158 1 push @{$self->{attributes}}, XML::DOM::Lite::Node->new({
159 nodeType => ATTRIBUTE_NODE,
160 nodeName => $attname,
161 nodeValue => $value
162 });
163 1 return $value;
164  
165 }
166  
167 sub firstChild {
168 19 ▶ my ($self) = @_;
Subroutine firstChild: called CC=1 SCAR=0.0
Pod undocumented
169 19 return $self->childNodes->item(0);
170 }
171  
172 sub lastChild {
173 7 ▶ my ($self) = @_;
Subroutine lastChild: called CC=1 SCAR=0.0
Pod undocumented
174 7 return $self->childNodes->[$#{$self->childNodes}];
175 }
176  
177 sub ownerDocument {
178 84 ▶ my $self = shift; weaken($self->{ownerDocument} = shift) if @_;
Subroutine ownerDocument: called CC=2 SCAR=6.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
if @_ 76 8 84
179 84 $self->{ownerDocument};
180 }
181  
182 sub getElementsByTagName {
183 1 ▶ my ($self, $tag_name) = @_;
Subroutine getElementsByTagName: called CC=4 SCAR=13.9
Pod undocumented
184 1 my $nlist = XML::DOM::Lite::NodeList->new([ ]);
185 1 my @stack = @{ $self->childNodes };
186 1 ▶ while (my $n = shift(@stack)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $n = shift @stack) 17 1 18
187 17 ▶ if ($n->nodeType == ELEMENT_NODE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n->nodeType == 1) 8 9 17
188 8 ▶ if ($n->tagName eq $tag_name) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n->tagName eq $tag_name) 2 6 8
189 2 $nlist->insertNode($n);
190 }
191 8 push @stack, @{ $n->childNodes };
192 }
193 }
194 1 return $nlist;
195 }
196  
197 sub cloneNode {
198 0 ▶ my ($self, $deep) = @_;
Subroutine cloneNode: not called CC=3 SCAR=24.8
Pod undocumented
199  
200 0 my $copy = { };
201 0 @copy{keys %$self} = values %$self;
202 0 $copy->{childNodes} = XML::DOM::Lite::NodeList->new([ ]);
203 0 $copy->{attributes} = XML::DOM::Lite::NodeList->new([@{$self->attributes}]);
204 0 $copy->{tagName} = $self->tagName;
205 0 $copy->{nodeName} = $self->nodeName;
206 0 $copy->{nodeType} = $self->nodeType;
207 0 $copy->{ownerDocument} = $self->ownerDocument;
208  
209 0 bless $copy, ref($self);
210  
211 0 ▶ if ($deep) {
Branch: 1 branch
BranchTrueFalseTotal
if ($deep) 0 0 0
212 0 foreach (@{$self->childNodes}) {
213 0 $copy->childNodes->insertNode($_->cloneNode($deep));
214 }
215 }
216 0 return $copy;
217 }
218  
219 sub xml {
220 0 ▶ my $self = shift;
Subroutine xml: not called CC=1 SCAR=6.9
Pod undocumented
221 0 require XML::DOM::Lite::Serializer;
222 0 my $serializer = XML::DOM::Lite::Serializer->new();
223 0 return $serializer->serializeToString( $self );
224 }
225  
226  
227 8 1;
228  
« blib/lib/XML/DOM/Lite/Parser.pm ↑ Summary blib/lib/XML/DOM/Lite/Serializer.pm »