DOM58/Entities.pm

filter: Statementstmt 100% 40 / 40 Branchbran 92.8% 13 / 14 Conditioncond 95.2% 20 / 21 MC/DCmcdc 80.0% 8 / 10 Subroutinesub 100% 8 / 8 Podpod 100% 3 / 3 totaltotal 95.8% 92 / 96 CC 13 SCAR 21.7
CC
13
Cov
97%
_entity
13.0
BEGIN
1.0
BEGIN
1.0
CRAP 8.8
« DOM58/_CSS.pm ↑ Summary DOM58.pm »
1 package Mojo::DOM58::Entities;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 3 our $VERSION = '3.003';
8  
9 3 our @EXPORT_OK = qw(html_attr_unescape html_escape html_unescape);
10  
11 # To generate a new HTML entity table run this command
12 # perl examples/entities.pl
13 3 my %ENTITIES;
14 3 for my $line (split "\n", join('', <DATA>)) {
15 6693 ▶ next unless $line =~ /^(\S+)\s+U\+(\S+)(?:\s+U\+(\S+))?/;
Branch: 1 branch
BranchTrueFalseTotal
unless $line =~ /^(\S+)\s+U\+(\S+)(?:\s+U\+(\S+))?/ 0 6693 6693
16 6693 ▶ $ENTITIES{$1} = defined $3 ? (chr(hex $2) . chr(hex $3)) : chr(hex $2);
Branch: 1 branch
BranchTrueFalseTotal
defined $3 ? : 279 6414 6693
17 }
18 3 close DATA;
19  
20 # Characters that should be escaped in HTML/XML
21 3 my %ESCAPE = (
22 '&' => '&amp;',
23 '<' => '&lt;',
24 '>' => '&gt;',
25 '"' => '&quot;',
26 '\'' => '&#39;'
27 );
28  
29 # Numeric character references to C1 controls are remapped through windows-1252 (WHATWG HTML)
30 3 my %C1 = (
31 0x80 => 0x20AC,
32 0x82 => 0x201A,
33 0x83 => 0x0192,
34 0x84 => 0x201E,
35 0x85 => 0x2026,
36 0x86 => 0x2020,
37 0x87 => 0x2021,
38 0x88 => 0x02C6,
39 0x89 => 0x2030,
40 0x8A => 0x0160,
41 0x8B => 0x2039,
42 0x8C => 0x0152,
43 0x8E => 0x017D,
44 0x91 => 0x2018,
45 0x92 => 0x2019,
46 0x93 => 0x201C,
47 0x94 => 0x201D,
48 0x95 => 0x2022,
49 0x96 => 0x2013,
50 0x97 => 0x2014,
51 0x98 => 0x02DC,
52 0x99 => 0x2122,
53 0x9A => 0x0161,
54 0x9B => 0x203A,
55 0x9C => 0x0153,
56 0x9E => 0x017E,
57 0x9F => 0x0178
58 );
59  
60 # HTML entities
61 3 my $ENTITY_RE = qr/&(?:\#((?:[0-9]{1,7}|x[0-9a-fA-F]{1,6}));|(\w+[;=]?))/;
62  
63 512 ▶sub html_escape {
Subroutine html_escape: called CC=1 SCAR=0.0
Pod documented
64 512 my $str = shift;
65 512 $str =~ s/([&<>"'])/$ESCAPE{$1}/ge;
66 512 return $str;
67 }
68  
69 33066 ▶sub html_attr_unescape { _html(shift, 1) }
Subroutine html_attr_unescape: called CC=1 SCAR=0.0
Pod documented
70 1461 ▶sub html_unescape { _html(shift, 0) }
Subroutine html_unescape: called CC=1 SCAR=0.0
Pod documented
71  
72 84 ▶sub _entity {
Subroutine _entity: called CC=13 SCAR=25.7
73 84 my ($point, $name, $attr) = @_;
74
75 # Code point
76 84 ▶ unless (defined $name) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $name) 34 50 84
77 34 ▶ my $cp = $point !~ /^x/ ? $point + 0 : hex $point;
Branch: 1 branch
BranchTrueFalseTotal
!($point =~ /^x/) ? : 9 25 34
78 34 ▶ return "\N{U+FFFD}" if $cp == 0 || $cp > 0x10FFFF || ($cp >= 0xD800 && $cp <= 0xDFFF);
Branch: 1 branch
BranchTrueFalseTotal
if $cp == 0 or $cp > 1114111 or $cp >= 55296 and $cp <= 57343 11 23 34
Condition100%9/9
$cp == 0 or $cp > 1114111 or $cp >= 55296 and $cp <= 57343
l!l&&r!l&&!r
4723
$cp == 0 or $cp > 1114111
l!l&&r!l&&!r
2230
$cp >= 55296 and $cp <= 57343
!ll&&!rl&&r
2037
MC/DC100%4/4
$cp == 0 or $cp > 1114111 or $cp >= 55296 and $cp <= 57343
$cp == 0 $cp > 1114111 $cp >= 55296 $cp <= 57343
Truth table100%5/5
A or B or C and D
A= $cp == 0B= $cp > 1114111C= $cp >= 55296D= $cp <= 57343
ABCDresult
1XXX1
01XX1
00111
000X0
00100
79 23 ▶ return chr($C1{$cp} || $cp);
Condition66%2/3
$C1{$cp} || $cp
l!l&&r!l&&!r
6170
MC/DC0%0/2
$C1{$cp} || $cp
$C1{$cp} $cp
Truth table66%2/3
A || B
A= $C1{$cp}B= $cp
ABresult
1X1
011
000
80 }
81
82 # Named character reference
83 50 my $rest = my $last = '';
84 50 ▶ while (length $name) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $name) 88 10 98
85 return $ENTITIES{$name} . reverse $rest
86 88 ▶ if exists $ENTITIES{$name}
Branch: 1 branch
BranchTrueFalseTotal
if exists $ENTITIES{$name} and !$attr || $name =~ /;$/ || !($last =~ /[A-Za-z0-9=]/) 40 48 88
Condition100%9/9
exists $ENTITIES{$name} and !$attr || $name =~ /;$/ || !($last =~ /[A-Za-z0-9=]/)
!ll&&!rl&&r
44440
not $attr or $name =~ /;$/ or not $last =~ /[A-Za-z0-9=]/
l!l&&r!l&&!r
3644
not $attr or $name =~ /;$/
l!l&&r!l&&!r
3068
MC/DC100%4/4
exists $ENTITIES{$name} and !$attr || $name =~ /;$/ || !($last =~ /[A-Za-z0-9=]/)
exists $ENTITIES{$name} not $attr $name =~ /;$/ not $last =~ /[A-Za-z0-9=]/
Truth table100%5/5
A and not(B) or C or not(D)
A= exists $ENTITIES{$name}B= not $attrC= $name =~ /;$/D= not $last =~ /[A-Za-z0-9=]/
ABCDresult
0XXX0
10000
11XX1
101X1
10011
87 && (!$attr || $name =~ /;$/ || $last !~ /[A-Za-z0-9=]/);
88 48 $rest .= $last = chop $name;
89 }
90 10 return '&' . reverse $rest;
91 }
92  
93 34527 ▶sub _html {
Subroutine _html: called CC=1 SCAR=0.0
94 34527 my ($str, $attr) = @_;
95 34527 $str =~ s/$ENTITY_RE/_entity($1, $2, $attr)/geo;
96 34527 return $str;
97 }
98  
99 3 1;
100  
101 =encoding utf8
102  
103 =head1 NAME
104  
105 Mojo::DOM58::Entities - Escape or unescape HTML entities in strings
106  
107 =head1 SYNOPSIS
108  
109 use Mojo::DOM58::Entities qw(html_escape html_unescape);
110
111 my $str = 'foo &amp; bar';
112 $str = html_unescape $str; # "foo & bar"
113 $str = html_escape $str; # "foo &amp; bar"
114  
115 =head1 DESCRIPTION
116  
117 L<Mojo::DOM58::Entities> contains functions for escaping and unescaping HTML
118 entities for L<Mojo::DOM58>, based on functions from L<Mojo::Util>. All
119 functions are exported on demand.
120  
121 =head1 FUNCTIONS
122  
123 =head2 html_attr_unescape
124  
125 my $str = html_attr_unescape $escaped;
126  
127 Same as L</"html_unescape">, but handles special rules from the
128 L<HTML Living Standard|https://html.spec.whatwg.org> for HTML attributes.
129  
130 # "foo=bar&ltest=baz"
131 html_attr_unescape 'foo=bar&ltest=baz';
132  
133 # "foo=bar<est=baz"
134 html_attr_unescape 'foo=bar&lt;est=baz';
135  
136 =head2 html_escape
137  
138 my $escaped = html_escape $str;
139  
140 Escape unsafe characters C<&>, C<< < >>, C<< > >>, C<">, and C<'> in string.
141  
142 html_escape '<div>'; # "&lt;div&gt;"
143  
144 =head2 html_unescape
145  
146 my $str = html_unescape $escaped;
147  
148 Unescape all HTML entities in string, according to the
149 L<HTML Living Standard|https://html.spec.whatwg.org/#named-character-references-table>.
150  
151 html_unescape '&lt;div&gt'; # "<div>"
152  
153 =head1 BUGS
154  
155 Report issues related to the format of this distribution or Perl 5.8 support to
156 the public bugtracker. Any other issues should be reported directly to the
157 upstream L<Mojolicious> issue tracker.
158  
159 =head1 AUTHOR
160  
161 Dan Book <book.d.wit@proton.me>
162  
163 Code and tests adapted from L<Mojo::Util>, a set of utilities by the
164 L<Mojolicious> team.
165  
166 =head1 COPYRIGHT AND LICENSE
167  
168 Copyright (c) 2008-2016 Sebastian Riedel and others.
169  
170 Copyright (c) 2016 Dan Book for adaptation to standalone format.
171  
172 This is free software, licensed under:
173  
174 The Artistic License 2.0 (GPL Compatible)
175  
176 =head1 SEE ALSO
177  
178 L<HTML::Entities>
179  
180 =cut
181  
182 __DATA__
« DOM58/_CSS.pm ↑ Summary DOM58.pm »