File Coverage

lib/Badger/Codec/HTML.pm
Criterion Covered Total %
statement 25 32 78.1
branch 10 18 55.5
condition 1 3 33.3
subroutine 5 6 83.3
pod 3 3 100.0
total 44 62 70.9


line stmt bran cond sub pod time code
1             #========================================================================
2             #
3             # Badger::Codec::HTML
4             #
5             # DESCRIPTION
6             # Module for encoding (escaping) and decoding (unescaping) HTML
7             #
8             # AUTHOR
9             # Andy Wardley based on code extracted from
10             # Lincoln Stein's CGI.pm module.
11             #
12             #========================================================================
13              
14             package Badger::Codec::HTML;
15              
16 1     1   6 use strict;
  1         1  
  1         27  
17 1     1   4 use warnings;
  1         3  
  1         52  
18             use Badger::Class
19 1         9 version => 0.01,
20 1     1   5 base => 'Badger::Codec';
  1         2  
21              
22             our $CHARSET = {
23             'ISO-8859-1' => \&fix_windows,
24             'WINDOWS-1252' => \&fix_windows,
25             };
26              
27             sub encode {
28 4     4 1 10 my ($class, $html, $charset) = @_;
29 4         5 my $filter;
30              
31 4 50       8 return undef unless defined($html);
32              
33 4         9 for ($html) {
34 4         12 s/&/&/g;
35 4         11 s/\"/"/g;
36 4         10 s/>/>/g;
37 4         12 s/
38             }
39              
40             # pass resulting HTML through any corresponding filter for the
41             # character set (if specified)
42 4 50 33     8 if ($charset && ($filter = $CHARSET->{ $charset })) {
43 0         0 $html = &$filter($html);
44             }
45            
46 4         17 return $html;
47             }
48              
49             sub decode {
50 4     4 1 8 my ($class, $html) = @_;
51 4 50       8 return undef unless defined($html);
52              
53             # Thanks to Randal Schwartz for the correct solution to this one.
54 4         30 $html =~ s[ &(.*?); ] {
55 28         43 local $_ = $1;
56 28 0       105 /^amp$/i ? "&" :
    0          
    50          
    100          
    100          
    100          
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         18 return $html;
66             }
67              
68             sub fix_windows {
69 0     0 1   my $html = shift;
70              
71             # work around bug in some inferior browsers
72 0           for ($html) {
73 0           s/'/'/g;
74 0           s/\x8b/‹/g;
75 0           s/\x9b/›/g;
76             }
77 0           return $html;
78             }
79              
80             1;
81              
82             __END__