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