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
|
|
7
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
28
|
|
17
|
1
|
|
|
1
|
|
4
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
31
|
|
18
|
|
|
|
|
|
|
use Badger::Class |
19
|
1
|
|
|
|
|
8
|
version => 0.01, |
20
|
1
|
|
|
1
|
|
5
|
base => 'Badger::Codec'; |
|
1
|
|
|
|
|
1
|
|
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
our $CHARSET = { |
23
|
|
|
|
|
|
|
'ISO-8859-1' => \&fix_windows, |
24
|
|
|
|
|
|
|
'WINDOWS-1252' => \&fix_windows, |
25
|
|
|
|
|
|
|
}; |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
sub encode { |
28
|
4
|
|
|
4
|
1
|
12
|
my ($class, $html, $charset) = @_; |
29
|
4
|
|
|
|
|
7
|
my $filter; |
30
|
|
|
|
|
|
|
|
31
|
4
|
50
|
|
|
|
10
|
return undef unless defined($html); |
32
|
|
|
|
|
|
|
|
33
|
4
|
|
|
|
|
7
|
for ($html) { |
34
|
4
|
|
|
|
|
15
|
s/&/&/g; |
35
|
4
|
|
|
|
|
14
|
s/\"/"/g; |
36
|
4
|
|
|
|
|
11
|
s/>/>/g; |
37
|
4
|
|
|
|
|
14
|
s/</g; |
38
|
|
|
|
|
|
|
} |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
# pass resulting HTML through any corresponding filter for the |
41
|
|
|
|
|
|
|
# character set (if specified) |
42
|
4
|
50
|
33
|
|
|
12
|
if ($charset && ($filter = $CHARSET->{ $charset })) { |
43
|
0
|
|
|
|
|
0
|
$html = &$filter($html); |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
|
46
|
4
|
|
|
|
|
15
|
return $html; |
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
sub decode { |
50
|
4
|
|
|
4
|
1
|
9
|
my ($class, $html) = @_; |
51
|
4
|
50
|
|
|
|
10
|
return undef unless defined($html); |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
# Thanks to Randal Schwartz for the correct solution to this one. |
54
|
4
|
|
|
|
|
24
|
$html =~ s[ &(.*?); ] { |
55
|
28
|
|
|
|
|
57
|
local $_ = $1; |
56
|
28
|
0
|
|
|
|
139
|
/^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
|
|
|
|
|
19
|
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__ |