Bug 1882581: Allow parsing scope-end selector as relative, anchoring at :scope. r=firefox-style-system-reviewers,emilio

WPT is adjusted to reflect the resolution of Issue #9621
(https://github.com/w3c/csswg-drafts/issues/9621).
That is, relative selectors are serialized with `:scope` e.g.
`> .foo` becomes `:scope > .foo`.

Differential Revision: https://phabricator.services.mozilla.com/D203154
This commit is contained in:
David Shin 2024-03-19 13:36:48 +00:00
parent 1e31a04af4
commit 9814aed542
3 changed files with 14 additions and 7 deletions

View file

@ -438,6 +438,9 @@ pub enum ParseRelative {
/// Allow selectors to start with a combinator, prepending a parent selector if so. Do nothing
/// otherwise
ForNesting,
/// Allow selectors to start with a combinator, prepending a scope selector if so. Do nothing
/// otherwise
ForScope,
/// Treat as parse error if any selector begins with a combinator.
No,
}
@ -2619,9 +2622,14 @@ where
// combinator.
builder.push_combinator(combinator.unwrap_or(Combinator::Descendant));
},
ParseRelative::ForNesting => {
ParseRelative::ForNesting | ParseRelative::ForScope => {
if let Ok(combinator) = combinator {
builder.push_simple_selector(Component::ParentSelector);
let selector = match parse_relative {
ParseRelative::ForHas | ParseRelative::No => unreachable!(),
ParseRelative::ForNesting => Component::ParentSelector,
ParseRelative::ForScope => Component::Scope,
};
builder.push_simple_selector(selector);
builder.push_combinator(combinator);
}
},

View file

@ -119,8 +119,7 @@ fn parse_scope<'a>(
for_supports_rule: false,
};
let parse_relative = if for_end {
// TODO(dshin): scope-end can be a relative selector, with the anchor being `:scope`.
ParseRelative::No
ParseRelative::ForScope
} else if in_style_rule {
ParseRelative::ForNesting
} else {

View file

@ -42,9 +42,9 @@
test_valid('@scope to (.a)');
test_valid('@scope (.a) to (&)');
test_valid('@scope (.a) to (& > &)');
test_valid('@scope (.a) to (> .b)');
test_valid('@scope (.a) to (+ .b)');
test_valid('@scope (.a) to (~ .b)');
test_valid('@scope (.a) to (> .b)', '@scope (.a) to (:scope > .b)');
test_valid('@scope (.a) to (+ .b)', '@scope (.a) to (:scope + .b)');
test_valid('@scope (.a) to (~ .b)', '@scope (.a) to (:scope ~ .b)');
test_valid('@scope ()', '@scope');
test_valid('@scope to ()', '@scope');
test_valid('@scope () to ()', '@scope');