woltlab-compiler
Version:
A compiler for WoltLab-Package Archives and WoltLab-Package Components
318 lines • 8.44 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const util_1 = require("util");
/**
* Provides the functionality to edit xml-files.
*/
class XMLEditor {
/**
* Initializes a new instance of the `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;
}
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.
*
* @param processor
* A method for manipulating the new element.
*/
CreateElement(tag, processor) {
let editor = new XMLEditor(this.Document.createElement(tag));
if (!util_1.isNullOrUndefined(processor)) {
processor(editor);
}
return editor;
}
/**
* Creates a new element with the specified `textContent` wrapped by a CDATA-section.
*
* @param tag
* The tag of the element to create.
*
* @param textContent
* The text to insert into the element.
*
* @param processor
* A method for manipulating the new element.
*/
CreateCDATAElement(tag, textContent, processor) {
return this.CreateElement(tag, (element) => {
element.Add(this.Document.createCDATASection(textContent));
if (!util_1.isNullOrUndefined(processor)) {
processor(element);
}
});
}
/**
* Creates a new element with the specified `textContent`.
*
* @param tag
* The tag of the element to create.
*
* @param textContent
* The text to insert into the element.
*/
CreateTextElement(tag, textContent, processor) {
return this.CreateElement(tag, (element) => {
element.TextContent = textContent;
if (!util_1.isNullOrUndefined(processor)) {
processor(element);
}
});
}
/**
* Adds a new node.
*
* @param node
* The node to add.
*/
Add(node, processor) {
let element;
if (node instanceof XMLEditor) {
element = node.Element;
}
else {
element = node;
}
this.Element.appendChild(element);
if (!util_1.isNullOrUndefined(processor)) {
processor(node);
}
}
/**
* Adds a new element.
*
* @param tag
* The tag of the element to create.
*
* @param processor
* A method for manipulating the new element.
*/
AddElement(tag, processor) {
this.Add(this.CreateElement(tag, processor));
}
/**
* Adds a new element with the specified `textContent` wrapped by a CDATA-section.
*
* @param tag
* The tag of the element to create.
*
* @param textContent
* The text to insert into the element.
*
* @param processor
* A method for manipulating the new element.
*/
AddCDATAElement(tag, textContent, processor) {
this.Add(this.CreateCDATAElement(tag, textContent, processor));
}
/**
* Adds a new element with the specified `textContent`.
*
* @param tag
* The tag of the element to create.
*
* @param textContent
* The text to insert into the element.
*/
AddTextElement(tag, textContent, processor) {
this.Add(this.CreateTextElement(tag, textContent, processor));
}
/**
* 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.
*/
GetChildrenByTag(tag) {
return this.GetElementsByTag(tag).filter((node) => node.ParentNode === this.Element);
}
/**
* Gets all elements with a specified tag.
*
* @param tag
* The tag to look for.
*/
GetElementsByTag(tag) {
return XMLEditor.ToArray(this.Element.getElementsByTagName(tag)).map((element) => new XMLEditor(element));
}
/**
* Gets the text of a child-node.
*
* @param tag
* The tag of the node to get the text.
*/
GetText(tag) {
if (this.HasTag(tag, true)) {
return this.GetChildrenByTag(tag)[0].TextContent;
}
else {
throw new RangeError(`The tag "${tag}" either is not unique or does not exist.`);
}
}
/**
* Gets the value of an attribute.
*
* @param name
* The name of the attribute to get.
*/
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.
*/
HasAttribute(name, value) {
return this.Element.hasAttribute(name) && (util_1.isNullOrUndefined(value) || (this.Element.getAttribute(name) === value));
}
/**
* Asserts a tag to contain a text.
*
* @param text
* The text to assert.
*
* @param tag
* The tag to check.
*/
HasText(tag, text) {
let original;
if (!util_1.isNullOrUndefined(tag)) {
try {
original = this.GetText(tag);
}
catch (_a) {
original = null;
}
}
else {
original = this.TextContent;
}
return original === text;
}
/**
* Asserts the element to have a tag.
* @param tag
* The tag to assert.
*
* @param unique
* A value indicating whether the tag is unique.
*/
HasTag(tag, unique) {
let children = this.GetChildrenByTag(tag);
unique = unique || false;
if (unique) {
return children.length === 1;
}
else {
return children.length > 0;
}
}
/**
* Converts a node-list to an array.
*
* @param nodeList
* The node-list to convert.
*/
static ToArray(nodeList) {
let result = [];
for (let i = 0; i < nodeList.length; i++) {
result.push(nodeList.item(i));
}
return result;
}
}
exports.XMLEditor = XMLEditor;
//# sourceMappingURL=XMLEditor.js.map