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.

190 lines
48 KiB

4 years ago
  1. var ENTITIES = [['Aacute', [193]], ['aacute', [225]], ['Abreve', [258]], ['abreve', [259]], ['ac', [8766]], ['acd', [8767]], ['acE', [8766, 819]], ['Acirc', [194]], ['acirc', [226]], ['acute', [180]], ['Acy', [1040]], ['acy', [1072]], ['AElig', [198]], ['aelig', [230]], ['af', [8289]], ['Afr', [120068]], ['afr', [120094]], ['Agrave', [192]], ['agrave', [224]], ['alefsym', [8501]], ['aleph', [8501]], ['Alpha', [913]], ['alpha', [945]], ['Amacr', [256]], ['amacr', [257]], ['amalg', [10815]], ['amp', [38]], ['AMP', [38]], ['andand', [10837]], ['And', [10835]], ['and', [8743]], ['andd', [10844]], ['andslope', [10840]], ['andv', [10842]], ['ang', [8736]], ['ange', [10660]], ['angle', [8736]], ['angmsdaa', [10664]], ['angmsdab', [10665]], ['angmsdac', [10666]], ['angmsdad', [10667]], ['angmsdae', [10668]], ['angmsdaf', [10669]], ['angmsdag', [10670]], ['angmsdah', [10671]], ['angmsd', [8737]], ['angrt', [8735]], ['angrtvb', [8894]], ['angrtvbd', [10653]], ['angsph', [8738]], ['angst', [197]], ['angzarr', [9084]], ['Aogon', [260]], ['aogon', [261]], ['Aopf', [120120]], ['aopf', [120146]], ['apacir', [10863]], ['ap', [8776]], ['apE', [10864]], ['ape', [8778]], ['apid', [8779]], ['apos', [39]], ['ApplyFunction', [8289]], ['approx', [8776]], ['approxeq', [8778]], ['Aring', [197]], ['aring', [229]], ['Ascr', [119964]], ['ascr', [119990]], ['Assign', [8788]], ['ast', [42]], ['asymp', [8776]], ['asympeq', [8781]], ['Atilde', [195]], ['atilde', [227]], ['Auml', [196]], ['auml', [228]], ['awconint', [8755]], ['awint', [10769]], ['backcong', [8780]], ['backepsilon', [1014]], ['backprime', [8245]], ['backsim', [8765]], ['backsimeq', [8909]], ['Backslash', [8726]], ['Barv', [10983]], ['barvee', [8893]], ['barwed', [8965]], ['Barwed', [8966]], ['barwedge', [8965]], ['bbrk', [9141]], ['bbrktbrk', [9142]], ['bcong', [8780]], ['Bcy', [1041]], ['bcy', [1073]], ['bdquo', [8222]], ['becaus', [8757]], ['because', [8757]], ['Because', [8757]], ['bemptyv', [10672]], ['bepsi', [1014]], ['bernou', [8492]], ['Bernoullis', [8492]], ['Beta', [914]], ['beta', [946]], ['beth', [8502]], ['between', [8812]], ['Bfr', [120069]], ['bfr', [120095]], ['bigcap', [8898]], ['bigcirc', [9711]], ['bigcup', [8899]], ['bigodot', [10752]], ['bigoplus', [10753]], ['bigotimes', [10754]], ['bigsqcup', [10758]], ['bigstar', [9733]], ['bigtriangledown', [9661]], ['bigtriangleup', [9651]], ['biguplus', [10756]], ['bigvee', [8897]], ['bigwedge', [8896]], ['bkarow', [10509]], ['blacklozenge', [10731]], ['blacksquare', [9642]], ['blacktriangle', [9652]], ['blacktriangledown', [9662]], ['blacktriangleleft', [9666]], ['blacktriangleright', [9656]], ['blank', [9251]], ['blk12', [9618]], ['blk14', [9617]], ['blk34', [9619]], ['block', [9608]], ['bne', [61, 8421]], ['bnequiv', [8801, 8421]], ['bNot', [10989]], ['bnot', [8976]], ['Bopf', [120121]], ['bopf', [120147]], ['bot', [8869]], ['bottom', [8869]], ['bowtie', [8904]], ['boxbox', [10697]], ['boxdl', [9488]], ['boxdL', [9557]], ['boxDl', [9558]], ['boxDL', [9559]], ['boxdr', [9484]], ['boxdR', [9554]], ['boxDr', [9555]], ['boxDR', [9556]], ['boxh', [9472]], ['boxH', [9552]], ['boxhd', [9516]], ['boxHd', [9572]], ['boxhD', [9573]], ['boxHD', [9574]], ['boxhu', [9524]], ['boxHu', [9575]], ['boxhU', [9576]], ['boxHU', [9577]], ['boxminus', [8863]], ['boxplus', [8862]], ['boxtimes', [8864]], ['boxul', [9496]], ['boxuL', [9563]], ['boxUl', [9564]], ['boxUL', [9565]], ['boxur', [9492]], ['boxuR', [9560]], ['boxUr', [9561]], ['boxUR', [9562]], ['boxv', [9474]], ['boxV', [9553]], ['boxvh', [9532]], ['boxvH', [9578]], ['boxVh', [9579]], ['boxVH', [9580]], ['boxvl', [9508]], ['boxvL', [9569]], ['boxVl', [9570]], ['boxVL', [9571]], ['boxvr', [9500]], ['boxvR', [9566]], ['boxVr', [9567]], ['boxVR', [9568]], ['bprime', [8245]], ['breve', [728]], ['Breve', [728]], ['brvbar', [166]], ['bscr', [119991]], ['Bscr', [8492]], ['bsemi', [8271]], ['bsim', [8765]], ['bsime', [8909]], ['bsolb', [10693]], ['bsol', [92]], ['bsolhsub', [10184]], ['bull', [8226]], ['bullet', [8226]], ['bump', [8782]], ['bumpE', [10926]], ['bumpe', [8783]], ['Bumpeq', [8782]
  2. var alphaIndex = {};
  3. var charIndex = {};
  4. createIndexes(alphaIndex, charIndex);
  5. /**
  6. * @constructor
  7. */
  8. function Html5Entities() {}
  9. /**
  10. * @param {String} str
  11. * @returns {String}
  12. */
  13. Html5Entities.prototype.decode = function(str) {
  14. if (!str || !str.length) {
  15. return '';
  16. }
  17. return str.replace(/&(#?[\w\d]+);?/g, function(s, entity) {
  18. var chr;
  19. if (entity.charAt(0) === "#") {
  20. var code = entity.charAt(1) === 'x' ?
  21. parseInt(entity.substr(2).toLowerCase(), 16) :
  22. parseInt(entity.substr(1));
  23. if (!(isNaN(code) || code < -32768 || code > 65535)) {
  24. chr = String.fromCharCode(code);
  25. }
  26. } else {
  27. chr = alphaIndex[entity];
  28. }
  29. return chr || s;
  30. });
  31. };
  32. /**
  33. * @param {String} str
  34. * @returns {String}
  35. */
  36. Html5Entities.decode = function(str) {
  37. return new Html5Entities().decode(str);
  38. };
  39. /**
  40. * @param {String} str
  41. * @returns {String}
  42. */
  43. Html5Entities.prototype.encode = function(str) {
  44. if (!str || !str.length) {
  45. return '';
  46. }
  47. var strLength = str.length;
  48. var result = '';
  49. var i = 0;
  50. while (i < strLength) {
  51. var charInfo = charIndex[str.charCodeAt(i)];
  52. if (charInfo) {
  53. var alpha = charInfo[str.charCodeAt(i + 1)];
  54. if (alpha) {
  55. i++;
  56. } else {
  57. alpha = charInfo[''];
  58. }
  59. if (alpha) {
  60. result += "&" + alpha + ";";
  61. i++;
  62. continue;
  63. }
  64. }
  65. result += str.charAt(i);
  66. i++;
  67. }
  68. return result;
  69. };
  70. /**
  71. * @param {String} str
  72. * @returns {String}
  73. */
  74. Html5Entities.encode = function(str) {
  75. return new Html5Entities().encode(str);
  76. };
  77. /**
  78. * @param {String} str
  79. * @returns {String}
  80. */
  81. Html5Entities.prototype.encodeNonUTF = function(str) {
  82. if (!str || !str.length) {
  83. return '';
  84. }
  85. var strLength = str.length;
  86. var result = '';
  87. var i = 0;
  88. while (i < strLength) {
  89. var c = str.charCodeAt(i);
  90. var charInfo = charIndex[c];
  91. if (charInfo) {
  92. var alpha = charInfo[str.charCodeAt(i + 1)];
  93. if (alpha) {
  94. i++;
  95. } else {
  96. alpha = charInfo[''];
  97. }
  98. if (alpha) {
  99. result += "&" + alpha + ";";
  100. i++;
  101. continue;
  102. }
  103. }
  104. if (c < 32 || c > 126) {
  105. result += '&#' + c + ';';
  106. } else {
  107. result += str.charAt(i);
  108. }
  109. i++;
  110. }
  111. return result;
  112. };
  113. /**
  114. * @param {String} str
  115. * @returns {String}
  116. */
  117. Html5Entities.encodeNonUTF = function(str) {
  118. return new Html5Entities().encodeNonUTF(str);
  119. };
  120. /**
  121. * @param {String} str
  122. * @returns {String}
  123. */
  124. Html5Entities.prototype.encodeNonASCII = function(str) {
  125. if (!str || !str.length) {
  126. return '';
  127. }
  128. var strLength = str.length;
  129. var result = '';
  130. var i = 0;
  131. while (i < strLength) {
  132. var c = str.charCodeAt(i);
  133. if (c <= 255) {
  134. result += str[i++];
  135. continue;
  136. }
  137. result += '&#' + c + ';';
  138. i++
  139. }
  140. return result;
  141. };
  142. /**
  143. * @param {String} str
  144. * @returns {String}
  145. */
  146. Html5Entities.encodeNonASCII = function(str) {
  147. return new Html5Entities().encodeNonASCII(str);
  148. };
  149. /**
  150. * @param {Object} alphaIndex Passed by reference.
  151. * @param {Object} charIndex Passed by reference.
  152. */
  153. function createIndexes(alphaIndex, charIndex) {
  154. var i = ENTITIES.length;
  155. var _results = [];
  156. while (i--) {
  157. var e = ENTITIES[i];
  158. var alpha = e[0];
  159. var chars = e[1];
  160. var chr = chars[0];
  161. var addChar = (chr < 32 || chr > 126) || chr === 62 || chr === 60 || chr === 38 || chr === 34 || chr === 39;
  162. var charInfo;
  163. if (addChar) {
  164. charInfo = charIndex[chr] = charIndex[chr] || {};
  165. }
  166. if (chars[1]) {
  167. var chr2 = chars[1];
  168. alphaIndex[alpha] = String.fromCharCode(chr) + String.fromCharCode(chr2);
  169. _results.push(addChar && (charInfo[chr2] = alpha));
  170. } else {
  171. alphaIndex[alpha] = String.fromCharCode(chr);
  172. _results.push(addChar && (charInfo[''] = alpha));
  173. }
  174. }
  175. }
  176. module.exports = Html5Entities;