lib/Badger/Codec/HTML.pm

filter: Statementstmt 79.4% 27 / 34 Branchbran 55.5% 10 / 18 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 83.3% 5 / 6 Podpod 100% 3 / 3 totaltotal 69.7% 46 / 66 CC 13 SCAR 19.0
CC
13
Cov
69%
decode
9.7
fix_windows
6.0
encode
5.5
CRAP 6.7
« blib/lib/Badger/Data/Type/Class.pm ↑ Summary blib/lib/Badger/Duration.pm »
1 #========================================================================
2 #
3 # Badger::Codec::HTML
4 #
5 # DESCRIPTION
6 # Module for encoding (escaping) and decoding (unescaping) HTML
7 #
8 # AUTHOR
9 # Andy Wardley <abw@wardley.org> based on code extracted from
10 # Lincoln Stein's CGI.pm module.
11 #
12 #========================================================================
13  
14 package Badger::Codec::HTML;
15  
16 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 use Badger::Class
19 1 version => 0.01,
20 1 ▶ base => 'Badger::Codec';
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 1 our $CHARSET = {
23 'ISO-8859-1' => \&fix_windows,
24 'WINDOWS-1252' => \&fix_windows,
25 };
26  
27 sub encode {
28 4 ▶ my ($class, $html, $charset) = @_;
Subroutine encode: called CC=5 SCAR=17.1
Pod documented
29 4 my $filter;
30  
31 4 ▶ return undef unless defined($html);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $html 0 4 4
32  
33 4 for ($html) {
34 4 s/&/&amp;/g;
35 4 s/\"/&quot;/g;
36 4 s/>/&gt;/g;
37 4 s/</&lt;/g;
38 }
39  
40 # pass resulting HTML through any corresponding filter for the
41 # character set (if specified)
42 4 ▶ if ($charset && ($filter = $CHARSET->{ $charset })) {
Branch: 1 branch
BranchTrueFalseTotal
if ($charset and $filter = $Badger::Codec::HTML::CHARSET->{$charset}) 0 4 4
Condition33%1/3
$charset and $filter = $Badger::Codec::HTML::CHARSET->{$charset}
!ll&&!rl&&r
400
MC/DC0%0/2
$charset and $filter = $Badger::Codec::HTML::CHARSET->{$charset}
$charset $filter = $Badger::Codec::HTML::CHARSET->{$charset}
Truth table33%1/3
A and B
A= $charsetB= $filter = $Badger::Codec::HTML::CHARSET->{$charset}
ABresult
0X0
100
111
43 0 $html = &$filter($html);
44 }
45
46 4 return $html;
47 }
48  
49 sub decode {
50 4 ▶ my ($class, $html) = @_;
Subroutine decode: called CC=8 SCAR=22.8
Pod documented
51 4 ▶ return undef unless defined($html);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $html 0 4 4
52  
53 # Thanks to Randal Schwartz for the correct solution to this one.
54 4 $html =~ s[ &(.*?); ] {
55 28 local $_ = $1;
56 28 ▶ /^amp$/i ? "&" :
Branch: 6 branches
BranchTrueFalseTotal
/^amp$/i ? : 4 24 28
/^quot$/i ? : 8 16 24
/^gt$/i ? : 8 8 16
/^lt$/i ? : 8 0 8
/^#(\d+)$/ ? : 0 0 0
/^#x([0-9a-f]+)$/i ? : 0 0 0
57 /^quot$/i ? '"' :
58 /^gt$/i ? ">" :
59 /^lt$/i ? "<" :
60 /^#(\d+)$/ ? chr($1) :
61 /^#x([0-9a-f]+)$/i ? chr(hex($1)) :
62 $_
63 }gex;
64  
65 4 return $html;
66 }
67  
68 sub fix_windows {
69 0 ▶ my $html = shift;
Subroutine fix_windows: not called CC=2 SCAR=17.9
Pod documented
70  
71 # work around bug in some inferior browsers
72 0 for ($html) {
73 0 s/'/&#39;/g;
74 0 s/\x8b/&#8249;/g;
75 0 s/\x9b/&#8250;/g;
76 }
77 0 return $html;
78 }
79  
80 1 1;
81  
82 __END__
« blib/lib/Badger/Data/Type/Class.pm ↑ Summary blib/lib/Badger/Duration.pm »