Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
replaced escape attribute in Mojo::Template with escape function that…
… can be redefined
  • Loading branch information
kraih committed Oct 19, 2012
1 parent 20a5753 commit 84fea69
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 20 deletions.
2 changes: 1 addition & 1 deletion Changes
@@ -1,6 +1,6 @@

3.49 2012-10-19
- Added escape attribute to Mojo::Template.
- Improved Mojo::Template to allow custom escape functions.
- Improved documentation.
- Improved tests.
- Fixed % escaping bug in Mojo::Parameters. (dmw397)
Expand Down
22 changes: 7 additions & 15 deletions lib/Mojo/Template.pm
Expand Up @@ -14,7 +14,6 @@ has capture_end => 'end';
has capture_start => 'begin';
has comment_mark => '#';
has encoding => 'UTF-8';
has escape => 'Mojo::Util::xml_escape("$_[0]")';
has [qw(escape_mark expression_mark trim_mark)] => '=';
has [qw(line_start replace_mark)] => '%';
has name => 'template';
Expand Down Expand Up @@ -94,10 +93,13 @@ sub build {
}
}

# Escape helper
my $escape = "no warnings 'redefine'; sub _escape {";
$escape .= q{return $_[0] if ref $_[0] eq 'Mojo::ByteStream';};
$escape .= "no warnings 'uninitialized'; @{[$self->escape]} }";
# XML escape function (can be redefined)
my $escape
= q/no warnings 'redefine'; sub escape { Mojo::Util::xml_escape($_[0]) }/;

# Escape function with Mojo::ByteStream support
$escape .= q[sub _escape { no warnings 'uninitialized';];
$escape .= q/ref $_[0] eq 'Mojo::ByteStream' ? $_[0] : escape("$_[0]") }/;

# Wrap lines
my $first = $lines[0] ||= '';
Expand Down Expand Up @@ -523,16 +525,6 @@ Compiled template code.
Encoding used for template files.
=head2 C<escape>
my $code = $mt->escape;
$mt = $mt->escape('Mojo::Util::html_escape("$_[0]")');
Perl code used to escape the results of expressions, defaults to
C<Mojo::Util::xml_escape("$_[0]")>. Note that this code should not contain
newline characters, or line numbers in error messages might end up being
wrong.
=head2 C<escape_mark>
my $mark = $mt->escape_mark;
Expand Down
4 changes: 2 additions & 2 deletions lib/Mojolicious/Guides/Rendering.pod
Expand Up @@ -99,7 +99,7 @@ Perl code, a naive translation could look like this.
$output .= '<ul>';
for my $j (1 .. $i) {
$output .= '<li>';
$output .= xml_escape scalar $j;
$output .= escape scalar $j;
$output .= '</li>';
}
$output .= '</ul>';
Expand Down Expand Up @@ -518,7 +518,7 @@ A naive translation to Perl code could look like this.
my $name = shift;
my $output = '';
$output .= 'Hello ';
$output .= xml_escape scalar $name;
$output .= escape scalar $name;
$output .= '.';
return Mojo::ByteStream->new($output);
}
Expand Down
6 changes: 4 additions & 2 deletions t/mojolicious/twinkle_lite_app.t
Expand Up @@ -16,6 +16,9 @@ use Test::Mojo;
# Custom format
app->renderer->default_format('foo');

# Custom escape function
my $escape = 'sub escape { Mojo::Util::html_escape("$_[0]") }';

# Twinkle template syntax
my $twinkle = {
append => '$self->res->headers->header("X-Append" => $prepended);',
Expand All @@ -26,8 +29,7 @@ my $twinkle = {
expression_mark => '*',
line_start => '.',
namespace => 'TwinkleSandBoxTest',
escape => 'Mojo::Util::html_escape("$_[0]")',
prepend => 'my $prepended = $self->config("foo");',
prepend => qq{my \$prepended = \$self->config("foo"); $escape},
tag_end => '**',
tag_start => '**',
trim_mark => '*'
Expand Down

0 comments on commit 84fea69

Please sign in to comment.