Valiant/HTML/SafeString.pm

filter: Statementstmt 100% 60 / 60 Branchbran 69.2% 18 / 26 Conditioncond 71.4% 5 / 7 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 100% 22 / 22 Podpod 81.8% 9 / 11 totaltotal 89.8% 115 / 128 CC 17 SCAR 10.4
CC
17
Cov
89%
is_safe
6.3
raw
4.1
safe
4.0
CRAP 2.8
« Valiant/Filter/Each.pm ↑ Summary Valiant/HTML/Util/View.pm »
1 package Valiant::HTML::SafeString;
2  
3 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 7 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 7 ▶use HTML::Escape ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 7 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 7 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 use overload
11 3 ▶ bool => sub { shift->to_bool },
Subroutine __ANON__: called CC=1 SCAR=0.0
12 603 ▶ '""' => sub { shift->to_string },
Subroutine __ANON__: called CC=1 SCAR=0.0
13 '+' => sub {
14 2 ▶ my ($self, $other, $reverse) = @_;
Subroutine __ANON__: called CC=3 SCAR=11.7
15 2 ▶ carp "Can only join two safe string objects" unless (ref($other) eq ref($self));
Branch: 1 branch
BranchTrueFalseTotal
unless ref $other eq ref $self 0 2 2
16
17 2 ▶ return $reverse
Branch: 1 branch
BranchTrueFalseTotal
$reverse ? : 0 2 2
18 ? raw("${other}${self}")
19 : raw("${self}${other}");
20 },
21 7 ▶ fallback => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 7 our @EXPORT_OK = qw(raw flattened_raw safe flattened_safe is_safe escape_html safe_concat concat);
24 7 our %EXPORT_TAGS = (all => \@EXPORT_OK, core => ['raw', 'safe', 'escape_html', 'safe_concat']);
25  
26 sub _make_safe {
27 1262 ▶ my $string_to_make_safe = shift;
Subroutine _make_safe: called CC=1 SCAR=0.0
28 1262 return bless(\$string_to_make_safe, 'Valiant::HTML::SafeString');
29 }
30  
31 sub escape_html {
32 1190 ▶ my $string = shift;
Subroutine escape_html: called CC=1 SCAR=0.0
Pod undocumented
33 1190 return HTML::Escape::escape_html($string);
34 }
35  
36 sub raw {
37 798 ▶ if(scalar(@_) == 1) {
Subroutine raw: called CC=4 SCAR=14.2
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ == 1) { } 797 1 798
38 797 ▶ return is_safe($_[0]) ? $_[0] : _make_safe($_[0])
Branch: 1 branch
BranchTrueFalseTotal
is_safe($_[0]) ? : 0 797 797
39 } else {
40 1 ▶ return map { is_safe($_) ? $_ : _make_safe($_) } @_;
Branch: 1 branch
BranchTrueFalseTotal
is_safe($_) ? : 0 2 2
41 }
42 }
43  
44 sub flattened_raw {
45 1 ▶ my $string = join '', map { is_safe($_) ? $_->to_string : $_ } @_;
Subroutine flattened_raw: called CC=2 SCAR=7.1
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
is_safe($_) ? : 0 2 2
46 1 return _make_safe $string;
47 }
48  
49 sub is_safe {
50 1473 ▶ my $string_to_test = shift;
Subroutine is_safe: called CC=6 SCAR=18.4
Pod documented
51 1473 ▶ return 0 unless defined($string_to_test);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $string_to_test 0 1473 1473
52 1473 ▶ return 0 unless
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($string_to_test) || "") eq "Valiant::HTML::SafeString" or (builtin::blessed($string_to_test) || "") eq "Mojo::ByteStream" 1066 407 1473
Condition71%5/7
(builtin::blessed($string_to_test) || "") eq "Valiant::HTML::SafeString" or (builtin::blessed($string_to_test) || "") eq "Mojo::ByteStream"
l!l&&r!l&&!r
40701066
builtin::blessed($string_to_test) || ''
l!l
4071066
builtin::blessed($string_to_test) || ''
l!l
01066
MC/DC50%1/2
(builtin::blessed($string_to_test) || "") eq "Valiant::HTML::SafeString" or (builtin::blessed($string_to_test) || "") eq "Mojo::ByteStream"
builtin::blessed($string_to_test) builtin::blessed($string_to_test)
Truth table66%2/3
A or B
A= builtin::blessed($string_to_test)B= builtin::blessed($string_to_test)
ABresult
1X1
011
000
53 ((Scalar::Util::blessed($string_to_test)||'') eq 'Valiant::HTML::SafeString') ||
54 ((Scalar::Util::blessed($string_to_test)||'') eq 'Mojo::ByteStream');
55 407 return 1;
56 }
57  
58 sub safe {
59 19 ▶ if(scalar(@_) == 1) {
Subroutine safe: called CC=4 SCAR=13.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ == 1) { } 18 1 19
60 18 ▶ return is_safe($_[0]) ? $_[0] : _make_safe(escape_html($_[0]))
Branch: 1 branch
BranchTrueFalseTotal
is_safe($_[0]) ? : 2 16 18
61 } else {
62 1 ▶ return map { is_safe($_) ? $_ : _make_safe(escape_html($_)) } @_;
Branch: 1 branch
BranchTrueFalseTotal
is_safe($_) ? : 0 2 2
63 }
64 }
65  
66 441 ▶sub safe_concat { return flattened_safe(@_) }
Subroutine safe_concat: called CC=1 SCAR=0.0
Pod undocumented
67  
68 sub flattened_safe {
69 444 ▶ my $string = join '', map { is_safe($_) ? $_->to_string : escape_html($_) } grep { defined($_) } @_;
Subroutine flattened_safe: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
is_safe($_) ? : 401 247 648
70 444 return _make_safe $string;
71 }
72  
73 sub new {
74 1 ▶ my $class = shift;
Subroutine new: called CC=1 SCAR=0.0
Pod documented
75 1 return flattened_safe(@_);
76 }
77  
78 1 ▶sub concat { return flattened_safe(@_) }
Subroutine concat: called CC=1 SCAR=0.0
Pod documented
79  
80 1014 ▶sub to_string { return ${$_[0]} }
Subroutine to_string: called CC=1 SCAR=0.0
Pod documented
81  
82 3 ▶sub to_bool { return ${$_[0]} ? 1 : 0 }
Subroutine to_bool: called CC=2 SCAR=7.2
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
${$_[0];} ? : 3 0 3
83  
84 7 1;
85  
86 =head1 NAME
87  
88 Valiant::HTML::SafeString - String rendering safety
89  
90 =head1 SYNOPSIS
91  
92 use Valiant::HTML::SafeString 'safe', 'escape';
93  
94 =head1 DESCRIPTION
95  
96 Protecting your templates from the various types of character injection attacks is
97 a prime concern for anyone working with the HTML user interface. This class provides
98 some methods and exports to make this job easier.
99  
100 =head1 EXPORTABLE FUNCTIONS
101  
102 The following functions can be exported by this library
103  
104 =head2 safe
105  
106 Given a string or array, returns such marked as 'safe' by using C<html_escape> on the string and
107 then encapsulating it inside an instance of L<Valiant::HTML::SafeString>. You can safely pass arguments
108 to this since if the string is already marked safe we just return it unaltered.
109  
110 =head2 flattened_safe
111  
112 Same as C<safe> but always returns a string even if you pass an array of strings (they are all
113 joined together).
114  
115 =head2 raw
116  
117 Given a string or array of strings, return each marked as safe (by encapsulating it inside an
118 instance of L<Valiant::HTML::SafeString>. This will just mark strings as safe without doing any
119 escaping first (for that see C<safe>) so be careful with this.
120  
121 =head2 flattened_raw
122  
123 Same as C<raw> but always returns a string even if you pass an array of strings (they are all
124 joined together).
125  
126 =head2 is_safe
127  
128 Given a string return a boolean indicating if its marked safe or not. Since C<safe> and C<raw> never
129 double the escapulations / escaping, you probably never need this but saw no reason to not expose it.
130  
131 =head1 CLASS METHODS
132  
133 This package exposes the folllowing class methods
134  
135 =head2 new
136  
137 my $safe_string = Valiant::HTML::SafeString->new(@strings);
138  
139 Given a string, or array of strings, returns a single string that has been C<html_escape>'d as needed
140 and encapulated in an instance. Its safe to pass arguments to this without testing since if a string
141 is already marked safe we don't do any extra escaping (although you will get a new instance).
142  
143 =head1 INSTANCE METHODS
144  
145 Instances of L<Valiant::HTML::SafeString> expose the following public methods
146  
147 =head2 concat
148  
149 Returns a new safe string which appends a list of strings to the old one, making those new strings
150 'safe' as needed. Basically this will escape any strings not marked safe already and then joins them
151 altogether in a single safe string.
152  
153 =head2 to_string
154  
155 Returns the raw string, suitable for display.
156  
157 =head2 to_bool
158  
159 Returns a boolean indicating if the string is empty or not.
160  
161 =head1 OVERLOADING
162  
163 String context calles C<to_string>; Boolean context returns true unless the string is empty.
164  
165 =head1 SEE ALSO
166
167 L<Valiant>, L<Valiant::HTML::FormBuilder>
168  
169 =head1 AUTHOR
170
171 See L<Valiant>
172  
173 =head1 COPYRIGHT & LICENSE
174
175 See L<Valiant>
176  
177 =cut
« Valiant/Filter/Each.pm ↑ Summary Valiant/HTML/Util/View.pm »