UNPKG

woltlab-compiler

Version:

A compiler for WoltLab-Package Archives and WoltLab-Package Components

87 lines 3.22 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const xmldom_1 = require("xmldom"); /** * Provides utilities for the xml-serialization. */ class XML { /** * Creates a new xml-document. * * @param tagName * The name of the tag of the `documentElement`. */ static CreateDocument(tagName) { let result = new xmldom_1.DOMParser().parseFromString(`<${tagName} />`); result.insertBefore(result.createProcessingInstruction("xml", 'version="1.0" encoding="UTF-8"'), result.documentElement); return result; } /** * Formats xml-code. * * @param xml * The xml-code to format. * * @returns * Formatted xml-code. */ static Format(xml) { let document = new xmldom_1.DOMParser().parseFromString(xml); let children = []; for (let i = 0; i < document.childNodes.length; i++) { children.push(document.childNodes.item(i)); } if (children.length > 0) { for (let child of children) { if (child !== document.firstChild) { document.insertBefore(document.createTextNode("\n"), child); } } } this.FormatElement(document.documentElement); return new xmldom_1.XMLSerializer().serializeToString(document); } /** * Formats an element. * * @param element * The element to format. * * @param indent * The indentation of the element itself. */ static FormatElement(element, indent = "") { let innerIndent = `${" ".repeat(4)}${indent}`; if (element.childNodes.length > 0) { let children = []; for (let i = 0; i < element.childNodes.length; i++) { children.push(element.childNodes.item(i)); } for (let child of children) { switch (child.nodeType) { case element.ELEMENT_NODE: case element.PROCESSING_INSTRUCTION_NODE: case element.COMMENT_NODE: case element.DOCUMENT_NODE: case element.DOCUMENT_TYPE_NODE: element.insertBefore(element.ownerDocument.createTextNode(`\n${innerIndent}`), child); if (child.nodeType === element.ELEMENT_NODE) { this.FormatElement(child, `${innerIndent}`); } break; case element.TEXT_NODE: if ((children.length > 1) && child.textContent.trim().length === 0) { element.removeChild(child); } } } if (children.length > 0 && element.lastChild.nodeType !== element.CDATA_SECTION_NODE && element.lastChild.nodeType !== element.TEXT_NODE) { element.appendChild(element.ownerDocument.createTextNode(`\n${indent}`)); } } } } exports.XML = XML; //# sourceMappingURL=XML.js.map