xml-entities.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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.length === 0) {
  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.length === 0) {
  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. var strLength = str.length;
  84. if (strLength === 0) {
  85. return '';
  86. }
  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. var strLenght = str.length;
  119. if (strLenght === 0) {
  120. return '';
  121. }
  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;