X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FDOM%2FTiny%2FEntities.pm;h=7b76fd418b4fe254725dac67fab06272b232949c;hb=4d7ffd9750b680d72934879b281bce45448807a4;hp=c3a32f8672a1f883494b8c358dfe4d1b7936b3e6;hpb=927f135111d601e084c5a50e2d806bddd5c9cefb;p=catagits%2FDOM-Tiny.git diff --git a/lib/DOM/Tiny/Entities.pm b/lib/DOM/Tiny/Entities.pm index c3a32f8..7b76fd4 100644 --- a/lib/DOM/Tiny/Entities.pm +++ b/lib/DOM/Tiny/Entities.pm @@ -4,9 +4,9 @@ use strict; use warnings; use Exporter 'import'; -our $VERSION = '0.001'; +our $VERSION = '0.003'; -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/&(?:\#((?:[0-9]{1,7}|x[0-9a-fA-F]{1,6}));|(\w+;))/_decode($1, $2)/ge; return $str; } @@ -53,15 +53,15 @@ sub _decode { =head1 NAME -DOM::Tiny::Entities - Encode or decode HTML entities in strings +DOM::Tiny::Entities - Escape or unescape 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_unescape $str; # "foo & bar" - $str = xml_escape $str; # "foo & bar" + $str = html_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.