123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485 |
- // Generated by CoffeeScript 2.4.1
- (function() {
- var NodeType, WriterState, XMLCData, XMLComment, XMLDTDAttList, XMLDTDElement, XMLDTDEntity, XMLDTDNotation, XMLDeclaration, XMLDocType, XMLDummy, XMLElement, XMLProcessingInstruction, XMLRaw, XMLText, XMLWriterBase, assign,
- hasProp = {}.hasOwnProperty;
- ({assign} = require('./Utility'));
- NodeType = require('./NodeType');
- XMLDeclaration = require('./XMLDeclaration');
- XMLDocType = require('./XMLDocType');
- XMLCData = require('./XMLCData');
- XMLComment = require('./XMLComment');
- XMLElement = require('./XMLElement');
- XMLRaw = require('./XMLRaw');
- XMLText = require('./XMLText');
- XMLProcessingInstruction = require('./XMLProcessingInstruction');
- XMLDummy = require('./XMLDummy');
- XMLDTDAttList = require('./XMLDTDAttList');
- XMLDTDElement = require('./XMLDTDElement');
- XMLDTDEntity = require('./XMLDTDEntity');
- XMLDTDNotation = require('./XMLDTDNotation');
- WriterState = require('./WriterState');
- // Base class for XML writers
- module.exports = XMLWriterBase = class XMLWriterBase {
- // Initializes a new instance of `XMLWriterBase`
- // `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.width` maximum column width
- // `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(options) {
- var key, ref, value;
- options || (options = {});
- this.options = options;
- ref = options.writer || {};
- for (key in ref) {
- if (!hasProp.call(ref, key)) continue;
- value = ref[key];
- this["_" + key] = this[key];
- this[key] = value;
- }
- }
- // Filters writer options and provides defaults
- // `options` writer options
- filterOptions(options) {
- var filteredOptions, ref, ref1, ref2, ref3, ref4, ref5, ref6, ref7;
- options || (options = {});
- options = assign({}, this.options, options);
- filteredOptions = {
- writer: this
- };
- filteredOptions.pretty = options.pretty || false;
- filteredOptions.allowEmpty = options.allowEmpty || false;
- filteredOptions.indent = (ref = options.indent) != null ? ref : ' ';
- filteredOptions.newline = (ref1 = options.newline) != null ? ref1 : '\n';
- filteredOptions.offset = (ref2 = options.offset) != null ? ref2 : 0;
- filteredOptions.width = (ref3 = options.width) != null ? ref3 : 0;
- filteredOptions.dontPrettyTextNodes = (ref4 = (ref5 = options.dontPrettyTextNodes) != null ? ref5 : options.dontprettytextnodes) != null ? ref4 : 0;
- filteredOptions.spaceBeforeSlash = (ref6 = (ref7 = options.spaceBeforeSlash) != null ? ref7 : options.spacebeforeslash) != null ? ref6 : '';
- if (filteredOptions.spaceBeforeSlash === true) {
- filteredOptions.spaceBeforeSlash = ' ';
- }
- filteredOptions.suppressPrettyCount = 0;
- filteredOptions.user = {};
- filteredOptions.state = WriterState.None;
- return filteredOptions;
- }
- // Returns the indentation string for the current level
- // `node` current node
- // `options` writer options
- // `level` current indentation level
- indent(node, options, level) {
- var indentLevel;
- if (!options.pretty || options.suppressPrettyCount) {
- return '';
- } else if (options.pretty) {
- indentLevel = (level || 0) + options.offset + 1;
- if (indentLevel > 0) {
- return new Array(indentLevel).join(options.indent);
- }
- }
- return '';
- }
- // Returns the newline string
- // `node` current node
- // `options` writer options
- // `level` current indentation level
- endline(node, options, level) {
- if (!options.pretty || options.suppressPrettyCount) {
- return '';
- } else {
- return options.newline;
- }
- }
- attribute(att, options, level) {
- var r;
- this.openAttribute(att, options, level);
- if (options.pretty && options.width > 0) {
- r = att.name + '="' + att.value + '"';
- } else {
- r = ' ' + att.name + '="' + att.value + '"';
- }
- this.closeAttribute(att, options, level);
- return r;
- }
- cdata(node, options, level) {
- var r;
- this.openNode(node, options, level);
- options.state = WriterState.OpenTag;
- r = this.indent(node, options, level) + '<![CDATA[';
- options.state = WriterState.InsideTag;
- r += node.value;
- options.state = WriterState.CloseTag;
- r += ']]>' + this.endline(node, options, level);
- options.state = WriterState.None;
- this.closeNode(node, options, level);
- return r;
- }
- comment(node, options, level) {
- var r;
- this.openNode(node, options, level);
- options.state = WriterState.OpenTag;
- r = this.indent(node, options, level) + '<!-- ';
- options.state = WriterState.InsideTag;
- r += node.value;
- options.state = WriterState.CloseTag;
- r += ' -->' + this.endline(node, options, level);
- options.state = WriterState.None;
- this.closeNode(node, options, level);
- return r;
- }
- declaration(node, options, level) {
- var r;
- this.openNode(node, options, level);
- options.state = WriterState.OpenTag;
- r = this.indent(node, options, level) + '<?xml';
- options.state = WriterState.InsideTag;
- r += ' version="' + node.version + '"';
- if (node.encoding != null) {
- r += ' encoding="' + node.encoding + '"';
- }
- if (node.standalone != null) {
- r += ' standalone="' + node.standalone + '"';
- }
- options.state = WriterState.CloseTag;
- r += options.spaceBeforeSlash + '?>';
- r += this.endline(node, options, level);
- options.state = WriterState.None;
- this.closeNode(node, options, level);
- return r;
- }
- docType(node, options, level) {
- var child, i, len1, r, ref;
- level || (level = 0);
- this.openNode(node, options, level);
- options.state = WriterState.OpenTag;
- r = this.indent(node, options, level);
- r += '<!DOCTYPE ' + node.root().name;
- // external identifier
- if (node.pubID && node.sysID) {
- r += ' PUBLIC "' + node.pubID + '" "' + node.sysID + '"';
- } else if (node.sysID) {
- r += ' SYSTEM "' + node.sysID + '"';
- }
- // internal subset
- if (node.children.length > 0) {
- r += ' [';
- r += this.endline(node, options, level);
- options.state = WriterState.InsideTag;
- ref = node.children;
- for (i = 0, len1 = ref.length; i < len1; i++) {
- child = ref[i];
- r += this.writeChildNode(child, options, level + 1);
- }
- options.state = WriterState.CloseTag;
- r += ']';
- }
- // close tag
- options.state = WriterState.CloseTag;
- r += options.spaceBeforeSlash + '>';
- r += this.endline(node, options, level);
- options.state = WriterState.None;
- this.closeNode(node, options, level);
- return r;
- }
- element(node, options, level) {
- var att, attLen, child, childNodeCount, firstChildNode, i, j, len, len1, len2, name, prettySuppressed, r, ratt, ref, ref1, ref2, ref3, rline;
- level || (level = 0);
- prettySuppressed = false;
- // 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);
- }
- }
- 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) {
- r += '>';
- options.state = WriterState.CloseTag;
- r += '</' + node.name + '>' + this.endline(node, options, level);
- } else {
- options.state = WriterState.CloseTag;
- r += options.spaceBeforeSlash + '/>' + this.endline(node, options, level);
- }
- } 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
- r += '>';
- options.state = WriterState.InsideTag;
- options.suppressPrettyCount++;
- prettySuppressed = true;
- r += this.writeChildNode(firstChildNode, options, level + 1);
- options.suppressPrettyCount--;
- prettySuppressed = false;
- options.state = WriterState.CloseTag;
- r += '</' + node.name + '>' + this.endline(node, options, level);
- } else {
- // if ANY are a text node, then suppress pretty now
- if (options.dontPrettyTextNodes) {
- ref2 = node.children;
- for (i = 0, len1 = ref2.length; i < len1; i++) {
- child = ref2[i];
- if ((child.type === NodeType.Text || child.type === NodeType.Raw || child.type === NodeType.CData) && (child.value != null)) {
- options.suppressPrettyCount++;
- prettySuppressed = true;
- break;
- }
- }
- }
- // close the opening tag, after dealing with newline
- r += '>' + this.endline(node, options, level);
- options.state = WriterState.InsideTag;
- ref3 = node.children;
- // inner tags
- for (j = 0, len2 = ref3.length; j < len2; j++) {
- child = ref3[j];
- r += this.writeChildNode(child, options, level + 1);
- }
- // close tag
- options.state = WriterState.CloseTag;
- r += this.indent(node, options, level) + '</' + node.name + '>';
- if (prettySuppressed) {
- options.suppressPrettyCount--;
- }
- r += this.endline(node, options, level);
- options.state = WriterState.None;
- }
- this.closeNode(node, options, level);
- return r;
- }
- writeChildNode(node, options, level) {
- switch (node.type) {
- case NodeType.CData:
- return this.cdata(node, options, level);
- case NodeType.Comment:
- return this.comment(node, options, level);
- case NodeType.Element:
- return this.element(node, options, level);
- case NodeType.Raw:
- return this.raw(node, options, level);
- case NodeType.Text:
- return this.text(node, options, level);
- case NodeType.ProcessingInstruction:
- return this.processingInstruction(node, options, level);
- case NodeType.Dummy:
- return '';
- case NodeType.Declaration:
- return this.declaration(node, options, level);
- case NodeType.DocType:
- return this.docType(node, options, level);
- case NodeType.AttributeDeclaration:
- return this.dtdAttList(node, options, level);
- case NodeType.ElementDeclaration:
- return this.dtdElement(node, options, level);
- case NodeType.EntityDeclaration:
- return this.dtdEntity(node, options, level);
- case NodeType.NotationDeclaration:
- return this.dtdNotation(node, options, level);
- default:
- throw new Error("Unknown XML node type: " + node.constructor.name);
- }
- }
- processingInstruction(node, options, level) {
- var r;
- this.openNode(node, options, level);
- options.state = WriterState.OpenTag;
- r = this.indent(node, options, level) + '<?';
- options.state = WriterState.InsideTag;
- r += node.target;
- if (node.value) {
- r += ' ' + node.value;
- }
- options.state = WriterState.CloseTag;
- r += options.spaceBeforeSlash + '?>';
- r += this.endline(node, options, level);
- options.state = WriterState.None;
- this.closeNode(node, options, level);
- return r;
- }
- raw(node, options, level) {
- var r;
- this.openNode(node, options, level);
- options.state = WriterState.OpenTag;
- r = this.indent(node, options, level);
- options.state = WriterState.InsideTag;
- r += node.value;
- options.state = WriterState.CloseTag;
- r += this.endline(node, options, level);
- options.state = WriterState.None;
- this.closeNode(node, options, level);
- return r;
- }
- text(node, options, level) {
- var r;
- this.openNode(node, options, level);
- options.state = WriterState.OpenTag;
- r = this.indent(node, options, level);
- options.state = WriterState.InsideTag;
- r += node.value;
- options.state = WriterState.CloseTag;
- r += this.endline(node, options, level);
- options.state = WriterState.None;
- this.closeNode(node, options, level);
- return r;
- }
- dtdAttList(node, options, level) {
- var r;
- this.openNode(node, options, level);
- options.state = WriterState.OpenTag;
- r = this.indent(node, options, level) + '<!ATTLIST';
- options.state = WriterState.InsideTag;
- r += ' ' + node.elementName + ' ' + node.attributeName + ' ' + node.attributeType;
- if (node.defaultValueType !== '#DEFAULT') {
- r += ' ' + node.defaultValueType;
- }
- if (node.defaultValue) {
- r += ' "' + node.defaultValue + '"';
- }
- options.state = WriterState.CloseTag;
- r += options.spaceBeforeSlash + '>' + this.endline(node, options, level);
- options.state = WriterState.None;
- this.closeNode(node, options, level);
- return r;
- }
- dtdElement(node, options, level) {
- var r;
- this.openNode(node, options, level);
- options.state = WriterState.OpenTag;
- r = this.indent(node, options, level) + '<!ELEMENT';
- options.state = WriterState.InsideTag;
- r += ' ' + node.name + ' ' + node.value;
- options.state = WriterState.CloseTag;
- r += options.spaceBeforeSlash + '>' + this.endline(node, options, level);
- options.state = WriterState.None;
- this.closeNode(node, options, level);
- return r;
- }
- dtdEntity(node, options, level) {
- var r;
- this.openNode(node, options, level);
- options.state = WriterState.OpenTag;
- r = this.indent(node, options, level) + '<!ENTITY';
- options.state = WriterState.InsideTag;
- if (node.pe) {
- r += ' %';
- }
- r += ' ' + node.name;
- if (node.value) {
- r += ' "' + node.value + '"';
- } else {
- if (node.pubID && node.sysID) {
- r += ' PUBLIC "' + node.pubID + '" "' + node.sysID + '"';
- } else if (node.sysID) {
- r += ' SYSTEM "' + node.sysID + '"';
- }
- if (node.nData) {
- r += ' NDATA ' + node.nData;
- }
- }
- options.state = WriterState.CloseTag;
- r += options.spaceBeforeSlash + '>' + this.endline(node, options, level);
- options.state = WriterState.None;
- this.closeNode(node, options, level);
- return r;
- }
- dtdNotation(node, options, level) {
- var r;
- this.openNode(node, options, level);
- options.state = WriterState.OpenTag;
- r = this.indent(node, options, level) + '<!NOTATION';
- options.state = WriterState.InsideTag;
- r += ' ' + node.name;
- if (node.pubID && node.sysID) {
- r += ' PUBLIC "' + node.pubID + '" "' + node.sysID + '"';
- } else if (node.pubID) {
- r += ' PUBLIC "' + node.pubID + '"';
- } else if (node.sysID) {
- r += ' SYSTEM "' + node.sysID + '"';
- }
- options.state = WriterState.CloseTag;
- r += options.spaceBeforeSlash + '>' + this.endline(node, options, level);
- options.state = WriterState.None;
- this.closeNode(node, options, level);
- return r;
- }
- openNode(node, options, level) {}
- closeNode(node, options, level) {}
- openAttribute(att, options, level) {}
- closeAttribute(att, options, level) {}
- };
- }).call(this);
|