Skip to content

Commit 8afd538

Browse files
authored
postcss-is-pseudo : fix compound selectors with universal selectors (#959)
1 parent 1621941 commit 8afd538

13 files changed

+69
-6
lines changed

plugins/postcss-is-pseudo-class/CHANGELOG.md

+13
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
11
# Changes to PostCSS Is Pseudo Class
22

3+
### Unreleased (patch)
4+
5+
- Fix compound selectors with `*`.
6+
7+
```diff
8+
:is(.a *):is(h1, h2, h3) {}
9+
10+
/* becomes : */
11+
12+
- .a *h1, .a *h2, .a *h3 {}
13+
+ .a h1, .a h2, .a h3 {}
14+
```
15+
316
### 3.2.0 (April 10, 2023)
417

518
- Add support for more complex selector patterns. In particular anything where `:is()` is in the left-most compound selector.

plugins/postcss-is-pseudo-class/dist/index.cjs

+1-1
Large diffs are not rendered by default.

plugins/postcss-is-pseudo-class/dist/index.mjs

+1-1
Large diffs are not rendered by default.

plugins/postcss-is-pseudo-class/src/split-selectors/compound-selector-order.ts

+15-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export function sortCompoundSelectorsInsideComplexSelector(node) {
55
return;
66
}
77

8-
const compoundSelectors = [];
8+
const compoundSelectors: Array<Array<parser.Node>> = [];
99
let currentCompoundSelector = [];
1010
for (let i = 0; i < node.nodes.length; i++) {
1111
if (node.nodes[i].type === 'combinator') {
@@ -39,6 +39,7 @@ export function sortCompoundSelectorsInsideComplexSelector(node) {
3939
const sortedCompoundSelectors = [];
4040
for (let i = 0; i < compoundSelectors.length; i++) {
4141
const compoundSelector = compoundSelectors[i];
42+
4243
compoundSelector.sort((a, b) => {
4344
if (a.type === 'selector' && b.type === 'selector' && a.nodes.length && b.nodes.length) {
4445
return selectorTypeOrder(a.nodes[0], a.nodes[0].type) - selectorTypeOrder(b.nodes[0], b.nodes[0].type);
@@ -55,7 +56,20 @@ export function sortCompoundSelectorsInsideComplexSelector(node) {
5556
return selectorTypeOrder(a, a.type) - selectorTypeOrder(b, b.type);
5657
});
5758

59+
const compoundSelectorChildTypes = new Set(compoundSelector.map(x => x.type));
60+
const skipUniversals = compoundSelectorChildTypes.has('universal') && (
61+
compoundSelectorChildTypes.has('tag') ||
62+
compoundSelectorChildTypes.has('attribute') ||
63+
compoundSelectorChildTypes.has('class') ||
64+
compoundSelectorChildTypes.has('id') ||
65+
compoundSelectorChildTypes.has('pseudo'));
66+
5867
for (let j = 0; j < compoundSelector.length; j++) {
68+
if (compoundSelector[j].type === 'universal' && skipUniversals) {
69+
compoundSelector[j].remove();
70+
continue;
71+
}
72+
5973
sortedCompoundSelectors.push(compoundSelector[j]);
6074
}
6175
}

plugins/postcss-is-pseudo-class/test/basic.css

+4
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,10 @@ foo[baz=":is(.some, .other)"], .ok {
146146
order: 37;
147147
}
148148

149+
:is(.a, .a *):is(h1, h2, h3, h4, h5, h6) {
150+
order: 38;
151+
}
152+
149153
:is(input, button):is(:hover, :focus) {
150154
to-clone: 1;
151155
}

plugins/postcss-is-pseudo-class/test/basic.does-not-exist.expect.css

+4
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,10 @@ button:is(textarea) {
254254
order: 37;
255255
}
256256

257+
h1.a, h2.a, h3.a, h4.a, h5.a, h6.a, .a h1, .a h2, .a h3, .a h4, .a h5, .a h6 {
258+
order: 38;
259+
}
260+
257261
input:hover, input:focus, button:hover, button:focus {
258262
to-clone: 1;
259263
}

plugins/postcss-is-pseudo-class/test/basic.expect.css

+4
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,10 @@ button:is(textarea) {
254254
order: 37;
255255
}
256256

257+
h1.a, h2.a, h3.a, h4.a, h5.a, h6.a, .a h1, .a h2, .a h3, .a h4, .a h5, .a h6 {
258+
order: 38;
259+
}
260+
257261
input:hover, input:focus, button:hover, button:focus {
258262
to-clone: 1;
259263
}

plugins/postcss-is-pseudo-class/test/basic.oncomplex.no-warning.expect.css

+4
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,10 @@ button:is(textarea) {
254254
order: 37;
255255
}
256256

257+
h1.a, h2.a, h3.a, h4.a, h5.a, h6.a, .a h1, .a h2, .a h3, .a h4, .a h5, .a h6 {
258+
order: 38;
259+
}
260+
257261
input:hover, input:focus, button:hover, button:focus {
258262
to-clone: 1;
259263
}

plugins/postcss-is-pseudo-class/test/basic.oncomplex.warning.expect.css

+4
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,10 @@ button:is(textarea) {
254254
order: 37;
255255
}
256256

257+
h1.a, h2.a, h3.a, h4.a, h5.a, h6.a, .a h1, .a h2, .a h3, .a h4, .a h5, .a h6 {
258+
order: 38;
259+
}
260+
257261
input:hover, input:focus, button:hover, button:focus {
258262
to-clone: 1;
259263
}

plugins/postcss-is-pseudo-class/test/basic.preserve.expect.css

+8
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,14 @@ button:is(textarea) {
386386
order: 37;
387387
}
388388

389+
h1.a, h2.a, h3.a, h4.a, h5.a, h6.a, .a h1, .a h2, .a h3, .a h4, .a h5, .a h6 {
390+
order: 38;
391+
}
392+
393+
:is(.a, .a *):is(h1, h2, h3, h4, h5, h6) {
394+
order: 38;
395+
}
396+
389397
input:hover, input:focus, button:hover, button:focus {
390398
to-clone: 1;
391399
}

plugins/postcss-is-pseudo-class/test/basic.with-cloned-declarations.expect.css

+8
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,14 @@ button:is(textarea) {
386386
order: 37;
387387
}
388388

389+
h1.a, h2.a, h3.a, h4.a, h5.a, h6.a, .a h1, .a h2, .a h3, .a h4, .a h5, .a h6 {
390+
order: 38;
391+
}
392+
393+
:is(.a, .a *):is(h1, h2, h3, h4, h5, h6) {
394+
order: 38;
395+
}
396+
389397
input:hover, input:focus, button:hover, button:focus {
390398
cloned: 1;
391399
to-clone: 1;

plugins/postcss-is-pseudo-class/test/browser.expect.css

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
font-size: 20px;
1616
}
1717

18-
#main_a *.e.e:not(.does-not-exist):not(.does-not-exist)+.d, #main_a .e.e.g:not(.does-not-exist)+.d {
18+
#main_a .e.e:not(.does-not-exist):not(.does-not-exist)+.d, #main_a .e.e.g:not(.does-not-exist)+.d {
1919
color: red;
2020
font-size: 20px;
2121
}

plugins/postcss-is-pseudo-class/test/generated-selector-class-function-cases.expect.css

+2-2
Original file line numberDiff line numberDiff line change
@@ -1990,15 +1990,15 @@ __foo {
19901990
order: 239;
19911991
}
19921992

1993-
*.some, *.other {
1993+
.some, .other {
19941994
order: 240;
19951995
}
19961996

19971997
*:is(.some, .other) {
19981998
order: 240;
19991999
}
20002000

2001-
*.some, *.other {
2001+
.some, .other {
20022002
order: 241;
20032003
}
20042004

0 commit comments

Comments
 (0)