| 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
| |||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||
| 33 | 4 | for ($html) { | |||||||||||||||||||||||||||||
| 34 | 4 | s/&/&/g; | |||||||||||||||||||||||||||||
| 35 | 4 | s/\"/"/g; | |||||||||||||||||||||||||||||
| 36 | 4 | s/>/>/g; | |||||||||||||||||||||||||||||
| 37 | 4 | s/</</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
Condition33%1/3
$charset and $filter = $Badger::Codec::HTML::CHARSET->{$charset}
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}
| |||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||
| 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/'/'/g; | |||||||||||||||||||||||||||||
| 74 | 0 | s/\x8b/‹/g; | |||||||||||||||||||||||||||||
| 75 | 0 | s/\x9b/›/g; | |||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||||
| 77 | 0 | return $html; | |||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||
| 80 | 1 | 1; | |||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||
| 82 | __END__ | ||||||||||||||||||||||||||||||