SVG/XML.pm

filter: Statementstmt 96.3% 80 / 83 Branchbran 66.6% 20 / 30 Conditioncond 66.6% 10 / 15 MC/DCmcdc 37.5% 3 / 8 Subroutinesub 100% 16 / 16 Podpod 0.0% 0 / 13 totaltotal 78.1% 129 / 165 SCAR 32.0
CC
23
Cov
86%
dtddecl
9.6
xmldecl
4.3
xmlescp
4.1
CRAP 24.5
« SVG.pm ↑ Summary
1 package SVG::XML;
2 25 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 25 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 25 our $VERSION = '2.87';
6  
7 =pod
8  
9 =head1 NAME
10  
11 SVG::XML - Handle the XML generation bits for SVG.pm
12  
13 =head1 AUTHOR
14  
15 Ronan Oger, cpan@roitsystems.com
16  
17 =head1 SEE ALSO
18  
19 L<SVG>,
20 L<SVG::DOM>,
21 L<SVG::Element>,
22 L<SVG::Parser>,
23 L<SVG::Extension>
24  
25 =cut
26  
27 25 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 25 our @ISA = ('Exporter');
30  
31 25 our @EXPORT = qw(
32 xmlesc
33 xmlescape
34 xmlescp
35 cssstyle
36 xmlattrib
37 xmlcomment
38 xmlpi
39 xmltag
40 xmltagopen
41 xmltagclose
42 xmltag_ln
43 xmltagopen_ln
44 xmltagclose_ln
45 processtag
46 xmldecl
47 dtddecl
48 );
49  
50 sub xmlescp {
51 13 ▶ my ( $self, $s ) = @_;
Subroutine xmlescp: called CC=4 SCAR=14.1
Pod undocumented
52  
53 13 ▶ $s = '0' unless defined $s;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $s 0 13 13
54 13 ▶ $s = join( ', ', @{$s} ) if ( ref($s) eq 'ARRAY' );
Branch: 1 branch
BranchTrueFalseTotal
if ref $s eq "ARRAY" 0 13 13
55  
56 # Special XML entities are escaped
57 13 $s =~ s/&(?!#(x\w\w|\d+?);)/&amp;/g;
58 13 $s =~ s/>/&gt;/g;
59 13 $s =~ s/</&lt;/g;
60 13 $s =~ s/\"/&quot;/g;
61 13 $s =~ s/\'/&apos;/g;
62  
63 # Backtick is just a regular XML citizen
64 #$s=~s/\`/&apos;/g;
65  
66 # Invalid XML characters are removed, not just escaped: \x00-\x08\x0b\x1f
67 # Tabs (\x09) and newlines (\x0a) are valid.
68 13 ▶ while ( $s =~ s/([\x00-\x08\x0b\x1f])/''/e ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s =~ s/([\x00-\x08\x0b\x1f])/"";/e) 10 13 23
69 10 my $char = q{'\\x} . sprintf( '%02X', ord($1) ) . q{'};
70 10 $self->error( $char => 'This forbidden XML character was removed' );
71 }
72  
73 # Per suggestion from Adam Schneider
74 13 $s =~ s/([\200-\377])/'&#'.ord($1).';'/ge;
75  
76 13 return $s;
77 }
78  
79 25 *xmlesc = \&xmlescp;
80  
81 25 *xmlescape = \&xmlescp;
82  
83 sub cssstyle {
84 5 ▶ my %attrs = @_;
Subroutine cssstyle: called CC=1 SCAR=0.0
Pod undocumented
85 5 return ( join( '; ', map { qq($_: ) . $attrs{$_} } sort keys(%attrs) ) );
86 }
87  
88 # Per suggestion from Adam Schneider
89 sub xmlattrib {
90 85 ▶ my %attrs = @_;
Subroutine xmlattrib: called CC=2 SCAR=6.9
Pod undocumented
91 85 ▶ return '' unless ( scalar( keys %attrs ) );
Branch: 1 branch
BranchTrueFalseTotal
unless scalar keys %attrs 24 61 85
92 return (
93 ' '
94 . join( ' ',
95 61 map { qq($_=") . $attrs{$_} . q(") } sort keys(%attrs) )
96 );
97 }
98  
99 sub xmltag {
100 36 ▶ my ( $name, $ns, %attrs ) = @_;
Subroutine xmltag: called CC=3 SCAR=11.0
Pod undocumented
101 36 ▶ $ns = $ns ? "$ns:" : '';
Branch: 1 branch
BranchTrueFalseTotal
$ns ? : 0 36 36
102 36 ▶ my $at = xmlattrib(%attrs) || '';
Condition100%2/2
xmlattrib(%attrs) || ''
l!l
297
MC/DC100%1/1
xmlattrib(%attrs) || ''
xmlattrib(%attrs)
Truth table100%2/2
A
A= xmlattrib(%attrs)
Aresult
11
00
103 36 return qq(<$ns$name$at />);
104 }
105  
106 sub xmltag_ln {
107 36 ▶ my ( $name, $ns, %attrs ) = @_;
Subroutine xmltag_ln: called CC=1 SCAR=0.0
Pod undocumented
108 36 return xmltag( $name, $ns, %attrs );
109 }
110  
111 sub xmltagopen {
112 49 ▶ my ( $name, $ns, %attrs ) = @_;
Subroutine xmltagopen: called CC=3 SCAR=11.0
Pod undocumented
113 49 ▶ $ns = $ns ? "$ns:" : '';
Branch: 1 branch
BranchTrueFalseTotal
$ns ? : 0 49 49
114 49 ▶ my $at = xmlattrib(%attrs) || '';
Condition100%2/2
xmlattrib(%attrs) || ''
l!l
3217
MC/DC100%1/1
xmlattrib(%attrs) || ''
xmlattrib(%attrs)
Truth table100%2/2
A
A= xmlattrib(%attrs)
Aresult
11
00
115 49 return qq(<$ns$name$at>);
116 }
117  
118 sub xmltagopen_ln {
119 49 ▶ my ( $name, $ns, %attrs ) = @_;
Subroutine xmltagopen_ln: called CC=1 SCAR=0.0
Pod undocumented
120 49 return xmltagopen( $name, $ns, %attrs );
121 }
122  
123 sub xmlcomment {
124 22 ▶ my ( $self, $r_comment ) = @_;
Subroutine xmlcomment: called CC=3 SCAR=11.4
Pod undocumented
125 my $ind = $self->{-docref}->{-elsep}
126 22 . $self->{-docref}->{-indent} x $self->{-docref}->{-level};
127  
128 # If the comment starts with newline character then do not prefix
129 # with space (RT #123896).
130 return (
131 $ind . join(
132 $ind,
133 map {
134 22 ▶ ( (/^\n/) ? (q(<!--)) : (q(<!-- )) ) . qq($_)
Branch: 2 branches
BranchTrueFalseTotal
/^\n/ ? : 22 0 22
/\n[\t]?$/ ? : 22 0 22
135 . ( (/\n[\t]?$/) ? (q(-->)) : (q( -->)) );
136 } @$r_comment
137 )
138 );
139 }
140  
141 sub xmlpi {
142 2 ▶ my ( $self, $r_pi ) = @_;
Subroutine xmlpi: called CC=1 SCAR=0.0
Pod undocumented
143 my $ind = $self->{-docref}->{-elsep}
144 2 . $self->{-docref}->{-indent} x $self->{-docref}->{-level};
145 2 return ( join( $ind, map {qq(<?$_?>)} @$r_pi ) );
146 }
147  
148 25 *processinginstruction = \&xmlpi;
149  
150 sub xmltagclose {
151 49 ▶ my ( $name, $ns ) = @_;
Subroutine xmltagclose: called CC=2 SCAR=7.1
Pod undocumented
152 49 ▶ $ns = $ns ? "$ns:" : '';
Branch: 1 branch
BranchTrueFalseTotal
$ns ? : 0 49 49
153 49 return qq(</$ns$name>);
154 }
155  
156 sub xmltagclose_ln {
157 49 ▶ my ( $name, $ns ) = @_;
Subroutine xmltagclose_ln: called CC=1 SCAR=0.0
Pod undocumented
158 49 return xmltagclose( $name, $ns );
159 }
160  
161 sub dtddecl {
162 26 ▶ my $self = shift;
Subroutine dtddecl: called CC=9 SCAR=22.6
Pod undocumented
163 26 ▶ my $docroot = $self->{-docroot} || 'svg';
Condition50%1/2
$self->{'-docroot'} || 'svg'
l!l
260
MC/DC0%0/1
$self->{'-docroot'} || 'svg'
$self->{'-docroot'}
Truth table50%1/2
A
A= $self->{'-docroot'}
Aresult
11
00
164 26 my $id;
165  
166 26 ▶ if ( $self->{-pubid} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'-pubid'}) { } 25 1 26
elsif ($self->{'-sysid'}) { } 1 0 1
167 25 $id = 'PUBLIC "' . $self->{-pubid} . '"';
168 25 ▶ $id .= ' "' . $self->{-sysid} . '"' if ( $self->{-sysid} );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"-sysid"} 25 0 25
169 }
170 elsif ( $self->{-sysid} ) {
171 1 $id = 'SYSTEM "' . $self->{-sysid} . '"';
172 }
173 else {
174 $id
175 = 'PUBLIC "-//W3C//DTD SVG 1.0//EN"'
176 . $self->{-docref}->{-elsep}
177 0 . "\"$self->{-docref}->{-dtd}\"";
178 }
179  
180 26 my $at = join( ' ', ( $docroot, $id ) );
181  
182 #>>>TBD: add internal() method to return this
183 my $extension
184 = ( exists $self->{-internal} )
185 ? $self->{-internal}->render()
186 26 ▶ : q{};
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{'-internal'} ? : 0 26 26
187 26 ▶ if ( exists $self->{-extension} and $self->{-extension} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"-extension"} and $self->{"-extension"}) 1 25 26
Condition66%2/3
exists $self->{"-extension"} and $self->{"-extension"}
!ll&&!rl&&r
0251
MC/DC50%1/2
exists $self->{"-extension"} and $self->{"-extension"}
exists $self->{"-extension"} $self->{"-extension"}
Truth table66%2/3
A and B
A= exists $self->{"-extension"}B= $self->{"-extension"}
ABresult
0X0
100
111
188 $extension
189 .= $self->{-docref}{-elsep}
190 . $self->{-extension}
191 1 . $self->{-docref}{-elsep};
192 }
193 26 ▶ $extension = ' [' . $self->{-docref}{-elsep} . $extension . ']'
Branch: 1 branch
BranchTrueFalseTotal
if $extension 1 25 26
194 if $extension;
195  
196 26 return qq[$self->{-docref}{-elsep}<!DOCTYPE $at$extension>];
197 }
198  
199 sub xmldecl {
200 21 ▶ my $self = shift;
Subroutine xmldecl: called CC=4 SCAR=14.6
Pod undocumented
201  
202 21 ▶ my $version = $self->{-version} || '1.0';
Condition50%1/2
$self->{'-version'} || '1.0'
l!l
210
MC/DC0%0/1
$self->{'-version'} || '1.0'
$self->{'-version'}
Truth table50%1/2
A
A= $self->{'-version'}
Aresult
11
00
203 21 ▶ my $encoding = $self->{-encoding} || 'UTF-8';
Condition50%1/2
$self->{'-encoding'} || 'UTF-8'
l!l
210
MC/DC0%0/1
$self->{'-encoding'} || 'UTF-8'
$self->{'-encoding'}
Truth table50%1/2
A
A= $self->{'-encoding'}
Aresult
11
00
204 21 ▶ my $standalone = $self->{-standalone} || 'yes';
Condition50%1/2
$self->{'-standalone'} || 'yes'
l!l
210
MC/DC0%0/1
$self->{'-standalone'} || 'yes'
$self->{'-standalone'}
Truth table50%1/2
A
A= $self->{'-standalone'}
Aresult
11
00
205  
206 return
207 21 qq{<?xml version="$version" encoding="$encoding" standalone="$standalone"?>};
208 }
209  
210 #-------------------------------------------------------------------------------
211  
212 25 1;
« SVG.pm ↑ Summary