52 lines
1.6 KiB
JavaScript
52 lines
1.6 KiB
JavaScript
"use strict";
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.XmlNode = void 0;
|
|
const escape_attribute_1 = require("./escape-attribute");
|
|
const XmlText_1 = require("./XmlText");
|
|
class XmlNode {
|
|
static of(name, childText, withName) {
|
|
const node = new XmlNode(name);
|
|
if (childText !== undefined) {
|
|
node.addChildNode(new XmlText_1.XmlText(childText));
|
|
}
|
|
if (withName !== undefined) {
|
|
node.withName(withName);
|
|
}
|
|
return node;
|
|
}
|
|
constructor(name, children = []) {
|
|
this.name = name;
|
|
this.children = children;
|
|
this.attributes = {};
|
|
}
|
|
withName(name) {
|
|
this.name = name;
|
|
return this;
|
|
}
|
|
addAttribute(name, value) {
|
|
this.attributes[name] = value;
|
|
return this;
|
|
}
|
|
addChildNode(child) {
|
|
this.children.push(child);
|
|
return this;
|
|
}
|
|
removeAttribute(name) {
|
|
delete this.attributes[name];
|
|
return this;
|
|
}
|
|
toString() {
|
|
const hasChildren = Boolean(this.children.length);
|
|
let xmlText = `<${this.name}`;
|
|
const attributes = this.attributes;
|
|
for (const attributeName of Object.keys(attributes)) {
|
|
const attribute = attributes[attributeName];
|
|
if (typeof attribute !== "undefined" && attribute !== null) {
|
|
xmlText += ` ${attributeName}="${(0, escape_attribute_1.escapeAttribute)("" + attribute)}"`;
|
|
}
|
|
}
|
|
return (xmlText += !hasChildren ? "/>" : `>${this.children.map((c) => c.toString()).join("")}</${this.name}>`);
|
|
}
|
|
}
|
|
exports.XmlNode = XmlNode;
|