Re: [csswg-drafts] [css-selectors] Nearest descendant selector (#4940)

Just because something *can* be implemented with `:has()`, doesn't mean it's equivalent to `:has()`; `:has()` is a very big hammer and can pound a *lot* of nails. ^_^

This is just a request for "first descendant matching a selector", which yeah, has been a common request over the years. @radogado's original example is a somewhat novel showing of it, I think (usually it's "I have nested components, and I want to select the nearest one only".  Looks like it was last discussed [in 2015 on the mailing list](

The simpler form that's just about nesting isn't too bad. `.foo /closest/ .bar .baz` would be matched as:

> 1. Is this element a `.baz`? If no, bail.
> 2. If so, walk the ancestors until I find a `.bar`. If none, bail.
> 3. Continue walking the ancestors, looking for either a `.bar` or `.foo`. If I find a `.bar`, bail. If I find nothing, bail.
> 4. Yay, the element matched!

In other words it matches just like a normal descendant combinator, just checking the compound selectors on either side of the combinator, rather than just the one on the left. Slightly more expensive, but not a huge deal. It also might slightly widen the space of mutations to watch for to indicate you need to rerun styling on a subtree.

But the more general form that @radogado opened with where the non-matched element might be in a different subtree than the matched one would be *substantially* more expensive, and I suspect would kick us out of the realm of possibility.

GitHub Notification of comment by tabatkins
Please view or discuss this issue at using your GitHub account

Received on Friday, 10 April 2020 18:43:25 UTC