Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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/sour-feet-carry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': minor
---

feat: migrate css `:id`, `:where``:not` and `:has`
10 changes: 9 additions & 1 deletion packages/svelte/src/compiler/migrate/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,15 @@ export function migrate(source, { filename } = {}) {
if (!parsed.instance && need_script) {
str.appendRight(insertion_point, '\n</script>\n\n');
}
return { code: str.toString() };
return {
code: str
.toString()
// for some reason replacing the magic string doesn't work
.replaceAll(
/(?<=<style[^>]*>[\s\S]*?:(?:is|not|where|has)\()([^)]+)(?=[\s\S]*?<\/style>)/gm,
':global($1)'
)
};
} catch (e) {
if (!(e instanceof MigrationError)) {
// eslint-disable-next-line no-console
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<script>
function is(){}
function where(){}
function not(){}
function has(){}
// looks like css but it's not in style tag
const x = {
div:is(42),
span:where(42),
form:not(42),
input:has(42),
}
</script>

what if i'm talking about `:has()` in my blog?

```css
:has(.is_cool)
```

<style lang="postcss">
div:has(span){}
div > :not(span){}
div > :is(span){}
div > :where(span){}
div:has(:is(span)){}
div > :not(:is(span)){}
div > :is(:is(span)){}
div > :where(:is(span)){}
Copy link
Member

Choose a reason for hiding this comment

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

just for safety: can we add more tests around nested braces? Like :has(.x:not(...))

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...if you can think of any other case please let me know

Copy link
Member

Choose a reason for hiding this comment

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

div :is(.class:is(span:is(:hover)), .x){} -> the .x isn't properly wrapped

Copy link
Member Author

Choose a reason for hiding this comment

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

Oh I know why...let me fix

div{
p:has(&){
}
:not(span > *){
:where(form){}
}
}
</style>
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<script>
function is(){}
function where(){}
function not(){}
function has(){}

// looks like css but it's not in style tag
const x = {
div:is(42),
span:where(42),
form:not(42),
input:has(42),
}
</script>

what if i'm talking about `:has()` in my blog?

```css
:has(.is_cool)
```

<style lang="postcss">
div:has(:global(span)){}
div > :not(:global(span)){}
div > :is(:global(span)){}
div > :where(:global(span)){}

div:has(:global(:is(span))){}
div > :not(:global(:is(span))){}
div > :is(:global(:is(span))){}
div > :where(:global(:is(span))){}

div{
p:has(:global(&)){

}
:not(:global(span > *)){
:where(:global(form)){}
}
}
</style>