Template-Plugin-JSON-Escape-0.02/0000755000076500007650000000000011575374326016652 5ustar nanto_vinanto_viTemplate-Plugin-JSON-Escape-0.02/META.yml0000664000076500007650000000104011575374326020120 0ustar nanto_vinanto_vi--- #YAML:1.0 name: Template-Plugin-JSON-Escape version: 0.02 abstract: ~ author: [] license: unknown distribution_type: module configure_requires: ExtUtils::MakeMaker: 0 build_requires: ExtUtils::MakeMaker: 0 requires: JSON: 2.12 Template: 2.20 Test::More: 0 no_index: directory: - t - inc generated_by: ExtUtils::MakeMaker version 6.56 meta-spec: url: http://module-build.sourceforge.net/META-spec-v1.4.html version: 1.4 Template-Plugin-JSON-Escape-0.02/MANIFEST0000644000076500007650000000027311575374326020005 0ustar nanto_vinanto_viChanges lib/Template/Plugin/JSON/Escape.pm Makefile.PL MANIFEST This list of files MANIFEST.SKIP t/live.t META.yml Module meta-data (added by MakeMaker) Template-Plugin-JSON-Escape-0.02/Makefile.PL0000644000076500007650000000100311547023456020610 0ustar nanto_vinanto_vi#!/usr/bin/perl use strict; use warnings; use ExtUtils::MakeMaker; WriteMakefile( 'NAME' => 'Template::Plugin::JSON::Escape', 'VERSION_FROM' => 'lib/Template/Plugin/JSON/Escape.pm', 'PREREQ_PM' => { 'Template' => '2.20', 'Test::More' => '0', 'JSON' => '2.12', }, 'INSTALLDIRS' => 'site', 'EXE_FILES' => [], 'PL_FILES' => {} ); unless ( eval { require JSON::XS } ) { warn < undef, args => $args }, $class; my $encode = sub { $self->json_encode( @_ ) }; $context->define_vmethod( $_ => json => $encode ) for qw/hash list scalar/; $context->define_filter( json => \&json_filter ); return $self; } sub json { my $self = shift; return $self->{json} if $self->{json}; my $json = JSON->new->allow_nonref; my $args = $self->{args}; for ( keys %$args ) { $json->$_( $args->{ $_ } ) if $json->can( $_ ); } return $self->{json} = $json; } sub json_encode { my ($self, $value) = @_; json_filter( $self->json->encode( $value ) ); } sub json_decode { my ($self, $value) = @_; $self->json->decode( $value ); } sub json_filter { my $value = shift; $value =~ s!&!\\u0026!g; $value =~ s!!\\u003e!g; $value =~ s!\+!\\u002b!g; $value =~ s!\x{2028}!\\u2028!g; $value =~ s!\x{2029}!\\u2029!g; $value; } 1; __END__ =pod =head1 NAME Template::Plugin::JSON::Escape - Adds a .json vmethod and a json filter. =head1 SYNOPSIS [% USE JSON.Escape( pretty => 1 ) %]; or read in JSON [% USE JSON.Escape %] [% data = JSON.Escape.json_decode(json) %] [% data.thing %] =head1 DESCRIPTION This plugin allows you to embed JSON strings in HTML. In the output, special characters such as C> and C<&> are escaped as C<\uxxxx> to prevent XSS attacks. It also provides decoding function to keep compatibility with L. =head1 FEATURES =head2 USE JSON.Escape Any options on the USE line are passed through to the JSON object, much like L. =head2 json vmethod A C<.json> vmethod converts scalars, arrays and hashes into corresponding JSON strings. [% json_stuct = { foo => 42, bar => [ 1, 2, 3 ] } %] =head2 json filter A C filter escapes C>, C>, C<&>, C<+>, C and C as C<\uxxxx>. In the attribute, you may just use an C filter. [% json_string = '{ "foo": 42, "bar": [ 1, 2, 3 ] }' %] =head2 json_decode method A C method allow you to convert from a JSON string into a corresponding data structure. [% SET json_struct = JSON.Escape.json_decode(json_string) %] [% json_struct.foo | html %] =head1 SEE ALSO L, L, L =head1 VERSION CONTROL L =head1 AUTHOR nanto_vi (TOYAMA Nao) =head1 COPYRIGHT & LICENSE Copyright (c) 2011 nanto_vi (TOYAMA Nao). Copyright (c) 2006, 2008 Infinity Interactive, Yuval Kogman. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. =cut Template-Plugin-JSON-Escape-0.02/Changes0000644000076500007650000000005711575374154020146 0ustar nanto_vinanto_vi0.02 - Escape "+" 0.01 - Initial release Template-Plugin-JSON-Escape-0.02/t/0000755000076500007650000000000011575374326017115 5ustar nanto_vinanto_viTemplate-Plugin-JSON-Escape-0.02/t/live.t0000644000076500007650000000452011542644527020237 0ustar nanto_vinanto_viuse strict; use warnings; use Test::More tests => 14; use Template; use JSON qw/from_json to_json/; BEGIN { use_ok 'Template::Plugin::JSON::Escape'; } { my $vars = { blah => { foo => "bar" }, baz => "ze special string wis some ' qvotes\"", oink => [ 1..3 ], }; my $tt = Template->new; my $ret = $tt->process( \q{ [%~ USE JSON.Escape( pretty => 1 ) ~%] { "blah":[% blah.json %], "baz":[% baz.json %], "oink":[% oink.json %] } }, $vars, \my $out, ); ok $ret, "template processing" or diag $tt->error; like $out, qr/\{\W*foo\W*:\W*bar\W*\}/, "output seems OK"; like $out, qr/\n/, "pretty output"; is_deeply from_json( $out ), $vars, "round tripping"; } { my $warnings = 0; local $SIG{__WARN__} = sub { $warnings++ }; my $tt = Template->new; my $ret = $tt->process( \q{ [%~ USE JSON.Escape ~%] [%~ SET foo = [ 1, 2, 3 ]; foo.json ~%] }, {}, \my $out, ); ok $ret, "template processing" or diag $tt->error; is $warnings, 0, "no warning"; } { # pass JSON to the template my $tt = Template->new; my $ret = $tt->process( \q{ [%~ USE JSON.Escape ~%] [%~ val = JSON.Escape.json_decode(json_string) ~%] [%~ 'ok' IF val.blah.foo == 'bar' ~%] }, { json_string => '{ "blah": { "foo": "bar" }, "oink": [1, 2, 3] }' }, \my $out, ); ok $ret, "template processing" or diag $tt->error; is $out, 'ok', 'match on extract'; } { my $tt = Template->new; my $ret = $tt->process( \q{ [%~ USE JSON.Escape ~%] [%~ data.json ~%] }, { data => { foo => 'bar & baz <3' } }, \my $out, ); ok $ret, "template processing" or diag $tt->error; unlike $out, qr/[<&]/, 'escape special characters'; } { my $data = { foo => 'bar & baz <3' }; my $tt = Template->new; my $ret = $tt->process( \q{ [%~ USE JSON.Escape ~%] [%~ string | json ~%] }, { string => to_json( $data ) }, \my $out, ); ok $ret, "template processing" or diag $tt->error; unlike $out, qr/[<&]/, 'escape special characters by filter'; is_deeply from_json( $out ), $data, 'still valid json' }