UNPKG

xpath-ts2

Version:

DOM 3 and 4 XPath 1.0 implementation for browser and Node.js environment with support for typescript 5.

133 lines 5.7 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.XPathResultImpl = void 0; const xpath_exception_1 = require("./xpath-exception"); const xpath_types_1 = require("./xpath-types"); class XPathResultImpl { static ANY_TYPE = 0; static NUMBER_TYPE = 1; static STRING_TYPE = 2; static BOOLEAN_TYPE = 3; static UNORDERED_NODE_ITERATOR_TYPE = 4; static ORDERED_NODE_ITERATOR_TYPE = 5; static UNORDERED_NODE_SNAPSHOT_TYPE = 6; static ORDERED_NODE_SNAPSHOT_TYPE = 7; static ANY_UNORDERED_NODE_TYPE = 8; static FIRST_ORDERED_NODE_TYPE = 9; resultType; numberValue; stringValue; booleanValue; nodes = undefined; singleNodeValue = undefined; invalidIteratorState = undefined; iteratorIndex = undefined; snapshotLength = undefined; ANY_TYPE = XPathResultImpl.ANY_TYPE; NUMBER_TYPE = XPathResultImpl.NUMBER_TYPE; STRING_TYPE = XPathResultImpl.STRING_TYPE; BOOLEAN_TYPE = XPathResultImpl.BOOLEAN_TYPE; UNORDERED_NODE_ITERATOR_TYPE = XPathResultImpl.UNORDERED_NODE_ITERATOR_TYPE; ORDERED_NODE_ITERATOR_TYPE = XPathResultImpl.ORDERED_NODE_ITERATOR_TYPE; UNORDERED_NODE_SNAPSHOT_TYPE = XPathResultImpl.UNORDERED_NODE_SNAPSHOT_TYPE; ORDERED_NODE_SNAPSHOT_TYPE = XPathResultImpl.ORDERED_NODE_SNAPSHOT_TYPE; ANY_UNORDERED_NODE_TYPE = XPathResultImpl.ANY_UNORDERED_NODE_TYPE; FIRST_ORDERED_NODE_TYPE = XPathResultImpl.FIRST_ORDERED_NODE_TYPE; constructor(v, t) { if (t === XPathResultImpl.ANY_TYPE) { if (v instanceof xpath_types_1.XString) { t = XPathResultImpl.STRING_TYPE; } else if (v instanceof xpath_types_1.XNumber) { t = XPathResultImpl.NUMBER_TYPE; } else if (v instanceof xpath_types_1.XBoolean) { t = XPathResultImpl.BOOLEAN_TYPE; } else if (v instanceof xpath_types_1.XNodeSet || v == null) { t = XPathResultImpl.UNORDERED_NODE_ITERATOR_TYPE; } } this.resultType = t; switch (t) { case XPathResultImpl.NUMBER_TYPE: this.numberValue = v.numberValue; this.stringValue = v.stringValue; this.booleanValue = v.booleanValue; return; case XPathResultImpl.STRING_TYPE: this.numberValue = v.numberValue; this.stringValue = v.stringValue; this.booleanValue = v.booleanValue; return; case XPathResultImpl.BOOLEAN_TYPE: this.numberValue = v.numberValue; this.stringValue = v.stringValue; this.booleanValue = v.booleanValue; return; case XPathResultImpl.ANY_UNORDERED_NODE_TYPE: case XPathResultImpl.FIRST_ORDERED_NODE_TYPE: if (v instanceof xpath_types_1.XNodeSet) { const first = v.first(); this.singleNodeValue = first; this.numberValue = v.numberValue; this.stringValue = v.stringValue; this.booleanValue = v.booleanValue; this.nodes = first != null ? [first] : []; return; } break; case XPathResultImpl.UNORDERED_NODE_ITERATOR_TYPE: case XPathResultImpl.ORDERED_NODE_ITERATOR_TYPE: if (v instanceof xpath_types_1.XNodeSet) { this.invalidIteratorState = false; this.nodes = v.toArray(); this.iteratorIndex = 0; this.numberValue = v.numberValue; this.stringValue = v.stringValue; this.booleanValue = v.booleanValue; return; } else if (v == null) { this.nodes = []; this.snapshotLength = 0; this.numberValue = 0; this.stringValue = ''; this.booleanValue = false; return; } break; case XPathResultImpl.UNORDERED_NODE_SNAPSHOT_TYPE: case XPathResultImpl.ORDERED_NODE_SNAPSHOT_TYPE: if (v instanceof xpath_types_1.XNodeSet) { this.nodes = v.toArray(); this.snapshotLength = this.nodes.length; this.numberValue = v.numberValue; this.stringValue = v.stringValue; this.booleanValue = v.booleanValue; return; } break; } throw new xpath_exception_1.XPathException(xpath_exception_1.XPathException.TYPE_ERR); } iterateNext() { if (this.resultType !== XPathResultImpl.UNORDERED_NODE_ITERATOR_TYPE && this.resultType !== XPathResultImpl.ORDERED_NODE_ITERATOR_TYPE) { throw new xpath_exception_1.XPathException(xpath_exception_1.XPathException.TYPE_ERR); } if (this.iteratorIndex === this.nodes.length) { return undefined; } return this.nodes[this.iteratorIndex++]; } snapshotItem(i) { if (this.resultType !== XPathResultImpl.UNORDERED_NODE_SNAPSHOT_TYPE && this.resultType !== XPathResultImpl.ORDERED_NODE_SNAPSHOT_TYPE) { throw new xpath_exception_1.XPathException(xpath_exception_1.XPathException.TYPE_ERR); } return this.nodes[i]; } } exports.XPathResultImpl = XPathResultImpl; //# sourceMappingURL=xpath-result-impl.js.map