Skip to content

fix: refine css :global() selector checks in a compound selector #11142

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

Merged
merged 1 commit into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/dry-fans-march.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"svelte": patch
---

fix: refine css `:global()` selector checks in a compound selector
2 changes: 2 additions & 0 deletions packages/svelte/src/compiler/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ const css = {
'invalid-css-global-selector': () => `:global(...) must contain exactly one selector`,
'invalid-css-global-selector-list': () =>
`:global(...) must not contain type or universal selectors when used in a compound selector`,
'invalid-css-type-selector-placement': () =>
`:global(...) must not be followed with a type selector`,
'invalid-css-selector': () => `Invalid selector`,
'invalid-css-identifier': () => 'Expected a valid CSS identifier',
'invalid-nesting-selector': () => `Nesting selectors can only be used inside a rule`,
Expand Down
44 changes: 17 additions & 27 deletions packages/svelte/src/compiler/phases/2-analyze/css/css-analyze.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,41 +99,31 @@ const validation_visitors = {
}
}

// ensure `:global(...)`contains a single selector
// (standalone :global() with multiple selectors is OK)
if (node.children.length > 1 || node.children[0].selectors.length > 1) {
for (const relative_selector of node.children) {
for (const selector of relative_selector.selectors) {
if (
selector.type === 'PseudoClassSelector' &&
selector.name === 'global' &&
selector.args !== null &&
selector.args.children.length > 1
) {
error(selector, 'invalid-css-global-selector');
}
}
}
}

// ensure `:global(...)` is not part of a larger compound selector
// ensure `:global(...)` do not lead to invalid css after `:global()` is removed
for (const relative_selector of node.children) {
for (let i = 0; i < relative_selector.selectors.length; i++) {
const selector = relative_selector.selectors[i];

if (selector.type === 'PseudoClassSelector' && selector.name === 'global') {
const child = selector.args?.children[0].children[0];
// ensure `:global(element)` to be at the first position in a compound selector
if (child?.selectors[0].type === 'TypeSelector' && i !== 0) {
error(selector, 'invalid-css-global-selector-list');
}

// ensure `:global(.class)` is not followed by a type selector, eg: `:global(.class)element`
if (relative_selector.selectors[i + 1]?.type === 'TypeSelector') {
error(relative_selector.selectors[i + 1], 'invalid-css-type-selector-placement');
}

// ensure `:global(...)`contains a single selector
// (standalone :global() with multiple selectors is OK)
if (
child?.selectors[0].type === 'TypeSelector' &&
!/[.:#]/.test(child.selectors[0].name[0]) &&
(i !== 0 ||
relative_selector.selectors
.slice(1)
.some(
(s) => s.type !== 'PseudoElementSelector' && s.type !== 'PseudoClassSelector'
))
selector.args !== null &&
selector.args.children.length > 1 &&
(node.children.length > 1 || relative_selector.selectors.length > 1)
) {
error(selector, 'invalid-css-global-selector-list');
error(selector, 'invalid-css-global-selector');
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ const visitors = {

context.state.specificity.bumped = true;

// TODO err... can this happen?
// for any :global() at the middle of compound selector
for (const selector of relative_selector.selectors) {
if (selector.type === 'PseudoClassSelector' && selector.name === 'global') {
remove_global_pseudo_class(selector);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[
{
"code": "invalid-css-global-selector-list",
"message": ":global(...) must not contain type or universal selectors when used in a compound selector",
"start": {
"line": 20,
"column": 6
},
"end": {
"line": 20,
"column": 17
}
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<style>
::foo:global([data-state='checked']) {
color: red;
}
::foo:global(.foo) {
color: red;
}
::foo:global(#foo) {
color: red;
}
::foo:global(::foo) {
color: red;
}
::foo:global(:foo) {
color: red;
}
:global(h1) {
color: red;
}
::foo:global(h1) {
color: red;
}
</style>

<div>
<h1>hello world</h1>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[
{
"code": "invalid-css-type-selector-placement",
"message": ":global(...) must not be followed with a type selector",
"start": {
"line": 17,
"column": 14
},
"end": {
"line": 17,
"column": 16
}
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<style>
:global(.foo):foo {
color: red;
}
:global(.foo)::foo {
color: red;
}
:global(.foo).bar {
color: red;
}
:global(.foo)#baz {
color: red;
}
:global(.foo)[id] {
color: red;
}
:global(.foo)h1 {
color: red;
}
</style>

<div>
<h1 class="bar" id="baz">hello world</h1>
</div>