| 1 | package Template::EmbeddedPerl::Utils; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 25 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 25 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 25 | ▶ | use Exporter 'import'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 25 | ▶ | use URI::Escape (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 25 | ▶ | use JSON::MaybeXS; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 25 | our @EXPORT_OK = qw( | |||||||||||||||||||||||||||
| 10 | diagnostic_source_label | ||||||||||||||||||||||||||||
| 11 | normalize_linefeeds | ||||||||||||||||||||||||||||
| 12 | uri_escape | ||||||||||||||||||||||||||||
| 13 | escape_javascript | ||||||||||||||||||||||||||||
| 14 | decorate_render_error | ||||||||||||||||||||||||||||
| 15 | generate_error_message | ||||||||||||||||||||||||||||
| 16 | ); | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | # uri_escape is a function from URI::Escape | ||||||||||||||||||||||||||||
| 19 | # it is used to escape the uri string. | ||||||||||||||||||||||||||||
| 20 | # uri_escape('http://www.google.com') => 'http%3A%2F%2Fwww.google.com' | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 0 | ▶ | sub uri_escape { | ||||||||||||||||||||||||||
|
Subroutine
uri_escape: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 23 | 0 | my ($string) = @_; | |||||||||||||||||||||||||||
| 24 | 0 | return URI::Escape::uri_escape($string); | |||||||||||||||||||||||||||
| 25 | } | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | # normalized the line endings to \n from mac and windows format. | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 364 | ▶ | sub normalize_linefeeds { | ||||||||||||||||||||||||||
|
Subroutine
normalize_linefeeds: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 30 | 364 | my ($template) = @_; | |||||||||||||||||||||||||||
| 31 | 364 | $template =~ s/\r\n/\n/g; | |||||||||||||||||||||||||||
| 32 | 364 | $template =~ s/\r/\n/g; | |||||||||||||||||||||||||||
| 33 | 364 | return $template; | |||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | # Create a JSON encoder | ||||||||||||||||||||||||||||
| 37 | 25 | my $json = JSON::MaybeXS->new(utf8 => 0, ascii => 1, allow_nonref => 1); | |||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | # Define the escape_javascript function | ||||||||||||||||||||||||||||
| 40 | 7 | ▶ | sub escape_javascript { | ||||||||||||||||||||||||||
|
Subroutine
escape_javascript: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 41 | 7 | my ($javascript) = @_; | |||||||||||||||||||||||||||
| 42 | 7 | ▶ | return '' unless defined $javascript; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | # Encode the string as a JSON string | ||||||||||||||||||||||||||||
| 45 | 7 | my $escaped = $json->encode($javascript); | |||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | # Remove the surrounding quotes added by JSON encoding | ||||||||||||||||||||||||||||
| 48 | 7 | $escaped =~ s/^"(.*)"$/$1/; | |||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | # Escape additional characters not handled by JSON encoding | ||||||||||||||||||||||||||||
| 51 | 7 | $escaped =~ s/`/\\`/g; # Escape backticks | |||||||||||||||||||||||||||
| 52 | 7 | $escaped =~ s/\$/\\\$/g; # Escape dollar signs | |||||||||||||||||||||||||||
| 53 | 7 | $escaped =~ s/'/\\'/g; # Escape single quotes | |||||||||||||||||||||||||||
| 54 | 7 | $escaped =~ s{</}{<\\/}g; # Prevent closing an enclosing script element | |||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 7 | return $escaped; | |||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 808 | ▶ | sub diagnostic_source_label { | ||||||||||||||||||||||||||
|
Subroutine
diagnostic_source_label: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 60 | 808 | my ($source) = @_; | |||||||||||||||||||||||||||
| 61 | 808 | ▶ | my $label = defined($source) && length("$source") ? "$source" : 'unknown'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $source and length "$source"
MC/DC50%1/2
defined $source and length "$source"
defined $source
length "$source"
Truth table66%2/3
A and B
A= defined $sourceB= length "$source"
| |||||||||||||||||||||||||||||
| 62 | 808 | $label =~ s/(?:\r\n?|\n)+/ /g; | |||||||||||||||||||||||||||
| 63 | 808 | $label =~ tr/"/'/; | |||||||||||||||||||||||||||
| 64 | 808 | $label =~ s/[\x00-\x1f\x7f]/?/g; | |||||||||||||||||||||||||||
| 65 | 808 | return $label; | |||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 103 | ▶ | sub generate_error_message { | ||||||||||||||||||||||||||
|
Subroutine
generate_error_message: called CC=9 SCAR=22.0
Pod
documented
| |||||||||||||||||||||||||||||
| 69 | 103 | my ($msg, $template, $source) = @_; | |||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 103 | ▶ | warn "RAW MESSAGE: [$msg]" if $ENV{DEBUG_TEMPLATE_EMBEDDED_PERL}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 103 | ▶ | return $msg if _has_render_stack($msg); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 76 | $source = diagnostic_source_label($source); | |||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 76 | my $text = ''; | |||||||||||||||||||||||||||
| 78 | 76 | my $has_template_location = 0; | |||||||||||||||||||||||||||
| 79 | 76 | for my $diagnostic_line (split /(?<=\n)/, $msg) { | |||||||||||||||||||||||||||
| 80 | 83 | my ($message, $line) = _template_location($diagnostic_line, $source); | |||||||||||||||||||||||||||
| 81 | 83 | ▶ | if (!defined $line) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | 28 | $text .= $diagnostic_line; | |||||||||||||||||||||||||||
| 83 | 28 | next; | |||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 55 | $has_template_location = 1; | |||||||||||||||||||||||||||
| 87 | 55 | $text .= "$message at $source line $line\n\n"; | |||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 55 | $line--; | |||||||||||||||||||||||||||
| 90 | 55 | ▶ | my $start = $line -1 >= 0 ? $line -1 : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | 55 | ▶ | my $end = $line + 1 < scalar(@$template) ? $line + 1 : scalar(@$template) - 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 92 | 55 | for my $i ($start..$end) { | |||||||||||||||||||||||||||
| 93 | 106 | $text .= "@{[ $i+1 ]}: $template->[$i]\n"; | |||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | 55 | $text .= "\n"; | |||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 76 | ▶ | return $has_template_location ? $text : $msg; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 83 | ▶ | sub _template_location { | ||||||||||||||||||||||||||
|
Subroutine
_template_location: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 102 | 83 | my ($diagnostic_line, $source) = @_; | |||||||||||||||||||||||||||
| 103 | 83 | my $source_or_eval = qr/(?:\Q$source\E|\(eval \d+\))/; | |||||||||||||||||||||||||||
| 104 | 83 | my ($message, $line); | |||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 83 | ▶ | while ($diagnostic_line =~ /\s+at\s+$source_or_eval\s+line\s+(\d+)(?:\.\n?\z|,\s+at\s+EOF\n?\z)/g) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 107 | 55 | ($message, $line) = (substr($diagnostic_line, 0, $-[0]), $1); | |||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 83 | return ($message, $line); | |||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 111 | ▶ | sub decorate_render_error { | ||||||||||||||||||||||||||
|
Subroutine
decorate_render_error: called CC=8 SCAR=21.4
Pod
undocumented
| |||||||||||||||||||||||||||||
| 114 | 111 | my ($error, $stack) = @_; | |||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 111 | ▶ | return $error if _has_render_stack($error); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 117 | 84 | ▶ | return $error unless $stack && @$stack; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$stack and @$stack
MC/DC0%0/2
$stack and @$stack
$stack
@$stack
Truth table33%1/3
A and B
A= $stackB= @$stack
| |||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 84 | ▶ | my $separator = $error =~ /\n\z/ ? "\n" : "\n\n"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 120 | 84 | my $render_stack = "Render stack:\n"; | |||||||||||||||||||||||||||
| 121 | 84 | for my $entry (@$stack) { | |||||||||||||||||||||||||||
| 122 | 114 | my $kind = $entry->{kind}; | |||||||||||||||||||||||||||
| 123 | 114 | my $identifier = $entry->{identifier}; | |||||||||||||||||||||||||||
| 124 | my $source = defined($entry->{source}) && length($entry->{source}) | ||||||||||||||||||||||||||||
| 125 | ? $entry->{source} | ||||||||||||||||||||||||||||
| 126 | 114 | ▶ | : 'unknown'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $entry->{"source"} and length $entry->{"source"}
MC/DC50%1/2
defined $entry->{"source"} and length $entry->{"source"}
defined $entry->{"source"}
length $entry->{"source"}
Truth table66%2/3
A and B
A= defined $entry->{"source"}B= length $entry->{"source"}
| |||||||||||||||||||||||||||||
| 127 | 114 | $render_stack .= " $kind $identifier ($source)\n"; | |||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 84 | return $error . $separator . $render_stack; | |||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 214 | ▶ | sub _has_render_stack { | ||||||||||||||||||||||||||
|
Subroutine
_has_render_stack: called CC=2 SCAR=7.1
| |||||||||||||||||||||||||||||
| 134 | 214 | my ($error) = @_; | |||||||||||||||||||||||||||
| 135 | 214 | ▶ | return defined($error) | ||||||||||||||||||||||||||
|
Condition66%2/3
defined $error && $error =~ /(?:\A|\n)Render stack:\n(?: [^\n]+(?:\n|\z))+\z/
MC/DC50%1/2
defined $error && $error =~ /(?:\A|\n)Render stack:\n(?: [^\n]+(?:\n|\z))+\z/
defined $error
$error =~ /(?:\A|\n)Render stack:\n(?: [^\n]+(?:\n|\z))+\z/
Truth table66%2/3
A && B
A= defined $errorB= $error =~ /(?:\A|\n)Render stack:\n(?: [^\n]+(?:\n|\z))+\z/
| |||||||||||||||||||||||||||||
| 136 | && $error =~ /(?:\A|\n)Render stack:\n(?: [^\n]+(?:\n|\z))+\z/; | ||||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | 25 | 1; | |||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | =head1 NAME | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | Template::EmbeddedPerl::Utils - Utility functions for Template::EmbeddedPerl | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | This module provides utility functions for L<Template::EmbeddedPerl>. It is not intended to be used directly. | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | =head1 EXPORTS | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | =head2 normalize_linefeeds | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | my $normalized = normalize_linefeeds($template); | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | Normalize the line endings to \n from mac and windows format. | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | =head2 uri_escape | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | my $escaped = uri_escape($string); | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | Escape the uri string. | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | =head2 escape_javascript | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | my $escaped = escape_javascript($javascript); | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | Escape a value so that it can be embedded in a JavaScript string. This escapes quotes, | ||||||||||||||||||||||||||||
| 169 | backticks, dollar signs, JSON control characters, and closing HTML tags. It is not a | ||||||||||||||||||||||||||||
| 170 | general JavaScript sanitizer and does not make untrusted JavaScript code safe to execute. | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | =head2 generate_error_message | ||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | my $error_message = generate_error_message($msg, $template, $source); | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | Generate an error message. | ||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | L<Template::EmbeddedPerl> | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | See L<Template::EmbeddedPerl> | ||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | =head1 COPYRIGHT & LICENSE | ||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | See L<Template::EmbeddedPerl> | ||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | =cut | ||||||||||||||||||||||||||||