-
-
Save ducksoupdev/44a4724395be93b87d39c770b34e71f0 to your computer and use it in GitHub Desktop.
Get depth of a DOM tree with a list of nodes included into longest path
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
function getDomDepthLevel(root = document.documentElement) { | |
let pathInfo = { | |
route: [], | |
level: 0 | |
}; | |
for (let i = 0, j = root.children.length; i < j; i++) { | |
const curNodePathInfo = getDomDepthLevel(root.children[i]); | |
if (curNodePathInfo.level > pathInfo.level) { | |
pathInfo = curNodePathInfo; | |
} | |
} | |
pathInfo.route.unshift(root); | |
pathInfo.level += 1; | |
return pathInfo; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment