/var/www/greso.tech/server/nsm/node_modules/xmlcreate/lib/nodes/XmlDocument.js (6427B)
I hope you enjoy visiting my website.
*
*
*
* ```
*
* Each component of the document, such as the declaration, document type
* definition, and root element, are children of this node.
*
* Documents must have exactly one element, which is the document's root
* element.
*
* Documents can have exactly one declaration and one document type definition
* in that order, so long as they precede the element.
*
* Documents can have an unlimited number of comments or processing
* instructions, so long as they follow the declaration, if one exists.
*/
var XmlDocument = /** @class */ (function () {
function XmlDocument(options) {
this._children = [];
this._validation = !(0, validate_1.isUndefined)(options.validation)
? options.validation
: true;
}
/**
* Adds a comment to this document and returns the new comment.
*/
XmlDocument.prototype.comment = function (options) {
var comment = new XmlComment_1.default(this, this._validation, options);
this._children.push(comment);
return comment;
};
/**
* Adds a declaration to this document and returns the new declaration.
*/
XmlDocument.prototype.decl = function (options) {
if (options === void 0) { options = {}; }
if (this._validation && this._children.length !== 0) {
throw new Error("in XML document: declaration must be the first"
+ " child");
}
var declaration = new XmlDecl_1.default(this, this._validation, options);
this._children.push(declaration);
return declaration;
};
/**
* Adds a document type definition to this document and returns the new
* document type definition.
*/
XmlDocument.prototype.dtd = function (options) {
var filteredChildren = this._children.filter(function (value) {
return value instanceof XmlElement_1.default;
});
if (this._validation && filteredChildren.length !== 0) {
throw new Error("in XML document: DTD must precede the root"
+ " element");
}
var dtd = new XmlDtd_1.default(this, this._validation, options);
this._children.push(dtd);
return dtd;
};
/**
* Adds the root element to this document and returns the element.
*/
XmlDocument.prototype.element = function (options) {
var filteredChildren = this._children.filter(function (value) {
return value instanceof XmlElement_1.default;
});
if (this._validation && filteredChildren.length !== 0) {
throw new Error("in XML document: only one root element is"
+ " permitted");
}
var element = new XmlElement_1.default(this, this._validation, options);
this._children.push(element);
return element;
};
/**
* Adds a processing instruction to this document and returns the new
* processing instruction.
*/
XmlDocument.prototype.procInst = function (options) {
var procInst = new XmlProcInst_1.default(this, this._validation, options);
this._children.push(procInst);
return procInst;
};
/**
* Returns an XML string representation of this document using the
* specified options.
*/
XmlDocument.prototype.toString = function (options) {
if (options === void 0) { options = {}; }
var filteredChildren = this._children.filter(function (value) {
return value instanceof XmlElement_1.default;
});
if (this._validation && filteredChildren.length !== 1) {
throw new Error("in XML document: no more than one root element"
+ " is permitted");
}
var optionsObj = new options_1.StringOptions(options);
var str = "";
for (var _i = 0, _a = this._children; _i < _a.length; _i++) {
var node = _a[_i];
if (node instanceof XmlDecl_1.default
|| node instanceof XmlDtd_1.default
|| node instanceof XmlElement_1.default) {
str += node.toString(options);
}
else {
str += node.toString();
}
if (optionsObj.pretty) {
str += optionsObj.newline;
}
}
var len = str.length - optionsObj.newline.length;
if (str.substr(len) === optionsObj.newline) {
str = str.substr(0, len);
}
return str;
};
return XmlDocument;
}());
exports.default = XmlDocument;