123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302 |
- "use strict";
- var __importDefault = (this && this.__importDefault) || function (mod) {
- return (mod && mod.__esModule) ? mod : { "default": mod };
- };
- Object.defineProperty(exports, "__esModule", { value: true });
- var error_1 = require("../error");
- var options_1 = require("../options");
- var validate_1 = require("../validate");
- var XmlAttribute_1 = __importDefault(require("./XmlAttribute"));
- var XmlCdata_1 = __importDefault(require("./XmlCdata"));
- var XmlCharData_1 = __importDefault(require("./XmlCharData"));
- var XmlCharRef_1 = __importDefault(require("./XmlCharRef"));
- var XmlComment_1 = __importDefault(require("./XmlComment"));
- var XmlEntityRef_1 = __importDefault(require("./XmlEntityRef"));
- var XmlProcInst_1 = __importDefault(require("./XmlProcInst"));
- var XmlElement = (function () {
- function XmlElement(parent, validation, options) {
- this._validation = validation;
- if (!(0, validate_1.isUndefined)(options.replaceInvalidCharsInName)) {
- this._replaceInvalidCharsInName = options.replaceInvalidCharsInName;
- }
- else {
- this._replaceInvalidCharsInName = false;
- }
- if (!(0, validate_1.isUndefined)(options.useSelfClosingTagIfEmpty)) {
- this._useSelfClosingTagIfEmpty = options.useSelfClosingTagIfEmpty;
- }
- else {
- this._useSelfClosingTagIfEmpty = true;
- }
- this._children = [];
- this._attributeNames = [];
- this._parent = parent;
- this.name = options.name;
- }
- Object.defineProperty(XmlElement.prototype, "name", {
-
- get: function () {
- return this._name;
- },
-
- set: function (name) {
- if (this._replaceInvalidCharsInName) {
- name = (0, validate_1.fixName)(name);
- if (name.length === 0) {
- throw new Error((0, error_1.getContext)(this.up()) + ": element name should"
- + " not be empty");
- }
- }
- else if (this._validation && !(0, validate_1.validateName)(name)) {
- if (name.length === 0) {
- throw new Error((0, error_1.getContext)(this.up()) + ": element name should"
- + " not be empty");
- }
- else {
- throw new Error((0, error_1.getContext)(this.up()) + ": element name"
- + (" \"" + name + "\" should not contain characters not")
- + " allowed in XML names");
- }
- }
- this._name = name;
- },
- enumerable: false,
- configurable: true
- });
-
- XmlElement.prototype.attribute = function (options) {
- if (this._validation
- && this._attributeNames.indexOf(options.name) !== -1) {
- throw new Error((0, error_1.getContext)(this.up()) + ": element \"" + this.name + "\""
- + " already contains an attribute with the"
- + (" name \"" + options.name + "\""));
- }
- var attribute = new XmlAttribute_1.default(this, this._validation, options);
- this._children.push(attribute);
- this._attributeNames.push(options.name);
- return attribute;
- };
-
- XmlElement.prototype.cdata = function (options) {
- var cdata = new XmlCdata_1.default(this, this._validation, options);
- this._children.push(cdata);
- return cdata;
- };
-
- XmlElement.prototype.charData = function (options) {
- var charDataNode = new XmlCharData_1.default(this, this._validation, options);
- this._children.push(charDataNode);
- return charDataNode;
- };
-
- XmlElement.prototype.charRef = function (options) {
- var charRef = new XmlCharRef_1.default(this, this._validation, options);
- this._children.push(charRef);
- return charRef;
- };
-
- XmlElement.prototype.comment = function (options) {
- var comment = new XmlComment_1.default(this, this._validation, options);
- this._children.push(comment);
- return comment;
- };
-
- XmlElement.prototype.element = function (options) {
- var element = new XmlElement(this, this._validation, options);
- this._children.push(element);
- return element;
- };
-
- XmlElement.prototype.entityRef = function (options) {
- var entityRef = new XmlEntityRef_1.default(this, this._validation, options);
- this._children.push(entityRef);
- return entityRef;
- };
-
- XmlElement.prototype.procInst = function (options) {
- var procInst = new XmlProcInst_1.default(this, this._validation, options);
- this._children.push(procInst);
- return procInst;
- };
-
- XmlElement.prototype.toString = function (options) {
- if (options === void 0) { options = {}; }
- return this.toStringWithIndent(options, "");
- };
-
- XmlElement.prototype.up = function () {
- return this._parent;
- };
-
- XmlElement.prototype.toStringWithIndent = function (options, indent) {
- var optionsObj = new options_1.StringOptions(options);
- var newIndent = indent + optionsObj.indent;
-
- var str = "<" + this._name;
-
- var nodes = [];
- for (var _i = 0, _a = this._children; _i < _a.length; _i++) {
- var node = _a[_i];
- if (node instanceof XmlAttribute_1.default) {
- str += " " + node.toString(options);
- }
- else {
- nodes.push(node);
- }
- }
-
- if (nodes.length > 0) {
- var childStr = "";
- for (var i = 0; i < nodes.length; i++) {
- var next = nodes[i];
- var nextStr = "";
- if (next instanceof XmlElement) {
- nextStr += next.toStringWithIndent(optionsObj, newIndent);
- }
- else {
- nextStr += next.toString();
- }
- var prev = i > 0 ? nodes[i - 1] : undefined;
-
- if (next instanceof XmlCharData_1.default && next.toString() === "") {
- continue;
- }
-
-
-
- if (optionsObj.pretty) {
- if (!this.allSameLineNodes(nodes)) {
- if (!(i > 0 && this.onSameLine(next, prev))) {
- nextStr = optionsObj.newline + newIndent + nextStr;
- }
- }
- }
- childStr += nextStr;
- }
-
-
- if (optionsObj.pretty) {
- if (!this.allSameLineNodes(nodes)) {
- childStr += optionsObj.newline + indent;
- }
- }
- if (childStr.length === 0 && this._useSelfClosingTagIfEmpty) {
-
- str += "/>";
- }
- else {
-
- str += ">" + childStr + "</" + this._name + ">";
- }
- }
- else if (this._useSelfClosingTagIfEmpty) {
-
- str += "/>";
- }
- else {
-
- str += "></" + this._name + ">";
- }
- return str;
- };
-
- XmlElement.prototype.allSameLineNodes = function (nodes) {
- for (var _i = 0, nodes_1 = nodes; _i < nodes_1.length; _i++) {
- var node = nodes_1[_i];
- if (!((node instanceof XmlCharRef_1.default
- || node instanceof XmlEntityRef_1.default
- || node instanceof XmlCharData_1.default))) {
- return false;
- }
- }
- return true;
- };
-
- XmlElement.prototype.onSameLine = function (prev, next) {
- return (prev instanceof XmlCharRef_1.default
- || prev instanceof XmlEntityRef_1.default
- || prev instanceof XmlCharData_1.default)
- && (!(0, validate_1.isUndefined)(next)
- && (next instanceof XmlCharRef_1.default
- || next instanceof XmlEntityRef_1.default
- || next instanceof XmlCharData_1.default));
- };
- return XmlElement;
- }());
- exports.default = XmlElement;
|