@manuth/woltlab-compiler
Version:
A compiler for generating WoltLab-Package `.tar` Archives and other WoltLab-Package Components
233 lines • 5.82 kB
JavaScript
/**
* Provides the functionality to edit xml-files.
*/
export class XMLEditor {
/**
* Initializes a new instance of the {@link XMLEditor `XMLEditor`} class.
*
* @param element
* The element to edit.
*/
constructor(element) {
this.element = element;
}
/**
* Gets the name of the tag of the element.
*/
get TagName() {
return this.Element.tagName;
}
/**
* Gets the element to edit.
*/
get Element() {
return this.element;
}
/**
* Gets or sets the text of the element.
*/
get TextContent() {
return this.Element.textContent;
}
/**
* @inheritdoc
*/
set TextContent(value) {
this.Element.textContent = value;
}
/**
* Gets the parent of the element.
*/
get ParentNode() {
return this.Element.parentNode;
}
/**
* Gets the document of the element.
*/
get Document() {
return this.Element.ownerDocument;
}
/**
* Gets the child-nodes of the element.
*/
get ChildNodes() {
return XMLEditor.ToArray(this.Element.childNodes);
}
/**
* Creates a new element.
*
* @param tag
* The tag of the element to create.
*
* @returns
* The editor for the newly created element.
*/
CreateElement(tag) {
return new XMLEditor(this.Document.createElement(tag));
}
/**
* Creates a new element with the specified {@link textContent `textContent`} wrapped by a CDATA-section.
*
* @param tag
* The tag of the element to create.
*
* @param textContent
* The text to insert into the element.
*
* @returns
* The editor for the newly created element.
*/
CreateCDATAElement(tag, textContent) {
let result = this.CreateElement(tag);
result.Add(this.Document.createCDATASection(textContent));
return result;
}
/**
* Creates a new element with the specified {@link textContent `textContent`}.
*
* @param tag
* The tag of the element to create.
*
* @param textContent
* The text to insert into the element.
*
* @returns
* The editor for the newly created element.
*/
CreateTextElement(tag, textContent) {
let result = this.CreateElement(tag);
result.TextContent = textContent;
return result;
}
/**
* Adds a node.
*
* @template T
* The type of the node to add.
*
* @param node
* The node to add.
*/
Add(node) {
let element;
if (node instanceof XMLEditor) {
element = node.Element;
}
else {
element = node;
}
this.Element.appendChild(element);
}
/**
* Inserts an element at a specific position.
*
* @param index
* The index to insert the element.
*
* @param element
* The element to insert.
*/
Insert(index, element) {
let node;
if (element instanceof XMLEditor) {
node = element.Element;
}
else {
node = element;
}
if (index === this.ChildNodes.length) {
this.Add(node);
}
else if (index < this.ChildNodes.length) {
this.Element.insertBefore(node, this.ChildNodes[index]);
}
else {
throw new RangeError();
}
}
/**
* Gets all children of the element with a specified tag.
*
* @param tag
* The tag to look for.
*
* @returns
* The xml-editor of the children which were found.
*/
GetChildrenByTag(tag) {
return XMLEditor.ToArray(this.Element.childNodes).filter((childNode) => childNode.nodeType === this.Element.ELEMENT_NODE).map((childNode) => new XMLEditor(childNode)).filter((node) => node.TagName === tag);
}
/**
* Gets all elements with a specified tag.
*
* @param tag
* The tag to look for.
*
* @returns
* The xml-editor of the children which were found.
*/
GetElementsByTag(tag) {
return XMLEditor.ToArray(this.Element.getElementsByTagName(tag)).map((element) => new XMLEditor(element));
}
/**
* Gets the value of an attribute.
*
* @param name
* The name of the attribute to get.
*
* @returns
* The value of the attribute.
*/
GetAttribute(name) {
if (this.HasAttribute(name)) {
return this.Element.getAttribute(name);
}
else {
throw new RangeError(`The attribute "${name}" does not exist.`);
}
}
/**
* Sets the value of an attribute.
*
* @param name
* The name of the attribute to set.
*
* @param value
* The value to set.
*/
SetAttribute(name, value) {
this.Element.setAttribute(name, value);
}
/**
* Gets a value indicating whether an attribute with the specified name exists.
*
* @param name
* The name to look for.
*
* @returns
* A value indicating whether an attribute with the specified {@link name `name`} exists.
*/
HasAttribute(name) {
return this.Element.hasAttribute(name);
}
/**
* Converts a node-list to an array.
*
* @param nodeList
* The node-list to convert.
*
* @template T
* The type of the nodes.
*
* @returns
* An array representing the specified {@link nodeList `nodeList`}.
*/
static ToArray(nodeList) {
let result = [];
for (let i = 0; i < nodeList.length; i++) {
result.push(nodeList.item(i));
}
return result;
}
}
//# sourceMappingURL=XMLEditor.js.map