Skip to content

Commit

Permalink
merge branch 'pr/10'
Browse files Browse the repository at this point in the history
 - Use XML::Writer when creating tags to escape quotes. (GH#10, Lisa Hare)

Fixes #10
  • Loading branch information
yanick committed Feb 25, 2017
2 parents d5c2f75 + 25e8281 commit 7a12cf7
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 11 deletions.
1 change: 1 addition & 0 deletions Changes
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Revision history for Template-Caribou
[ DOCUMENTATION ]

[ ENHANCEMENTS ]
- Use XML::Writer when creating tags to escape quotes. (GH#10, Lisa Hare)

[ NEW FEATURES ]

Expand Down
1 change: 1 addition & 0 deletions MANIFEST
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ t/lib/UseCase/Two.pm
t/lib/WithDirs/One.pm
t/lib/WithDirs/One/alpha.bou
t/render.t
t/tag-attributes.t
t/tags.t
t/tags_extended.t
t/tags_html.t
Expand Down
32 changes: 21 additions & 11 deletions lib/Template/Caribou/Tags.pm
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use List::AllUtils qw/ pairmap pairgrep /;

use parent 'Exporter::Tiny';
use experimental 'signatures', 'postderef';

use XML::Writer;

our @EXPORT_OK = qw/ render_tag mytag attr /;

Expand Down Expand Up @@ -92,7 +92,7 @@ sub render_tag {

# need to use the object for calls to 'show'
my $bou = $Template::Caribou::TEMPLATE || Moose::Meta::Class->create_anon_class(
roles => [ 'Template::Caribou::Role' ]
roles => [ 'Template::Caribou::Role' ]
)->new_object;

local %_;
Expand All @@ -114,30 +114,40 @@ sub render_tag {
$inner = $_;
}

my $attrs = join ' ', '',
pairmap { ( qq{$a="$b"} ) x (length $b > 0) }
# Setting UNSAFE here so that the inner can be written with raw
# as we don't want inner to be escaped as it is already escaped
my $writer = XML::Writer->new(OUTPUT => 'self', UNSAFE => 1);
my @attributes = pairmap { ( $a => $b ) x (length $b > 0) }
map {
$_ => ref $_{$_}
? join ' ', sort { $a cmp $b } pairmap { $a } pairgrep { $b } $_{$_}->%*
: $_{$_}
} grep { defined $_{$_} } sort keys %_;
}
grep { defined $_{$_} }
sort keys %_;

no warnings qw/ uninitialized /;

my $prefix = !!$Template::Caribou::TAG_INDENT_LEVEL
my $prefix = !!$Template::Caribou::TAG_INDENT_LEVEL
&& "\n" . ( ' ' x $Template::Caribou::TAG_INDENT_LEVEL );

my $output = length($inner)
? Template::Caribou::String->new( "$prefix<${tag}$attrs>$inner$prefix</$tag>" )
: Template::Caribou::String->new( "$prefix<${tag}$attrs />" )
;
if (length($inner)) {
$writer->startTag($tag, @attributes);
$writer->raw("$inner$prefix");
$writer->endTag($tag);
}
else {
$writer->emptyTag($tag, @attributes);
}

my $output = Template::Caribou::String->new( $prefix . $writer->to_string() );

return print_raw( $output );
}

sub print_raw($text) {
print ::RAW $text;
return $text;
return $text;
}

1;
16 changes: 16 additions & 0 deletions t/tag-attributes.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use strict;
use warnings;

use Test::More tests => 3;

use Template::Caribou::Tags qw/ render_tag attr /;

local *::RAW;
open ::RAW, '>', \my $raw;

unlike render_tag( 'div', sub { attr stuff => '"' } ) => qr/"""/,
'attribute with double quote';
like render_tag( 'div', sub { attr stuff => "'" } ) => qr/"'"/,
'attribute with single quote';
unlike render_tag( 'div', sub { attr stuff => q{'"} }) => qr/"'""/,
'attribute with both';

0 comments on commit 7a12cf7

Please sign in to comment.