Template/Util.pm

filter: Statementstmt 85.7% 24 / 28 Branchbran 62.5% 5 / 8 Conditioncond 33.3% 2 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 100% 7 / 7 Podpod 100% 1 / 1 totaltotal 72.2% 39 / 54 CC 7 SCAR 19.6
CC
7
Cov
74%
clean_text_ref
12.4
BEGIN
1.0
BEGIN
1.0
CRAP 7.1
« Template/IO.pm ↑ Summary Template/JSAdapter.pm »
1 6 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 =head1 NAME
6  
7 EJS::Template::Util - Utility for EJS::Template
8  
9 =cut
10  
11 package EJS::Template::Util;
12 6 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 6 our @EXPORT_OK = qw(clean_text_ref);
15  
16 6 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 6 ▶use Scalar::Util qw(tainted);
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 =head1 Methods
20  
21 =head2 clean_text_ref
22  
23 Usage:
24  
25 my $original_ref = \'some text';
26 my $modified_ref = clean_text_ref($original_ref,
27 $encode_utf8, $sanitize_utf8, $force_untaint);
28
29 # where the last three arguments are boolean values
30 # to indicate whether each conversion is required.
31  
32 Depending on JavaScript engines, the text value passed from Perl to JavaScript
33 needs to be cleaned up, especially related to the UTF8 flag and the taint mode.
34  
35 It takes a reference to the text as the first argument, and returns a reference
36 to the modified text, of if no conversion is necessary, the original reference
37 is returned.
38  
39 =over 4
40  
41 =item * $encode_utf8
42  
43 Indicates the text needs to be a utf8-encoded string, where the utf8 flag
44 has to be turned off.
45  
46 =item * $sanitize_utf8
47  
48 Indicates the text cannot contain any invalid utf8 characters. The conversion
49 is done by applying C<Encode::decode_utf8()> and then C<Encode::encode_utf8()>.
50  
51 =item * $force_untaint
52  
53 Indicates tainted strings cannot be passed to the JavaScript engine. This flag
54 effectively disables the taint flag, trusting the JavaScript code to be safe.
55  
56 =back
57  
58 =cut
59  
60 479 ▶sub clean_text_ref {
Subroutine clean_text_ref: called CC=7 SCAR=25.1
Pod documented
61 479 my ($value_ref, $encode_utf8, $sanitize_utf8, $force_untaint) = @_;
62
63 479 ▶ if (Encode::is_utf8($$value_ref)) {
Branch: 2 branches
BranchTrueFalseTotal
if (&Encode::is_utf8($$value_ref)) { } 26 453 479
elsif ($sanitize_utf8 and $$value_ref =~ /[\x80-\xFF]/) { } 0 906 906
64 26 ▶ if ($encode_utf8) {
Branch: 1 branch
BranchTrueFalseTotal
if ($encode_utf8) 0 26 26
65 # UTF8 flag must be turned off. (Otherwise, segmentation fault occurs)
66 0 $value_ref = \Encode::encode_utf8($$value_ref);
67 }
68 ▶ } elsif ($sanitize_utf8 && $$value_ref =~ /[\x80-\xFF]/) {
Condition33%1/3
$sanitize_utf8 and $$value_ref =~ /[\x80-\xFF]/
!ll&&!rl&&r
45300
MC/DC0%0/2
$sanitize_utf8 and $$value_ref =~ /[\x80-\xFF]/
$sanitize_utf8 $$value_ref =~ /[\x80-\xFF]/
Truth table33%1/3
A and B
A= $sanitize_utf8B= $$value_ref =~ /[\x80-\xFF]/
ABresult
0X0
100
111
69 # All characters must be valid UTF8. (Otherwise, segmentation fault occurs)
70 0 $value_ref = \Encode::encode_utf8(Encode::decode_utf8($$value_ref));
71 }
72
73 479 ▶ if ($force_untaint && tainted($$value_ref)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($force_untaint and &tainted($$value_ref)) 0 479 479
Condition33%1/3
$force_untaint and &tainted($$value_ref)
!ll&&!rl&&r
47900
MC/DC0%0/2
$force_untaint and &tainted($$value_ref)
$force_untaint &tainted($$value_ref)
Truth table33%1/3
A and B
A= $force_untaintB= &tainted($$value_ref)
ABresult
0X0
100
111
74 0 $$value_ref =~ /(.*)/s;
75 0 $value_ref = \qq($1);
76 }
77
78 479 return $value_ref;
79 }
80  
81 =head1 SEE ALSO
82  
83 =over 4
84  
85 =item * L<EJS::Template>
86  
87 =back
88  
89 =cut
90  
91 6 1;
« Template/IO.pm ↑ Summary Template/JSAdapter.pm »