| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 56 | 11 | ▶ | $s->tag($le, @_, $htmlbool{$le} && $#_%2 ? undef : ()); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$htmlbool{$le} and $#_ % 2
MC/DC50%1/2
$htmlbool{$le} and $#_ % 2
$htmlbool{$le}
$#_ % 2
Truth table66%2/3
A and B
A= $htmlbool{$le}B= $#_ % 2
| |||||||||||||||||||||||||||||||||||||||||||
| 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 @_;
}
MC/DC0%0/2
$o{"write"} ||= sub {
print @_;
}
$o{"write"}
sub {
print @_;
}
Truth table33%1/3
A ||= B
A= $o{"write"}B= sub {
print @_;
}
| |||||||||||||||||||||||||||||||||||||||||||
| 101 | 1 | my $self = bless { | |||||||||||||||||||||||||||||||||||||||||
| 102 | %o, | ||||||||||||||||||||||||||||||||||||||||||
| 103 | nesting => 0, | ||||||||||||||||||||||||||||||||||||||||||
| 104 | stack => [], | ||||||||||||||||||||||||||||||||||||||||||
| 105 | }, $pack; | ||||||||||||||||||||||||||||||||||||||||||
| 106 | 1 | ▶ | $OBJ = $self if $o{default}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||||||||||
| 119 | # XML escape (not a method) | ||||||||||||||||||||||||||||||||||||||||||
| 120 | 1 | my %XML = qw/& & < < " "/; | |||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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><bar></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"><bar></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
| |||||||||||||||||||||||||||||||||||||||||||
| 184 | 11 | my $name = shift; | |||||||||||||||||||||||||||||||||||||||||
| 185 | 11 | ▶ | croak "Invalid XML tag name" if !$name || $name =~ /^[^a-z]/i || $name =~ / /; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
not $name or $name =~ /^[^a-z]/i or $name =~ / /
not $name or $name =~ /^[^a-z]/i
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 =~ / /
| |||||||||||||||||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||||||||||||||||
| 187 | 11 | ▶ | my $indent = $s->{pretty} ? "\n".(' 'x($s->{nesting}*$s->{pretty})) : ''; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 188 | 11 | my $t = $indent.'<'.$name; | |||||||||||||||||||||||||||||||||||||||||
| 189 | 11 | my %concat; | |||||||||||||||||||||||||||||||||||||||||
| 190 | my $last; | ||||||||||||||||||||||||||||||||||||||||||
| 191 | 11 | ▶ | while(@_ > 1) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 195 | 28 | ▶ | if($attr =~ /^\+(.*)/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 196 | 15 | ▶ | $last = $1 if $1; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 197 | 15 | ▶ | $concat{$last} .= (length $concat{$last} ? ' ' : '') . $val if defined $val; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 198 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 199 | 13 | ▶ | if (defined $val) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 200 | 8 | ▶ | if (@_ && defined $_[0] && $_[0] eq '+') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%3/6
@_ and defined $_[0] and $_[0] eq "+"
@_ and defined $_[0]
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 "+"
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition33%1/3
$w and $w ne $l
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
| |||||||||||||||||||||||||||||||||||||||||||
| 240 | 1 | ▶ | $s->lit("\n".(' 'x($s->{nesting}*$s->{pretty}))) if $s->{pretty}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 250 | 0 | my $hascontent = @_ % 2 == 1; | |||||||||||||||||||||||||||||||||||||||||
| 251 | 0 | ▶ | my $c = $hascontent && pop; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$hascontent && pop()
MC/DC0%0/2
$hascontent && pop()
$hascontent
pop()
Truth table0%0/3
A && B
A= $hascontentB= pop()
| |||||||||||||||||||||||||||||||||||||||||||
| 252 | 0 | my %o = @_; | |||||||||||||||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||||||||||||||||
| 254 | 0 | ▶ | my $doctype = delete $o{doctype} || 'html5'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
delete $o{'doctype'} || 'html5'
MC/DC0%0/1
delete $o{'doctype'} || 'html5'
delete $o{'doctype'}
Truth table0%0/2
A
A= delete $o{'doctype'}
| |||||||||||||||||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||||||||||||||||
| 256 | 0 | $s->lit($doctypes{$doctype}."\n"); | |||||||||||||||||||||||||||||||||||||||||
| 257 | 0 | my $lang = delete $o{lang}; | |||||||||||||||||||||||||||||||||||||||||
| 258 | 0 | ▶ | $s->tag('html', | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 279 | 0 | $s->lit(qq|<?xml version="1.0" encoding="UTF-8"?>|); | |||||||||||||||||||||||||||||||||||||||||
| 280 | } | ||||||||||||||||||||||||||||||||||||||||||
| 281 | |||||||||||||||||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||||||||||||||||
| 283 | 1 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||||||||||||||||