Skip to content

Instantly share code, notes, and snippets.

@ducksoupdev
Forked from JasonRammoray/dom-tree-depth-level.js
Created December 18, 2019 06:20
Show Gist options
  • Save ducksoupdev/44a4724395be93b87d39c770b34e71f0 to your computer and use it in GitHub Desktop.
Save ducksoupdev/44a4724395be93b87d39c770b34e71f0 to your computer and use it in GitHub Desktop.

Revisions

  1. @JasonRammoray JasonRammoray created this gist Mar 9, 2018.
    15 changes: 15 additions & 0 deletions dom-tree-depth-level.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,15 @@
    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;
    }