svgdom
Version:
Straightforward DOM implementation for SVG, HTML and XML
44 lines (35 loc) • 1.91 kB
JavaScript
import { NodeFilter } from '../dom/NodeFilter.js'
const showThisNode = (whatToShow, node) => {
if (whatToShow === NodeFilter.SHOW_ALL) return true
if (whatToShow & NodeFilter.SHOW_ELEMENT && node.nodeType === node.ELEMENT_NODE) return true
if (whatToShow & NodeFilter.SHOW_TEXT && node.nodeType === node.TEXT_NODE) return true
if (whatToShow & NodeFilter.SHOW_ENTITY_REFERENCE && node.nodeType === node.ENTITY_REFERENCE_NODE) return true
if (whatToShow & NodeFilter.SHOW_ENTITY && node.nodeType === node.ENTITY_NODE) return true
if (whatToShow & NodeFilter.SHOW_PROCESSING_INSTRUCTION && node.nodeType === node.PROCESSING_INSTRUCTION_NODE) return true
if (whatToShow & NodeFilter.SHOW_COMMENT && node.nodeType === node.COMMENT_NODE) return true
if (whatToShow & NodeFilter.SHOW_DOCUMENT && node.nodeType === node.DOCUMENT_NODE) return true
if (whatToShow & NodeFilter.SHOW_DOCUMENT_TYPE && node.nodeType === node.DOCUMENT_TYPE_NODE) return true
if (whatToShow & NodeFilter.SHOW_DOCUMENT_FRAGMENT && node.nodeType === node.DOCUMENT_FRAGMENT_NODE) return true
if (whatToShow & NodeFilter.SHOW_NOTATION && node.nodeType === node.NOTATION_NODE) return true
return false
}
export class NodeIterator {
constructor (root, whatToShow = NodeFilter.SHOW_ALL, filter = () => NodeFilter.FILTER_ACCEPT, includeParent = true) {
this.root = includeParent ? { childNodes: [ root ] } : root
this.whatToShow = whatToShow
this.filter = filter
}
* [Symbol.iterator] () {
const nodes = this.root.childNodes
for (const node of nodes) {
if (!showThisNode(this.whatToShow, node)) continue
const filterRet = this.filter(node)
if (filterRet === NodeFilter.FILTER_REJECT) continue
if (filterRet === NodeFilter.FILTER_ACCEPT) {
yield node
}
yield * new NodeIterator(node, this.whatToShow, this.filter, false)
}
return this
}
}