Skip to content

Commit

Permalink
Escape selectors containing forward slashes (#816)
Browse files Browse the repository at this point in the history
This PR adds support for forward slashes within certain classes:

```js
tw`group-hover/link:bg-black`

// ↓ ↓ ↓ ↓ ↓ ↓

({
    ".group\\/link:hover &": { // < Added escaping to class name
      "--tw-bg-opacity": "1",
      "backgroundColor": "rgb(0 0 0 / var(--tw-bg-opacity))"
    }
});
```

Closes #808
  • Loading branch information
ben-rogerson committed Jul 23, 2023
1 parent 8668bf2 commit 45ff6ad
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 22 deletions.
3 changes: 3 additions & 0 deletions src/core/lib/util/sassifySelector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ const sassifySelectorTasks: SassifySelectorTasks = [
// Fix the spotty `:root` support in emotion/styled-components
(selector): string => selector.replace(SELECTOR_ROOT, '*:root'),

// Escape selectors containing forward slashes, eg: group-hover/link:bg-black
(selector): string => selector.replace(/\//g, '\\/'),

(selector): string => selector.trim(),
]

Expand Down
44 changes: 22 additions & 22 deletions tests/arbitraryVariants.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -335,12 +335,12 @@ it('should support aria variants', async () => {
}
});
({
'.group/foo[aria-checked="true"] &': {
'.group\\\\/foo[aria-checked="true"] &': {
"textDecorationLine": "underline"
}
});
({
'.peer/foo[aria-checked="true"] ~ &': {
'.peer\\\\/foo[aria-checked="true"] ~ &': {
"textDecorationLine": "underline"
}
});
Expand All @@ -365,12 +365,12 @@ it('should support aria variants', async () => {
}
});
({
".group/foo[aria-sort=ascending] &": {
".group\\\\/foo[aria-sort=ascending] &": {
"textDecorationLine": "underline"
}
});
({
".peer/foo[aria-sort=ascending] ~ &": {
".peer\\\\/foo[aria-sort=ascending] ~ &": {
"textDecorationLine": "underline"
}
});
Expand Down Expand Up @@ -425,12 +425,12 @@ it('should support data variants', async () => {
}
});
({
'.group/foo[data-ui~="checked"] &': {
'.group\\\\/foo[data-ui~="checked"] &': {
"textDecorationLine": "underline"
}
});
({
'.peer/foo[data-ui~="checked"] ~ &': {
'.peer\\\\/foo[data-ui~="checked"] ~ &': {
"textDecorationLine": "underline"
}
});
Expand All @@ -455,12 +455,12 @@ it('should support data variants', async () => {
}
});
({
".group/foo[data-position=top] &": {
".group\\\\/foo[data-position=top] &": {
"textDecorationLine": "underline"
}
});
({
".peer/foo[data-position=top] ~ &": {
".peer\\\\/foo[data-position=top] ~ &": {
"textDecorationLine": "underline"
}
});
Expand Down Expand Up @@ -602,37 +602,37 @@ it('should be possible to use modifiers and arbitrary groups', async () => {
}
});
({
".group/foo:hover &": {
".group\\\\/foo:hover &": {
"textDecorationLine": "underline"
}
});
({
".group/foo:focus &": {
".group\\\\/foo:focus &": {
"textDecorationLine": "underline"
}
});
({
".group/foo:hover &": {
".group\\\\/foo:hover &": {
"textDecorationLine": "underline"
}
});
({
".group/foo[data-open] &": {
".group\\\\/foo[data-open] &": {
"textDecorationLine": "underline"
}
});
({
".group/foo[data-open] &": {
".group\\\\/foo[data-open] &": {
"textDecorationLine": "underline"
}
});
({
".in-foo .group/foo &": {
".in-foo .group\\\\/foo &": {
"textDecorationLine": "underline"
}
});
({
".group/foo.in-foo &": {
".group\\\\/foo.in-foo &": {
"textDecorationLine": "underline"
}
});
Expand Down Expand Up @@ -707,37 +707,37 @@ it('should be possible to use modifiers and arbitrary peers', async () => {
}
});
({
".peer/foo:hover ~ &": {
".peer\\\\/foo:hover ~ &": {
"textDecorationLine": "underline"
}
});
({
".peer/foo:focus ~ &": {
".peer\\\\/foo:focus ~ &": {
"textDecorationLine": "underline"
}
});
({
".peer/foo:hover ~ &": {
".peer\\\\/foo:hover ~ &": {
"textDecorationLine": "underline"
}
});
({
".peer/foo[data-open] ~ &": {
".peer\\\\/foo[data-open] ~ &": {
"textDecorationLine": "underline"
}
});
({
".peer/foo[data-open] ~ &": {
".peer\\\\/foo[data-open] ~ &": {
"textDecorationLine": "underline"
}
});
({
".in-foo .peer/foo ~ &": {
".in-foo .peer\\\\/foo ~ &": {
"textDecorationLine": "underline"
}
});
({
".peer/foo.in-foo ~ &": {
".peer\\\\/foo.in-foo ~ &": {
"textDecorationLine": "underline"
}
});
Expand Down

0 comments on commit 45ff6ad

Please sign in to comment.