Match elements by tag selectors with descendant and child steps
A browser’s selector engine matches selectors such as nav ul > li a by walking the DOM. In this problem you build a small version of it that understands tag names, the descendant combinator (a space), the child combinator (>), and the * wildcard.
Implement findByTagPath(root, path). It returns an array of every element inside root that matches path.
findByTagPath(root, path) // => Element[]
ul) or *, which matches any element.LI, li, and Li are the same step.section a, the a must be somewhere inside a section.> use the child combinator: in li > a, the a‘s parent must be an li. Spaces around > are optional (li>a is the same path).> and never contain two > in a row. A path that is empty or only whitespace matches nothing.root. root itself and its ancestors never match any step.querySelector, querySelectorAll, getElementsByTagName, matches, or closest. Walk the tree yourself (for example with children and parentElement).<main id="docs">
<section>
<h2>Guides</h2>
<ul>
<li><a>Install</a></li>
<li><span><a>Config</a></span></li>
</ul>
</section>
<aside><a>Help</a></aside>
</main>
const docs = document.getElementById('docs');
const text = (elements) => elements.map((el) => el.textContent);
text(findByTagPath(docs, 'a')); // => ['Install', 'Config', 'Help']
text(findByTagPath(docs, 'section a')); // => ['Install', 'Config']
text(findByTagPath(docs, 'li > a')); // => ['Install']
text(findByTagPath(docs, 'UL>LI > * > A')); // => ['Config']
text(findByTagPath(docs, 'main a')); // => [] (main is the root, so it cannot match)
text(findByTagPath(docs, ' ')); // => []
The nearest matching ancestor is not always the right one. In div > section span, a span may sit inside a section whose parent is not a div, while a section further up does have a div parent. That span matches, so your matcher must keep looking past the first candidate.