Skip to content

Commit c07af0a

Browse files
authored
fix selector order in pseudo :is (#278)
* fix selector order in pseudo is * fix
1 parent b7bc5f6 commit c07af0a

File tree

14 files changed

+189
-36
lines changed

14 files changed

+189
-36
lines changed

plugins/postcss-is-pseudo-class/.tape.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ postcssTape(plugin)({
1919
},
2020
'basic:oncomplex:warning': {
2121
message: "supports basic usage with { onComplexSelector: 'warning' }",
22-
warnings: 11,
22+
warnings: 10,
2323
options: {
2424
onComplexSelector: 'warning'
2525
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changes to PostCSS Is Pseudo Class
22

3+
### Unreleased (patch)
4+
5+
- Preserve selector order as much as possible. Fixes issues where pseudo elements `::before` were moved.
6+
37
### 2.0.0 (January 31, 2022)
48

59
- Remove `skip` flag in `onComplexSelectors` option.

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

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,7 @@ export function sortCompoundSelector(node) {
3939
if (!node || !node.nodes) {
4040
return;
4141
}
42-
// compound selectors with nesting can be written with tag selectors as later parts.
43-
// for example : `&h1`
44-
//
45-
// simply concating with parent selectors can lead to :
42+
// simply concatenating with selectors can lead to :
4643
// `.fooh1`
4744
//
4845
// applying a sort where tag selectors are first will result in :
@@ -92,14 +89,6 @@ export function sortCompoundSelector(node) {
9289
}
9390

9491
if (a.type === b.type) {
95-
if (a.value < b.value) {
96-
return -1;
97-
}
98-
99-
if (a.value > b.value) {
100-
return 1;
101-
}
102-
10392
return 0;
10493
}
10594

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,3 +93,11 @@ foo[baz=":is(.some, .other)"], .ok {
9393
.invalid-is:is {
9494
order: 24;
9595
}
96+
97+
:is(a, button):is(:hover::before, :focus) {
98+
order: 25;
99+
}
100+
101+
:is(a, button):is(:hover, :focus)::before {
102+
order: 26;
103+
}

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

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ foo[baz=":is(.some, .other)"], .ok {
214214
order: 14;
215215
}
216216

217-
:focus:is(.alpha ~ .delta) > .beta + .beta {
217+
:is(.alpha ~ .delta):focus > .beta + .beta {
218218
order: 15;
219219
}
220220

@@ -266,7 +266,7 @@ foo[baz=":is(.some, .other)"], .ok {
266266
order: 19;
267267
}
268268

269-
.alpha.pre:is(.one > .two) {
269+
.pre.alpha:is(.one > .two) {
270270
order: 20;
271271
}
272272

@@ -293,3 +293,35 @@ a:not(.something-random) {
293293
.invalid-is:is {
294294
order: 24;
295295
}
296+
297+
a:hover::before {
298+
order: 25;
299+
}
300+
301+
a:focus:not(something-random) {
302+
order: 25;
303+
}
304+
305+
button:hover::before {
306+
order: 25;
307+
}
308+
309+
button:focus:not(something-random) {
310+
order: 25;
311+
}
312+
313+
a:hover::before {
314+
order: 26;
315+
}
316+
317+
a:focus::before {
318+
order: 26;
319+
}
320+
321+
button:hover::before {
322+
order: 26;
323+
}
324+
325+
button:focus::before {
326+
order: 26;
327+
}

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

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ foo[baz=":is(.some, .other)"], .ok {
214214
order: 14;
215215
}
216216

217-
:focus:is(.alpha ~ .delta) > .beta + .beta {
217+
:is(.alpha ~ .delta):focus > .beta + .beta {
218218
order: 15;
219219
}
220220

@@ -266,7 +266,7 @@ foo[baz=":is(.some, .other)"], .ok {
266266
order: 19;
267267
}
268268

269-
.alpha.pre:is(.one > .two) {
269+
.pre.alpha:is(.one > .two) {
270270
order: 20;
271271
}
272272

@@ -293,3 +293,35 @@ a:not(.does-not-exist) {
293293
.invalid-is:is {
294294
order: 24;
295295
}
296+
297+
a:hover::before {
298+
order: 25;
299+
}
300+
301+
a:focus:not(does-not-exist) {
302+
order: 25;
303+
}
304+
305+
button:hover::before {
306+
order: 25;
307+
}
308+
309+
button:focus:not(does-not-exist) {
310+
order: 25;
311+
}
312+
313+
a:hover::before {
314+
order: 26;
315+
}
316+
317+
a:focus::before {
318+
order: 26;
319+
}
320+
321+
button:hover::before {
322+
order: 26;
323+
}
324+
325+
button:focus::before {
326+
order: 26;
327+
}

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

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ foo[baz=":is(.some, .other)"], .ok {
214214
order: 14;
215215
}
216216

217-
:focus:is(.alpha ~ .delta) > .beta + .beta {
217+
:is(.alpha ~ .delta):focus > .beta + .beta {
218218
order: 15;
219219
}
220220

@@ -266,7 +266,7 @@ foo[baz=":is(.some, .other)"], .ok {
266266
order: 19;
267267
}
268268

269-
.alpha.pre:is(.one > .two) {
269+
.pre.alpha:is(.one > .two) {
270270
order: 20;
271271
}
272272

@@ -293,3 +293,35 @@ a:not(.does-not-exist) {
293293
.invalid-is:is {
294294
order: 24;
295295
}
296+
297+
a:hover::before {
298+
order: 25;
299+
}
300+
301+
a:focus:not(does-not-exist) {
302+
order: 25;
303+
}
304+
305+
button:hover::before {
306+
order: 25;
307+
}
308+
309+
button:focus:not(does-not-exist) {
310+
order: 25;
311+
}
312+
313+
a:hover::before {
314+
order: 26;
315+
}
316+
317+
a:focus::before {
318+
order: 26;
319+
}
320+
321+
button:hover::before {
322+
order: 26;
323+
}
324+
325+
button:focus::before {
326+
order: 26;
327+
}

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

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ foo[baz=":is(.some, .other)"], .ok {
254254
order: 14;
255255
}
256256

257-
:focus:is(.alpha ~ .delta) > .beta + .beta {
257+
:is(.alpha ~ .delta):focus > .beta + .beta {
258258
order: 15;
259259
}
260260

@@ -322,10 +322,6 @@ foo[baz=":is(.some, .other)"], .ok {
322322
order: 19;
323323
}
324324

325-
.alpha.pre:is(.one > .two) {
326-
order: 20;
327-
}
328-
329325
.pre.alpha:is(.one > .two) {
330326
order: 20;
331327
}
@@ -361,3 +357,43 @@ a:not(.does-not-exist) {
361357
.invalid-is:is {
362358
order: 24;
363359
}
360+
361+
a:hover::before {
362+
order: 25;
363+
}
364+
365+
a:focus:not(does-not-exist) {
366+
order: 25;
367+
}
368+
369+
button:hover::before {
370+
order: 25;
371+
}
372+
373+
button:focus:not(does-not-exist) {
374+
order: 25;
375+
}
376+
377+
:is(a, button):is(:hover::before, :focus) {
378+
order: 25;
379+
}
380+
381+
a:hover::before {
382+
order: 26;
383+
}
384+
385+
a:focus::before {
386+
order: 26;
387+
}
388+
389+
button:hover::before {
390+
order: 26;
391+
}
392+
393+
button:focus::before {
394+
order: 26;
395+
}
396+
397+
:is(a, button):is(:hover, :focus)::before {
398+
order: 26;
399+
}

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

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
order: 0;
33
}
44

5+
.some.other {
6+
order: 0;
7+
}
8+
59
.other.some {
610
order: 0;
711
}
@@ -18,6 +22,10 @@
1822
order: 1;
1923
}
2024

25+
.some.other {
26+
order: 1;
27+
}
28+
2129
.other.some {
2230
order: 1;
2331
}
@@ -34,6 +42,10 @@
3442
order: 2;
3543
}
3644

45+
.some.other {
46+
order: 2;
47+
}
48+
3749
.other.some {
3850
order: 2;
3951
}
@@ -890,11 +902,11 @@ button {
890902
order: 59;
891903
}
892904

893-
.some.🧑🏾‍🎤 {
905+
.🧑🏾‍🎤.some {
894906
order: 60;
895907
}
896908

897-
.other.🧑🏾‍🎤 {
909+
.🧑🏾‍🎤.other {
898910
order: 60;
899911
}
900912

@@ -1230,11 +1242,11 @@ button {
12301242
order: 90;
12311243
}
12321244

1233-
.foo.some {
1245+
.some.foo {
12341246
order: 91;
12351247
}
12361248

1237-
.foo.other {
1249+
.other.foo {
12381250
order: 91;
12391251
}
12401252

plugins/postcss-nesting/src/lib/merge-selectors/compound-selector-order.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,7 @@ export function sortCompoundSelectorsInsideComplexSelector(node, wrapWithIsPseud
5050
}
5151

5252
export function sortCompoundSelector(node) {
53-
// compound selectors with nesting can be written with tag selectors as later parts.
54-
// for example : `&h1`
55-
//
56-
// simply concating with parent selectors can lead to :
53+
// simply concatenating with selectors can lead to :
5754
// `.fooh1`
5855
//
5956
// applying a sort where tag selectors are first will result in :

plugins/postcss-nesting/test/basic.css

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,3 +109,9 @@ a {
109109
}
110110
}
111111
}
112+
113+
.a:hover, .b:focus {
114+
&::before, &::after {
115+
order: 51;
116+
}
117+
}

plugins/postcss-nesting/test/basic.expect.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,3 +124,7 @@ a b[a="a&b"] {
124124
.a.c:before, .b.c:before, .a.d:before, .b.d:before {
125125
order: 41;
126126
}
127+
128+
.a:hover::before, .b:focus::before, .a:hover::after, .b:focus::after {
129+
order: 51;
130+
}

plugins/postcss-nesting/test/basic.no-is-pseudo-selector.expect.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,3 +124,7 @@ a b[a="a&b"] {
124124
.a.c:before, .b.c:before, .a.d:before, .b.d:before {
125125
order: 41;
126126
}
127+
128+
.a:hover::before, .b:focus::before, .a:hover::after, .b:focus::after {
129+
order: 51;
130+
}

plugins/postcss-pseudo-class-any-link/src/compound-selector-order.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,7 @@ export function sortCompoundSelector(node) {
3939
if (!node || !node.nodes) {
4040
return;
4141
}
42-
// compound selectors with nesting can be written with tag selectors as later parts.
43-
// for example : `&h1`
44-
//
45-
// simply concating with parent selectors can lead to :
42+
// simply concatenating with selectors can lead to :
4643
// `.fooh1`
4744
//
4845
// applying a sort where tag selectors are first will result in :

0 commit comments

Comments
 (0)