Skip to content

Commit

Permalink
avoid reference loops ; trim stale documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom Rathborne committed Oct 4, 2011
1 parent bc02c39 commit 835e508
Showing 1 changed file with 17 additions and 13 deletions.
30 changes: 17 additions & 13 deletions lib/Dancer/Plugin/EscapeHTML.pm
Expand Up @@ -41,10 +41,6 @@ You can encode specific bits of data yourself using the C<escape_html> and
C<unescape_html> keywords, or you can enable automatic escaping of all values
passed to the template.
In a future version, it is likely that this automatic escaping can be bypassed
for certain values - probably by providing parameter names/patterns in the
configuration to indicate parameters which should be left alone.
=head1 KEYWORDS
Expand Down Expand Up @@ -101,6 +97,7 @@ The above would exclude token names ending in C<_html> from being escaped.
=cut

my $exclude_pattern;
my %seen;

hook before_template_render => sub {
my $tokens = shift;
Expand All @@ -112,23 +109,30 @@ hook before_template_render => sub {
? qr/$config->{exclude_pattern}/
: undef;

# flush seen cache
%seen = ();

$tokens = _encode($tokens);
};

# Encode values, recursing down into hash/arrayrefs.
# TODO: this will probably choke on circular references
sub _encode {
my $in = shift;
return unless defined $in; # avoid interpolation warnings
if (!ref $in) {
$in = HTML::Entities::encode_entities($in);
} elsif (ref $in eq 'ARRAY') {
$in->[$_] = _encode($in->[$_]) for (0..$#$in);
} elsif (ref $in eq 'HASH') {
while (my($k,$v) = each %$in) {
next if defined $exclude_pattern
&& $k =~ $exclude_pattern;
$in->{$k} = _encode($v);
} else {
next if exists $seen{scalar $in}; # avoid reference loops
$seen{scalar $in} = 1;

if (ref $in eq 'ARRAY') {
$in->[$_] = _encode($in->[$_]) for (0..$#$in);
} elsif (ref $in eq 'HASH') {
while (my($k,$v) = each %$in) {
next if defined $exclude_pattern
&& $k =~ $exclude_pattern;
$in->{$k} = _encode($v);
}
}
}
return $in;
Expand All @@ -149,7 +153,7 @@ David Precious, C<< <davidp at preshweb.co.uk> >>
=head1 ACKNOWLEDGEMENTS
Tom Rathborne (trathborne)
Tom Rathborne C<< <tom.rathborne at gmail.com> >>
=head1 LICENSE AND COPYRIGHT
Expand Down

0 comments on commit 835e508

Please sign in to comment.