Last active
May 23, 2023 18:57
-
-
Save Zaibot/df316511a60477d68207dd310733cafa to your computer and use it in GitHub Desktop.
recursive, descendants
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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