Skip to content

Instantly share code, notes, and snippets.

@Zaibot
Last active May 23, 2023 18:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Zaibot/df316511a60477d68207dd310733cafa to your computer and use it in GitHub Desktop.
Save Zaibot/df316511a60477d68207dd310733cafa to your computer and use it in GitHub Desktop.
recursive, descendants
let nodeCurrent: AnchorNode = this;
let idxChild = 0;
for (; ;) {
result.push(nodeCurrent);
while (nodeCurrent !== this && idxChild >= nodeCurrent.children.length) {
// Reached the end of the current node? Select first next sibling of ancestor
idxChild = nodeCurrent.parent.children.indexOf(nodeCurrent) + 1;
nodeCurrent = nodeCurrent.parent;
}
// Is idxChild is pointing beyond the children?
if (idxChild >= nodeCurrent.children.length) {
// Reached the end of the tree
break;
}
// Step into the child
nodeCurrent = nodeCurrent.children[idxChild];
idxChild = 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment