| 1 | package DOM::Tiny::Entities; | ||||||||||||||
| 2 | |||||||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | 2 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | 2 | ▶ | use Exporter 'import'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | |||||||||||||||
| 7 | 2 | our $VERSION = '0.003'; | |||||||||||||
| 8 | |||||||||||||||
| 9 | 2 | our @EXPORT_OK = qw(html_escape html_unescape); | |||||||||||||
| 10 | |||||||||||||||
| 11 | # To generate a new HTML entity table run this command | ||||||||||||||
| 12 | # perl examples/entities.pl | ||||||||||||||
| 13 | 2 | my %ENTITIES; | |||||||||||||
| 14 | 2 | for my $line (split "\n", join('', <DATA>)) { | |||||||||||||
| 15 | 4250 | ▶ | next unless $line =~ /^(\S+)\s+U\+(\S+)(?:\s+U\+(\S+))?/; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 16 | 4250 | ▶ | $ENTITIES{$1} = defined $3 ? (chr(hex $2) . chr(hex $3)) : chr(hex $2); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 17 | } | ||||||||||||||
| 18 | |||||||||||||||
| 19 | # Characters that should be escaped in HTML/XML | ||||||||||||||
| 20 | 2 | my %ESCAPE = ( | |||||||||||||
| 21 | '&' => '&', | ||||||||||||||
| 22 | '<' => '<', | ||||||||||||||
| 23 | '>' => '>', | ||||||||||||||
| 24 | '"' => '"', | ||||||||||||||
| 25 | '\'' => ''' | ||||||||||||||
| 26 | ); | ||||||||||||||
| 27 | |||||||||||||||
| 28 | sub html_escape { | ||||||||||||||
| 29 | 425 | ▶ | my $str = shift; | ||||||||||||
|
Subroutine
html_escape: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 30 | 425 | $str =~ s/([&<>"'])/$ESCAPE{$1}/ge; | |||||||||||||
| 31 | 425 | return $str; | |||||||||||||
| 32 | } | ||||||||||||||
| 33 | |||||||||||||||
| 34 | sub html_unescape { | ||||||||||||||
| 35 | 33935 | ▶ | my $str = shift; | ||||||||||||
|
Subroutine
html_unescape: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 36 | 33935 | $str =~ s/&(?:\#((?:[0-9]{1,7}|x[0-9a-fA-F]{1,6}));|(\w+;))/_decode($1, $2)/ge; | |||||||||||||
| 37 | 33935 | return $str; | |||||||||||||
| 38 | } | ||||||||||||||
| 39 | |||||||||||||||
| 40 | sub _decode { | ||||||||||||||
| 41 | 28 | ▶ | my ($point, $name) = @_; | ||||||||||||
|
Subroutine
_decode: called CC=4 SCAR=13.9
| |||||||||||||||
| 42 | |||||||||||||||
| 43 | # Code point | ||||||||||||||
| 44 | 28 | ▶ | return chr($point !~ /^x/ ? $point : hex $point) unless defined $name; | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 45 | |||||||||||||||
| 46 | # Named character reference | ||||||||||||||
| 47 | 19 | ▶ | return exists $ENTITIES{$name} ? $ENTITIES{$name} : "&$name"; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 48 | } | ||||||||||||||
| 49 | |||||||||||||||
| 50 | 2 | 1; | |||||||||||||
| 51 | |||||||||||||||
| 52 | =encoding utf8 | ||||||||||||||
| 53 | |||||||||||||||
| 54 | =head1 NAME | ||||||||||||||
| 55 | |||||||||||||||
| 56 | DOM::Tiny::Entities - Escape or unescape HTML entities in strings | ||||||||||||||
| 57 | |||||||||||||||
| 58 | =head1 SYNOPSIS | ||||||||||||||
| 59 | |||||||||||||||
| 60 | use DOM::Tiny::Entities qw(html_escape html_unescape); | ||||||||||||||
| 61 | |||||||||||||||
| 62 | my $str = 'foo & bar'; | ||||||||||||||
| 63 | $str = html_unescape $str; # "foo & bar" | ||||||||||||||
| 64 | $str = html_escape $str; # "foo & bar" | ||||||||||||||
| 65 | |||||||||||||||
| 66 | =head1 DESCRIPTION | ||||||||||||||
| 67 | |||||||||||||||
| 68 | L<DOM::Tiny::Entities> contains functions for escaping and unescaping HTML | ||||||||||||||
| 69 | entities for L<DOM::Tiny>, based on functions from L<Mojo::Util>. All functions | ||||||||||||||
| 70 | are exported on demand. | ||||||||||||||
| 71 | |||||||||||||||
| 72 | =head1 FUNCTIONS | ||||||||||||||
| 73 | |||||||||||||||
| 74 | =head2 html_escape | ||||||||||||||
| 75 | |||||||||||||||
| 76 | my $escaped = html_escape $str; | ||||||||||||||
| 77 | |||||||||||||||
| 78 | Escape unsafe characters C<&>, C<< < >>, C<< > >>, C<">, and C<'> in string. | ||||||||||||||
| 79 | |||||||||||||||
| 80 | html_escape '<div>'; # "<div>" | ||||||||||||||
| 81 | |||||||||||||||
| 82 | =head2 html_unescape | ||||||||||||||
| 83 | |||||||||||||||
| 84 | my $str = html_unescape $escaped; | ||||||||||||||
| 85 | |||||||||||||||
| 86 | Unescape all HTML entities in string, according to the | ||||||||||||||
| 87 | L<HTML Living Standard|https://html.spec.whatwg.org/#named-character-references-table>. | ||||||||||||||
| 88 | |||||||||||||||
| 89 | html_unescape '<div> # "<div>" | ||||||||||||||
| 90 | |||||||||||||||
| 91 | =head1 BUGS | ||||||||||||||
| 92 | |||||||||||||||
| 93 | Report any issues on the public bugtracker. | ||||||||||||||
| 94 | |||||||||||||||
| 95 | =head1 AUTHOR | ||||||||||||||
| 96 | |||||||||||||||
| 97 | Dan Book <dbook@cpan.org> | ||||||||||||||
| 98 | |||||||||||||||
| 99 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||
| 100 | |||||||||||||||
| 101 | This software is Copyright (c) 2015 by Dan Book. | ||||||||||||||
| 102 | |||||||||||||||
| 103 | This is free software, licensed under: | ||||||||||||||
| 104 | |||||||||||||||
| 105 | The Artistic License 2.0 (GPL Compatible) | ||||||||||||||
| 106 | |||||||||||||||
| 107 | =head1 SEE ALSO | ||||||||||||||
| 108 | |||||||||||||||
| 109 | L<HTML::Entities> | ||||||||||||||
| 110 | |||||||||||||||
| 111 | =cut | ||||||||||||||
| 112 | |||||||||||||||
| 113 | __DATA__ | ||||||||||||||