blib/lib/HTML/Untidy.pm

filter: Statementstmt 100% 86 / 86 Branchbran 100% 16 / 16 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 100% 16 / 16 Podpod 70.0% 7 / 10 totaltotal 97.7% 130 / 133 CC 10 SCAR 13.8
CC
10
Cov
100%
element
9.0
attr
2.0
BEGIN
1.0
CRAP 4.0
↑ Summary
1 package HTML::Untidy;
2 # ABSTRACT: yet another way to write HTML quickly and programmatically
3 1 $HTML::Untidy::VERSION = '0.01';
4  
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use parent 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use HTML::Escape 'escape_html';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 my @BASE = qw(element class attr prop text raw note);
11  
12 # source: https://developer.mozilla.org/en-US/docs/Web/HTML/Element
13 1 my @TAGS = qw(
14 a abbr address area article aside audio
15 b base bdi bdo blockquote body br button
16 canvas caption cite code col colgroup
17 data datalist dd del details dfn dialog div dl dt
18 em embed
19 fieldset figcaption figure footer form
20 h1 h2 h3 h4 h5 h6 head header hgroup hr html
21 i iframe img input ins
22 kbd
23 label legend li link
24 main map mark menu menuitem meta meter
25 nav noframes noscript
26 object ol optgroup option output
27 p param picture pre progress
28 q
29 rp rt rtc ruby
30 s samp script section select slot small source span strong style sub summary sup
31 table tbody td template textarea tfoot th thead time title tr track
32 u ul
33 var video
34 wbr
35 );
36  
37 1 my @COMMON = qw(
38 html head body title meta link script style
39 h1 h2 h3 h4 h5 h6
40 div p hr pre nav code img a b i u em strong sup sub small
41 table tbody thead tr th td
42 ul dl ol li dd dt
43 form input textarea select option button label
44 canvas
45 );
46  
47 1 our @EXPORT_OK = (@BASE, @TAGS);
48  
49 1 our %EXPORT_TAGS = (
50 base => [@BASE],
51 common => [@BASE, @COMMON],
52 all => [@BASE, @TAGS],
53 );
54  
55 1 our @CLASS;
56 1 our @ATTR;
57 1 our @PROP;
58 1 our @BODY;
59 1 our $INDENT = 0;
60  
61 1 my $DEPTH = 0;
62  
63 sub install_sub{
64 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
65 115 ▶ my ($name, $code) = @_;
Subroutine install_sub: called CC=1 SCAR=0.0
Pod undocumented
66 115 *{"${name}"} = $code;
67 }
68  
69 sub e ($){
70 42 ▶ goto \&escape_html;
Subroutine e: called CC=1 SCAR=0.0
Pod undocumented
71 }
72  
73 sub indent {
74 56 ▶ return ' ' x ($DEPTH * $INDENT);
Subroutine indent: called CC=1 SCAR=0.0
Pod undocumented
75 }
76  
77 sub element ($&){
78 15 ▶ my ($tag, $code) = @_;
Subroutine element: called CC=9 SCAR=22.0
Pod documented
79  
80 15 my $html = do {
81 15 local @CLASS;
82 15 local @ATTR;
83 15 local @PROP;
84 15 local @BODY;
85  
86 15 ++$DEPTH;
87 15 my $inner_html = $code->();
88 15 --$DEPTH;
89  
90 15 ▶ if ($inner_html) {
Branch: 1 branch
BranchTrueFalseTotal
if ($inner_html) 6 9 15
91 6 push @BODY, $inner_html;
92 }
93  
94 15 my @attrs;
95 15 ▶ for (my $i = 0; $i < @ATTR; $i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @HTML::Untidy::ATTR) 2 15 17
96 2 push @attrs, qq{$ATTR[$i]="$ATTR[$i + 1]"};
97 }
98  
99 15 ▶ my $attr = ''; $attr = ' ' . join ' ', @attrs if @attrs;
Branch: 1 branch
BranchTrueFalseTotal
if @attrs 2 13 15
100 15 ▶ my $prop = ''; $prop = ' ' . join ' ', @PROP if @PROP;
Branch: 1 branch
BranchTrueFalseTotal
if @HTML::Untidy::PROP 1 14 15
101 15 ▶ my $class = ''; $class = sprintf ' class="%s"', join ' ', @CLASS if @CLASS;
Branch: 1 branch
BranchTrueFalseTotal
if @HTML::Untidy::CLASS 1 14 15
102  
103 15 ▶ if (@BODY) {
Branch: 1 branch
BranchTrueFalseTotal
if (@HTML::Untidy::BODY) { } 12 3 15
104 12 my $open = sprintf '%s<%s%s%s%s>', indent, $tag, $class, $attr, $prop;
105 12 my $close = sprintf '%s</%s>', indent, $tag;
106 12 join("\n", $open, join("\n", @BODY), $close);
107 }
108 else {
109 3 sprintf q{%s<%s%s%s%s></%s>}, indent, $tag, $class, $attr, $prop, $tag;
110 }
111 };
112  
113 15 my $void = !defined wantarray;
114  
115 # At root of tag stack or called in non-void context
116 15 ▶ if ($DEPTH == 0 || !$void) {
Branch: 1 branch
BranchTrueFalseTotal
if ($DEPTH == 0 or not $void) { } 10 5 15
Condition100%3/3
$DEPTH == 0 or not $void
l!l&&r!l&&!r
465
MC/DC100%2/2
$DEPTH == 0 or not $void
$DEPTH == 0 not $void
Truth table100%3/3
A or not(B)
A= $DEPTH == 0B= not $void
ABresult
1X1
011
000
117 10 return $html;
118 }
119 # Inner-tag body call in void context
120 else {
121 5 push @BODY, $html;
122 5 return;
123 }
124 }
125  
126 4 ▶sub class (@){ push @CLASS, map{ e $_ } map{ split /\s+/, $_ } @_; return; }
Subroutine class: called CC=1 SCAR=0.0
Pod documented
127 4 ▶sub prop (@){ push @PROP, map{ e $_ } @_; return; }
Subroutine prop: called CC=1 SCAR=0.0
Pod documented
128 10 ▶sub text (@){ push @BODY, map{ indent . e $_ } @_; return; }
Subroutine text: called CC=1 SCAR=0.0
Pod documented
129 3 ▶sub raw (@){ push @BODY, map{ indent . $_ } @_; return; }
Subroutine raw: called CC=1 SCAR=0.0
Pod documented
130 4 ▶sub note (@){ push @BODY, map{ indent . '<!-- ' . e($_) . ' -->' } @_; return }
Subroutine note: called CC=1 SCAR=0.0
Pod documented
131  
132 sub attr (@){
133 2 ▶ for (my $i = 0; $i < @_; $i += 2) {
Subroutine attr: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @_) 2 2 4
134 2 push @ATTR, e $_[$i], e $_[$i + 1];
135 }
136  
137 2 return;
138 }
139  
140 1 foreach my $tag (@TAGS){
141 15 ▶ install_sub($tag, sub(&){ unshift @_, $tag; goto \&element; });
Subroutine __ANON__: called CC=1 SCAR=0.0
142 }
143  
144 1 1;
145  
146 __END__
↑ Summary