Re: option to turn ESCAPE=HTML on by default

Alex Kapranoff <[email protected]>
Newsgroups gmane.comp.lang.perl.modules.html-template
Organization Inner Mongolia
Message-ID <[email protected]>
* Roger Burton West <[email protected]> [October 17 2005, 07:10]:
> >my $template = HTML::Template->new(filename=>'filename.tmpl', 
> >html_escape=>1);
> 
> Orthogonality, please:
> escape => 'html'
> 
> just as we have ESCAPE=HTML in the templates.

One name, one function, please :)
default_escape => 'html'.

Patch below, with tests.

diff -ruN /tmp/HTML-Template-2.7/Template.pm HTML-Template-2.7/Template.pm
--- /tmp/HTML-Template-2.7/Template.pm	Fri Jun 18 21:42:06 2004
+++ HTML-Template-2.7/Template.pm	Mon Oct 17 14:43:36 2005
@@ -955,6 +955,7 @@
                no_includes => 0,
                case_sensitive => 0,
                filter => [],
+               default_template => undef,
               );
   
   # load in options supplied to new()
@@ -1076,6 +1077,12 @@
     $self->{cache} = \%cache;
   }
 
+  if ($options->{default_escape}) {
+    unless ($options->{default_escape} =~ s/^(html|url|js)$/uc($1)/ie) {
+      croak("Wrong default_escape specified: \"$options->{default_escape}\".");
+    }
+  }
+
   print STDERR "### HTML::Template Memory Debug ### POST CACHE INIT ", $self->{proc_mem}->size(), "\n"
     if $options->{memory_debug};
 
@@ -1952,7 +1959,7 @@
 
       $which = uc($1); # which tag is it
 
-      $escape = defined $5 ? $5 : defined $15 ? $15 : 0; # escape set?
+      $escape = defined $5 ? $5 : defined $15 ? $15 : defined $options->{default_escape} ? $options->{default_escape} : 0; # escape set?
       
       # what name for the tag?  undef for a /tag at most, one of the
       # following three will be defined
diff -ruN /tmp/HTML-Template-2.7/t/99-old-test-pl.t HTML-Template-2.7/t/99-old-test-pl.t
--- /tmp/HTML-Template-2.7/t/99-old-test-pl.t	Fri Jun 18 21:34:59 2004
+++ HTML-Template-2.7/t/99-old-test-pl.t	Mon Oct 17 14:54:18 2005
@@ -795,7 +795,7 @@
 ok($output =~ /I AM INNER 2/);
 
 # test javascript escaping
-$template = $template = HTML::Template->new(path => ['templates'],
+$template = HTML::Template->new(path => ['templates'],
                                             filename => 'js.tmpl');
 $template->param(msg => qq{"He said 'Hello'.\n\r"});
 $output = $template->output();
@@ -807,3 +807,24 @@
 };
 like($@, qr/empty filename/);
 
+# test default escaping
+$template = HTML::Template->new(path => ['templates'],
+                                            filename => 'default_escape.tmpl',
+                                            default_escape => 'UrL');
+$template->param(STUFF => q{Joined with space});
+$output = $template->output();
+is($output, q{Joined%20with%20space} . "\n");
+
+$template = HTML::Template->new(path => ['templates'],
+                                            filename => 'default_escape.tmpl',
+                                            default_escape => 'html');
+$template->param(STUFF => q{Joined&with"cruft});
+$output = $template->output();
+is($output, q{Joined&amp;with&quot;cruft} . "\n");
+
+eval {
+$template = HTML::Template->new(path => ['templates'],
+                                            filename => 'default_escape.tmpl',
+                                            default_escape => 'wml');
+};
+like($@, qr/Wrong default_escape/);
diff -ruN /tmp/HTML-Template-2.7/templates/default_escape.tmpl HTML-Template-2.7/templates/default_escape.tmpl
--- /tmp/HTML-Template-2.7/templates/default_escape.tmpl	Thu Jan  1 03:00:00 1970
+++ HTML-Template-2.7/templates/default_escape.tmpl	Mon Oct 17 14:47:39 2005
@@ -0,0 +1 @@
+<TMPL_VAR STUFF>

-- 
Alex Kapranoff,
$n=["1another7Perl213Just3hacker49"=~/\d|\D*/g];
$$n[0]={grep/\d/,@$n};print"@$n{1..4}\n"


-------------------------------------------------------
This SF.Net email is sponsored by:
Power Architecture Resource Center: Free content, downloads, discussions,
and more. http://solutions.newsforge.com/ibmarch.tmpl
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.