No Description

XMLStringWriter.js 11KB

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