@tenphi/tasty 0.0.0-snapshot.c11c8b8 → 0.0.0-snapshot.fafcbfe

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.
@@ -1,4 +1,5 @@
1
1
  import { Lru } from "../parser/lru.js";
2
+ import { not } from "./conditions.js";
2
3
 
3
4
  //#region src/pipeline/materialize.ts
4
5
  /**
@@ -31,8 +32,7 @@ function emptyVariant() {
31
32
  containerConditions: [],
32
33
  supportsConditions: [],
33
34
  rootConditions: [],
34
- parentConditions: [],
35
- parentDirect: false,
35
+ parentGroups: [],
36
36
  startingStyle: false
37
37
  };
38
38
  }
@@ -66,37 +66,46 @@ function stateToCSS(state) {
66
66
  }),
67
67
  isImpossible: false
68
68
  };
69
- default: {
70
- const variant = emptyVariant();
71
- switch (state.type) {
72
- case "modifier":
73
- variant.modifierConditions.push(modifierToParsed(state));
74
- break;
75
- case "pseudo":
76
- variant.pseudoConditions.push(pseudoToParsed(state));
77
- break;
78
- case "container":
79
- variant.containerConditions.push(containerToParsed(state));
80
- break;
81
- case "supports":
82
- variant.supportsConditions.push(supportsToParsed(state));
83
- break;
84
- case "root":
85
- variant.rootConditions.push(...extractInnerConditions(state.innerCondition, state.negated ?? false));
86
- break;
87
- case "parent":
88
- variant.parentConditions.push(...extractInnerConditions(state.innerCondition, state.negated ?? false));
89
- variant.parentDirect = state.direct;
90
- break;
91
- case "own":
92
- variant.ownConditions.push(...extractInnerConditions(state.innerCondition, state.negated ?? false));
93
- break;
94
- case "starting":
95
- variant.startingStyle = !state.negated;
96
- break;
97
- }
69
+ case "root": return innerConditionToVariants(state.innerCondition, state.negated ?? false, "rootConditions");
70
+ case "parent": return parentConditionToVariants(state.innerCondition, state.negated ?? false, state.direct);
71
+ case "own": return innerConditionToVariants(state.innerCondition, state.negated ?? false, "ownConditions");
72
+ case "modifier": {
73
+ const v = emptyVariant();
74
+ v.modifierConditions.push(modifierToParsed(state));
75
+ return {
76
+ variants: [v],
77
+ isImpossible: false
78
+ };
79
+ }
80
+ case "pseudo": {
81
+ const v = emptyVariant();
82
+ v.pseudoConditions.push(pseudoToParsed(state));
83
+ return {
84
+ variants: [v],
85
+ isImpossible: false
86
+ };
87
+ }
88
+ case "container": {
89
+ const v = emptyVariant();
90
+ v.containerConditions.push(containerToParsed(state));
91
+ return {
92
+ variants: [v],
93
+ isImpossible: false
94
+ };
95
+ }
96
+ case "supports": {
97
+ const v = emptyVariant();
98
+ v.supportsConditions.push(supportsToParsed(state));
99
+ return {
100
+ variants: [v],
101
+ isImpossible: false
102
+ };
103
+ }
104
+ case "starting": {
105
+ const v = emptyVariant();
106
+ v.startingStyle = !state.negated;
98
107
  return {
99
- variants: [variant],
108
+ variants: [v],
100
109
  isImpossible: false
101
110
  };
102
111
  }
@@ -235,24 +244,93 @@ function supportsToParsed(state) {
235
244
  };
236
245
  }
237
246
  /**
238
- * Extract modifier/pseudo conditions from an inner condition tree.
239
- * Shared by @own(), @root(), and @parent().
247
+ * Collect all modifier and pseudo conditions from a variant as a flat array.
248
+ */
249
+ function collectSelectorConditions(variant) {
250
+ return [...variant.modifierConditions, ...variant.pseudoConditions];
251
+ }
252
+ /**
253
+ * Convert an inner condition tree into SelectorVariants.
254
+ * Each inner OR branch becomes a separate variant, preserving disjunction.
255
+ * Shared by @root() and @own().
256
+ */
257
+ function innerConditionToVariants(innerCondition, negated, target) {
258
+ const innerCSS = conditionToCSS(negated ? not(innerCondition) : innerCondition);
259
+ if (innerCSS.isImpossible || innerCSS.variants.length === 0) return {
260
+ variants: [],
261
+ isImpossible: true
262
+ };
263
+ const variants = [];
264
+ for (const innerVariant of innerCSS.variants) {
265
+ const conditions = collectSelectorConditions(innerVariant);
266
+ if (conditions.length > 0) {
267
+ const v = emptyVariant();
268
+ v[target].push(...conditions);
269
+ variants.push(v);
270
+ }
271
+ }
272
+ if (variants.length === 0) return {
273
+ variants: [emptyVariant()],
274
+ isImpossible: false
275
+ };
276
+ return {
277
+ variants,
278
+ isImpossible: false
279
+ };
280
+ }
281
+ /**
282
+ * Convert a @parent() inner condition into SelectorVariants with ParentGroups.
283
+ *
284
+ * Positive: each inner OR branch becomes a separate variant with one :is() group.
285
+ * Negated: !(A | B) = !A & !B — all branches become :not() groups collected
286
+ * into a single variant so they produce :not([a] *):not([b] *) on one element.
240
287
  */
241
- function extractInnerConditions(innerCondition, negated) {
288
+ function parentConditionToVariants(innerCondition, negated, direct) {
242
289
  const innerCSS = conditionToCSS(innerCondition);
243
- if (innerCSS.isImpossible || innerCSS.variants.length === 0) return [];
244
- const result = [];
245
- for (const variant of innerCSS.variants) {
246
- for (const mod of variant.modifierConditions) result.push({
247
- ...mod,
248
- negated: negated ? !mod.negated : mod.negated
249
- });
250
- for (const pseudo of variant.pseudoConditions) result.push({
251
- ...pseudo,
252
- negated: negated ? !pseudo.negated : pseudo.negated
253
- });
290
+ if (innerCSS.isImpossible || innerCSS.variants.length === 0) return {
291
+ variants: [],
292
+ isImpossible: true
293
+ };
294
+ if (negated) {
295
+ const v = emptyVariant();
296
+ for (const innerVariant of innerCSS.variants) {
297
+ const conditions = collectSelectorConditions(innerVariant);
298
+ if (conditions.length > 0) v.parentGroups.push({
299
+ conditions,
300
+ direct,
301
+ negated: true
302
+ });
303
+ }
304
+ if (v.parentGroups.length === 0) return {
305
+ variants: [emptyVariant()],
306
+ isImpossible: false
307
+ };
308
+ return {
309
+ variants: [v],
310
+ isImpossible: false
311
+ };
254
312
  }
255
- return result;
313
+ const variants = [];
314
+ for (const innerVariant of innerCSS.variants) {
315
+ const conditions = collectSelectorConditions(innerVariant);
316
+ if (conditions.length > 0) {
317
+ const v = emptyVariant();
318
+ v.parentGroups.push({
319
+ conditions,
320
+ direct,
321
+ negated: false
322
+ });
323
+ variants.push(v);
324
+ }
325
+ }
326
+ if (variants.length === 0) return {
327
+ variants: [emptyVariant()],
328
+ isImpossible: false
329
+ };
330
+ return {
331
+ variants,
332
+ isImpossible: false
333
+ };
256
334
  }
257
335
  /**
258
336
  * Convert parsed root conditions to CSS selector prefix (for final output)
@@ -264,14 +342,19 @@ function rootConditionsToCSS(roots) {
264
342
  return prefix;
265
343
  }
266
344
  /**
267
- * Convert parsed parent conditions to CSS selector fragment (for final output)
345
+ * Convert parent groups to CSS selector fragments (for final output).
346
+ * Each group produces its own :is() wrapper.
268
347
  */
269
- function parentConditionsToCSS(parents, direct) {
270
- if (parents.length === 0) return "";
271
- const combinator = direct ? " > *" : " *";
272
- let selectorParts = "";
273
- for (const cond of parents) selectorParts += selectorConditionToCSS(cond);
274
- return `:is(${selectorParts}${combinator})`;
348
+ function parentGroupsToCSS(groups) {
349
+ let result = "";
350
+ for (const group of groups) {
351
+ const combinator = group.direct ? " > *" : " *";
352
+ let parts = "";
353
+ for (const cond of group.conditions) parts += selectorConditionToCSS(cond);
354
+ const wrapper = group.negated ? ":not" : ":is";
355
+ result += `${wrapper}(${parts}${combinator})`;
356
+ }
357
+ return result;
275
358
  }
276
359
  /**
277
360
  * Convert a modifier or pseudo condition to a CSS selector fragment
@@ -300,36 +383,6 @@ function getSelectorConditionKey(cond) {
300
383
  return "attribute" in cond ? `mod:${getModifierKey(cond)}` : `pseudo:${getPseudoKey(cond)}`;
301
384
  }
302
385
  /**
303
- * Deduplicate modifier conditions
304
- */
305
- function dedupeModifierConditions(conditions) {
306
- const seen = /* @__PURE__ */ new Set();
307
- const result = [];
308
- for (const c of conditions) {
309
- const key = getModifierKey(c);
310
- if (!seen.has(key)) {
311
- seen.add(key);
312
- result.push(c);
313
- }
314
- }
315
- return result;
316
- }
317
- /**
318
- * Deduplicate pseudo conditions
319
- */
320
- function dedupePseudoConditions(conditions) {
321
- const seen = /* @__PURE__ */ new Set();
322
- const result = [];
323
- for (const c of conditions) {
324
- const key = getPseudoKey(c);
325
- if (!seen.has(key)) {
326
- seen.add(key);
327
- result.push(c);
328
- }
329
- }
330
- return result;
331
- }
332
- /**
333
386
  * Deduplicate selector conditions (modifiers or pseudos).
334
387
  * Shared by root, parent, and own conditions.
335
388
  */
@@ -344,17 +397,26 @@ function dedupeSelectorConditions(conditions) {
344
397
  }
345
398
  }
346
399
  const negatedBooleanAttrs = /* @__PURE__ */ new Set();
347
- const positiveValueByAttr = /* @__PURE__ */ new Map();
400
+ const positiveExactValuesByAttr = /* @__PURE__ */ new Map();
348
401
  for (const c of result) {
349
402
  if (!("attribute" in c)) continue;
350
403
  if (c.negated && c.value === void 0) negatedBooleanAttrs.add(c.attribute);
351
- if (!c.negated && c.value !== void 0) positiveValueByAttr.set(c.attribute, c.value);
404
+ const op = c.operator ?? "=";
405
+ if (!c.negated && c.value !== void 0 && op === "=") {
406
+ let values = positiveExactValuesByAttr.get(c.attribute);
407
+ if (!values) {
408
+ values = /* @__PURE__ */ new Set();
409
+ positiveExactValuesByAttr.set(c.attribute, values);
410
+ }
411
+ values.add(c.value);
412
+ }
352
413
  }
353
414
  result = result.filter((c) => {
354
415
  if (!("attribute" in c) || !c.negated || c.value === void 0) return true;
355
416
  if (negatedBooleanAttrs.has(c.attribute)) return false;
356
- const positiveValue = positiveValueByAttr.get(c.attribute);
357
- if (positiveValue !== void 0 && c.value !== positiveValue) return false;
417
+ if ((c.operator ?? "=") !== "=") return true;
418
+ const positiveValues = positiveExactValuesByAttr.get(c.attribute);
419
+ if (positiveValues !== void 0 && positiveValues.size === 1 && !positiveValues.has(c.value)) return false;
358
420
  return true;
359
421
  });
360
422
  return result;
@@ -396,6 +458,21 @@ function hasSelectorConditionContradiction(conditions) {
396
458
  return hasModifierContradiction(modifiers) || hasPseudoContradiction(pseudos);
397
459
  }
398
460
  /**
461
+ * Check for parent group contradiction: same target (direct + conditions)
462
+ * with opposite negation. E.g. :not([data-hovered] *) and :is([data-hovered] *)
463
+ * in the same variant is impossible.
464
+ */
465
+ function hasParentGroupContradiction(groups) {
466
+ const byBaseKey = /* @__PURE__ */ new Map();
467
+ for (const g of groups) {
468
+ const baseKey = `${g.direct ? ">" : ""}(${g.conditions.map(getSelectorConditionKey).sort().join(",")})`;
469
+ const existing = byBaseKey.get(baseKey);
470
+ if (existing !== void 0 && existing !== !g.negated) return true;
471
+ byBaseKey.set(baseKey, !g.negated);
472
+ }
473
+ return false;
474
+ }
475
+ /**
399
476
  * Merge two selector variants (AND operation)
400
477
  * Deduplicates conditions and checks for contradictions
401
478
  */
@@ -404,12 +481,11 @@ function mergeVariants(a, b) {
404
481
  if (hasMediaContradiction(mergedMedia)) return null;
405
482
  const mergedRoots = dedupeSelectorConditions([...a.rootConditions, ...b.rootConditions]);
406
483
  if (hasSelectorConditionContradiction(mergedRoots)) return null;
407
- const mergedModifiers = dedupeModifierConditions([...a.modifierConditions, ...b.modifierConditions]);
408
- if (hasModifierContradiction(mergedModifiers)) return null;
409
- const mergedPseudos = dedupePseudoConditions([...a.pseudoConditions, ...b.pseudoConditions]);
410
- if (hasPseudoContradiction(mergedPseudos)) return null;
411
- const mergedParents = dedupeSelectorConditions([...a.parentConditions, ...b.parentConditions]);
412
- if (hasSelectorConditionContradiction(mergedParents)) return null;
484
+ const mergedModifiers = dedupeSelectorConditions([...a.modifierConditions, ...b.modifierConditions]);
485
+ const mergedPseudos = dedupeSelectorConditions([...a.pseudoConditions, ...b.pseudoConditions]);
486
+ if (hasSelectorConditionContradiction([...mergedModifiers, ...mergedPseudos])) return null;
487
+ const mergedParentGroups = [...a.parentGroups, ...b.parentGroups];
488
+ if (hasParentGroupContradiction(mergedParentGroups)) return null;
413
489
  const mergedOwn = dedupeSelectorConditions([...a.ownConditions, ...b.ownConditions]);
414
490
  if (hasSelectorConditionContradiction(mergedOwn)) return null;
415
491
  const mergedContainers = dedupeContainerConditions([...a.containerConditions, ...b.containerConditions]);
@@ -424,8 +500,7 @@ function mergeVariants(a, b) {
424
500
  containerConditions: mergedContainers,
425
501
  supportsConditions: mergedSupports,
426
502
  rootConditions: mergedRoots,
427
- parentConditions: mergedParents,
428
- parentDirect: a.parentDirect || b.parentDirect,
503
+ parentGroups: mergedParentGroups,
429
504
  startingStyle: a.startingStyle || b.startingStyle
430
505
  };
431
506
  }
@@ -585,7 +660,7 @@ function getVariantKey(v) {
585
660
  containerKey,
586
661
  v.supportsConditions.map((c) => `${c.subtype}:${c.negated ? "!" : ""}${c.condition}`).sort().join("|"),
587
662
  v.rootConditions.map(getSelectorConditionKey).sort().join("|"),
588
- v.parentConditions.map(getSelectorConditionKey).sort().join("|"),
663
+ v.parentGroups.map(getParentGroupKey).sort().join("|"),
589
664
  v.startingStyle ? "1" : "0"
590
665
  ].join("###");
591
666
  }
@@ -609,8 +684,9 @@ function isVariantSuperset(a, b) {
609
684
  if (!isModifierConditionsSuperset(a.modifierConditions, b.modifierConditions)) return false;
610
685
  if (!isPseudoConditionsSuperset(a.pseudoConditions, b.pseudoConditions)) return false;
611
686
  if (!isSelectorConditionsSuperset(a.ownConditions, b.ownConditions)) return false;
612
- if (!isSelectorConditionsSuperset(a.parentConditions, b.parentConditions)) return false;
613
- return a.mediaConditions.length + a.containerConditions.length + a.supportsConditions.length + a.modifierConditions.length + a.pseudoConditions.length + a.rootConditions.length + a.parentConditions.length + a.ownConditions.length > b.mediaConditions.length + b.containerConditions.length + b.supportsConditions.length + b.modifierConditions.length + b.pseudoConditions.length + b.rootConditions.length + b.parentConditions.length + b.ownConditions.length;
687
+ if (!isParentGroupsSuperset(a.parentGroups, b.parentGroups)) return false;
688
+ const parentConditionCount = (groups) => groups.reduce((sum, g) => sum + g.conditions.length, 0);
689
+ 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;
614
690
  }
615
691
  /**
616
692
  * Check if media conditions A is a superset of B
@@ -663,7 +739,7 @@ function isPseudoConditionsSuperset(a, b) {
663
739
  }
664
740
  /**
665
741
  * Check if selector conditions A is a superset of B.
666
- * Shared by root, parent, and own conditions.
742
+ * Shared by root and own conditions.
667
743
  */
668
744
  function isSelectorConditionsSuperset(a, b) {
669
745
  const aKeys = new Set(a.map(getSelectorConditionKey));
@@ -671,6 +747,19 @@ function isSelectorConditionsSuperset(a, b) {
671
747
  return true;
672
748
  }
673
749
  /**
750
+ * Check if parent groups A is a superset of B.
751
+ * Each group in B must have a matching group in A.
752
+ */
753
+ function isParentGroupsSuperset(a, b) {
754
+ if (a.length < b.length) return false;
755
+ const aKeys = new Set(a.map(getParentGroupKey));
756
+ for (const g of b) if (!aKeys.has(getParentGroupKey(g))) return false;
757
+ return true;
758
+ }
759
+ function getParentGroupKey(g) {
760
+ return `${g.negated ? "!" : ""}${g.direct ? ">" : ""}(${g.conditions.map(getSelectorConditionKey).sort().join(",")})`;
761
+ }
762
+ /**
674
763
  * Deduplicate variants
675
764
  *
676
765
  * Removes:
@@ -687,9 +776,8 @@ function dedupeVariants(variants) {
687
776
  result.push(v);
688
777
  }
689
778
  }
690
- result.sort((a, b) => {
691
- return a.modifierConditions.length + a.pseudoConditions.length + a.ownConditions.length + a.mediaConditions.length + a.containerConditions.length + a.supportsConditions.length + a.rootConditions.length + a.parentConditions.length - (b.modifierConditions.length + b.pseudoConditions.length + b.ownConditions.length + b.mediaConditions.length + b.containerConditions.length + b.supportsConditions.length + b.rootConditions.length + b.parentConditions.length);
692
- });
779
+ 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);
780
+ result.sort((a, b) => variantConditionCount(a) - variantConditionCount(b));
693
781
  const filtered = [];
694
782
  for (const candidate of result) {
695
783
  let isRedundant = false;
@@ -816,5 +904,5 @@ function buildAtRulesFromVariant(variant) {
816
904
  }
817
905
 
818
906
  //#endregion
819
- export { buildAtRulesFromVariant, conditionToCSS, modifierToCSS, parentConditionsToCSS, pseudoToCSS, rootConditionsToCSS, selectorConditionToCSS };
907
+ export { buildAtRulesFromVariant, conditionToCSS, modifierToCSS, parentGroupsToCSS, pseudoToCSS, rootConditionsToCSS, selectorConditionToCSS };
820
908
  //# sourceMappingURL=materialize.js.map