--- Template.pm 2004-06-17 21:25:42.000000000 +0200 +++ TemplateJS.pm 2004-06-17 21:25:43.000000000 +0200 @@ -133,6 +133,11 @@ populate a URL. It will do URL escaping, like replacing ' ' with '+' and '/' with '%2F'. +There is also the "ESCAPE=JS" option which may be used for VARs that +need to be placed within a Javascript string. All \n, \r, ' and " characters +are escaped. This is especially useful for building dynamic Javascript menus +which are becoming increasingly popular. + You can assign a default value to a variable with the DEFAULT attribute. For example, this will output "the devil gave me a taco" if the "who" variable is not set. @@ -1737,6 +1742,7 @@ my $NOOP = HTML::Template::NOOP->new(); my $ESCAPE = HTML::Template::ESCAPE->new(); + my $JSESCAPE = HTML::Template::JSESCAPE->new(); my $URLESCAPE = HTML::Template::URLESCAPE->new(); # all the tags that need NAMEs: @@ -1815,6 +1821,9 @@ (?:[Uu][Rr][Ll]) | (?:"[Uu][Rr][Ll]") | (?:'[Uu][Rr][Ll]') | + (?:[Jj][Ss]) | + (?:"[Jj][Ss]") | + (?:'[Jj][Ss]') | ) # $5 => ESCAPE on ) )* # allow multiple ESCAPEs @@ -1882,6 +1891,9 @@ (?:[Uu][Rr][Ll]) | (?:"[Uu][Rr][Ll]") | (?:'[Uu][Rr][Ll]') | + (?:[Jj][Ss]) | + (?:"[Jj][Ss]") | + (?:'[Jj][Ss]') | ) # $15 => ESCAPE on ) )* # allow multiple ESCAPEs @@ -1967,6 +1979,8 @@ if ($escape) { if ($escape =~ /^"?[Uu][Rr][Ll]"?$/) { push(@pstack, $URLESCAPE); + } elsif ($escape =~ /^"?[Jj][Ss]"?$/) { + push(@pstack, $JSESCAPE); } else { push(@pstack, $ESCAPE); } @@ -2677,6 +2691,17 @@ $result .= $_; } next; + } elsif ($type eq 'HTML::Template::JSESCAPE') { + $x++; + *line = \$parse_stack[$x]; + if (defined($$line)) { + $_ = $$line; + s/\\/\\\\/g; + s/(['"])/\\$1/g; + s/\n/\\n/g; + s/\r/\\r/g; + $result .= $_; + } } elsif ($type eq 'HTML::Template::URLESCAPE') { $x++; *line = \$parse_stack[$x]; @@ -2943,6 +2968,14 @@ return $self; } +package HTML::Template::JSESCAPE; +sub new { + my $unused; + my $self = \$unused; + bless($self, $_[0]); + return $self; +} + package HTML::Template::URLESCAPE; sub new { my $unused;