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.

155 lines
2.9 KiB

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