Skip to curriculum
Jump to the editor

16.4. Implement getRootNode

Easy
Topics

This question is in

16. DOM: finding elements
10–20 min

Every node lives in a tree, and every tree has a root: the document for nodes in the page, a document fragment for nodes that have not been inserted yet, the top element of a detached tree, or a shadow root for nodes inside a web component. node.getRootNode() returns that root. Components use it to find the shadow root they render into, to read activeElement there, or to check whether a node is in the page at all.

Write getRootNode(node, options) in root-node.js. It returns the same node as node.getRootNode(options), without calling it.

Signature

JAVASCRIPT
function getRootNode(node, options) // returns a Node
  • node: any node.
  • options: optional, { composed: boolean }. Without it, composed is false.
  • Returns the root of node's tree. With composed true, a shadow root is not a stopping point: the search goes on from the shadow root's host element, until the root is not a shadow root.

Requirements

  1. For a node in the page (an element or a text node), the root is the document. The document is its own root.
  2. A node with no parent is its own root; a node in a detached tree has the tree's top node as its root.
  3. A document fragment, including a template's content, is the root of the nodes inside it.
  4. For a node inside a shadow tree, the root is the shadow root, both without options and with composed: false.
  5. With composed: true, the search continues from the shadow root's host, through any number of nested shadow trees, to wherever the host's tree ends (the document, or a detached top element).
  6. Closed shadow roots behave the same as open ones.
  7. composed is read as true or false, so { composed: 1 } counts as true.
  8. The answer follows the node's current position, and it works for very deep trees.
  9. It works when Node.prototype.getRootNode does not exist.

Examples

JAVASCRIPT
getRootNode(document.body);              // document
getRootNode(document.createElement("p")); // that <p>: no parent, so it is the root
JAVASCRIPT
const card = document.createElement("article");
card.innerHTML = "<h2>Title</h2>";
getRootNode(card.querySelector("h2"));   // card
JAVASCRIPT
// <user-menu> in the page has a shadow root holding <button id="open">
const shadow = menu.attachShadow({ mode: "open" });
shadow.innerHTML = '<button id="open">Menu</button>';
const button = shadow.getElementById("open");

getRootNode(button);                      // shadow (the ShadowRoot)
getRootNode(button, { composed: true });  // document

Environment

  • Plain JavaScript in a browser page; the tests call the global getRootNode from root-node.js.
  • Node.prototype.getRootNode is not allowed: one test removes it while it runs. parentNode, ShadowRoot and a shadow root's host are allowed.

Hints

  1. What is the parentNode of a shadow root?
  2. Once you jump from a shadow root to its host, how is the rest of the search different from the start?

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…