-
Notifications
You must be signed in to change notification settings - Fork 671
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[cssom] There should be a way to test CSSStyleRules against Elements #10470
Comments
Seems like a valid request/use-case. Not sure if it should become |
I have no strong preference for either, but since |
To be slightly pedantic, this new function should be on Element not HTMLElement (between the two). As is the case with matches(). |
Good callout, thanks. I've updated the proposal. |
This seems like a useful addition! |
I updated the proposal to handle pseudo classes and elements. |
I created a designer (https://github.com/node-projects/web-component-designer) where I use these APIs to show the matching CSS. For nesting (wich I do not support yet), I will have a solution to at least combine all the parent rules, but for scope this would be much complexer.
and this API should return true, if the element is inside of the scope. But for sure, I also vote for the API for the Style Rule |
The new |
To determine whether a given
CSSStyleRule
applies to a DOM element you can take itsselectorText
and use theelement.matches()
DOM API to check whether that selector applies to it. Doing so hasn't been sufficient for a long time:<style>
or<link>
element with a media attribute&
.:scope
.For media queries there is
window.matchMedia()
, for container queries there is going to beelement.matchContainer()
, for@supports
there isCSS.supports()
. This means you can at least get the information, but you will still need to walk the CSSOM to findconditionText
s to match against. (It seems that media and supports at-rules are getting a.matches
property as discussed in #4240. This would be easier.)For nesting and scoping however, you will need to walk the CSSOM and build up a complete, potentially very complex, selector to test against and, in the case of implicit scoping, also check the DOM and determine a selector to use for the common ancestor. This is far from ideal. For nesting this could potentially be solved with a resolved selectorText like discussed in #10246, but that wouldn't account for scoping.
All situations require you to loop over at least all ancestors in the CSSOM to get a full picture for a single
CSSStyleRule
, and potentially switching over to the DOM for attributes or common ancestors.Proposal
The addition of a new API that explicitly tests if a given CSSStyleRule applies to a given Element with an optional pseudo-state (or class).
Applies here means that the content of the CSSStyleRule is used when determining the style of an element, regardless of whether the declarations inside the rule actually end up being used or are overwritten by style with higher specificity.
CSSStyleRule
currently does not expose any functions so it makes sense to add a new method to theElement
instead, possibly calledmatchRule()
, that takes aCSSStyleRule
and an optional second string argument indicating the pseudo state or element to match against, and returns a boolean value.Alternatively, when matching for example a
p
against a CSSStyleRule with the selectordiv:hover p
the matcher could not take that:hover
pseudo-state into account and instead return an object that has a.matches
boolean property (likewindow.matchMedia
) as well as aconditional
property that has an object property that could be shaped{ element: Element, state: string }
:In addition it would be very useful if that function returned more information such as the resolved specificity or resolved/matched
selectorText
(for when theCSSStyleRule
contains a selectorlist) and information on the conditionals (media, container, supports layer,) applied to theCSSStyleRule
, though this is less important than the matching test itself.I'm happy to do the work here in terms of spec writing (after consensus), if someone can nudge me in the right direction.
edit 20 june 24: expanded the proposal to include checking for pseudo classes and elements.
The text was updated successfully, but these errors were encountered: