Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

412 lines
12 KiB

  1. package HTML::Entities;
  2. # $Id: Entities.pm,v 1.22 2001/04/11 17:22:45 gisle Exp $
  3. =head1 NAME
  4. HTML::Entities - Encode or decode strings with HTML entities
  5. =head1 SYNOPSIS
  6. use HTML::Entities;
  7. $a = "Våre norske tegn bør &#230res";
  8. decode_entities($a);
  9. encode_entities($a, "\200-\377");
  10. =head1 DESCRIPTION
  11. This module deals with encoding and decoding of strings with HTML
  12. character entities. The module provides the following functions:
  13. =over 4
  14. =item decode_entities($string)
  15. This routine replaces HTML entities found in the $string with the
  16. corresponding ISO-8859/1 (or with perl-5.7 or better Unicode)
  17. character. Unrecognized entities are left alone.
  18. =item encode_entities($string, [$unsafe_chars])
  19. This routine replaces unsafe characters in $string with their entity
  20. representation. A second argument can be given to specify which
  21. characters to concider as unsafe. The default set of characters to
  22. expand are control chars, high-bit chars and the '<', '&', '>' and '"'
  23. characters.
  24. =back
  25. Both routines modify the string passed as the first argument if
  26. called in a void context. In scalar and array contexts the encoded or
  27. decoded string is returned (and the argument string is left
  28. unchanged).
  29. If you prefer not to import these routines into your namespace you can
  30. call them as:
  31. use HTML::Entities ();
  32. $encoded = HTML::Entities::encode($a);
  33. $decoded = HTML::Entities::decode($a);
  34. The module can also export the %char2entity and the %entity2char
  35. hashes which contain the mapping from all characters to the
  36. corresponding entities.
  37. =head1 COPYRIGHT
  38. Copyright 1995-2001 Gisle Aas. All rights reserved.
  39. This library is free software; you can redistribute it and/or
  40. modify it under the same terms as Perl itself.
  41. =cut
  42. use strict;
  43. use vars qw(@ISA @EXPORT @EXPORT_OK $VERSION);
  44. use vars qw(%entity2char %char2entity);
  45. require 5.004;
  46. require Exporter;
  47. @ISA = qw(Exporter);
  48. @EXPORT = qw(encode_entities decode_entities _decode_entities);
  49. @EXPORT_OK = qw(%entity2char %char2entity);
  50. $VERSION = sprintf("%d.%02d", q$Revision: 1.22 $ =~ /(\d+)\.(\d+)/);
  51. sub Version { $VERSION; }
  52. require HTML::Parser; # for fast XS implemented decode_entities
  53. %entity2char = (
  54. # Some normal chars that have special meaning in SGML context
  55. amp => '&', # ampersand
  56. 'gt' => '>', # greater than
  57. 'lt' => '<', # less than
  58. quot => '"', # double quote
  59. apos => "'", # single quote
  60. # PUBLIC ISO 8879-1986//ENTITIES Added Latin 1//EN//HTML
  61. AElig => '�', # capital AE diphthong (ligature)
  62. Aacute => '�', # capital A, acute accent
  63. Acirc => '�', # capital A, circumflex accent
  64. Agrave => '�', # capital A, grave accent
  65. Aring => '�', # capital A, ring
  66. Atilde => '�', # capital A, tilde
  67. Auml => '�', # capital A, dieresis or umlaut mark
  68. Ccedil => '�', # capital C, cedilla
  69. ETH => '�', # capital Eth, Icelandic
  70. Eacute => '�', # capital E, acute accent
  71. Ecirc => '�', # capital E, circumflex accent
  72. Egrave => '�', # capital E, grave accent
  73. Euml => '�', # capital E, dieresis or umlaut mark
  74. Iacute => '�', # capital I, acute accent
  75. Icirc => '�', # capital I, circumflex accent
  76. Igrave => '�', # capital I, grave accent
  77. Iuml => '�', # capital I, dieresis or umlaut mark
  78. Ntilde => '�', # capital N, tilde
  79. Oacute => '�', # capital O, acute accent
  80. Ocirc => '�', # capital O, circumflex accent
  81. Ograve => '�', # capital O, grave accent
  82. Oslash => '�', # capital O, slash
  83. Otilde => '�', # capital O, tilde
  84. Ouml => '�', # capital O, dieresis or umlaut mark
  85. THORN => '�', # capital THORN, Icelandic
  86. Uacute => '�', # capital U, acute accent
  87. Ucirc => '�', # capital U, circumflex accent
  88. Ugrave => '�', # capital U, grave accent
  89. Uuml => '�', # capital U, dieresis or umlaut mark
  90. Yacute => '�', # capital Y, acute accent
  91. aacute => '�', # small a, acute accent
  92. acirc => '�', # small a, circumflex accent
  93. aelig => '�', # small ae diphthong (ligature)
  94. agrave => '�', # small a, grave accent
  95. aring => '�', # small a, ring
  96. atilde => '�', # small a, tilde
  97. auml => '�', # small a, dieresis or umlaut mark
  98. ccedil => '�', # small c, cedilla
  99. eacute => '�', # small e, acute accent
  100. ecirc => '�', # small e, circumflex accent
  101. egrave => '�', # small e, grave accent
  102. eth => '�', # small eth, Icelandic
  103. euml => '�', # small e, dieresis or umlaut mark
  104. iacute => '�', # small i, acute accent
  105. icirc => '�', # small i, circumflex accent
  106. igrave => '�', # small i, grave accent
  107. iuml => '�', # small i, dieresis or umlaut mark
  108. ntilde => '�', # small n, tilde
  109. oacute => '�', # small o, acute accent
  110. ocirc => '�', # small o, circumflex accent
  111. ograve => '�', # small o, grave accent
  112. oslash => '�', # small o, slash
  113. otilde => '�', # small o, tilde
  114. ouml => '�', # small o, dieresis or umlaut mark
  115. szlig => '�', # small sharp s, German (sz ligature)
  116. thorn => '�', # small thorn, Icelandic
  117. uacute => '�', # small u, acute accent
  118. ucirc => '�', # small u, circumflex accent
  119. ugrave => '�', # small u, grave accent
  120. uuml => '�', # small u, dieresis or umlaut mark
  121. yacute => '�', # small y, acute accent
  122. yuml => '�', # small y, dieresis or umlaut mark
  123. # Some extra Latin 1 chars that are listed in the HTML3.2 draft (21-May-96)
  124. copy => '�', # copyright sign
  125. reg => '�', # registered sign
  126. nbsp => "\240", # non breaking space
  127. # Additional ISO-8859/1 entities listed in rfc1866 (section 14)
  128. iexcl => '�',
  129. cent => '�',
  130. pound => '�',
  131. curren => '�',
  132. yen => '�',
  133. brvbar => '�',
  134. sect => '�',
  135. uml => '�',
  136. ordf => '�',
  137. laquo => '�',
  138. 'not' => '�', # not is a keyword in perl
  139. shy => '�',
  140. macr => '�',
  141. deg => '�',
  142. plusmn => '�',
  143. sup1 => '�',
  144. sup2 => '�',
  145. sup3 => '�',
  146. acute => '�',
  147. micro => '�',
  148. para => '�',
  149. middot => '�',
  150. cedil => '�',
  151. ordm => '�',
  152. raquo => '�',
  153. frac14 => '�',
  154. frac12 => '�',
  155. frac34 => '�',
  156. iquest => '�',
  157. 'times' => '�', # times is a keyword in perl
  158. divide => '�',
  159. ( $] > 5.007 ? (
  160. OElig => chr(338),
  161. oelig => chr(339),
  162. Scaron => chr(352),
  163. scaron => chr(353),
  164. Yuml => chr(376),
  165. fnof => chr(402),
  166. circ => chr(710),
  167. tilde => chr(732),
  168. Alpha => chr(913),
  169. Beta => chr(914),
  170. Gamma => chr(915),
  171. Delta => chr(916),
  172. Epsilon => chr(917),
  173. Zeta => chr(918),
  174. Eta => chr(919),
  175. Theta => chr(920),
  176. Iota => chr(921),
  177. Kappa => chr(922),
  178. Lambda => chr(923),
  179. Mu => chr(924),
  180. Nu => chr(925),
  181. Xi => chr(926),
  182. Omicron => chr(927),
  183. Pi => chr(928),
  184. Rho => chr(929),
  185. Sigma => chr(931),
  186. Tau => chr(932),
  187. Upsilon => chr(933),
  188. Phi => chr(934),
  189. Chi => chr(935),
  190. Psi => chr(936),
  191. Omega => chr(937),
  192. alpha => chr(945),
  193. beta => chr(946),
  194. gamma => chr(947),
  195. delta => chr(948),
  196. epsilon => chr(949),
  197. zeta => chr(950),
  198. eta => chr(951),
  199. theta => chr(952),
  200. iota => chr(953),
  201. kappa => chr(954),
  202. lambda => chr(955),
  203. mu => chr(956),
  204. nu => chr(957),
  205. xi => chr(958),
  206. omicron => chr(959),
  207. pi => chr(960),
  208. rho => chr(961),
  209. sigmaf => chr(962),
  210. sigma => chr(963),
  211. tau => chr(964),
  212. upsilon => chr(965),
  213. phi => chr(966),
  214. chi => chr(967),
  215. psi => chr(968),
  216. omega => chr(969),
  217. thetasym => chr(977),
  218. upsih => chr(978),
  219. piv => chr(982),
  220. ensp => chr(8194),
  221. emsp => chr(8195),
  222. thinsp => chr(8201),
  223. zwnj => chr(8204),
  224. zwj => chr(8205),
  225. lrm => chr(8206),
  226. rlm => chr(8207),
  227. ndash => chr(8211),
  228. mdash => chr(8212),
  229. lsquo => chr(8216),
  230. rsquo => chr(8217),
  231. sbquo => chr(8218),
  232. ldquo => chr(8220),
  233. rdquo => chr(8221),
  234. bdquo => chr(8222),
  235. dagger => chr(8224),
  236. Dagger => chr(8225),
  237. bull => chr(8226),
  238. hellip => chr(8230),
  239. permil => chr(8240),
  240. prime => chr(8242),
  241. Prime => chr(8243),
  242. lsaquo => chr(8249),
  243. rsaquo => chr(8250),
  244. oline => chr(8254),
  245. frasl => chr(8260),
  246. euro => chr(8364),
  247. image => chr(8465),
  248. weierp => chr(8472),
  249. real => chr(8476),
  250. trade => chr(8482),
  251. alefsym => chr(8501),
  252. larr => chr(8592),
  253. uarr => chr(8593),
  254. rarr => chr(8594),
  255. darr => chr(8595),
  256. harr => chr(8596),
  257. crarr => chr(8629),
  258. lArr => chr(8656),
  259. uArr => chr(8657),
  260. rArr => chr(8658),
  261. dArr => chr(8659),
  262. hArr => chr(8660),
  263. forall => chr(8704),
  264. part => chr(8706),
  265. exist => chr(8707),
  266. empty => chr(8709),
  267. nabla => chr(8711),
  268. isin => chr(8712),
  269. notin => chr(8713),
  270. ni => chr(8715),
  271. prod => chr(8719),
  272. sum => chr(8721),
  273. minus => chr(8722),
  274. lowast => chr(8727),
  275. radic => chr(8730),
  276. prop => chr(8733),
  277. infin => chr(8734),
  278. ang => chr(8736),
  279. 'and' => chr(8743),
  280. 'or' => chr(8744),
  281. cap => chr(8745),
  282. cup => chr(8746),
  283. 'int' => chr(8747),
  284. there4 => chr(8756),
  285. sim => chr(8764),
  286. cong => chr(8773),
  287. asymp => chr(8776),
  288. 'ne' => chr(8800),
  289. equiv => chr(8801),
  290. 'le' => chr(8804),
  291. 'ge' => chr(8805),
  292. 'sub' => chr(8834),
  293. sup => chr(8835),
  294. nsub => chr(8836),
  295. sube => chr(8838),
  296. supe => chr(8839),
  297. oplus => chr(8853),
  298. otimes => chr(8855),
  299. perp => chr(8869),
  300. sdot => chr(8901),
  301. lceil => chr(8968),
  302. rceil => chr(8969),
  303. lfloor => chr(8970),
  304. rfloor => chr(8971),
  305. lang => chr(9001),
  306. rang => chr(9002),
  307. loz => chr(9674),
  308. spades => chr(9824),
  309. clubs => chr(9827),
  310. hearts => chr(9829),
  311. diams => chr(9830),
  312. ) : ())
  313. );
  314. # Make the oposite mapping
  315. while (my($entity, $char) = each(%entity2char)) {
  316. $char2entity{$char} = "&$entity;";
  317. }
  318. delete $char2entity{"'"}; # only one-way decoding
  319. # Fill inn missing entities
  320. for (0 .. 255) {
  321. next if exists $char2entity{chr($_)};
  322. $char2entity{chr($_)} = "&#$_;";
  323. }
  324. my %subst; # compiled encoding regexps
  325. sub decode_entities_old
  326. {
  327. my $array;
  328. if (defined wantarray) {
  329. $array = [@_]; # copy
  330. } else {
  331. $array = \@_; # modify in-place
  332. }
  333. my $c;
  334. for (@$array) {
  335. s/(&\#(\d+);?)/$2 < 256 ? chr($2) : $1/eg;
  336. s/(&\#[xX]([0-9a-fA-F]+);?)/$c = hex($2); $c < 256 ? chr($c) : $1/eg;
  337. s/(&(\w+);?)/$entity2char{$2} || $1/eg;
  338. }
  339. wantarray ? @$array : $array->[0];
  340. }
  341. sub encode_entities
  342. {
  343. my $ref;
  344. if (defined wantarray) {
  345. my $x = $_[0];
  346. $ref = \$x; # copy
  347. } else {
  348. $ref = \$_[0]; # modify in-place
  349. }
  350. if (defined $_[1]) {
  351. unless (exists $subst{$_[1]}) {
  352. # Because we can't compile regex we fake it with a cached sub
  353. $subst{$_[1]} =
  354. eval "sub {\$_[0] =~ s/([$_[1]])/\$char2entity{\$1} || num_entity(\$1)/ge; }";
  355. die $@ if $@;
  356. }
  357. &{$subst{$_[1]}}($$ref);
  358. } else {
  359. # Encode control chars, high bit chars and '<', '&', '>', '"'
  360. $$ref =~ s/([^\n\t !\#\$%\'-;=?-~])/$char2entity{$1} || num_entity($1)/ge;
  361. }
  362. $$ref;
  363. }
  364. sub num_entity {
  365. sprintf "&#x%X;", ord($_[0]);
  366. }
  367. # Set up aliases
  368. *encode = \&encode_entities;
  369. *decode = \&decode_entities;
  370. 1;