@teacss/classes 0.10.9 → 0.10.10

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.
package/dist/recipe.js ADDED
@@ -0,0 +1,594 @@
1
+ import { appendOwnArrayValue, setOwnArrayIndex, toClassString } from "./input.js";
2
+ import { mergePrepared, prepareFragment } from "./merge.js";
3
+ const propertyIsEnumerable = Object.prototype.propertyIsEnumerable;
4
+ const DEFAULTS_BOUNDARY = "definition.defaults";
5
+ const VARIANTS_BOUNDARY = "definition.variants";
6
+ const DEFAULT_VARIANTS_BOUNDARY = "definition.defaultVariants";
7
+ const COMPOUNDS_BOUNDARY = "definition.compoundVariants";
8
+ const RECIPE_PROPS_BOUNDARY = "recipe props";
9
+ const CLASS_NAME_FIELD = "className";
10
+ const DEFAULTS_FIELD = "defaults";
11
+ const CLASS_KEYS_FIELD = "classKeys";
12
+ const NULL_NOT_ALLOWED = "null is not allowed";
13
+ const REQUIRED_OWN_FIELD = "required own enumerable field";
14
+ const DEFINITION_FIELDS = new Set(["defaults", "variants", "defaultVariants", "compoundVariants", "extend"]);
15
+ const RESERVED_SLOT_NAMES = new Set(["class", "className", "then", "toJSON", "toString", "valueOf"]);
16
+ const RESERVED_VARIANT_NAMES = new Set(["class", "className", "classKeys"]);
17
+ const SNAPSHOT_KEY_SET_THRESHOLD = 16;
18
+ function snapshotKeySet(keys) {
19
+ return keys.length > SNAPSHOT_KEY_SET_THRESHOLD ? new Set(keys) : undefined;
20
+ }
21
+ function snapshotHasKey(keys, set, key) {
22
+ return set?.has(key) ?? keys.includes(key);
23
+ }
24
+ function recipeError(boundary, reason) {
25
+ return new TypeError(`[TeaCSS recipe] ${boundary}: ${reason}`);
26
+ }
27
+ function assertSchemaRecord(value, boundary) {
28
+ if (typeof value !== "object" || value === null || Array.isArray(value)) {
29
+ throw recipeError(boundary, "expected a plain record");
30
+ }
31
+ const prototype = Object.getPrototypeOf(value);
32
+ if (prototype !== Object.prototype && prototype !== null) {
33
+ throw recipeError(boundary, "expected Object.prototype or null prototype");
34
+ }
35
+ return Object.keys(value);
36
+ }
37
+ function assertPropsRecord(value, boundary) {
38
+ if (typeof value !== "object" || value === null || Array.isArray(value)) {
39
+ throw recipeError(boundary, "expected a non-null, non-array object");
40
+ }
41
+ return Object.keys(value);
42
+ }
43
+ function readOwnEnumerableField(record, key, boundary) {
44
+ const descriptor = Reflect.getOwnPropertyDescriptor(record, key);
45
+ if (descriptor === undefined || !descriptor.enumerable) {
46
+ throw recipeError(boundary, "expected an own enumerable field");
47
+ }
48
+ if ("value" in descriptor)
49
+ return descriptor.value;
50
+ return descriptor.get === undefined ? undefined : Reflect.apply(descriptor.get, record, []);
51
+ }
52
+ function snapshotArrayLength(value, boundary) {
53
+ const descriptor = Reflect.getOwnPropertyDescriptor(value, "length");
54
+ const length = descriptor !== undefined && "value" in descriptor ? descriptor.value : undefined;
55
+ if (typeof length !== "number" || !Number.isInteger(length) || length < 0 || length >= 0xffffffff) {
56
+ throw recipeError(boundary, "expected an array with a valid own length");
57
+ }
58
+ return length;
59
+ }
60
+ function assertDenseArray(value, boundary, allowEmpty) {
61
+ if (!Array.isArray(value))
62
+ throw recipeError(boundary, "expected an array");
63
+ const keys = Object.keys(value);
64
+ const length = snapshotArrayLength(value, boundary);
65
+ if (!allowEmpty && length === 0)
66
+ throw recipeError(boundary, "expected a non-empty array");
67
+ if (keys.length !== length)
68
+ throw recipeError(boundary, "expected a dense own-indexed array");
69
+ for (const key of keys) {
70
+ const index = Number(key);
71
+ if (!Number.isInteger(index) || index < 0 || index >= length || String(index) !== key)
72
+ throw recipeError(boundary, "expected a dense own-indexed array");
73
+ }
74
+ const snapshot = createDenseSlots(length);
75
+ for (let index = 0; index < length; index++) {
76
+ snapshot[index] = readOwnEnumerableField(value, index, `${boundary}[${index}]`);
77
+ }
78
+ return snapshot;
79
+ }
80
+ function assertNonWhitespaceName(name, boundary) {
81
+ if (name.trim().length === 0)
82
+ throw recipeError(boundary, "name must contain a non-whitespace character");
83
+ }
84
+ function normalizeFragment(value, prepare) {
85
+ const normalized = toClassString([value]);
86
+ return normalized.length === 0 ? undefined : prepare(normalized);
87
+ }
88
+ function appendFragment(target, index, fragment) {
89
+ if (fragment === undefined)
90
+ return;
91
+ const existing = propertyIsEnumerable.call(target, index) ? target[index] : undefined;
92
+ target[index] = existing === undefined ? [fragment] : [...existing, fragment];
93
+ }
94
+ function createDenseSlots(length) {
95
+ // Own elements at every index, like Array.from, without its per-slot
96
+ // callback; the `in` check keeps density under Array.prototype pollution.
97
+ const slots = new Array(length);
98
+ for (let index = 0; index < length; index++) {
99
+ if (index in slots) {
100
+ setOwnArrayIndex(slots, index, undefined);
101
+ }
102
+ else {
103
+ slots[index] = undefined;
104
+ }
105
+ }
106
+ return slots;
107
+ }
108
+ function createInlineBuffer() {
109
+ return [undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined];
110
+ }
111
+ // Own slots keep the measured common path fast; larger vectors fall back to explicit own-property creation.
112
+ const INLINE_BUFFER_CAPACITY = /* @__PURE__ */ createInlineBuffer().length;
113
+ function setBufferedArrayIndex(target, index, value) {
114
+ if (index < INLINE_BUFFER_CAPACITY || !(index in target)) {
115
+ target[index] = value;
116
+ }
117
+ else {
118
+ setOwnArrayIndex(target, index, value);
119
+ }
120
+ }
121
+ function cloneFragments(source, length = source.length) {
122
+ const clone = createDenseSlots(length);
123
+ const sourceLength = Math.min(source.length, length);
124
+ for (let index = 0; index < sourceLength; index++) {
125
+ if (!propertyIsEnumerable.call(source, index))
126
+ continue;
127
+ const fragments = source[index];
128
+ if (fragments !== undefined)
129
+ clone[index] = [...fragments];
130
+ }
131
+ return clone;
132
+ }
133
+ function cloneVariants(source, slotCount) {
134
+ return source.map((variant) => {
135
+ const choices = Object.create(null);
136
+ for (const key of Object.keys(variant.choices)) {
137
+ choices[key] = {
138
+ fragmentsBySlot: cloneFragments(variant.choices[key].fragmentsBySlot, slotCount),
139
+ };
140
+ }
141
+ return { ...variant, choices };
142
+ });
143
+ }
144
+ function normalizeSelection(value, variant, boundary) {
145
+ if (value === null)
146
+ return null;
147
+ if (typeof value === "number") {
148
+ if (!Number.isFinite(value))
149
+ throw recipeError(boundary, "expected a finite numeric choice");
150
+ value = String(value);
151
+ }
152
+ if (typeof value === "string") {
153
+ if (!propertyIsEnumerable.call(variant.choices, value)) {
154
+ throw recipeError(boundary, `unknown choice ${JSON.stringify(value)}`);
155
+ }
156
+ return value;
157
+ }
158
+ if (typeof value === "boolean") {
159
+ if (!variant.booleanCapable)
160
+ throw recipeError(boundary, "boolean selection requires a true or false choice");
161
+ return String(value);
162
+ }
163
+ throw recipeError(boundary, "expected null, a declared string or finite numeric choice, or a supported boolean state");
164
+ }
165
+ function compileBaseStyles(defaultsValue, parent, inherit, prepare) {
166
+ if (inherit)
167
+ defaultsValue = parent.slotNames.length === 0 ? "" : {};
168
+ if (typeof defaultsValue === "string") {
169
+ if (parent !== undefined && parent.slotNames.length > 0) {
170
+ throw recipeError(DEFAULTS_BOUNDARY, "must be a record when extending a multi-element recipe");
171
+ }
172
+ const baseFragments = parent === undefined ? createDenseSlots(1) : cloneFragments(parent.baseFragmentsBySlot, 1);
173
+ appendFragment(baseFragments, 0, normalizeFragment(defaultsValue, prepare));
174
+ return { names: [], indexes: Object.create(null), baseFragments };
175
+ }
176
+ const keys = assertSchemaRecord(defaultsValue, DEFAULTS_BOUNDARY);
177
+ if (parent !== undefined && parent.slotNames.length === 0) {
178
+ throw recipeError(DEFAULTS_BOUNDARY, "must be a string when extending a single-element recipe");
179
+ }
180
+ if (!inherit && keys.length === 0)
181
+ throw recipeError(DEFAULTS_BOUNDARY, "expected at least one named class");
182
+ for (const name of keys) {
183
+ const classBoundary = `${DEFAULTS_BOUNDARY}.${name}`;
184
+ assertNonWhitespaceName(name, classBoundary);
185
+ if (RESERVED_SLOT_NAMES.has(name))
186
+ throw recipeError(classBoundary, "reserved class name");
187
+ }
188
+ const names = parent === undefined ? [] : [...parent.slotNames];
189
+ const indexes = Object.create(null);
190
+ for (let index = 0; index < names.length; index++)
191
+ indexes[names[index]] = index;
192
+ const baseFragments = parent === undefined ? [] : cloneFragments(parent.baseFragmentsBySlot);
193
+ const classes = defaultsValue;
194
+ for (const name of keys) {
195
+ let index = indexes[name];
196
+ if (index === undefined) {
197
+ index = names.length;
198
+ appendOwnArrayValue(names, name);
199
+ indexes[name] = index;
200
+ appendOwnArrayValue(baseFragments, undefined);
201
+ }
202
+ appendFragment(baseFragments, index, normalizeFragment(readOwnEnumerableField(classes, name, `${DEFAULTS_BOUNDARY}.${name}`), prepare));
203
+ }
204
+ return { names, indexes, baseFragments };
205
+ }
206
+ function compileStyleOutput(fragmentsBySlot, value, single, slotIndexes, boundary, requireTarget, prepare) {
207
+ if (single) {
208
+ if (typeof value !== "string")
209
+ throw recipeError(boundary, "expected a primitive string");
210
+ appendFragment(fragmentsBySlot, 0, normalizeFragment(value, prepare));
211
+ return;
212
+ }
213
+ const targetNames = assertSchemaRecord(value, boundary);
214
+ if (requireTarget && targetNames.length === 0)
215
+ throw recipeError(boundary, "expected at least one class target");
216
+ const targetRecord = value;
217
+ for (const targetName of targetNames) {
218
+ const slotIndex = slotIndexes[targetName];
219
+ if (slotIndex === undefined)
220
+ throw recipeError(`${boundary}.${targetName}`, "unknown class target");
221
+ const targetBoundary = `${boundary}.${targetName}`;
222
+ appendFragment(fragmentsBySlot, slotIndex, normalizeFragment(readOwnEnumerableField(targetRecord, targetName, targetBoundary), prepare));
223
+ }
224
+ }
225
+ function compileVariants(variantsValue, parent, single, slotIndexes, slotCount, prepare) {
226
+ const variants = parent === undefined ? [] : cloneVariants(parent.variants, slotCount);
227
+ const indexes = Object.create(null);
228
+ for (let index = 0; index < variants.length; index++)
229
+ indexes[variants[index].name] = index;
230
+ if (variantsValue === undefined)
231
+ return { variants, indexes };
232
+ if (variantsValue === null)
233
+ throw recipeError(VARIANTS_BOUNDARY, NULL_NOT_ALLOWED);
234
+ const variantNames = assertSchemaRecord(variantsValue, VARIANTS_BOUNDARY);
235
+ const variantsRecord = variantsValue;
236
+ for (const name of variantNames) {
237
+ const variantBoundary = `${VARIANTS_BOUNDARY}.${name}`;
238
+ assertNonWhitespaceName(name, variantBoundary);
239
+ if (RESERVED_VARIANT_NAMES.has(name))
240
+ throw recipeError(variantBoundary, "reserved variant name");
241
+ const choicesValue = readOwnEnumerableField(variantsRecord, name, variantBoundary);
242
+ const choiceNames = assertSchemaRecord(choicesValue, variantBoundary);
243
+ if (choiceNames.length === 0)
244
+ throw recipeError(variantBoundary, "expected at least one choice");
245
+ const choicesRecord = choicesValue;
246
+ let variantIndex = indexes[name];
247
+ let mutableChoices;
248
+ if (variantIndex === undefined) {
249
+ variantIndex = variants.length;
250
+ indexes[name] = variantIndex;
251
+ mutableChoices = Object.create(null);
252
+ appendOwnArrayValue(variants, {
253
+ name,
254
+ choices: mutableChoices,
255
+ booleanCapable: false,
256
+ });
257
+ }
258
+ else {
259
+ mutableChoices = variants[variantIndex].choices;
260
+ }
261
+ for (const choiceName of choiceNames) {
262
+ const choiceBoundary = `${variantBoundary}.${choiceName}`;
263
+ const targetValue = readOwnEnumerableField(choicesRecord, choiceName, choiceBoundary);
264
+ const previousChoice = mutableChoices[choiceName];
265
+ const fragmentsBySlot = previousChoice === undefined ? createDenseSlots(slotCount) : cloneFragments(previousChoice.fragmentsBySlot, slotCount);
266
+ compileStyleOutput(fragmentsBySlot, targetValue, single, slotIndexes, choiceBoundary, false, prepare);
267
+ mutableChoices[choiceName] = { fragmentsBySlot };
268
+ }
269
+ variants[variantIndex] = {
270
+ name,
271
+ choices: mutableChoices,
272
+ booleanCapable: propertyIsEnumerable.call(mutableChoices, "true") || propertyIsEnumerable.call(mutableChoices, "false"),
273
+ };
274
+ }
275
+ return { variants, indexes };
276
+ }
277
+ function compileDefaultVariants(defaultsValue, parent, variants, variantIndexes) {
278
+ const defaults = createDenseSlots(variants.length);
279
+ if (parent !== undefined) {
280
+ const parentLength = Math.min(parent.defaultStates.length, variants.length);
281
+ for (let index = 0; index < parentLength; index++) {
282
+ if (propertyIsEnumerable.call(parent.defaultStates, index))
283
+ defaults[index] = parent.defaultStates[index];
284
+ }
285
+ }
286
+ if (defaultsValue === undefined)
287
+ return defaults;
288
+ if (defaultsValue === null)
289
+ throw recipeError(DEFAULT_VARIANTS_BOUNDARY, NULL_NOT_ALLOWED);
290
+ const keys = assertSchemaRecord(defaultsValue, DEFAULT_VARIANTS_BOUNDARY);
291
+ const keySet = snapshotKeySet(keys);
292
+ for (const name of keys) {
293
+ if (variantIndexes[name] === undefined)
294
+ throw recipeError(`${DEFAULT_VARIANTS_BOUNDARY}.${name}`, "unknown variant");
295
+ }
296
+ const record = defaultsValue;
297
+ for (let index = 0; index < variants.length; index++) {
298
+ const variant = variants[index];
299
+ if (!snapshotHasKey(keys, keySet, variant.name))
300
+ continue;
301
+ const boundary = `${DEFAULT_VARIANTS_BOUNDARY}.${variant.name}`;
302
+ const value = readOwnEnumerableField(record, variant.name, boundary);
303
+ if (value !== undefined)
304
+ defaults[index] = normalizeSelection(value, variant, boundary);
305
+ }
306
+ return defaults;
307
+ }
308
+ function compileConditionStates(value, variant, boundary) {
309
+ if (!Array.isArray(value)) {
310
+ if (value === undefined)
311
+ throw recipeError(boundary, "undefined is not a condition");
312
+ return [normalizeSelection(value, variant, boundary)];
313
+ }
314
+ const values = assertDenseArray(value, boundary, false);
315
+ const states = [];
316
+ for (let index = 0; index < values.length; index++) {
317
+ const member = values[index];
318
+ if (member === undefined)
319
+ throw recipeError(`${boundary}[${index}]`, "undefined is not a condition");
320
+ appendOwnArrayValue(states, normalizeSelection(member, variant, `${boundary}[${index}]`));
321
+ }
322
+ return states;
323
+ }
324
+ function compileCompounds(compoundsValue, parent, single, slotIndexes, slotCount, variants, variantIndexes, prepare) {
325
+ const compounds = parent === undefined
326
+ ? []
327
+ : parent.compounds.map((compound) => ({
328
+ conditions: compound.conditions,
329
+ fragmentsBySlot: cloneFragments(compound.fragmentsBySlot, slotCount),
330
+ }));
331
+ if (compoundsValue === undefined)
332
+ return compounds;
333
+ if (compoundsValue === null)
334
+ throw recipeError(COMPOUNDS_BOUNDARY, NULL_NOT_ALLOWED);
335
+ const entries = assertDenseArray(compoundsValue, COMPOUNDS_BOUNDARY, true);
336
+ for (let entryIndex = 0; entryIndex < entries.length; entryIndex++) {
337
+ const entryValue = entries[entryIndex];
338
+ const entryBoundary = `${COMPOUNDS_BOUNDARY}[${entryIndex}]`;
339
+ const keys = assertSchemaRecord(entryValue, entryBoundary);
340
+ const keySet = snapshotKeySet(keys);
341
+ const hasClassKeys = snapshotHasKey(keys, keySet, CLASS_KEYS_FIELD);
342
+ if (!snapshotHasKey(keys, keySet, CLASS_NAME_FIELD)) {
343
+ throw recipeError(`${entryBoundary}.className`, single || hasClassKeys ? "required own enumerable string" : "required own enumerable class map");
344
+ }
345
+ for (const name of keys) {
346
+ if (name === CLASS_NAME_FIELD || name === CLASS_KEYS_FIELD)
347
+ continue;
348
+ const variantIndex = variantIndexes[name];
349
+ if (variantIndex === undefined)
350
+ throw recipeError(`${entryBoundary}.${name}`, "unknown variant condition");
351
+ }
352
+ const record = entryValue;
353
+ const conditions = [];
354
+ for (let variantIndex = 0; variantIndex < variants.length; variantIndex++) {
355
+ const variant = variants[variantIndex];
356
+ if (!snapshotHasKey(keys, keySet, variant.name))
357
+ continue;
358
+ const boundary = `${entryBoundary}.${variant.name}`;
359
+ appendOwnArrayValue(conditions, {
360
+ variantIndex,
361
+ states: compileConditionStates(readOwnEnumerableField(record, variant.name, boundary), variant, boundary),
362
+ });
363
+ }
364
+ const classNameBoundary = `${entryBoundary}.className`;
365
+ const classNameValue = readOwnEnumerableField(record, CLASS_NAME_FIELD, classNameBoundary);
366
+ const fragmentsBySlot = createDenseSlots(slotCount);
367
+ if (hasClassKeys) {
368
+ const classKeysBoundary = `${entryBoundary}.classKeys`;
369
+ if (single)
370
+ throw recipeError(classKeysBoundary, "only valid for multi-element recipes");
371
+ if (typeof classNameValue !== "string")
372
+ throw recipeError(classNameBoundary, "expected a primitive string when classKeys is present");
373
+ const targetNames = assertDenseArray(readOwnEnumerableField(record, CLASS_KEYS_FIELD, classKeysBoundary), classKeysBoundary, false);
374
+ const fragment = normalizeFragment(classNameValue, prepare);
375
+ for (let targetIndex = 0; targetIndex < targetNames.length; targetIndex++) {
376
+ const targetName = targetNames[targetIndex];
377
+ const targetBoundary = `${classKeysBoundary}[${targetIndex}]`;
378
+ if (typeof targetName !== "string")
379
+ throw recipeError(targetBoundary, "expected a declared class name");
380
+ const slotIndex = slotIndexes[targetName];
381
+ if (slotIndex === undefined)
382
+ throw recipeError(targetBoundary, "unknown class target");
383
+ if (targetNames.indexOf(targetName) !== targetIndex)
384
+ throw recipeError(targetBoundary, `duplicate class target ${JSON.stringify(targetName)}`);
385
+ appendFragment(fragmentsBySlot, slotIndex, fragment);
386
+ }
387
+ }
388
+ else {
389
+ compileStyleOutput(fragmentsBySlot, classNameValue, single, slotIndexes, classNameBoundary, true, prepare);
390
+ }
391
+ appendOwnArrayValue(compounds, { conditions, fragmentsBySlot });
392
+ }
393
+ return compounds;
394
+ }
395
+ function compileDefinition(value, registry, prepare) {
396
+ const keys = assertSchemaRecord(value, "definition");
397
+ for (const key of keys) {
398
+ if (!DEFINITION_FIELDS.has(key))
399
+ throw recipeError(`definition.${key}`, "unknown field");
400
+ }
401
+ const definition = value;
402
+ let parent;
403
+ if (keys.includes("extend")) {
404
+ const extend = readOwnEnumerableField(definition, "extend", "definition.extend");
405
+ if (extend !== undefined) {
406
+ if (typeof extend !== "function" || (parent = registry.get(extend)) === undefined) {
407
+ throw recipeError("definition.extend", "expected a recipe from this exact reciper");
408
+ }
409
+ }
410
+ }
411
+ const hasDefaults = keys.includes(DEFAULTS_FIELD);
412
+ if (!hasDefaults && parent === undefined)
413
+ throw recipeError(DEFAULTS_BOUNDARY, REQUIRED_OWN_FIELD);
414
+ const compiledDefaults = compileBaseStyles(hasDefaults ? readOwnEnumerableField(definition, DEFAULTS_FIELD, DEFAULTS_BOUNDARY) : undefined, parent, !hasDefaults, prepare);
415
+ const slotCount = compiledDefaults.baseFragments.length;
416
+ const single = compiledDefaults.names.length === 0;
417
+ const variantsValue = keys.includes("variants") ? readOwnEnumerableField(definition, "variants", VARIANTS_BOUNDARY) : undefined;
418
+ const compiledVariants = compileVariants(variantsValue, parent, single, compiledDefaults.indexes, slotCount, prepare);
419
+ const defaultVariantsValue = keys.includes("defaultVariants") ? readOwnEnumerableField(definition, "defaultVariants", DEFAULT_VARIANTS_BOUNDARY) : undefined;
420
+ const defaultStates = compileDefaultVariants(defaultVariantsValue, parent, compiledVariants.variants, compiledVariants.indexes);
421
+ const compoundsValue = keys.includes("compoundVariants") ? readOwnEnumerableField(definition, "compoundVariants", COMPOUNDS_BOUNDARY) : undefined;
422
+ const compounds = compileCompounds(compoundsValue, parent, single, compiledDefaults.indexes, slotCount, compiledVariants.variants, compiledVariants.indexes, prepare);
423
+ const compoundsBySlot = createDenseSlots(slotCount);
424
+ for (const compound of compounds) {
425
+ for (let slotIndex = 0; slotIndex < slotCount; slotIndex++) {
426
+ if (compound.fragmentsBySlot[slotIndex] === undefined)
427
+ continue;
428
+ const slotCompounds = (compoundsBySlot[slotIndex] ??= []);
429
+ appendOwnArrayValue(slotCompounds, compound);
430
+ }
431
+ }
432
+ return {
433
+ slotNames: compiledDefaults.names,
434
+ baseFragmentsBySlot: compiledDefaults.baseFragments,
435
+ variants: compiledVariants.variants,
436
+ defaultStates,
437
+ compounds,
438
+ compoundsBySlot,
439
+ };
440
+ }
441
+ function captureRecipeSelections(props, plan) {
442
+ if (props === undefined)
443
+ return plan.defaultStates;
444
+ const keys = assertPropsRecord(props, RECIPE_PROPS_BOUNDARY);
445
+ const keySet = snapshotKeySet(keys);
446
+ if (snapshotHasKey(keys, keySet, "class"))
447
+ throw recipeError(`${RECIPE_PROPS_BOUNDARY}.class`, "reserved field");
448
+ if (snapshotHasKey(keys, keySet, CLASS_NAME_FIELD))
449
+ throw recipeError(`${RECIPE_PROPS_BOUNDARY}.className`, "caller classes belong to a style resolver");
450
+ const record = props;
451
+ let states;
452
+ for (let index = 0; index < plan.variants.length; index++) {
453
+ const variant = plan.variants[index];
454
+ if (!snapshotHasKey(keys, keySet, variant.name))
455
+ continue;
456
+ const boundary = `${RECIPE_PROPS_BOUNDARY}.${variant.name}`;
457
+ const value = readOwnEnumerableField(record, variant.name, boundary);
458
+ if (value === undefined)
459
+ continue;
460
+ states ??= [...plan.defaultStates];
461
+ states[index] = normalizeSelection(value, variant, boundary);
462
+ }
463
+ return states ?? plan.defaultStates;
464
+ }
465
+ function selectedState(captured, overrides, index) {
466
+ if (overrides !== undefined) {
467
+ const override = index < INLINE_BUFFER_CAPACITY || propertyIsEnumerable.call(overrides, index) ? overrides[index] : undefined;
468
+ if (override !== undefined)
469
+ return override;
470
+ }
471
+ return captured[index];
472
+ }
473
+ function compoundMatches(compound, captured, overrides) {
474
+ for (const condition of compound.conditions) {
475
+ if (!condition.states.includes(selectedState(captured, overrides, condition.variantIndex)))
476
+ return false;
477
+ }
478
+ return true;
479
+ }
480
+ function appendFragments(inputs, inputCount, fragments) {
481
+ if (fragments === undefined)
482
+ return inputCount;
483
+ for (const fragment of fragments) {
484
+ setBufferedArrayIndex(inputs, inputCount, fragment);
485
+ inputCount++;
486
+ }
487
+ return inputCount;
488
+ }
489
+ function resolveSlotStyle(plan, slotIndex, captured, props, propsBoundary, merge) {
490
+ let overrides;
491
+ let callerClassName;
492
+ if (props !== undefined) {
493
+ const keys = assertPropsRecord(props, propsBoundary);
494
+ const keySet = snapshotKeySet(keys);
495
+ if (snapshotHasKey(keys, keySet, "class"))
496
+ throw recipeError(`${propsBoundary}.class`, "reserved field");
497
+ const record = props;
498
+ for (let variantIndex = 0; variantIndex < plan.variants.length; variantIndex++) {
499
+ const variant = plan.variants[variantIndex];
500
+ if (!snapshotHasKey(keys, keySet, variant.name))
501
+ continue;
502
+ const boundary = `${propsBoundary}.${variant.name}`;
503
+ const value = readOwnEnumerableField(record, variant.name, boundary);
504
+ if (value === undefined)
505
+ continue;
506
+ overrides ??= createInlineBuffer();
507
+ setBufferedArrayIndex(overrides, variantIndex, normalizeSelection(value, variant, boundary));
508
+ }
509
+ if (snapshotHasKey(keys, keySet, "className")) {
510
+ callerClassName = readOwnEnumerableField(record, "className", `${propsBoundary}.className`);
511
+ }
512
+ }
513
+ const inputs = createInlineBuffer();
514
+ let inputCount = appendFragments(inputs, 0, plan.baseFragmentsBySlot[slotIndex]);
515
+ for (let variantIndex = 0; variantIndex < plan.variants.length; variantIndex++) {
516
+ const state = selectedState(captured, overrides, variantIndex);
517
+ if (state === undefined || state === null)
518
+ continue;
519
+ const choice = plan.variants[variantIndex].choices[state];
520
+ if (choice !== undefined)
521
+ inputCount = appendFragments(inputs, inputCount, choice.fragmentsBySlot[slotIndex]);
522
+ }
523
+ const slotCompounds = plan.compoundsBySlot[slotIndex];
524
+ if (slotCompounds !== undefined) {
525
+ for (const compound of slotCompounds) {
526
+ if (compoundMatches(compound, captured, overrides))
527
+ inputCount = appendFragments(inputs, inputCount, compound.fragmentsBySlot[slotIndex]);
528
+ }
529
+ }
530
+ inputs.length = inputCount;
531
+ return merge(inputs, callerClassName);
532
+ }
533
+ // The snapshot objects keep `Object.prototype` so the public
534
+ // `Readonly<Record<…>>` typing holds (`hasOwnProperty` etc. work); axis names
535
+ // that collide with prototype members — including `__proto__` — are written
536
+ // with `defineProperty`, which creates own data properties unconditionally.
537
+ function setIntrospectionField(target, key, value) {
538
+ Object.defineProperty(target, key, { value, configurable: true, enumerable: true, writable: true });
539
+ }
540
+ function buildRecipeIntrospection(plan) {
541
+ const variants = {};
542
+ for (const variant of plan.variants)
543
+ setIntrospectionField(variants, variant.name, Object.freeze(Object.keys(variant.choices)));
544
+ const defaults = {};
545
+ for (let index = 0; index < plan.variants.length; index++) {
546
+ const state = plan.defaultStates[index];
547
+ if (typeof state === "string")
548
+ setIntrospectionField(defaults, plan.variants[index].name, state);
549
+ }
550
+ return Object.freeze({
551
+ anatomy: Object.freeze([...plan.slotNames]),
552
+ variants: Object.freeze(variants),
553
+ defaultVariants: Object.freeze(defaults),
554
+ compoundVariantCount: plan.compounds.length,
555
+ });
556
+ }
557
+ function createRecipe(plan, merge) {
558
+ const result = (plan.slotNames.length === 0
559
+ ? (props) => resolveSlotStyle(plan, 0, plan.defaultStates, props, RECIPE_PROPS_BOUNDARY, merge)
560
+ : (props) => {
561
+ const captured = captureRecipeSelections(props, plan);
562
+ const styles = Object.create(null);
563
+ for (let slotIndex = 0; slotIndex < plan.slotNames.length; slotIndex++) {
564
+ const name = plan.slotNames[slotIndex];
565
+ styles[name] = (styleProps) => resolveSlotStyle(plan, slotIndex, captured, styleProps, `style props.${name}`, merge);
566
+ }
567
+ return styles;
568
+ });
569
+ return Object.defineProperty(result, "config", {
570
+ value: buildRecipeIntrospection(plan),
571
+ configurable: false,
572
+ enumerable: false,
573
+ writable: false,
574
+ });
575
+ }
576
+ // Source-only construction supports cache-isolated correctness and benchmark adapters.
577
+ // The public entry exports the singleton below; it never exposes this factory.
578
+ export function createRecipeRuntime(runtime) {
579
+ const registry = new WeakMap();
580
+ return ((definition) => {
581
+ const plan = compileDefinition(definition, registry, runtime.prepareFragment);
582
+ const result = createRecipe(plan, runtime.mergePrepared);
583
+ registry.set(result, plan);
584
+ return result;
585
+ });
586
+ }
587
+ // Ownership follows the physical classes module, including facade re-exports.
588
+ const registry = new WeakMap();
589
+ export const recipe = ((definition) => {
590
+ const plan = compileDefinition(definition, registry, prepareFragment);
591
+ const result = createRecipe(plan, mergePrepared);
592
+ registry.set(result, plan);
593
+ return result;
594
+ });
@@ -0,0 +1,35 @@
1
+ /**
2
+ * Grouping `{decl;decl;…}@cond`.
3
+ *
4
+ * A pure, textual expansion run before the whitespace split: a `{ }` group is
5
+ * local DRY sugar that expands to individual atomic tokens and itself emits no
6
+ * CSS. Declarations inside are `;`-separated; any `@cond` immediately following
7
+ * the closing `}` is distributed to every declaration.
8
+ *
9
+ * {bg:red;c:white;p:4}@hover → bg:red@hover c:white@hover p:4@hover
10
+ * {bg:red;c:white} → bg:red c:white
11
+ *
12
+ * `()` and `[]` inside the group are literal regions: `;`, `}`, `@` inside
13
+ * them do not terminate the group or a declaration. Text outside groups is kept
14
+ * verbatim, so this is safe to run over whole source strings.
15
+ */
16
+ /**
17
+ * A group body may hold another group. One pass expands the inner one and
18
+ * leaves the outer `{` in place, because a `{` inside a body ends the candidate
19
+ * — that guard is what keeps host object literals out. Repeating settles the
20
+ * outer group too; without it the leftover braces reach the whitespace split,
21
+ * which eats them and detaches the outer `@cond`.
22
+ */
23
+ export declare const MAX_GROUP_NESTING = 5;
24
+ /**
25
+ * Raised when a `{` inside a group candidate ended that candidate's scan, which
26
+ * is the only way a pass can leave an outer group behind. Repeating a pass over
27
+ * a whole source file is far too expensive to do on the chance one is there —
28
+ * this says when there is.
29
+ */
30
+ export interface NestedGroupSignal {
31
+ seen: boolean;
32
+ }
33
+ /** Expand groups in a class list while preserving braces inside attached literals. */
34
+ export declare function expandClassGroups(code: string): string;
35
+ //# sourceMappingURL=group.d.ts.map