Skip to curriculum
Jump to the editor

16.8. Find elements by a tag path

Hard
Topics

This question is in

16. DOM: finding elements
30–45 min

A CSS selector like nav ul a is a path of tags: an <a> with a <ul> somewhere above it, and a <nav> above that. Browsers match millions of these, and the way they do it is worth knowing: they start from the last tag and look up through the ancestors, not from the first tag down. Writing a small version yourself shows why, and shows the one case where looking up is harder than it seems: mixing "anywhere above" with "directly above".

Write findByTagPath(root, path) in tag-path.js. It returns the elements inside root that end a chain of tags matching path.

Signature

JAVASCRIPT
function findByTagPath(root, path) // returns Element[]
  • root: an element or the document.
  • path: tag names separated by whitespace or >:
    • whitespace means "somewhere above": in div span, the <div> is any ancestor of the <span>;
    • > means "directly above": in ul > li, the <ul> is the <li>'s parent;
    • * is any tag.
  • Returns every element that matches the last tag and has the rest of the chain above it, inside root, in document order, each once. This is the same set as root.querySelectorAll(":scope " + path).

Requirements

  1. With one tag, every element with that tag inside root matches. With more, each tag must be on an ancestor of the next one, in the order written.
  2. Ancestors do not have to be parents: other elements may sit in between.
  3. > requires the parent itself, and may appear with or without spaces around it (ul>li).
  4. A path that mixes both kinds must try farther ancestors when the nearest one does not lead to a full match (section > div p with two nested divs).
  5. * matches any tag; tag names match in any case; extra whitespace is ignored.
  6. Every element of the chain is inside root: root itself and anything above it never count. When root is the document, <html> counts.
  7. Results are in document order and each element appears once, however many chains end on it.
  8. An empty or blank path returns []. Template content and shadow roots are not searched.
  9. It stays fast on deep trees: a six-step path that fails on a chain 300 levels deep must not check the same ancestors again and again. It works with every built-in selector method removed.

Examples

JAVASCRIPT
// <nav id="top">
//   <ul>
//     <li><a id="home">Home</a></li>
//     <li><span><a id="docs">Docs</a></span></li>
//   </ul>
// </nav>
const nav = document.getElementById("top");
findByTagPath(nav, "ul a");         // [a#home, a#docs]
findByTagPath(nav, "ul > li > a");  // [a#home]: a#docs has a <span> parent
findByTagPath(nav, "li>span a");    // [a#docs]
findByTagPath(nav, "nav a");        // []: nav is root, and root does not count
JAVASCRIPT
// <section><div id="outer"><div id="inner"><p id="p">x</p></div></div></section>
findByTagPath(document, "section > div p"); // [p#p]: through #outer, not #inner
JAVASCRIPT
findByTagPath(nav, "  ");  // []

Environment

  • Plain JavaScript in a browser page; the tests call the global findByTagPath from tag-path.js.
  • Not allowed: querySelector, querySelectorAll, matches, closest, getElementsByTagName and getElementsByClassName. Two tests remove them all while they run.
  • Paths contain only tag names, *, whitespace and >. They never start or end with >, and never have two > in a row. The tests use HTML elements only.

Hints

  1. For div span a, is it easier to find each <a> and look up, or to find each <div> and look down?
  2. "Does the chain up to step 3 end at this element?" can be asked about the same element many times. What could you keep so it is answered only once?

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…