/** * Enumerate through all the descendent including self. * @param e Element */ export function *descendentIterator(e: Node, addSelf = true) { const stack: Node[] = []; const start = e; if (!start) { return; } if (addSelf) { stack.push(start); } else { const { firstChild } = start; if (!firstChild) { return; } stack.push(firstChild); } while (stack.length) { const pop = stack.pop(); const next = pop.nextSibling; yield pop; if (next) { stack.push(next); } const child = pop.firstChild; if (child) { stack.push(child); } } } /** * Enumerate through all the descendants including self. * @param e Element */ export function *descendentElementIterator(e: Element) { const stack: Element[] = []; let current = e; do { do { const next = current.nextElementSibling; yield current; // this is the case if current was modified // firstElementChild might be different const child = current.firstElementChild; if (child) { stack.push(child); } current = next; } while (current); current = stack.pop(); } while(current); }