UNPKG

uhtml

Version:
36 lines (31 loc) • 833 B
'use strict'; const { COMMENT_NODE, ELEMENT_NODE } = require('domconstants/constants'); const { childNodes, nodeType } = require('./symbols.js'); const asType = (accept, type) => ( (type === ELEMENT_NODE && (accept & 0x1)) ? ELEMENT_NODE : (type === COMMENT_NODE && (accept & 0x80)) ? COMMENT_NODE : 0 ); module.exports = class TreeWalker { constructor(parent, accept) { this[childNodes] = walk(parent, accept); } nextNode() { const { value, done } = this[childNodes].next(); return done ? null : value; } } function* walk(parent, accept) { for (const node of parent[childNodes]) { switch (asType(accept, node[nodeType])) { case ELEMENT_NODE: yield node; yield* walk(node, accept); break; case COMMENT_NODE: yield node; break; } } }