Jump to the editorJAVASCRIPT
JAVASCRIPT JAVASCRIPT JAVASCRIPT
16.3. Implement Node.contains
Easy
Topics
This question is in
16. DOM: finding elements"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
function contains(node, other) // returns true or falsenode: any node: an element, a text node, the document, a document fragment or a shadow root.other: the node to look for, ornull.- Returns
truewhenotherisnodeor one of its descendants, otherwisefalse. Always a boolean.
Requirements
- A node contains itself.
- A node contains its children and every descendant below them, at any depth.
- A node does not contain its parent, its siblings or their descendants.
- Text and comment nodes count as descendants; a text node contains only itself.
contains(node, null)isfalse.- The document contains exactly the nodes that are in the page; a detached tree contains only its own nodes.
- 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.
- The answer reflects where
otheris now: a fragment's child stops being inside the fragment once it is moved into the page. - It works for very deep trees, and when
Node.prototype.containsandcompareDocumentPositiondo not exist.
Examples
// <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); // falseconst loose = document.createElement("div");
contains(document, loose); // false: not in the page
document.body.append(loose);
contains(document, loose); // true// a custom element with a shadow root holding <button id="close">
contains(widget, closeButton); // false
contains(widget.shadowRoot, closeButton); // trueEnvironment
- Plain JavaScript in a browser page; the tests call the global
containsfromcontains.js. Node.prototype.containsandNode.prototype.compareDocumentPositionare not allowed: one test removes both while it runs.otheris always a node ornullin the tests.
Hints
- Searching down from
nodecould visit thousands of nodes. Is there a direction with only one path? - 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…