UNPKG

@rxap/xml-parser

Version:

Provides a set of decorators and services for parsing and serializing XML documents into TypeScript classes. It simplifies the process of mapping XML elements and attributes to class properties, handling data validation, and serializing objects back into

200 lines 7.56 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.RxapElement = void 0; exports.normalizeNodeName = normalizeNodeName; const parse_value_1 = require("./parse-value"); /** * Normalizes the provided node name based on the specified options. * * This function adjusts the `nodeName` according to the `options` provided. It can convert the node name to lowercase if `caseSensitive` is set to false. Additionally, it can remove the namespace prefix from the node name if `withNamespace` is set to false. * * @param {string} nodeName - The original node name to be normalized. * @param {RxapElementOptions} options - Configuration options that determine how the node name should be normalized. The options include: * - `caseSensitive`: A boolean that indicates whether the node name should be treated as case-sensitive. If false, the node name will be converted to lowercase. * - `withNamespace`: A boolean that indicates whether the namespace prefix should be included in the node name. If false, any namespace prefix is removed. * @returns {string} The normalized node name based on the provided options. */ function normalizeNodeName(nodeName, options) { let name = nodeName; if (!options.caseSensitive) { name = name.toLowerCase(); } if (!options.withNamespace) { name = name.replace(/[^:]+:/, ''); } return name; } class RxapElement { constructor(element, DOMParser, options = {}) { this.element = element; this.DOMParser = DOMParser; this.options = options; } get attributes() { return Array.from(this.element.attributes).reduce((acc, attr) => (Object.assign(Object.assign({}, acc), { [attr.name]: attr.value })), {}); } get attributeNames() { return Object.keys(this.attributes); } get name() { return this.normalizeNodeName(this.nodeName); } get nodeName() { return this.element.nodeName; } getString(qualifiedName, defaultValue) { if (this.element.hasAttribute(qualifiedName)) { return this.element.getAttribute(qualifiedName); } return defaultValue; } get(qualifiedName, defaultValue, raw = false) { if (this.element.hasAttribute(qualifiedName)) { const value = this.element.getAttribute(qualifiedName); return raw ? value : (0, parse_value_1.parseValue)(value); } return defaultValue; } set(qualifiedName, value) { this.element.setAttribute(qualifiedName, value); } hasName(name) { return this.name === this.normalizeNodeName(name); } getDate(qualifiedName) { return undefined; } getNumber(qualifiedName, defaultValue) { if (this.element.hasAttribute(qualifiedName)) { return Number(this.element.getAttribute(qualifiedName)); } return defaultValue; } getBoolean(qualifiedName, defaultValue) { if (this.element.hasAttribute(qualifiedName)) { return this.element.getAttribute(qualifiedName) !== 'false'; } return defaultValue; } has(qualifiedName) { return this.element.hasAttribute(qualifiedName); } hasChildren() { return this.getAllChildNodes().length !== 0; } hasChild(nodeName) { return !!this.getChild(nodeName); } getAllChildNodes() { return Array.from(this.element.childNodes) .filter(n => !!n.nodeName && n.nodeType === 1) .map((child) => new RxapElement(child, this.DOMParser, this.options)); } getCountChildren() { return Array.from(this.element.childNodes).filter(n => !!n.nodeName && n.nodeType === 1).length; } getChildren(...nodeNames) { return this.getAllChildNodes().filter(e => nodeNames.map(nodeName => this.normalizeNodeName(nodeName)).includes(e.name)); } getChild(nodeName) { return this.getAllChildNodes().find(e => e.hasName(nodeName)); } getTextContent(defaultValue, raw = false) { const textContent = this.element.textContent === null || this.element.textContent === '' ? defaultValue : this.element.textContent; return textContent !== undefined ? raw ? textContent.trim() : (0, parse_value_1.parseValue)(textContent.trim()) : undefined; } getRawContent() { if (this.element.innerHTML !== undefined) { return this.element.innerHTML; } return Array.from(this.element.childNodes).map(child => child.toString()).join(''); } removeAllChildren() { for (const child of this.getAllChildNodes()) { this.element.removeChild(child.element); } } setRawContent(value) { this.removeAllChildren(); if (this.element.innerHTML !== undefined) { this.element.innerHTML = value; } else { this.element.textContent = value; } } getChildRawContent(nodeName, defaultValue) { if (this.hasChild(nodeName)) { return this.getChild(nodeName).getRawContent(); } return defaultValue !== null && defaultValue !== void 0 ? defaultValue : ''; } setChildRawContent(nodeName, value) { let child = this.getChild(nodeName); if (!child) { child = this.addChild(nodeName); } child.setRawContent(value); } getChildTextContent(nodeName, defaultValue, raw = false) { var _a; if (this.hasChild(nodeName)) { return (_a = this.getChild(nodeName).getTextContent(defaultValue, raw)) !== null && _a !== void 0 ? _a : defaultValue; } return defaultValue; } getChildrenTextContent(nodeName, defaultValue) { if (this.hasChild(nodeName)) { return this.getChildren(nodeName).map(child => child.getTextContent(defaultValue)); } return []; } normalizeNodeName(nodeName) { return normalizeNodeName(nodeName, this.options); } appendChild(node) { if (node instanceof RxapElement) { node = node.element; } this.element.appendChild(node); } addChild(nodeName) { var _a; const element = ((_a = this.element.ownerDocument) !== null && _a !== void 0 ? _a : new this.DOMParser().parseFromString('<html></html>', 'text/xml')).createElement(nodeName); this.element.appendChild(element); return new RxapElement(element, this.DOMParser, this.options); } setChildTextContent(nodeName, value) { let child = this.getChild(nodeName); if (!child) { child = this.addChild(nodeName); } child.setTextContent(value); } addChildTextContent(nodeName, value) { const child = this.addChild(nodeName); child.setTextContent(value); } setTextContent(value) { this.removeAllChildren(); if (this.element.textContent !== undefined) { this.element.textContent = value; } else if (this.element.innerHTML !== undefined) { this.element.innerHTML = value; } else { throw new Error('Could not set text content. Element does not have the property textContent or innerHTML'); } } setChildren(children) { this.removeAllChildren(); for (const child of children) { this.appendChild(child); } } } exports.RxapElement = RxapElement; //# sourceMappingURL=element.js.map