From: Dan Book Date: Sat, 7 Nov 2015 05:46:54 +0000 (-0500) Subject: rename xml_escape to html_escape X-Git-Tag: v0.001~20 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FDOM-Tiny.git;a=commitdiff_plain;h=e085469f74acf95e5d60b2fd88b0e7d720710e24 rename xml_escape to html_escape --- diff --git a/lib/DOM/Tiny/Entities.pm b/lib/DOM/Tiny/Entities.pm index c3a32f8..c94982a 100644 --- a/lib/DOM/Tiny/Entities.pm +++ b/lib/DOM/Tiny/Entities.pm @@ -6,7 +6,7 @@ use Exporter 'import'; our $VERSION = '0.001'; -our @EXPORT_OK = qw(html_unescape xml_escape); +our @EXPORT_OK = qw(html_escape html_unescape); # To generate a new HTML entity table run this command # perl examples/entities.pl @@ -16,8 +16,8 @@ for my $line (split "\n", join('', )) { $ENTITIES{$1} = defined $3 ? (chr(hex $2) . chr(hex $3)) : chr(hex $2); } -# Characters that should be escaped in XML -my %XML = ( +# Characters that should be escaped in HTML/XML +my %ESCAPE = ( '&' => '&', '<' => '<', '>' => '>', @@ -25,15 +25,15 @@ my %XML = ( '\'' => ''' ); -sub html_unescape { +sub html_escape { my $str = shift; - $str =~ s/&(?:\#((?:\d{1,7}|x[0-9a-fA-F]{1,6}));|(\w+;))/_decode($1, $2)/ge; + $str =~ s/([&<>"'])/$ESCAPE{$1}/ge; return $str; } -sub xml_escape { +sub html_unescape { my $str = shift; - $str =~ s/([&<>"'])/$XML{$1}/ge; + $str =~ s/&(?:\#((?:\d{1,7}|x[0-9a-fA-F]{1,6}));|(\w+;))/_decode($1, $2)/ge; return $str; } @@ -57,11 +57,11 @@ DOM::Tiny::Entities - Encode or decode HTML entities in strings =head1 SYNOPSIS - use DOM::Tiny::Entities qw(html_unescape xml_escape); + use DOM::Tiny::Entities qw(html_escape html_unescape); my $str = 'foo & bar'; + $str = html_escape $str; # "foo & bar" $str = html_unescape $str; # "foo & bar" - $str = xml_escape $str; # "foo & bar" =head1 DESCRIPTION @@ -71,6 +71,14 @@ are exported on demand. =head1 FUNCTIONS +=head2 html_escape + + my $escaped = html_escape $str; + +Escape unsafe characters C<&>, C<< < >>, C<< > >>, C<">, and C<'> in string. + + html_escape '
'; # "<div>" + =head2 html_unescape my $str = html_unescape $escaped; @@ -80,14 +88,6 @@ L" -=head2 xml_escape - - my $escaped = xml_escape $str; - -Escape unsafe characters C<&>, C<< < >>, C<< > >>, C<">, and C<'> in string. - - xml_escape '
'; # "<div>" - =head1 BUGS Report any issues on the public bugtracker. diff --git a/lib/DOM/Tiny/HTML.pm b/lib/DOM/Tiny/HTML.pm index faec2f5..e58aff6 100644 --- a/lib/DOM/Tiny/HTML.pm +++ b/lib/DOM/Tiny/HTML.pm @@ -2,7 +2,7 @@ package DOM::Tiny::HTML; use strict; use warnings; -use DOM::Tiny::Entities qw(html_unescape xml_escape); +use DOM::Tiny::Entities qw(html_escape html_unescape); use Scalar::Util 'weaken'; use Class::Tiny::Chained 'xml', { tree => sub { ['root'] } }; @@ -196,7 +196,7 @@ sub _render { # Text (escaped) my $type = $tree->[0]; - return xml_escape($tree->[1]) if $type eq 'text'; + return html_escape($tree->[1]) if $type eq 'text'; # Raw text return $tree->[1] if $type eq 'raw'; @@ -225,7 +225,7 @@ sub _render { for my $key (sort keys %{$tree->[2]}) { my $value = $tree->[2]{$key}; $result .= $xml ? qq{ $key="$key"} : " $key" and next unless defined $value; - $result .= qq{ $key="} . xml_escape($value) . '"'; + $result .= qq{ $key="} . html_escape($value) . '"'; } # No children diff --git a/t/entities.t b/t/entities.t index 79fa8cd..e3012d5 100644 --- a/t/entities.t +++ b/t/entities.t @@ -2,7 +2,7 @@ use strict; use warnings; use utf8; use Test::More; -use DOM::Tiny::Entities qw(html_unescape xml_escape); +use DOM::Tiny::Entities qw(html_escape html_unescape); use Encode 'decode'; # html_unescape @@ -28,16 +28,16 @@ is html_unescape('foobar'), 'foobar', 'right HTML unescaped result'; is html_unescape(decode 'UTF-8', 'foo<baz>&"Œ&Foo;'), "foo&\"\x{152}&Foo;", 'right HTML unescaped result'; -# xml_escape -is xml_escape(qq{la\nbar"baz"'yada\n'<la}), +# html_escape +is html_escape(qq{la\nbar"baz"'yada\n'<la}), "la<f>\nbar"baz"'yada\n'&lt;la", - 'right XML escaped result'; + 'right HTML escaped result'; -# xml_escape (UTF-8 with nothing to escape) -is xml_escape('привет'), 'привет', 'right XML escaped result'; +# html_escape (UTF-8 with nothing to escape) +is html_escape('привет'), 'привет', 'right HTML escaped result'; -# xml_escape (UTF-8) -is xml_escape('привет'), 'привет<foo>', - 'right XML escaped result'; +# html_escape (UTF-8) +is html_escape('привет'), 'привет<foo>', + 'right HTML escaped result'; done_testing;