tailwindcss 0.0.0-oxide-insiders.cc1eec6 → 0.0.0-oxide-insiders.ba54545

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -11,7 +11,7 @@ const _postcssSelectorParser = /*#__PURE__*/ _interopRequireDefault(require("pos
11
11
  const _generateRules = require("./generateRules");
12
12
  const _escapeClassName = /*#__PURE__*/ _interopRequireDefault(require("../util/escapeClassName"));
13
13
  const _applyImportantSelector = require("../util/applyImportantSelector");
14
- const _formatVariantSelectorJs = require("../util/formatVariantSelector.js");
14
+ const _pseudoElements = require("../util/pseudoElements");
15
15
  function _interopRequireDefault(obj) {
16
16
  return obj && obj.__esModule ? obj : {
17
17
  default: obj
@@ -490,12 +490,7 @@ function processApply(root, context, localCache) {
490
490
  });
491
491
  // Move pseudo elements to the end of the selector (if necessary)
492
492
  let selector = (0, _postcssSelectorParser.default)().astSync(rule.selector);
493
- selector.each((sel)=>{
494
- let [pseudoElements] = (0, _formatVariantSelectorJs.collectPseudoElements)(sel);
495
- if (pseudoElements.length > 0) {
496
- sel.nodes.push(...pseudoElements.sort(_formatVariantSelectorJs.sortSelector));
497
- }
498
- });
493
+ selector.each((sel)=>(0, _pseudoElements.movePseudos)(sel));
499
494
  rule.selector = selector.toString();
500
495
  });
501
496
  }
@@ -7,7 +7,7 @@ Object.defineProperty(exports, "applyImportantSelector", {
7
7
  get: ()=>applyImportantSelector
8
8
  });
9
9
  const _postcssSelectorParser = /*#__PURE__*/ _interopRequireDefault(require("postcss-selector-parser"));
10
- const _formatVariantSelectorJs = require("./formatVariantSelector.js");
10
+ const _pseudoElements = require("./pseudoElements");
11
11
  function _interopRequireDefault(obj) {
12
12
  return obj && obj.__esModule ? obj : {
13
13
  default: obj
@@ -28,10 +28,7 @@ function applyImportantSelector(selector, important) {
28
28
  })
29
29
  ];
30
30
  }
31
- let [pseudoElements] = (0, _formatVariantSelectorJs.collectPseudoElements)(sel);
32
- if (pseudoElements.length > 0) {
33
- sel.nodes.push(...pseudoElements.sort(_formatVariantSelectorJs.sortSelector));
34
- }
31
+ (0, _pseudoElements.movePseudos)(sel);
35
32
  });
36
33
  return `${important} ${sel.toString()}`;
37
34
  }
@@ -12,14 +12,13 @@ _export(exports, {
12
12
  formatVariantSelector: ()=>formatVariantSelector,
13
13
  eliminateIrrelevantSelectors: ()=>eliminateIrrelevantSelectors,
14
14
  finalizeSelector: ()=>finalizeSelector,
15
- handleMergePseudo: ()=>handleMergePseudo,
16
- collectPseudoElements: ()=>collectPseudoElements,
17
- sortSelector: ()=>sortSelector
15
+ handleMergePseudo: ()=>handleMergePseudo
18
16
  });
19
17
  const _postcssSelectorParser = /*#__PURE__*/ _interopRequireDefault(require("postcss-selector-parser"));
20
18
  const _unesc = /*#__PURE__*/ _interopRequireDefault(require("postcss-selector-parser/dist/util/unesc"));
21
19
  const _escapeClassName = /*#__PURE__*/ _interopRequireDefault(require("../util/escapeClassName"));
22
20
  const _prefixSelector = /*#__PURE__*/ _interopRequireDefault(require("../util/prefixSelector"));
21
+ const _pseudoElements = require("./pseudoElements");
23
22
  function _interopRequireDefault(obj) {
24
23
  return obj && obj.__esModule ? obj : {
25
24
  default: obj
@@ -203,12 +202,7 @@ function finalizeSelector(current, formats, { context , candidate , base }) {
203
202
  }
204
203
  });
205
204
  // Move pseudo elements to the end of the selector (if necessary)
206
- selector.each((sel)=>{
207
- let [pseudoElements] = collectPseudoElements(sel);
208
- if (pseudoElements.length > 0) {
209
- sel.nodes.push(...pseudoElements.sort(sortSelector));
210
- }
211
- });
205
+ selector.each((sel)=>(0, _pseudoElements.movePseudos)(sel));
212
206
  return selector.toString();
213
207
  }
214
208
  function handleMergePseudo(selector, format) {
@@ -259,85 +253,3 @@ function handleMergePseudo(selector, format) {
259
253
  format
260
254
  ];
261
255
  }
262
- // Note: As a rule, double colons (::) should be used instead of a single colon
263
- // (:). This distinguishes pseudo-classes from pseudo-elements. However, since
264
- // this distinction was not present in older versions of the W3C spec, most
265
- // browsers support both syntaxes for the original pseudo-elements.
266
- let pseudoElementsBC = [
267
- ":before",
268
- ":after",
269
- ":first-line",
270
- ":first-letter"
271
- ];
272
- // These pseudo-elements _can_ be combined with other pseudo selectors AND the order does matter.
273
- let pseudoElementExceptions = [
274
- "::file-selector-button",
275
- // Webkit scroll bar pseudo elements can be combined with user-action pseudo classes
276
- "::-webkit-scrollbar",
277
- "::-webkit-scrollbar-button",
278
- "::-webkit-scrollbar-thumb",
279
- "::-webkit-scrollbar-track",
280
- "::-webkit-scrollbar-track-piece",
281
- "::-webkit-scrollbar-corner",
282
- "::-webkit-resizer"
283
- ];
284
- function collectPseudoElements(selector, force = false) {
285
- /** @type {Node[]} */ let nodes = [];
286
- let seenPseudoElement = null;
287
- for (let node of [
288
- ...selector.nodes
289
- ]){
290
- if (isPseudoElement(node, force)) {
291
- nodes.push(node);
292
- selector.removeChild(node);
293
- seenPseudoElement = node.value;
294
- } else if (seenPseudoElement !== null) {
295
- if (pseudoElementExceptions.includes(seenPseudoElement) && isPseudoClass(node, force)) {
296
- nodes.push(node);
297
- selector.removeChild(node);
298
- } else {
299
- seenPseudoElement = null;
300
- }
301
- }
302
- if (node === null || node === void 0 ? void 0 : node.nodes) {
303
- let hasPseudoElementRestrictions = node.type === "pseudo" && (node.value === ":is" || node.value === ":has");
304
- let [collected, seenPseudoElementInSelector] = collectPseudoElements(node, force || hasPseudoElementRestrictions);
305
- if (seenPseudoElementInSelector) {
306
- seenPseudoElement = seenPseudoElementInSelector;
307
- }
308
- nodes.push(...collected);
309
- }
310
- }
311
- return [
312
- nodes,
313
- seenPseudoElement
314
- ];
315
- }
316
- function sortSelector(a, z) {
317
- // Both nodes are non-pseudo's so we can safely ignore them and keep
318
- // them in the same order.
319
- if (a.type !== "pseudo" && z.type !== "pseudo") {
320
- return 0;
321
- }
322
- // If one of them is a combinator, we need to keep it in the same order
323
- // because that means it will start a new "section" in the selector.
324
- if (a.type === "combinator" ^ z.type === "combinator") {
325
- return 0;
326
- }
327
- // One of the items is a pseudo and the other one isn't. Let's move
328
- // the pseudo to the right.
329
- if (a.type === "pseudo" ^ z.type === "pseudo") {
330
- return (a.type === "pseudo") - (z.type === "pseudo");
331
- }
332
- // Both are pseudo's, move the pseudo elements (except for
333
- // ::file-selector-button) to the right.
334
- return isPseudoElement(a) - isPseudoElement(z);
335
- }
336
- function isPseudoElement(node, force = false) {
337
- if (node.type !== "pseudo") return false;
338
- if (pseudoElementExceptions.includes(node.value) && !force) return false;
339
- return node.value.startsWith("::") || pseudoElementsBC.includes(node.value);
340
- }
341
- function isPseudoClass(node, force) {
342
- return node.type === "pseudo" && !isPseudoElement(node, force);
343
- }
@@ -0,0 +1,223 @@
1
+ /** @typedef {import('postcss-selector-parser').Root} Root */ /** @typedef {import('postcss-selector-parser').Selector} Selector */ /** @typedef {import('postcss-selector-parser').Pseudo} Pseudo */ /** @typedef {import('postcss-selector-parser').Node} Node */ // There are some pseudo-elements that may or may not be:
2
+ // **Actionable**
3
+ // Zero or more user-action pseudo-classes may be attached to the pseudo-element itself
4
+ // structural-pseudo-classes are NOT allowed but we don't make
5
+ // The spec is not clear on whether this is allowed or not — but in practice it is.
6
+ // **Terminal**
7
+ // It MUST be placed at the end of a selector
8
+ //
9
+ // This is the required in the spec. However, some pseudo elements are not "terminal" because
10
+ // they represent a "boundary piercing" that is compiled out by a build step.
11
+ // **Jumpable**
12
+ // Any terminal element may "jump" over combinators when moving to the end of the selector
13
+ //
14
+ // This is a backwards-compat quirk of :before and :after variants.
15
+ /** @typedef {'terminal' | 'actionable' | 'jumpable'} PseudoProperty */ /** @type {Record<string, PseudoProperty[]>} */ "use strict";
16
+ Object.defineProperty(exports, "__esModule", {
17
+ value: true
18
+ });
19
+ Object.defineProperty(exports, "movePseudos", {
20
+ enumerable: true,
21
+ get: ()=>movePseudos
22
+ });
23
+ let elementProperties = {
24
+ "::after": [
25
+ "terminal",
26
+ "jumpable"
27
+ ],
28
+ "::backdrop": [
29
+ "terminal"
30
+ ],
31
+ "::before": [
32
+ "terminal",
33
+ "jumpable"
34
+ ],
35
+ "::cue": [
36
+ "terminal"
37
+ ],
38
+ "::cue-region": [
39
+ "terminal"
40
+ ],
41
+ "::first-letter": [
42
+ "terminal",
43
+ "jumpable"
44
+ ],
45
+ "::first-line": [
46
+ "terminal",
47
+ "jumpable"
48
+ ],
49
+ "::grammar-error": [
50
+ "terminal"
51
+ ],
52
+ "::marker": [
53
+ "terminal"
54
+ ],
55
+ "::part": [
56
+ "terminal",
57
+ "actionable"
58
+ ],
59
+ "::placeholder": [
60
+ "terminal"
61
+ ],
62
+ "::selection": [
63
+ "terminal"
64
+ ],
65
+ "::slotted": [
66
+ "terminal"
67
+ ],
68
+ "::spelling-error": [
69
+ "terminal"
70
+ ],
71
+ "::target-text": [
72
+ "terminal"
73
+ ],
74
+ // other
75
+ "::file-selector-button": [
76
+ "terminal",
77
+ "actionable"
78
+ ],
79
+ "::-webkit-progress-bar": [
80
+ "terminal",
81
+ "actionable"
82
+ ],
83
+ // Webkit scroll bar pseudo elements can be combined with user-action pseudo classes
84
+ "::-webkit-scrollbar": [
85
+ "terminal",
86
+ "actionable"
87
+ ],
88
+ "::-webkit-scrollbar-button": [
89
+ "terminal",
90
+ "actionable"
91
+ ],
92
+ "::-webkit-scrollbar-thumb": [
93
+ "terminal",
94
+ "actionable"
95
+ ],
96
+ "::-webkit-scrollbar-track": [
97
+ "terminal",
98
+ "actionable"
99
+ ],
100
+ "::-webkit-scrollbar-track-piece": [
101
+ "terminal",
102
+ "actionable"
103
+ ],
104
+ "::-webkit-scrollbar-corner": [
105
+ "terminal",
106
+ "actionable"
107
+ ],
108
+ "::-webkit-resizer": [
109
+ "terminal",
110
+ "actionable"
111
+ ],
112
+ // Note: As a rule, double colons (::) should be used instead of a single colon
113
+ // (:). This distinguishes pseudo-classes from pseudo-elements. However, since
114
+ // this distinction was not present in older versions of the W3C spec, most
115
+ // browsers support both syntaxes for the original pseudo-elements.
116
+ ":after": [
117
+ "terminal",
118
+ "jumpable"
119
+ ],
120
+ ":before": [
121
+ "terminal",
122
+ "jumpable"
123
+ ],
124
+ ":first-letter": [
125
+ "terminal",
126
+ "jumpable"
127
+ ],
128
+ ":first-line": [
129
+ "terminal",
130
+ "jumpable"
131
+ ],
132
+ // The default value is used when the pseudo-element is not recognized
133
+ // Because it's not recognized, we don't know if it's terminal or not
134
+ // So we assume it can't be moved AND can have user-action pseudo classes attached to it
135
+ __default__: [
136
+ "actionable"
137
+ ]
138
+ };
139
+ function movePseudos(sel) {
140
+ let [pseudos] = movablePseudos(sel);
141
+ // Remove all pseudo elements from their respective selectors
142
+ pseudos.forEach(([sel, pseudo])=>sel.removeChild(pseudo));
143
+ // Re-add them to the end of the selector in the correct order.
144
+ // This moves terminal pseudo elements to the end of the
145
+ // selector otherwise the selector will not be valid.
146
+ //
147
+ // Examples:
148
+ // - `before:hover:text-center` would result in `.before\:hover\:text-center:hover::before`
149
+ // - `hover:before:text-center` would result in `.hover\:before\:text-center:hover::before`
150
+ //
151
+ // The selector `::before:hover` does not work but we
152
+ // can make it work for you by flipping the order.
153
+ sel.nodes.push(...pseudos.map(([, pseudo])=>pseudo));
154
+ return sel;
155
+ }
156
+ /** @typedef {[sel: Selector, pseudo: Pseudo, attachedTo: Pseudo | null]} MovablePseudo */ /** @typedef {[pseudos: MovablePseudo[], lastSeenElement: Pseudo | null]} MovablePseudosResult */ /**
157
+ * @param {Selector} sel
158
+ * @returns {MovablePseudosResult}
159
+ */ function movablePseudos(sel) {
160
+ /** @type {MovablePseudo[]} */ let buffer = [];
161
+ /** @type {Pseudo | null} */ let lastSeenElement = null;
162
+ for (let node of sel.nodes){
163
+ if (node.type === "combinator") {
164
+ buffer = buffer.filter(([, node])=>propertiesForPseudo(node).includes("jumpable"));
165
+ lastSeenElement = null;
166
+ } else if (node.type === "pseudo") {
167
+ if (isMovablePseudoElement(node)) {
168
+ lastSeenElement = node;
169
+ buffer.push([
170
+ sel,
171
+ node,
172
+ null
173
+ ]);
174
+ } else if (lastSeenElement && isAttachablePseudoClass(node, lastSeenElement)) {
175
+ buffer.push([
176
+ sel,
177
+ node,
178
+ lastSeenElement
179
+ ]);
180
+ } else {
181
+ lastSeenElement = null;
182
+ }
183
+ var _node_nodes;
184
+ for (let sub of (_node_nodes = node.nodes) !== null && _node_nodes !== void 0 ? _node_nodes : []){
185
+ let [movable, lastSeenElementInSub] = movablePseudos(sub);
186
+ lastSeenElement = lastSeenElementInSub || lastSeenElement;
187
+ buffer.push(...movable);
188
+ }
189
+ }
190
+ }
191
+ return [
192
+ buffer,
193
+ lastSeenElement
194
+ ];
195
+ }
196
+ /**
197
+ * @param {Node} node
198
+ * @returns {boolean}
199
+ */ function isPseudoElement(node) {
200
+ return node.value.startsWith("::") || elementProperties[node.value] !== undefined;
201
+ }
202
+ /**
203
+ * @param {Node} node
204
+ * @returns {boolean}
205
+ */ function isMovablePseudoElement(node) {
206
+ return isPseudoElement(node) && propertiesForPseudo(node).includes("terminal");
207
+ }
208
+ /**
209
+ * @param {Node} node
210
+ * @param {Pseudo} pseudo
211
+ * @returns {boolean}
212
+ */ function isAttachablePseudoClass(node, pseudo) {
213
+ if (node.type !== "pseudo") return false;
214
+ if (isPseudoElement(node)) return false;
215
+ return propertiesForPseudo(pseudo).includes("actionable");
216
+ }
217
+ /**
218
+ * @param {Pseudo} pseudo
219
+ * @returns {PseudoProperty[]}
220
+ */ function propertiesForPseudo(pseudo) {
221
+ var _elementProperties_pseudo_value;
222
+ return (_elementProperties_pseudo_value = elementProperties[pseudo.value]) !== null && _elementProperties_pseudo_value !== void 0 ? _elementProperties_pseudo_value : elementProperties.__default__;
223
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tailwindcss",
3
- "version": "0.0.0-oxide-insiders.cc1eec6",
3
+ "version": "0.0.0-oxide-insiders.ba54545",
4
4
  "description": "A utility-first CSS framework for rapidly building custom user interfaces.",
5
5
  "license": "MIT",
6
6
  "main": "lib/index.js",
@@ -70,7 +70,7 @@
70
70
  "postcss": "^8.0.9"
71
71
  },
72
72
  "dependencies": {
73
- "@tailwindcss/oxide": "0.0.0-oxide-insiders.cc1eec6",
73
+ "@tailwindcss/oxide": "0.0.0-oxide-insiders.ba54545",
74
74
  "arg": "^5.0.2",
75
75
  "browserslist": "^4.21.5",
76
76
  "chokidar": "^3.5.3",
@@ -4,7 +4,7 @@ import parser from 'postcss-selector-parser'
4
4
  import { resolveMatches } from './generateRules'
5
5
  import escapeClassName from '../util/escapeClassName'
6
6
  import { applyImportantSelector } from '../util/applyImportantSelector'
7
- import { collectPseudoElements, sortSelector } from '../util/formatVariantSelector.js'
7
+ import { movePseudos } from '../util/pseudoElements'
8
8
 
9
9
  /** @typedef {Map<string, [any, import('postcss').Rule[]]>} ApplyCache */
10
10
 
@@ -566,13 +566,7 @@ function processApply(root, context, localCache) {
566
566
 
567
567
  // Move pseudo elements to the end of the selector (if necessary)
568
568
  let selector = parser().astSync(rule.selector)
569
- selector.each((sel) => {
570
- let [pseudoElements] = collectPseudoElements(sel)
571
- if (pseudoElements.length > 0) {
572
- sel.nodes.push(...pseudoElements.sort(sortSelector))
573
- }
574
- })
575
-
569
+ selector.each((sel) => movePseudos(sel))
576
570
  rule.selector = selector.toString()
577
571
  })
578
572
  }
@@ -1,5 +1,5 @@
1
1
  import parser from 'postcss-selector-parser'
2
- import { collectPseudoElements, sortSelector } from './formatVariantSelector.js'
2
+ import { movePseudos } from './pseudoElements'
3
3
 
4
4
  export function applyImportantSelector(selector, important) {
5
5
  let sel = parser().astSync(selector)
@@ -20,10 +20,7 @@ export function applyImportantSelector(selector, important) {
20
20
  ]
21
21
  }
22
22
 
23
- let [pseudoElements] = collectPseudoElements(sel)
24
- if (pseudoElements.length > 0) {
25
- sel.nodes.push(...pseudoElements.sort(sortSelector))
26
- }
23
+ movePseudos(sel)
27
24
  })
28
25
 
29
26
  return `${important} ${sel.toString()}`
@@ -2,6 +2,7 @@ import selectorParser from 'postcss-selector-parser'
2
2
  import unescape from 'postcss-selector-parser/dist/util/unesc'
3
3
  import escapeClassName from '../util/escapeClassName'
4
4
  import prefixSelector from '../util/prefixSelector'
5
+ import { movePseudos } from './pseudoElements'
5
6
 
6
7
  /** @typedef {import('postcss-selector-parser').Root} Root */
7
8
  /** @typedef {import('postcss-selector-parser').Selector} Selector */
@@ -245,12 +246,7 @@ export function finalizeSelector(current, formats, { context, candidate, base })
245
246
  })
246
247
 
247
248
  // Move pseudo elements to the end of the selector (if necessary)
248
- selector.each((sel) => {
249
- let [pseudoElements] = collectPseudoElements(sel)
250
- if (pseudoElements.length > 0) {
251
- sel.nodes.push(...pseudoElements.sort(sortSelector))
252
- }
253
- })
249
+ selector.each((sel) => movePseudos(sel))
254
250
 
255
251
  return selector.toString()
256
252
  }
@@ -318,121 +314,3 @@ export function handleMergePseudo(selector, format) {
318
314
 
319
315
  return [selector, format]
320
316
  }
321
-
322
- // Note: As a rule, double colons (::) should be used instead of a single colon
323
- // (:). This distinguishes pseudo-classes from pseudo-elements. However, since
324
- // this distinction was not present in older versions of the W3C spec, most
325
- // browsers support both syntaxes for the original pseudo-elements.
326
- let pseudoElementsBC = [':before', ':after', ':first-line', ':first-letter']
327
-
328
- // These pseudo-elements _can_ be combined with other pseudo selectors AND the order does matter.
329
- let pseudoElementExceptions = [
330
- '::file-selector-button',
331
-
332
- // Webkit scroll bar pseudo elements can be combined with user-action pseudo classes
333
- '::-webkit-scrollbar',
334
- '::-webkit-scrollbar-button',
335
- '::-webkit-scrollbar-thumb',
336
- '::-webkit-scrollbar-track',
337
- '::-webkit-scrollbar-track-piece',
338
- '::-webkit-scrollbar-corner',
339
- '::-webkit-resizer',
340
- ]
341
-
342
- /**
343
- * This will make sure to move pseudo's to the correct spot (the end for
344
- * pseudo elements) because otherwise the selector will never work
345
- * anyway.
346
- *
347
- * E.g.:
348
- * - `before:hover:text-center` would result in `.before\:hover\:text-center:hover::before`
349
- * - `hover:before:text-center` would result in `.hover\:before\:text-center:hover::before`
350
- *
351
- * `::before:hover` doesn't work, which means that we can make it work for you by flipping the order.
352
- *
353
- * @param {Selector} selector
354
- * @param {boolean} force
355
- **/
356
- export function collectPseudoElements(selector, force = false) {
357
- /** @type {Node[]} */
358
- let nodes = []
359
- let seenPseudoElement = null
360
-
361
- for (let node of [...selector.nodes]) {
362
- if (isPseudoElement(node, force)) {
363
- nodes.push(node)
364
- selector.removeChild(node)
365
- seenPseudoElement = node.value
366
- } else if (seenPseudoElement !== null) {
367
- if (pseudoElementExceptions.includes(seenPseudoElement) && isPseudoClass(node, force)) {
368
- nodes.push(node)
369
- selector.removeChild(node)
370
- } else {
371
- seenPseudoElement = null
372
- }
373
- }
374
-
375
- if (node?.nodes) {
376
- let hasPseudoElementRestrictions =
377
- node.type === 'pseudo' && (node.value === ':is' || node.value === ':has')
378
-
379
- let [collected, seenPseudoElementInSelector] = collectPseudoElements(
380
- node,
381
- force || hasPseudoElementRestrictions
382
- )
383
-
384
- if (seenPseudoElementInSelector) {
385
- seenPseudoElement = seenPseudoElementInSelector
386
- }
387
-
388
- nodes.push(...collected)
389
- }
390
- }
391
-
392
- return [nodes, seenPseudoElement]
393
- }
394
-
395
- // This will make sure to move pseudo's to the correct spot (the end for
396
- // pseudo elements) because otherwise the selector will never work
397
- // anyway.
398
- //
399
- // E.g.:
400
- // - `before:hover:text-center` would result in `.before\:hover\:text-center:hover::before`
401
- // - `hover:before:text-center` would result in `.hover\:before\:text-center:hover::before`
402
- //
403
- // `::before:hover` doesn't work, which means that we can make it work
404
- // for you by flipping the order.
405
- export function sortSelector(a, z) {
406
- // Both nodes are non-pseudo's so we can safely ignore them and keep
407
- // them in the same order.
408
- if (a.type !== 'pseudo' && z.type !== 'pseudo') {
409
- return 0
410
- }
411
-
412
- // If one of them is a combinator, we need to keep it in the same order
413
- // because that means it will start a new "section" in the selector.
414
- if ((a.type === 'combinator') ^ (z.type === 'combinator')) {
415
- return 0
416
- }
417
-
418
- // One of the items is a pseudo and the other one isn't. Let's move
419
- // the pseudo to the right.
420
- if ((a.type === 'pseudo') ^ (z.type === 'pseudo')) {
421
- return (a.type === 'pseudo') - (z.type === 'pseudo')
422
- }
423
-
424
- // Both are pseudo's, move the pseudo elements (except for
425
- // ::file-selector-button) to the right.
426
- return isPseudoElement(a) - isPseudoElement(z)
427
- }
428
-
429
- function isPseudoElement(node, force = false) {
430
- if (node.type !== 'pseudo') return false
431
- if (pseudoElementExceptions.includes(node.value) && !force) return false
432
-
433
- return node.value.startsWith('::') || pseudoElementsBC.includes(node.value)
434
- }
435
-
436
- function isPseudoClass(node, force) {
437
- return node.type === 'pseudo' && !isPseudoElement(node, force)
438
- }
@@ -0,0 +1,170 @@
1
+ /** @typedef {import('postcss-selector-parser').Root} Root */
2
+ /** @typedef {import('postcss-selector-parser').Selector} Selector */
3
+ /** @typedef {import('postcss-selector-parser').Pseudo} Pseudo */
4
+ /** @typedef {import('postcss-selector-parser').Node} Node */
5
+
6
+ // There are some pseudo-elements that may or may not be:
7
+
8
+ // **Actionable**
9
+ // Zero or more user-action pseudo-classes may be attached to the pseudo-element itself
10
+ // structural-pseudo-classes are NOT allowed but we don't make
11
+ // The spec is not clear on whether this is allowed or not — but in practice it is.
12
+
13
+ // **Terminal**
14
+ // It MUST be placed at the end of a selector
15
+ //
16
+ // This is the required in the spec. However, some pseudo elements are not "terminal" because
17
+ // they represent a "boundary piercing" that is compiled out by a build step.
18
+
19
+ // **Jumpable**
20
+ // Any terminal element may "jump" over combinators when moving to the end of the selector
21
+ //
22
+ // This is a backwards-compat quirk of :before and :after variants.
23
+
24
+ /** @typedef {'terminal' | 'actionable' | 'jumpable'} PseudoProperty */
25
+
26
+ /** @type {Record<string, PseudoProperty[]>} */
27
+ let elementProperties = {
28
+ '::after': ['terminal', 'jumpable'],
29
+ '::backdrop': ['terminal'],
30
+ '::before': ['terminal', 'jumpable'],
31
+ '::cue': ['terminal'],
32
+ '::cue-region': ['terminal'],
33
+ '::first-letter': ['terminal', 'jumpable'],
34
+ '::first-line': ['terminal', 'jumpable'],
35
+ '::grammar-error': ['terminal'],
36
+ '::marker': ['terminal'],
37
+ '::part': ['terminal', 'actionable'],
38
+ '::placeholder': ['terminal'],
39
+ '::selection': ['terminal'],
40
+ '::slotted': ['terminal'],
41
+ '::spelling-error': ['terminal'],
42
+ '::target-text': ['terminal'],
43
+
44
+ // other
45
+ '::file-selector-button': ['terminal', 'actionable'],
46
+ '::-webkit-progress-bar': ['terminal', 'actionable'],
47
+
48
+ // Webkit scroll bar pseudo elements can be combined with user-action pseudo classes
49
+ '::-webkit-scrollbar': ['terminal', 'actionable'],
50
+ '::-webkit-scrollbar-button': ['terminal', 'actionable'],
51
+ '::-webkit-scrollbar-thumb': ['terminal', 'actionable'],
52
+ '::-webkit-scrollbar-track': ['terminal', 'actionable'],
53
+ '::-webkit-scrollbar-track-piece': ['terminal', 'actionable'],
54
+ '::-webkit-scrollbar-corner': ['terminal', 'actionable'],
55
+ '::-webkit-resizer': ['terminal', 'actionable'],
56
+
57
+ // Note: As a rule, double colons (::) should be used instead of a single colon
58
+ // (:). This distinguishes pseudo-classes from pseudo-elements. However, since
59
+ // this distinction was not present in older versions of the W3C spec, most
60
+ // browsers support both syntaxes for the original pseudo-elements.
61
+ ':after': ['terminal', 'jumpable'],
62
+ ':before': ['terminal', 'jumpable'],
63
+ ':first-letter': ['terminal', 'jumpable'],
64
+ ':first-line': ['terminal', 'jumpable'],
65
+
66
+ // The default value is used when the pseudo-element is not recognized
67
+ // Because it's not recognized, we don't know if it's terminal or not
68
+ // So we assume it can't be moved AND can have user-action pseudo classes attached to it
69
+ __default__: ['actionable'],
70
+ }
71
+
72
+ /**
73
+ * @param {Selector} sel
74
+ * @returns {Selector}
75
+ */
76
+ export function movePseudos(sel) {
77
+ let [pseudos] = movablePseudos(sel)
78
+
79
+ // Remove all pseudo elements from their respective selectors
80
+ pseudos.forEach(([sel, pseudo]) => sel.removeChild(pseudo))
81
+
82
+ // Re-add them to the end of the selector in the correct order.
83
+ // This moves terminal pseudo elements to the end of the
84
+ // selector otherwise the selector will not be valid.
85
+ //
86
+ // Examples:
87
+ // - `before:hover:text-center` would result in `.before\:hover\:text-center:hover::before`
88
+ // - `hover:before:text-center` would result in `.hover\:before\:text-center:hover::before`
89
+ //
90
+ // The selector `::before:hover` does not work but we
91
+ // can make it work for you by flipping the order.
92
+ sel.nodes.push(...pseudos.map(([, pseudo]) => pseudo))
93
+
94
+ return sel
95
+ }
96
+
97
+ /** @typedef {[sel: Selector, pseudo: Pseudo, attachedTo: Pseudo | null]} MovablePseudo */
98
+ /** @typedef {[pseudos: MovablePseudo[], lastSeenElement: Pseudo | null]} MovablePseudosResult */
99
+
100
+ /**
101
+ * @param {Selector} sel
102
+ * @returns {MovablePseudosResult}
103
+ */
104
+ function movablePseudos(sel) {
105
+ /** @type {MovablePseudo[]} */
106
+ let buffer = []
107
+
108
+ /** @type {Pseudo | null} */
109
+ let lastSeenElement = null
110
+
111
+ for (let node of sel.nodes) {
112
+ if (node.type === 'combinator') {
113
+ buffer = buffer.filter(([, node]) => propertiesForPseudo(node).includes('jumpable'))
114
+ lastSeenElement = null
115
+ } else if (node.type === 'pseudo') {
116
+ if (isMovablePseudoElement(node)) {
117
+ lastSeenElement = node
118
+ buffer.push([sel, node, null])
119
+ } else if (lastSeenElement && isAttachablePseudoClass(node, lastSeenElement)) {
120
+ buffer.push([sel, node, lastSeenElement])
121
+ } else {
122
+ lastSeenElement = null
123
+ }
124
+
125
+ for (let sub of node.nodes ?? []) {
126
+ let [movable, lastSeenElementInSub] = movablePseudos(sub)
127
+ lastSeenElement = lastSeenElementInSub || lastSeenElement
128
+ buffer.push(...movable)
129
+ }
130
+ }
131
+ }
132
+
133
+ return [buffer, lastSeenElement]
134
+ }
135
+
136
+ /**
137
+ * @param {Node} node
138
+ * @returns {boolean}
139
+ */
140
+ function isPseudoElement(node) {
141
+ return node.value.startsWith('::') || elementProperties[node.value] !== undefined
142
+ }
143
+
144
+ /**
145
+ * @param {Node} node
146
+ * @returns {boolean}
147
+ */
148
+ function isMovablePseudoElement(node) {
149
+ return isPseudoElement(node) && propertiesForPseudo(node).includes('terminal')
150
+ }
151
+
152
+ /**
153
+ * @param {Node} node
154
+ * @param {Pseudo} pseudo
155
+ * @returns {boolean}
156
+ */
157
+ function isAttachablePseudoClass(node, pseudo) {
158
+ if (node.type !== 'pseudo') return false
159
+ if (isPseudoElement(node)) return false
160
+
161
+ return propertiesForPseudo(pseudo).includes('actionable')
162
+ }
163
+
164
+ /**
165
+ * @param {Pseudo} pseudo
166
+ * @returns {PseudoProperty[]}
167
+ */
168
+ function propertiesForPseudo(pseudo) {
169
+ return elementProperties[pseudo.value] ?? elementProperties.__default__
170
+ }