SVG/DOM.pm

filter: Statementstmt 59.1% 159 / 269 Branchbran 37.9% 44 / 116 Conditioncond 7.1% 2 / 28 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 60.0% 24 / 40 Podpod 0.0% 0 / 37 totaltotal 45.5% 229 / 503 CC 79 SCAR 34.4
CC
79
Cov
50%
cloneNode
210.0
getAttributes
20.0
getElements
18.0
CRAP 31.2
« SVG/Element.pm ↑ Summary SVG.pm »
1 package SVG::DOM;
2 30 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 30 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 30 ▶use Scalar::Util qw/weaken/;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 30 our $VERSION = '2.89';
8  
9 # this module extends SVG::Element
10 package SVG::Element;
11  
12 #-----------------
13 # sub getFirstChild
14  
15 1 ▶sub getFirstChild {
Subroutine getFirstChild: called CC=2 SCAR=7.6
Pod undocumented
16 1 my $self = shift;
17  
18 1 ▶ if ( my @children = $self->getChildren ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@children) = $self->getChildren) 1 0 1
19 1 return $children[0];
20 }
21 0 return;
22 }
23  
24 #-----------------
25 # sub getChildIndex
26 # return the array index of this element in the parent
27 # or the passed list (if there is one).
28  
29 3 ▶sub getChildIndex {
Subroutine getChildIndex: called CC=5 SCAR=20.9
Pod undocumented
30 3 my ( $self, @children ) = @_;
31  
32 3 ▶ unless (@children) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@children) 0 3 3
33 0 my $parent = $self->getParent();
34 0 @children = $parent->getChildren();
35 0 ▶ return unless @children;
Branch: 1 branch
BranchTrueFalseTotal
unless @children 0 0 0
36 }
37  
38 3 for my $index ( 0 .. $#children ) {
39 5 ▶ return $index if $children[$index] == $self;
Branch: 1 branch
BranchTrueFalseTotal
if $children[$index] == $self 3 2 5
40 }
41  
42 0 return;
43 }
44  
45 #-----------------
46 # sub getChildAtIndex
47 # return the element at the specified index
48 # (the index can be negative)
49  
50 0 ▶sub getChildAtIndex {
Subroutine getChildAtIndex: not called CC=3 SCAR=24.8
Pod undocumented
51 0 my ( $self, $index, @children ) = @_;
52  
53 0 ▶ unless (@children) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@children) 0 0 0
54 0 my $parent = $self->getParent();
55 0 @children = $parent->getChildren();
56 0 ▶ return unless @children;
Branch: 1 branch
BranchTrueFalseTotal
unless @children 0 0 0
57 }
58  
59 0 return $children[$index];
60 }
61  
62 #-----------------
63 # sub getNextSibling
64  
65 2 ▶sub getNextSibling {
Subroutine getNextSibling: called CC=4 SCAR=15.5
Pod undocumented
66 2 my $self = shift;
67  
68 2 ▶ if ( my $parent = $self->getParent ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $parent = $self->getParent) 2 0 2
69 2 my @children = $parent->getChildren();
70 2 my $index = $self->getChildIndex(@children);
71 2 ▶ if ( defined $index and scalar(@children) > $index ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $index and scalar @children > $index) 2 0 2
Condition33%1/3
defined $index and scalar @children > $index
!ll&&!rl&&r
002
MC/DC0%0/2
defined $index and scalar @children > $index
defined $index scalar @children > $index
Truth table33%1/3
A and B
A= defined $indexB= scalar @children > $index
ABresult
0X0
100
111
72 2 return $children[ $index + 1 ];
73 }
74 }
75  
76 0 return;
77 }
78  
79 #-----------------
80 # sub getPreviousSibling
81  
82 1 ▶sub getPreviousSibling {
Subroutine getPreviousSibling: called CC=3 SCAR=11.6
Pod undocumented
83 1 my $self = shift;
84  
85 1 ▶ if ( my $parent = $self->getParent ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $parent = $self->getParent) 1 0 1
86 1 my @children = $parent->getChildren();
87 1 my $index = $self->getChildIndex(@children);
88 1 ▶ if ($index) {
Branch: 1 branch
BranchTrueFalseTotal
if ($index) 1 0 1
89 1 return $children[ $index - 1 ];
90 }
91 }
92  
93 0 return;
94 }
95  
96 #-----------------
97 # sub getLastChild
98  
99 1 ▶sub getLastChild {
Subroutine getLastChild: called CC=2 SCAR=7.6
Pod undocumented
100 1 my $self = shift;
101  
102 1 ▶ if ( my @children = $self->getChildren ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@children) = $self->getChildren) 1 0 1
103 1 return $children[-1];
104 }
105  
106 0 return;
107 }
108  
109 #-----------------
110 # sub getChildren
111  
112 7 ▶sub getChildren {
Subroutine getChildren: called CC=3 SCAR=11.2
Pod undocumented
113 7 my $self = shift;
114  
115 7 ▶ if ( $self->{-childs} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"-childs"}) 7 0 7
116 7 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) 6 1 7
117 6 return @{ $self->{-childs} };
118 }
119 1 return $self->{-childs};
120 }
121  
122 0 return;
123 }
124 30 *getChildElements = \&getChildren;
125 30 *getChildNodes = \&getChildren;
126  
127 #-----------------
128  
129 1 ▶sub hasChildren {
Subroutine hasChildren: called CC=3 SCAR=11.8
Pod undocumented
130 1 my $self = shift;
131  
132 1 ▶ if ( exists $self->{-childs} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"-childs"}) 1 0 1
133 1 ▶ if ( scalar @{ $self->{-childs} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @{$self->{"-childs"};}) 1 0 1
134 1 return 1;
135 }
136 }
137  
138 0 return 0;
139 }
140 30 *hasChildElements = \&hasChildren;
141 30 *hasChildNodes = \&hasChildren;
142  
143 #-----------------
144 # sub getParent / getParentElement
145 # return the ref of the parent of the current node
146  
147 8 ▶sub getParent {
Subroutine getParent: called CC=2 SCAR=7.6
Pod undocumented
148 8 my $self = shift;
149  
150 8 ▶ if ( $self->{-parent} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"-parent"}) 8 0 8
151 8 return $self->{-parent};
152 }
153  
154 0 return;
155 }
156 30 *getParentElement = \&getParent;
157 30 *getParentNode = \&getParent;
158  
159 #-----------------
160 # sub getParents / getParentElements
161  
162 0 ▶sub getParents {
Subroutine getParents: not called CC=3 SCAR=24.8
Pod undocumented
163 0 my $self = shift;
164  
165 0 my $parent = $self->{-parent};
166 0 ▶ return unless $parent;
Branch: 1 branch
BranchTrueFalseTotal
unless $parent 0 0 0
167  
168 0 my @parents;
169 0 ▶ while ($parent) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parent) 0 0 0
170 0 push @parents, $parent;
171 0 $parent = $parent->{-parent};
172 }
173  
174 0 return @parents;
175 }
176 30 *getParentElements = \&getParents;
177 30 *getParentNodes = \&getParents;
178 30 *getAncestors = \&getParents;
179  
180 #-----------------
181 # sub isAncestor
182  
183 0 ▶sub isAncestor {
Subroutine isAncestor: not called CC=3 SCAR=24.8
Pod undocumented
184 0 my ( $self, $descendant ) = @_;
185  
186 0 my @parents = $descendant->getParents();
187 0 foreach my $parent (@parents) {
188 0 ▶ return 1 if $parent == $self;
Branch: 1 branch
BranchTrueFalseTotal
if $parent == $self 0 0 0
189 }
190  
191 0 return 0;
192 }
193  
194 #-----------------
195 # sub isDescendant
196  
197 0 ▶sub isDescendant {
Subroutine isDescendant: not called CC=3 SCAR=24.8
Pod undocumented
198 0 my ( $self, $ancestor ) = @_;
199  
200 0 my @parents = $self->getParents();
201 0 foreach my $parent (@parents) {
202 0 ▶ return 1 if $parent == $ancestor;
Branch: 1 branch
BranchTrueFalseTotal
if $parent == $ancestor 0 0 0
203 }
204  
205 0 return 0;
206 }
207  
208 #-----------------
209 # sub getSiblings
210  
211 0 ▶sub getSiblings {
Subroutine getSiblings: not called CC=2 SCAR=17.9
Pod undocumented
212 0 my $self = shift;
213  
214 0 ▶ if ( my $parent = $self->getParent ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $parent = $self->getParent) 0 0 0
215 0 return $parent->getChildren();
216 }
217  
218 0 return;
219 }
220  
221 #-----------------
222 # sub hasSiblings
223  
224 1 ▶sub hasSiblings {
Subroutine hasSiblings: called CC=3 SCAR=12.0
Pod undocumented
225 1 my $self = shift;
226  
227 1 ▶ if ( my $parent = $self->getParent ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $parent = $self->getParent) 1 0 1
228 1 my $siblings = scalar( $parent->getChildren );
229 1 ▶ return 1 if $siblings >= 2;
Branch: 1 branch
BranchTrueFalseTotal
if $siblings >= 2 1 0 1
230 }
231  
232 0 return;
233 }
234  
235 #-----------------
236 # sub getElementName / getType
237  
238 1 ▶sub getElementName {
Subroutine getElementName: called CC=2 SCAR=7.6
Pod undocumented
239 1 my $self = shift;
240  
241 1 ▶ if ( exists $self->{-name} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"-name"}) 1 0 1
242 1 return $self->{-name};
243 }
244  
245 0 return;
246 }
247 30 *getType = \&getElementName;
248 30 *getElementType = \&getElementName;
249 30 *getTagName = \&getElementName;
250 30 *getTagType = \&getElementName;
251 30 *getNodeName = \&getElementName;
252 30 *getNodeType = \&getElementName;
253  
254 #-----------------
255 # sub getElements
256 # get all elements of the specified type
257 # if none is specified, get all elements in document.
258  
259 2 ▶sub getElements {
Subroutine getElements: called CC=8 SCAR=28.9
Pod undocumented
260 2 my ( $self, $element ) = @_;
261  
262 2 ▶ return unless exists $self->{-docref};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"-docref"} 0 2 2
263 2 ▶ return unless exists $self->{-docref}->{-elist};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"-docref"}{"-elist"} 0 2 2
264  
265 2 my $elist = $self->{-docref}->{-elist};
266 2 ▶ if ( defined $element ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $element) { } 2 0 2
267 2 ▶ if ( exists $elist->{$element} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $elist->{$element}) 2 0 2
268 0 ▶ return wantarray ? @{ $elist->{$element} } : $elist->{$element};
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 2 2
269 }
270 0 return;
271 }
272 else {
273 0 my @elements;
274 0 foreach my $element_type ( keys %$elist ) {
275 0 push @elements, @{ $elist->{$element_type} };
276 }
277 0 ▶ return wantarray ? @elements : \@elements;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
278 }
279 }
280  
281 # forces the use of the second argument for element name
282 2 ▶sub getElementsByName {
Subroutine getElementsByName: called CC=1 SCAR=0.0
Pod undocumented
283 2 return shift->getElements(shift);
284 }
285 30 *getElementsByType = \&getElementsByName;
286  
287 #-----------------
288 1 ▶sub getElementNames {
Subroutine getElementNames: called CC=2 SCAR=7.0
Pod undocumented
289 1 my $self = shift;
290  
291 1 my @types = keys %{ $self->{-docref}->{-elist} };
292  
293 1 ▶ return wantarray ? @types : \@types;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 1 1
294 }
295 30 *getElementTypes = \&getElementNames;
296  
297 #-----------------
298 # sub getElementID
299  
300 1 ▶sub getElementID {
Subroutine getElementID: called CC=2 SCAR=7.6
Pod undocumented
301 1 my $self = shift;
302  
303 1 ▶ if ( exists $self->{id} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"id"}) 1 0 1
304 1 return $self->{id};
305 }
306  
307 0 return;
308 }
309  
310 #-----------------
311 # sub getElementByID / getElementbyID
312  
313 15 ▶sub getElementByID {
Subroutine getElementByID: called CC=3 SCAR=11.0
Pod undocumented
314 15 my ( $self, $id ) = @_;
315  
316 15 ▶ return unless defined($id);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $id 0 15 15
317 15 my $idlist = $self->{-docref}->{-idlist};
318 15 ▶ if ( exists $idlist->{$id} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $idlist->{$id}) 3 12 15
319 3 return $idlist->{$id};
320 }
321  
322 12 return;
323 }
324 30 *getElementbyID = \&getElementByID;
325  
326 #-----------------
327 # sub getAttribute
328 # see also SVG::attrib()
329  
330 0 ▶sub getAttribute {
Subroutine getAttribute: not called CC=2 SCAR=17.9
Pod undocumented
331 0 my ( $self, $attr ) = @_;
332  
333 0 ▶ if ( exists $self->{$attr} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{$attr}) 0 0 0
334 0 return $self->{$attr};
335 }
336  
337 0 return;
338 }
339  
340 #-----------------
341 # sub getAttributes
342  
343 0 ▶sub getAttributes {
Subroutine getAttributes: not called CC=4 SCAR=30.0
Pod undocumented
344 0 my $self = shift;
345  
346 0 my $out = {};
347 0 foreach my $i ( keys %$self ) {
348 0 ▶ $out->{$i} = $self->{$i} unless $i =~ /^-/;
Branch: 1 branch
BranchTrueFalseTotal
unless $i =~ /^-/ 0 0 0
349 }
350  
351 0 ▶ return wantarray ? %{$out} : $out;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
352 }
353  
354 #-----------------
355 # sub setAttribute
356  
357 0 ▶sub setAttributes {
Subroutine setAttributes: not called CC=2 SCAR=17.9
Pod undocumented
358 0 my ( $self, $attr ) = @_;
359 0 foreach my $i ( keys %$attr ) {
360 0 $self->attrib( $i, $attr->{$i} );
361 }
362 }
363  
364 #-----------------
365 # sub setAttribute
366  
367 0 ▶sub setAttribute {
Subroutine setAttribute: not called CC=1 SCAR=6.9
Pod undocumented
368 0 my ( $self, $att, $val ) = @_;
369 0 $self->attrib( $att, $val );
370 }
371  
372 #-----------------
373 # sub getCDATA / getCdata / getData
374  
375 0 ▶sub getCDATA {
Subroutine getCDATA: not called CC=2 SCAR=17.9
Pod undocumented
376 0 my $self = shift;
377  
378 0 ▶ if ( exists $self->{-cdata} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"-cdata"}) 0 0 0
379 0 return $self->{-cdata};
380 }
381  
382 0 return;
383 }
384 30 *getCdata = \&getCDATA;
385 30 *getData = \&getCDATA;
386  
387 # ----------------
388 # 2005-12-30 - Martin Owens, apply greater DOM specification (write)
389 # http://www.w3.org/TR/1998/REC-DOM-Level-1-19981001/level-one-core.html
390  
391 # ----------------
392 # sub document
393 0 ▶sub document {
Subroutine document: not called CC=1 SCAR=6.9
Pod undocumented
394 0 my ($self) = @_;
395 0 return $self->{-docref};
396 }
397  
398 # DOM specified method names
399 30 *createElement = \&tag;
400 30 *firstChild = \&getFirstChild;
401 30 *lastChild = \&getLastChild;
402 30 *previousSibling = \&getPreviousSibling;
403 30 *nextSibling = \&getNextSibling;
404  
405 # ----------------
406 # sub insertBefore
407 0 ▶sub insertBefore {
Subroutine insertBefore: not called CC=3 SCAR=24.8
Pod undocumented
408 0 my ( $self, $newChild, $refChild ) = @_;
409 0 ▶ return $self->appendElement($newChild) if not $refChild;
Branch: 1 branch
BranchTrueFalseTotal
unless $refChild 0 0 0
410 0 my $index = $self->findChildIndex($refChild);
411 0 ▶ return 0 if $index < 0; # NO_FOUND_ERR
Branch: 1 branch
BranchTrueFalseTotal
if $index < 0 0 0 0
412 0 return $self->insertAtIndex( $newChild, $index );
413 }
414 30 *insertChildBefore = \&insertBefore;
415 30 *insertNodeBefore = \&insertBefore;
416 30 *insertElementBefore = \&insertBefore;
417  
418 # ----------------
419 # sub insertAfter
420 1 ▶sub insertAfter {
Subroutine insertAfter: called CC=3 SCAR=11.3
Pod undocumented
421 1 my ( $self, $newChild, $refChild ) = @_;
422 1 ▶ return $self->appendElement($newChild) if not $refChild;
Branch: 1 branch
BranchTrueFalseTotal
unless $refChild 0 1 1
423 1 my $index = $self->findChildIndex($refChild);
424 1 ▶ return 0 if $index < 0; # NO_FOUND_ERR
Branch: 1 branch
BranchTrueFalseTotal
if $index < 0 0 1 1
425 1 return $self->insertAtIndex( $newChild, $index + 1 );
426 }
427 30 *insertChildAfter = \&insertAfter;
428 30 *insertNodeAfter = \&insertAfter;
429 30 *insertElementAfter = \&insertAfter;
430  
431 # ----------------
432 # sub insertSiblingAfter (Not in W3C DOM)
433 1 ▶sub insertSiblingAfter {
Subroutine insertSiblingAfter: called CC=2 SCAR=8.1
Pod undocumented
434 1 my ( $self, $newChild ) = @_;
435 1 ▶ return $self->getParent->insertAfter( $newChild, $self )
Branch: 1 branch
BranchTrueFalseTotal
if $self->getParent 1 0 1
436 if $self->getParent;
437 0 return 0;
438 }
439  
440 # ----------------
441 # sub insertSiblingBefore (Not in W3C DOM)
442 0 ▶sub insertSiblingBefore {
Subroutine insertSiblingBefore: not called CC=2 SCAR=17.9
Pod undocumented
443 0 my ( $self, $newChild ) = @_;
444 0 ▶ return $self->getParent->insertBefore( $newChild, $self )
Branch: 1 branch
BranchTrueFalseTotal
if $self->getParent 0 0 0
445 if $self->getParent;
446 0 return 0;
447 }
448  
449 # ----------------
450 # sub replaceChild
451 0 ▶sub replaceChild {
Subroutine replaceChild: not called CC=3 SCAR=24.8
Pod undocumented
452 0 my ( $self, $newChild, $oldChild ) = @_;
453  
454 # Replace newChild if it is in this list of children already
455 0 ▶ $self->removeChild($newChild) if $newChild->{-parent} eq $self;
Branch: 1 branch
BranchTrueFalseTotal
if $newChild->{"-parent"} eq $self 0 0 0
456  
457 # We need the index of the node to replace
458 0 my $index = $self->findChildIndex($oldChild);
459 0 ▶ return 0 if ( $index < 0 ); # NOT_FOUND_ERR
Branch: 1 branch
BranchTrueFalseTotal
if $index < 0 0 0 0
460 # Replace and bind new node with its family
461 0 $self->removeChildAtIndex($index);
462 0 $self->insertChildAtIndex($index);
463 0 return $oldChild;
464 }
465 30 *replaceElement = \&replaceChild;
466 30 *replaceNode = \&replaceChild;
467  
468 # ----------------
469 # sub removeChild
470 4 ▶sub removeChild {
Subroutine removeChild: called CC=2 SCAR=6.9
Pod undocumented
471 4 my ( $self, $oldChild ) = @_;
472 4 my $index = $self->findChildIndex($oldChild);
473 4 ▶ return 0 if ( $index < 0 ); # NOT_FOUND_ERR
Branch: 1 branch
BranchTrueFalseTotal
if $index < 0 1 3 4
474 3 return $self->removeChildAtIndex($index);
475 }
476 30 *removeElement = \&removeChild;
477 30 *removeNode = \&removeChild;
478  
479 # ----------------
480 # sub appendChild
481 0 ▶sub appendChild {
Subroutine appendChild: not called CC=3 SCAR=24.8
Pod undocumented
482 0 my ( $self, $element ) = @_;
483 my $index
484 0 ▶ = ( defined $self->{-childs} && scalar @{ $self->{-childs} } ) || 0;
Condition0%0/4
defined $self->{'-childs'} && scalar @{$self->{'-childs'};} || 0
l!l
00
defined $self->{"-childs"} and scalar @{$self->{"-childs"};}
!ll
00
MC/DC0%0/1
defined $self->{'-childs'} && scalar @{$self->{'-childs'};} || 0
defined $self->{"-childs"}
Truth table0%0/2
A
A= defined $self->{"-childs"}
Aresult
11
00
485 0 $self->insertAtIndex( $element, $index );
486 0 return 1;
487 }
488 30 *appendElement = \&appendChild;
489 30 *appendNode = \&appendChild;
490  
491 # ----------------
492 # sub cloneNode
493 0 ▶sub cloneNode {
Subroutine cloneNode: not called CC=14 SCAR=53.5
Pod undocumented
494 0 my ( $self, $deep ) = @_;
495 0 my $clone = new SVG::Element;
496 0 foreach my $key ( keys( %{$self} ) ) {
497 0 ▶ next if $key eq '-childs' or $key eq '-parent';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "-childs" or $key eq "-parent" 0 0 0
Condition0%0/3
$key eq "-childs" or $key eq "-parent"
l!l&&r!l&&!r
000
MC/DC0%0/2
$key eq "-childs" or $key eq "-parent"
$key eq "-childs" $key eq "-parent"
Truth table0%0/3
A or B
A= $key eq "-childs"B= $key eq "-parent"
ABresult
1X1
011
000
498 0 ▶ if ( $key eq '-docref' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq '-docref') { } 0 0 0
499  
500 # need to forge a docref based on the docref of the template element
501 0 foreach my $dockey ( keys( %{ $self->{-docref} } ) ) {
502 next
503 0 ▶ if $dockey eq '-childs'
Branch: 1 branch
BranchTrueFalseTotal
if $dockey eq "-childs" or $dockey eq "-parent" or $dockey eq "-idlist" or $dockey eq "-elist" or $dockey eq "-document" or $dockey eq "-docref" 0 0 0
Condition0%0/15
$dockey eq "-childs" or $dockey eq "-parent" or $dockey eq "-idlist" or $dockey eq "-elist" or $dockey eq "-document" or $dockey eq "-docref"
l!l&&r!l&&!r
000
$dockey eq "-childs" or $dockey eq "-parent" or $dockey eq "-idlist" or $dockey eq "-elist" or $dockey eq "-document"
l!l&&r!l&&!r
000
$dockey eq "-childs" or $dockey eq "-parent" or $dockey eq "-idlist" or $dockey eq "-elist"
l!l&&r!l&&!r
000
$dockey eq "-childs" or $dockey eq "-parent" or $dockey eq "-idlist"
l!l&&r!l&&!r
000
$dockey eq "-childs" or $dockey eq "-parent"
l!l&&r!l&&!r
000
MC/DC0%0/6
$dockey eq "-childs" or $dockey eq "-parent" or $dockey eq "-idlist" or $dockey eq "-elist" or $dockey eq "-document" or $dockey eq "-docref"
$dockey eq "-childs" $dockey eq "-parent" $dockey eq "-idlist" $dockey eq "-elist" $dockey eq "-document" $dockey eq "-docref"
Truth table0%0/7
A or B or C or D or E or F
A= $dockey eq "-childs"B= $dockey eq "-parent"C= $dockey eq "-idlist"D= $dockey eq "-elist"E= $dockey eq "-document"F= $dockey eq "-docref"
ABCDEFresult
1XXXXX1
01XXXX1
001XXX1
0001XX1
00001X1
0000011
0000000
504 or $dockey eq '-parent'
505 or $dockey eq '-idlist'
506 or $dockey eq '-elist'
507 or $dockey eq '-document'
508 or $dockey eq '-docref';
509 0 $clone->{-docref}->{$dockey} = $self->{-docref}->{$dockey};
510 }
511 }
512 else {
513 0 $clone->{$key} = $self->{$key};
514 }
515 }
516  
517 # We need to clone the children if deep is specified.
518 0 ▶ if ($deep) {
Branch: 1 branch
BranchTrueFalseTotal
if ($deep) 0 0 0
519 0 foreach my $child ( @{ $self->{-childs} } ) {
520 0 my $childClone = $child->cloneNode($deep);
521 0 $clone->appendChild($childClone);
522 }
523 }
524  
525 0 return $clone;
526 }
527 30 *cloneElement = \&cloneNode;
528  
529 # ---------------------------------------
530 # NONE DOM Supporting methodss
531  
532 # ----------------
533 # sub findChildIndex
534 8 ▶sub findChildIndex {
Subroutine findChildIndex: called CC=3 SCAR=11.0
Pod undocumented
535 8 my ( $self, $refChild ) = @_;
536  
537 8 my $index = 0;
538 8 foreach my $child ( @{ $self->{-childs} } ) {
539 7 ▶ if ( $child eq $refChild ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($child eq $refChild) 5 2 7
540 5 return $index; # Child found
541 }
542 2 $index++;
543 }
544  
545 3 return -1; # Child not found
546 }
547  
548 # ---------------
549 # sub insertAtIndex
550 2 ▶sub insertAtIndex {
Subroutine insertAtIndex: called CC=4 SCAR=14.0
Pod undocumented
551 2 my ( $self, $newChild, $index ) = @_;
552  
553 # add child
554 2 splice @{ $self->{-childs} }, $index, 0, $newChild;
555  
556 # update parent and document reference
557 2 $newChild->{-docref} = $self->{-docref};
558 2 weaken( $newChild->{-docref} );
559 2 $newChild->{-parent} = $self;
560 2 weaken( $newChild->{-parent} );
561  
562 # update ID and element list
563 2 ▶ if ( defined( $newChild->{id} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $newChild->{"id"}) 0 2 2
564 0 $self->{-docref}->{-idlist}->{ $newChild->{id} } = $newChild;
565 }
566 $self->{-docref}->{-elist} = {}
567 2 ▶ unless ( defined $self->{-docref}->{-elist} );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"-docref"}{"-elist"} 0 2 2
568 $self->{-docref}->{-elist}->{ $newChild->{-name} } = []
569 2 ▶ unless ( defined $self->{-docref}->{-elist}->{ $newChild->{-name} } );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"-docref"}{"-elist"}{$newChild->{"-name"}} 1 1 2
570 2 unshift @{ $self->{-docref}->{-elist}->{ $newChild->{-name} } },
571 $newChild;
572  
573 2 return 1;
574 }
575 30 *insertChildAtIndex = \&insertAtIndex;
576  
577 # ----------------
578 # sub removeChildAtIndex
579 4 ▶sub removeChildAtIndex {
Subroutine removeChildAtIndex: called CC=5 SCAR=16.4
Pod undocumented
580 4 my ( $self, $index ) = @_;
581  
582 # remove child
583 4 my $oldChild = splice @{ $self->{-childs} }, $index, 1;
584 4 ▶ if ( not @{ $self->{-childs} } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@{$self->{"-childs"};}) 2 2 4
585 2 delete $self->{-childs};
586 }
587  
588 # update parent and document reference
589 4 $oldChild->{-docref} = undef;
590 4 $oldChild->{-parent} = undef;
591  
592 # update ID and element list
593 4 ▶ if ( defined( $oldChild->{id} )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $oldChild->{"id"} and exists $self->{"-docref"}{"-idlist"}{$oldChild->{"id"}}) 0 4 4
Condition33%1/3
defined $oldChild->{"id"} and exists $self->{"-docref"}{"-idlist"}{$oldChild->{"id"}}
!ll&&!rl&&r
400
MC/DC0%0/2
defined $oldChild->{"id"} and exists $self->{"-docref"}{"-idlist"}{$oldChild->{"id"}}
defined $oldChild->{"id"} exists $self->{"-docref"}{"-idlist"}{$oldChild->{"id"}}
Truth table33%1/3
A and B
A= defined $oldChild->{"id"}B= exists $self->{"-docref"}{"-idlist"}{$oldChild->{"id"}}
ABresult
0X0
100
111
594 && exists $self->{-docref}->{-idlist}->{ $oldChild->{id} } )
595 {
596 0 delete $self->{-docref}->{-idlist}->{ $oldChild->{id} };
597 }
598 4 ▶ if ( exists $self->{-docref}->{-elist}->{ $oldChild->{-name} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"-docref"}{"-elist"}{$oldChild->{"-name"}}) 2 2 4
599 2 delete $self->{-docref}->{-elist}->{ $oldChild->{-name} };
600 }
601  
602 4 return $oldChild;
603 }
604 30 *removeAtIndex = \&removeChildAtIndex;
605  
606 #-------------------------------------------------------------------------------
607  
608 =pod
609  
610 =head1 NAME
611  
612 SVG::DOM - A library of DOM (Document Object Model) methods for SVG objects.
613  
614 =head1 SUMMARY
615  
616 SVG::DOM provides a selection of methods for accessing and manipulating SVG
617 elements through DOM-like methods such as getElements, getChildren, getNextSibling
618 and so on.
619  
620 =head1 SYNOPSIS
621  
622 my $svg=SVG->new(id=>"svg_dom_synopsis", width=>"100", height=>"100");
623 my %attributes=$svg->getAttributes;
624  
625 my $group=$svg->group(id=>"group_1");
626 my $name=$group->getElementName;
627 my $id=$group->getElementID;
628  
629 $group->circle(id=>"circle_1", cx=>20, cy=>20, r=>5, fill=>"red");
630 my $rect=$group->rect(id=>"rect_1", x=>10, y=>10, width=>20, height=>30);
631 my $width=$rect->getAttribute("width");
632  
633 my $has_children=$group->hasChildren();
634 my @children=$group->getChildren();
635  
636 my $kid=$group->getFirstChild();
637 do {
638 print $kid->xmlify();
639 } while ($kid=$kid->getNextSibling);
640  
641 my @ancestors=$rect->getParents();
642 my $is_ancestor=$group->isAncestor($rect);
643 my $is_descendant=$rect->isDescendant($svg);
644  
645 my @rectangles=$svg->getElements("rect");
646 my $allelements_arrayref=$svg->getElements();
647  
648 $group->insertBefore($newChild,$rect);
649 $group->insertAfter($newChild,$rect);
650 $rect = $group->replaceChild($newChild,$rect);
651 $group->removeChild($newChild);
652 my $newRect = $rect->cloneNode($deep);
653  
654 ...and so on...
655  
656 =head1 METHODS
657  
658 =head2 @elements = $obj->getElements($element_name)
659  
660 Return a list of all elements with the specified name (i.e. type) in the document. If
661 no element name is provided, returns a list of all elements in the document.
662 In scalar context returns an array reference.
663  
664 =head2 @children = $obj->getChildren()
665  
666 Return a list of all children defined on the current node, or undef if there are no children.
667 In scalar context returns an array reference.
668  
669 Alias: getChildElements(), getChildNodes()
670  
671 =head2 @children = $obj->hasChildren()
672  
673 Return 1 if the current node has children, or 0 if there are no children.
674  
675 Alias: hasChildElements, hasChildNodes()
676  
677 =head2 $ref = $obj->getFirstChild()
678  
679 Return the first child element of the current node, or undef if there are no children.
680  
681 =head2 $ref = $obj->getLastChild()
682  
683 Return the last child element of the current node, or undef if there are no children.
684  
685 =head2 $ref = $obj->getSiblings()
686  
687 Return a list of all children defined on the parent node, containing the current node.
688  
689 =head2 $ref = $obj->getNextSibling()
690  
691 Return the next child element of the parent node, or undef if this is the last child.
692  
693 =head2 $ref = $obj->getPreviousSibling()
694  
695 Return the previous child element of the parent node, or undef if this is the first child.
696  
697 =head2 $index = $obj->getChildIndex()
698  
699 Return the place of this element in the parent node's list of children, starting from 0.
700  
701 =head2 $element = $obj->getChildAtIndex($index)
702  
703 Returns the child element at the specified index in the parent node's list of children.
704  
705 =head2 $ref = $obj->getParentElement()
706  
707 Return the parent of the current node.
708  
709 Alias: getParent()
710  
711 =head2 @refs = $obj->getParentElements()
712  
713 Return a list of the parents of the current node, starting from the immediate parent. The
714 last member of the list should be the document element.
715  
716 Alias: getParents()
717  
718 =head2 $name = $obj->getElementName()
719  
720 Return a string containing the name (i.e. the type, not the ID) of an element.
721  
722 Alias: getType(), getTagName(), getNodeName()
723  
724 =head2 $ref = $svg->getElementByID($id)
725  
726 Alias: getElementbyID()
727  
728 Return a reference to the element which has ID $id, or undef if no element with this ID exists.
729  
730 =head2 $id = $obj->getElementID()
731  
732 Return a string containing the ID of the current node, or undef if it has no ID.
733  
734 =head2 $ref = $obj->getAttributes()
735  
736 Return a hash reference of attribute names and values for the current node.
737  
738 =head2 $value = $obj->getAttribute($name);
739  
740 Return the string value attribute value for an attribute of name $name.
741  
742 =head2 $ref = $obj->setAttributes({name1=>$value1,name2=>undef,name3=>$value3})
743  
744 Set a set of attributes. If $value is undef, deletes the attribute.
745  
746 =head2 $value = $obj->setAttribute($name,$value);
747  
748 Set attribute $name to $value. If $value is undef, deletes the attribute.
749  
750 =head2 $cdata = $obj->getCDATA()
751  
752 Return the canonical data (i.e. textual content) of the current node.
753  
754 Alias: getCdata(), getData()
755  
756 =head2 $boolean = $obj->isAncestor($element)
757  
758 Returns 1 if the current node is an ancestor of the specified element, otherwise 0.
759  
760 =head2 $boolean = $obj->isDescendant($element)
761  
762 Returns 1 if the current node is a descendant of the specified element, otherwise 0.
763  
764 =head2 $boolean = $obj->insertBefore( $element, $child );
765  
766 Returns 1 if $element was successfully inserted before $child in $obj
767  
768 =head2 $boolean = $obj->insertAfter( $element, $child );
769  
770 Returns 1 if $element was successfully inserted after $child in $obj
771  
772 =head2 $boolean = $obj->insertSiblingBefore( $element );
773  
774 Returns 1 if $element was successfully inserted before $obj
775  
776 =head2 $boolean = $obj->insertSiblingAfter( $element );
777  
778 Returns 1 if $element was successfully inserted after $obj
779  
780 =head2 $element = $obj->replaceChild( $element, $child );
781  
782 Returns $child if $element successfully replaced $child in $obj
783  
784 =head2 $element = $obj->removeChild( $child );
785  
786 Returns $child if it was removed successfully from $obj
787  
788 =head2 $element = $obj->cloneNode( $deep );
789  
790 Returns a new $element clone of $obj, without parents or children. If deep is set to 1, all children are included recursively.
791  
792 =head1 AUTHOR
793  
794 Ronan Oger, ronan@roitsystems.com
795 Martin Owens, doctormo@postmaster.co.uk
796  
797 =head1 SEE ALSO
798  
799 perl(1), L<SVG>, L<SVG::XML>, L<SVG::Element>, L<SVG::Parser>
800  
801 L<http://www.w3c.org/Graphics/SVG/> SVG at the W3C
802  
803 =cut
804  
805 30 1;
« SVG/Element.pm ↑ Summary SVG.pm »