UNPKG

@openxmldev/linq-to-xml

Version:
70 lines 2.65 kB
/** * @author Thomas Barnekow * @license MIT */ import { ArgumentException, XNamespace } from './internal.js'; /** * Represents XML names. */ export class XName { /** * Initializes a new `XName` with the given namespace, local name, and prefix. * * @param namespace The `XNamespace`. * @param localName The local name. */ constructor(namespace, localName) { this.namespace = namespace; this.localName = localName; } /** * Gets the namespace name. */ get namespaceName() { return this.namespace.namespaceName; } // Implementation static get(expandedOrLocalName, namespaceName) { if (namespaceName !== undefined) { // get(localName: string, namespaceName: string): XName const localName = expandedOrLocalName; return XNamespace.get(namespaceName).getName(localName); } else { // get(expandedName: string): XName const expandedName = expandedOrLocalName; if (expandedName === '') { throw new ArgumentException('expandedName', 'Expanded name is empty.'); } if (expandedName[0] !== '{') { // Expanded name does not contain "{namespaceName}". return XNamespace.none.getName(expandedName); } const lastIndex = expandedName.lastIndexOf('}'); if (lastIndex <= 1) { // Expanded name does not contain closing braces or starts with "{}". throw new ArgumentException('expandedName', 'Namespace name is malformed.'); } if (lastIndex === expandedName.length - 1) { // Expanded name is "{namespaceName}". throw new ArgumentException('expandedName', 'Local name is empty.'); } namespaceName = expandedName.substring(1, lastIndex); const localName = expandedName.substring(lastIndex + 1); return XNamespace.get(namespaceName).getName(localName); } } /** * Gets the string representation of this `XName`, i.e., either: * - `localName`, if the namespace name is the empty string, or * - \{`namespaceName`\}`localName`, if the namespace name is not empty. * * @returns The string representation. */ toString() { return this.namespace.namespaceName.length === 0 ? this.localName : '{' + this.namespace.namespaceName + '}' + this.localName; } } //# sourceMappingURL=XName.js.map