123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209 |
- // Generated by CoffeeScript 2.4.1
- (function() {
- var NodeType, WriterState, XMLStreamWriter, XMLWriterBase,
- hasProp = {}.hasOwnProperty;
-
- NodeType = require('./NodeType');
-
- XMLWriterBase = require('./XMLWriterBase');
-
- WriterState = require('./WriterState');
-
- // Prints XML nodes to a stream
- module.exports = XMLStreamWriter = class XMLStreamWriter extends XMLWriterBase {
- // Initializes a new instance of `XMLStreamWriter`
-
- // `stream` output stream
- // `options.pretty` pretty prints the result
- // `options.indent` indentation string
- // `options.newline` newline sequence
- // `options.offset` a fixed number of indentations to add to every line
- // `options.allowEmpty` do not self close empty element tags
- // 'options.dontPrettyTextNodes' if any text is present in node, don't indent or LF
- // `options.spaceBeforeSlash` add a space before the closing slash of empty elements
- constructor(stream, options) {
- super(options);
- this.stream = stream;
- }
-
- endline(node, options, level) {
- if (node.isLastRootNode && options.state === WriterState.CloseTag) {
- return '';
- } else {
- return super.endline(node, options, level);
- }
- }
-
- document(doc, options) {
- var child, i, j, k, len1, len2, ref, ref1, results;
- ref = doc.children;
- // set a flag so that we don't insert a newline after the last root level node
- for (i = j = 0, len1 = ref.length; j < len1; i = ++j) {
- child = ref[i];
- child.isLastRootNode = i === doc.children.length - 1;
- }
- options = this.filterOptions(options);
- ref1 = doc.children;
- results = [];
- for (k = 0, len2 = ref1.length; k < len2; k++) {
- child = ref1[k];
- results.push(this.writeChildNode(child, options, 0));
- }
- return results;
- }
-
- cdata(node, options, level) {
- return this.stream.write(super.cdata(node, options, level));
- }
-
- comment(node, options, level) {
- return this.stream.write(super.comment(node, options, level));
- }
-
- declaration(node, options, level) {
- return this.stream.write(super.declaration(node, options, level));
- }
-
- docType(node, options, level) {
- var child, j, len1, ref;
- level || (level = 0);
- this.openNode(node, options, level);
- options.state = WriterState.OpenTag;
- this.stream.write(this.indent(node, options, level));
- this.stream.write('<!DOCTYPE ' + node.root().name);
- // external identifier
- if (node.pubID && node.sysID) {
- this.stream.write(' PUBLIC "' + node.pubID + '" "' + node.sysID + '"');
- } else if (node.sysID) {
- this.stream.write(' SYSTEM "' + node.sysID + '"');
- }
- // internal subset
- if (node.children.length > 0) {
- this.stream.write(' [');
- this.stream.write(this.endline(node, options, level));
- options.state = WriterState.InsideTag;
- ref = node.children;
- for (j = 0, len1 = ref.length; j < len1; j++) {
- child = ref[j];
- this.writeChildNode(child, options, level + 1);
- }
- options.state = WriterState.CloseTag;
- this.stream.write(']');
- }
- // close tag
- options.state = WriterState.CloseTag;
- this.stream.write(options.spaceBeforeSlash + '>');
- this.stream.write(this.endline(node, options, level));
- options.state = WriterState.None;
- return this.closeNode(node, options, level);
- }
-
- element(node, options, level) {
- var att, attLen, child, childNodeCount, firstChildNode, j, len, len1, name, prettySuppressed, r, ratt, ref, ref1, ref2, rline;
- level || (level = 0);
- // open tag
- this.openNode(node, options, level);
- options.state = WriterState.OpenTag;
- r = this.indent(node, options, level) + '<' + node.name;
- // attributes
- if (options.pretty && options.width > 0) {
- len = r.length;
- ref = node.attribs;
- for (name in ref) {
- if (!hasProp.call(ref, name)) continue;
- att = ref[name];
- ratt = this.attribute(att, options, level);
- attLen = ratt.length;
- if (len + attLen > options.width) {
- rline = this.indent(node, options, level + 1) + ratt;
- r += this.endline(node, options, level) + rline;
- len = rline.length;
- } else {
- rline = ' ' + ratt;
- r += rline;
- len += rline.length;
- }
- }
- } else {
- ref1 = node.attribs;
- for (name in ref1) {
- if (!hasProp.call(ref1, name)) continue;
- att = ref1[name];
- r += this.attribute(att, options, level);
- }
- }
- this.stream.write(r);
- childNodeCount = node.children.length;
- firstChildNode = childNodeCount === 0 ? null : node.children[0];
- if (childNodeCount === 0 || node.children.every(function(e) {
- return (e.type === NodeType.Text || e.type === NodeType.Raw || e.type === NodeType.CData) && e.value === '';
- })) {
- // empty element
- if (options.allowEmpty) {
- this.stream.write('>');
- options.state = WriterState.CloseTag;
- this.stream.write('</' + node.name + '>');
- } else {
- options.state = WriterState.CloseTag;
- this.stream.write(options.spaceBeforeSlash + '/>');
- }
- } else if (options.pretty && childNodeCount === 1 && (firstChildNode.type === NodeType.Text || firstChildNode.type === NodeType.Raw || firstChildNode.type === NodeType.CData) && (firstChildNode.value != null)) {
- // do not indent text-only nodes
- this.stream.write('>');
- options.state = WriterState.InsideTag;
- options.suppressPrettyCount++;
- prettySuppressed = true;
- this.writeChildNode(firstChildNode, options, level + 1);
- options.suppressPrettyCount--;
- prettySuppressed = false;
- options.state = WriterState.CloseTag;
- this.stream.write('</' + node.name + '>');
- } else {
- this.stream.write('>' + this.endline(node, options, level));
- options.state = WriterState.InsideTag;
- ref2 = node.children;
- // inner tags
- for (j = 0, len1 = ref2.length; j < len1; j++) {
- child = ref2[j];
- this.writeChildNode(child, options, level + 1);
- }
- // close tag
- options.state = WriterState.CloseTag;
- this.stream.write(this.indent(node, options, level) + '</' + node.name + '>');
- }
- this.stream.write(this.endline(node, options, level));
- options.state = WriterState.None;
- return this.closeNode(node, options, level);
- }
-
- processingInstruction(node, options, level) {
- return this.stream.write(super.processingInstruction(node, options, level));
- }
-
- raw(node, options, level) {
- return this.stream.write(super.raw(node, options, level));
- }
-
- text(node, options, level) {
- return this.stream.write(super.text(node, options, level));
- }
-
- dtdAttList(node, options, level) {
- return this.stream.write(super.dtdAttList(node, options, level));
- }
-
- dtdElement(node, options, level) {
- return this.stream.write(super.dtdElement(node, options, level));
- }
-
- dtdEntity(node, options, level) {
- return this.stream.write(super.dtdEntity(node, options, level));
- }
-
- dtdNotation(node, options, level) {
- return this.stream.write(super.dtdNotation(node, options, level));
- }
-
- };
-
- }).call(this);
|