Skip to content

Commit

Permalink
added escape attribute to Mojo::Template
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Oct 18, 2012
1 parent 0eb2e5f commit 43a8a5a
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 16 deletions.
3 changes: 2 additions & 1 deletion Changes
@@ -1,5 +1,6 @@

3.49 2012-10-18
3.49 2012-10-19
- Added escape attribute to Mojo::Template.
- Improved documentation.

3.48 2012-10-16
Expand Down
30 changes: 17 additions & 13 deletions lib/Mojo/Template.pm
Expand Up @@ -14,6 +14,7 @@ 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 All @@ -22,18 +23,6 @@ has tag_start => '<%';
has tag_end => '%>';
has tree => sub { [] };

# Escape helper
my $ESCAPE = <<'EOF';
no warnings 'redefine';
sub _escape {
return $_[0] if ref $_[0] eq 'Mojo::ByteStream';
no warnings 'uninitialized';
Mojo::Util::xml_escape "$_[0]";
}
use Mojo::Base -strict;
EOF
$ESCAPE =~ s/\n//g;

sub build {
my $self = shift;

Expand Down Expand Up @@ -105,9 +94,15 @@ 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;
$escape .= "} use Mojo::Base -strict;";

# Wrap lines
my $first = $lines[0] ||= '';
$lines[0] = 'package ' . $self->namespace . "; $ESCAPE ";
$lines[0] = 'package ' . $self->namespace . ";$escape";
$lines[0] .= "sub { my \$_M = ''; " . $self->prepend . "; do { $first";
$lines[-1] .= $self->append . "; \$_M; } };";

Expand Down Expand Up @@ -529,6 +524,15 @@ Compiled template code.
Encoding used for template files.
=head2 C<escape>
my $code = $mt->escape;
$mt = $mt->escape('Mojo::Util::html_escape(@_)');
Code used to escape the results of escaped expressions. 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
7 changes: 5 additions & 2 deletions t/mojolicious/twinkle_lite_app.t
@@ -1,5 +1,7 @@
use Mojo::Base -strict;

use utf8;

# Disable IPv6 and libev
BEGIN {
$ENV{MOJO_NO_IPV6} = 1;
Expand All @@ -24,6 +26,7 @@ my $twinkle = {
expression_mark => '*',
line_start => '.',
namespace => 'TwinkleSandBoxTest',
escape => 'Mojo::Util::html_escape(@_)',
prepend => 'my $prepended = $self->config("foo");',
tag_end => '**',
tag_start => '**',
Expand Down Expand Up @@ -87,7 +90,7 @@ $t->get_ok('/')->status_is(200)->header_is('X-Append' => 'bar')

# GET /advanced
$t->get_ok('/advanced')->status_is(200)->header_is('X-Append' => 'bar')
->content_is("&lt;escape me&gt;\n123423");
->content_is("&lt;escape me&gt;&awconint;\n123423");

# GET /docs
$t->get_ok('/docs')->status_is(200)->content_like(qr!<h3>snowman</h3>!);
Expand Down Expand Up @@ -120,7 +123,7 @@ Hello *** $name **!\
test<%= content %>123\
@@ advanced.foo.twinkle
.** '<escape me>'
.** "<escape me>\x{2233}"
. my $numbers = [1 .. 4];
** for my $i (@$numbers) { ***
*** $i ***
Expand Down

0 comments on commit 43a8a5a

Please sign in to comment.