Re: [whatwg/dom] Add an optional options dictionary to `closest` to allow jumping across shadow boundaries (Issue #1265)

GarrettS left a comment (whatwg/dom#1265)

It doesn't do a composed walk on its own. It does a composed walk because that is how the new API signature gets defined. And if you can't get an ownerDocument or anything, pass it `null` to signal "keep goin thru shadow root"; an imperative override: "search until node X".

As for the workaround, I think I see where you're coming from now: if `target.closest('#STOP-HERE, button')` hits, it's in the direct ancestral path between target, up to and including that boundary, so if it is the boundary, `#STOP-HERE`, that guarantees there is no button on the path.

Let's say best case, you have a good selector for the ancestor say `.boundary` and don't need to stamp it with `#STOP-HERE`. How's it hold up?
```js
const match = target.closest('.boundary, button');
const finalMatch = (match && !match.matches('.boundary')) ? match : null;
```
That could work. Clunks, tho. Extra conditional checks in the code. Selector engine evaluates both branches of that string at every step of the ancestral climb, then `matches` to check one more time what matched. Computationally heavier than an internal pointer-equality check (currentNode === ancestor). Plus, it requires having a good selector for the boundary node, which isn't always the case.

-- 
Reply to this email directly or view it on GitHub:
https://github.com/whatwg/dom/issues/1265#issuecomment-4652171537
You are receiving this because you are subscribed to this thread.

Message ID: <whatwg/dom/issues/1265/4652171537@github.com>

Received on Monday, 8 June 2026 18:32:13 UTC