blib/lib/Badger/Codec/HTML.pm untested

filter: Statementstmt 0.0% 0 / 33 Branchbran 0.0% 0 / 6 Conditioncond 0.0% 0 / 3 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 6 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 51 CC 4 SCAR 18.3
CC
4
Cov
0%
encode
12.0
decode
6.0
fix_windows
2.0
CRAP 6.2
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 use strict;
17 use warnings;
18 use Badger::Class
19 version => 0.01,
20 base => 'Badger::Codec';
21  
22 our $CHARSET = {
23 'ISO-8859-1' => \&fix_windows,
24 'WINDOWS-1252' => \&fix_windows,
25 };
26  
27 sub encode {
28 my ($class, $html, $charset) = @_;
29 my $filter;
30  
31 return undef unless defined($html);
32  
33 for ($html) {
34 s/&/&amp;/g;
35 s/\"/&quot;/g;
36 s/>/&gt;/g;
37 s/</&lt;/g;
38 }
39  
40 # pass resulting HTML through any corresponding filter for the
41 # character set (if specified)
42 if ($charset && ($filter = $CHARSET->{ $charset })) {
43 $html = &$filter($html);
44 }
45
46 return $html;
47 }
48  
49 sub decode {
50 my ($class, $html) = @_;
51 return undef unless defined($html);
52  
53 # Thanks to Randal Schwartz for the correct solution to this one.
54 $html =~ s[ &(.*?); ] {
55 local $_ = $1;
56 /^amp$/i ? "&" :
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 return $html;
66 }
67  
68 sub fix_windows {
69 my $html = shift;
70  
71 # work around bug in some inferior browsers
72 for ($html) {
73 s/'/&#39;/g;
74 s/\x8b/&#8249;/g;
75 s/\x9b/&#8250;/g;
76 }
77 return $html;
78 }
79  
80 1;
81  
82 __END__