Skip to content

Instantly share code, notes, and snippets.

@JasonRammoray
Created March 9, 2018 20:36
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save JasonRammoray/6c605865578c294aaa4d21cb65d6e7f2 to your computer and use it in GitHub Desktop.
Save JasonRammoray/6c605865578c294aaa4d21cb65d6e7f2 to your computer and use it in GitHub Desktop.
Get depth of a DOM tree with a list of nodes included into longest path
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