1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
|
use Test::Base;
plan 'no_plan';
use URI;
use Template::Stash::AutoEscaping;
use Template::Stash::AutoEscaping::Escaped::HTML;
my $html_class = Template::Stash::AutoEscaping->class_for('HTML');
sub my_html_filter {
my $text = shift;
if ( ref $text eq $html_class ) {
warn "already escaped: " . $text;
return $text;
}
for ($text) {
s/&/&/g;
s/</</g;
s/>/>/g;
s/"/"/g;
}
return $text;
}
sub as_html {
Template::Stash::AutoEscaping->class_for('HTML')->new_as_escaped( $_[0] );
}
my $stash = Template::Stash::AutoEscaping->new(
escape_type => "HTML", # default => HTML
method_for_raw => "raw", # default => raw
method_for_escape => "escape", # default => escape
die_on_unescaped => 1, # default => 0
);
my $tt = Template->new(
{
STASH => $stash,
FILTERS => { html => \&my_html_filter, },
}
);
filters {
template => ['chomp'],
data => ['chomp'],
expected => ['chomp'],
error => ['eval'],
};
sub templatize {
my $input = $_[0];
}
run {
my $block = shift;
my $error_code = $tt->process( \($block->template), { data => $block->data }, \my $output );
ok (($error_code xor $block->error()),
$block->name . " - Error code and error agree",
);
if (! $block->error())
{
is( "$output", $block->expected, $block->name . ' output ok' );
}
};
__DATA__
=== simple error
--- template
[% data %]
--- data eval
'<One&Two>'
--- expected
--- error
1
=== escape
--- template
[% data.escape %]
--- data eval
'<One&Two>'
--- expected
<One&Two>
--- error
0
=== raw
--- template
[% data.raw %]
--- data eval
'<p>Hello & Welcome!</p>'
--- expected
<p>Hello & Welcome!</p>
--- error
0
|