File Coverage

blib/lib/FU/XMLWriter.pm
Criterion Covered Total %
statement 15 39 38.4
branch 1 4 25.0
condition n/a
subroutine 5 11 45.4
pod 4 5 80.0
total 25 59 42.3


line stmt bran cond sub pod time code
1             package FU::XMLWriter 1.4;
2 1     1   76745 use v5.36;
  1         2  
3 1     1   4 use Carp 'confess';
  1         1  
  1         60  
4 1     1   5 use Exporter 'import';
  1         2  
  1         44  
5              
6             our $XSPRINT;
7 1 50   1   290 BEGIN { require FU::XS unless $XSPRINT }
8              
9             my @NORMAL_TAGS = qw/
10             a_ abbr_ address_ article_ aside_ audio_ b_ bb_ bdo_ blockquote_ body_
11             button_ canvas_ caption_ cite_ code_ colgroup_ datagrid_ datalist_ dd_ del_
12             details_ dfn_ dialog_ div_ dl_ dt_ em_ fieldset_ figure_ footer_ form_ h1_
13             h2_ h3_ h4_ h5_ h6_ head_ header_ i_ iframe_ ins_ kbd_ label_ legend_ li_
14             main_ map_ mark_ menu_ meter_ nav_ noscript_ object_ ol_ optgroup_ option_
15             output_ p_ pre_ progress_ q_ rp_ rt_ ruby_ samp_ script_ section_ select_
16             small_ span_ strong_ style_ sub_ summary_ sup_ table_ tbody_ td_ textarea_
17             tfoot_ th_ thead_ time_ title_ tr_ ul_ var_ video_
18             /;
19              
20             my @SELFCLOSE_TAGS = qw/
21             area_ base_ br_ col_ command_ embed_ hr_ img_ input_ link_ meta_ param_
22             source_
23             /;
24              
25             # Used by FU.xs to generate an XS function for each tag.
26             # (Wrapping tag_() within Perl is slow, using ALIAS is possible but still benefits from code gen)
27             if ($XSPRINT) {
28 0     0 0 0 sub f($name, $selfclose) {
  0         0  
  0         0  
  0         0  
29 0         0 my $tag = $name =~ s/_$//r;
30 0         0 my $len = length $tag;
31 0         0 printf <<~_;
32             void $name(...)
33             CODE:
34             if (!fuxmlwr_tail) fu_confess("No active FU::XMLWriter instance");
35             fuxmlwr_tag(aTHX_ fuxmlwr_tail, ax, 0, items, $selfclose, "$tag", $len);
36              
37             _
38             }
39             f $_, 0 for @NORMAL_TAGS;
40             f $_, 1 for @SELFCLOSE_TAGS;
41             }
42              
43              
44             our %EXPORT_TAGS = (
45             html5_ => [ qw/tag_ html_ lit_ txt_/, @NORMAL_TAGS, @SELFCLOSE_TAGS ],
46             xml_ => [ qw/xml_ tag_ lit_ txt_/ ],
47             );
48              
49             our @EXPORT_OK = (
50             qw/fragment xml_ xml_escape/,
51             @{$EXPORT_TAGS{html5_}},
52             );
53              
54             my %XML = qw/& & < < " "/;
55 0     0 1 0 sub xml_escape($s) {
  0         0  
  0         0  
56 0 0       0 return '' if !defined $s;
57 0         0 $s =~ s/([&<"])/$XML{$1}/gr;
58             }
59              
60 25     25 1 123622 sub fragment :prototype(&) ($f) {
  25         49  
  25         55  
61 25         325 my $wr = _new();
62 25         110 $f->();
63 21         8179 $wr->_done;
64             }
65              
66 0     0 1   sub html_(@arg) {
  0            
  0            
67             fragment {
68 0     0     lit_("\n");
69 0           tag_('html', @arg);
70             }
71 0           }
72              
73 0     0 1   sub xml_ :prototype(&) ($f) {
  0            
  0            
74             fragment {
75 0     0     lit_(qq{\n});
76 0           $f->();
77             }
78 0           }
79              
80             1;
81             __END__