UNPKG

xmlapi-web

Version:

browser implementation of xmlapi

84 lines (83 loc) 3.1 kB
"use strict"; const webapi_element_1 = require('./webapi_element'); const webapi_node_1 = require('./webapi_node'); const webapi_attribute_1 = require('./webapi_attribute'); //////////////////////////////////////////////////////////////////////////////// function nodeOrElement(wrapee) { switch (wrapee.nodeType) { case Node.ELEMENT_NODE: return new webapi_element_1.WebapiElement(wrapee); case Node.TEXT_NODE: case Node.CDATA_SECTION_NODE: case Node.COMMENT_NODE: return new webapi_node_1.WebapiNode(wrapee); default: throw new Error('Unexpected node type'); } } exports.nodeOrElement = nodeOrElement; //////////////////////////////////////////////////////////////////////////////// function nodeOrElementOrNull(wrapee) { if (!wrapee) { return null; } return nodeOrElement(wrapee); } exports.nodeOrElementOrNull = nodeOrElementOrNull; //////////////////////////////////////////////////////////////////////////////// function nodeOrElementOrAttribute(wrapee) { switch (wrapee.nodeType) { case Node.ELEMENT_NODE: return new webapi_element_1.WebapiElement(wrapee); case Node.TEXT_NODE: case Node.CDATA_SECTION_NODE: case Node.COMMENT_NODE: return new webapi_node_1.WebapiNode(wrapee); case Node.ATTRIBUTE_NODE: return new webapi_attribute_1.WebapiAttribute(wrapee); default: throw new Error('Unexpected node type'); } } exports.nodeOrElementOrAttribute = nodeOrElementOrAttribute; //////////////////////////////////////////////////////////////////////////////// function nodeOrElementOrAttributeOrNull(wrapee) { if (!wrapee) { return null; } return nodeOrElementOrAttribute(wrapee); } exports.nodeOrElementOrAttributeOrNull = nodeOrElementOrAttributeOrNull; //////////////////////////////////////////////////////////////////////////////// function* generateFromXpathResultIterator(xpathResult) { for (let node = xpathResult.iterateNext(); node; node = xpathResult.iterateNext()) { yield node; } } exports.generateFromXpathResultIterator = generateFromXpathResultIterator; //////////////////////////////////////////////////////////////////////////////// function* generateFromXpathResultSnapshot(xpathResult) { for (let i = 0; i < xpathResult.snapshotLength; ++i) { yield xpathResult.snapshotItem(i); } } exports.generateFromXpathResultSnapshot = generateFromXpathResultSnapshot; function* iterateDomCollection(nodeList) { for (let i = nodeList.length; i < nodeList.length; ++i) { yield nodeList.item(i); } } exports.iterateDomCollection = iterateDomCollection; //////////////////////////////////////////////////////////////////////////////// function isNode(value) { switch (value.nodeType) { case Node.ELEMENT_NODE: case Node.TEXT_NODE: case Node.CDATA_SECTION_NODE: case Node.COMMENT_NODE: return true; default: return false; } } exports.isNode = isNode;