Skip to curriculum
Jump to the editor

16.3. Implement Node.contains

Easy
Topics

This question is in

16. DOM: finding elements
10–15 min

"Did this click land inside the menu?" is the question behind every dropdown, popover and modal that closes on an outside click. node.contains(other) answers it: it is true when other is node itself or anywhere inside it, at any depth. Focus traps, drag-and-drop targets and "is this element still in the page?" checks use it too.

Write contains(node, other) in contains.js. It returns the same answer as node.contains(other), without calling it.

Signature

JAVASCRIPT
function contains(node, other) // returns true or false
  • node: any node: an element, a text node, the document, a document fragment or a shadow root.
  • other: the node to look for, or null.
  • Returns true when other is node or one of its descendants, otherwise false. Always a boolean.

Requirements

  1. A node contains itself.
  2. A node contains its children and every descendant below them, at any depth.
  3. A node does not contain its parent, its siblings or their descendants.
  4. Text and comment nodes count as descendants; a text node contains only itself.
  5. contains(node, null) is false.
  6. The document contains exactly the nodes that are in the page; a detached tree contains only its own nodes.
  7. Shadow trees are separate, as with the built-in: a host does not contain the nodes in its shadow root, but the shadow root does.
  8. The answer reflects where other is now: a fragment's child stops being inside the fragment once it is moved into the page.
  9. It works for very deep trees, and when Node.prototype.contains and compareDocumentPosition do not exist.

Examples

JAVASCRIPT
// <nav id="menu"><ul><li><a id="docs">Docs</a></li></ul></nav>
const menu = document.getElementById("menu");
const link = document.getElementById("docs");
contains(menu, link);             // true
contains(menu, link.firstChild);  // true: the text node "Docs"
contains(link, menu);             // false
contains(menu, menu);             // true
contains(menu, null);             // false
JAVASCRIPT
const loose = document.createElement("div");
contains(document, loose);        // false: not in the page
document.body.append(loose);
contains(document, loose);        // true
JAVASCRIPT
// a custom element with a shadow root holding <button id="close">
contains(widget, closeButton);             // false
contains(widget.shadowRoot, closeButton);  // true

Environment

  • Plain JavaScript in a browser page; the tests call the global contains from contains.js.
  • Node.prototype.contains and Node.prototype.compareDocumentPosition are not allowed: one test removes both while it runs.
  • other is always a node or null in the tests.

Hints

  1. Searching down from node could visit thousands of nodes. Is there a direction with only one path?
  2. Which parent property does a text node have, and where does it lead from the top of a tree?

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…