blib/lib/HTML/Entities.pm

filter: Statementstmt 98.0% 49 / 50 Branchbran 83.3% 15 / 18 Conditioncond 66.6% 2 / 3 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 80.0% 4 / 5 Podpod 75.0% 3 / 4 totaltotal 90.2% 74 / 82 CC 9 SCAR 19.0
CC
9
Cov
93%
encode_entities
9.1
Version
2.0
BEGIN
1.0
CRAP 6.7
« blib/lib/HTML/Parser.pm ↑ Summary blib/lib/HTML/LinkExtor.pm »
1 package HTML::Entities;
2  
3 =encoding utf8
4  
5 =head1 NAME
6  
7 HTML::Entities - Encode or decode strings with HTML entities
8  
9 =head1 SYNOPSIS
10  
11 use HTML::Entities;
12  
13 $a = "Våre norske tegn bør &#230res";
14 decode_entities($a);
15 encode_entities($a, "\200-\377");
16  
17 For example, this:
18  
19 $input = "vis-à-vis Beyoncé's naïve\npapier-mâché résumé";
20 print encode_entities($input), "\n"
21  
22 Prints this out:
23  
24 vis-à-vis Beyoncé's naïve
25 papier-mâché résumé
26  
27 =head1 DESCRIPTION
28  
29 This module deals with encoding and decoding of strings with HTML
30 character entities. The module provides the following functions:
31  
32 =over 4
33  
34 =item decode_entities( $string, ... )
35  
36 This routine replaces HTML entities found in the $string with the
37 corresponding Unicode character. Unrecognized entities are left alone.
38  
39 If multiple strings are provided as argument they are each decoded
40 separately and the same number of strings are returned.
41  
42 If called in void context the arguments are decoded in-place.
43  
44 This routine is exported by default.
45  
46 =item _decode_entities( $string, \%entity2char )
47  
48 =item _decode_entities( $string, \%entity2char, $expand_prefix )
49  
50 This will in-place replace HTML entities in $string. The %entity2char
51 hash must be provided. Named entities not found in the %entity2char
52 hash are left alone. Numeric entities are expanded unless their value
53 overflow.
54  
55 The keys in %entity2char are the entity names to be expanded and their
56 values are what they should expand into. The values do not have to be
57 single character strings. If a key has ";" as suffix,
58 then occurrences in $string are only expanded if properly terminated
59 with ";". Entities without ";" will be expanded regardless of how
60 they are terminated for compatibility with how common browsers treat
61 entities in the Latin-1 range.
62  
63 If $expand_prefix is TRUE then entities without trailing ";" in
64 %entity2char will even be expanded as a prefix of a longer
65 unrecognized name. The longest matching name in %entity2char will be
66 used. This is mainly present for compatibility with an MSIE
67 misfeature.
68  
69 $string = "foo&nbspbar";
70 _decode_entities($string, { nb => "@", nbsp => "\xA0" }, 1);
71 print $string; # will print "foo bar"
72  
73 This routine is exported by default.
74  
75 =item encode_entities( $string )
76  
77 =item encode_entities( $string, $unsafe_chars )
78  
79 This routine replaces unsafe characters in $string with their entity
80 representation. A second argument can be given to specify which characters to
81 consider unsafe. The unsafe characters is specified using the regular
82 expression character class syntax (what you find within brackets in regular
83 expressions).
84  
85 The default set of characters to encode are control chars, high-bit chars, and
86 the C<< < >>, C<< & >>, C<< > >>, C<< ' >> and C<< " >> characters. But this,
87 for example, would encode I<just> the C<< < >>, C<< & >>, C<< > >>, and C<< "
88 >> characters:
89  
90 $encoded = encode_entities($input, '<>&"');
91  
92 and this would only encode non-plain ASCII:
93  
94 $encoded = encode_entities($input, '^\n\x20-\x25\x27-\x7e');
95  
96 This routine is exported by default.
97  
98 =item encode_entities_numeric( $string )
99  
100 =item encode_entities_numeric( $string, $unsafe_chars )
101  
102 This routine works just like encode_entities, except that the replacement
103 entities are always C<&#xI<hexnum>;> and never C<&I<entname>;>. For
104 example, C<encode_entities("r\xF4le")> returns "r&ocirc;le", but
105 C<encode_entities_numeric("r\xF4le")> returns "r&#xF4;le".
106  
107 This routine is I<not> exported by default. But you can always
108 export it with C<use HTML::Entities qw(encode_entities_numeric);>
109 or even C<use HTML::Entities qw(:DEFAULT encode_entities_numeric);>
110  
111 =back
112  
113 All these routines modify the string passed as the first argument, if
114 called in a void context. In scalar and array contexts, the encoded or
115 decoded string is returned (without changing the input string).
116  
117 If you prefer not to import these routines into your namespace, you can
118 call them as:
119  
120 use HTML::Entities ();
121 $decoded = HTML::Entities::decode($a);
122 $encoded = HTML::Entities::encode($a);
123 $encoded = HTML::Entities::encode_numeric($a);
124  
125 The module can also export the %char2entity and the %entity2char
126 hashes, which contain the mapping from all characters to the
127 corresponding entities (and vice versa, respectively).
128  
129 =head1 COPYRIGHT
130  
131 Copyright 1995-2006 Gisle Aas. All rights reserved.
132  
133 This library is free software; you can redistribute it and/or
134 modify it under the same terms as Perl itself.
135  
136 =cut
137  
138 49 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
139 49 our $VERSION = '3.85';
140 49 our (%entity2char, %char2entity);
141  
142 49 require 5.008;
143 49 require Exporter;
144 49 our @ISA = qw(Exporter);
145  
146 49 our @EXPORT = qw(encode_entities decode_entities _decode_entities);
147 49 our @EXPORT_OK = qw(%entity2char %char2entity encode_entities_numeric);
148  
149 0 ▶sub Version { $VERSION; }
Subroutine Version: not called CC=1 SCAR=6.9
Pod documented
150  
151 49 require HTML::Parser; # for fast XS implemented decode_entities
152  
153  
154 49 %entity2char = (
155 # Some normal chars that have special meaning in SGML context
156 amp => '&', # ampersand
157 'gt' => '>', # greater than
158 'lt' => '<', # less than
159 quot => '"', # double quote
160 apos => "'", # single quote
161  
162 # PUBLIC ISO 8879-1986//ENTITIES Added Latin 1//EN//HTML
163 AElig => chr(198), # capital AE diphthong (ligature)
164 Aacute => chr(193), # capital A, acute accent
165 Acirc => chr(194), # capital A, circumflex accent
166 Agrave => chr(192), # capital A, grave accent
167 Aring => chr(197), # capital A, ring
168 Atilde => chr(195), # capital A, tilde
169 Auml => chr(196), # capital A, dieresis or umlaut mark
170 Ccedil => chr(199), # capital C, cedilla
171 ETH => chr(208), # capital Eth, Icelandic
172 Eacute => chr(201), # capital E, acute accent
173 Ecirc => chr(202), # capital E, circumflex accent
174 Egrave => chr(200), # capital E, grave accent
175 Euml => chr(203), # capital E, dieresis or umlaut mark
176 Iacute => chr(205), # capital I, acute accent
177 Icirc => chr(206), # capital I, circumflex accent
178 Igrave => chr(204), # capital I, grave accent
179 Iuml => chr(207), # capital I, dieresis or umlaut mark
180 Ntilde => chr(209), # capital N, tilde
181 Oacute => chr(211), # capital O, acute accent
182 Ocirc => chr(212), # capital O, circumflex accent
183 Ograve => chr(210), # capital O, grave accent
184 Oslash => chr(216), # capital O, slash
185 Otilde => chr(213), # capital O, tilde
186 Ouml => chr(214), # capital O, dieresis or umlaut mark
187 THORN => chr(222), # capital THORN, Icelandic
188 Uacute => chr(218), # capital U, acute accent
189 Ucirc => chr(219), # capital U, circumflex accent
190 Ugrave => chr(217), # capital U, grave accent
191 Uuml => chr(220), # capital U, dieresis or umlaut mark
192 Yacute => chr(221), # capital Y, acute accent
193 aacute => chr(225), # small a, acute accent
194 acirc => chr(226), # small a, circumflex accent
195 aelig => chr(230), # small ae diphthong (ligature)
196 agrave => chr(224), # small a, grave accent
197 aring => chr(229), # small a, ring
198 atilde => chr(227), # small a, tilde
199 auml => chr(228), # small a, dieresis or umlaut mark
200 ccedil => chr(231), # small c, cedilla
201 eacute => chr(233), # small e, acute accent
202 ecirc => chr(234), # small e, circumflex accent
203 egrave => chr(232), # small e, grave accent
204 eth => chr(240), # small eth, Icelandic
205 euml => chr(235), # small e, dieresis or umlaut mark
206 iacute => chr(237), # small i, acute accent
207 icirc => chr(238), # small i, circumflex accent
208 igrave => chr(236), # small i, grave accent
209 iuml => chr(239), # small i, dieresis or umlaut mark
210 ntilde => chr(241), # small n, tilde
211 oacute => chr(243), # small o, acute accent
212 ocirc => chr(244), # small o, circumflex accent
213 ograve => chr(242), # small o, grave accent
214 oslash => chr(248), # small o, slash
215 otilde => chr(245), # small o, tilde
216 ouml => chr(246), # small o, dieresis or umlaut mark
217 szlig => chr(223), # small sharp s, German (sz ligature)
218 thorn => chr(254), # small thorn, Icelandic
219 uacute => chr(250), # small u, acute accent
220 ucirc => chr(251), # small u, circumflex accent
221 ugrave => chr(249), # small u, grave accent
222 uuml => chr(252), # small u, dieresis or umlaut mark
223 yacute => chr(253), # small y, acute accent
224 yuml => chr(255), # small y, dieresis or umlaut mark
225  
226 # Some extra Latin 1 chars that are listed in the HTML3.2 draft (21-May-96)
227 copy => chr(169), # copyright sign
228 reg => chr(174), # registered sign
229 nbsp => chr(160), # non breaking space
230  
231 # Additional ISO-8859/1 entities listed in rfc1866 (section 14)
232 iexcl => chr(161),
233 cent => chr(162),
234 pound => chr(163),
235 curren => chr(164),
236 yen => chr(165),
237 brvbar => chr(166),
238 sect => chr(167),
239 uml => chr(168),
240 ordf => chr(170),
241 laquo => chr(171),
242 'not' => chr(172), # not is a keyword in perl
243 shy => chr(173),
244 macr => chr(175),
245 deg => chr(176),
246 plusmn => chr(177),
247 sup1 => chr(185),
248 sup2 => chr(178),
249 sup3 => chr(179),
250 acute => chr(180),
251 micro => chr(181),
252 para => chr(182),
253 middot => chr(183),
254 cedil => chr(184),
255 ordm => chr(186),
256 raquo => chr(187),
257 frac14 => chr(188),
258 frac12 => chr(189),
259 frac34 => chr(190),
260 iquest => chr(191),
261 'times' => chr(215), # times is a keyword in perl
262 divide => chr(247),
263  
264 'OElig;' => chr(338),
265 'oelig;' => chr(339),
266 'Scaron;' => chr(352),
267 'scaron;' => chr(353),
268 'Yuml;' => chr(376),
269 'fnof;' => chr(402),
270 'circ;' => chr(710),
271 'tilde;' => chr(732),
272 'Alpha;' => chr(913),
273 'Beta;' => chr(914),
274 'Gamma;' => chr(915),
275 'Delta;' => chr(916),
276 'Epsilon;' => chr(917),
277 'Zeta;' => chr(918),
278 'Eta;' => chr(919),
279 'Theta;' => chr(920),
280 'Iota;' => chr(921),
281 'Kappa;' => chr(922),
282 'Lambda;' => chr(923),
283 'Mu;' => chr(924),
284 'Nu;' => chr(925),
285 'Xi;' => chr(926),
286 'Omicron;' => chr(927),
287 'Pi;' => chr(928),
288 'Rho;' => chr(929),
289 'Sigma;' => chr(931),
290 'Tau;' => chr(932),
291 'Upsilon;' => chr(933),
292 'Phi;' => chr(934),
293 'Chi;' => chr(935),
294 'Psi;' => chr(936),
295 'Omega;' => chr(937),
296 'alpha;' => chr(945),
297 'beta;' => chr(946),
298 'gamma;' => chr(947),
299 'delta;' => chr(948),
300 'epsilon;' => chr(949),
301 'zeta;' => chr(950),
302 'eta;' => chr(951),
303 'theta;' => chr(952),
304 'iota;' => chr(953),
305 'kappa;' => chr(954),
306 'lambda;' => chr(955),
307 'mu;' => chr(956),
308 'nu;' => chr(957),
309 'xi;' => chr(958),
310 'omicron;' => chr(959),
311 'pi;' => chr(960),
312 'rho;' => chr(961),
313 'sigmaf;' => chr(962),
314 'sigma;' => chr(963),
315 'tau;' => chr(964),
316 'upsilon;' => chr(965),
317 'phi;' => chr(966),
318 'chi;' => chr(967),
319 'psi;' => chr(968),
320 'omega;' => chr(969),
321 'thetasym;' => chr(977),
322 'upsih;' => chr(978),
323 'piv;' => chr(982),
324 'ensp;' => chr(8194),
325 'emsp;' => chr(8195),
326 'thinsp;' => chr(8201),
327 'zwnj;' => chr(8204),
328 'zwj;' => chr(8205),
329 'lrm;' => chr(8206),
330 'rlm;' => chr(8207),
331 'ndash;' => chr(8211),
332 'mdash;' => chr(8212),
333 'lsquo;' => chr(8216),
334 'rsquo;' => chr(8217),
335 'sbquo;' => chr(8218),
336 'ldquo;' => chr(8220),
337 'rdquo;' => chr(8221),
338 'bdquo;' => chr(8222),
339 'dagger;' => chr(8224),
340 'Dagger;' => chr(8225),
341 'bull;' => chr(8226),
342 'hellip;' => chr(8230),
343 'permil;' => chr(8240),
344 'prime;' => chr(8242),
345 'Prime;' => chr(8243),
346 'lsaquo;' => chr(8249),
347 'rsaquo;' => chr(8250),
348 'oline;' => chr(8254),
349 'frasl;' => chr(8260),
350 'euro;' => chr(8364),
351 'image;' => chr(8465),
352 'weierp;' => chr(8472),
353 'real;' => chr(8476),
354 'trade;' => chr(8482),
355 'alefsym;' => chr(8501),
356 'larr;' => chr(8592),
357 'uarr;' => chr(8593),
358 'rarr;' => chr(8594),
359 'darr;' => chr(8595),
360 'harr;' => chr(8596),
361 'crarr;' => chr(8629),
362 'lArr;' => chr(8656),
363 'uArr;' => chr(8657),
364 'rArr;' => chr(8658),
365 'dArr;' => chr(8659),
366 'hArr;' => chr(8660),
367 'forall;' => chr(8704),
368 'part;' => chr(8706),
369 'exist;' => chr(8707),
370 'empty;' => chr(8709),
371 'nabla;' => chr(8711),
372 'isin;' => chr(8712),
373 'notin;' => chr(8713),
374 'ni;' => chr(8715),
375 'prod;' => chr(8719),
376 'sum;' => chr(8721),
377 'minus;' => chr(8722),
378 'lowast;' => chr(8727),
379 'radic;' => chr(8730),
380 'prop;' => chr(8733),
381 'infin;' => chr(8734),
382 'ang;' => chr(8736),
383 'and;' => chr(8743),
384 'or;' => chr(8744),
385 'cap;' => chr(8745),
386 'cup;' => chr(8746),
387 'int;' => chr(8747),
388 'there4;' => chr(8756),
389 'sim;' => chr(8764),
390 'cong;' => chr(8773),
391 'asymp;' => chr(8776),
392 'ne;' => chr(8800),
393 'equiv;' => chr(8801),
394 'le;' => chr(8804),
395 'ge;' => chr(8805),
396 'sub;' => chr(8834),
397 'sup;' => chr(8835),
398 'nsub;' => chr(8836),
399 'sube;' => chr(8838),
400 'supe;' => chr(8839),
401 'oplus;' => chr(8853),
402 'otimes;' => chr(8855),
403 'perp;' => chr(8869),
404 'sdot;' => chr(8901),
405 'lceil;' => chr(8968),
406 'rceil;' => chr(8969),
407 'lfloor;' => chr(8970),
408 'rfloor;' => chr(8971),
409 'lang;' => chr(9001),
410 'rang;' => chr(9002),
411 'loz;' => chr(9674),
412 'spades;' => chr(9824),
413 'clubs;' => chr(9827),
414 'hearts;' => chr(9829),
415 'diams;' => chr(9830),
416 );
417  
418  
419 # Make the opposite mapping
420 49 ▶while (my($entity, $char) = each(%entity2char)) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %HTML::Entities::entity2char) 12397 49 12446
421 12397 $entity =~ s/;\z//;
422 12397 $char2entity{$char} = "&$entity;";
423 }
424 49 delete $char2entity{"'"}; # only one-way decoding
425  
426 # Fill in missing entities
427 49 for (0 .. 255) {
428 12544 ▶ next if exists $char2entity{chr($_)};
Branch: 1 branch
BranchTrueFalseTotal
if exists $HTML::Entities::char2entity{chr $_} 4900 7644 12544
429 7644 $char2entity{chr($_)} = "&#$_;";
430 }
431  
432 49 my %subst; # compiled encoding regexps
433  
434 sub encode_entities
435 {
436 28 ▶ return undef unless defined $_[0];
Subroutine encode_entities: called CC=9 SCAR=22.1
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[0] 0 28 28
437 28 my $ref;
438 28 ▶ if (defined wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined wantarray) { } 25 3 28
439 25 my $x = $_[0];
440 25 $ref = \$x; # copy
441 } else {
442 3 $ref = \$_[0]; # modify in-place
443 }
444 28 my $regex;
445 28 ▶ if (defined $_[1] and length $_[1]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[1] and length $_[1]) { } 17 11 28
Condition66%2/3
defined $_[1] and length $_[1]
!ll&&!rl&&r
11017
MC/DC50%1/2
defined $_[1] and length $_[1]
defined $_[1] length $_[1]
Truth table66%2/3
A and B
A= defined $_[1]B= length $_[1]
ABresult
0X0
100
111
446 17 $regex = $subst{$_[1]};
447 17 ▶ unless (defined $regex) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $regex) 17 0 17
448 17 my $chars = $_[1];
449  
450 # keep existing escapes, but also escape any unescaped special character:
451 # [ (technically unnecessary but included for symmetry with ])
452 # ] (end of character class)
453 # \ (escape character)
454 17 $chars =~ s{
455 # capture group 1: things to skip and keep
456 (
457 # any escaped character
458 \\.
459 |
460 # either an actual POSIX character class or anything
461 # similar enough to trigger a regex syntax error
462 \[: \^? [[:lower:][:digit:]]{3,} :\]
463 )
464 |
465 # capture group 2: things to be escaped
466 (
467 [\[\]\\]
468 )
469 }{
470 18 ▶ defined $1 ? $1 : '\\' . $2
Branch: 1 branch
BranchTrueFalseTotal
defined $1 ? : 12 6 18
471 }xseg;
472  
473 17 $regex = eval { qr/([$chars])/ };
474 17 ▶ die( $@ . " while trying to turn range: \"$_[1]\"\n "
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 17 17
475 . "into code: /([$chars])/\n "
476 ) if $@;
477 17 $subst{$_[1]} = $regex;
478 }
479 } else {
480 # Encode control chars, high bit chars and '<', '&', '>', ''' and '"'
481 11 $regex = qr/([^\n\r\t !\#\$%\(-;=?-~])/;
482 }
483 28 ▶ $$ref =~ s/$regex/$char2entity{$1} || num_entity($1)/eg;
Branch: 1 branch
BranchTrueFalseTotal
$HTML::Entities::char2entity{$1} or num_entity($1) 25 225 250
484 28 $$ref;
485 }
486  
487 sub encode_entities_numeric {
488 2 ▶ local %char2entity;
Subroutine encode_entities_numeric: called CC=1 SCAR=0.0
Pod documented
489 2 return &encode_entities; # a goto &encode_entities wouldn't work
490 }
491  
492  
493 sub num_entity {
494 25 ▶ sprintf "&#x%X;", ord($_[0]);
Subroutine num_entity: called CC=1 SCAR=0.0
Pod undocumented
495 }
496  
497 # Set up aliases
498 49 *encode = \&encode_entities;
499 49 *encode_numeric = \&encode_entities_numeric;
500 49 *encode_numerically = \&encode_entities_numeric;
501 49 *decode = \&decode_entities;
502  
503 49 1;
« blib/lib/HTML/Parser.pm ↑ Summary blib/lib/HTML/LinkExtor.pm »