Subject: | ESCAPE=JS for compiled templates |
This is needed to compile templates with newer ESCAPE=JS construct.
I've been trying to send this patch by mail but in vain.
--- t/templates/jsescape.tmpl.orig Thu Sep 16 15:55:22 2004
+++ t/templates/jsescape.tmpl Thu Sep 16 15:55:43 2004
@@ -0,0 +1,2 @@
+Some JS escaped stuff:
+<TMPL_VAR ESCAPE=JS STUFF>
--- t/10escape.t.orig Thu Sep 16 16:48:26 2004
+++ t/10escape.t Thu Sep 16 16:49:19 2004
@@ -1,4 +1,4 @@
-use Test::More tests => 4;
+use Test::More tests => 5;
use HTML::Template::JIT;
my $debug = 0;
@@ -34,6 +34,18 @@
);
$output = $template->output;
like($output, qr/Some URL escaped stuff:/);
+
+# test js-escaped var
+$template = HTML::Template::JIT->new(
+ filename => 'jsescape.tmpl',
+ path => ['t/templates'],
+ jit_path => 't/jit_path',
+ jit_debug => $debug,
+ );
+$template->param(STUFF => qq{1st line w/ "quotes"\n2nd line with 'apostrophes'\n\r3rd line \\s\\i\\mple});
+$output = $template->output;
+my $res = q{1st line w/ \\"quotes\\"\\n2nd line with \\'apostrophes\\'\\n\\r3rd line \\\\s\\\\i\\\\mple};
+like($output, qr/\Q$res/);
# test 8bit char in urlescaped var
$template = HTML::Template::JIT->new(
--- JIT/Compiler.pm.orig Thu Sep 16 16:07:05 2004
+++ JIT/Compiler.pm Thu Sep 16 16:37:30 2004
@@ -263,6 +263,8 @@
$do_escape = 'HTML';
} elsif ($type eq 'HTML::Template::URLESCAPE') {
$do_escape = 'URL';
+ } elsif ($type eq 'HTML::Template::JSESCAPE') {
+ $do_escape = 'JS';
} elsif ($type eq 'HTML::Template::NOOP') {
# noop
} else {
@@ -494,6 +496,27 @@
sprintf(buf, "%%%02X", c);
sv_insert(temp_sv, len, 1, buf, 3);
len += 2;
+ }
+END
+ } elsif ($escape eq 'JS') {
+ push @code, <<'END';
+ switch (c) {
+ case '\\':
+ case '\'':
+ case '"':
+ sprintf(buf, "\\%c", c);
+ sv_insert(temp_sv, len, 1, buf, 2);
+ len += 1;
+ break;
+ case '\n':
+ sprintf(buf, "\\n");
+ sv_insert(temp_sv, len, 1, buf, 2);
+ len += 1;
+ break;
+ case '\r':
+ sprintf(buf, "\\r");
+ sv_insert(temp_sv, len, 1, buf, 2);
+ len += 1;
}
END