Skip to curriculum
Jump to the editor

16.11. Depth of a DOM tree, its unique tags and duplicate ids

Medium
Topics

This question is in

16. DOM: finding elements
15–25 min

Page audits ask simple questions about a whole DOM tree. How deeply nested is it (very deep trees make style and layout slower)? Which tags does it use? Does any id appear twice (which silently breaks <label for>, aria-labelledby and getElementById)? Each answer is one walk over the tree, collecting something different on the way.

Write three functions in tree-stats.js.

Signature

JAVASCRIPT
function treeDepth(root)    // returns a number
function uniqueTags(root)   // returns string[]
function duplicateIds(root) // returns string[]
  • treeDepth(root): the number of levels of elements, counting root as level 1. null has depth 0.
  • uniqueTags(root): the tag names used by root and every element inside it, each once, in the order they first appear in the document. Names as localName gives them.
  • duplicateIds(root): the ids that more than one element (counting root) uses, each once, in the order their first element appears.

Requirements

  1. treeDepth is 1 for an element with no element children and 0 for null; otherwise 1 plus the depth of its deepest child.
  2. Only elements are levels: text and comment nodes do not add depth.
  3. uniqueTags includes root, lists each name once, and keeps the order of first appearance in a depth-first walk (an element, then everything inside it, then its next sibling).
  4. HTML names are lower case; SVG names keep their case (linearGradient).
  5. duplicateIds lists an id once however many times it is used, in the order its first element appears, and includes root's own id.
  6. Empty ids are ignored, ids compare exactly (Main is not main), and SVG elements' ids count.
  7. Template content and shadow roots are not part of the tree.
  8. Everything works 3,000 levels deep and when the built-in search methods are removed.

Examples

JAVASCRIPT
// <div id="app">
//   <header id="top"><h1>Shop</h1></header>
//   <main>
//     <section id="top"><h2>Deals</h2><p>Up to <b id="pct">50%</b></p></section>
//     <p id="pct">Ends soon</p>
//   </main>
// </div>
const app = document.getElementById("app");
treeDepth(app);     // 5: div > main > section > p > b
JAVASCRIPT
treeDepth(document.createElement("p")); // 1
treeDepth(null);                        // 0
JAVASCRIPT
uniqueTags(app);    // ["div", "header", "h1", "main", "section", "h2", "p", "b"]
duplicateIds(app);  // ["top", "pct"]

Environment

  • Plain JavaScript in a browser page; the tests call the globals treeDepth, uniqueTags and duplicateIds from tree-stats.js.
  • root is an element (or null for treeDepth), in the page or not.
  • Not allowed: querySelector, querySelectorAll, getElementsByTagName, getElementsByClassName and getElementById. Two tests remove them while they run. Set and Map are allowed.

Hints

  1. The depth of a tree, in terms of the depths of the trees under its children: what is it?
  2. Which built-in collection remembers the order in which keys were first added, and can also count?

The explanation comes with the course

A walkthrough of every line of a solution, the mistakes the tests catch, other ways to write it, and links to the lessons that teach each idea.

  • Every line explained
  • The mistakes each test catches
  • Links to the lessons behind it
Back to the question
Loading the editor…