xmlbuilder2
Version:
An XML builder for node.js
236 lines • 8.87 kB
JavaScript
"use strict";
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
var util_1 = require("@oozcitak/util");
var interfaces_1 = require("@oozcitak/dom/lib/dom/interfaces");
var BaseWriter_1 = require("./BaseWriter");
var util_2 = require("@oozcitak/dom/lib/util");
/**
* Serializes XML nodes into strings.
*/
var XMLWriter = /** @class */ (function (_super) {
__extends(XMLWriter, _super);
/**
* Initializes a new instance of `XMLWriter`.
*
* @param builderOptions - XML builder options
* @param writerOptions - serialization options
*/
function XMLWriter(builderOptions, writerOptions) {
var _this = _super.call(this, builderOptions) || this;
_this._indentation = {};
_this._lengthToLastNewline = 0;
// provide default options
_this._writerOptions = util_1.applyDefaults(writerOptions, {
wellFormed: false,
headless: false,
prettyPrint: false,
indent: " ",
newline: "\n",
offset: 0,
width: 0,
allowEmptyTags: false,
indentTextOnlyNodes: false,
spaceBeforeSlash: false
});
return _this;
}
/**
* Produces an XML serialization of the given node.
*
* @param node - node to serialize
*/
XMLWriter.prototype.serialize = function (node) {
this._refs = { suppressPretty: false, emptyNode: false, markup: "" };
// Serialize XML declaration
if (node.nodeType === interfaces_1.NodeType.Document && !this._writerOptions.headless) {
this.declaration(this._builderOptions.version, this._builderOptions.encoding, this._builderOptions.standalone);
}
// recursively serialize node
this.serializeNode(node, this._writerOptions.wellFormed);
// remove trailing newline
if (this._writerOptions.prettyPrint &&
this._refs.markup.slice(-this._writerOptions.newline.length) === this._writerOptions.newline) {
this._refs.markup = this._refs.markup.slice(0, -this._writerOptions.newline.length);
}
return this._refs.markup;
};
/** @inheritdoc */
XMLWriter.prototype.declaration = function (version, encoding, standalone) {
this._beginLine();
this._refs.markup += "xml version=\"" + version + "\"";
if (encoding !== undefined) {
this._refs.markup += " encoding=\"" + encoding + "\"";
}
if (standalone !== undefined) {
this._refs.markup += " standalone=\"" + (standalone ? "yes" : "no") + "\"";
}
this._refs.markup += "?>";
this._endLine();
};
/** @inheritdoc */
XMLWriter.prototype.docType = function (name, publicId, systemId) {
this._beginLine();
if (publicId && systemId) {
this._refs.markup += "<?" + (data === "" ? target : target + " " + data) + "?>