XMLStringifier.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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.assertLegalName = bind(this.assertLegalName, this);
  9. this.assertLegalChar = bind(this.assertLegalChar, this);
  10. var key, ref, value;
  11. options || (options = {});
  12. this.options = options;
  13. if (!this.options.version) {
  14. this.options.version = '1.0';
  15. }
  16. ref = options.stringify || {};
  17. for (key in ref) {
  18. if (!hasProp.call(ref, key)) continue;
  19. value = ref[key];
  20. this[key] = value;
  21. }
  22. }
  23. XMLStringifier.prototype.name = function(val) {
  24. if (this.options.noValidation) {
  25. return val;
  26. }
  27. return this.assertLegalName('' + val || '');
  28. };
  29. XMLStringifier.prototype.text = function(val) {
  30. if (this.options.noValidation) {
  31. return val;
  32. }
  33. return this.assertLegalChar(this.textEscape('' + val || ''));
  34. };
  35. XMLStringifier.prototype.cdata = function(val) {
  36. if (this.options.noValidation) {
  37. return val;
  38. }
  39. val = '' + val || '';
  40. val = val.replace(']]>', ']]]]><![CDATA[>');
  41. return this.assertLegalChar(val);
  42. };
  43. XMLStringifier.prototype.comment = function(val) {
  44. if (this.options.noValidation) {
  45. return val;
  46. }
  47. val = '' + val || '';
  48. if (val.match(/--/)) {
  49. throw new Error("Comment text cannot contain double-hypen: " + val);
  50. }
  51. return this.assertLegalChar(val);
  52. };
  53. XMLStringifier.prototype.raw = function(val) {
  54. if (this.options.noValidation) {
  55. return val;
  56. }
  57. return '' + val || '';
  58. };
  59. XMLStringifier.prototype.attValue = function(val) {
  60. if (this.options.noValidation) {
  61. return val;
  62. }
  63. return this.assertLegalChar(this.attEscape(val = '' + val || ''));
  64. };
  65. XMLStringifier.prototype.insTarget = function(val) {
  66. if (this.options.noValidation) {
  67. return val;
  68. }
  69. return this.assertLegalChar('' + val || '');
  70. };
  71. XMLStringifier.prototype.insValue = function(val) {
  72. if (this.options.noValidation) {
  73. return val;
  74. }
  75. val = '' + val || '';
  76. if (val.match(/\?>/)) {
  77. throw new Error("Invalid processing instruction value: " + val);
  78. }
  79. return this.assertLegalChar(val);
  80. };
  81. XMLStringifier.prototype.xmlVersion = function(val) {
  82. if (this.options.noValidation) {
  83. return val;
  84. }
  85. val = '' + val || '';
  86. if (!val.match(/1\.[0-9]+/)) {
  87. throw new Error("Invalid version number: " + val);
  88. }
  89. return val;
  90. };
  91. XMLStringifier.prototype.xmlEncoding = function(val) {
  92. if (this.options.noValidation) {
  93. return val;
  94. }
  95. val = '' + val || '';
  96. if (!val.match(/^[A-Za-z](?:[A-Za-z0-9._-])*$/)) {
  97. throw new Error("Invalid encoding: " + val);
  98. }
  99. return this.assertLegalChar(val);
  100. };
  101. XMLStringifier.prototype.xmlStandalone = function(val) {
  102. if (this.options.noValidation) {
  103. return val;
  104. }
  105. if (val) {
  106. return "yes";
  107. } else {
  108. return "no";
  109. }
  110. };
  111. XMLStringifier.prototype.dtdPubID = function(val) {
  112. if (this.options.noValidation) {
  113. return val;
  114. }
  115. return this.assertLegalChar('' + val || '');
  116. };
  117. XMLStringifier.prototype.dtdSysID = function(val) {
  118. if (this.options.noValidation) {
  119. return val;
  120. }
  121. return this.assertLegalChar('' + val || '');
  122. };
  123. XMLStringifier.prototype.dtdElementValue = function(val) {
  124. if (this.options.noValidation) {
  125. return val;
  126. }
  127. return this.assertLegalChar('' + val || '');
  128. };
  129. XMLStringifier.prototype.dtdAttType = function(val) {
  130. if (this.options.noValidation) {
  131. return val;
  132. }
  133. return this.assertLegalChar('' + val || '');
  134. };
  135. XMLStringifier.prototype.dtdAttDefault = function(val) {
  136. if (this.options.noValidation) {
  137. return val;
  138. }
  139. return this.assertLegalChar('' + val || '');
  140. };
  141. XMLStringifier.prototype.dtdEntityValue = function(val) {
  142. if (this.options.noValidation) {
  143. return val;
  144. }
  145. return this.assertLegalChar('' + val || '');
  146. };
  147. XMLStringifier.prototype.dtdNData = function(val) {
  148. if (this.options.noValidation) {
  149. return val;
  150. }
  151. return this.assertLegalChar('' + val || '');
  152. };
  153. XMLStringifier.prototype.convertAttKey = '@';
  154. XMLStringifier.prototype.convertPIKey = '?';
  155. XMLStringifier.prototype.convertTextKey = '#text';
  156. XMLStringifier.prototype.convertCDataKey = '#cdata';
  157. XMLStringifier.prototype.convertCommentKey = '#comment';
  158. XMLStringifier.prototype.convertRawKey = '#raw';
  159. XMLStringifier.prototype.assertLegalChar = function(str) {
  160. var regex, res;
  161. if (this.options.noValidation) {
  162. return str;
  163. }
  164. regex = '';
  165. if (this.options.version === '1.0') {
  166. regex = /[\0-\x08\x0B\f\x0E-\x1F\uFFFE\uFFFF]|[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?:[^\uD800-\uDBFF]|^)[\uDC00-\uDFFF]/;
  167. if (res = str.match(regex)) {
  168. throw new Error("Invalid character in string: " + str + " at index " + res.index);
  169. }
  170. } else if (this.options.version === '1.1') {
  171. regex = /[\0\uFFFE\uFFFF]|[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?:[^\uD800-\uDBFF]|^)[\uDC00-\uDFFF]/;
  172. if (res = str.match(regex)) {
  173. throw new Error("Invalid character in string: " + str + " at index " + res.index);
  174. }
  175. }
  176. return str;
  177. };
  178. XMLStringifier.prototype.assertLegalName = function(str) {
  179. var regex;
  180. if (this.options.noValidation) {
  181. return str;
  182. }
  183. this.assertLegalChar(str);
  184. regex = /^([:A-Z_a-z\xC0-\xD6\xD8-\xF6\xF8-\u02FF\u0370-\u037D\u037F-\u1FFF\u200C\u200D\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD]|[\uD800-\uDB7F][\uDC00-\uDFFF])([\x2D\.0-:A-Z_a-z\xB7\xC0-\xD6\xD8-\xF6\xF8-\u037D\u037F-\u1FFF\u200C\u200D\u203F\u2040\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD]|[\uD800-\uDB7F][\uDC00-\uDFFF])*$/;
  185. if (!str.match(regex)) {
  186. throw new Error("Invalid character in name");
  187. }
  188. return str;
  189. };
  190. XMLStringifier.prototype.textEscape = function(str) {
  191. var ampregex;
  192. if (this.options.noValidation) {
  193. return str;
  194. }
  195. ampregex = this.options.noDoubleEncoding ? /(?!&\S+;)&/g : /&/g;
  196. return str.replace(ampregex, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/\r/g, '&#xD;');
  197. };
  198. XMLStringifier.prototype.attEscape = function(str) {
  199. var ampregex;
  200. if (this.options.noValidation) {
  201. return str;
  202. }
  203. ampregex = this.options.noDoubleEncoding ? /(?!&\S+;)&/g : /&/g;
  204. return str.replace(ampregex, '&amp;').replace(/</g, '&lt;').replace(/"/g, '&quot;').replace(/\t/g, '&#x9;').replace(/\n/g, '&#xA;').replace(/\r/g, '&#xD;');
  205. };
  206. return XMLStringifier;
  207. })();
  208. }).call(this);