XMLStreamWriter.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. // Generated by CoffeeScript 1.12.7
  2. (function() {
  3. var NodeType, WriterState, XMLStreamWriter, 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. NodeType = require('./NodeType');
  7. XMLWriterBase = require('./XMLWriterBase');
  8. WriterState = require('./WriterState');
  9. module.exports = XMLStreamWriter = (function(superClass) {
  10. extend(XMLStreamWriter, superClass);
  11. function XMLStreamWriter(stream, options) {
  12. this.stream = stream;
  13. XMLStreamWriter.__super__.constructor.call(this, options);
  14. }
  15. XMLStreamWriter.prototype.endline = function(node, options, level) {
  16. if (node.isLastRootNode && options.state === WriterState.CloseTag) {
  17. return '';
  18. } else {
  19. return XMLStreamWriter.__super__.endline.call(this, node, options, level);
  20. }
  21. };
  22. XMLStreamWriter.prototype.document = function(doc, options) {
  23. var child, i, j, k, len, len1, ref, ref1, results;
  24. ref = doc.children;
  25. for (i = j = 0, len = ref.length; j < len; i = ++j) {
  26. child = ref[i];
  27. child.isLastRootNode = i === doc.children.length - 1;
  28. }
  29. options = this.filterOptions(options);
  30. ref1 = doc.children;
  31. results = [];
  32. for (k = 0, len1 = ref1.length; k < len1; k++) {
  33. child = ref1[k];
  34. results.push(this.writeChildNode(child, options, 0));
  35. }
  36. return results;
  37. };
  38. XMLStreamWriter.prototype.attribute = function(att, options, level) {
  39. return this.stream.write(XMLStreamWriter.__super__.attribute.call(this, att, options, level));
  40. };
  41. XMLStreamWriter.prototype.cdata = function(node, options, level) {
  42. return this.stream.write(XMLStreamWriter.__super__.cdata.call(this, node, options, level));
  43. };
  44. XMLStreamWriter.prototype.comment = function(node, options, level) {
  45. return this.stream.write(XMLStreamWriter.__super__.comment.call(this, node, options, level));
  46. };
  47. XMLStreamWriter.prototype.declaration = function(node, options, level) {
  48. return this.stream.write(XMLStreamWriter.__super__.declaration.call(this, node, options, level));
  49. };
  50. XMLStreamWriter.prototype.docType = function(node, options, level) {
  51. var child, j, len, ref;
  52. level || (level = 0);
  53. this.openNode(node, options, level);
  54. options.state = WriterState.OpenTag;
  55. this.stream.write(this.indent(node, options, level));
  56. this.stream.write('<!DOCTYPE ' + node.root().name);
  57. if (node.pubID && node.sysID) {
  58. this.stream.write(' PUBLIC "' + node.pubID + '" "' + node.sysID + '"');
  59. } else if (node.sysID) {
  60. this.stream.write(' SYSTEM "' + node.sysID + '"');
  61. }
  62. if (node.children.length > 0) {
  63. this.stream.write(' [');
  64. this.stream.write(this.endline(node, options, level));
  65. options.state = WriterState.InsideTag;
  66. ref = node.children;
  67. for (j = 0, len = ref.length; j < len; j++) {
  68. child = ref[j];
  69. this.writeChildNode(child, options, level + 1);
  70. }
  71. options.state = WriterState.CloseTag;
  72. this.stream.write(']');
  73. }
  74. options.state = WriterState.CloseTag;
  75. this.stream.write(options.spaceBeforeSlash + '>');
  76. this.stream.write(this.endline(node, options, level));
  77. options.state = WriterState.None;
  78. return this.closeNode(node, options, level);
  79. };
  80. XMLStreamWriter.prototype.element = function(node, options, level) {
  81. var att, child, childNodeCount, firstChildNode, j, len, name, prettySuppressed, ref, ref1;
  82. level || (level = 0);
  83. this.openNode(node, options, level);
  84. options.state = WriterState.OpenTag;
  85. this.stream.write(this.indent(node, options, level) + '<' + node.name);
  86. ref = node.attribs;
  87. for (name in ref) {
  88. if (!hasProp.call(ref, name)) continue;
  89. att = ref[name];
  90. this.attribute(att, options, level);
  91. }
  92. childNodeCount = node.children.length;
  93. firstChildNode = childNodeCount === 0 ? null : node.children[0];
  94. if (childNodeCount === 0 || node.children.every(function(e) {
  95. return (e.type === NodeType.Text || e.type === NodeType.Raw) && e.value === '';
  96. })) {
  97. if (options.allowEmpty) {
  98. this.stream.write('>');
  99. options.state = WriterState.CloseTag;
  100. this.stream.write('</' + node.name + '>');
  101. } else {
  102. options.state = WriterState.CloseTag;
  103. this.stream.write(options.spaceBeforeSlash + '/>');
  104. }
  105. } else if (options.pretty && childNodeCount === 1 && (firstChildNode.type === NodeType.Text || firstChildNode.type === NodeType.Raw) && (firstChildNode.value != null)) {
  106. this.stream.write('>');
  107. options.state = WriterState.InsideTag;
  108. options.suppressPrettyCount++;
  109. prettySuppressed = true;
  110. this.writeChildNode(firstChildNode, options, level + 1);
  111. options.suppressPrettyCount--;
  112. prettySuppressed = false;
  113. options.state = WriterState.CloseTag;
  114. this.stream.write('</' + node.name + '>');
  115. } else {
  116. this.stream.write('>' + this.endline(node, options, level));
  117. options.state = WriterState.InsideTag;
  118. ref1 = node.children;
  119. for (j = 0, len = ref1.length; j < len; j++) {
  120. child = ref1[j];
  121. this.writeChildNode(child, options, level + 1);
  122. }
  123. options.state = WriterState.CloseTag;
  124. this.stream.write(this.indent(node, options, level) + '</' + node.name + '>');
  125. }
  126. this.stream.write(this.endline(node, options, level));
  127. options.state = WriterState.None;
  128. return this.closeNode(node, options, level);
  129. };
  130. XMLStreamWriter.prototype.processingInstruction = function(node, options, level) {
  131. return this.stream.write(XMLStreamWriter.__super__.processingInstruction.call(this, node, options, level));
  132. };
  133. XMLStreamWriter.prototype.raw = function(node, options, level) {
  134. return this.stream.write(XMLStreamWriter.__super__.raw.call(this, node, options, level));
  135. };
  136. XMLStreamWriter.prototype.text = function(node, options, level) {
  137. return this.stream.write(XMLStreamWriter.__super__.text.call(this, node, options, level));
  138. };
  139. XMLStreamWriter.prototype.dtdAttList = function(node, options, level) {
  140. return this.stream.write(XMLStreamWriter.__super__.dtdAttList.call(this, node, options, level));
  141. };
  142. XMLStreamWriter.prototype.dtdElement = function(node, options, level) {
  143. return this.stream.write(XMLStreamWriter.__super__.dtdElement.call(this, node, options, level));
  144. };
  145. XMLStreamWriter.prototype.dtdEntity = function(node, options, level) {
  146. return this.stream.write(XMLStreamWriter.__super__.dtdEntity.call(this, node, options, level));
  147. };
  148. XMLStreamWriter.prototype.dtdNotation = function(node, options, level) {
  149. return this.stream.write(XMLStreamWriter.__super__.dtdNotation.call(this, node, options, level));
  150. };
  151. return XMLStreamWriter;
  152. })(XMLWriterBase);
  153. }).call(this);