dperini / nwsapi

Fast CSS Selectors API Engine
MIT License
103 stars 35 forks source link

:scope in the context of the Document (DOCUMENT_NODE) object does not refer to the root element #93

Closed olegbarabanov closed 7 months ago

olegbarabanov commented 1 year ago

In the context of a Document object (DOCUMENT_NODE), the :scope pseudo-class does not refer to the root element, according to the specification details https://drafts.csswg.org/selectors-4/#the-scope-pseudo.

Minimal reproduction case:

// browser (Chromium-based, Firefox, ...)
document.querySelector(':scope > head'); // => OK - return HTMLHeadElement element
document.querySelector(':scope > body'); // => OK - return HTMLBodyElement element

// nwsapi@2.2.7
NW.Dom.first(':scope > head'); // Error - return null
NW.Dom.first(':scope > body'); // Error - return null

Live example (in sandbox): https://codepen.io/olegbarabanov/pen/qBQpzxR

Note

nwsapi version: 2.2.7

b-fuze commented 1 year ago

You can pass the root node like this

NW.Dom.first(':scope > head', document.documentElement);

in the case of no explicit context. It probably doesn't solve the case of NW.Dom.first(":scope", document) incorrectly returning null when it should return the root element.

olegbarabanov commented 1 year ago

You can pass the root node like this

NW.Dom.first(':scope > head', document.documentElement);

in the case of no explicit context. It probably doesn't solve the case of NW.Dom.first(":scope", document) incorrectly returning null when it should return the root element.

@b-fuze, thank you.

A similar solution using document.documentElement has already been proposed in a pending pull request (#94) to make the behavior of :scope on document the same as described in the standard and how it works in browsers.

For now, explicitly passing document.documentElement is the best workaround when dealing with :scope and document.