Skip to content

fix: allow & to appear at the top when inside a :global(...) #13215

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 2 commits into from
Sep 13, 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
2 changes: 1 addition & 1 deletion packages/svelte/messages/compile-errors/style.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@

## css_nesting_selector_invalid_placement

> Nesting selectors can only be used inside a rule
> Nesting selectors can only be used inside a rule or as the first selector inside a lone `:global(...)`

## css_selector_invalid

Expand Down
4 changes: 2 additions & 2 deletions packages/svelte/src/compiler/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -536,12 +536,12 @@ export function css_global_invalid_selector_list(node) {
}

/**
* Nesting selectors can only be used inside a rule
* Nesting selectors can only be used inside a rule or as the first selector inside a lone `:global(...)`
* @param {null | number | NodeLike} node
* @returns {never}
*/
export function css_nesting_selector_invalid_placement(node) {
e(node, "css_nesting_selector_invalid_placement", "Nesting selectors can only be used inside a rule");
e(node, "css_nesting_selector_invalid_placement", "Nesting selectors can only be used inside a rule or as the first selector inside a lone `:global(...)`");
}

/**
Expand Down
13 changes: 12 additions & 1 deletion packages/svelte/src/compiler/phases/2-analyze/css/css-analyze.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,18 @@ const css_visitors = {
const parent_rule = rule.metadata.parent_rule;

if (!parent_rule) {
e.css_nesting_selector_invalid_placement(node);
// https://developer.mozilla.org/en-US/docs/Web/CSS/Nesting_selector#using_outside_nested_rule
const children = rule.prelude.children;
const selectors = children[0].children[0].selectors;
if (
children.length > 1 ||
selectors.length > 1 ||
selectors[0].type !== 'PseudoClassSelector' ||
selectors[0].name !== 'global' ||
selectors[0].args?.children[0]?.children[0].selectors[0] !== node
) {
e.css_nesting_selector_invalid_placement(node);
}
} else if (
// :global { &.foo { ... } } is invalid
parent_rule.metadata.is_global_block &&
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { test } from '../../test';

export default test({
error: {
code: 'css_nesting_selector_invalid_placement',
message:
'Nesting selectors can only be used inside a rule or as the first selector inside a lone `:global(...)`',
position: [151, 152]
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<style>
/* valid */
:global(&) {
color: blue;
}
:global(& div) {
color: blue;
}
:global(&) div {
color: blue;
}
/* error */
:global(div &) {
color: blue;
}
</style>