blib/lib/HTML/LoL.pm

filter: Statementstmt 88.3% 121 / 137 Branchbran 75.0% 36 / 48 Conditioncond 52.7% 19 / 36 MC/DCmcdc 33.3% 8 / 24 Subroutinesub 95.2% 20 / 21 Podpod 36.3% 4 / 11 totaltotal 75.0% 208 / 277 CC 43 SCAR 25.6
CC
43
Cov
80%
_node
23.9
_str
19.9
_emit
5.4
CRAP 12.9
↑ Summary
1 package HTML::LoL::Special;
2  
3 5 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
4 5 my($type, $str) = @_;
5 5 bless \$str, $type;
6 }
7  
8 package HTML::LoL;
9  
10 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use vars qw(@ISA @EXPORT $VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 1 $VERSION = '1.3';
15 1 @EXPORT = qw(hl hl_noquote hl_requote hl_entity hl_bool hl_preserve);
16  
17 1 ▶use constant TABWIDTH => 8;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 1 ▶use HTML::Entities;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶use HTML::Tagset;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 1 my $hl_bool_yes = new HTML::LoL::Special('bool_yes');
23 1 my $hl_bool_no = new HTML::LoL::Special('bool_no');
24 1 my $hl_noquote = new HTML::LoL::Special('noquote');
25 1 my $hl_requote = new HTML::LoL::Special('requote');
26 1 my $hl_preserve = new HTML::LoL::Special('preserve');
27  
28 6 ▶sub is_bool_yes {
Subroutine is_bool_yes: called CC=2 SCAR=6.9
Pod undocumented
29 6 my $x = shift;
30 6 ▶ return UNIVERSAL::isa($x, 'HTML::LoL::Special') && ($$x eq 'bool_yes');
Condition100%3/3
UNIVERSAL::isa($x, 'HTML::LoL::Special') && $$x eq 'bool_yes'
!ll&&!rl&&r
411
MC/DC100%2/2
UNIVERSAL::isa($x, 'HTML::LoL::Special') && $$x eq 'bool_yes'
UNIVERSAL::isa($x, 'HTML::LoL::Special') $$x eq 'bool_yes'
Truth table100%3/3
A && B
A= UNIVERSAL::isa($x, 'HTML::LoL::Special')B= $$x eq 'bool_yes'
ABresult
0X0
100
111
31 }
32  
33 5 ▶sub is_bool_no {
Subroutine is_bool_no: called CC=2 SCAR=7.1
Pod undocumented
34 5 my $x = shift;
35 5 ▶ return UNIVERSAL::isa($x, 'HTML::LoL::Special') && ($$x eq 'bool_no');
Condition66%2/3
UNIVERSAL::isa($x, 'HTML::LoL::Special') && $$x eq 'bool_no'
!ll&&!rl&&r
401
MC/DC50%1/2
UNIVERSAL::isa($x, 'HTML::LoL::Special') && $$x eq 'bool_no'
UNIVERSAL::isa($x, 'HTML::LoL::Special') $$x eq 'bool_no'
Truth table66%2/3
A && B
A= UNIVERSAL::isa($x, 'HTML::LoL::Special')B= $$x eq 'bool_no'
ABresult
0X0
100
111
36 }
37  
38 10 ▶sub is_noquote {
Subroutine is_noquote: called CC=2 SCAR=6.9
Pod undocumented
39 10 my $x = shift;
40 10 ▶ return UNIVERSAL::isa($x, 'HTML::LoL::Special') && ($$x eq 'noquote');
Condition100%3/3
UNIVERSAL::isa($x, 'HTML::LoL::Special') && $$x eq 'noquote'
!ll&&!rl&&r
712
MC/DC100%2/2
UNIVERSAL::isa($x, 'HTML::LoL::Special') && $$x eq 'noquote'
UNIVERSAL::isa($x, 'HTML::LoL::Special') $$x eq 'noquote'
Truth table100%3/3
A && B
A= UNIVERSAL::isa($x, 'HTML::LoL::Special')B= $$x eq 'noquote'
ABresult
0X0
100
111
41 }
42  
43 8 ▶sub is_requote {
Subroutine is_requote: called CC=2 SCAR=7.1
Pod undocumented
44 8 my $x = shift;
45 8 ▶ return UNIVERSAL::isa($x, 'HTML::LoL::Special') && ($$x eq 'requote');
Condition66%2/3
UNIVERSAL::isa($x, 'HTML::LoL::Special') && $$x eq 'requote'
!ll&&!rl&&r
710
MC/DC0%0/2
UNIVERSAL::isa($x, 'HTML::LoL::Special') && $$x eq 'requote'
UNIVERSAL::isa($x, 'HTML::LoL::Special') $$x eq 'requote'
Truth table66%2/3
A && B
A= UNIVERSAL::isa($x, 'HTML::LoL::Special')B= $$x eq 'requote'
ABresult
0X0
100
111
46 }
47  
48 8 ▶sub is_preserve {
Subroutine is_preserve: called CC=2 SCAR=7.1
Pod undocumented
49 8 my $x = shift;
50 8 ▶ return UNIVERSAL::isa($x, 'HTML::LoL::Special') && ($$x eq 'preserve');
Condition66%2/3
UNIVERSAL::isa($x, 'HTML::LoL::Special') && $$x eq 'preserve'
!ll&&!rl&&r
701
MC/DC50%1/2
UNIVERSAL::isa($x, 'HTML::LoL::Special') && $$x eq 'preserve'
UNIVERSAL::isa($x, 'HTML::LoL::Special') $$x eq 'preserve'
Truth table66%2/3
A && B
A= UNIVERSAL::isa($x, 'HTML::LoL::Special')B= $$x eq 'preserve'
ABresult
0X0
100
111
51 }
52  
53 # elements inside which it is OK to add whitespace
54 1 my %hl_wsok;
55 1 map { $hl_wsok{$_} = 1 } qw(area col colgroup frame frameset
56 head html object table tr);
57  
58 # elements whose layout should not be altered
59 1 my %hl_pre;
60 1 map { $hl_pre{$_} = 1 } qw(pre style script textarea);
61  
62 35 ▶sub _emit {
Subroutine _emit: called CC=4 SCAR=16.9
63 35 my($cb, $str, $columnref) = @_;
64 35 my $result = &$cb($str);
65  
66 35 ▶ if ($str =~ /.*\n([^\n]*)$/s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ /.*\n([^\n]*)$/s) 0 35 35
67 0 $str = $1;
68 0 $$columnref = 0;
69 }
70 35 my @s = split(/\t/, $str);
71 35 foreach my $s (@s) {
72 35 $$columnref += length($s);
73 }
74 35 ▶ if (@s > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@s > 1) 0 35 35
75 0 $$columnref += (TABWIDTH * (@s - 1));
76 0 $$columnref = int($$columnref / TABWIDTH);
77 0 ++$$columnref;
78 0 $$columnref *= TABWIDTH;
79 }
80  
81 35 return $result;
82 }
83  
84 9 ▶sub _str {
Subroutine _str: called CC=11 SCAR=29.9
85 9 my($cb, $str, $depth, $columnref, $wsokref, $pre, $noquote) = @_;
86  
87 9 my $result;
88  
89 9 ▶ $str = &encode_entities($str) unless $noquote;
Branch: 1 branch
BranchTrueFalseTotal
unless $noquote 7 2 9
90 9 ▶ if ($pre) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pre) { } 1 8 9
91 1 $result = &_emit($cb, $str, $columnref);
92 } else {
93 8 my $leading_ws = ($str =~ /^\s/s);
94 8 my $trailing_ws = ($str =~ /\s$/s);
95  
96 8 $str =~ s/^\s+//s;
97 8 $str =~ s/\s+$//s;
98  
99 8 my @words = split(/\s+/, $str);
100  
101 8 ▶ if (@words) {
Branch: 2 branches
BranchTrueFalseTotal
if (@words) { } 8 0 8
elsif ($leading_ws or $trailing_ws) { } 0 0 0
102 8 ▶ $$wsokref ||= $leading_ws;
Condition33%1/3
$$wsokref ||= $leading_ws
l!l&&r!l&&!r
008
MC/DC0%0/2
$$wsokref ||= $leading_ws
$$wsokref $leading_ws
Truth table33%1/3
A ||= B
A= $$wsokrefB= $leading_ws
ABresult
1X1
011
000
103  
104 8 foreach my $word (@words) {
105 8 ▶ if ($$wsokref) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$wsokref) 0 8 8
106 0 ▶ if (($$columnref > 0)
Branch: 1 branch
BranchTrueFalseTotal
if ($$columnref > 0 and 1 + length($word) + $$columnref > 72) { } 0 0 0
Condition0%0/3
$$columnref > 0 and 1 + length($word) + $$columnref > 72
!ll&&!rl&&r
000
MC/DC0%0/2
$$columnref > 0 and 1 + length($word) + $$columnref > 72
$$columnref > 0 1 + length($word) + $$columnref > 72
Truth table0%0/3
A and B
A= $$columnref > 0B= 1 + length($word) + $$columnref > 72
ABresult
0X0
100
111
107 && ((1 + length($word) + $$columnref) > 72)) {
108 0 $result = &_emit($cb, ("\n" . (' ' x ($depth + 1))), $columnref);
109 } else {
110 0 $result = &_emit($cb, ' ', $columnref);
111 }
112 }
113  
114 8 $result = &_emit($cb, $word, $columnref);
115  
116 8 $$wsokref = 1;
117 }
118 ▶ } elsif ($leading_ws || $trailing_ws) {
Condition0%0/3
$leading_ws or $trailing_ws
l!l&&r!l&&!r
000
MC/DC0%0/2
$leading_ws or $trailing_ws
$leading_ws $trailing_ws
Truth table0%0/3
A or B
A= $leading_wsB= $trailing_ws
ABresult
1X1
011
000
119 0 $result = &_emit($cb, ' ', $columnref);
120 }
121  
122 8 $$wsokref = $trailing_ws;
123 }
124  
125 9 return $result;
126 }
127  
128 10 ▶sub _node {
Subroutine _node: called CC=22 SCAR=31.8
129 10 my($cb, $node, $depth, $columnref, $wsokref, $pre, $noquote) = @_;
130  
131 10 my $result;
132  
133 10 my @node = @$node;
134 10 my $tag = $node[0];
135  
136 10 my $empty;
137  
138 10 ▶ if (&is_noquote($tag)) {
Branch: 3 branches
BranchTrueFalseTotal
if (&is_noquote($tag)) { } 2 8 10
elsif (&is_requote($tag)) { } 0 8 8
elsif (&is_preserve($tag)) { } 1 7 8
139 2 $noquote = 1;
140 2 undef $tag;
141 } elsif (&is_requote($tag)) {
142 0 $noquote = 0;
143 0 undef $tag;
144 } elsif (&is_preserve($tag)) {
145 1 $pre = 1;
146 1 undef $tag;
147 } else {
148 7 $tag = lc($tag);
149 7 $empty = $HTML::Tagset::emptyElement{$tag};
150 7 ▶ $pre ||= $hl_pre{$tag};
Condition66%2/3
$pre ||= $hl_pre{$tag}
l!l&&r!l&&!r
106
MC/DC50%1/2
$pre ||= $hl_pre{$tag}
$pre $hl_pre{$tag}
Truth table66%2/3
A ||= B
A= $preB= $hl_pre{$tag}
ABresult
1X1
011
000
151 }
152  
153 10 ▶ if ($$wsokref && !$pre) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$wsokref and not $pre) 0 10 10
Condition33%1/3
$$wsokref and not $pre
!ll&&!rl&&r
1000
MC/DC0%0/2
$$wsokref and not $pre
$$wsokref not $pre
Truth table33%1/3
A and not(B)
A= $$wsokrefB= not $pre
ABresult
0X0
100
111
154 0 $result = &_emit($cb, ("\n" . (' ' x $depth)), $columnref);
155 }
156  
157 10 ▶ if (defined($tag)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $tag) 7 3 10
158 7 $result = &_emit($cb, "<$tag", $columnref);
159 7 foreach my $content (@node[1 .. $#node]) {
160 12 ▶ next unless ref($content) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $content eq "HASH" 7 5 12
161 5 foreach my $hashitem (keys %$content) {
162 6 my $val = $content->{$hashitem};
163  
164 6 ▶ if (&is_bool_yes($val)) {
Branch: 3 branches
BranchTrueFalseTotal
if (&is_bool_yes($val)) { } 1 5 6
elsif (&is_bool_no($val)) { } 1 4 5
elsif (ref $val eq 'ARRAY') { } 1 3 4
165 1 $result = &_emit($cb, " $hashitem", $columnref);
166 } elsif (&is_bool_no($val)) {
167 # do nothing
168 } elsif (ref($val) eq 'ARRAY') {
169 # the caller wants the value interpolated literally
170 1 $result = &_emit($cb,
171 sprintf(' %s=%s', $hashitem, $val->[0]),
172 $columnref);
173 } else {
174 3 $result = &_emit($cb,
175 sprintf(' %s="%s"', $hashitem,
176 &encode_entities($val)),
177 $columnref);
178 }
179 }
180 }
181 7 $result = &_emit($cb, ">", $columnref);
182 7 $$wsokref = $hl_wsok{$tag};
183 }
184  
185 10 foreach my $content (@node[1 .. $#node]) {
186 15 my $ref = ref($content);
187 15 ▶ next if ($ref eq 'HASH');
Branch: 1 branch
BranchTrueFalseTotal
if $ref eq "HASH" 5 10 15
188  
189 10 ▶ if ($ref eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref eq 'ARRAY') { } 1 9 10
190 1 $result = &_node($cb, $content, $depth + 1, $columnref, $wsokref,
191 $pre, $noquote);
192 } else {
193 9 $result = &_str($cb, $content, $depth + 1, $columnref, $wsokref,
194 $pre, $noquote);
195 }
196  
197 10 ▶ $$wsokref ||= $hl_wsok{$tag} if defined($tag);
Branch: 1 branch
BranchTrueFalseTotal
if defined $tag 7 3 10
Condition33%1/3
$$wsokref ||= $hl_wsok{$tag}
l!l&&r!l&&!r
007
MC/DC0%0/2
$$wsokref ||= $hl_wsok{$tag}
$$wsokref $hl_wsok{$tag}
Truth table33%1/3
A ||= B
A= $$wsokrefB= $hl_wsok{$tag}
ABresult
1X1
011
000
198 }
199  
200 10 ▶ if (defined($tag) && !$empty) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $tag and not $empty) 7 3 10
Condition66%2/3
defined $tag and not $empty
!ll&&!rl&&r
307
MC/DC50%1/2
defined $tag and not $empty
defined $tag not $empty
Truth table66%2/3
A and not(B)
A= defined $tagB= not $empty
ABresult
0X0
100
111
201 7 ▶ if ($$wsokref) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$wsokref) 0 7 7
202 0 $result = &_emit($cb, ("\n" . (' ' x $depth)), $columnref);
203 }
204 7 $result = &_emit($cb, "</$tag>", $columnref);
205 7 $$wsokref = 0;
206 }
207  
208 10 return $result;
209 }
210  
211 9 ▶sub hl {
Subroutine hl: called CC=3 SCAR=11.2
Pod undocumented
212 9 my $cb = $_[0];
213  
214 9 my $column = 0;
215 9 my $wsok = 0;
216  
217 9 my $result;
218  
219 9 foreach my $elt (@_[1 .. $#_]) {
220 9 ▶ if (ref($elt)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $elt) { } 9 0 9
221 9 $result = &_node($cb, $elt, 0, \$column, \$wsok, 0, 0);
222 } else {
223 0 $result = &_str($cb, $elt, 0, \$column, \$wsok, 0, 0);
224 }
225 }
226  
227 9 return $result;
228 }
229  
230 1 ▶sub hl_noquote { [$hl_noquote => @_]; }
Subroutine hl_noquote: called CC=1 SCAR=0.0
Pod documented
231 0 ▶sub hl_requote { [$hl_requote => @_]; }
Subroutine hl_requote: not called CC=1 SCAR=6.9
Pod documented
232 1 ▶sub hl_preserve { [$hl_preserve => @_]; }
Subroutine hl_preserve: called CC=1 SCAR=0.0
Pod documented
233 1 ▶sub hl_entity { [$hl_noquote => map { "&$_;" } @_]; }
Subroutine hl_entity: called CC=1 SCAR=0.0
Pod documented
234  
235 2 ▶sub hl_bool { $_[0] ? $hl_bool_yes : $hl_bool_no }
Subroutine hl_bool: called CC=2 SCAR=6.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
$_[0] ? : 1 1 2
236  
237 1 1;
238  
239 __END__
↑ Summary