-
Notifications
You must be signed in to change notification settings - Fork 661
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
[selectors-4] reconsider specificity rule for :matches() #1027
Comments
@dbaron, could you please provide an example of this possibility? My understanding so far is that Also, given that |
Yes, :matches() is solely syntactic sugar (tho with complex selector arguments, the expanded form can get very verbose - try to expand out |
This example isn't very verbose. Consider |
If I understand correctly, an example could be <div id="a"></div>
<div id="b">
<div id="c"></div>
<div id="d" class="foo">
<span id="e">Styles are being assigned to this element</span>
</div>
</div> :matches(#a, *) + :matches(.foo, *) span An implementation could detect that
So the whole selector is matched with specificity (0,0,0)+(0,1,0)+(0,0,1) = (0,1,1) However, there is another way to match the selector:
So the whole selector is matched with specificity (1,0,0)+(0,0,0)+(0,0,1) = (1,0,1) It would be bad if different implementations calculated the specificity of
I see various possible solutions:
|
@Loirooriol, I don’t see this ambiguity in the spec. Since the spec for
this example will be expanded as
and by the rules of the selectors list specificity, its specificity will be the largest one of the individual selectors in the list that match the element, in this example it would be (1, 0, 1) of Do I miss something? |
Yes, with that interpretation the specificity is not ambiguous, but as dbaron said, the definition "isn't worded in a particularly clear way". And may not be the best choice if it has performance problems (expanding |
Attempted to address the clarity issue in 88b91c0; the specificity rules were written with only compound selectors in mind, and so for complex arguments the existing prose indeed didn't make sense. I don't think it's perfect now, but should be better. That doesn't address the perf concerns, though. @Loirooriol's comment #1027 (comment) summarizes some of the options; I'll repeat them here and add the missing one:
|
The last option looks promising because the specificity of the selector can be determined without expanding it, but would it play a significant role in practice? If checking for selector matching would require expanding it anyway, will calculating the specificity dynamically necessary need any overhead in terms of performance? Maybe @victoriasu can provide some details from the implementer's perspective? |
@fantasai While definitely useful, I think 88b91c0 still does not clarify that the "calculating a selector’s specificity" algorithm requires an element, and which element is used for the argument of And thanks for adding the missing option, I thought I included it but it seems I forgot. It may be the nicest one. I'm not an expert but I expect that matching |
@SelenIT I am planning on expanding |
Oh shoot, you're right, I meant to write |
As far as I understand, we have 2 implementations of Is the way of calculating its specificity still subject to change? |
So at first glance at the Chromium's code it seems that very few expansions are allowed: Effectively https://jsfiddle.net/u9q1ogc7/ demonstrates the failure. Text should be green. I think |
FWIW, @Loirooriol 's previous comment led to bug 817835. Today @emilio and I filed bug 842157 about how Chromium's expansion approach produces incorrect results, e.g., on this test (which WebKit passes). |
So this seems reasonable to me. Slightly unfortunate, but reasonable. Agenda+ing for confirmation. |
If, like me, you had trouble following how head ~ :matches(html > *) {} If I understand correctly, that selector should match head ~ html > * { /* nonsense */ } Chrome (and postcss-selector-matches) have incorrectly interpreted Anyway, this is just here to save people like me an hour or so of processing time. And if I misunderstood, please correct me. |
@jonathantneal, I agree with your understanding! This selector says "all following siblings of the html > head ~ * {
/* targeting elements that are siblings of `head` _and_ children of `html`
implies that `head` itself must be child of `html`, too */
} However, your example made me realize that expanding the brackets of :matches(.a .b .c):matches(.d .e) { ... } (based on examples above) which would be expanded out as .a .b .d .c.e,
.a .b.d .c.e,
.a .d .b .c.e,
.a.d .b .c.e.
.d .a .b .c.e {
/* target elements with both 'c' and 'e' classes inside '.a .b' and '.d' in the same time */
} which becomes rather verbose, but still easier to figure out. So some implementation feedback from the WebKit team would be really appreciated! |
Considering the selector in the test from the @dbaron's comment above: if there were classes instead of type selectors there .h3 ~ :matches(.h1 ~ p.test):matches(.h2 ~ p.test) { ... } then it would be expanded as .h1 ~ .h2 ~ .h3 ~ p.test,
.h1 ~ .h3 ~ .h2 ~ p.test,
.h2 ~ .h1 ~ .h3 ~ p.test,
.h2 ~ .h3 ~ .h1 ~ p.test,
.h3 ~ .h1 ~ .h2 ~ p.test,
.h3 ~ .h2 ~ .h1 ~ p.test,
.h1.h2 ~ .h3 ~ p.test,
.h1.h3 ~ .h2 ~ p.test,
.h2.h3 ~ .h1 ~ p.test,
.h1 ~ .h2.h3 ~ p.test,
.h2 ~ .h1.h3 ~ p.test,
.h3 ~ .h1.h2 ~ p.test,
.h1.h2.h3 ~ p.test { /* p.test preceded by all .h1, .h2, and .h3 in any possible order */ } With type selectors (as in the original example) only the first 6 combinations make sense, so the equivalent expanded result can be shorter. |
Correct all around; expanding |
suggests specificity (0,0,5)
suggests specificity (0,0,3), unless we consider the "equivalent selector written without :matches()" to mean
This appears to imply that the specificity of the :matches() pseudo-class is replaced by the specificity of the most specific selector in its argument, so |
The current spec defines that it has the specificity of the matched branch, exactly as if you'd fully expanded the The proposal in this thread is that it instead has a fixed specificity, probably identical to |
The Working Group just discussed
The full IRC log of that discussion<dael> Topic: reconsider specificity rule for :matches()<dael> github: https://github.com//issues/1027 <dael> dbaron: I originally filed this, but don't have a strong opinion on decision. Spec needs to be clear on which <dael> TabAtkins: Other people have argued one direction: :matches() can introduce some thorny issues on selector inheritence. matches specificity is as specific as the most specific branch. More then one :matches with combinators in the branches...you get...you get a combinatorial explosion. You get 100s or 1000s of selectosr without going deep <dael> TabAtkins: Naive calc is expensive for memory and unbounded costs. <fantasai> List of options for considerations - https://github.com//issues/1027#issuecomment-354655842 <dael> TabAtkins: Suggestion was don't bother with that. Resolve it the same as :not and :has where it'sspecificity of the most specific branch. So if you put an ID or a tag it'll b e that. THat's straight forward and matches other similar pseudo classes <dael> TabAtkins: Only problem is that pre-processors doing :matches ahead can only do it with expanding. @extend in SASS will result in a specificity change. It's not a backwards commpat issue but may be a problem with people or SASS trying to switch to doing the new stuff. <dael> astearns: [reads dbaron comment] <dael> TabAtkins: I believe it's correct. <dael> fantasai: :not takes specificity of most specific arg that didn't match. <dael> TabAtkins: :not takesa full selector list <dael> TabAtkins: There's a note. "is replacecd by specificity of most specific element" That note is a liar. That's not true according to spec. <dael> ??: POinted out a few lies in my comment on the issue <astearns> s/??/ericwilligers <dael> TabAtkins: If you look at section 16 :matches and :has uses the brancht hat matches and :not uses the most specific regardless of matching <fantasai> s/That note is a liar/Also says it has the exact behavior of :not(:matches(argument)), which is a lie./ <dael> frremy: I have another proposal, we don't allow combintators inside :matches() <dael> fantasai: We had that for a while. original matches had everything. impl said too complex, we tooki t out, impl then said they want it. So I thinkw e have impl that handle complex selectors <dael> fantasai: The biggest use case is commas. <dael> frremy: Commas is the whole point of :match I said combinators <dael> TabAtkins: Combinators are the difficulty <dael> frremy: :match without combinators is easy. <fantasai> i/fantasai: The biggest use case/[some confusion about combinators vs commas]/ <dael> TabAtkins: Without combinators, jsut making it compound, doesn't simplify. Still have branches. Look at HTML on list bullets. It's a big list. If you do a simple :matches() rule you still h ave combinatorial branching. <dael> emilio: Removing combinators makes it simplier <TabAtkins> `:matches(a, #foo) :matches(a, #foo) :matches(a, #foo)` <= naively expands to 8 choices anyway <TabAtkins> `:matches(a, #foo, .bar) :matches(a, #foo, .bar) :matches(a, #foo, .bar)` <= naively expands to *27* choices anyway <dael> dbaron: Thing that's still hard is if you leave commas and you can have multiple matches and have you have backtrack to find the right one. As you walk up ancestors you might match the first on the element and a match for the second with ID but have to try ID ID path <dael> frremy: Oh, I see <dael> emilio: Making specificity a property of the selector is nice, i think <dael> TabAtkins: I see the difficulty and I'm happy to simplify it <dael> frremy: I think proposal i s in the right direction. Easier to impl i f only compute specificity of the selector as a selector. I can see why people would be confused, but I think it's simplier <dael> ericwilligers: Same for :not and make it most specific if it matches or not? <dael> frremy: Yes <dael> dbaron: I'd be more concerned if I thought specificity was more useful, but I thinik most people fight with it. <dael> astearns: I'm hearing at least 3 things <dbaron> s/concerned/concerned with this proposal/ <dael> astearns: 1) places where current spec lies. Need resolutions on those? <dael> TabAtkins: Won't be a lie once we resolve <dael> astearns: 2) Removing combinators in :matches() <dael> TabAtkins: I'd like to keep that separate and reject it. <TabAtkins> `:matches(.a .b .c, .d .e .f)` expands even faster, of course - expands to over a dozen combination, don't wanna compute the actual number right now because it's non-trivial <dael> astearns: 3) What we're doing for :matches() and :not(). Is there consensus? <dael> dbaron: Consesnus to make specificity is only for the selector and not the element <dbaron> s/only for/only a function of/ <dael> astearns: specificity on :not and :matches depends on selector and not any possible matching. <dael> TabAtkins: Should do for has as well <dbaron> s/for has/for :has()/ <dael> astearns: Do not consider matching when determining specificity of :not :matches and d:has <dael> ericwilligers: Doesn't know why has needs specificity <dael> TabAtkins: You can't right now, but in theory an impl could allow it. <TabAtkins> proposed resolution: :matches() and :has() should only consider their selector arguments (using most specific argument) rather than which branch matched, like :not() currently does. <dael> astearns: Having a non-testable assertion is annoing <dael> fantasai: Of course has needs specificity. <dael> TabAtkins: You can only use it i n JS so specifificty doesn't do anything <dael> fantasai: but if we ever use it in stylesheet <dael> TabAtkins: Current spec has an assertion, we should make it accurate. <TabAtkins> s/accurate/consistent/ <dael> astearns: Objections to making specificity of :not :has and :matches not depend on matching <dael> RESOLVED: Make specificity of :not() :has() and :matches() not depend on matching |
Don't forget about |
…) not depend on matching. (Use most specific argument, like :not().) #1027 (comment)
OK, committed these changes to the ED: Anyone here want to take a stab at reviewing it to make sure a) I got it right b) I fixed all the points that needed fixing c) It's sufficiently clear? :) |
Looks good. |
csswg-drafts/selectors-4/Overview.bs Line 1124 in 76eefbb
Typo: extra csswg-drafts/selectors-4/Overview.bs Line 3317 in 76eefbb
Typo: csswg-drafts/selectors-4/Overview.bs Line 3319 in 76eefbb
Suggestion: link "complex selector" to https://drafts.csswg.org/selectors-4/#complex csswg-drafts/selectors-4/Overview.bs Lines 3336 to 3337 in 76eefbb
csswg-drafts/selectors-4/Overview.bs Lines 3347 to 3348 in 76eefbb
csswg-drafts/selectors-4/Overview.bs Line 3352 in 76eefbb
I would also prefer if the specificity was defined with a properly clear algorithm. The current one can be ambiguous, e.g. in csswg-drafts/selectors-4/Overview.bs Line 3301 in 76eefbb
people may think ID selectors inside Something like this:
|
Doesn't the phrase "its most specific argument" (implying that this functional pseudo-class takes several arguments) technically contradict the phrase "a functional pseudo-class taking a selector list as its argument" from the definition (implying that the selector list as a whole is considered a single argument)? Maybe it would be better to reuse the phrase "the most specific complex selector in its selector list argument" in the definition of |
Oh dang, actually, I think I remember there was an earlier intent that we could use I'll open a different issue about this. |
@tabatkins, should |
OK, I'm closing out this issue. @Loirooriol I didn't remove the specific examples in favor of "any element" because I think they help point out the unintuitive result that while I am wondering now if we should have been choosing the least specific argument instead of the most specific one, though... |
The rules on specificity in selectors-4 say:
I just realized two things:
What is unclear, I think, is that the concept of specificity of a selector list requires both a selector list and an element being matched, since it uses the highest specificity form that matches the element. The algorithm doesn't make it clear that the element being matched is passed to the algorithm.
But that, in turn, pointed out to me that since
:matches()
can appear anywhere in a selector (between combinators), there might be multiple elements that could match a given matches. In order to not expose the order in which an implementation searches the subtree to find the set of elements that match the combinators, the specification also needs to require that the specificity of a complex selector be the highest-specificity way of matching that complex selector. This is because the new rule for:matches()
introduces the possibility that different ways of matching a complex selector (i.e., pairings between elements and the compound selectors in the complex selector) have different specificity.This, in turn, requires changes to how implementations match combinators, so that they search the tree for the highest-specificity way. I believe this may have substantive performance implications for at least some combinations of combinators, although I haven't actually worked the problem through yet. Somebody should!
This, in turn, makes me wonder whether we should reconsider whether the new specificity rule for
:matches()
is actually a good idea. (Another option is marking it at risk, but that has its own problems.)If we do keep it, I'd like to ensure we add tests that exercise the performance-sensitive cases.
The text was updated successfully, but these errors were encountered: