Skip to content

fix: allow global next to & for nesting #11784

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 3 commits into from
May 27, 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/selfish-panthers-add.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"svelte": patch
---

fix: allow global next to `&` for nesting
14 changes: 7 additions & 7 deletions packages/svelte/src/compiler/phases/3-transform/css/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,13 @@ const visitors = {
}
}

// 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);
}
}

if (relative_selector.selectors.some((s) => s.type === 'NestingSelector')) {
continue;
}
Expand All @@ -250,13 +257,6 @@ const visitors = {

context.state.specificity.bumped = true;

// 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);
}
}

let i = relative_selector.selectors.length;
while (i--) {
const selector = relative_selector.selectors[i];
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { test } from '../../test';

export default test({
warnings: []
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
div.svelte-xyz {
&.class{
color: red;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<div class="svelte-xyz">
</div>

<div class="class svelte-xyz">
foo
</div>
14 changes: 14 additions & 0 deletions packages/svelte/tests/css/samples/global-with-nesting/input.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<style>
div {
&:global(.class){
color: red;
}
}
</style>

<div>
</div>
Copy link
Member

@dummdidumm dummdidumm May 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we have another test case (within this test, doesn't need to be another one) for <div><p class="class">foo</p></div> to check that class isn't scoped in the HTML?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure...but actually it needs to be a bit different i think because this cover the case for

div{
    &.class{
    }
}

which is where the class is attached to the div.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added now!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, doh - thanks!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It tripped me too when i was testing if my change broke something else 😄


<div class="class">
foo
</div>
Loading