#!perl -w
use strict;
use Test::More;
use Text::Xslate qw(html_builder mark_raw);
sub custom_html_escape { # for html_escape()
my $s = shift;
return $s if ref $s;
my %h = (
'<' => '<',
'>' => '>',
q{'} => ''',
q{"} => '"',
# '&' => '&', # Don't espcae &. allowing to use character-entitfy-references(like 'hearts)
);
$s =~ s/(.)/$h{$1} or $1/xmsge;
$s;
}
sub custom_html_filter { # for html()
my $s = shift;
return $s if ref $s;
my %h = (
'<' => '<',
'>' => '>',
q{'} => ''',
q{"} => '"',
# '&' => '&', # Don't espcae &. allowing to use character-entitfy-references(like 'hearts)
);
$s =~ s/(.)/$h{$1} or $1/xmsge;
mark_raw($s);
}
note 'override html_escape()';
{
my $tx = Text::Xslate->new(
cache => 0,
verbose => 2,
warn_handler => sub { die @_ },
function => {
html_escape => \&custom_html_escape
},
);
is $tx->render_string(q{
<: '<^_^>♥' :>
}),
'<^_^>♥
';
is $tx->render_string(q{<: $foo :>
}, { foo => '<^_^>♥' }),
'<^_^>♥
';
is $tx->render_string(q{<: '' :>}),
'<div>';
is $tx->render_string(q{<: '
' | raw :>}),
'
';
is $tx->render_string(q{<: '
' | html :>}),
'<div>';
}
note 'override html()';
{
my $tx = Text::Xslate->new(
cache => 0,
verbose => 2,
warn_handler => sub { die @_ },
function => {
html => \&custom_html_filter
},
);
is $tx->render_string(q{
<: '<^_^>♥' :>
}),
'
<^_^>♥
';
is $tx->render_string(q{
<: $foo :>
}, { foo => '<^_^>♥' }),
'
<^_^>♥
';
is $tx->render_string(q{<: '
' :>}),
'<div>';
is $tx->render_string(q{<: '
' | raw :>}),
'
';
is $tx->render_string(q{<: '
' | html :>}),
'<div>';
is $tx->render_string(q{
<: $foo | html :>
}, { foo => '<^_^>♥' }),
'
<^_^>♥
' or die 'stop';
}
note 'override html_escape() with type=text';
{
my $tx_no_autoescape = Text::Xslate->new(
cache => 0,
verbose => 2,
warn_handler => sub { die @_ },
type => 'text',
function => {
html_escape => \&custom_html_escape
},
);
is $tx_no_autoescape->render_string(q{<: '
' :>}),
'
';
is $tx_no_autoescape->render_string(q{<: '
' | raw :>}),
'
';
is $tx_no_autoescape->render_string(q{<: '
' | html :>}),
'<div>';
}
done_testing;