16.8. Find elements by a tag path
Topics
This question is in
16. DOM: finding elementsA 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
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": inul > li, the<ul>is the<li>'s parent;*is any tag.
- whitespace means "somewhere above": in
- 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 asroot.querySelectorAll(":scope " + path).
Requirements
- With one tag, every element with that tag inside
rootmatches. With more, each tag must be on an ancestor of the next one, in the order written. - Ancestors do not have to be parents: other elements may sit in between.
>requires the parent itself, and may appear with or without spaces around it (ul>li).- A path that mixes both kinds must try farther ancestors when the nearest one does not lead to a full match (
section > div pwith two nesteddivs). *matches any tag; tag names match in any case; extra whitespace is ignored.- Every element of the chain is inside
root:rootitself and anything above it never count. Whenrootis the document,<html>counts. - Results are in document order and each element appears once, however many chains end on it.
- An empty or blank path returns
[]. Template content and shadow roots are not searched. - 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
// <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// <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 #innerfindByTagPath(nav, " "); // []Environment
- Plain JavaScript in a browser page; the tests call the global
findByTagPathfromtag-path.js. - Not allowed:
querySelector,querySelectorAll,matches,closest,getElementsByTagNameandgetElementsByClassName. 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
- For
div span a, is it easier to find each<a>and look up, or to find each<div>and look down? - "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