TUWF/XML.pm

filter: Statementstmt 83.3% 105 / 126 Branchbran 54.8% 34 / 62 Conditioncond 34.6% 9 / 26 MC/DCmcdc 13.3% 2 / 15 Subroutinesub 71.4% 15 / 21 Podpod 91.6% 11 / 12 totaltotal 67.1% 176 / 262 CC 45 SCAR 28.8
CC
45
Cov
69%
html
72.0
tag
21.5
txt
12.0
CRAP 17.9
« TUWF/Misc.pm ↑ Summary TUWF/Validate.pm »
1 #!/usr/bin/perl
2  
3  
4 package TUWF::XML;
5  
6  
7 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Carp 'carp', 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12  
13 1 our $VERSION = '1.6';
14 1 our(@EXPORT_OK, %EXPORT_TAGS, $OBJ);
15  
16 # List::Util provides a uniq() since 1.45, but for some reason my Perl comes
17 # with an even more ancient version.
18 1 ▶sub uniq { my %h = map +($_,1), @_; keys %h }
Subroutine uniq: called CC=1 SCAR=0.0
Pod undocumented
19  
20  
21 1 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=6.9
22 1 my @htmltags = qw|
23 a abbr acronym address area b base bdo big blockquote body br button caption
24 cite code col colgroup dd del dfn div dl dt em fieldset form h1 h2 h3 h4 h5 h6
25 head i img input ins kbd label legend li Link Map meta noscript object ol
26 optgroup option p param pre Q samp script Select small span strong style Sub
27 sup table tbody td textarea tfoot th thead title Tr tt ul var
28 |;
29 1 my @html5tags = qw|
30 a abbr address area article aside audio b base bb bdo blockquote body br
31 button canvas caption cite code col colgroup command datagrid datalist dd
32 del details dfn dialog div dl dt em embed fieldset figure footer form h1 h2
33 h3 h4 h5 h6 head header hr i iframe img input ins kbd label legend li Link
34 main Map mark menu meta meter nav noscript object ol optgroup option output p
35 param pre progress Q rp rt ruby samp script section Select small source
36 span strong style Sub summary sup table tbody td textarea tfoot th thead
37 Time title Tr ul var video
38 |;
39 1 my @Htmltags = map ucfirst, @htmltags;
40 1 my @Html5tags = map ucfirst, @html5tags;
41 1 my @html_tags = map lc($_).'_', @htmltags;
42 1 my @html5_tags = map lc($_).'_', @html5tags;
43 1 my @all = uniq @htmltags, @html5tags, @Htmltags, @Html5tags, @html_tags, @html5_tags;
44  
45 # boolean/empty/self-closing tags
46 1 my %htmlbool = map +($_,1), qw{
47 area base br col command embed hr img input link meta param source
48 };
49  
50 # create the subroutines to map to the html tags
51 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
52 1 for my $e (@all) {
53 311 (my $le = lc $e) =~ s/_$//;
54 311 ▶ *{__PACKAGE__."::$e"} = sub {
Subroutine __ANON__: called CC=4 SCAR=14.2
55 11 ▶ my $s = ref($_[0]) eq __PACKAGE__ ? shift : $OBJ;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq __PACKAGE__ ? : 0 11 11
56 11 ▶ $s->tag($le, @_, $htmlbool{$le} && $#_%2 ? undef : ());
Branch: 1 branch
BranchTrueFalseTotal
$htmlbool{$le} && $#_ % 2 ? : 1 10 11
Condition66%2/3
$htmlbool{$le} and $#_ % 2
!ll&&!rl&&r
1001
MC/DC50%1/2
$htmlbool{$le} and $#_ % 2
$htmlbool{$le} $#_ % 2
Truth table66%2/3
A and B
A= $htmlbool{$le}B= $#_ % 2
ABresult
0X0
100
111
57 }
58 311 }
59  
60 # functions to export
61 1 @EXPORT_OK = (@all, qw(
62 xml mkclass xml_escape html_escape xml_string
63 tag html lit txt end
64 Tag Html Lit Txt End
65 tag_ html_ lit_ txt_ end_
66 ));
67 1 %EXPORT_TAGS = (
68 html => [ @htmltags, qw(tag html lit txt end ) ],
69 html5 => [ @html5tags, qw(tag html lit txt end ) ],
70 Html => [ @Htmltags, qw(Tag Html Lit Txt End ) ],
71 Html5 => [ @Html5tags, qw(Tag Html Lit Txt End ) ],
72 html_ => [ @html_tags, qw(tag_ html_ lit_ txt_ end_) ],
73 html5_=> [ @html5_tags, qw(tag_ html_ lit_ txt_ end_) ],
74 xml => [ qw(xml tag lit txt end) ],
75 );
76 };
77  
78  
79 # the common (X)HTML doctypes, from http://www.w3.org/QA/2002/04/valid-dtd-list.html
80 1 my %doctypes = split /\r?\n/, <<__;
81 xhtml1-strict
82 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
83 xhtml1-transitional
84 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
85 xhtml1-frameset
86 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">
87 xhtml11
88 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
89 xhtml-basic11
90 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML Basic 1.1//EN" "http://www.w3.org/TR/xhtml-basic/xhtml-basic11.dtd">
91 xhtml-math-svg
92 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1 plus MathML 2.0 plus SVG 1.1//EN" "http://www.w3.org/2002/04/xhtml-math-svg/xhtml-math-svg.dtd">
93 html5
94 <!DOCTYPE html>
95 __
96  
97  
98 1 ▶sub new {
Subroutine new: called CC=3 SCAR=12.3
Pod documented
99 1 my($pack, %o) = @_;
100 0 ▶ $o{write} ||= sub { print @_ };
Subroutine __ANON__: not called CC=1 SCAR=2.0
Condition33%1/3
$o{"write"} ||= sub { print @_; }
l!l&&r!l&&!r
100
MC/DC0%0/2
$o{"write"} ||= sub { print @_; }
$o{"write"} sub { print @_; }
Truth table33%1/3
A ||= B
A= $o{"write"}B= sub { print @_; }
ABresult
1X1
011
000
101 1 my $self = bless {
102 %o,
103 nesting => 0,
104 stack => [],
105 }, $pack;
106 1 ▶ $OBJ = $self if $o{default};
Branch: 1 branch
BranchTrueFalseTotal
if $o{"default"} 0 1 1
107 1 return $self;
108 };
109  
110  
111 # Convenient function to generate a dynamic class attribute.
112 0 ▶sub mkclass {
Subroutine mkclass: not called CC=2 SCAR=17.9
Pod documented
113 0 my %c = @_;
114 0 my $c = join ' ', grep $c{$_}, keys %c;
115 0 ▶ return $c ? (class => $c) : ();
Branch: 1 branch
BranchTrueFalseTotal
$c ? : 0 0 0
116 }
117  
118  
119 # XML escape (not a method)
120 1 my %XML = qw/& &amp; < &lt; " &quot;/;
121 14 ▶sub xml_escape {
Subroutine xml_escape: called CC=2 SCAR=7.9
Pod documented
122 14 local $_ = $_[0];
123 14 ▶ if(!defined $_) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $_) 0 14 14
124 0 carp "Attempting to XML-escape an undefined value";
125 0 return '';
126 }
127 14 s/([&<"])/$XML{$1}/g;
128 14 $_;
129 }
130  
131 # HTML escape, also does \n to <br /> conversion
132 # (not a method)
133 0 ▶sub html_escape {
Subroutine html_escape: not called CC=1 SCAR=6.9
Pod documented
134 0 local $_ = xml_escape shift;
135 0 s/\r?\n/<br \/>/g;
136 0 return $_;
137 }
138  
139 # Evaluate a function and return XML as a string
140 1 ▶sub xml_string {
Subroutine xml_string: called CC=1 SCAR=0.0
Pod documented
141 1 my $f = pop;
142 1 my $buf = '';
143 1 ▶ local $OBJ = TUWF::XML->new(@_, write => sub { $buf .= shift });
Subroutine __ANON__: called CC=1 SCAR=0.0
144 1 $f->();
145 1 $buf
146 }
147  
148  
149 # output literal data (not HTML escaped)
150 14 ▶sub lit {
Subroutine lit: called CC=3 SCAR=11.4
Pod documented
151 14 ▶ my $s = ref($_[0]) eq __PACKAGE__ ? shift : $OBJ;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq __PACKAGE__ ? : 14 0 14
152 14 $s->{write}->($_) for @_;
153 }
154  
155 1 *Lit = \&lit;
156 1 *lit_ = \&lit;
157  
158  
159 # output text (HTML escaped)
160 0 ▶sub txt {
Subroutine txt: not called CC=3 SCAR=24.8
Pod documented
161 0 ▶ my $s = ref($_[0]) eq __PACKAGE__ ? shift : $OBJ;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq __PACKAGE__ ? : 0 0 0
162 0 $s->lit(xml_escape $_) for @_;
163 }
164  
165 1 *Txt = \&txt;
166 1 *txt_ = \&txt;
167  
168  
169 # Output any XML or HTML tag.
170 # Arguments Output
171 # 'tagname' <tagname>
172 # 'tagname', id => "main" <tagname id="main">
173 # 'tagname', '<bar>' <tagname>&lt;bar&gt;</tagname>
174 # 'tagname', sub { .. } <tagname>..</tagname>
175 # 'tagname', class => undef <tagname>
176 # 'tagname', '+a' => 1, '+a' => 2 <tagname a="1 2">
177 # 'tagname', a => 1, '+' => 2 <tagname a="1 2">
178 # 'tagname', id => 'main', '<bar>' <tagname id="main">&lt;bar&gt;</tagname>
179 # 'tagname', id => 'main', sub { .. } <tagname id="main">..</tagname>
180 # 'tagname', id => 'main', undef <tagname id="main" />
181 # 'tagname', undef <tagname />
182 11 ▶sub tag {
Subroutine tag: called CC=20 SCAR=30.7
Pod documented
183 11 ▶ my $s = ref($_[0]) eq __PACKAGE__ ? shift : $OBJ;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq __PACKAGE__ ? : 11 0 11
184 11 my $name = shift;
185 11 ▶ croak "Invalid XML tag name" if !$name || $name =~ /^[^a-z]/i || $name =~ / /;
Branch: 1 branch
BranchTrueFalseTotal
if not $name or $name =~ /^[^a-z]/i or $name =~ / / 0 11 11
Condition33%2/6
not $name or $name =~ /^[^a-z]/i or $name =~ / /
l!l&&r!l&&!r
0011
not $name or $name =~ /^[^a-z]/i
l!l&&r!l&&!r
0011
MC/DC0%0/3
not $name or $name =~ /^[^a-z]/i or $name =~ / /
not $name $name =~ /^[^a-z]/i $name =~ / /
Truth table25%1/4
not(A) or B or C
A= not $nameB= $name =~ /^[^a-z]/iC= $name =~ / /
ABCresult
1XX1
01X1
0011
0000
186  
187 11 ▶ my $indent = $s->{pretty} ? "\n".(' 'x($s->{nesting}*$s->{pretty})) : '';
Branch: 1 branch
BranchTrueFalseTotal
$s->{'pretty'} ? : 11 0 11
188 11 my $t = $indent.'<'.$name;
189 11 my %concat;
190 my $last;
191 11 ▶ while(@_ > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) 28 11 39
192 28 my $attr = shift;
193 28 my $val = shift;
194 28 ▶ croak "Invalid XML attribute name" if $attr =~ /[\s'"&<>=]/; # Not comprehensive, just enough to prevent XSS-by-fucking-up-XML-structure
Branch: 1 branch
BranchTrueFalseTotal
if $attr =~ /[\s'"&<>=]/ 0 28 28
195 28 ▶ if($attr =~ /^\+(.*)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attr =~ /^\+(.*)/) { } 15 13 28
196 15 ▶ $last = $1 if $1;
Branch: 1 branch
BranchTrueFalseTotal
if $1 3 12 15
197 15 ▶ $concat{$last} .= (length $concat{$last} ? ' ' : '') . $val if defined $val;
Branch: 2 branches
BranchTrueFalseTotal
if defined $val 9 6 15
length $concat{$last} ? : 5 4 9
198 } else {
199 13 ▶ if (defined $val) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $val) 8 5 13
200 8 ▶ if (@_ && defined $_[0] && $_[0] eq '+') {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ and defined $_[0] and $_[0] eq '+') { } 4 4 8
Condition50%3/6
@_ and defined $_[0] and $_[0] eq "+"
!ll&&!rl&&r
044
@_ and defined $_[0]
!ll&&!rl&&r
008
MC/DC33%1/3
@_ and defined $_[0] and $_[0] eq "+"
@_ defined $_[0] $_[0] eq "+"
Truth table50%2/4
A and B and C
A= @_B= defined $_[0]C= $_[0] eq "+"
ABCresult
0XX0
10X0
1100
1111
201 4 $concat{$attr} = $val;
202 } else {
203 4 $t .= qq{ $attr="}.xml_escape($val).'"';
204 }
205 }
206 13 $last = $attr;
207 }
208 }
209 11 $t .= qq{ $_="}.xml_escape($concat{$_}).'"' for sort keys %concat;
210  
211 11 ▶ if(!@_) {
Branch: 3 branches
BranchTrueFalseTotal
if (not @_) { } 1 10 11
elsif (not defined $_[0]) { } 7 3 10
elsif (ref $_[0] eq 'CODE') { } 1 2 3
212 1 $s->lit($t.'>');
213 1 push @{$s->{stack}}, $name;
214 1 $s->{nesting}++;
215 } elsif(!defined $_[0]) {
216 7 $s->lit($t.' />');
217 } elsif(ref $_[0] eq 'CODE') {
218 1 $s->lit($t.'>');
219 1 local $s->{nesting} = $s->{nesting}+1;
220 1 local $s->{stack} = []; # Call the sub with an empty stack, there's nothing to end() now.
221 1 $_[0]->();
222 1 $s->lit($indent.'</'.$name.'>');
223 } else {
224 2 $s->lit($t.'>'.xml_escape(shift).'</'.$name.'>');
225 }
226 }
227  
228 1 *Tag = \&tag;
229 1 *tag_ = \&tag;
230  
231  
232 # Ends the last opened tag
233 1 ▶sub end {
Subroutine end: called CC=6 SCAR=19.4
Pod documented
234 1 ▶ my $s = ref($_[0]) eq __PACKAGE__ ? shift : $OBJ;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq __PACKAGE__ ? : 0 1 1
235 1 my $w = shift;
236 1 my $l = pop @{$s->{stack}};
237 1 $s->{nesting}--;
238 1 ▶ croak "No more tags to close" if !$l;
Branch: 1 branch
BranchTrueFalseTotal
unless $l 0 1 1
239 1 ▶ croak "Specified tag to end ($w) is not equal to the last opened tag ($l)" if $w && $w ne $l;
Branch: 1 branch
BranchTrueFalseTotal
if $w and $w ne $l 0 1 1
Condition33%1/3
$w and $w ne $l
!ll&&!rl&&r
100
MC/DC0%0/2
$w and $w ne $l
$w $w ne $l
Truth table33%1/3
A and B
A= $wB= $w ne $l
ABresult
0X0
100
111
240 1 ▶ $s->lit("\n".(' 'x($s->{nesting}*$s->{pretty}))) if $s->{pretty};
Branch: 1 branch
BranchTrueFalseTotal
if $s->{"pretty"} 1 0 1
241 1 $s->lit('</'.$l.'>');
242 }
243  
244 1 *End = \&end;
245 1 *end_ = \&end;
246  
247  
248 0 ▶sub html {
Subroutine html: not called CC=8 SCAR=42.8
Pod documented
249 0 ▶ my $s = ref($_[0]) eq __PACKAGE__ ? shift : $OBJ;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq __PACKAGE__ ? : 0 0 0
250 0 my $hascontent = @_ % 2 == 1;
251 0 ▶ my $c = $hascontent && pop;
Condition0%0/3
$hascontent && pop()
!ll&&!rl&&r
000
MC/DC0%0/2
$hascontent && pop()
$hascontent pop()
Truth table0%0/3
A && B
A= $hascontentB= pop()
ABresult
0X0
100
111
252 0 my %o = @_;
253  
254 0 ▶ my $doctype = delete $o{doctype} || 'html5';
Condition0%0/2
delete $o{'doctype'} || 'html5'
l!l
00
MC/DC0%0/1
delete $o{'doctype'} || 'html5'
delete $o{'doctype'}
Truth table0%0/2
A
A= delete $o{'doctype'}
Aresult
11
00
255  
256 0 $s->lit($doctypes{$doctype}."\n");
257 0 my $lang = delete $o{lang};
258 0 ▶ $s->tag('html',
Branch: 4 branches
BranchTrueFalseTotal
$doctype eq 'html5' ? : 0 0 0
$lang ? : 0 0 0
$lang ? : 0 0 0
$hascontent ? : 0 0 0
259 # html5 has no 'xmlns' or 'xml:lang'
260 $doctype eq 'html5' ? (
261 $lang ? (lang => $lang) : (),
262 ) : (
263 xmlns => 'http://www.w3.org/1999/xhtml',
264 $lang ? ('xml:lang' => $lang, lang => $lang) : (),
265 ),
266 %o,
267 $hascontent ? ($c) : ()
268 );
269 }
270  
271 1 *Html = \&html;
272 1 *html_ = \&html;
273  
274  
275 # Writes an xml header, doesn't open an <xml> tag, and doesn't need an
276 # end() either.
277 0 ▶sub xml() {
Subroutine xml: not called CC=2 SCAR=17.9
Pod documented
278 0 ▶ my $s = ref($_[0]) eq __PACKAGE__ ? shift : $OBJ;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq __PACKAGE__ ? : 0 0 0
279 0 $s->lit(qq|<?xml version="1.0" encoding="UTF-8"?>|);
280 }
281  
282  
283 1 1;
284  
« TUWF/Misc.pm ↑ Summary TUWF/Validate.pm »