| 1 | |||||||||||
| 2 | package Templ::Filter::HTML; | ||||||||||
| 3 | |||||||||||
| 4 | 1 | ▶ | use Exporter; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 1 | push @ISA, 'Exporter'; | |||||||||
| 6 | 1 | @EXPORT = qw(encode_html encode_entities uri_escape); | |||||||||
| 7 | |||||||||||
| 8 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 1 | ▶ | no warnings 'uninitialized'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | |||||||||||
| 12 | 1 | my $url_regex; | |||||||||
| 13 | my %url_charmap; | ||||||||||
| 14 | 1 | my %entity_cache; | |||||||||
| 15 | |||||||||||
| 16 | sub encode_html { | ||||||||||
| 17 | 5 | ▶ | my $str = shift; | ||||||||
|
Subroutine
encode_html: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||
| 18 | 5 | ▶ | return '' unless defined $str; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 19 | 5 | $str =~ s/&/&/gs; | |||||||||
| 20 | 5 | $str =~ s/"/"/gs; | |||||||||
| 21 | 5 | $str =~ s/</</gs; | |||||||||
| 22 | 5 | $str =~ s/>/>/gs; | |||||||||
| 23 | 5 | return $str; | |||||||||
| 24 | } | ||||||||||
| 25 | |||||||||||
| 26 | sub uri_escape { | ||||||||||
| 27 | 2 | ▶ | my $str = shift; | ||||||||
|
Subroutine
uri_escape: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||
| 28 | 2 | ▶ | return '' unless defined $str; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 29 | |||||||||||
| 30 | 2 | ▶ | if ( not defined $url_regex ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 31 | 1 | $url_regex = qr/[^A-Za-z0-9_\.~-]/; | |||||||||
| 32 | } | ||||||||||
| 33 | |||||||||||
| 34 | 2 | ▶ | if ( not scalar keys %url_charmap ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 35 | 190 | %url_charmap = map { chr($_) => sprintf( '%%%02X', $_ ); } | |||||||||
| 36 | 1 | grep { chr($_) =~ m/^$url_regex$/ } 0 .. 255; | |||||||||
| 37 | } | ||||||||||
| 38 | |||||||||||
| 39 | 2 | $str =~ s/($url_regex)/$url_charmap{$1}/gs; | |||||||||
| 40 | 2 | $str =~ s/\%20/+/gs; | |||||||||
| 41 | 2 | return $str; | |||||||||
| 42 | } | ||||||||||
| 43 | |||||||||||
| 44 | sub encode_entities { | ||||||||||
| 45 | 2 | ▶ | my $str = shift; | ||||||||
|
Subroutine
encode_entities: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||
| 46 | 2 | ▶ | return '' unless defined $str; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 47 | |||||||||||
| 48 | 2 | ▶ | if ( not scalar keys %entity_cache ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 49 | 1 | %entity_cache = ( | |||||||||
| 50 | '&' => '&', | ||||||||||
| 51 | '"' => '"', | ||||||||||
| 52 | '<' => '<', | ||||||||||
| 53 | '>' => '>', | ||||||||||
| 54 | ); | ||||||||||
| 55 | } | ||||||||||
| 56 | |||||||||||
| 57 | 2 | $str =~ s/([^\ \!\#\$\%\x28-\x3B\=\x3F-\x7E])/ | |||||||||
| 58 | 2 | my $out = $entity_cache{$1}; | |||||||||
| 59 | 2 | ▶ | unless (defined $out) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 60 | { | ||||||||||
| 61 | 1 | $out = sprintf '&#x%X;', ord($1); | |||||||||
| 62 | 1 | $entity_cache{$1} = $out; | |||||||||
| 63 | } | ||||||||||
| 64 | 2 | $out; | |||||||||
| 65 | /egsx; | ||||||||||
| 66 | 2 | return $str; | |||||||||
| 67 | } | ||||||||||
| 68 | |||||||||||
| 69 | 1 | 1; | |||||||||