explainers icon indicating copy to clipboard operation
explainers copied to clipboard

[nodesFromRect] why only on document?

Open keithamus opened this issue 4 months ago • 2 comments

This adds a new API to document, but given the sequence<ShadowRoot> shadowRoots = []; it seems as though it would be possible to include shadowdom children by passing references to their roots; however if I just wanted to get elements intersecting a Rect inside my shadowroot, I'd have to do something like:

for(const node of document.nodesFromRect(rect, { shadowRoots: [myshadow] })) {
	if (node.getRootNode() != myShadow) continue;
    // logic
}

Of course this code looks easy but it's giving the user more data than they needed, which they are then discarding. Simple would be if it was on DocumentOrShadowRoot, then one could do:

for(const node of myshadow.nodesFromRect(rect)) {
    // logic
}

Of course this may beg the question: why DocumentOrShadowRoot? Why not simply add it to Node? I think that's something also worth exploring!

keithamus avatar Oct 06 '25 11:10 keithamus

~~Fwiw the reason it's currently on Document is just to match the other cssom-view functions which are also all scoped to Document. If this were added to DocumentOrShadowRoot it would introduce an inconsistency with those others. I think it's definitely a valid point though and potentially something we can do for elementFromPoint() and elementsFromPoint() too?~~

~~I'll update the explainer to, well, explain that decision.~~

As for adding to Node itself see https://github.com/Igalia/explainers/tree/main/nodes-from-rect#nodenodesfromrect - I agree it does seem like it could be valuable for pre-filtering but I think a change like that would probably need some concrete use cases which I don't currently have.

lukewarlow avatar Oct 06 '25 12:10 lukewarlow

I've made a PR to switch to using DocumentOrShadowRoot as it turns out that's what browsers actually use already.

lukewarlow avatar Oct 06 '25 14:10 lukewarlow