@tenphi/tasty 0.0.0-snapshot.24109f9 → 0.0.0-snapshot.2705ab9

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.
Files changed (228) hide show
  1. package/README.md +318 -158
  2. package/dist/chunks/cacheKey.js +16 -8
  3. package/dist/chunks/cacheKey.js.map +1 -1
  4. package/dist/chunks/definitions.d.ts +1 -1
  5. package/dist/chunks/definitions.js +2 -3
  6. package/dist/chunks/definitions.js.map +1 -1
  7. package/dist/chunks/renderChunk.js +31 -32
  8. package/dist/chunks/renderChunk.js.map +1 -1
  9. package/dist/config.d.ts +98 -12
  10. package/dist/config.js +134 -33
  11. package/dist/config.js.map +1 -1
  12. package/dist/core/index.d.ts +11 -10
  13. package/dist/core/index.js +8 -7
  14. package/dist/counter-style/index.js +51 -0
  15. package/dist/counter-style/index.js.map +1 -0
  16. package/dist/debug.d.ts +26 -141
  17. package/dist/debug.js +356 -635
  18. package/dist/debug.js.map +1 -1
  19. package/dist/font-face/index.js +63 -0
  20. package/dist/font-face/index.js.map +1 -0
  21. package/dist/hooks/resolve-ssr-collector.js +15 -0
  22. package/dist/hooks/resolve-ssr-collector.js.map +1 -0
  23. package/dist/hooks/useCounterStyle.d.ts +50 -0
  24. package/dist/hooks/useCounterStyle.js +47 -0
  25. package/dist/hooks/useCounterStyle.js.map +1 -0
  26. package/dist/hooks/useFontFace.d.ts +43 -0
  27. package/dist/hooks/useFontFace.js +71 -0
  28. package/dist/hooks/useFontFace.js.map +1 -0
  29. package/dist/hooks/useGlobalStyles.d.ts +3 -0
  30. package/dist/hooks/useGlobalStyles.js +24 -1
  31. package/dist/hooks/useGlobalStyles.js.map +1 -1
  32. package/dist/hooks/useKeyframes.js +14 -3
  33. package/dist/hooks/useKeyframes.js.map +1 -1
  34. package/dist/hooks/useProperty.js +32 -13
  35. package/dist/hooks/useProperty.js.map +1 -1
  36. package/dist/hooks/useRawCSS.js +9 -1
  37. package/dist/hooks/useRawCSS.js.map +1 -1
  38. package/dist/hooks/useStyles.d.ts +5 -0
  39. package/dist/hooks/useStyles.js +115 -46
  40. package/dist/hooks/useStyles.js.map +1 -1
  41. package/dist/index.d.ts +22 -11
  42. package/dist/index.js +10 -7
  43. package/dist/injector/index.d.ts +28 -2
  44. package/dist/injector/index.js +27 -1
  45. package/dist/injector/index.js.map +1 -1
  46. package/dist/injector/injector.d.ts +28 -1
  47. package/dist/injector/injector.js +93 -32
  48. package/dist/injector/injector.js.map +1 -1
  49. package/dist/injector/sheet-manager.d.ts +16 -7
  50. package/dist/injector/sheet-manager.js +31 -11
  51. package/dist/injector/sheet-manager.js.map +1 -1
  52. package/dist/injector/types.d.ts +70 -1
  53. package/dist/parser/classify.js +5 -4
  54. package/dist/parser/classify.js.map +1 -1
  55. package/dist/parser/const.js +17 -1
  56. package/dist/parser/const.js.map +1 -1
  57. package/dist/parser/parser.js +1 -1
  58. package/dist/pipeline/conditions.js +14 -8
  59. package/dist/pipeline/conditions.js.map +1 -1
  60. package/dist/pipeline/index.d.ts +3 -1
  61. package/dist/pipeline/index.js +137 -69
  62. package/dist/pipeline/index.js.map +1 -1
  63. package/dist/pipeline/materialize.js +452 -240
  64. package/dist/pipeline/materialize.js.map +1 -1
  65. package/dist/pipeline/parseStateKey.d.ts +1 -1
  66. package/dist/pipeline/parseStateKey.js +37 -20
  67. package/dist/pipeline/parseStateKey.js.map +1 -1
  68. package/dist/pipeline/simplify.js +111 -152
  69. package/dist/pipeline/simplify.js.map +1 -1
  70. package/dist/pipeline/warnings.js +18 -0
  71. package/dist/pipeline/warnings.js.map +1 -0
  72. package/dist/plugins/okhsl-plugin.js +2 -275
  73. package/dist/plugins/okhsl-plugin.js.map +1 -1
  74. package/dist/plugins/types.d.ts +9 -2
  75. package/dist/properties/index.js +82 -17
  76. package/dist/properties/index.js.map +1 -1
  77. package/dist/properties/property-type-resolver.d.ts +24 -0
  78. package/dist/properties/property-type-resolver.js +91 -0
  79. package/dist/properties/property-type-resolver.js.map +1 -0
  80. package/dist/ssr/astro.d.ts +29 -0
  81. package/dist/ssr/astro.js +65 -0
  82. package/dist/ssr/astro.js.map +1 -0
  83. package/dist/ssr/async-storage.d.ts +17 -0
  84. package/dist/ssr/async-storage.js +35 -0
  85. package/dist/ssr/async-storage.js.map +1 -0
  86. package/dist/ssr/collect-auto-properties.js +40 -0
  87. package/dist/ssr/collect-auto-properties.js.map +1 -0
  88. package/dist/ssr/collector.d.ts +102 -0
  89. package/dist/ssr/collector.js +227 -0
  90. package/dist/ssr/collector.js.map +1 -0
  91. package/dist/ssr/context.d.ts +8 -0
  92. package/dist/ssr/context.js +14 -0
  93. package/dist/ssr/context.js.map +1 -0
  94. package/dist/ssr/format-global-rules.js +22 -0
  95. package/dist/ssr/format-global-rules.js.map +1 -0
  96. package/dist/ssr/format-keyframes.js +70 -0
  97. package/dist/ssr/format-keyframes.js.map +1 -0
  98. package/dist/ssr/format-property.js +50 -0
  99. package/dist/ssr/format-property.js.map +1 -0
  100. package/dist/ssr/format-rules.js +73 -0
  101. package/dist/ssr/format-rules.js.map +1 -0
  102. package/dist/ssr/hydrate.d.ts +22 -0
  103. package/dist/ssr/hydrate.js +50 -0
  104. package/dist/ssr/hydrate.js.map +1 -0
  105. package/dist/ssr/index.d.ts +5 -0
  106. package/dist/ssr/index.js +12 -0
  107. package/dist/ssr/index.js.map +1 -0
  108. package/dist/ssr/next.d.ts +45 -0
  109. package/dist/ssr/next.js +71 -0
  110. package/dist/ssr/next.js.map +1 -0
  111. package/dist/ssr/ssr-collector-ref.js +12 -0
  112. package/dist/ssr/ssr-collector-ref.js.map +1 -0
  113. package/dist/states/index.js +12 -257
  114. package/dist/states/index.js.map +1 -1
  115. package/dist/styles/align.d.ts +1 -1
  116. package/dist/styles/align.js.map +1 -1
  117. package/dist/styles/border.d.ts +1 -1
  118. package/dist/styles/border.js +2 -2
  119. package/dist/styles/border.js.map +1 -1
  120. package/dist/styles/color.d.ts +2 -2
  121. package/dist/styles/color.js +9 -5
  122. package/dist/styles/color.js.map +1 -1
  123. package/dist/styles/createStyle.js +25 -22
  124. package/dist/styles/createStyle.js.map +1 -1
  125. package/dist/styles/fade.d.ts +1 -1
  126. package/dist/styles/fade.js.map +1 -1
  127. package/dist/styles/fill.d.ts +14 -16
  128. package/dist/styles/fill.js +6 -5
  129. package/dist/styles/fill.js.map +1 -1
  130. package/dist/styles/flow.d.ts +3 -3
  131. package/dist/styles/flow.js.map +1 -1
  132. package/dist/styles/index.js +1 -1
  133. package/dist/styles/inset.d.ts +3 -1
  134. package/dist/styles/inset.js +48 -40
  135. package/dist/styles/inset.js.map +1 -1
  136. package/dist/styles/justify.d.ts +1 -1
  137. package/dist/styles/justify.js.map +1 -1
  138. package/dist/styles/margin.d.ts +1 -5
  139. package/dist/styles/margin.js +48 -40
  140. package/dist/styles/margin.js.map +1 -1
  141. package/dist/styles/padding.d.ts +1 -5
  142. package/dist/styles/padding.js +48 -40
  143. package/dist/styles/padding.js.map +1 -1
  144. package/dist/styles/predefined.d.ts +0 -2
  145. package/dist/styles/predefined.js +1 -4
  146. package/dist/styles/predefined.js.map +1 -1
  147. package/dist/styles/preset.d.ts +5 -0
  148. package/dist/styles/preset.js +48 -47
  149. package/dist/styles/preset.js.map +1 -1
  150. package/dist/styles/radius.d.ts +1 -1
  151. package/dist/styles/radius.js.map +1 -1
  152. package/dist/styles/scrollbar.d.ts +9 -5
  153. package/dist/styles/scrollbar.js +25 -82
  154. package/dist/styles/scrollbar.js.map +1 -1
  155. package/dist/styles/shadow.d.ts +2 -2
  156. package/dist/styles/shadow.js.map +1 -1
  157. package/dist/styles/transition.d.ts +1 -1
  158. package/dist/styles/transition.js +27 -7
  159. package/dist/styles/transition.js.map +1 -1
  160. package/dist/styles/types.d.ts +70 -21
  161. package/dist/tasty.d.ts +30 -893
  162. package/dist/tasty.js +60 -19
  163. package/dist/tasty.js.map +1 -1
  164. package/dist/types.d.ts +3 -3
  165. package/dist/utils/cache-wrapper.js +4 -8
  166. package/dist/utils/cache-wrapper.js.map +1 -1
  167. package/dist/utils/color-math.d.ts +46 -0
  168. package/dist/utils/color-math.js +749 -0
  169. package/dist/utils/color-math.js.map +1 -0
  170. package/dist/utils/color-space.d.ts +5 -0
  171. package/dist/utils/color-space.js +229 -0
  172. package/dist/utils/color-space.js.map +1 -0
  173. package/dist/utils/colors.d.ts +1 -1
  174. package/dist/utils/colors.js +3 -1
  175. package/dist/utils/colors.js.map +1 -1
  176. package/dist/utils/css-types.d.ts +1 -1
  177. package/dist/utils/dotize.d.ts +1 -1
  178. package/dist/utils/has-keys.js +13 -0
  179. package/dist/utils/has-keys.js.map +1 -0
  180. package/dist/utils/mod-attrs.js +2 -2
  181. package/dist/utils/mod-attrs.js.map +1 -1
  182. package/dist/utils/process-tokens.d.ts +3 -13
  183. package/dist/utils/process-tokens.js +18 -98
  184. package/dist/utils/process-tokens.js.map +1 -1
  185. package/dist/utils/selector-transform.js +32 -0
  186. package/dist/utils/selector-transform.js.map +1 -0
  187. package/dist/utils/string.js.map +1 -1
  188. package/dist/utils/styles.d.ts +4 -88
  189. package/dist/utils/styles.js +34 -398
  190. package/dist/utils/styles.js.map +1 -1
  191. package/dist/utils/typography.d.ts +24 -13
  192. package/dist/utils/typography.js +14 -16
  193. package/dist/utils/typography.js.map +1 -1
  194. package/dist/zero/babel.d.ts +67 -8
  195. package/dist/zero/babel.js +109 -17
  196. package/dist/zero/babel.js.map +1 -1
  197. package/dist/zero/extractor.js +118 -1
  198. package/dist/zero/extractor.js.map +1 -1
  199. package/dist/zero/next.d.ts +44 -30
  200. package/dist/zero/next.js +102 -38
  201. package/dist/zero/next.js.map +1 -1
  202. package/docs/PIPELINE.md +519 -0
  203. package/docs/README.md +31 -0
  204. package/docs/adoption.md +296 -0
  205. package/docs/comparison.md +420 -0
  206. package/docs/configuration.md +326 -0
  207. package/docs/debug.md +318 -0
  208. package/docs/design-system.md +424 -0
  209. package/docs/dsl.md +673 -0
  210. package/docs/getting-started.md +217 -0
  211. package/docs/injector.md +528 -0
  212. package/docs/methodology.md +567 -0
  213. package/docs/runtime.md +485 -0
  214. package/docs/ssr.md +384 -0
  215. package/docs/styles.md +579 -0
  216. package/docs/tasty-static.md +433 -0
  217. package/package.json +102 -30
  218. package/tasty.config.ts +14 -0
  219. package/dist/styles/styledScrollbar.d.ts +0 -47
  220. package/dist/styles/styledScrollbar.js +0 -38
  221. package/dist/styles/styledScrollbar.js.map +0 -1
  222. package/dist/tokens/typography.d.ts +0 -19
  223. package/dist/tokens/typography.js +0 -237
  224. package/dist/tokens/typography.js.map +0 -1
  225. package/dist/utils/hsl-to-rgb.js +0 -38
  226. package/dist/utils/hsl-to-rgb.js.map +0 -1
  227. package/dist/utils/okhsl-to-rgb.js +0 -296
  228. package/dist/utils/okhsl-to-rgb.js.map +0 -1
@@ -1,5 +1,5 @@
1
1
  import { Lru } from "../parser/lru.js";
2
- import { not } from "./conditions.js";
2
+ import { getConditionUniqueId } from "./conditions.js";
3
3
 
4
4
  //#region src/pipeline/materialize.ts
5
5
  /**
@@ -13,25 +13,23 @@ const conditionCache = new Lru(3e3);
13
13
  * Convert a condition tree to CSS components
14
14
  */
15
15
  function conditionToCSS(node) {
16
- const key = getConditionKey(node);
16
+ const key = getConditionUniqueId(node);
17
17
  const cached = conditionCache.get(key);
18
18
  if (cached) return cached;
19
19
  const result = conditionToCSSInner(node);
20
20
  conditionCache.set(key, result);
21
21
  return result;
22
22
  }
23
- /**
24
- * Create an empty selector variant
25
- */
26
23
  function emptyVariant() {
27
24
  return {
28
25
  modifierConditions: [],
29
26
  pseudoConditions: [],
30
- ownConditions: [],
27
+ selectorGroups: [],
28
+ ownGroups: [],
31
29
  mediaConditions: [],
32
30
  containerConditions: [],
33
31
  supportsConditions: [],
34
- rootConditions: [],
32
+ rootGroups: [],
35
33
  parentGroups: [],
36
34
  startingStyle: false
37
35
  };
@@ -66,30 +64,46 @@ function stateToCSS(state) {
66
64
  }),
67
65
  isImpossible: false
68
66
  };
69
- case "root": return innerConditionToVariants(state.innerCondition, state.negated ?? false, "rootConditions");
67
+ case "root": return innerConditionToVariants(state.innerCondition, state.negated ?? false, "rootGroups");
70
68
  case "parent": return parentConditionToVariants(state.innerCondition, state.negated ?? false, state.direct);
71
- case "own": return innerConditionToVariants(state.innerCondition, state.negated ?? false, "ownConditions");
72
- default: {
73
- const variant = emptyVariant();
74
- switch (state.type) {
75
- case "modifier":
76
- variant.modifierConditions.push(modifierToParsed(state));
77
- break;
78
- case "pseudo":
79
- variant.pseudoConditions.push(pseudoToParsed(state));
80
- break;
81
- case "container":
82
- variant.containerConditions.push(containerToParsed(state));
83
- break;
84
- case "supports":
85
- variant.supportsConditions.push(supportsToParsed(state));
86
- break;
87
- case "starting":
88
- variant.startingStyle = !state.negated;
89
- break;
90
- }
69
+ case "own": return innerConditionToVariants(state.innerCondition, state.negated ?? false, "ownGroups");
70
+ case "modifier": {
71
+ const v = emptyVariant();
72
+ v.modifierConditions.push(modifierToParsed(state));
73
+ return {
74
+ variants: [v],
75
+ isImpossible: false
76
+ };
77
+ }
78
+ case "pseudo": {
79
+ const v = emptyVariant();
80
+ v.pseudoConditions.push(pseudoToParsed(state));
81
+ return {
82
+ variants: [v],
83
+ isImpossible: false
84
+ };
85
+ }
86
+ case "container": {
87
+ const v = emptyVariant();
88
+ v.containerConditions.push(containerToParsed(state));
91
89
  return {
92
- variants: [variant],
90
+ variants: [v],
91
+ isImpossible: false
92
+ };
93
+ }
94
+ case "supports": {
95
+ const v = emptyVariant();
96
+ v.supportsConditions.push(supportsToParsed(state));
97
+ return {
98
+ variants: [v],
99
+ isImpossible: false
100
+ };
101
+ }
102
+ case "starting": {
103
+ const v = emptyVariant();
104
+ v.startingStyle = !state.negated;
105
+ return {
106
+ variants: [v],
93
107
  isImpossible: false
94
108
  };
95
109
  }
@@ -128,14 +142,31 @@ function pseudoToParsed(state) {
128
142
  };
129
143
  }
130
144
  /**
131
- * Convert parsed pseudo to CSS selector string (for final output)
145
+ * Convert parsed pseudo to CSS selector string (for final output).
146
+ *
147
+ * :not() is normalized to negated :is() at parse time, so pseudo.pseudo
148
+ * never starts with ':not(' here. When negated:
149
+ * - :is(X) → :not(X) (unwrap :is)
150
+ * - :where(X) → :not(X) (unwrap :where)
151
+ * - :has(X) → :not(:has(X))
152
+ * - other → :not(other)
153
+ *
154
+ * When not negated, single-argument :is()/:where() is unwrapped when the
155
+ * inner content is a simple compound selector that can safely append to
156
+ * the base selector (this happens after double-negation of :not()).
132
157
  */
133
158
  function pseudoToCSS(pseudo) {
159
+ const p = pseudo.pseudo;
134
160
  if (pseudo.negated) {
135
- if (pseudo.pseudo.startsWith(":not(")) return pseudo.pseudo.slice(5, -1);
136
- return `:not(${pseudo.pseudo})`;
161
+ if (p.startsWith(":is(") || p.startsWith(":where(")) return `:not(${p.slice(p.indexOf("(") + 1, -1)})`;
162
+ return `:not(${p})`;
137
163
  }
138
- return pseudo.pseudo;
164
+ if ((p.startsWith(":is(") || p.startsWith(":where(")) && !p.includes(",")) {
165
+ const inner = p.slice(p.indexOf("(") + 1, -1);
166
+ const ch = inner[0];
167
+ if ((ch === ":" || ch === "." || ch === "[" || ch === "#") && !/\s/.test(inner)) return inner;
168
+ }
169
+ return p;
139
170
  }
140
171
  /**
141
172
  * Convert media condition to parsed structure(s)
@@ -228,38 +259,56 @@ function supportsToParsed(state) {
228
259
  };
229
260
  }
230
261
  /**
231
- * Convert an inner condition tree into SelectorVariants.
232
- * Each inner OR branch becomes a separate variant, preserving disjunction.
262
+ * Collect all modifier and pseudo conditions from a variant as a flat array.
263
+ */
264
+ function collectSelectorConditions(variant) {
265
+ return [...variant.modifierConditions, ...variant.pseudoConditions];
266
+ }
267
+ /**
268
+ * Convert an inner condition tree into a single SelectorVariant with
269
+ * one SelectorGroup whose branches represent the inner OR alternatives.
233
270
  * Shared by @root() and @own().
271
+ *
272
+ * Both positive and negated cases produce one variant with one group.
273
+ * Negation simply sets the `negated` flag, which swaps :is() for :not()
274
+ * in the final CSS output — no De Morgan transformation is needed.
275
+ *
276
+ * This mirrors parentConditionToVariants: OR branches are kept inside
277
+ * a single group and rendered as comma-separated arguments in
278
+ * :is()/:not(), e.g. :root:is([a], [b]) or [el]:not([a], [b]).
234
279
  */
235
280
  function innerConditionToVariants(innerCondition, negated, target) {
236
- const innerCSS = conditionToCSS(negated ? not(innerCondition) : innerCondition);
281
+ const innerCSS = conditionToCSS(innerCondition);
237
282
  if (innerCSS.isImpossible || innerCSS.variants.length === 0) return {
238
283
  variants: [],
239
284
  isImpossible: true
240
285
  };
241
- const variants = [];
286
+ const branches = [];
242
287
  for (const innerVariant of innerCSS.variants) {
243
- const v = emptyVariant();
244
- for (const mod of innerVariant.modifierConditions) v[target].push(mod);
245
- for (const pseudo of innerVariant.pseudoConditions) v[target].push(pseudo);
246
- if (v[target].length > 0) variants.push(v);
288
+ const conditions = collectSelectorConditions(innerVariant);
289
+ if (conditions.length > 0) branches.push(conditions);
247
290
  }
248
- if (variants.length === 0) return {
291
+ if (branches.length === 0) return {
249
292
  variants: [emptyVariant()],
250
293
  isImpossible: false
251
294
  };
295
+ const v = emptyVariant();
296
+ v[target].push({
297
+ branches,
298
+ negated
299
+ });
252
300
  return {
253
- variants,
301
+ variants: [v],
254
302
  isImpossible: false
255
303
  };
256
304
  }
257
305
  /**
258
- * Convert a @parent() inner condition into SelectorVariants with ParentGroups.
306
+ * Convert a @parent() inner condition into a single SelectorVariant with
307
+ * one ParentGroup whose branches represent the inner OR alternatives.
259
308
  *
260
- * Positive: each inner OR branch becomes a separate variant with one :is() group.
261
- * Negated: !(A | B) = !A & !B — all branches become :not() groups collected
262
- * into a single variant so they produce :not([a] *):not([b] *) on one element.
309
+ * Both positive and negated cases produce one variant with one group.
310
+ * Negation simply sets the `negated` flag, which swaps :is() for :not()
311
+ * in the final CSS output — no structural transformation is needed.
263
312
  */
264
313
  function parentConditionToVariants(innerCondition, negated, direct) {
265
314
  const innerCSS = conditionToCSS(innerCondition);
@@ -267,72 +316,178 @@ function parentConditionToVariants(innerCondition, negated, direct) {
267
316
  variants: [],
268
317
  isImpossible: true
269
318
  };
270
- if (negated) {
271
- const v = emptyVariant();
272
- for (const innerVariant of innerCSS.variants) {
273
- const group = {
274
- conditions: [],
275
- direct,
276
- negated: true
277
- };
278
- for (const mod of innerVariant.modifierConditions) group.conditions.push(mod);
279
- for (const pseudo of innerVariant.pseudoConditions) group.conditions.push(pseudo);
280
- if (group.conditions.length > 0) v.parentGroups.push(group);
281
- }
282
- if (v.parentGroups.length === 0) return {
283
- variants: [emptyVariant()],
284
- isImpossible: false
285
- };
286
- return {
287
- variants: [v],
288
- isImpossible: false
289
- };
290
- }
291
- const variants = [];
319
+ const branches = [];
292
320
  for (const innerVariant of innerCSS.variants) {
293
- const v = emptyVariant();
294
- const group = {
295
- conditions: [],
296
- direct,
297
- negated: false
298
- };
299
- for (const mod of innerVariant.modifierConditions) group.conditions.push(mod);
300
- for (const pseudo of innerVariant.pseudoConditions) group.conditions.push(pseudo);
301
- if (group.conditions.length > 0) {
302
- v.parentGroups.push(group);
303
- variants.push(v);
304
- }
321
+ const conditions = collectSelectorConditions(innerVariant);
322
+ if (conditions.length > 0) branches.push(conditions);
305
323
  }
306
- if (variants.length === 0) return {
324
+ if (branches.length === 0) return {
307
325
  variants: [emptyVariant()],
308
326
  isImpossible: false
309
327
  };
328
+ const v = emptyVariant();
329
+ v.parentGroups.push({
330
+ branches,
331
+ direct,
332
+ negated
333
+ });
310
334
  return {
311
- variants,
335
+ variants: [v],
312
336
  isImpossible: false
313
337
  };
314
338
  }
315
339
  /**
316
- * Convert parsed root conditions to CSS selector prefix (for final output)
340
+ * Sort key for canonical condition output within selectors.
341
+ *
342
+ * Priority order:
343
+ * 0: Boolean attribute selectors ([data-hovered])
344
+ * 1: Value attribute selectors ([data-size="small"])
345
+ * 2: Negated boolean attributes (:not([data-disabled]))
346
+ * 3: Negated value attributes (:not([data-size="small"]))
347
+ * 4: Pseudo-classes (:hover, :focus)
348
+ * 5: Negated pseudo-classes (:not(:disabled))
349
+ *
350
+ * Secondary sort: alphabetical by attribute name / pseudo string.
317
351
  */
318
- function rootConditionsToCSS(roots) {
319
- if (roots.length === 0) return void 0;
352
+ function conditionSortKey(cond) {
353
+ if ("attribute" in cond) {
354
+ const hasValue = cond.value !== void 0 ? 1 : 0;
355
+ return `${(cond.negated ? 2 : 0) + hasValue}|${cond.attribute}|${cond.value ?? ""}`;
356
+ }
357
+ return `${cond.negated ? 5 : 4}|${cond.pseudo}`;
358
+ }
359
+ function sortConditions(conditions) {
360
+ return conditions.toSorted((a, b) => conditionSortKey(a).localeCompare(conditionSortKey(b)));
361
+ }
362
+ function branchToCSS(branch) {
363
+ let parts = "";
364
+ for (const cond of sortConditions(branch)) parts += selectorConditionToCSS(cond);
365
+ return parts;
366
+ }
367
+ /**
368
+ * Wrap serialized selector arguments in :is() or :not().
369
+ * Arguments are sorted for canonical output.
370
+ */
371
+ function wrapInIsOrNot(args, negated) {
372
+ return `${negated ? ":not" : ":is"}(${args.sort().join(", ")})`;
373
+ }
374
+ /**
375
+ * Convert a selector group to a CSS selector fragment.
376
+ *
377
+ * Single-branch groups are unwrapped (no :is() wrapper).
378
+ * Multi-branch groups use :is() or :not().
379
+ * Negation swaps :is() for :not().
380
+ */
381
+ function selectorGroupToCSS(group) {
382
+ if (group.branches.length === 0) return "";
383
+ if (group.branches.length === 1) {
384
+ const parts = branchToCSS(group.branches[0]);
385
+ if (group.negated) return `:not(${parts})`;
386
+ return parts;
387
+ }
388
+ return wrapInIsOrNot(group.branches.map(branchToCSS), group.negated);
389
+ }
390
+ /**
391
+ * Collect facts about modifier conditions for subsumption analysis.
392
+ * Tracks negated boolean attrs (:not([attr])) and positive exact values ([attr="X"]).
393
+ */
394
+ function collectSubsumptionFacts(modifiers) {
395
+ const negatedBooleanAttrs = /* @__PURE__ */ new Set();
396
+ const positiveExactValuesByAttr = /* @__PURE__ */ new Map();
397
+ for (const mod of modifiers) {
398
+ if (mod.negated && mod.value === void 0) negatedBooleanAttrs.add(mod.attribute);
399
+ if (!mod.negated && mod.value !== void 0 && (mod.operator ?? "=") === "=") {
400
+ let vals = positiveExactValuesByAttr.get(mod.attribute);
401
+ if (!vals) {
402
+ vals = /* @__PURE__ */ new Set();
403
+ positiveExactValuesByAttr.set(mod.attribute, vals);
404
+ }
405
+ vals.add(mod.value);
406
+ }
407
+ }
408
+ return {
409
+ negatedBooleanAttrs,
410
+ positiveExactValuesByAttr
411
+ };
412
+ }
413
+ /**
414
+ * Check if a negated-value modifier is subsumed by stronger facts:
415
+ * - :not([attr]) subsumes :not([attr="val"])
416
+ * - [attr="X"] implies :not([attr="Y"]) is redundant (single exact value)
417
+ *
418
+ * Only applies to exact-match (=) operators; substring operators don't
419
+ * imply exclusivity between values.
420
+ */
421
+ function isSubsumedNegatedModifier(mod, facts) {
422
+ if (!mod.negated || mod.value === void 0) return false;
423
+ if (facts.negatedBooleanAttrs.has(mod.attribute)) return true;
424
+ if ((mod.operator ?? "=") === "=") {
425
+ const posVals = facts.positiveExactValuesByAttr.get(mod.attribute);
426
+ if (posVals && posVals.size === 1 && !posVals.has(mod.value)) return true;
427
+ }
428
+ return false;
429
+ }
430
+ /**
431
+ * Remove redundant single-condition groups that are subsumed by stronger
432
+ * groups on the same attribute. O(n) — only inspects single-branch,
433
+ * single-condition groups.
434
+ */
435
+ function optimizeGroups(groups) {
436
+ if (groups.length <= 1) return groups;
437
+ const seen = /* @__PURE__ */ new Set();
438
+ const result = [];
439
+ for (const g of groups) {
440
+ const key = getSelectorGroupKey(g);
441
+ if (!seen.has(key)) {
442
+ seen.add(key);
443
+ result.push(g);
444
+ }
445
+ }
446
+ if (result.length <= 1) return result;
447
+ const effectiveModifiers = [];
448
+ for (const g of result) {
449
+ if (g.branches.length !== 1 || g.branches[0].length !== 1) continue;
450
+ const cond = g.branches[0][0];
451
+ if (!("attribute" in cond)) continue;
452
+ effectiveModifiers.push({
453
+ ...cond,
454
+ negated: g.negated !== cond.negated
455
+ });
456
+ }
457
+ const facts = collectSubsumptionFacts(effectiveModifiers);
458
+ if (facts.negatedBooleanAttrs.size === 0 && facts.positiveExactValuesByAttr.size === 0) return result;
459
+ return result.filter((g) => {
460
+ if (g.branches.length !== 1 || g.branches[0].length !== 1) return true;
461
+ const cond = g.branches[0][0];
462
+ if (!("attribute" in cond) || !g.negated || cond.negated || cond.value === void 0) return true;
463
+ return !isSubsumedNegatedModifier({
464
+ ...cond,
465
+ negated: true
466
+ }, facts);
467
+ });
468
+ }
469
+ /**
470
+ * Convert root groups to CSS selector prefix (for final output)
471
+ */
472
+ function rootGroupsToCSS(groups) {
473
+ if (groups.length === 0) return void 0;
474
+ const optimized = optimizeGroups(groups);
475
+ if (optimized.length === 0) return void 0;
320
476
  let prefix = ":root";
321
- for (const cond of roots) prefix += selectorConditionToCSS(cond);
477
+ for (const group of optimized) prefix += selectorGroupToCSS(group);
322
478
  return prefix;
323
479
  }
324
480
  /**
325
481
  * Convert parent groups to CSS selector fragments (for final output).
326
- * Each group produces its own :is() wrapper.
482
+ * Each group produces its own :is()/:not() wrapper with a combinator
483
+ * suffix (` *` or ` > *`) appended to each branch.
327
484
  */
328
485
  function parentGroupsToCSS(groups) {
329
486
  let result = "";
330
487
  for (const group of groups) {
331
488
  const combinator = group.direct ? " > *" : " *";
332
- let parts = "";
333
- for (const cond of group.conditions) parts += selectorConditionToCSS(cond);
334
- const wrapper = group.negated ? ":not" : ":is";
335
- result += `${wrapper}(${parts}${combinator})`;
489
+ const args = group.branches.map((branch) => branchToCSS(branch) + combinator);
490
+ result += wrapInIsOrNot(args, group.negated);
336
491
  }
337
492
  return result;
338
493
  }
@@ -368,7 +523,7 @@ function getSelectorConditionKey(cond) {
368
523
  */
369
524
  function dedupeSelectorConditions(conditions) {
370
525
  const seen = /* @__PURE__ */ new Set();
371
- let result = [];
526
+ const result = [];
372
527
  for (const c of conditions) {
373
528
  const key = getSelectorConditionKey(c);
374
529
  if (!seen.has(key)) {
@@ -376,28 +531,12 @@ function dedupeSelectorConditions(conditions) {
376
531
  result.push(c);
377
532
  }
378
533
  }
379
- const negatedBooleanAttrs = /* @__PURE__ */ new Set();
380
- const positiveValuesByAttr = /* @__PURE__ */ new Map();
381
- for (const c of result) {
382
- if (!("attribute" in c)) continue;
383
- if (c.negated && c.value === void 0) negatedBooleanAttrs.add(c.attribute);
384
- if (!c.negated && c.value !== void 0) {
385
- let values = positiveValuesByAttr.get(c.attribute);
386
- if (!values) {
387
- values = /* @__PURE__ */ new Set();
388
- positiveValuesByAttr.set(c.attribute, values);
389
- }
390
- values.add(c.value);
391
- }
392
- }
393
- result = result.filter((c) => {
394
- if (!("attribute" in c) || !c.negated || c.value === void 0) return true;
395
- if (negatedBooleanAttrs.has(c.attribute)) return false;
396
- const positiveValues = positiveValuesByAttr.get(c.attribute);
397
- if (positiveValues !== void 0 && positiveValues.size === 1 && !positiveValues.has(c.value)) return false;
398
- return true;
534
+ const facts = collectSubsumptionFacts(result.filter((c) => "attribute" in c));
535
+ if (facts.negatedBooleanAttrs.size === 0 && facts.positiveExactValuesByAttr.size === 0) return result;
536
+ return result.filter((c) => {
537
+ if (!("attribute" in c)) return true;
538
+ return !isSubsumedNegatedModifier(c, facts);
399
539
  });
400
- return result;
401
540
  }
402
541
  /**
403
542
  * Check for modifier contradiction: same attribute with opposite negation
@@ -440,10 +579,32 @@ function hasSelectorConditionContradiction(conditions) {
440
579
  * with opposite negation. E.g. :not([data-hovered] *) and :is([data-hovered] *)
441
580
  * in the same variant is impossible.
442
581
  */
582
+ function getBranchesKey(branches) {
583
+ if (branches.length === 1) {
584
+ const b = branches[0];
585
+ if (b.length === 1) return getSelectorConditionKey(b[0]);
586
+ return b.map(getSelectorConditionKey).sort().join("+");
587
+ }
588
+ return branches.map((b) => b.map(getSelectorConditionKey).sort().join("+")).sort().join(",");
589
+ }
443
590
  function hasParentGroupContradiction(groups) {
444
591
  const byBaseKey = /* @__PURE__ */ new Map();
445
592
  for (const g of groups) {
446
- const baseKey = `${g.direct ? ">" : ""}(${g.conditions.map(getSelectorConditionKey).sort().join(",")})`;
593
+ const baseKey = `${g.direct ? ">" : ""}(${getBranchesKey(g.branches)})`;
594
+ const existing = byBaseKey.get(baseKey);
595
+ if (existing !== void 0 && existing !== !g.negated) return true;
596
+ byBaseKey.set(baseKey, !g.negated);
597
+ }
598
+ return false;
599
+ }
600
+ /**
601
+ * Check for selector group contradiction: same branches with opposite negation.
602
+ * E.g. :is([data-a]) and :not([data-a]) in the same variant is impossible.
603
+ */
604
+ function hasSelectorGroupContradiction(groups) {
605
+ const byBaseKey = /* @__PURE__ */ new Map();
606
+ for (const g of groups) {
607
+ const baseKey = getBranchesKey(g.branches);
447
608
  const existing = byBaseKey.get(baseKey);
448
609
  if (existing !== void 0 && existing !== !g.negated) return true;
449
610
  byBaseKey.set(baseKey, !g.negated);
@@ -457,21 +618,17 @@ function hasParentGroupContradiction(groups) {
457
618
  function mergeVariants(a, b) {
458
619
  const mergedMedia = dedupeMediaConditions([...a.mediaConditions, ...b.mediaConditions]);
459
620
  if (hasMediaContradiction(mergedMedia)) return null;
460
- const mergedRoots = dedupeSelectorConditions([...a.rootConditions, ...b.rootConditions]);
461
- if (hasSelectorConditionContradiction(mergedRoots)) return null;
462
- const mergedSelectorConditions = dedupeSelectorConditions([
463
- ...a.modifierConditions,
464
- ...b.modifierConditions,
465
- ...a.pseudoConditions,
466
- ...b.pseudoConditions
467
- ]);
468
- if (hasSelectorConditionContradiction(mergedSelectorConditions)) return null;
469
- const mergedModifiers = mergedSelectorConditions.filter((c) => "attribute" in c);
470
- const mergedPseudos = mergedSelectorConditions.filter((c) => !("attribute" in c));
621
+ const mergedRootGroups = optimizeGroups([...a.rootGroups, ...b.rootGroups]);
622
+ if (hasSelectorGroupContradiction(mergedRootGroups)) return null;
623
+ const mergedModifiers = dedupeSelectorConditions([...a.modifierConditions, ...b.modifierConditions]);
624
+ const mergedPseudos = dedupeSelectorConditions([...a.pseudoConditions, ...b.pseudoConditions]);
625
+ if (hasSelectorConditionContradiction([...mergedModifiers, ...mergedPseudos])) return null;
626
+ const mergedSelectorGroups = optimizeGroups([...a.selectorGroups, ...b.selectorGroups]);
627
+ if (hasSelectorGroupContradiction(mergedSelectorGroups)) return null;
471
628
  const mergedParentGroups = [...a.parentGroups, ...b.parentGroups];
472
629
  if (hasParentGroupContradiction(mergedParentGroups)) return null;
473
- const mergedOwn = dedupeSelectorConditions([...a.ownConditions, ...b.ownConditions]);
474
- if (hasSelectorConditionContradiction(mergedOwn)) return null;
630
+ const mergedOwnGroups = optimizeGroups([...a.ownGroups, ...b.ownGroups]);
631
+ if (hasSelectorGroupContradiction(mergedOwnGroups)) return null;
475
632
  const mergedContainers = dedupeContainerConditions([...a.containerConditions, ...b.containerConditions]);
476
633
  if (hasContainerStyleContradiction(mergedContainers)) return null;
477
634
  const mergedSupports = dedupeSupportsConditions([...a.supportsConditions, ...b.supportsConditions]);
@@ -479,59 +636,40 @@ function mergeVariants(a, b) {
479
636
  return {
480
637
  modifierConditions: mergedModifiers,
481
638
  pseudoConditions: mergedPseudos,
482
- ownConditions: mergedOwn,
639
+ selectorGroups: mergedSelectorGroups,
640
+ ownGroups: mergedOwnGroups,
483
641
  mediaConditions: mergedMedia,
484
642
  containerConditions: mergedContainers,
485
643
  supportsConditions: mergedSupports,
486
- rootConditions: mergedRoots,
644
+ rootGroups: mergedRootGroups,
487
645
  parentGroups: mergedParentGroups,
488
646
  startingStyle: a.startingStyle || b.startingStyle
489
647
  };
490
648
  }
491
649
  /**
492
- * Deduplicate media conditions by their key (subtype + condition + negated)
650
+ * Generic deduplication by a key extraction function.
651
+ * Preserves insertion order, keeping the first occurrence of each key.
493
652
  */
494
- function dedupeMediaConditions(conditions) {
653
+ function dedupeByKey(items, getKey) {
495
654
  const seen = /* @__PURE__ */ new Set();
496
655
  const result = [];
497
- for (const c of conditions) {
498
- const key = `${c.subtype}|${c.condition}|${c.negated}`;
656
+ for (const item of items) {
657
+ const key = getKey(item);
499
658
  if (!seen.has(key)) {
500
659
  seen.add(key);
501
- result.push(c);
660
+ result.push(item);
502
661
  }
503
662
  }
504
663
  return result;
505
664
  }
506
- /**
507
- * Deduplicate container conditions by their key (name + condition + negated)
508
- */
665
+ function dedupeMediaConditions(conditions) {
666
+ return dedupeByKey(conditions, (c) => `${c.subtype}|${c.condition}|${c.negated}`);
667
+ }
509
668
  function dedupeContainerConditions(conditions) {
510
- const seen = /* @__PURE__ */ new Set();
511
- const result = [];
512
- for (const c of conditions) {
513
- const key = `${c.name ?? ""}|${c.condition}|${c.negated}`;
514
- if (!seen.has(key)) {
515
- seen.add(key);
516
- result.push(c);
517
- }
518
- }
519
- return result;
669
+ return dedupeByKey(conditions, (c) => `${c.name ?? ""}|${c.condition}|${c.negated}`);
520
670
  }
521
- /**
522
- * Deduplicate supports conditions by their key (subtype + condition + negated)
523
- */
524
671
  function dedupeSupportsConditions(conditions) {
525
- const seen = /* @__PURE__ */ new Set();
526
- const result = [];
527
- for (const c of conditions) {
528
- const key = `${c.subtype}|${c.condition}|${c.negated}`;
529
- if (!seen.has(key)) {
530
- seen.add(key);
531
- result.push(c);
532
- }
533
- }
534
- return result;
672
+ return dedupeByKey(conditions, (c) => `${c.subtype}|${c.condition}|${c.negated}`);
535
673
  }
536
674
  /**
537
675
  * Check if supports conditions contain contradictions
@@ -628,26 +766,50 @@ function hasContainerStyleContradiction(conditions) {
628
766
  }
629
767
  return false;
630
768
  }
769
+ const variantKeyCache = /* @__PURE__ */ new WeakMap();
631
770
  /**
632
- * Get a unique key for a variant (for deduplication)
771
+ * Get a unique key for a variant (for deduplication).
772
+ * Cached via WeakMap since variants are compared multiple times during
773
+ * deduplication and sorting.
633
774
  */
634
- function getVariantKey(v) {
635
- const modifierKey = v.modifierConditions.map(getModifierKey).sort().join("|");
636
- const pseudoKey = v.pseudoConditions.map(getPseudoKey).sort().join("|");
637
- const ownKey = v.ownConditions.map(getSelectorConditionKey).sort().join("|");
638
- const containerKey = v.containerConditions.map((c) => `${c.name ?? ""}:${c.negated ? "!" : ""}${c.condition}`).sort().join("|");
775
+ function getSelectorGroupKey(g) {
776
+ return `${g.negated ? "!" : ""}(${getBranchesKey(g.branches)})`;
777
+ }
778
+ /**
779
+ * Get a context key for a variant — everything except flat modifier/pseudo
780
+ * conditions. Variants with the same context key can be merged into an
781
+ * :is() group. Also used by getVariantKey as the shared non-selector portion.
782
+ */
783
+ function getVariantContextKey(v) {
639
784
  return [
640
- modifierKey,
641
- pseudoKey,
642
- ownKey,
643
785
  v.mediaConditions.map((c) => `${c.subtype}:${c.negated ? "!" : ""}${c.condition}`).sort().join("|"),
644
- containerKey,
786
+ v.containerConditions.map((c) => `${c.name ?? ""}:${c.negated ? "!" : ""}${c.condition}`).sort().join("|"),
645
787
  v.supportsConditions.map((c) => `${c.subtype}:${c.negated ? "!" : ""}${c.condition}`).sort().join("|"),
646
- v.rootConditions.map(getSelectorConditionKey).sort().join("|"),
647
- v.parentGroups.map((g) => `${g.direct ? ">" : ""}(${g.conditions.map(getSelectorConditionKey).sort().join(",")})`).sort().join("|"),
788
+ v.rootGroups.map(getSelectorGroupKey).sort().join("|"),
789
+ v.parentGroups.map(getParentGroupKey).sort().join("|"),
790
+ v.ownGroups.map(getSelectorGroupKey).sort().join("|"),
791
+ v.selectorGroups.map(getSelectorGroupKey).sort().join("|"),
648
792
  v.startingStyle ? "1" : "0"
649
793
  ].join("###");
650
794
  }
795
+ function getVariantKey(v) {
796
+ const cached = variantKeyCache.get(v);
797
+ if (cached !== void 0) return cached;
798
+ const modifierKey = v.modifierConditions.map(getModifierKey).sort().join("|");
799
+ const pseudoKey = v.pseudoConditions.map(getPseudoKey).sort().join("|");
800
+ const key = modifierKey + "###" + pseudoKey + "###" + getVariantContextKey(v);
801
+ variantKeyCache.set(v, key);
802
+ return key;
803
+ }
804
+ /**
805
+ * Total number of leaf conditions in a variant (for superset / dedup comparisons).
806
+ */
807
+ function groupConditionCount(groups) {
808
+ return groups.reduce((sum, g) => sum + g.branches.reduce((s, b) => s + b.length, 0), 0);
809
+ }
810
+ function variantConditionCount(v) {
811
+ return v.modifierConditions.length + v.pseudoConditions.length + groupConditionCount(v.selectorGroups) + groupConditionCount(v.ownGroups) + v.mediaConditions.length + v.containerConditions.length + v.supportsConditions.length + groupConditionCount(v.rootGroups) + groupConditionCount(v.parentGroups);
812
+ }
651
813
  /**
652
814
  * Check if variant A is a superset of variant B (A is more restrictive)
653
815
  *
@@ -661,74 +823,42 @@ function getVariantKey(v) {
661
823
  */
662
824
  function isVariantSuperset(a, b) {
663
825
  if (a.startingStyle !== b.startingStyle) return false;
664
- if (!isSelectorConditionsSuperset(a.rootConditions, b.rootConditions)) return false;
826
+ if (!isSelectorGroupsSuperset(a.rootGroups, b.rootGroups)) return false;
665
827
  if (!isMediaConditionsSuperset(a.mediaConditions, b.mediaConditions)) return false;
666
828
  if (!isContainerConditionsSuperset(a.containerConditions, b.containerConditions)) return false;
667
829
  if (!isSupportsConditionsSuperset(a.supportsConditions, b.supportsConditions)) return false;
668
830
  if (!isModifierConditionsSuperset(a.modifierConditions, b.modifierConditions)) return false;
669
831
  if (!isPseudoConditionsSuperset(a.pseudoConditions, b.pseudoConditions)) return false;
670
- if (!isSelectorConditionsSuperset(a.ownConditions, b.ownConditions)) return false;
832
+ if (!isSelectorGroupsSuperset(a.selectorGroups, b.selectorGroups)) return false;
833
+ if (!isSelectorGroupsSuperset(a.ownGroups, b.ownGroups)) return false;
671
834
  if (!isParentGroupsSuperset(a.parentGroups, b.parentGroups)) return false;
672
- const parentConditionCount = (groups) => groups.reduce((sum, g) => sum + g.conditions.length, 0);
673
- return a.mediaConditions.length + a.containerConditions.length + a.supportsConditions.length + a.modifierConditions.length + a.pseudoConditions.length + a.rootConditions.length + parentConditionCount(a.parentGroups) + a.ownConditions.length > b.mediaConditions.length + b.containerConditions.length + b.supportsConditions.length + b.modifierConditions.length + b.pseudoConditions.length + b.rootConditions.length + parentConditionCount(b.parentGroups) + b.ownConditions.length;
835
+ return variantConditionCount(a) > variantConditionCount(b);
674
836
  }
675
837
  /**
676
- * Check if media conditions A is a superset of B
838
+ * Generic superset check: true if every item in B has a matching key in A.
677
839
  */
840
+ function isConditionsSuperset(a, b, getKey) {
841
+ const aKeys = new Set(a.map(getKey));
842
+ return b.every((c) => aKeys.has(getKey(c)));
843
+ }
678
844
  function isMediaConditionsSuperset(a, b) {
679
- const aKeys = new Set(a.map((c) => `${c.subtype}|${c.condition}|${c.negated}`));
680
- for (const c of b) {
681
- const key = `${c.subtype}|${c.condition}|${c.negated}`;
682
- if (!aKeys.has(key)) return false;
683
- }
684
- return true;
845
+ return isConditionsSuperset(a, b, (c) => `${c.subtype}|${c.condition}|${c.negated}`);
685
846
  }
686
- /**
687
- * Check if container conditions A is a superset of B
688
- */
689
847
  function isContainerConditionsSuperset(a, b) {
690
- const aKeys = new Set(a.map((c) => `${c.name ?? ""}|${c.condition}|${c.negated}`));
691
- for (const c of b) {
692
- const key = `${c.name ?? ""}|${c.condition}|${c.negated}`;
693
- if (!aKeys.has(key)) return false;
694
- }
695
- return true;
848
+ return isConditionsSuperset(a, b, (c) => `${c.name ?? ""}|${c.condition}|${c.negated}`);
696
849
  }
697
- /**
698
- * Check if supports conditions A is a superset of B
699
- */
700
850
  function isSupportsConditionsSuperset(a, b) {
701
- const aKeys = new Set(a.map((c) => `${c.subtype}|${c.condition}|${c.negated}`));
702
- for (const c of b) {
703
- const key = `${c.subtype}|${c.condition}|${c.negated}`;
704
- if (!aKeys.has(key)) return false;
705
- }
706
- return true;
851
+ return isConditionsSuperset(a, b, (c) => `${c.subtype}|${c.condition}|${c.negated}`);
707
852
  }
708
- /**
709
- * Check if modifier conditions A is a superset of B
710
- */
711
853
  function isModifierConditionsSuperset(a, b) {
712
- const aKeys = new Set(a.map(getModifierKey));
713
- for (const c of b) if (!aKeys.has(getModifierKey(c))) return false;
714
- return true;
854
+ return isConditionsSuperset(a, b, getModifierKey);
715
855
  }
716
- /**
717
- * Check if pseudo conditions A is a superset of B
718
- */
719
856
  function isPseudoConditionsSuperset(a, b) {
720
- const aKeys = new Set(a.map(getPseudoKey));
721
- for (const c of b) if (!aKeys.has(getPseudoKey(c))) return false;
722
- return true;
857
+ return isConditionsSuperset(a, b, getPseudoKey);
723
858
  }
724
- /**
725
- * Check if selector conditions A is a superset of B.
726
- * Shared by root and own conditions.
727
- */
728
- function isSelectorConditionsSuperset(a, b) {
729
- const aKeys = new Set(a.map(getSelectorConditionKey));
730
- for (const c of b) if (!aKeys.has(getSelectorConditionKey(c))) return false;
731
- return true;
859
+ function isSelectorGroupsSuperset(a, b) {
860
+ if (a.length < b.length) return false;
861
+ return isConditionsSuperset(a, b, getSelectorGroupKey);
732
862
  }
733
863
  /**
734
864
  * Check if parent groups A is a superset of B.
@@ -736,12 +866,10 @@ function isSelectorConditionsSuperset(a, b) {
736
866
  */
737
867
  function isParentGroupsSuperset(a, b) {
738
868
  if (a.length < b.length) return false;
739
- const aKeys = new Set(a.map(getParentGroupKey));
740
- for (const g of b) if (!aKeys.has(getParentGroupKey(g))) return false;
741
- return true;
869
+ return isConditionsSuperset(a, b, getParentGroupKey);
742
870
  }
743
871
  function getParentGroupKey(g) {
744
- return `${g.negated ? "!" : ""}${g.direct ? ">" : ""}(${g.conditions.map(getSelectorConditionKey).sort().join(",")})`;
872
+ return `${g.negated ? "!" : ""}${g.direct ? ">" : ""}(${getBranchesKey(g.branches)})`;
745
873
  }
746
874
  /**
747
875
  * Deduplicate variants
@@ -751,6 +879,7 @@ function getParentGroupKey(g) {
751
879
  * 2. Superset variants (more restrictive selectors that are redundant)
752
880
  */
753
881
  function dedupeVariants(variants) {
882
+ if (variants.length <= 1) return variants;
754
883
  const seen = /* @__PURE__ */ new Set();
755
884
  const result = [];
756
885
  for (const v of variants) {
@@ -760,7 +889,7 @@ function dedupeVariants(variants) {
760
889
  result.push(v);
761
890
  }
762
891
  }
763
- const variantConditionCount = (v) => v.modifierConditions.length + v.pseudoConditions.length + v.ownConditions.length + v.mediaConditions.length + v.containerConditions.length + v.supportsConditions.length + v.rootConditions.length + v.parentGroups.reduce((sum, g) => sum + g.conditions.length, 0);
892
+ if (result.length <= 1) return result;
764
893
  result.sort((a, b) => variantConditionCount(a) - variantConditionCount(b));
765
894
  const filtered = [];
766
895
  for (const candidate of result) {
@@ -810,8 +939,8 @@ function andToCSS(children) {
810
939
  * Combine OR conditions into CSS
811
940
  *
812
941
  * OR in CSS means multiple selector variants (DNF).
813
- * Each variant becomes a separate selector in the comma-separated list,
814
- * or multiple CSS rules if they have different at-rules.
942
+ * After deduplication, variants that differ only in their base
943
+ * modifier/pseudo conditions are merged into :is() groups.
815
944
  *
816
945
  * Note: OR exclusivity is handled at the pipeline level (expandOrConditions),
817
946
  * so here we just collect all variants. Any remaining ORs in the condition
@@ -834,17 +963,101 @@ function orToCSS(children) {
834
963
  };
835
964
  }
836
965
  /**
837
- * Get a cache key for a condition
966
+ * Find keys present in ALL condition arrays.
838
967
  */
839
- function getConditionKey(node) {
840
- if (node.kind === "true") return "TRUE";
841
- if (node.kind === "false") return "FALSE";
842
- if (node.kind === "state") return node.uniqueId;
843
- if (node.kind === "compound") {
844
- const childKeys = node.children.map(getConditionKey).sort();
845
- return `${node.operator}(${childKeys.join(",")})`;
968
+ function findCommonKeys(conditionSets, getKey) {
969
+ if (conditionSets.length === 0) return /* @__PURE__ */ new Set();
970
+ const common = new Set(conditionSets[0].map(getKey));
971
+ for (let i = 1; i < conditionSets.length; i++) {
972
+ const keys = new Set(conditionSets[i].map(getKey));
973
+ for (const key of common) if (!keys.has(key)) common.delete(key);
846
974
  }
847
- return "UNKNOWN";
975
+ return common;
976
+ }
977
+ /**
978
+ * Merge OR variants that share the same "context" (at-rules, root, parent,
979
+ * own, starting) into a single variant with a SelectorGroup.
980
+ *
981
+ * Variants with no modifier/pseudo conditions are kept separate (they match
982
+ * unconditionally and can't be expressed inside :is()).
983
+ */
984
+ function mergeVariantsIntoSelectorGroups(variants) {
985
+ if (variants.length <= 1) return variants;
986
+ const groups = /* @__PURE__ */ new Map();
987
+ for (const v of variants) {
988
+ const key = getVariantContextKey(v);
989
+ const group = groups.get(key);
990
+ if (group) group.push(v);
991
+ else groups.set(key, [v]);
992
+ }
993
+ const result = [];
994
+ for (const group of groups.values()) {
995
+ if (group.length === 1) {
996
+ result.push(group[0]);
997
+ continue;
998
+ }
999
+ const withSelectors = [];
1000
+ const withoutSelectors = [];
1001
+ for (const v of group) if (v.modifierConditions.length === 0 && v.pseudoConditions.length === 0) withoutSelectors.push(v);
1002
+ else withSelectors.push(v);
1003
+ result.push(...withoutSelectors);
1004
+ if (withSelectors.length <= 1) {
1005
+ result.push(...withSelectors);
1006
+ continue;
1007
+ }
1008
+ result.push(factorAndGroup(withSelectors));
1009
+ }
1010
+ return result;
1011
+ }
1012
+ /**
1013
+ * Factor common modifier/pseudo conditions out of variants and create
1014
+ * a single variant with a SelectorGroup for the remaining (differing)
1015
+ * conditions.
1016
+ *
1017
+ * Precondition: all variants must share the same context key (identical
1018
+ * at-rules, root/parent/own/selector groups, startingStyle).
1019
+ */
1020
+ function factorAndGroup(variants) {
1021
+ {
1022
+ const key0 = getVariantContextKey(variants[0]);
1023
+ for (let i = 1; i < variants.length; i++) {
1024
+ const keyI = getVariantContextKey(variants[i]);
1025
+ if (keyI !== key0) throw new Error(`factorAndGroup: context key mismatch at index ${i}.\n expected: ${key0}\n got: ${keyI}`);
1026
+ }
1027
+ }
1028
+ const commonModKeys = findCommonKeys(variants.map((v) => v.modifierConditions), getModifierKey);
1029
+ const commonPseudoKeys = findCommonKeys(variants.map((v) => v.pseudoConditions), getPseudoKey);
1030
+ const commonModifiers = variants[0].modifierConditions.filter((m) => commonModKeys.has(getModifierKey(m)));
1031
+ const commonPseudos = variants[0].pseudoConditions.filter((p) => commonPseudoKeys.has(getPseudoKey(p)));
1032
+ const branches = [];
1033
+ let hasEmptyBranch = false;
1034
+ for (const v of variants) {
1035
+ const branch = [];
1036
+ for (const mod of v.modifierConditions) if (!commonModKeys.has(getModifierKey(mod))) branch.push(mod);
1037
+ for (const pseudo of v.pseudoConditions) if (!commonPseudoKeys.has(getPseudoKey(pseudo))) branch.push(pseudo);
1038
+ if (branch.length > 0) branches.push(branch);
1039
+ else hasEmptyBranch = true;
1040
+ }
1041
+ if (hasEmptyBranch) return {
1042
+ ...variants[0],
1043
+ modifierConditions: commonModifiers,
1044
+ pseudoConditions: commonPseudos
1045
+ };
1046
+ return {
1047
+ modifierConditions: commonModifiers,
1048
+ pseudoConditions: commonPseudos,
1049
+ selectorGroups: [...variants[0].selectorGroups, {
1050
+ branches,
1051
+ negated: false
1052
+ }],
1053
+ ownGroups: [...variants[0].ownGroups],
1054
+ mediaConditions: [...variants[0].mediaConditions],
1055
+ containerConditions: [...variants[0].containerConditions],
1056
+ supportsConditions: [...variants[0].supportsConditions],
1057
+ rootGroups: [...variants[0].rootGroups],
1058
+ parentGroups: [...variants[0].parentGroups],
1059
+ startingStyle: variants[0].startingStyle
1060
+ };
848
1061
  }
849
1062
  /**
850
1063
  * Build at-rules array from a variant
@@ -883,10 +1096,9 @@ function buildAtRulesFromVariant(variant) {
883
1096
  });
884
1097
  atRules.push(`@supports ${conditionParts.join(" and ")}`);
885
1098
  }
886
- if (variant.startingStyle) atRules.push("@starting-style");
887
1099
  return atRules;
888
1100
  }
889
1101
 
890
1102
  //#endregion
891
- export { buildAtRulesFromVariant, conditionToCSS, modifierToCSS, parentGroupsToCSS, pseudoToCSS, rootConditionsToCSS, selectorConditionToCSS };
1103
+ export { branchToCSS, buildAtRulesFromVariant, conditionToCSS, mergeVariantsIntoSelectorGroups, optimizeGroups, parentGroupsToCSS, rootGroupsToCSS, selectorGroupToCSS };
892
1104
  //# sourceMappingURL=materialize.js.map