XMLStringifier.js 5.4 KB

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