lib/PXML/Serialize.pm

filter: Statementstmt 80.2% 175 / 218 Branchbran 54.3% 75 / 138 Conditioncond 52.5% 31 / 59 MC/DCmcdc 24.1% 7 / 29 Subroutinesub 86.1% 31 / 36 Podpod 0.0% 0 / 13 totaltotal 64.7% 319 / 493 CC 93 SCAR 39.7
CC
93
Cov
68%
_pxml_print_fragment_fast
124.6
puthtmlfile
42.0
putxmlfile
20.0
CRAP 53.2
« lib/Chj/IO/File.pm ↑ Summary lib/FP/RegexMatch.pm »
1 #
2 # Copyright (c) 2013-2020 Christian Jaeger, copying@christianjaeger.ch
3 #
4 # This is free software, offered under either the same terms as perl 5
5 # or the terms of the Artistic License version 2 or the terms of the
6 # MIT License (Expat version). See the file COPYING.md that came
7 # bundled with this file.
8 #
9  
10 =head1 NAME
11  
12 PXML::Serialize
13  
14 =head1 SYNOPSIS
15  
16 =head1 DESCRIPTION
17  
18  
19 =head1 SPECIAL VALUES
20  
21 There are some special values that the serializer will evaluate
22 transparently:
23  
24 =over 4
25  
26 =item promises from FP::Lazy
27  
28 are C<force>d
29  
30 =item code references
31  
32 are called with no arguments
33  
34 =item objects
35  
36 In body context, their `pxml_serialized_body_string` method (if
37 available) will be called, in attribute context,
38 `pxml_serialized_attribute_string`, in both cases the string is
39 inserted into the output without escaping (see `PXML::Preserialize`
40 for an example that uses this). Missing those, `string` will be called
41 if available and the result escaped, otherwise an exception is thrown.
42  
43 =back
44  
45 =head1 NOTE
46  
47 This is alpha software! Read the status section in the package README
48 or on the L<website|http://functional-perl.org/>.
49  
50 =cut
51  
52 package PXML::Serialize;
53 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
54 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
55 7 ▶use warnings FATAL => 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
56 7 ▶use Exporter "import";
Subroutine BEGIN: called CC=1 SCAR=0.0
57  
58 7 our @EXPORT = qw(pxml_xhtml_print);
59 7 our @EXPORT_OK = qw(pxml_print
60 pxml_print_fragment
61 pxml_xhtml_print_fast
62 pxml_print_fragment_fast
63 putxmlfile
64 puthtmlfile
65 attribute_escape
66 content_escape
67 );
68 7 our %EXPORT_TAGS = (all => [@EXPORT, @EXPORT_OK]);
69  
70 7 ▶use FP::Show;
Subroutine BEGIN: called CC=1 SCAR=0.0
71 7 ▶use PXML::Element;
Subroutine BEGIN: called CC=1 SCAR=0.0
72 7 ▶use PXML qw(is_pxml_element is_pxmlflush);
Subroutine BEGIN: called CC=1 SCAR=0.0
73 7 ▶use FP::Lazy;
Subroutine BEGIN: called CC=1 SCAR=0.0
74 7 ▶use FP::List;
Subroutine BEGIN: called CC=1 SCAR=0.0
75 7 ▶use FP::Stream;
Subroutine BEGIN: called CC=1 SCAR=0.0
76 7 ▶use Chj::xperlfunc qw(xprint xprintln);
Subroutine BEGIN: called CC=1 SCAR=0.0
77 7 ▶use FP::Weak 'weaken'; # instead of from Scalar::Util so that it can
Subroutine BEGIN: called CC=1 SCAR=0.0
78 # be turned off globally (and we depend on FP
79 # anyway)
80 7 ▶use Scalar::Util qw(blessed refaddr);
Subroutine BEGIN: called CC=1 SCAR=0.0
81 7 ▶use FP::Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
82  
83 667 ▶sub is_somearray {
Subroutine is_somearray: called CC=3 SCAR=11.1
Pod undocumented
84 667 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 667 0 667
85 667 my $r = ref($_[0]);
86  
87 # XX mess, make this a proper dependency
88 667 ▶ $r eq "ARRAY" or $r eq "PXML::Body"
Branch: 1 branch
BranchTrueFalseTotal
$r eq "ARRAY" or $r eq "PXML::Body" 456 211 667
89 }
90  
91 141 ▶sub is_empty_string {
Subroutine is_empty_string: called CC=4 SCAR=14.3
Pod undocumented
92 141 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 141 0 141
93 141 ▶ defined $_[0] and !length ref $_[0] and $_[0] eq ""
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] and not length ref $_[0] and $_[0] eq "" 75 66 141
Condition66%2/3
defined $_[0] and not length ref $_[0]
!ll&&!rl&&r
06675
MC/DC50%1/2
defined $_[0] and not length ref $_[0]
defined $_[0] not length ref $_[0]
Truth table66%2/3
A and not(B)
A= defined $_[0]B= not length ref $_[0]
ABresult
0X0
100
111
94 }
95  
96 7 my %attribute_escape
97 = ('&' => '&amp;', '<' => '&lt;', '>' => '&gt;', '"' => '&quot;');
98  
99 79 ▶sub attribute_escape {
Subroutine attribute_escape: called CC=2 SCAR=7.0
Pod undocumented
100 79 my ($str) = @_;
101 79 ▶ return "" unless defined $str;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $str 0 79 79
102  
103 # XX or should attributes with undefined value be dropped? (Or,
104 # OTOH, should list processing be done?)
105 79 $str =~ s/([&<>"])/$attribute_escape{$1}/sg;
106 79 $str
107 }
108  
109 7 my %content_escape = ('&' => '&amp;', '<' => '&lt;', '>' => '&gt;');
110  
111 0 ▶sub content_escape {
Subroutine content_escape: not called CC=1 SCAR=6.9
Pod undocumented
112 0 my ($str) = @_;
113 0 $str =~ s/([&<>])/$content_escape{$1}/sg;
114 0 $str
115 }
116  
117 sub pxmlforce;
118  
119 5 ▶sub pxmlforce {
Subroutine pxmlforce: called CC=4 SCAR=15.9
Pod undocumented
120 5 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 5 0 5
121 5 my ($v) = @_;
122 5 ▶ if (my $r = ref $v) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $r = ref $v) { } 5 0 5
123 5 ▶ if ($r eq "CODE") {
Branch: 1 branch
BranchTrueFalseTotal
if ($r eq 'CODE') { } 0 5 5
124 0 pxmlforce(&$r())
125 } else {
126 5 force $v
127 }
128 } else {
129 0 $v
130 }
131 }
132  
133 30 ▶sub object_force_escape {
Subroutine object_force_escape: called CC=5 SCAR=20.2
Pod undocumented
134 30 ▶ @_ == 4 or fp_croak_arity 4;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 4 or fp_croak_arity(4) 30 0 30
135 30 my ($v, $string_method_for_context, $escape, $fh) = @_;
136  
137 # $v is certain to be a reference (XX ehr, not even that?!), but
138 # not necessarily blessed.
139 30 ▶ if (defined blessed $v) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined builtin::blessed($v)) 30 0 30
140 30 ▶ if (my $m = $v->can($string_method_for_context)) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $m = $v->can($string_method_for_context)) { } 30 0 30
elsif ($m = $v->can('string')) { } 0 0 0
141  
142 # no escaping
143 30 return &$m($v, $fh);
144 } elsif (
145 $m =
146  
147 # XX should this instead simply stringify using
148 # '"$v"'? That would not show up errors with
149 # context. But it would be less interruptive
150 # perhaps? Just issue a warning? Ideal would
151 # probably be to do the '""', but give a warning
152 # if it was Perl's default stringification. How to
153 # do this?
154 $v->can("string")
155 )
156 {
157 0 return &$escape(&$m($v));
158 }
159 }
160 0 die "unexpected type of reference that doesn't have a 'string' method: "
161 . (show $v);
162 }
163  
164 # XXX hack, the code is really hopeless, should use ~same code as for
165 # body parts.
166 83 ▶sub _attribute_val_to_string {
Subroutine _attribute_val_to_string: called CC=5 SCAR=16.1
167 83 my ($v, $fh) = @_;
168 83 my $ref = ref $v;
169 83 ▶ if (length($ref)) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $ref) { } 8 75 83
170 8 ▶ if ($ref eq "ARRAY") {
Branch: 2 branches
BranchTrueFalseTotal
if ($ref eq 'ARRAY') { } 2 6 8
elsif (is_pxmlflush($v)) { } 1 5 6
171 2 join("", map { _attribute_val_to_string($_, $fh) } @$v)
172 } elsif (is_pxmlflush($v)) {
173 1 ▶ flush $fh or die $!;
Branch: 1 branch
BranchTrueFalseTotal
$fh->flush or die $! 1 0 1
174 1 ""
175 } else {
176 5 object_force_escape(pxmlforce($v),
177 "pxml_serialized_attribute_string",
178 \&attribute_escape, $fh)
179 }
180 } else {
181  
182 # fast path:
183 75 attribute_escape $v
184 }
185 }
186  
187 824 ▶sub _pxml_print_fragment_fast {
Subroutine _pxml_print_fragment_fast: called CC=49 SCAR=48.3
188 824 ▶ @_ == 4 or fp_croak_arity 4;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 4 or fp_croak_arity(4) 824 0 824
189 824 my ($v, $fh, $html5compat, $void_element_h) = @_;
190 824 ▶ weaken $_[0]
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] 601 223 824
191  
192 # necessary since we're also called with strings:
193 if ref $_[0];
194 LP: {
195 ## **NOTE**: this has seen some evil optimizations; before
196 ## working on the code, please undo them first by using git
197 ## revert.
198 824 ▶ if (my $ref = ref $v) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $ref = ref $v) { } 865 225 1090
elsif (not defined $v) { } 2 223 225
199 865 ▶ if (defined(my $class = blessed $v)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $class = builtin::blessed($v))) { } 615 250 865
200 615 ▶ if (
Branch: 3 branches
BranchTrueFalseTotal
if ($ref eq 'PXML::Element' or $ref eq 'PXML::_::XHTML' or $v->isa('PXML::Element')) { } 242 373 615
elsif (my $car_and_cdr = $v->can('car_and_cdr')) { } 155 218 373
elsif (my $for_each = $v->can('for_each')) { } 73 145 218
Condition83%5/6
$ref eq "PXML::Element" or $ref eq "PXML::_::XHTML" or $v->isa("PXML::Element")
l!l&&r!l&&!r
2420373
$ref eq "PXML::Element" or $ref eq "PXML::_::XHTML"
l!l&&r!l&&!r
34208373
MC/DC66%2/3
$ref eq "PXML::Element" or $ref eq "PXML::_::XHTML" or $v->isa("PXML::Element")
$ref eq "PXML::Element" $ref eq "PXML::_::XHTML" $v->isa("PXML::Element")
Truth table75%3/4
A or B or C
A= $ref eq "PXML::Element"B= $ref eq "PXML::_::XHTML"C= $v->isa("PXML::Element")
ABCresult
1XX1
01X1
0011
0000
201 $ref eq "PXML::Element"
202 or $ref eq "PXML::_::XHTML"
203  
204 # ^ is this a worthwhile optimization?
205 or $v->isa("PXML::Element")
206 )
207 {
208 242 PXML:
209 my $n = $v->name;
210 242 ▶ print $fh "<$n" or die $!;
Branch: 1 branch
BranchTrueFalseTotal
print $fh "<$n" or die $! 242 0 242
211 242 ▶ if (my $attrs = $v->maybe_attributes) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $attrs = $v->maybe_attributes) 84 158 242
212 84 for my $k (sort keys %$attrs) {
213 77 ▶ print $fh " $k=\"" or die $!;
Branch: 1 branch
BranchTrueFalseTotal
print $fh qq[ $k="] or die $! 77 0 77
214 77 my $str = _attribute_val_to_string $$attrs{$k}, $fh;
215 77 ▶ print $fh "$str\"" or die $!;
Branch: 1 branch
BranchTrueFalseTotal
print $fh qq[$str"] or die $! 77 0 77
216 }
217 }
218 242 my $body = $v->body;
219  
220 my $looksempty =
221  
222 # fast path
223 (
224 not defined $body # XX allow undef or don't? Please
225 # finally settle this!
226 or (not ref $body and length($body) == 0)
227 or (
228 is_somearray($body) and (
229 not @$body or (
230 @$body == 1
231 and
232 ( # XX remove undef check here now, too? OK?--nope, necessary
233 not defined $$body[0]
234 or (is_somearray($$body[0])
235 242 ▶ and not @{ $$body[0] })
Condition55%15/27
!defined($body) || !ref($body) && length $body == 0 || is_somearray($body) && (!@$body || @$body == 1 && (!defined($body->[0]) || is_somearray($body->[0]) && !@{$body->[0];} || is_empty_string($body->[0])))
l!l&&r!l&&!r
05237
not defined $body or not ref $body and length $body == 0
l!l&&r!l&&!r
00242
not ref $body and length $body == 0
!ll&&!rl&&r
24200
is_somearray($body) and !@$body || @$body == 1 && (!defined($body->[0]) || is_somearray($body->[0]) && !@{$body->[0];} || is_empty_string($body->[0]))
!ll&&!rl&&r
481895
not @$body or @$body == 1 and !defined($body->[0]) || is_somearray($body->[0]) && !@{$body->[0];} || is_empty_string($body->[0])
l!l&&r!l&&!r
50189
@$body == 1 and !defined($body->[0]) || is_somearray($body->[0]) && !@{$body->[0];} || is_empty_string($body->[0])
!ll&&!rl&&r
481410
not defined $body->[0] or is_somearray($body->[0]) and not @{$body->[0];} or is_empty_string($body->[0])
l!l&&r!l&&!r
00141
not defined $body->[0] or is_somearray($body->[0]) and not @{$body->[0];}
l!l&&r!l&&!r
00141
is_somearray($body->[0]) and not @{$body->[0];}
!ll&&!rl&&r
129120
MC/DC20%2/10
!defined($body) || !ref($body) && length $body == 0 || is_somearray($body) && (!@$body || @$body == 1 && (!defined($body->[0]) || is_somearray($body->[0]) && !@{$body->[0];} || is_empty_string($body->[0])))
not defined $body not ref $body length $body == 0 is_somearray($body) not @$body @$body == 1 not defined $body->[0] is_somearray($body->[0]) not @{$body->[0];} is_empty_string($body->[0])
Truth table25%5/20
not(A) or not(B) and C || D and not(E) or F and not(G) or H and not(I) or J
A= not defined $bodyB= not ref $bodyC= length $body == 0D= is_somearray($body)E= not @$bodyF= @$body == 1G= not defined $body->[0]H= is_somearray($body->[0])I= not @{$body->[0];}J= is_empty_string($body->[0])
ABCDEFGHIJresult
1XXXXXXXXX1
011XXXXXXX1
00X11XXXXX1
00X1011XXX1
00X101011X1
00X10100X11
00X10101011
01011XXXXX1
0101011XXX1
010101011X1
01010100X11
01010101011
00X0XXXXXX0
00X100XXXX0
00X10100X00
00X10101000
0100XXXXXX0
010100XXXX0
01010100X00
01010101000
236 or is_empty_string($$body[0])
237 )
238 )
239 )
240 )
241 );
242  
243 242 my $selfreferential;
244 242 ▶ if ($html5compat) {
Branch: 1 branch
BranchTrueFalseTotal
if ($html5compat) { } 204 38 242
245 204 ▶ if ($$void_element_h{$n}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($void_element_h->{$n}) { } 3 201 204
246 3 ▶ if ($looksempty) {
Branch: 1 branch
BranchTrueFalseTotal
if ($looksempty) { } 3 0 3
247 3 $selfreferential = 1;
248 } else {
249 0 my $isempty = # slow path
250 is_null(stream_mixed_flatten($body));
251 0 $selfreferential = $isempty;
252 0 ▶ warn "html5 compatible serialization requested "
Branch: 1 branch
BranchTrueFalseTotal
unless $isempty 0 0 0
253 . "but got void element '$n' that is not empty"
254 if not $isempty;
255 }
256 } else {
257 201 $selfreferential = 0;
258 }
259 } else {
260 38 $selfreferential = $looksempty;
261 }
262 242 ▶ if ($selfreferential) {
Branch: 1 branch
BranchTrueFalseTotal
if ($selfreferential) { } 5 237 242
263 5 ▶ print $fh "/>" or die $!;
Branch: 1 branch
BranchTrueFalseTotal
print $fh "/>" or die $! 5 0 5
264 } else {
265 237 ▶ print $fh ">" or die $!;
Branch: 1 branch
BranchTrueFalseTotal
print $fh ">" or die $! 237 0 237
266 7 ▶ no warnings "recursion"; # hu.
Subroutine BEGIN: called CC=1 SCAR=0.0
267 237 _pxml_print_fragment_fast($body, $fh, $html5compat,
268 $void_element_h);
269 237 ▶ print $fh "</$n>" or die $!;
Branch: 1 branch
BranchTrueFalseTotal
print $fh "</$n>" or die $! 237 0 237
270 }
271 } elsif (my $car_and_cdr = $v->can("car_and_cdr")) {
272 155 PAIR:
273  
274 #my $a;
275 ($a, $v) = &$car_and_cdr($v);
276 155 _pxml_print_fragment_fast($a, $fh, $html5compat,
277 $void_element_h);
278  
279 #_pxml_print_fragment_fast (cdr $v, $fh);
280 155 redo LP;
281 } elsif (my $for_each = $v->can("for_each")) {
282  
283 # catches null, too. Well.
284 &$for_each(
285 $v,
286 3 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
287 3 my ($a) = @_;
288 3 _pxml_print_fragment_fast($a, $fh, $html5compat,
289 $void_element_h);
290 }
291 73 );
292 } else {
293 145 my $v2 = force($v, 1);
294  
295 # ^XX why pass nocache flag? (Was this to avoid
296 # memory retention issues?)
297 145 my $addr2 = refaddr($v2);
298 145 ▶ if (defined($addr2) and $addr2 != refaddr($v)) {
Branch: 3 branches
BranchTrueFalseTotal
if (defined $addr2 and $addr2 != builtin::refaddr($v)) { } 111 34 145
elsif (is_somearray($v)) { } 9 25 34
elsif (is_pxmlflush($v)) { } 0 25 25
Condition66%2/3
defined $addr2 and $addr2 != builtin::refaddr($v)
!ll&&!rl&&r
034111
MC/DC50%1/2
defined $addr2 and $addr2 != builtin::refaddr($v)
defined $addr2 $addr2 != builtin::refaddr($v)
Truth table66%2/3
A and B
A= defined $addr2B= $addr2 != builtin::refaddr($v)
ABresult
0X0
100
111
299  
300 111 $v = $v2;
301 111 redo LP;
302 } elsif (is_somearray($v)) {
303  
304 # COPY-PASTE. Really should refactor
305 # _pxml_print_fragment_fast into local hash-table
306 # based dispatcher.
307 7 ▶ no warnings "recursion"; # hu.
Subroutine BEGIN: called CC=1 SCAR=0.0
308 9 for (@$v) {
309  
310 # XXX use Keep around $_ to prevent mutation of tree?
311 # nope, can't, will prevent streaming.
312 26 _pxml_print_fragment_fast($_, $fh, $html5compat,
313 $void_element_h);
314 }
315 } elsif (is_pxmlflush $v) {
316 0 ▶ flush $fh or die $!
Branch: 1 branch
BranchTrueFalseTotal
$fh->flush or die $! 0 0 0
317 } else {
318  
319 # Fallback for references, XX copy-paste
320 25 ▶ print $fh object_force_escape($v,
Branch: 1 branch
BranchTrueFalseTotal
print $fh object_force_escape($v, "pxml_serialized_body_string", \&PXML::Serialize::content_escape, $fh) or die $! 25 0 25
321 "pxml_serialized_body_string", \&content_escape,
322 $fh)
323 or die $!;
324 }
325 }
326 } else {
327 250 ▶ if (is_somearray($v)) {
Branch: 3 branches
BranchTrueFalseTotal
if (is_somearray($v)) { } 250 0 250
elsif ($ref eq 'CODE') { } 0 0 0
elsif (is_null($v)) { } 0 0 0
328 7 ▶ no warnings "recursion"; # hu.
Subroutine BEGIN: called CC=1 SCAR=0.0
329 250 for (@$v) {
330  
331 # XXX use Keep around $_ to prevent mutation of tree?
332 # nope, can't, will prevent streaming.
333 344 _pxml_print_fragment_fast($_, $fh, $html5compat,
334 $void_element_h);
335 }
336 }
337  
338 # 'force' doesn't evaluate CODE (probably rightly so),
339 # thus need to be explicit if we want 'late binding'
340 # (e.g. reference to dynamic variables) during
341 # serialization
342 elsif ($ref eq "CODE") {
343 0 $v = &$v();
344 0 redo LP;
345 } elsif (is_null $v) {
346 0 die "OBSOLETE?";
347  
348 # end of linked list, nothing
349 # XX obsolete now, since $v->can("for_each") above
350 # will catch it already.
351 } else {
352  
353 0 warn "XXX when does this happen?";
354  
355 #use FP::Repl;
356 #repl;
357  
358 # slow fallback... again, see above **NOTE** re
359 # evil.
360 0 ▶ $ref or die "BUG"; # we're in the if ref scope, right?
Branch: 1 branch
BranchTrueFalseTotal
$ref or die "BUG" 0 0 0
361 0 ▶ goto PXML if $v->isa("PXML::Element");
Branch: 1 branch
BranchTrueFalseTotal
if $v->isa("PXML::Element") 0 0 0
362 0 ▶ goto PAIR if is_pair $v;
Branch: 1 branch
BranchTrueFalseTotal
if is_pair($v) 0 0 0
363  
364 # goto PROMISE if is_promise $v;
365  
366 # Fallback for references, XX copy-paste
367 0 ▶ print $fh object_force_escape($v,
Branch: 1 branch
BranchTrueFalseTotal
print $fh object_force_escape($v, "pxml_serialized_body_string", \&PXML::Serialize::content_escape, $fh) or die $! 0 0 0
368 "pxml_serialized_body_string", \&content_escape, $fh)
369 or die $!;
370 }
371 }
372 } elsif (not defined $v) {
373  
374 # (previously end of linked list marker) nothing; XX
375 # should this give exception (to point out any issue with
376 # deleted streams, the reason why I changed from using
377 # undef to null)? But exception won't show a good
378 # backtrace anyway at this point.
379 #warn "warning: ignoring undef in PXML datastructure";
380 # XXX what to do about this?
381 } else {
382  
383 #print $fh content_escape($v) or die $!;
384 223 $v =~ s/([&<>])/$content_escape{$1}/sg;
385 223 ▶ print $fh $v or die $!;
Branch: 1 branch
BranchTrueFalseTotal
print $fh $v or die $! 223 0 223
386 }
387 }
388 }
389  
390 59 ▶sub pxml_print_fragment_fast {
Subroutine pxml_print_fragment_fast: called CC=7 SCAR=19.8
Pod undocumented
391 59 ▶ @_ == 2 or fp_croak_arity 2;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 or fp_croak_arity(2) 59 0 59
392 59 my ($v, $fh) = @_;
393 59 ▶ weaken $_[0] if ref $_[0]; # ref check perhaps unnecessary here
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] 58 1 59
394 12 ▶ my $no_element = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
395 12 @_ = ($v, $fh, undef, undef);
396 12 goto \&_pxml_print_fragment_fast;
397 59 };
398 47 ▶ my $with_first_element = sub {
Subroutine __ANON__: called CC=3 SCAR=11.2
399 47 my ($firstel) = @_;
400 47 ▶ weaken $_[0] if ref $_[0];
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] 47 0 47
401 47 my $html5compat
402 = $firstel->require_printing_nonvoid_elements_nonselfreferential;
403 47 ▶ @_ = ($v, $fh, $html5compat,
Condition66%2/3
$html5compat && $firstel->void_element_h
!ll&&!rl&&r
4043
MC/DC50%1/2
$html5compat && $firstel->void_element_h
$html5compat $firstel->void_element_h
Truth table66%2/3
A && B
A= $html5compatB= $firstel->void_element_h
ABresult
0X0
100
111
404 ($html5compat and $firstel->void_element_h));
405 47 goto \&_pxml_print_fragment_fast;
406 59 };
407 59 ▶ if (length(my $r = ref $v)) {
Branch: 1 branch
BranchTrueFalseTotal
if (length(my $r = ref $v)) { } 58 1 59
408 58 ▶ if (defined blessed $v and $v->isa("PXML::XHTML")) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined builtin::blessed($v) and $v->isa('PXML::XHTML')) { } 0 58 58
Condition66%2/3
defined builtin::blessed($v) and $v->isa("PXML::XHTML")
!ll&&!rl&&r
3550
MC/DC0%0/2
defined builtin::blessed($v) and $v->isa("PXML::XHTML")
defined builtin::blessed($v) $v->isa("PXML::XHTML")
Truth table66%2/3
A and B
A= defined builtin::blessed($v)B= $v->isa("PXML::XHTML")
ABresult
0X0
100
111
409 0 @_ = ($v);
410 0 goto &$with_first_element;
411 } else {
412 58 my $s = force(stream_mixed_flatten($v)->filter(\&is_pxml_element));
413 58 ▶ if (is_null $s) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_null($s)) { } 11 47 58
414 11 goto &$no_element
415 } else {
416 47 @_ = (car $s);
417 47 goto &$with_first_element;
418 }
419 }
420 } else {
421 1 goto &$no_element
422 }
423 }
424  
425 2 ▶sub pxml_xhtml_print_fast {
Subroutine pxml_xhtml_print_fast: called CC=10 SCAR=28.0
Pod undocumented
426 2 ▶ @_ >= 2 and @_ <= 3 or fp_croak_arity "2-3";
Branch: 1 branch
BranchTrueFalseTotal
@_ >= 2 and @_ <= 3 or fp_croak_arity("2-3") 2 0 2
Condition33%1/3
@_ >= 2 and @_ <= 3
!ll&&!rl&&r
002
MC/DC0%0/2
@_ >= 2 and @_ <= 3
@_ >= 2 @_ <= 3
Truth table33%1/3
A and B
A= @_ >= 2B= @_ <= 3
ABresult
0X0
100
111
427 2 my ($v, $fh, $maybe_lang) = @_;
428 2 ▶ weaken $_[0] if ref $_[0]; # ref check perhaps unnecessary here
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] 2 0 2
429 2 ▶ if (not ref $v or (defined(blessed $v) and not $v->isa("PXML::Element"))) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $v or defined builtin::blessed($v) and not $v->isa("PXML::Element")) 0 2 2
Condition33%2/6
not ref $v or defined builtin::blessed($v) and not $v->isa("PXML::Element")
l!l&&r!l&&!r
002
defined builtin::blessed($v) and not $v->isa("PXML::Element")
!ll&&!rl&&r
020
MC/DC0%0/3
not ref $v or defined builtin::blessed($v) and not $v->isa("PXML::Element")
not ref $v defined builtin::blessed($v) not $v->isa("PXML::Element")
Truth table25%1/4
not(A) or B and not(C)
A= not ref $vB= defined builtin::blessed($v)C= not $v->isa("PXML::Element")
ABCresult
1XX1
0111
00X0
0100
430 0 die "not an element: " . (show $v);
431 }
432 2 ▶ if (not "html" eq $v->name) {
Branch: 1 branch
BranchTrueFalseTotal
unless ("html" eq $v->name) 0 2 2
433 0 die "not an 'html' element: " . (show $v);
434 }
435 2 xprint($fh, "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
436 2 xprint($fh,
437 "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n"
438 );
439  
440 # add attributes to toplevel element
441 my $v2 = $v->maybe_attributes ? $v : $v->attributes_set(
442 2 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$v->maybe_attributes ? : 0 2 2
443 2 ▶ my $lang = $maybe_lang
Branch: 1 branch
BranchTrueFalseTotal
my $lang = $maybe_lang or die "missing 'lang' attribute from html element and no lang option given" 2 0 2
444 or die
445 "missing 'lang' attribute from html element and no lang option given";
446 +{
447 2 xmlns => "http://www.w3.org/1999/xhtml",
448 "xml:lang" => $lang,
449 lang => $lang
450 }
451 }
452 );
453 2 @_ = ($v2, $fh);
454 2 goto \&pxml_print_fragment_fast;
455 }
456  
457 # for now,
458 sub pxml_xhtml_print;
459 7 *pxml_xhtml_print = \&pxml_xhtml_print_fast;
460  
461 7 ▶use Chj::xopen "xopen_write";
Subroutine BEGIN: called CC=1 SCAR=0.0
462  
463 1 ▶sub pxml_print {
Subroutine pxml_print: called CC=3 SCAR=11.3
Pod undocumented
464 1 ▶ @_ == 2 or fp_croak_arity 2;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 or fp_croak_arity(2) 1 0 1
465 1 my ($v, $fh) = @_;
466 1 ▶ weaken $_[0] if ref $_[0]; # ref check perhaps unnecessary here
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] 1 0 1
467 1 xprintln($fh, q{<?xml version="1.0"?>});
468 1 pxml_print_fragment_fast($v, $fh);
469 }
470  
471 0 ▶sub putxmlfile {
Subroutine putxmlfile: not called CC=4 SCAR=30.0
Pod undocumented
472 0 ▶ @_ == 2 or fp_croak_arity 2;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 or fp_croak_arity(2) 0 0 0
473 0 my ($path, $xml) = @_;
474 0 ▶ weaken $_[1] if ref $_[0]; # ref check perhaps unnecessary here
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] 0 0 0
475 0 my $f = xopen_write $path;
476 0 ▶ binmode($f, ":utf8") or die "binmode";
Branch: 1 branch
BranchTrueFalseTotal
binmode $f, ":utf8" or die "binmode" 0 0 0
477  
478 # ^ XX should this use ":encoding(UTF-8)"? To validate in-memory
479 # strings? Shouldn't we just check all *inputs*?
480 0 pxml_print($xml, $f);
481 0 $f->xclose;
482 }
483  
484 0 ▶sub PXML::Element::xmlfile {
Subroutine xmlfile: not called CC=1 SCAR=6.9
Pod undocumented
485 0 my ($v, $path) = @_;
486 0 weaken $_[0];
487 0 putxmlfile($path, $v)
488 }
489  
490 0 ▶sub puthtmlfile {
Subroutine puthtmlfile: not called CC=6 SCAR=37.4
Pod undocumented
491 0 ▶ @_ >= 2 and @_ <= 3 or fp_croak_arity "2-3";
Branch: 1 branch
BranchTrueFalseTotal
@_ >= 2 and @_ <= 3 or fp_croak_arity("2-3") 0 0 0
Condition0%0/3
@_ >= 2 and @_ <= 3
!ll&&!rl&&r
000
MC/DC0%0/2
@_ >= 2 and @_ <= 3
@_ >= 2 @_ <= 3
Truth table0%0/3
A and B
A= @_ >= 2B= @_ <= 3
ABresult
0X0
100
111
492 0 my ($path, $v, $maybe_lang) = @_;
493 0 ▶ weaken $_[1] if ref $_[0]; # ref check perhaps unnecessary here
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] 0 0 0
494 #xmkdir_p dirname $path;
495 0 my $out = xopen_write($path);
496 0 ▶ binmode $out, ":utf8" or die "binmode";
Branch: 1 branch
BranchTrueFalseTotal
binmode $out, ":utf8" or die "binmode" 0 0 0
497  
498 # ^ XX dito, see comment in putxmlfile
499 0 ▶ pxml_xhtml_print_fast($v, $out, $maybe_lang || "en");
Condition0%0/2
$maybe_lang || 'en'
l!l
00
MC/DC0%0/1
$maybe_lang || 'en'
$maybe_lang
Truth table0%0/2
A
A= $maybe_lang
Aresult
11
00
500 0 $out->xclose;
501 }
502  
503 0 ▶sub PXML::Element::htmlfile {
Subroutine htmlfile: not called CC=1 SCAR=6.9
Pod undocumented
504 0 my ($v, $path, $maybe_lang) = @_;
505 0 weaken $_[0];
506 0 puthtmlfile($path, $v, $maybe_lang)
507 }
508  
509 7 1
« lib/Chj/IO/File.pm ↑ Summary lib/FP/RegexMatch.pm »