Templ/Filter/HTML.pm

filter: Statementstmt 100% 46 / 46 Branchbran 78.5% 11 / 14 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 7 / 7 Podpod 0.0% 0 / 3 totaltotal 91.4% 64 / 70 CC 8 SCAR 10.5
CC
8
Cov
95%
uri_escape
4.0
encode_entities
4.0
encode_html
2.0
CRAP 2.9
« Templ/Tag.pm ↑ Summary Templ/Tag/Print.pm »
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 5 ▶sub encode_html {
Subroutine encode_html: called CC=2 SCAR=7.0
Pod undocumented
17 5 my $str = shift;
18 5 ▶ return '' unless defined $str;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $str 0 5 5
19 5 $str =~ s/&/&/gs;
20 5 $str =~ s/"/"/gs;
21 5 $str =~ s/</&lt;/gs;
22 5 $str =~ s/>/&gt;/gs;
23 5 return $str;
24 }
25  
26 2 ▶sub uri_escape {
Subroutine uri_escape: called CC=4 SCAR=13.9
Pod undocumented
27 2 my $str = shift;
28 2 ▶ return '' unless defined $str;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $str 0 2 2
29  
30 2 ▶ if ( not defined $url_regex ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $url_regex) 1 1 2
31 1 $url_regex = qr/[^A-Za-z0-9_\.~-]/;
32 }
33  
34 2 ▶ if ( not scalar keys %url_charmap ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar keys %url_charmap) 1 1 2
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 2 ▶sub encode_entities {
Subroutine encode_entities: called CC=4 SCAR=13.9
Pod undocumented
45 2 my $str = shift;
46 2 ▶ return '' unless defined $str;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $str 0 2 2
47  
48 2 ▶ if ( not scalar keys %entity_cache ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar keys %entity_cache) 1 1 2
49 1 %entity_cache = (
50 '&' => '&amp;',
51 '"' => '&quot;',
52 '<' => '&lt;',
53 '>' => '&gt;',
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
BranchTrueFalseTotal
unless (defined $out) 1 1 2
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;
« Templ/Tag.pm ↑ Summary Templ/Tag/Print.pm »