XMLStringWriter.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. // Generated by CoffeeScript 1.12.7
  2. (function() {
  3. var XMLCData, XMLComment, XMLDTDAttList, XMLDTDElement, XMLDTDEntity, XMLDTDNotation, XMLDeclaration, XMLDocType, XMLDummy, XMLElement, XMLProcessingInstruction, XMLRaw, XMLStringWriter, XMLText, XMLWriterBase,
  4. extend = function(child, parent) { for (var key in parent) { if (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
  5. hasProp = {}.hasOwnProperty;
  6. XMLDeclaration = require('./XMLDeclaration');
  7. XMLDocType = require('./XMLDocType');
  8. XMLCData = require('./XMLCData');
  9. XMLComment = require('./XMLComment');
  10. XMLElement = require('./XMLElement');
  11. XMLRaw = require('./XMLRaw');
  12. XMLText = require('./XMLText');
  13. XMLProcessingInstruction = require('./XMLProcessingInstruction');
  14. XMLDummy = require('./XMLDummy');
  15. XMLDTDAttList = require('./XMLDTDAttList');
  16. XMLDTDElement = require('./XMLDTDElement');
  17. XMLDTDEntity = require('./XMLDTDEntity');
  18. XMLDTDNotation = require('./XMLDTDNotation');
  19. XMLWriterBase = require('./XMLWriterBase');
  20. module.exports = XMLStringWriter = (function(superClass) {
  21. extend(XMLStringWriter, superClass);
  22. function XMLStringWriter(options) {
  23. XMLStringWriter.__super__.constructor.call(this, options);
  24. }
  25. XMLStringWriter.prototype.document = function(doc) {
  26. var child, i, len, r, ref;
  27. this.textispresent = false;
  28. r = '';
  29. ref = doc.children;
  30. for (i = 0, len = ref.length; i < len; i++) {
  31. child = ref[i];
  32. if (child instanceof XMLDummy) {
  33. continue;
  34. }
  35. r += (function() {
  36. switch (false) {
  37. case !(child instanceof XMLDeclaration):
  38. return this.declaration(child);
  39. case !(child instanceof XMLDocType):
  40. return this.docType(child);
  41. case !(child instanceof XMLComment):
  42. return this.comment(child);
  43. case !(child instanceof XMLProcessingInstruction):
  44. return this.processingInstruction(child);
  45. default:
  46. return this.element(child, 0);
  47. }
  48. }).call(this);
  49. }
  50. if (this.pretty && r.slice(-this.newline.length) === this.newline) {
  51. r = r.slice(0, -this.newline.length);
  52. }
  53. return r;
  54. };
  55. XMLStringWriter.prototype.attribute = function(att) {
  56. return ' ' + att.name + '="' + att.value + '"';
  57. };
  58. XMLStringWriter.prototype.cdata = function(node, level) {
  59. return this.space(level) + '<![CDATA[' + node.text + ']]>' + this.newline;
  60. };
  61. XMLStringWriter.prototype.comment = function(node, level) {
  62. return this.space(level) + '<!-- ' + node.text + ' -->' + this.newline;
  63. };
  64. XMLStringWriter.prototype.declaration = function(node, level) {
  65. var r;
  66. r = this.space(level);
  67. r += '<?xml version="' + node.version + '"';
  68. if (node.encoding != null) {
  69. r += ' encoding="' + node.encoding + '"';
  70. }
  71. if (node.standalone != null) {
  72. r += ' standalone="' + node.standalone + '"';
  73. }
  74. r += this.spacebeforeslash + '?>';
  75. r += this.newline;
  76. return r;
  77. };
  78. XMLStringWriter.prototype.docType = function(node, level) {
  79. var child, i, len, r, ref;
  80. level || (level = 0);
  81. r = this.space(level);
  82. r += '<!DOCTYPE ' + node.root().name;
  83. if (node.pubID && node.sysID) {
  84. r += ' PUBLIC "' + node.pubID + '" "' + node.sysID + '"';
  85. } else if (node.sysID) {
  86. r += ' SYSTEM "' + node.sysID + '"';
  87. }
  88. if (node.children.length > 0) {
  89. r += ' [';
  90. r += this.newline;
  91. ref = node.children;
  92. for (i = 0, len = ref.length; i < len; i++) {
  93. child = ref[i];
  94. r += (function() {
  95. switch (false) {
  96. case !(child instanceof XMLDTDAttList):
  97. return this.dtdAttList(child, level + 1);
  98. case !(child instanceof XMLDTDElement):
  99. return this.dtdElement(child, level + 1);
  100. case !(child instanceof XMLDTDEntity):
  101. return this.dtdEntity(child, level + 1);
  102. case !(child instanceof XMLDTDNotation):
  103. return this.dtdNotation(child, level + 1);
  104. case !(child instanceof XMLCData):
  105. return this.cdata(child, level + 1);
  106. case !(child instanceof XMLComment):
  107. return this.comment(child, level + 1);
  108. case !(child instanceof XMLProcessingInstruction):
  109. return this.processingInstruction(child, level + 1);
  110. default:
  111. throw new Error("Unknown DTD node type: " + child.constructor.name);
  112. }
  113. }).call(this);
  114. }
  115. r += ']';
  116. }
  117. r += this.spacebeforeslash + '>';
  118. r += this.newline;
  119. return r;
  120. };
  121. XMLStringWriter.prototype.element = function(node, level) {
  122. var att, child, i, j, len, len1, name, r, ref, ref1, ref2, space, textispresentwasset;
  123. level || (level = 0);
  124. textispresentwasset = false;
  125. if (this.textispresent) {
  126. this.newline = '';
  127. this.pretty = false;
  128. } else {
  129. this.newline = this.newlinedefault;
  130. this.pretty = this.prettydefault;
  131. }
  132. space = this.space(level);
  133. r = '';
  134. r += space + '<' + node.name;
  135. ref = node.attributes;
  136. for (name in ref) {
  137. if (!hasProp.call(ref, name)) continue;
  138. att = ref[name];
  139. r += this.attribute(att);
  140. }
  141. if (node.children.length === 0 || node.children.every(function(e) {
  142. return e.value === '';
  143. })) {
  144. if (this.allowEmpty) {
  145. r += '></' + node.name + '>' + this.newline;
  146. } else {
  147. r += this.spacebeforeslash + '/>' + this.newline;
  148. }
  149. } else if (this.pretty && node.children.length === 1 && (node.children[0].value != null)) {
  150. r += '>';
  151. r += node.children[0].value;
  152. r += '</' + node.name + '>' + this.newline;
  153. } else {
  154. if (this.dontprettytextnodes) {
  155. ref1 = node.children;
  156. for (i = 0, len = ref1.length; i < len; i++) {
  157. child = ref1[i];
  158. if (child.value != null) {
  159. this.textispresent++;
  160. textispresentwasset = true;
  161. break;
  162. }
  163. }
  164. }
  165. if (this.textispresent) {
  166. this.newline = '';
  167. this.pretty = false;
  168. space = this.space(level);
  169. }
  170. r += '>' + this.newline;
  171. ref2 = node.children;
  172. for (j = 0, len1 = ref2.length; j < len1; j++) {
  173. child = ref2[j];
  174. r += (function() {
  175. switch (false) {
  176. case !(child instanceof XMLCData):
  177. return this.cdata(child, level + 1);
  178. case !(child instanceof XMLComment):
  179. return this.comment(child, level + 1);
  180. case !(child instanceof XMLElement):
  181. return this.element(child, level + 1);
  182. case !(child instanceof XMLRaw):
  183. return this.raw(child, level + 1);
  184. case !(child instanceof XMLText):
  185. return this.text(child, level + 1);
  186. case !(child instanceof XMLProcessingInstruction):
  187. return this.processingInstruction(child, level + 1);
  188. case !(child instanceof XMLDummy):
  189. return '';
  190. default:
  191. throw new Error("Unknown XML node type: " + child.constructor.name);
  192. }
  193. }).call(this);
  194. }
  195. if (textispresentwasset) {
  196. this.textispresent--;
  197. }
  198. if (!this.textispresent) {
  199. this.newline = this.newlinedefault;
  200. this.pretty = this.prettydefault;
  201. }
  202. r += space + '</' + node.name + '>' + this.newline;
  203. }
  204. return r;
  205. };
  206. XMLStringWriter.prototype.processingInstruction = function(node, level) {
  207. var r;
  208. r = this.space(level) + '<?' + node.target;
  209. if (node.value) {
  210. r += ' ' + node.value;
  211. }
  212. r += this.spacebeforeslash + '?>' + this.newline;
  213. return r;
  214. };
  215. XMLStringWriter.prototype.raw = function(node, level) {
  216. return this.space(level) + node.value + this.newline;
  217. };
  218. XMLStringWriter.prototype.text = function(node, level) {
  219. return this.space(level) + node.value + this.newline;
  220. };
  221. XMLStringWriter.prototype.dtdAttList = function(node, level) {
  222. var r;
  223. r = this.space(level) + '<!ATTLIST ' + node.elementName + ' ' + node.attributeName + ' ' + node.attributeType;
  224. if (node.defaultValueType !== '#DEFAULT') {
  225. r += ' ' + node.defaultValueType;
  226. }
  227. if (node.defaultValue) {
  228. r += ' "' + node.defaultValue + '"';
  229. }
  230. r += this.spacebeforeslash + '>' + this.newline;
  231. return r;
  232. };
  233. XMLStringWriter.prototype.dtdElement = function(node, level) {
  234. return this.space(level) + '<!ELEMENT ' + node.name + ' ' + node.value + this.spacebeforeslash + '>' + this.newline;
  235. };
  236. XMLStringWriter.prototype.dtdEntity = function(node, level) {
  237. var r;
  238. r = this.space(level) + '<!ENTITY';
  239. if (node.pe) {
  240. r += ' %';
  241. }
  242. r += ' ' + node.name;
  243. if (node.value) {
  244. r += ' "' + node.value + '"';
  245. } else {
  246. if (node.pubID && node.sysID) {
  247. r += ' PUBLIC "' + node.pubID + '" "' + node.sysID + '"';
  248. } else if (node.sysID) {
  249. r += ' SYSTEM "' + node.sysID + '"';
  250. }
  251. if (node.nData) {
  252. r += ' NDATA ' + node.nData;
  253. }
  254. }
  255. r += this.spacebeforeslash + '>' + this.newline;
  256. return r;
  257. };
  258. XMLStringWriter.prototype.dtdNotation = function(node, level) {
  259. var r;
  260. r = this.space(level) + '<!NOTATION ' + node.name;
  261. if (node.pubID && node.sysID) {
  262. r += ' PUBLIC "' + node.pubID + '" "' + node.sysID + '"';
  263. } else if (node.pubID) {
  264. r += ' PUBLIC "' + node.pubID + '"';
  265. } else if (node.sysID) {
  266. r += ' SYSTEM "' + node.sysID + '"';
  267. }
  268. r += this.spacebeforeslash + '>' + this.newline;
  269. return r;
  270. };
  271. XMLStringWriter.prototype.openNode = function(node, level) {
  272. var att, name, r, ref;
  273. level || (level = 0);
  274. if (node instanceof XMLElement) {
  275. r = this.space(level) + '<' + node.name;
  276. ref = node.attributes;
  277. for (name in ref) {
  278. if (!hasProp.call(ref, name)) continue;
  279. att = ref[name];
  280. r += this.attribute(att);
  281. }
  282. r += (node.children ? '>' : '/>') + this.newline;
  283. return r;
  284. } else {
  285. r = this.space(level) + '<!DOCTYPE ' + node.rootNodeName;
  286. if (node.pubID && node.sysID) {
  287. r += ' PUBLIC "' + node.pubID + '" "' + node.sysID + '"';
  288. } else if (node.sysID) {
  289. r += ' SYSTEM "' + node.sysID + '"';
  290. }
  291. r += (node.children ? ' [' : '>') + this.newline;
  292. return r;
  293. }
  294. };
  295. XMLStringWriter.prototype.closeNode = function(node, level) {
  296. level || (level = 0);
  297. switch (false) {
  298. case !(node instanceof XMLElement):
  299. return this.space(level) + '</' + node.name + '>' + this.newline;
  300. case !(node instanceof XMLDocType):
  301. return this.space(level) + ']>' + this.newline;
  302. }
  303. };
  304. return XMLStringWriter;
  305. })(XMLWriterBase);
  306. }).call(this);