XMLWriterBase.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. // Generated by CoffeeScript 1.12.7
  2. (function() {
  3. var NodeType, WriterState, XMLCData, XMLComment, XMLDTDAttList, XMLDTDElement, XMLDTDEntity, XMLDTDNotation, XMLDeclaration, XMLDocType, XMLDummy, XMLElement, XMLProcessingInstruction, XMLRaw, XMLText, XMLWriterBase, assign,
  4. hasProp = {}.hasOwnProperty;
  5. assign = require('./Utility').assign;
  6. NodeType = require('./NodeType');
  7. XMLDeclaration = require('./XMLDeclaration');
  8. XMLDocType = require('./XMLDocType');
  9. XMLCData = require('./XMLCData');
  10. XMLComment = require('./XMLComment');
  11. XMLElement = require('./XMLElement');
  12. XMLRaw = require('./XMLRaw');
  13. XMLText = require('./XMLText');
  14. XMLProcessingInstruction = require('./XMLProcessingInstruction');
  15. XMLDummy = require('./XMLDummy');
  16. XMLDTDAttList = require('./XMLDTDAttList');
  17. XMLDTDElement = require('./XMLDTDElement');
  18. XMLDTDEntity = require('./XMLDTDEntity');
  19. XMLDTDNotation = require('./XMLDTDNotation');
  20. WriterState = require('./WriterState');
  21. module.exports = XMLWriterBase = (function() {
  22. function XMLWriterBase(options) {
  23. var key, ref, value;
  24. options || (options = {});
  25. this.options = options;
  26. ref = options.writer || {};
  27. for (key in ref) {
  28. if (!hasProp.call(ref, key)) continue;
  29. value = ref[key];
  30. this["_" + key] = this[key];
  31. this[key] = value;
  32. }
  33. }
  34. XMLWriterBase.prototype.filterOptions = function(options) {
  35. var filteredOptions, ref, ref1, ref2, ref3, ref4, ref5, ref6;
  36. options || (options = {});
  37. options = assign({}, this.options, options);
  38. filteredOptions = {
  39. writer: this
  40. };
  41. filteredOptions.pretty = options.pretty || false;
  42. filteredOptions.allowEmpty = options.allowEmpty || false;
  43. filteredOptions.indent = (ref = options.indent) != null ? ref : ' ';
  44. filteredOptions.newline = (ref1 = options.newline) != null ? ref1 : '\n';
  45. filteredOptions.offset = (ref2 = options.offset) != null ? ref2 : 0;
  46. filteredOptions.dontPrettyTextNodes = (ref3 = (ref4 = options.dontPrettyTextNodes) != null ? ref4 : options.dontprettytextnodes) != null ? ref3 : 0;
  47. filteredOptions.spaceBeforeSlash = (ref5 = (ref6 = options.spaceBeforeSlash) != null ? ref6 : options.spacebeforeslash) != null ? ref5 : '';
  48. if (filteredOptions.spaceBeforeSlash === true) {
  49. filteredOptions.spaceBeforeSlash = ' ';
  50. }
  51. filteredOptions.suppressPrettyCount = 0;
  52. filteredOptions.user = {};
  53. filteredOptions.state = WriterState.None;
  54. return filteredOptions;
  55. };
  56. XMLWriterBase.prototype.indent = function(node, options, level) {
  57. var indentLevel;
  58. if (!options.pretty || options.suppressPrettyCount) {
  59. return '';
  60. } else if (options.pretty) {
  61. indentLevel = (level || 0) + options.offset + 1;
  62. if (indentLevel > 0) {
  63. return new Array(indentLevel).join(options.indent);
  64. }
  65. }
  66. return '';
  67. };
  68. XMLWriterBase.prototype.endline = function(node, options, level) {
  69. if (!options.pretty || options.suppressPrettyCount) {
  70. return '';
  71. } else {
  72. return options.newline;
  73. }
  74. };
  75. XMLWriterBase.prototype.attribute = function(att, options, level) {
  76. var r;
  77. this.openAttribute(att, options, level);
  78. r = ' ' + att.name + '="' + att.value + '"';
  79. this.closeAttribute(att, options, level);
  80. return r;
  81. };
  82. XMLWriterBase.prototype.cdata = function(node, options, level) {
  83. var r;
  84. this.openNode(node, options, level);
  85. options.state = WriterState.OpenTag;
  86. r = this.indent(node, options, level) + '<![CDATA[';
  87. options.state = WriterState.InsideTag;
  88. r += node.value;
  89. options.state = WriterState.CloseTag;
  90. r += ']]>' + this.endline(node, options, level);
  91. options.state = WriterState.None;
  92. this.closeNode(node, options, level);
  93. return r;
  94. };
  95. XMLWriterBase.prototype.comment = function(node, options, level) {
  96. var r;
  97. this.openNode(node, options, level);
  98. options.state = WriterState.OpenTag;
  99. r = this.indent(node, options, level) + '<!-- ';
  100. options.state = WriterState.InsideTag;
  101. r += node.value;
  102. options.state = WriterState.CloseTag;
  103. r += ' -->' + this.endline(node, options, level);
  104. options.state = WriterState.None;
  105. this.closeNode(node, options, level);
  106. return r;
  107. };
  108. XMLWriterBase.prototype.declaration = function(node, options, level) {
  109. var r;
  110. this.openNode(node, options, level);
  111. options.state = WriterState.OpenTag;
  112. r = this.indent(node, options, level) + '<?xml';
  113. options.state = WriterState.InsideTag;
  114. r += ' version="' + node.version + '"';
  115. if (node.encoding != null) {
  116. r += ' encoding="' + node.encoding + '"';
  117. }
  118. if (node.standalone != null) {
  119. r += ' standalone="' + node.standalone + '"';
  120. }
  121. options.state = WriterState.CloseTag;
  122. r += options.spaceBeforeSlash + '?>';
  123. r += this.endline(node, options, level);
  124. options.state = WriterState.None;
  125. this.closeNode(node, options, level);
  126. return r;
  127. };
  128. XMLWriterBase.prototype.docType = function(node, options, level) {
  129. var child, i, len, r, ref;
  130. level || (level = 0);
  131. this.openNode(node, options, level);
  132. options.state = WriterState.OpenTag;
  133. r = this.indent(node, options, level);
  134. r += '<!DOCTYPE ' + node.root().name;
  135. if (node.pubID && node.sysID) {
  136. r += ' PUBLIC "' + node.pubID + '" "' + node.sysID + '"';
  137. } else if (node.sysID) {
  138. r += ' SYSTEM "' + node.sysID + '"';
  139. }
  140. if (node.children.length > 0) {
  141. r += ' [';
  142. r += this.endline(node, options, level);
  143. options.state = WriterState.InsideTag;
  144. ref = node.children;
  145. for (i = 0, len = ref.length; i < len; i++) {
  146. child = ref[i];
  147. r += this.writeChildNode(child, options, level + 1);
  148. }
  149. options.state = WriterState.CloseTag;
  150. r += ']';
  151. }
  152. options.state = WriterState.CloseTag;
  153. r += options.spaceBeforeSlash + '>';
  154. r += this.endline(node, options, level);
  155. options.state = WriterState.None;
  156. this.closeNode(node, options, level);
  157. return r;
  158. };
  159. XMLWriterBase.prototype.element = function(node, options, level) {
  160. var att, child, childNodeCount, firstChildNode, i, j, len, len1, name, prettySuppressed, r, ref, ref1, ref2;
  161. level || (level = 0);
  162. prettySuppressed = false;
  163. r = '';
  164. this.openNode(node, options, level);
  165. options.state = WriterState.OpenTag;
  166. r += this.indent(node, options, level) + '<' + node.name;
  167. ref = node.attribs;
  168. for (name in ref) {
  169. if (!hasProp.call(ref, name)) continue;
  170. att = ref[name];
  171. r += this.attribute(att, options, level);
  172. }
  173. childNodeCount = node.children.length;
  174. firstChildNode = childNodeCount === 0 ? null : node.children[0];
  175. if (childNodeCount === 0 || node.children.every(function(e) {
  176. return (e.type === NodeType.Text || e.type === NodeType.Raw) && e.value === '';
  177. })) {
  178. if (options.allowEmpty) {
  179. r += '>';
  180. options.state = WriterState.CloseTag;
  181. r += '</' + node.name + '>' + this.endline(node, options, level);
  182. } else {
  183. options.state = WriterState.CloseTag;
  184. r += options.spaceBeforeSlash + '/>' + this.endline(node, options, level);
  185. }
  186. } else if (options.pretty && childNodeCount === 1 && (firstChildNode.type === NodeType.Text || firstChildNode.type === NodeType.Raw) && (firstChildNode.value != null)) {
  187. r += '>';
  188. options.state = WriterState.InsideTag;
  189. options.suppressPrettyCount++;
  190. prettySuppressed = true;
  191. r += this.writeChildNode(firstChildNode, options, level + 1);
  192. options.suppressPrettyCount--;
  193. prettySuppressed = false;
  194. options.state = WriterState.CloseTag;
  195. r += '</' + node.name + '>' + this.endline(node, options, level);
  196. } else {
  197. if (options.dontPrettyTextNodes) {
  198. ref1 = node.children;
  199. for (i = 0, len = ref1.length; i < len; i++) {
  200. child = ref1[i];
  201. if ((child.type === NodeType.Text || child.type === NodeType.Raw) && (child.value != null)) {
  202. options.suppressPrettyCount++;
  203. prettySuppressed = true;
  204. break;
  205. }
  206. }
  207. }
  208. r += '>' + this.endline(node, options, level);
  209. options.state = WriterState.InsideTag;
  210. ref2 = node.children;
  211. for (j = 0, len1 = ref2.length; j < len1; j++) {
  212. child = ref2[j];
  213. r += this.writeChildNode(child, options, level + 1);
  214. }
  215. options.state = WriterState.CloseTag;
  216. r += this.indent(node, options, level) + '</' + node.name + '>';
  217. if (prettySuppressed) {
  218. options.suppressPrettyCount--;
  219. }
  220. r += this.endline(node, options, level);
  221. options.state = WriterState.None;
  222. }
  223. this.closeNode(node, options, level);
  224. return r;
  225. };
  226. XMLWriterBase.prototype.writeChildNode = function(node, options, level) {
  227. switch (node.type) {
  228. case NodeType.CData:
  229. return this.cdata(node, options, level);
  230. case NodeType.Comment:
  231. return this.comment(node, options, level);
  232. case NodeType.Element:
  233. return this.element(node, options, level);
  234. case NodeType.Raw:
  235. return this.raw(node, options, level);
  236. case NodeType.Text:
  237. return this.text(node, options, level);
  238. case NodeType.ProcessingInstruction:
  239. return this.processingInstruction(node, options, level);
  240. case NodeType.Dummy:
  241. return '';
  242. case NodeType.Declaration:
  243. return this.declaration(node, options, level);
  244. case NodeType.DocType:
  245. return this.docType(node, options, level);
  246. case NodeType.AttributeDeclaration:
  247. return this.dtdAttList(node, options, level);
  248. case NodeType.ElementDeclaration:
  249. return this.dtdElement(node, options, level);
  250. case NodeType.EntityDeclaration:
  251. return this.dtdEntity(node, options, level);
  252. case NodeType.NotationDeclaration:
  253. return this.dtdNotation(node, options, level);
  254. default:
  255. throw new Error("Unknown XML node type: " + node.constructor.name);
  256. }
  257. };
  258. XMLWriterBase.prototype.processingInstruction = function(node, options, level) {
  259. var r;
  260. this.openNode(node, options, level);
  261. options.state = WriterState.OpenTag;
  262. r = this.indent(node, options, level) + '<?';
  263. options.state = WriterState.InsideTag;
  264. r += node.target;
  265. if (node.value) {
  266. r += ' ' + node.value;
  267. }
  268. options.state = WriterState.CloseTag;
  269. r += options.spaceBeforeSlash + '?>';
  270. r += this.endline(node, options, level);
  271. options.state = WriterState.None;
  272. this.closeNode(node, options, level);
  273. return r;
  274. };
  275. XMLWriterBase.prototype.raw = function(node, options, level) {
  276. var r;
  277. this.openNode(node, options, level);
  278. options.state = WriterState.OpenTag;
  279. r = this.indent(node, options, level);
  280. options.state = WriterState.InsideTag;
  281. r += node.value;
  282. options.state = WriterState.CloseTag;
  283. r += this.endline(node, options, level);
  284. options.state = WriterState.None;
  285. this.closeNode(node, options, level);
  286. return r;
  287. };
  288. XMLWriterBase.prototype.text = function(node, options, level) {
  289. var r;
  290. this.openNode(node, options, level);
  291. options.state = WriterState.OpenTag;
  292. r = this.indent(node, options, level);
  293. options.state = WriterState.InsideTag;
  294. r += node.value;
  295. options.state = WriterState.CloseTag;
  296. r += this.endline(node, options, level);
  297. options.state = WriterState.None;
  298. this.closeNode(node, options, level);
  299. return r;
  300. };
  301. XMLWriterBase.prototype.dtdAttList = function(node, options, level) {
  302. var r;
  303. this.openNode(node, options, level);
  304. options.state = WriterState.OpenTag;
  305. r = this.indent(node, options, level) + '<!ATTLIST';
  306. options.state = WriterState.InsideTag;
  307. r += ' ' + node.elementName + ' ' + node.attributeName + ' ' + node.attributeType;
  308. if (node.defaultValueType !== '#DEFAULT') {
  309. r += ' ' + node.defaultValueType;
  310. }
  311. if (node.defaultValue) {
  312. r += ' "' + node.defaultValue + '"';
  313. }
  314. options.state = WriterState.CloseTag;
  315. r += options.spaceBeforeSlash + '>' + this.endline(node, options, level);
  316. options.state = WriterState.None;
  317. this.closeNode(node, options, level);
  318. return r;
  319. };
  320. XMLWriterBase.prototype.dtdElement = function(node, options, level) {
  321. var r;
  322. this.openNode(node, options, level);
  323. options.state = WriterState.OpenTag;
  324. r = this.indent(node, options, level) + '<!ELEMENT';
  325. options.state = WriterState.InsideTag;
  326. r += ' ' + node.name + ' ' + node.value;
  327. options.state = WriterState.CloseTag;
  328. r += options.spaceBeforeSlash + '>' + this.endline(node, options, level);
  329. options.state = WriterState.None;
  330. this.closeNode(node, options, level);
  331. return r;
  332. };
  333. XMLWriterBase.prototype.dtdEntity = function(node, options, level) {
  334. var r;
  335. this.openNode(node, options, level);
  336. options.state = WriterState.OpenTag;
  337. r = this.indent(node, options, level) + '<!ENTITY';
  338. options.state = WriterState.InsideTag;
  339. if (node.pe) {
  340. r += ' %';
  341. }
  342. r += ' ' + node.name;
  343. if (node.value) {
  344. r += ' "' + node.value + '"';
  345. } else {
  346. if (node.pubID && node.sysID) {
  347. r += ' PUBLIC "' + node.pubID + '" "' + node.sysID + '"';
  348. } else if (node.sysID) {
  349. r += ' SYSTEM "' + node.sysID + '"';
  350. }
  351. if (node.nData) {
  352. r += ' NDATA ' + node.nData;
  353. }
  354. }
  355. options.state = WriterState.CloseTag;
  356. r += options.spaceBeforeSlash + '>' + this.endline(node, options, level);
  357. options.state = WriterState.None;
  358. this.closeNode(node, options, level);
  359. return r;
  360. };
  361. XMLWriterBase.prototype.dtdNotation = function(node, options, level) {
  362. var r;
  363. this.openNode(node, options, level);
  364. options.state = WriterState.OpenTag;
  365. r = this.indent(node, options, level) + '<!NOTATION';
  366. options.state = WriterState.InsideTag;
  367. r += ' ' + node.name;
  368. if (node.pubID && node.sysID) {
  369. r += ' PUBLIC "' + node.pubID + '" "' + node.sysID + '"';
  370. } else if (node.pubID) {
  371. r += ' PUBLIC "' + node.pubID + '"';
  372. } else if (node.sysID) {
  373. r += ' SYSTEM "' + node.sysID + '"';
  374. }
  375. options.state = WriterState.CloseTag;
  376. r += options.spaceBeforeSlash + '>' + this.endline(node, options, level);
  377. options.state = WriterState.None;
  378. this.closeNode(node, options, level);
  379. return r;
  380. };
  381. XMLWriterBase.prototype.openNode = function(node, options, level) {};
  382. XMLWriterBase.prototype.closeNode = function(node, options, level) {};
  383. XMLWriterBase.prototype.openAttribute = function(att, options, level) {};
  384. XMLWriterBase.prototype.closeAttribute = function(att, options, level) {};
  385. return XMLWriterBase;
  386. })();
  387. }).call(this);