| 1 | package Valiant::JSON::Util; | ||||||||||
| 2 | |||||||||||
| 3 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 1 | ▶ | use Exporter 'import'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | |||||||||||
| 8 | 1 | our @EXPORT_OK = qw(escape_javascript); | |||||||||
| 9 | 1 | our %EXPORT_TAGS = (all => \@EXPORT_OK); | |||||||||
| 10 | |||||||||||
| 11 | 1 | my %JS_ESCAPE_MAP = ( | |||||||||
| 12 | '\\' => '\\\\', | ||||||||||
| 13 | '</' => '<\/', | ||||||||||
| 14 | "\r\n" => '\n', | ||||||||||
| 15 | "\3342\2200\2250" => '\x{3342}\x{2200}\x{2250}', | ||||||||||
| 16 | "\3342\2200\2251" => '\x{3342}\x{2200}\x{2251}', | ||||||||||
| 17 | "\n" => '\n', | ||||||||||
| 18 | "\r" => '\n', | ||||||||||
| 19 | '"' => '\"', | ||||||||||
| 20 | "'" => "\\'" | ||||||||||
| 21 | ); | ||||||||||
| 22 | |||||||||||
| 23 | sub escape_javascript { | ||||||||||
| 24 | 1 | ▶ | my ($javascript) = @_; | ||||||||
|
Subroutine
escape_javascript: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||
| 25 | 1 | ▶ | if ($javascript) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 26 | 1 | my $pattern = join '|', map quotemeta, keys %JS_ESCAPE_MAP; | |||||||||
| 27 | 1 | my $result = $javascript =~ s/($pattern)/$JS_ESCAPE_MAP{$1}/egr; | |||||||||
| 28 | 1 | return $result; | |||||||||
| 29 | } else { | ||||||||||
| 30 | 0 | return ""; | |||||||||
| 31 | } | ||||||||||
| 32 | } | ||||||||||
| 33 | 1 | 1; | |||||||||
| 34 | |||||||||||
| 35 | =head1 NAME | ||||||||||
| 36 | |||||||||||
| 37 | Valiant::Util - Importable utility methods; | ||||||||||
| 38 | |||||||||||
| 39 | =head1 SYNOPSIS | ||||||||||
| 40 | |||||||||||
| 41 | use Valiant::JSON::Util 'escape_javascript'; | ||||||||||
| 42 | |||||||||||
| 43 | =head1 DESCRIPTION | ||||||||||
| 44 | |||||||||||
| 45 | Just a place to stick various utility functions that are cross cutting concerns. | ||||||||||
| 46 | |||||||||||
| 47 | =head1 SUBROUTINES | ||||||||||
| 48 | |||||||||||
| 49 | This package has the following subroutines for EXPORT | ||||||||||
| 50 | |||||||||||
| 51 | =head2 escape_javascript | ||||||||||
| 52 | |||||||||||
| 53 | escape_javascript($string); | ||||||||||
| 54 | |||||||||||
| 55 | Escapes a string so it can be used in a javascript string. This is a wrapper around | ||||||||||
| 56 | The same method from L<Valiant::HTML::Util::TagBuilder/escape_javascript>. | ||||||||||
| 57 | |||||||||||
| 58 | Basically this escapes ' and " and \ and newlines and a few other neaten up so that you can | ||||||||||
| 59 | use a string as a javascript value. Helps with injection attackes (but isn't everything | ||||||||||
| 60 | you need). | ||||||||||
| 61 | =head1 SEE ALSO | ||||||||||
| 62 | |||||||||||
| 63 | L<Valiant> | ||||||||||
| 64 | |||||||||||
| 65 | =head1 AUTHOR | ||||||||||
| 66 | |||||||||||
| 67 | See L<Valiant> | ||||||||||
| 68 | |||||||||||
| 69 | =head1 COPYRIGHT & LICENSE | ||||||||||
| 70 | |||||||||||
| 71 | See L<Valiant> | ||||||||||
| 72 | |||||||||||
| 73 | =cut | ||||||||||