Re: [htmltmpl] option to turn ESCAPE=HTML on by default
Brought to you by:
samtregar
From: Alex K. <ka...@ra...> - 2005-10-17 10:56:02
|
* Roger Burton West <ro...@fi...> [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&with"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" |