entities.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*
  2. Copyright (c) 2004-2012, The Dojo Foundation All Rights Reserved.
  3. Available via Academic Free License >= 2.1 OR the modified BSD license.
  4. see: http://dojotoolkit.org/license for details
  5. */
  6. if(!dojo._hasResource["dojox.html.entities"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dojox.html.entities"] = true;
  8. dojo.provide("dojox.html.entities");
  9. (function(){
  10. // dojox.html.entities.html [public] Array
  11. // Entity characters for HTML, represented as an array of
  12. // character code, entity name (minus & and ; wrapping.
  13. // The function wrapper is to fix global leking with the build tools.
  14. var _applyEncodingMap = function(str, map){
  15. // summary:
  16. // Private internal function for performing encoding of entity characters.
  17. // tags:
  18. // private
  19. // Check to see if we have genned and cached a regexp for this map yet
  20. // If we have, use it, if not, gen it, cache, then use.
  21. var mapper, regexp;
  22. if(map._encCache &&
  23. map._encCache.regexp &&
  24. map._encCache.mapper &&
  25. map.length == map._encCache.length){
  26. mapper = map._encCache.mapper;
  27. regexp = map._encCache.regexp;
  28. }else{
  29. mapper = {};
  30. regexp = ["["];
  31. var i;
  32. for(i = 0; i < map.length; i++){
  33. mapper[map[i][0]] = "&" + map[i][1] + ";";
  34. regexp.push(map[i][0]);
  35. }
  36. regexp.push("]");
  37. regexp = new RegExp(regexp.join(""), "g");
  38. map._encCache = {
  39. mapper: mapper,
  40. regexp: regexp,
  41. length: map.length
  42. };
  43. }
  44. str = str.replace(regexp, function(c){
  45. return mapper[c];
  46. });
  47. return str;
  48. };
  49. var _applyDecodingMap = function(str, map){
  50. // summary:
  51. // Private internal function for performing decoding of entity characters.
  52. // tags:
  53. // private
  54. var mapper, regexp;
  55. if(map._decCache &&
  56. map._decCache.regexp &&
  57. map._decCache.mapper &&
  58. map.length == map._decCache.length){
  59. mapper = map._decCache.mapper;
  60. regexp = map._decCache.regexp;
  61. }else{
  62. mapper = {};
  63. regexp = ["("];
  64. var i;
  65. for(i = 0; i < map.length; i++){
  66. var e = "&" + map[i][1] + ";";
  67. if(i){regexp.push("|");}
  68. mapper[e] = map[i][0];
  69. regexp.push(e);
  70. }
  71. regexp.push(")");
  72. regexp = new RegExp(regexp.join(""), "g");
  73. map._decCache = {
  74. mapper: mapper,
  75. regexp: regexp,
  76. length: map.length
  77. };
  78. }
  79. str = str.replace(regexp, function(c){
  80. return mapper[c];
  81. });
  82. return str;
  83. };
  84. dojox.html.entities.html = [
  85. ["\u0026","amp"], ["\u0022","quot"],["\u003C","lt"], ["\u003E","gt"],
  86. ["\u00A0","nbsp"]
  87. ];
  88. // dojox.html.entities.latin [public] Array
  89. // Entity characters for Latin characters and similar, represented as an array of
  90. // character code, entity name (minus & and ; wrapping.
  91. dojox.html.entities.latin = [
  92. ["\u00A1","iexcl"],["\u00A2","cent"],["\u00A3","pound"],["\u20AC","euro"],
  93. ["\u00A4","curren"],["\u00A5","yen"],["\u00A6","brvbar"],["\u00A7","sect"],
  94. ["\u00A8","uml"],["\u00A9","copy"],["\u00AA","ordf"],["\u00AB","laquo"],
  95. ["\u00AC","not"],["\u00AD","shy"],["\u00AE","reg"],["\u00AF","macr"],
  96. ["\u00B0","deg"],["\u00B1","plusmn"],["\u00B2","sup2"],["\u00B3","sup3"],
  97. ["\u00B4","acute"],["\u00B5","micro"],["\u00B6","para"],["\u00B7","middot"],
  98. ["\u00B8","cedil"],["\u00B9","sup1"],["\u00BA","ordm"],["\u00BB","raquo"],
  99. ["\u00BC","frac14"],["\u00BD","frac12"],["\u00BE","frac34"],["\u00BF","iquest"],
  100. ["\u00C0","Agrave"],["\u00C1","Aacute"],["\u00C2","Acirc"],["\u00C3","Atilde"],
  101. ["\u00C4","Auml"],["\u00C5","Aring"],["\u00C6","AElig"],["\u00C7","Ccedil"],
  102. ["\u00C8","Egrave"],["\u00C9","Eacute"],["\u00CA","Ecirc"],["\u00CB","Euml"],
  103. ["\u00CC","Igrave"],["\u00CD","Iacute"],["\u00CE","Icirc"],["\u00CF","Iuml"],
  104. ["\u00D0","ETH"],["\u00D1","Ntilde"],["\u00D2","Ograve"],["\u00D3","Oacute"],
  105. ["\u00D4","Ocirc"],["\u00D5","Otilde"],["\u00D6","Ouml"],["\u00D7","times"],
  106. ["\u00D8","Oslash"],["\u00D9","Ugrave"],["\u00DA","Uacute"],["\u00DB","Ucirc"],
  107. ["\u00DC","Uuml"],["\u00DD","Yacute"],["\u00DE","THORN"],["\u00DF","szlig"],
  108. ["\u00E0","agrave"],["\u00E1","aacute"],["\u00E2","acirc"],["\u00E3","atilde"],
  109. ["\u00E4","auml"],["\u00E5","aring"],["\u00E6","aelig"],["\u00E7","ccedil"],
  110. ["\u00E8","egrave"],["\u00E9","eacute"],["\u00EA","ecirc"],["\u00EB","euml"],
  111. ["\u00EC","igrave"],["\u00ED","iacute"],["\u00EE","icirc"],["\u00EF","iuml"],
  112. ["\u00F0","eth"],["\u00F1","ntilde"],["\u00F2","ograve"],["\u00F3","oacute"],
  113. ["\u00F4","ocirc"],["\u00F5","otilde"],["\u00F6","ouml"],["\u00F7","divide"],
  114. ["\u00F8","oslash"],["\u00F9","ugrave"],["\u00FA","uacute"],["\u00FB","ucirc"],
  115. ["\u00FC","uuml"],["\u00FD","yacute"],["\u00FE","thorn"],["\u00FF","yuml"],
  116. ["\u0192","fnof"],["\u0391","Alpha"],["\u0392","Beta"],["\u0393","Gamma"],
  117. ["\u0394","Delta"],["\u0395","Epsilon"],["\u0396","Zeta"],["\u0397","Eta"],
  118. ["\u0398","Theta"], ["\u0399","Iota"],["\u039A","Kappa"],["\u039B","Lambda"],
  119. ["\u039C","Mu"],["\u039D","Nu"],["\u039E","Xi"],["\u039F","Omicron"],
  120. ["\u03A0","Pi"],["\u03A1","Rho"],["\u03A3","Sigma"],["\u03A4","Tau"],
  121. ["\u03A5","Upsilon"],["\u03A6","Phi"],["\u03A7","Chi"],["\u03A8","Psi"],
  122. ["\u03A9","Omega"],["\u03B1","alpha"],["\u03B2","beta"],["\u03B3","gamma"],
  123. ["\u03B4","delta"],["\u03B5","epsilon"],["\u03B6","zeta"],["\u03B7","eta"],
  124. ["\u03B8","theta"],["\u03B9","iota"],["\u03BA","kappa"],["\u03BB","lambda"],
  125. ["\u03BC","mu"],["\u03BD","nu"],["\u03BE","xi"],["\u03BF","omicron"],
  126. ["\u03C0","pi"],["\u03C1","rho"],["\u03C2","sigmaf"],["\u03C3","sigma"],
  127. ["\u03C4","tau"],["\u03C5","upsilon"],["\u03C6","phi"],["\u03C7","chi"],
  128. ["\u03C8","psi"],["\u03C9","omega"],["\u03D1","thetasym"],["\u03D2","upsih"],
  129. ["\u03D6","piv"],["\u2022","bull"],["\u2026","hellip"],["\u2032","prime"],
  130. ["\u2033","Prime"],["\u203E","oline"],["\u2044","frasl"],["\u2118","weierp"],
  131. ["\u2111","image"],["\u211C","real"],["\u2122","trade"],["\u2135","alefsym"],
  132. ["\u2190","larr"],["\u2191","uarr"],["\u2192","rarr"],["\u2193","darr"],
  133. ["\u2194","harr"],["\u21B5","crarr"],["\u21D0","lArr"],["\u21D1","uArr"],
  134. ["\u21D2","rArr"],["\u21D3","dArr"],["\u21D4","hArr"],["\u2200","forall"],
  135. ["\u2202","part"],["\u2203","exist"],["\u2205","empty"],["\u2207","nabla"],
  136. ["\u2208","isin"],["\u2209","notin"],["\u220B","ni"],["\u220F","prod"],
  137. ["\u2211","sum"],["\u2212","minus"],["\u2217","lowast"],["\u221A","radic"],
  138. ["\u221D","prop"],["\u221E","infin"],["\u2220","ang"],["\u2227","and"],
  139. ["\u2228","or"],["\u2229","cap"],["\u222A","cup"],["\u222B","int"],
  140. ["\u2234","there4"],["\u223C","sim"],["\u2245","cong"],["\u2248","asymp"],
  141. ["\u2260","ne"],["\u2261","equiv"],["\u2264","le"],["\u2265","ge"],
  142. ["\u2282","sub"],["\u2283","sup"],["\u2284","nsub"],["\u2286","sube"],
  143. ["\u2287","supe"],["\u2295","oplus"],["\u2297","otimes"],["\u22A5","perp"],
  144. ["\u22C5","sdot"],["\u2308","lceil"],["\u2309","rceil"],["\u230A","lfloor"],
  145. ["\u230B","rfloor"],["\u2329","lang"],["\u232A","rang"],["\u25CA","loz"],
  146. ["\u2660","spades"],["\u2663","clubs"],["\u2665","hearts"],["\u2666","diams"],
  147. ["\u0152","Elig"],["\u0153","oelig"],["\u0160","Scaron"],["\u0161","scaron"],
  148. ["\u0178","Yuml"],["\u02C6","circ"],["\u02DC","tilde"],["\u2002","ensp"],
  149. ["\u2003","emsp"],["\u2009","thinsp"],["\u200C","zwnj"],["\u200D","zwj"],
  150. ["\u200E","lrm"],["\u200F","rlm"],["\u2013","ndash"],["\u2014","mdash"],
  151. ["\u2018","lsquo"],["\u2019","rsquo"],["\u201A","sbquo"],["\u201C","ldquo"],
  152. ["\u201D","rdquo"],["\u201E","bdquo"],["\u2020","dagger"],["\u2021","Dagger"],
  153. ["\u2030","permil"],["\u2039","lsaquo"],["\u203A","rsaquo"]
  154. ];
  155. dojox.html.entities.encode = function(str/*string*/, m /*array?*/){
  156. // summary:
  157. // Function to obtain an entity encoding for a specified character
  158. // str:
  159. // The string to process for possible entity encoding.
  160. // m:
  161. // An optional list of character to entity name mappings (array of
  162. // arrays). If not provided, it uses the and Latin entities as the
  163. // set to map and escape.
  164. // tags:
  165. // public
  166. if(str){
  167. if(!m){
  168. // Apply the basic mappings. HTML should always come first when decoding
  169. // as well.
  170. str = _applyEncodingMap(str, dojox.html.entities.html);
  171. str = _applyEncodingMap(str, dojox.html.entities.latin);
  172. }else{
  173. str = _applyEncodingMap(str, m);
  174. }
  175. }
  176. return str;
  177. };
  178. dojox.html.entities.decode = function(str/*string*/, m /*array?*/){
  179. // summary:
  180. // Function to obtain an entity encoding for a specified character
  181. // str:
  182. // The string to process for possible entity encoding to decode.
  183. // m:
  184. // An optional list of character to entity name mappings (array of
  185. // arrays). If not provided, it uses the HTML and Latin entities as the
  186. // set to map and decode.
  187. // tags:
  188. // public
  189. if(str){
  190. if(!m){
  191. // Apply the basic mappings. HTML should always come first when decoding
  192. // as well.
  193. str = _applyDecodingMap(str, dojox.html.entities.html);
  194. str = _applyDecodingMap(str, dojox.html.entities.latin);
  195. }else{
  196. str = _applyDecodingMap(str, m);
  197. }
  198. }
  199. return str;
  200. };
  201. })();
  202. }