xml-entities.js 3.0 KB

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