@openxmldev/linq-to-xml
Version:
LINQ to XML for TypeScript
103 lines • 3.09 kB
JavaScript
/**
* @author Thomas Barnekow
* @license MIT
*/
/** @internal */
class NamespaceDeclaration {
constructor() {
this.prefix = undefined;
this.ns = undefined;
this.scope = undefined;
this.prev = undefined;
}
}
/** @internal */
export class NamespaceResolver {
constructor() {
this._scope = 0;
this._declaration = null;
this._rover = null;
}
pushScope() {
this._scope++;
}
popScope() {
let d = this._declaration;
if (d !== null) {
do {
d = d.prev;
if (d.scope !== this._scope)
break;
if (d === this._declaration) {
this._declaration === null;
}
else {
this._declaration.prev = d.prev;
}
this._rover = null;
} while (d !== this._declaration && this._declaration !== null);
}
this._scope--;
}
add(prefix, ns) {
const d = new NamespaceDeclaration();
d.prefix = prefix;
d.ns = ns;
d.scope = this._scope;
if (this._declaration === null) {
this._declaration = d;
}
else {
d.prev = this._declaration.prev;
}
this._declaration.prev = d;
this._rover = null;
}
addFirst(prefix, ns) {
const d = new NamespaceDeclaration();
d.prefix = prefix;
d.ns = ns;
d.scope = this._scope;
if (this._declaration === null) {
d.prev = d;
}
else {
d.prev = this._declaration.prev;
this._declaration.prev = d;
}
this._declaration = d;
this._rover = null;
}
// Only elements allow default namespace declarations. The rover
// caches the last namespace declaration used by an element.
getPrefixOfNamespace(ns, allowDefaultNamespace) {
if (this._rover !== null &&
this._rover.ns === ns &&
(allowDefaultNamespace || this._rover.prefix.length > 0)) {
return this._rover.prefix;
}
let d = this._declaration;
if (d !== null) {
do {
d = d.prev;
if (d.ns === ns) {
let x = this._declaration.prev;
while (x !== d && x.prefix !== d.prefix) {
x = x.prev;
}
if (x === d) {
if (allowDefaultNamespace) {
this._rover = d;
return d.prefix;
}
else if (d.prefix.length > 0) {
return d.prefix;
}
}
}
} while (d !== this._declaration);
}
return null;
}
}
//# sourceMappingURL=NamespaceResolver.js.map