xml-entities.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. var surrogate_pairs_1 = require("./surrogate-pairs");
  4. var ALPHA_INDEX = {
  5. '&lt': '<',
  6. '&gt': '>',
  7. '&quot': '"',
  8. '&apos': '\'',
  9. '&amp': '&',
  10. '&lt;': '<',
  11. '&gt;': '>',
  12. '&quot;': '"',
  13. '&apos;': '\'',
  14. '&amp;': '&'
  15. };
  16. var CHAR_INDEX = {
  17. 60: 'lt',
  18. 62: 'gt',
  19. 34: 'quot',
  20. 39: 'apos',
  21. 38: 'amp'
  22. };
  23. var CHAR_S_INDEX = {
  24. '<': '&lt;',
  25. '>': '&gt;',
  26. '"': '&quot;',
  27. '\'': '&apos;',
  28. '&': '&amp;'
  29. };
  30. var XmlEntities = /** @class */ (function () {
  31. function XmlEntities() {
  32. }
  33. XmlEntities.prototype.encode = function (str) {
  34. if (!str || !str.length) {
  35. return '';
  36. }
  37. return str.replace(/[<>"'&]/g, function (s) {
  38. return CHAR_S_INDEX[s];
  39. });
  40. };
  41. XmlEntities.encode = function (str) {
  42. return new XmlEntities().encode(str);
  43. };
  44. XmlEntities.prototype.decode = function (str) {
  45. if (!str || !str.length) {
  46. return '';
  47. }
  48. return str.replace(/&#?[0-9a-zA-Z]+;?/g, function (s) {
  49. if (s.charAt(1) === '#') {
  50. var code = s.charAt(2).toLowerCase() === 'x' ?
  51. parseInt(s.substr(3), 16) :
  52. parseInt(s.substr(2));
  53. if (!isNaN(code) || code >= -32768) {
  54. if (code <= 65535) {
  55. return String.fromCharCode(code);
  56. }
  57. else {
  58. return surrogate_pairs_1.fromCodePoint(code);
  59. }
  60. }
  61. return '';
  62. }
  63. return ALPHA_INDEX[s] || s;
  64. });
  65. };
  66. XmlEntities.decode = function (str) {
  67. return new XmlEntities().decode(str);
  68. };
  69. XmlEntities.prototype.encodeNonUTF = function (str) {
  70. if (!str || !str.length) {
  71. return '';
  72. }
  73. var strLength = str.length;
  74. var result = '';
  75. var i = 0;
  76. while (i < strLength) {
  77. var c = str.charCodeAt(i);
  78. var alpha = CHAR_INDEX[c];
  79. if (alpha) {
  80. result += "&" + alpha + ";";
  81. i++;
  82. continue;
  83. }
  84. if (c < 32 || c > 126) {
  85. if (c >= surrogate_pairs_1.highSurrogateFrom && c <= surrogate_pairs_1.highSurrogateTo) {
  86. result += '&#' + surrogate_pairs_1.getCodePoint(str, i) + ';';
  87. i++;
  88. }
  89. else {
  90. result += '&#' + c + ';';
  91. }
  92. }
  93. else {
  94. result += str.charAt(i);
  95. }
  96. i++;
  97. }
  98. return result;
  99. };
  100. XmlEntities.encodeNonUTF = function (str) {
  101. return new XmlEntities().encodeNonUTF(str);
  102. };
  103. XmlEntities.prototype.encodeNonASCII = function (str) {
  104. if (!str || !str.length) {
  105. return '';
  106. }
  107. var strLength = str.length;
  108. var result = '';
  109. var i = 0;
  110. while (i < strLength) {
  111. var c = str.charCodeAt(i);
  112. if (c <= 255) {
  113. result += str[i++];
  114. continue;
  115. }
  116. if (c >= surrogate_pairs_1.highSurrogateFrom && c <= surrogate_pairs_1.highSurrogateTo) {
  117. result += '&#' + surrogate_pairs_1.getCodePoint(str, i) + ';';
  118. i++;
  119. }
  120. else {
  121. result += '&#' + c + ';';
  122. }
  123. i++;
  124. }
  125. return result;
  126. };
  127. XmlEntities.encodeNonASCII = function (str) {
  128. return new XmlEntities().encodeNonASCII(str);
  129. };
  130. return XmlEntities;
  131. }());
  132. exports.XmlEntities = XmlEntities;