Open dshin-moz opened 2 months ago
On the other hand, a parallel could potentially be drawn to:
<!doctype html>
<style>
:root .foo {
color: red;
}
.foo {
color: lime;
}
</style>
<div class="foo"></div>
Where .foo
is red.
Yeah this is also important because depending on what behavior we decide, we might need to revert the resolution in https://github.com/w3c/csswg-drafts/issues/9621 (or change it so that we serialize to :where(:scope)
, but that's a bit weird...
I think it's very weird that:
> .foo
Has different specificity than:
:scope > .foo
in this case...
cc @mirisuzanne @tabatkins
Right, right.
Well, it looks like the current behavior is intentional at least, as @mirisuzanne and I apparently briefly discussed this June 19th, 2023 (chat history). We both concluded (from the discussion in https://github.com/w3c/csswg-drafts/issues/8500) that any implicit :scope
should not contribute to specificity.
I also noted at the time:
"That means
@scope (.a) { .b { ... } }
and@scope (.a) { :scope .b { ... } }
are different rules, and we can not serialize one as the other."
So yeah, oopsie. :-)
With both Chrome and Webkit having shipped now, a change here could be too late already. I'll try to investigate that if we indeed want to change.
It was intentional, and designed to match e.g. :root
. The goal is for the scope rule itself not to impact specificity, while both :scope
and &
selectors can reference it in different ways, and bring their own specificity implications to bear. I don't like either of the alternatives:
:scope
has been around for some time, and it makes sense for it to have a pseudo-class specificity (since that's what it is):scope
has a specificity, but adding/removing :scope
from the selector has no specificity impact.Should :scope
inserted by &
+ Implicit scope root add to specificity? Blink & WebKit diverge on it:
<!DOCTYPE html>
<div>
<style>
@scope {
& .foo {
color: blue;
}
}
:scope .foo {
color: red;
}
</style>
<div class="foo">What Color?</div>
</div>
Shows red on Chrome dev 125.0.6382.3 and blue on Safari Technology Preview 17.4.
I would expect the &
to have its regular "as if :is()"-specificity behavior, which in this case means the specificity of :is(:scope)
.
..... which means Chrome doesn't do what I expect.
I would expect the
&
to have its regular "as if :is()"-specificity behavior…
That's what we intended, yes. See https://www.w3.org/TR/css-cascade-6/#example-66991251 for example.
@astearns this one was also unexpectedly closed I believe.
The CSS Working Group just discussed [css-cascade-6] Specificity of Implicitly-Added `:scope` in Scoped Rules
, and agreed to the following:
RESOLVED: explicitly state in specs that when an ampersand doesn't actually have a parent rule to draw from then its specificity is zero
So as @emilio said before, we might need to revert "always serialize implicit :scope
"
https://github.com/w3c/csswg-drafts/issues/9621 then ?
@mdubet I added agenda+ to #9621
Ah, yes of course.
Current WPT test for scoped styles' specificity seems to assume that an unscoped rule has the same specificity as the identically written out scoped rule. However, the spec's example seems to imply that
:scope <descendant-combinator>
is implicitly added, and that:scope
's specificity is equal to that of other pseudo-classes (i.e. class-like +1).WebKit and Blink are shipping the behaviour matching the WPT.
This leads to behaviours where functionally-identical selectors with explicitly-added
:scope
selector wins despite the ordering. In the example below, WebKit and Chrome both show.foo
being purple:cc: @emilio, @mdubet, @andruud