You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

XMLStreamWriter.js 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. // Generated by CoffeeScript 2.4.1
  2. (function() {
  3. var NodeType, WriterState, XMLStreamWriter, XMLWriterBase,
  4. hasProp = {}.hasOwnProperty;
  5. NodeType = require('./NodeType');
  6. XMLWriterBase = require('./XMLWriterBase');
  7. WriterState = require('./WriterState');
  8. // Prints XML nodes to a stream
  9. module.exports = XMLStreamWriter = class XMLStreamWriter extends XMLWriterBase {
  10. // Initializes a new instance of `XMLStreamWriter`
  11. // `stream` output stream
  12. // `options.pretty` pretty prints the result
  13. // `options.indent` indentation string
  14. // `options.newline` newline sequence
  15. // `options.offset` a fixed number of indentations to add to every line
  16. // `options.allowEmpty` do not self close empty element tags
  17. // 'options.dontPrettyTextNodes' if any text is present in node, don't indent or LF
  18. // `options.spaceBeforeSlash` add a space before the closing slash of empty elements
  19. constructor(stream, options) {
  20. super(options);
  21. this.stream = stream;
  22. }
  23. endline(node, options, level) {
  24. if (node.isLastRootNode && options.state === WriterState.CloseTag) {
  25. return '';
  26. } else {
  27. return super.endline(node, options, level);
  28. }
  29. }
  30. document(doc, options) {
  31. var child, i, j, k, len1, len2, ref, ref1, results;
  32. ref = doc.children;
  33. // set a flag so that we don't insert a newline after the last root level node
  34. for (i = j = 0, len1 = ref.length; j < len1; i = ++j) {
  35. child = ref[i];
  36. child.isLastRootNode = i === doc.children.length - 1;
  37. }
  38. options = this.filterOptions(options);
  39. ref1 = doc.children;
  40. results = [];
  41. for (k = 0, len2 = ref1.length; k < len2; k++) {
  42. child = ref1[k];
  43. results.push(this.writeChildNode(child, options, 0));
  44. }
  45. return results;
  46. }
  47. cdata(node, options, level) {
  48. return this.stream.write(super.cdata(node, options, level));
  49. }
  50. comment(node, options, level) {
  51. return this.stream.write(super.comment(node, options, level));
  52. }
  53. declaration(node, options, level) {
  54. return this.stream.write(super.declaration(node, options, level));
  55. }
  56. docType(node, options, level) {
  57. var child, j, len1, ref;
  58. level || (level = 0);
  59. this.openNode(node, options, level);
  60. options.state = WriterState.OpenTag;
  61. this.stream.write(this.indent(node, options, level));
  62. this.stream.write('<!DOCTYPE ' + node.root().name);
  63. // external identifier
  64. if (node.pubID && node.sysID) {
  65. this.stream.write(' PUBLIC "' + node.pubID + '" "' + node.sysID + '"');
  66. } else if (node.sysID) {
  67. this.stream.write(' SYSTEM "' + node.sysID + '"');
  68. }
  69. // internal subset
  70. if (node.children.length > 0) {
  71. this.stream.write(' [');
  72. this.stream.write(this.endline(node, options, level));
  73. options.state = WriterState.InsideTag;
  74. ref = node.children;
  75. for (j = 0, len1 = ref.length; j < len1; j++) {
  76. child = ref[j];
  77. this.writeChildNode(child, options, level + 1);
  78. }
  79. options.state = WriterState.CloseTag;
  80. this.stream.write(']');
  81. }
  82. // close tag
  83. options.state = WriterState.CloseTag;
  84. this.stream.write(options.spaceBeforeSlash + '>');
  85. this.stream.write(this.endline(node, options, level));
  86. options.state = WriterState.None;
  87. return this.closeNode(node, options, level);
  88. }
  89. element(node, options, level) {
  90. var att, attLen, child, childNodeCount, firstChildNode, j, len, len1, name, prettySuppressed, r, ratt, ref, ref1, ref2, rline;
  91. level || (level = 0);
  92. // open tag
  93. this.openNode(node, options, level);
  94. options.state = WriterState.OpenTag;
  95. r = this.indent(node, options, level) + '<' + node.name;
  96. // attributes
  97. if (options.pretty && options.width > 0) {
  98. len = r.length;
  99. ref = node.attribs;
  100. for (name in ref) {
  101. if (!hasProp.call(ref, name)) continue;
  102. att = ref[name];
  103. ratt = this.attribute(att, options, level);
  104. attLen = ratt.length;
  105. if (len + attLen > options.width) {
  106. rline = this.indent(node, options, level + 1) + ratt;
  107. r += this.endline(node, options, level) + rline;
  108. len = rline.length;
  109. } else {
  110. rline = ' ' + ratt;
  111. r += rline;
  112. len += rline.length;
  113. }
  114. }
  115. } else {
  116. ref1 = node.attribs;
  117. for (name in ref1) {
  118. if (!hasProp.call(ref1, name)) continue;
  119. att = ref1[name];
  120. r += this.attribute(att, options, level);
  121. }
  122. }
  123. this.stream.write(r);
  124. childNodeCount = node.children.length;
  125. firstChildNode = childNodeCount === 0 ? null : node.children[0];
  126. if (childNodeCount === 0 || node.children.every(function(e) {
  127. return (e.type === NodeType.Text || e.type === NodeType.Raw || e.type === NodeType.CData) && e.value === '';
  128. })) {
  129. // empty element
  130. if (options.allowEmpty) {
  131. this.stream.write('>');
  132. options.state = WriterState.CloseTag;
  133. this.stream.write('</' + node.name + '>');
  134. } else {
  135. options.state = WriterState.CloseTag;
  136. this.stream.write(options.spaceBeforeSlash + '/>');
  137. }
  138. } else if (options.pretty && childNodeCount === 1 && (firstChildNode.type === NodeType.Text || firstChildNode.type === NodeType.Raw || firstChildNode.type === NodeType.CData) && (firstChildNode.value != null)) {
  139. // do not indent text-only nodes
  140. this.stream.write('>');
  141. options.state = WriterState.InsideTag;
  142. options.suppressPrettyCount++;
  143. prettySuppressed = true;
  144. this.writeChildNode(firstChildNode, options, level + 1);
  145. options.suppressPrettyCount--;
  146. prettySuppressed = false;
  147. options.state = WriterState.CloseTag;
  148. this.stream.write('</' + node.name + '>');
  149. } else {
  150. this.stream.write('>' + this.endline(node, options, level));
  151. options.state = WriterState.InsideTag;
  152. ref2 = node.children;
  153. // inner tags
  154. for (j = 0, len1 = ref2.length; j < len1; j++) {
  155. child = ref2[j];
  156. this.writeChildNode(child, options, level + 1);
  157. }
  158. // close tag
  159. options.state = WriterState.CloseTag;
  160. this.stream.write(this.indent(node, options, level) + '</' + node.name + '>');
  161. }
  162. this.stream.write(this.endline(node, options, level));
  163. options.state = WriterState.None;
  164. return this.closeNode(node, options, level);
  165. }
  166. processingInstruction(node, options, level) {
  167. return this.stream.write(super.processingInstruction(node, options, level));
  168. }
  169. raw(node, options, level) {
  170. return this.stream.write(super.raw(node, options, level));
  171. }
  172. text(node, options, level) {
  173. return this.stream.write(super.text(node, options, level));
  174. }
  175. dtdAttList(node, options, level) {
  176. return this.stream.write(super.dtdAttList(node, options, level));
  177. }
  178. dtdElement(node, options, level) {
  179. return this.stream.write(super.dtdElement(node, options, level));
  180. }
  181. dtdEntity(node, options, level) {
  182. return this.stream.write(super.dtdEntity(node, options, level));
  183. }
  184. dtdNotation(node, options, level) {
  185. return this.stream.write(super.dtdNotation(node, options, level));
  186. }
  187. };
  188. }).call(this);