Skip to content

Commit 5c029e3

Browse files
authored
feat: add support for parsing the ::part() selectors (#1)
1 parent 945866e commit 5c029e3

File tree

3 files changed

+112
-1
lines changed

3 files changed

+112
-1
lines changed

README.md

+34
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,40 @@ transform(`.foo { font-size: 1vh; }`);
185185
}
186186
```
187187

188+
### CSS ::part() pseudo-elements
189+
190+
To enable parsing [::part() selectors](https://developer.mozilla.org/en-US/docs/Web/CSS/::part), specify the `parsePartSelectors` flag:
191+
192+
```js
193+
transform(
194+
`
195+
.container {
196+
background-color: #f00;
197+
}
198+
199+
.container::part(input) {
200+
background-color: #00f;
201+
}
202+
`,
203+
{
204+
parsePartSelectors: true,
205+
},
206+
);
207+
```
208+
209+
↓ ↓ ↓ ↓ ↓ ↓
210+
211+
```js
212+
{
213+
container: {
214+
backgroundColor: "#f00",
215+
},
216+
"container::part(input)": {
217+
backgroundColor: "#00f",
218+
},
219+
}
220+
```
221+
188222
## Limitations
189223

190224
- For `rem` unit the root element `font-size` is currently set to 16 pixels. A

src/index.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const lengthRe = /^(0$|(?:[+-]?(?:\d*\.)?\d+(?:[Ee][+-]?\d+)?)(?=px|rem$))/;
1616
const viewportUnitRe = /^([+-]?[0-9.]+)(vh|vw|vmin|vmax)$/;
1717
const percentRe = /^([+-]?(?:\d*\.)?\d+(?:[Ee][+-]?\d+)?%)$/;
1818
const unsupportedUnitRe = /^([+-]?(?:\d*\.)?\d+(?:[Ee][+-]?\d+)?(ch|em|ex|cm|mm|in|pc|pt))$/;
19+
const cssPartRe = /::?part\(([^)]+)\)/;
1920
const shorthandBorderProps = [
2021
"border-radius",
2122
"border-width",
@@ -100,7 +101,10 @@ const transform = (css, options) => {
100101

101102
if (
102103
rule.selectors[s].indexOf(".") !== 0 ||
103-
rule.selectors[s].indexOf(":") !== -1 ||
104+
(rule.selectors[s].indexOf(":") !== -1 &&
105+
(options != null && options.parsePartSelectors
106+
? !cssPartRe.test(rule.selectors[s])
107+
: true)) ||
104108
rule.selectors[s].indexOf("[") !== -1 ||
105109
rule.selectors[s].indexOf("~") !== -1 ||
106110
rule.selectors[s].indexOf(">") !== -1 ||

src/index.spec.js

+73
Original file line numberDiff line numberDiff line change
@@ -3487,3 +3487,76 @@ describe("ICSS :export pseudo-selector", () => {
34873487
).toThrow();
34883488
});
34893489
});
3490+
3491+
describe("::part() selectors", () => {
3492+
it("transforms ::part() selectors", () => {
3493+
expect(
3494+
transform(
3495+
`
3496+
.container {
3497+
background-color: #f00;
3498+
}
3499+
3500+
.container::part(input) {
3501+
background-color: #00f;
3502+
}
3503+
`,
3504+
{
3505+
parsePartSelectors: true,
3506+
},
3507+
),
3508+
).toEqual({
3509+
container: {
3510+
backgroundColor: "#f00",
3511+
},
3512+
"container::part(input)": {
3513+
backgroundColor: "#00f",
3514+
},
3515+
});
3516+
});
3517+
3518+
it("does not transform ::part() selectors without option enabled", () => {
3519+
expect(
3520+
transform(
3521+
`
3522+
.container {
3523+
background-color: #f00;
3524+
}
3525+
3526+
.container::part(input) {
3527+
background-color: #00f;
3528+
}
3529+
`,
3530+
{},
3531+
),
3532+
).toEqual({
3533+
container: {
3534+
backgroundColor: "#f00",
3535+
},
3536+
});
3537+
3538+
expect(
3539+
transform(
3540+
`
3541+
.container {
3542+
background-color: #f00;
3543+
}
3544+
3545+
.container::part(input) {
3546+
background-color: #00f;
3547+
}
3548+
`,
3549+
{
3550+
parsePartSelectors: true,
3551+
},
3552+
),
3553+
).toEqual({
3554+
container: {
3555+
backgroundColor: "#f00",
3556+
},
3557+
"container::part(input)": {
3558+
backgroundColor: "#00f",
3559+
},
3560+
});
3561+
});
3562+
});

0 commit comments

Comments
 (0)