XMLStringifier.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. // Generated by CoffeeScript 1.12.7
  2. (function() {
  3. var XMLStringifier,
  4. bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; },
  5. hasProp = {}.hasOwnProperty;
  6. module.exports = XMLStringifier = (function() {
  7. function XMLStringifier(options) {
  8. this.assertLegalChar = bind(this.assertLegalChar, this);
  9. var key, ref, value;
  10. options || (options = {});
  11. this.noDoubleEncoding = options.noDoubleEncoding;
  12. ref = options.stringify || {};
  13. for (key in ref) {
  14. if (!hasProp.call(ref, key)) continue;
  15. value = ref[key];
  16. this[key] = value;
  17. }
  18. }
  19. XMLStringifier.prototype.eleName = function(val) {
  20. val = '' + val || '';
  21. return this.assertLegalChar(val);
  22. };
  23. XMLStringifier.prototype.eleText = function(val) {
  24. val = '' + val || '';
  25. return this.assertLegalChar(this.elEscape(val));
  26. };
  27. XMLStringifier.prototype.cdata = function(val) {
  28. val = '' + val || '';
  29. val = val.replace(']]>', ']]]]><![CDATA[>');
  30. return this.assertLegalChar(val);
  31. };
  32. XMLStringifier.prototype.comment = function(val) {
  33. val = '' + val || '';
  34. if (val.match(/--/)) {
  35. throw new Error("Comment text cannot contain double-hypen: " + val);
  36. }
  37. return this.assertLegalChar(val);
  38. };
  39. XMLStringifier.prototype.raw = function(val) {
  40. return '' + val || '';
  41. };
  42. XMLStringifier.prototype.attName = function(val) {
  43. return val = '' + val || '';
  44. };
  45. XMLStringifier.prototype.attValue = function(val) {
  46. val = '' + val || '';
  47. return this.attEscape(val);
  48. };
  49. XMLStringifier.prototype.insTarget = function(val) {
  50. return '' + val || '';
  51. };
  52. XMLStringifier.prototype.insValue = function(val) {
  53. val = '' + val || '';
  54. if (val.match(/\?>/)) {
  55. throw new Error("Invalid processing instruction value: " + val);
  56. }
  57. return val;
  58. };
  59. XMLStringifier.prototype.xmlVersion = function(val) {
  60. val = '' + val || '';
  61. if (!val.match(/1\.[0-9]+/)) {
  62. throw new Error("Invalid version number: " + val);
  63. }
  64. return val;
  65. };
  66. XMLStringifier.prototype.xmlEncoding = function(val) {
  67. val = '' + val || '';
  68. if (!val.match(/^[A-Za-z](?:[A-Za-z0-9._-])*$/)) {
  69. throw new Error("Invalid encoding: " + val);
  70. }
  71. return val;
  72. };
  73. XMLStringifier.prototype.xmlStandalone = function(val) {
  74. if (val) {
  75. return "yes";
  76. } else {
  77. return "no";
  78. }
  79. };
  80. XMLStringifier.prototype.dtdPubID = function(val) {
  81. return '' + val || '';
  82. };
  83. XMLStringifier.prototype.dtdSysID = function(val) {
  84. return '' + val || '';
  85. };
  86. XMLStringifier.prototype.dtdElementValue = function(val) {
  87. return '' + val || '';
  88. };
  89. XMLStringifier.prototype.dtdAttType = function(val) {
  90. return '' + val || '';
  91. };
  92. XMLStringifier.prototype.dtdAttDefault = function(val) {
  93. if (val != null) {
  94. return '' + val || '';
  95. } else {
  96. return val;
  97. }
  98. };
  99. XMLStringifier.prototype.dtdEntityValue = function(val) {
  100. return '' + val || '';
  101. };
  102. XMLStringifier.prototype.dtdNData = function(val) {
  103. return '' + val || '';
  104. };
  105. XMLStringifier.prototype.convertAttKey = '@';
  106. XMLStringifier.prototype.convertPIKey = '?';
  107. XMLStringifier.prototype.convertTextKey = '#text';
  108. XMLStringifier.prototype.convertCDataKey = '#cdata';
  109. XMLStringifier.prototype.convertCommentKey = '#comment';
  110. XMLStringifier.prototype.convertRawKey = '#raw';
  111. XMLStringifier.prototype.assertLegalChar = function(str) {
  112. var res;
  113. res = str.match(/[\0\uFFFE\uFFFF]|[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?:[^\uD800-\uDBFF]|^)[\uDC00-\uDFFF]/);
  114. if (res) {
  115. throw new Error("Invalid character in string: " + str + " at index " + res.index);
  116. }
  117. return str;
  118. };
  119. XMLStringifier.prototype.elEscape = function(str) {
  120. var ampregex;
  121. ampregex = this.noDoubleEncoding ? /(?!&\S+;)&/g : /&/g;
  122. return str.replace(ampregex, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/\r/g, '&#xD;');
  123. };
  124. XMLStringifier.prototype.attEscape = function(str) {
  125. var ampregex;
  126. ampregex = this.noDoubleEncoding ? /(?!&\S+;)&/g : /&/g;
  127. return str.replace(ampregex, '&amp;').replace(/</g, '&lt;').replace(/"/g, '&quot;').replace(/\t/g, '&#x9;').replace(/\n/g, '&#xA;').replace(/\r/g, '&#xD;');
  128. };
  129. return XMLStringifier;
  130. })();
  131. }).call(this);