@lostgradient/cinder 0.27.0 → 0.28.0
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/CHANGELOG.md +42 -0
- package/components.json +1 -1
- package/dist/_internal/code-highlighter-context.d.ts +28 -0
- package/dist/cli/index.js +12 -8
- package/dist/components/approval-card/index.js +4 -4
- package/dist/components/button/button.css +25 -0
- package/dist/components/calendar/calendar.css +14 -0
- package/dist/components/capability-gate/capability-gate.css +5 -0
- package/dist/components/checkbox/checkbox.css +19 -0
- package/dist/components/chip/chip.css +14 -0
- package/dist/components/code-block/code-block.d.svelte.ts +1 -1
- package/dist/components/code-block/code-block.examples.json +1 -1
- package/dist/components/code-block/code-block.schema.js +3 -3
- package/dist/components/code-block/code-block.svelte.d.ts +1 -1
- package/dist/components/code-block/code-block.types.d.ts +14 -12
- package/dist/components/code-block/index.js +2 -2
- package/dist/components/color-picker/color-picker.css +2 -0
- package/dist/components/combobox/combobox.css +9 -1
- package/dist/components/command-palette/command-palette.css +7 -5
- package/dist/components/file-upload/file-upload.css +2 -1
- package/dist/components/guidance-region/index.css +1 -1
- package/dist/components/image/image.examples.json +1 -1
- package/dist/components/input/input.css +20 -6
- package/dist/components/json-schema-editor/index.js +5 -5
- package/dist/components/json-schema-editor/json-view-highlighter.d.ts +14 -0
- package/dist/components/key-value-editor/index.css +1 -1
- package/dist/components/modal-region/index.css +1 -1
- package/dist/components/multi-select/multi-select.css +9 -0
- package/dist/components/pagination/pagination.css +12 -0
- package/dist/components/pin-input/pin-input.css +9 -1
- package/dist/components/radio-group/radio-group.css +16 -0
- package/dist/components/run-step-timeline/index.js +2 -2
- package/dist/components/schema-form/index.js +2 -2
- package/dist/components/schema-form/schema-form.css +9 -0
- package/dist/components/segmented-control/segmented-control.css +3 -3
- package/dist/components/selection-popover/selection-popover.css +7 -1
- package/dist/components/tag-input/tag-input.css +10 -1
- package/dist/components/terminal-frame/index.js +2 -2
- package/dist/components/terminal-frame/terminal-frame.schema.js +2 -2
- package/dist/components/terminal-frame/terminal-frame.types.d.ts +8 -0
- package/dist/components/textarea/textarea.css +1 -1
- package/dist/components/toggle/toggle.css +16 -0
- package/dist/components/tree/tree.examples.json +1 -1
- package/dist/components/virtual-list/index.js +2 -2
- package/dist/components/virtual-list/virtual-list.css +42 -9
- package/dist/highlighters/shiki/default.d.ts +13 -1
- package/dist/highlighters/shiki/default.js +2 -2
- package/dist/highlighters/shiki/index.d.ts +20 -12
- package/dist/highlighters/shiki/index.js +2 -2
- package/dist/index.browser.css +1 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.js +23 -23
- package/dist/scripts/knowledge/index.d.ts +5 -1
- package/dist/server/components/approval-card/index.js +1 -1
- package/dist/server/components/code-block/index.js +1 -1
- package/dist/server/components/color-field/index.js +2 -2
- package/dist/server/components/color-picker/index.js +2 -2
- package/dist/server/components/json-schema-editor/index.js +2 -2
- package/dist/server/components/run-step-timeline/index.js +1 -1
- package/dist/server/components/terminal-frame/index.js +1 -1
- package/dist/server/components/virtual-list/index.js +1 -1
- package/dist/server/github-dark-e7nywpbe.js +3 -0
- package/dist/server/github-light-bqtmm1zq.js +3 -0
- package/dist/server/highlighters/shiki/default.js +2 -2
- package/dist/server/highlighters/shiki/index.js +2 -2
- package/dist/server/index.js +6 -6
- package/dist/server/{index.server-4qyyw4y5.js → index.server-043cfznf.js} +1 -1
- package/dist/server/{index.server-p46tn4eb.js → index.server-09tjs48p.js} +2 -2
- package/dist/server/index.server-0hyvdf74.js +8 -0
- package/dist/server/{index.server-dxgkh9tk.js → index.server-17thvpd3.js} +1 -1
- package/dist/server/index.server-19z6hqd6.js +4 -0
- package/dist/server/index.server-31020qgk.js +3 -0
- package/dist/server/{index.server-w3nqzde5.js → index.server-45wmvjrn.js} +3 -3
- package/dist/server/{index.server-3tkgm2gn.js → index.server-6mb7w5g4.js} +2 -2
- package/dist/server/{index.server-2vxmn7x1.js → index.server-c5yajkkc.js} +4 -4
- package/dist/server/index.server-e6fknxd9.js +4 -0
- package/dist/server/index.server-fev4kwm0.js +4 -0
- package/dist/server/index.server-k4vjk75r.js +4 -0
- package/dist/server/index.server-mjbtd1gs.js +7 -0
- package/dist/server/{index.server-2bfkhzzq.js → index.server-rkp4py5n.js} +2 -2
- package/dist/server/index.server-wdz9tsq4.js +3 -0
- package/dist/server/index.server-zj9909hd.js +4 -0
- package/dist/server/json-68twtta3.js +3 -0
- package/dist/server/tokens/registry.generated.js +1 -1
- package/dist/server/tokens/runtime/index.js +92 -0
- package/dist/styles/check-token-contrast-test-values.d.ts +1 -3
- package/dist/styles/token-contrast-test-readers.d.ts +1 -1
- package/dist/tokens/registry.generated.js +2 -2
- package/dist/tokens/resolved/dark-reduced-motion.json +104 -43
- package/dist/tokens/resolved/dark.json +104 -43
- package/dist/tokens/resolved/light-reduced-motion.json +104 -43
- package/dist/tokens/resolved/light.json +104 -43
- package/dist/tokens/runtime/authoring-catalogue.d.ts +19 -0
- package/dist/tokens/runtime/authoring-catalogue.generated.d.ts +8 -0
- package/dist/tokens/runtime/authoring-palette.d.ts +25 -0
- package/dist/tokens/runtime/authoring-profiles.d.ts +48 -0
- package/dist/tokens/runtime/authoring-provenance.d.ts +31 -0
- package/dist/tokens/runtime/authoring-types.d.ts +182 -0
- package/dist/tokens/runtime/base-corpus.d.ts +29 -0
- package/dist/tokens/runtime/base-corpus.generated.d.ts +8 -0
- package/dist/tokens/runtime/context-order.d.ts +60 -0
- package/dist/tokens/runtime/context-stack.d.ts +11 -0
- package/dist/tokens/runtime/contrast/color-difference.d.ts +16 -0
- package/dist/{styles/color-contrast-utilities.d.ts → tokens/runtime/contrast/color-math.d.ts} +2 -17
- package/dist/tokens/runtime/contrast/color-mix.d.ts +3 -0
- package/dist/tokens/runtime/contrast/css-color.d.ts +29 -0
- package/dist/tokens/runtime/contrast/index.d.ts +7 -0
- package/dist/tokens/runtime/contrast/measure.d.ts +45 -0
- package/dist/tokens/runtime/contrast/pairings.d.ts +36 -0
- package/dist/tokens/runtime/contrast/thresholds.d.ts +47 -0
- package/dist/tokens/runtime/corpus-documents.d.ts +36 -0
- package/dist/tokens/runtime/corpus-validation.d.ts +10 -0
- package/dist/tokens/runtime/corpus.d.ts +35 -0
- package/dist/tokens/runtime/css-configuration.d.ts +1 -0
- package/dist/tokens/runtime/css-stylelint.d.ts +2 -0
- package/dist/tokens/runtime/css-support.d.ts +110 -0
- package/dist/tokens/runtime/evaluate.d.ts +95 -0
- package/dist/tokens/runtime/evaluated-provenance.d.ts +36 -0
- package/dist/tokens/runtime/index.d.ts +38 -0
- package/dist/tokens/runtime/index.js +95 -0
- package/dist/tokens/runtime/package-version.generated.d.ts +9 -0
- package/dist/tokens/runtime/projection.d.ts +32 -0
- package/dist/tokens/runtime/registry.d.ts +125 -0
- package/dist/tokens/runtime/resolve-extends.d.ts +30 -0
- package/dist/tokens/runtime/resolve-references.d.ts +49 -0
- package/dist/tokens/runtime/resolve-shared.d.ts +34 -0
- package/dist/tokens/runtime/resolve.d.ts +44 -0
- package/dist/tokens/runtime/resolver-order.d.ts +89 -0
- package/dist/tokens/runtime/source-digest.d.ts +20 -0
- package/dist/tokens/runtime/stylesheet-skeleton.d.ts +31 -0
- package/dist/tokens/runtime/theme-runtime.test-support.d.ts +24 -0
- package/dist/tokens/runtime/theme-sources.d.ts +46 -0
- package/dist/tokens/runtime/theme-stylesheet.d.ts +31 -0
- package/dist/tokens/runtime/types.d.ts +115 -0
- package/dist/tokens/runtime/validate-resolver.d.ts +31 -0
- package/dist/tokens/runtime/validate-schema.d.ts +22 -0
- package/dist/tokens/runtime/validate-tokens.d.ts +25 -0
- package/dist/tokens/runtime/validate-values.d.ts +33 -0
- package/dist/tokens/runtime/validate.d.ts +3 -0
- package/dist/tokens/runtime/value-color-parse.d.ts +128 -0
- package/dist/tokens/runtime/value-color.d.ts +85 -0
- package/dist/tokens/runtime/value-entry.d.ts +3 -0
- package/dist/tokens/runtime/value-formatters.d.ts +59 -0
- package/dist/tokens/sets/colors.tokens.json +65 -13
- package/dist/tokens/sets/foundation.tokens.json +23 -0
- package/dist/tokens/sets/semantic.tokens.json +136 -30
- package/dist/utilities/highlighter.d.ts +5 -5
- package/dist/utilities/retrying-loader-cache.d.ts +3 -6
- package/package.json +15 -3
- package/src/_internal/code-highlighter-context.ts +41 -0
- package/src/components/button/button.css +25 -0
- package/src/components/calendar/calendar.css +14 -0
- package/src/components/capability-gate/capability-gate.css +5 -0
- package/src/components/checkbox/checkbox.css +19 -0
- package/src/components/chip/chip.css +14 -0
- package/src/components/code-block/code-block.examples.json +1 -1
- package/src/components/code-block/code-block.schema.json +2 -2
- package/src/components/code-block/code-block.schema.ts +2 -2
- package/src/components/code-block/code-block.svelte +15 -19
- package/src/components/code-block/code-block.types.ts +14 -12
- package/src/components/color-picker/color-picker.css +2 -0
- package/src/components/combobox/combobox.css +9 -1
- package/src/components/command-palette/command-palette.css +7 -5
- package/src/components/file-upload/file-upload.css +2 -1
- package/src/components/image/image.examples.json +1 -1
- package/src/components/input/input.css +20 -6
- package/src/components/json-schema-editor/json-view-highlighter.ts +23 -0
- package/src/components/json-schema-editor/json-view.svelte +2 -0
- package/src/components/multi-select/multi-select.css +9 -0
- package/src/components/pagination/pagination.css +12 -0
- package/src/components/pin-input/pin-input.css +9 -1
- package/src/components/radio-group/radio-group.css +16 -0
- package/src/components/schema-form/schema-form.css +9 -0
- package/src/components/segmented-control/segmented-control.css +3 -3
- package/src/components/selection-popover/selection-popover.css +7 -1
- package/src/components/tag-input/tag-input.css +10 -1
- package/src/components/terminal-frame/terminal-frame.schema.json +5 -0
- package/src/components/terminal-frame/terminal-frame.schema.ts +6 -0
- package/src/components/terminal-frame/terminal-frame.svelte +6 -2
- package/src/components/terminal-frame/terminal-frame.types.ts +8 -0
- package/src/components/textarea/textarea.css +1 -1
- package/src/components/toggle/toggle.css +16 -0
- package/src/components/tree/tree.examples.json +1 -1
- package/src/components/virtual-list/virtual-list.css +42 -9
- package/src/components/virtual-list/virtual-list.svelte +54 -20
- package/src/highlighters/shiki/default.ts +13 -1
- package/src/highlighters/shiki/index.ts +20 -12
- package/src/index.ts +5 -0
- package/src/styles/foundation.css +33 -1
- package/src/styles/tokens-base.css +4 -4
- package/src/tokens/registry.generated.json +3 -3
- package/src/tokens/resolved/dark-reduced-motion.json +104 -43
- package/src/tokens/resolved/dark.json +104 -43
- package/src/tokens/resolved/light-reduced-motion.json +104 -43
- package/src/tokens/resolved/light.json +104 -43
- package/src/tokens/runtime/authoring-catalogue.generated.ts +19300 -0
- package/src/tokens/runtime/authoring-catalogue.ts +256 -0
- package/src/tokens/runtime/authoring-palette.ts +100 -0
- package/src/tokens/runtime/authoring-profiles.ts +288 -0
- package/src/tokens/runtime/authoring-provenance.ts +290 -0
- package/src/tokens/runtime/authoring-types.ts +199 -0
- package/src/tokens/runtime/base-corpus.generated.ts +10 -0
- package/src/tokens/runtime/base-corpus.ts +78 -0
- package/src/tokens/runtime/context-order.ts +182 -0
- package/src/tokens/runtime/context-stack.ts +52 -0
- package/src/tokens/runtime/contrast/color-difference.ts +114 -0
- package/src/tokens/runtime/contrast/color-math.ts +89 -0
- package/src/tokens/runtime/contrast/color-mix.ts +20 -0
- package/src/tokens/runtime/contrast/css-color.ts +312 -0
- package/src/tokens/runtime/contrast/index.ts +7 -0
- package/src/tokens/runtime/contrast/measure.ts +185 -0
- package/src/tokens/runtime/contrast/pairings.ts +324 -0
- package/src/tokens/runtime/contrast/thresholds.ts +81 -0
- package/src/tokens/runtime/corpus-documents.ts +289 -0
- package/src/tokens/runtime/corpus-validation.ts +336 -0
- package/src/tokens/runtime/corpus.ts +179 -0
- package/src/tokens/runtime/css-configuration.ts +1 -0
- package/src/tokens/runtime/css-stylelint.ts +15 -0
- package/src/tokens/runtime/css-support.ts +407 -0
- package/src/tokens/runtime/evaluate.ts +262 -0
- package/src/tokens/runtime/evaluated-provenance.ts +116 -0
- package/src/tokens/runtime/format-validator.generated.d.ts +18 -0
- package/src/tokens/runtime/format-validator.generated.js +13029 -0
- package/src/tokens/runtime/index.ts +78 -0
- package/src/tokens/runtime/package-version.generated.ts +9 -0
- package/src/tokens/runtime/projection.ts +65 -0
- package/src/tokens/runtime/registry.ts +365 -0
- package/src/tokens/runtime/resolve-extends.ts +211 -0
- package/src/tokens/runtime/resolve-references.ts +313 -0
- package/src/tokens/runtime/resolve-shared.ts +186 -0
- package/src/tokens/runtime/resolve.ts +246 -0
- package/src/tokens/runtime/resolver-order.ts +326 -0
- package/src/tokens/runtime/resolver-validator.generated.d.ts +18 -0
- package/src/tokens/runtime/resolver-validator.generated.js +15492 -0
- package/src/tokens/runtime/source-digest.ts +49 -0
- package/src/tokens/runtime/stylesheet-skeleton.ts +123 -0
- package/src/tokens/runtime/theme-sources.ts +121 -0
- package/src/tokens/runtime/theme-stylesheet.ts +177 -0
- package/src/tokens/runtime/types.ts +140 -0
- package/src/tokens/runtime/validate-resolver.ts +338 -0
- package/src/tokens/runtime/validate-schema.ts +269 -0
- package/src/tokens/runtime/validate-tokens.ts +181 -0
- package/src/tokens/runtime/validate-values.ts +396 -0
- package/src/tokens/runtime/validate.ts +57 -0
- package/src/tokens/runtime/value-color-parse.ts +309 -0
- package/src/tokens/runtime/value-color.ts +217 -0
- package/src/tokens/runtime/value-entry.ts +52 -0
- package/src/tokens/runtime/value-formatters.ts +414 -0
- package/src/tokens/sets/colors.tokens.json +65 -13
- package/src/tokens/sets/foundation.tokens.json +23 -0
- package/src/tokens/sets/semantic.tokens.json +136 -30
- package/src/utilities/highlighter.ts +5 -5
- package/src/utilities/retrying-loader-cache.ts +3 -6
- package/dist/components/code-block/code-block-default-highlighter.d.ts +0 -40
- package/dist/server/index.server-298z43yn.js +0 -4
- package/dist/server/index.server-3h11wjyh.js +0 -8
- package/dist/server/index.server-a3aerbfr.js +0 -4
- package/dist/server/index.server-kge3zp6n.js +0 -4
- package/dist/server/index.server-syf81nwz.js +0 -6
- package/dist/server/index.server-zs08f5te.js +0 -3
- package/src/components/code-block/code-block-default-highlighter.ts +0 -63
|
@@ -0,0 +1,256 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Builds the token authoring catalogue: one record per public registry entry,
|
|
3
|
+
* with per-context provenance, a CSS usage profile, and the edit / CSS-export /
|
|
4
|
+
* DTCG-portability disposition.
|
|
5
|
+
*
|
|
6
|
+
* Takes `(resolver, documentsByPath)` rather than reading the disk, so a
|
|
7
|
+
* fixture corpus feeds it exactly as the real one does. Reuses the registry's
|
|
8
|
+
* base index and theme-awareness instead of re-walking the corpus, and the
|
|
9
|
+
* provenance tracer instead of re-deriving evaluation.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import { NO_PALETTE, paletteOf, validatePalettes } from './authoring-palette.ts';
|
|
13
|
+
import { PROFILES, selectProfile } from './authoring-profiles.ts';
|
|
14
|
+
import { collectCinderExtensions, traceProvenance } from './authoring-provenance.ts';
|
|
15
|
+
import {
|
|
16
|
+
UNKNOWN,
|
|
17
|
+
type AuthoringCatalogue,
|
|
18
|
+
type AuthoringLayer,
|
|
19
|
+
type AuthoringRecord,
|
|
20
|
+
type ProvenanceVariant,
|
|
21
|
+
type TokenContextProvenance,
|
|
22
|
+
type TokenDisposition,
|
|
23
|
+
} from './authoring-types.ts';
|
|
24
|
+
import { authoringContexts, authoringLayerStack } from './context-stack.ts';
|
|
25
|
+
import type { CorpusEntry } from './corpus.ts';
|
|
26
|
+
import { buildBaseIndex, buildTokenRegistryFromIndexes, themeAwarePaths } from './registry.ts';
|
|
27
|
+
import { mergeAndExpandExtends } from './resolve.ts';
|
|
28
|
+
import type { ResolverDocument, TokenDocument } from './types.ts';
|
|
29
|
+
|
|
30
|
+
/** Scale name -> the token group whose public literal dimension members form it. */
|
|
31
|
+
export const DEFAULT_SCALE_GROUPS: Readonly<Record<string, string>> = {
|
|
32
|
+
spacing: 'space',
|
|
33
|
+
radius: 'radius',
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
export type CatalogueOptions = {
|
|
37
|
+
scaleGroups?: Readonly<Record<string, string>>;
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
type Extensions = Record<string, unknown>;
|
|
41
|
+
|
|
42
|
+
function dispositionOf(
|
|
43
|
+
path: string,
|
|
44
|
+
entry: CorpusEntry,
|
|
45
|
+
extensionsByContext: Extensions[],
|
|
46
|
+
): TokenDisposition {
|
|
47
|
+
const flags = new Set(extensionsByContext.map((ext) => ext['nonRepresentableValue'] === true));
|
|
48
|
+
if (flags.size > 1) {
|
|
49
|
+
throw new Error(
|
|
50
|
+
`Token "${path}" is css-only in some contexts and portable in others; nonRepresentableValue must be consistent.`,
|
|
51
|
+
);
|
|
52
|
+
}
|
|
53
|
+
if (flags.has(true)) {
|
|
54
|
+
if (typeof entry.cssRecipe !== 'string' || entry.cssRecipe.trim() === '') {
|
|
55
|
+
throw new Error(`CSS-only token "${path}" carries no cssRecipe, so it has no CSS value.`);
|
|
56
|
+
}
|
|
57
|
+
return {
|
|
58
|
+
edit: 'css-only',
|
|
59
|
+
cssExport: 'supported',
|
|
60
|
+
dtcgExport: 'none',
|
|
61
|
+
cssSerialization: 'trusted-recipe',
|
|
62
|
+
cssValue: entry.cssRecipe,
|
|
63
|
+
reason:
|
|
64
|
+
'The real CSS value has no faithful DTCG representation; the token is excluded from resolved contexts and portable export.',
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
// A recipe belongs to the declaration that wins in a context: a theme override
|
|
68
|
+
// that restates a literal value carries none, so recipes are judged per context.
|
|
69
|
+
const withRecipe = extensionsByContext.filter(
|
|
70
|
+
(ext) => typeof ext['cssRecipe'] === 'string' && ext['cssRecipe'].trim() !== '',
|
|
71
|
+
).length;
|
|
72
|
+
if (withRecipe === extensionsByContext.length) {
|
|
73
|
+
return {
|
|
74
|
+
edit: 'recipe',
|
|
75
|
+
cssExport: 'supported',
|
|
76
|
+
dtcgExport: 'subset',
|
|
77
|
+
cssSerialization: 'trusted-recipe',
|
|
78
|
+
reason: 'The DTCG value is exported; the trusted CSS recipe that governs emission is not.',
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
if (withRecipe > 0 || entry.cssRecipe !== undefined) {
|
|
82
|
+
return {
|
|
83
|
+
edit: 'supported',
|
|
84
|
+
cssExport: 'supported',
|
|
85
|
+
dtcgExport: 'subset',
|
|
86
|
+
cssSerialization: 'mixed',
|
|
87
|
+
reason:
|
|
88
|
+
'Some contexts emit a trusted CSS recipe and others a typed value; the DTCG value is exported, the recipe is not.',
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
return {
|
|
92
|
+
edit: 'supported',
|
|
93
|
+
cssExport: 'supported',
|
|
94
|
+
dtcgExport: 'complete',
|
|
95
|
+
cssSerialization: 'typed-value',
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Reads the scale membership the corpus declares and checks it against the
|
|
101
|
+
* group it is defined over: every public literal dimension member of the group
|
|
102
|
+
* is marked, every mark sits on such a member, and each appears exactly once.
|
|
103
|
+
*/
|
|
104
|
+
function collectScales(
|
|
105
|
+
baseIndex: ReadonlyMap<string, CorpusEntry>,
|
|
106
|
+
baseExtensions: ReadonlyMap<string, Extensions>,
|
|
107
|
+
scaleGroups: Readonly<Record<string, string>>,
|
|
108
|
+
): Record<string, string[]> {
|
|
109
|
+
const scales: Record<string, string[]> = {};
|
|
110
|
+
const claimed = new Map<string, string>();
|
|
111
|
+
for (const [path, extensions] of baseExtensions) {
|
|
112
|
+
const scale = extensions['scale'];
|
|
113
|
+
if (scale === undefined) continue;
|
|
114
|
+
if (typeof scale !== 'string' || scaleGroups[scale] === undefined) {
|
|
115
|
+
const shown = typeof scale === 'string' ? scale : JSON.stringify(scale);
|
|
116
|
+
throw new Error(`Token "${path}" declares unknown scale "${shown}".`);
|
|
117
|
+
}
|
|
118
|
+
claimed.set(path, scale);
|
|
119
|
+
}
|
|
120
|
+
for (const [scale, group] of Object.entries(scaleGroups)) {
|
|
121
|
+
const members: string[] = [];
|
|
122
|
+
for (const entry of baseIndex.values()) {
|
|
123
|
+
const inGroup =
|
|
124
|
+
entry.path.startsWith(`${group}.`) && !entry.path.slice(group.length + 1).includes('.');
|
|
125
|
+
const marked = claimed.get(entry.path) === scale;
|
|
126
|
+
const literalDimension =
|
|
127
|
+
entry.public === true && entry.type === 'dimension' && typeof entry.value !== 'string';
|
|
128
|
+
if (marked && !(inGroup && literalDimension)) {
|
|
129
|
+
throw new Error(
|
|
130
|
+
`Scale "${scale}" member "${entry.path}" is not a public literal dimension in group "${group}".`,
|
|
131
|
+
);
|
|
132
|
+
}
|
|
133
|
+
if (inGroup && literalDimension && !marked) {
|
|
134
|
+
throw new Error(
|
|
135
|
+
`Public literal dimension "${entry.path}" in group "${group}" lacks scale "${scale}" metadata.`,
|
|
136
|
+
);
|
|
137
|
+
}
|
|
138
|
+
if (marked) members.push(entry.path);
|
|
139
|
+
}
|
|
140
|
+
scales[scale] = members;
|
|
141
|
+
}
|
|
142
|
+
return scales;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/** Builds the catalogue for a loaded corpus. */
|
|
146
|
+
export function buildAuthoringCatalogue(
|
|
147
|
+
resolver: ResolverDocument,
|
|
148
|
+
documentsByPath: Map<string, TokenDocument>,
|
|
149
|
+
options: CatalogueOptions = {},
|
|
150
|
+
): AuthoringCatalogue {
|
|
151
|
+
const baseIndex = buildBaseIndex(resolver, documentsByPath);
|
|
152
|
+
const registry = buildTokenRegistryFromIndexes(
|
|
153
|
+
baseIndex,
|
|
154
|
+
themeAwarePaths(resolver, documentsByPath),
|
|
155
|
+
);
|
|
156
|
+
const contexts = authoringContexts(resolver);
|
|
157
|
+
const stacks = new Map(
|
|
158
|
+
contexts.map((c) => [c.id, authoringLayerStack(resolver, documentsByPath, c)]),
|
|
159
|
+
);
|
|
160
|
+
const traced = new Map(
|
|
161
|
+
[...stacks].map(([id, stack]) => [
|
|
162
|
+
id,
|
|
163
|
+
traceProvenance(stack, { cssPropertyToPath: registry.cssPropertyToPath }),
|
|
164
|
+
]),
|
|
165
|
+
);
|
|
166
|
+
const extensions = new Map(
|
|
167
|
+
[...stacks].map(([id, stack]) => [
|
|
168
|
+
id,
|
|
169
|
+
collectCinderExtensions(mergeAndExpandExtends(stack.map((layer) => layer.content))),
|
|
170
|
+
]),
|
|
171
|
+
);
|
|
172
|
+
const scales = collectScales(
|
|
173
|
+
baseIndex,
|
|
174
|
+
new Map(
|
|
175
|
+
[...baseIndex.keys()].map((path) => [path, extensions.get(contexts[0]!.id)!.get(path) ?? {}]),
|
|
176
|
+
),
|
|
177
|
+
options.scaleGroups ?? DEFAULT_SCALE_GROUPS,
|
|
178
|
+
);
|
|
179
|
+
const scaleOf = new Map(
|
|
180
|
+
Object.entries(scales).flatMap(([scale, members]) => members.map((m) => [m, scale] as const)),
|
|
181
|
+
);
|
|
182
|
+
|
|
183
|
+
const records: AuthoringRecord[] = [];
|
|
184
|
+
const usedProfiles = new Set<string>();
|
|
185
|
+
for (const entry of registry.entries) {
|
|
186
|
+
// Private runtime variables are not part of the authoring surface.
|
|
187
|
+
if (!entry.public || !entry.cssProperty.startsWith('--cinder-')) continue;
|
|
188
|
+
const base = baseIndex.get(entry.path)!;
|
|
189
|
+
const profile = selectProfile({
|
|
190
|
+
path: entry.path,
|
|
191
|
+
type: base.type,
|
|
192
|
+
category: entry.category,
|
|
193
|
+
hasRecipe: base.cssRecipe !== undefined,
|
|
194
|
+
});
|
|
195
|
+
usedProfiles.add(profile.id);
|
|
196
|
+
const variants: ProvenanceVariant[] = [];
|
|
197
|
+
const byKey = new Map<string, ProvenanceVariant>();
|
|
198
|
+
for (const context of contexts) {
|
|
199
|
+
const provenance: TokenContextProvenance | undefined = traced
|
|
200
|
+
.get(context.id)!
|
|
201
|
+
.get(entry.path);
|
|
202
|
+
if (!provenance) {
|
|
203
|
+
throw new Error(`Token "${entry.path}" is not defined in context ${context.id}.`);
|
|
204
|
+
}
|
|
205
|
+
const key = JSON.stringify(provenance);
|
|
206
|
+
const existing = byKey.get(key);
|
|
207
|
+
if (existing) existing.contexts.push(context.id);
|
|
208
|
+
else {
|
|
209
|
+
const variant = { contexts: [context.id], provenance };
|
|
210
|
+
byKey.set(key, variant);
|
|
211
|
+
variants.push(variant);
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
const scale = scaleOf.get(entry.path);
|
|
215
|
+
const palette = paletteOf(
|
|
216
|
+
entry.path,
|
|
217
|
+
contexts.map((c) => extensions.get(c.id)!.get(entry.path) ?? {}),
|
|
218
|
+
);
|
|
219
|
+
if (palette !== undefined && base.type !== 'color') {
|
|
220
|
+
throw new Error(`Token "${entry.path}" declares a palette but is not a color.`);
|
|
221
|
+
}
|
|
222
|
+
records.push({
|
|
223
|
+
path: entry.path,
|
|
224
|
+
cssProperty: entry.cssProperty,
|
|
225
|
+
category: entry.category ?? UNKNOWN,
|
|
226
|
+
component: entry.component ?? null,
|
|
227
|
+
description: entry.description ?? UNKNOWN,
|
|
228
|
+
themeAware: entry.themeAware,
|
|
229
|
+
deprecated: entry.deprecated,
|
|
230
|
+
profile: profile.id,
|
|
231
|
+
...(scale ? { scale } : {}),
|
|
232
|
+
...(base.type === 'color' ? { palette: palette ?? NO_PALETTE } : {}),
|
|
233
|
+
disposition: dispositionOf(
|
|
234
|
+
entry.path,
|
|
235
|
+
base,
|
|
236
|
+
contexts.map((c) => extensions.get(c.id)!.get(entry.path) ?? {}),
|
|
237
|
+
),
|
|
238
|
+
variants,
|
|
239
|
+
});
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
validatePalettes(records);
|
|
243
|
+
|
|
244
|
+
const layerStacks: Record<string, AuthoringLayer[]> = {};
|
|
245
|
+
for (const [id, stack] of stacks)
|
|
246
|
+
layerStacks[id] = stack.map(({ document, kind, layer }) => ({ document, kind, layer }));
|
|
247
|
+
return {
|
|
248
|
+
version: 1,
|
|
249
|
+
dtcgVersion: '2025.10',
|
|
250
|
+
contexts: contexts.map(({ id, theme, motion }) => ({ id, theme, motion })),
|
|
251
|
+
layerStacks,
|
|
252
|
+
profiles: Object.fromEntries([...usedProfiles].toSorted().map((id) => [id, PROFILES[id]!])),
|
|
253
|
+
scales,
|
|
254
|
+
records,
|
|
255
|
+
};
|
|
256
|
+
}
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Palette membership for the authoring catalogue. A color token joins a palette family only by
|
|
3
|
+
* declaring `palette: { family, role, anchor? }` in its `com.lostgradient.cinder` extension;
|
|
4
|
+
* nothing is inferred from a token-name prefix, so palette generation cannot reach an unrelated
|
|
5
|
+
* group. The metadata is value-neutral: it never changes a token's value or its CSS.
|
|
6
|
+
*
|
|
7
|
+
* Each family must have exactly one anchor in every concrete context, roles are unique within a
|
|
8
|
+
* family, and a token declares the same membership in every context.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import {
|
|
12
|
+
PALETTE_FAMILIES,
|
|
13
|
+
UNKNOWN,
|
|
14
|
+
type AuthoringRecord,
|
|
15
|
+
type PaletteFamily,
|
|
16
|
+
type PaletteMembership,
|
|
17
|
+
} from './authoring-types.ts';
|
|
18
|
+
|
|
19
|
+
type Extensions = Record<string, unknown>;
|
|
20
|
+
|
|
21
|
+
const PALETTE_KEYS = new Set(['family', 'role', 'anchor']);
|
|
22
|
+
|
|
23
|
+
const isRecord = (value: unknown): value is Record<string, unknown> =>
|
|
24
|
+
typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
25
|
+
|
|
26
|
+
const isFamily = (value: unknown): value is PaletteFamily =>
|
|
27
|
+
PALETTE_FAMILIES.some((family) => family === value);
|
|
28
|
+
|
|
29
|
+
/** Parses one declared `palette` extension, or throws a path-addressed error. */
|
|
30
|
+
function parseMembership(path: string, raw: unknown): PaletteMembership {
|
|
31
|
+
if (!isRecord(raw)) throw new Error(`Token "${path}" declares a palette that is not an object.`);
|
|
32
|
+
for (const key of Object.keys(raw)) {
|
|
33
|
+
if (!PALETTE_KEYS.has(key))
|
|
34
|
+
throw new Error(`Token "${path}" palette declares unknown "${key}".`);
|
|
35
|
+
}
|
|
36
|
+
const { family, role, anchor } = raw;
|
|
37
|
+
if (!isFamily(family)) {
|
|
38
|
+
const shown = typeof family === 'string' ? family : JSON.stringify(family);
|
|
39
|
+
throw new Error(`Token "${path}" declares unknown palette family "${shown}".`);
|
|
40
|
+
}
|
|
41
|
+
if (typeof role !== 'string' || role.trim() === '') {
|
|
42
|
+
throw new Error(`Token "${path}" palette needs a non-empty role.`);
|
|
43
|
+
}
|
|
44
|
+
if (anchor !== undefined && typeof anchor !== 'boolean') {
|
|
45
|
+
throw new Error(`Token "${path}" palette anchor must be a boolean.`);
|
|
46
|
+
}
|
|
47
|
+
return { family, role, anchor: anchor === true };
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** The palette a color token declares in each context, or `undefined` when it declares none. */
|
|
51
|
+
export function paletteOf(
|
|
52
|
+
path: string,
|
|
53
|
+
extensionsByContext: readonly Extensions[],
|
|
54
|
+
): PaletteMembership | undefined {
|
|
55
|
+
const declared = extensionsByContext.map((extensions) =>
|
|
56
|
+
extensions['palette'] === undefined ? undefined : parseMembership(path, extensions['palette']),
|
|
57
|
+
);
|
|
58
|
+
const first = declared[0];
|
|
59
|
+
if (declared.some((membership) => JSON.stringify(membership) !== JSON.stringify(first))) {
|
|
60
|
+
throw new Error(`Token "${path}" declares a different palette in different contexts.`);
|
|
61
|
+
}
|
|
62
|
+
return first;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export const NO_PALETTE = { family: UNKNOWN, role: UNKNOWN, anchor: UNKNOWN } as const;
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Checks the declared memberships as a whole: every family has exactly one anchor and no role
|
|
69
|
+
* repeats inside a family. Every context declares the same membership, so the check holds for
|
|
70
|
+
* each concrete context.
|
|
71
|
+
*/
|
|
72
|
+
export function validatePalettes(records: readonly AuthoringRecord[]): void {
|
|
73
|
+
type Member = PaletteMembership & { path: string };
|
|
74
|
+
const families = new Map<PaletteFamily, Member[]>();
|
|
75
|
+
for (const record of records) {
|
|
76
|
+
const palette = record.palette;
|
|
77
|
+
if (palette === undefined || palette.family === UNKNOWN) continue;
|
|
78
|
+
const membership = palette;
|
|
79
|
+
const members = families.get(membership.family) ?? [];
|
|
80
|
+
members.push({ ...membership, path: record.path });
|
|
81
|
+
families.set(membership.family, members);
|
|
82
|
+
}
|
|
83
|
+
for (const [family, members] of families) {
|
|
84
|
+
const anchors = members.filter((member) => member.anchor);
|
|
85
|
+
if (anchors.length === 0) {
|
|
86
|
+
throw new Error(`Palette family "${family}" has no anchor.`);
|
|
87
|
+
}
|
|
88
|
+
if (anchors.length > 1) {
|
|
89
|
+
const paths = anchors.map((member) => member.path).join(', ');
|
|
90
|
+
throw new Error(`Palette family "${family}" has more than one anchor: ${paths}.`);
|
|
91
|
+
}
|
|
92
|
+
const roles = new Set<string>();
|
|
93
|
+
for (const member of members) {
|
|
94
|
+
if (roles.has(member.role)) {
|
|
95
|
+
throw new Error(`Palette family "${family}" repeats the role "${member.role}".`);
|
|
96
|
+
}
|
|
97
|
+
roles.add(member.role);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
}
|
|
@@ -0,0 +1,288 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* CSS usage profiles: the numeric and structural contract each public token
|
|
3
|
+
* must honor, given the CSS properties it feeds.
|
|
4
|
+
*
|
|
5
|
+
* DTCG says a `number` is any number and a `dimension` any signed length. CSS
|
|
6
|
+
* is stricter per property (an opacity outside 0-1 is clamped, a negative
|
|
7
|
+
* padding is invalid, a fractional z-index is not an integer), so a theme
|
|
8
|
+
* editor needs the per-property limits, not the type's. A profile is that
|
|
9
|
+
* limit. It is looked up from an explicit ordered rule table over type,
|
|
10
|
+
* category, and path, never inferred from a name prefix alone, and the lookup
|
|
11
|
+
* fails loudly on a token no rule covers or two rules disagree about.
|
|
12
|
+
*
|
|
13
|
+
* No Node imports: TB05's value validation and the browser runtime reuse this.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
import type { ProfileDefinition, ProfileRange } from './authoring-types.ts';
|
|
17
|
+
import type { TokenType } from './types.ts';
|
|
18
|
+
|
|
19
|
+
const NON_NEGATIVE: Omit<ProfileRange, 'part'> = { finite: true, min: 0 };
|
|
20
|
+
|
|
21
|
+
function dimension(
|
|
22
|
+
id: string,
|
|
23
|
+
description: string,
|
|
24
|
+
cssUsage: string[],
|
|
25
|
+
range: Omit<ProfileRange, 'part'> | undefined,
|
|
26
|
+
): ProfileDefinition {
|
|
27
|
+
return {
|
|
28
|
+
id,
|
|
29
|
+
description,
|
|
30
|
+
types: ['dimension'],
|
|
31
|
+
cssUsage,
|
|
32
|
+
...(range ? { range: { part: 'dimension.value', ...range } } : {}),
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function plain(id: string, type: TokenType, description: string, cssUsage: string[]) {
|
|
37
|
+
return { id, description, types: [type], cssUsage } satisfies ProfileDefinition;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** DTCG 2025.10 named font weights (the `fontWeight` value schema's string enum). */
|
|
41
|
+
export const FONT_WEIGHT_KEYWORDS = [
|
|
42
|
+
'thin',
|
|
43
|
+
'hairline',
|
|
44
|
+
'extra-light',
|
|
45
|
+
'ultra-light',
|
|
46
|
+
'light',
|
|
47
|
+
'normal',
|
|
48
|
+
'regular',
|
|
49
|
+
'book',
|
|
50
|
+
'medium',
|
|
51
|
+
'semi-bold',
|
|
52
|
+
'demi-bold',
|
|
53
|
+
'bold',
|
|
54
|
+
'extra-bold',
|
|
55
|
+
'ultra-bold',
|
|
56
|
+
'black',
|
|
57
|
+
'heavy',
|
|
58
|
+
'extra-black',
|
|
59
|
+
'ultra-black',
|
|
60
|
+
];
|
|
61
|
+
|
|
62
|
+
const PROFILE_LIST: ProfileDefinition[] = [
|
|
63
|
+
plain('color', 'color', 'A color of any supported DTCG color space.', [
|
|
64
|
+
'color',
|
|
65
|
+
'background-color',
|
|
66
|
+
'border-color',
|
|
67
|
+
'outline-color',
|
|
68
|
+
'fill',
|
|
69
|
+
]),
|
|
70
|
+
plain('font-family', 'fontFamily', 'An ordered font stack.', ['font-family']),
|
|
71
|
+
plain('easing', 'cubicBezier', 'A cubic-bezier timing function.', [
|
|
72
|
+
'transition-timing-function',
|
|
73
|
+
'animation-timing-function',
|
|
74
|
+
]),
|
|
75
|
+
plain('shadow', 'shadow', 'One or more shadow layers.', ['box-shadow']),
|
|
76
|
+
plain('border', 'border', 'A composite border.', ['border']),
|
|
77
|
+
plain('transition', 'transition', 'A composite transition.', ['transition']),
|
|
78
|
+
plain('gradient', 'gradient', 'A gradient stop list.', ['background-image']),
|
|
79
|
+
plain('typography', 'typography', 'A composite typography style.', ['font']),
|
|
80
|
+
plain('stroke-style', 'strokeStyle', 'A named or dashed stroke style.', ['border-style']),
|
|
81
|
+
{
|
|
82
|
+
id: 'opacity',
|
|
83
|
+
description: 'A finite alpha between 0 and 1 inclusive.',
|
|
84
|
+
types: ['number'],
|
|
85
|
+
cssUsage: ['opacity'],
|
|
86
|
+
range: { part: 'number', finite: true, min: 0, max: 1 },
|
|
87
|
+
},
|
|
88
|
+
{
|
|
89
|
+
id: 'z-index',
|
|
90
|
+
description: 'A signed 32-bit integer stacking order.',
|
|
91
|
+
types: ['number'],
|
|
92
|
+
cssUsage: ['z-index'],
|
|
93
|
+
range: {
|
|
94
|
+
part: 'number',
|
|
95
|
+
finite: true,
|
|
96
|
+
integer: true,
|
|
97
|
+
min: -2147483648,
|
|
98
|
+
max: 2147483647,
|
|
99
|
+
},
|
|
100
|
+
},
|
|
101
|
+
{
|
|
102
|
+
id: 'font-weight',
|
|
103
|
+
description: 'A weight from 1 to 1000, or a named DTCG weight.',
|
|
104
|
+
types: ['fontWeight'],
|
|
105
|
+
cssUsage: ['font-weight'],
|
|
106
|
+
range: { part: 'fontWeight', finite: true, min: 1, max: 1000 },
|
|
107
|
+
keywords: FONT_WEIGHT_KEYWORDS,
|
|
108
|
+
},
|
|
109
|
+
{
|
|
110
|
+
id: 'duration',
|
|
111
|
+
description: 'A non-negative time.',
|
|
112
|
+
types: ['duration'],
|
|
113
|
+
cssUsage: ['transition-duration', 'animation-duration'],
|
|
114
|
+
range: { part: 'duration.value', ...NON_NEGATIVE },
|
|
115
|
+
},
|
|
116
|
+
dimension('font-size', 'A strictly positive length.', ['font-size'], {
|
|
117
|
+
finite: true,
|
|
118
|
+
min: 0,
|
|
119
|
+
minExclusive: true,
|
|
120
|
+
}),
|
|
121
|
+
{
|
|
122
|
+
id: 'line-height',
|
|
123
|
+
description: 'A non-negative unitless multiplier.',
|
|
124
|
+
types: ['number', 'dimension'],
|
|
125
|
+
cssUsage: ['line-height'],
|
|
126
|
+
range: { part: 'number', ...NON_NEGATIVE },
|
|
127
|
+
},
|
|
128
|
+
dimension('letter-spacing', 'A signed length.', ['letter-spacing'], { finite: true }),
|
|
129
|
+
{
|
|
130
|
+
id: 'tab-size',
|
|
131
|
+
description: 'A non-negative tab width in characters.',
|
|
132
|
+
types: ['number'],
|
|
133
|
+
cssUsage: ['tab-size'],
|
|
134
|
+
range: { part: 'number', ...NON_NEGATIVE },
|
|
135
|
+
},
|
|
136
|
+
dimension('spacing', 'A non-negative spacing-scale step.', ['padding', 'gap', 'margin'], {
|
|
137
|
+
...NON_NEGATIVE,
|
|
138
|
+
}),
|
|
139
|
+
dimension('padding', 'A non-negative length.', ['padding', 'padding-block', 'padding-inline'], {
|
|
140
|
+
...NON_NEGATIVE,
|
|
141
|
+
}),
|
|
142
|
+
dimension('gap', 'A non-negative length.', ['gap', 'row-gap', 'column-gap'], NON_NEGATIVE),
|
|
143
|
+
dimension('margin', 'A signed length.', ['margin', 'margin-inline', 'margin-block'], {
|
|
144
|
+
finite: true,
|
|
145
|
+
}),
|
|
146
|
+
dimension('offset', 'A signed length.', ['outline-offset', 'inset', 'translate'], {
|
|
147
|
+
finite: true,
|
|
148
|
+
}),
|
|
149
|
+
dimension('radius', 'A non-negative length.', ['border-radius'], NON_NEGATIVE),
|
|
150
|
+
{
|
|
151
|
+
id: 'width',
|
|
152
|
+
description: 'A non-negative length.',
|
|
153
|
+
types: ['dimension', 'number'],
|
|
154
|
+
cssUsage: ['width', 'inline-size', 'max-width'],
|
|
155
|
+
range: { part: 'dimension.value', ...NON_NEGATIVE },
|
|
156
|
+
},
|
|
157
|
+
dimension('height', 'A non-negative length.', ['height', 'block-size', 'min-height'], {
|
|
158
|
+
...NON_NEGATIVE,
|
|
159
|
+
}),
|
|
160
|
+
dimension('size', 'A non-negative square extent.', ['width', 'height', 'inline-size'], {
|
|
161
|
+
...NON_NEGATIVE,
|
|
162
|
+
}),
|
|
163
|
+
dimension('border-width', 'A non-negative length.', ['border-width', 'outline-width'], {
|
|
164
|
+
...NON_NEGATIVE,
|
|
165
|
+
}),
|
|
166
|
+
dimension('blur', 'A non-negative blur radius.', ['filter', 'backdrop-filter'], NON_NEGATIVE),
|
|
167
|
+
{
|
|
168
|
+
id: 'aspect-ratio',
|
|
169
|
+
description: 'A strictly positive ratio.',
|
|
170
|
+
types: ['number'],
|
|
171
|
+
cssUsage: ['aspect-ratio'],
|
|
172
|
+
range: { part: 'number', finite: true, min: 0, minExclusive: true },
|
|
173
|
+
},
|
|
174
|
+
];
|
|
175
|
+
|
|
176
|
+
export const PROFILES: Readonly<Record<string, ProfileDefinition>> = Object.fromEntries(
|
|
177
|
+
PROFILE_LIST.map((profile) => [profile.id, profile]),
|
|
178
|
+
);
|
|
179
|
+
|
|
180
|
+
/** What a rule needs to know about a token. */
|
|
181
|
+
export type ProfileSubject = {
|
|
182
|
+
path: string;
|
|
183
|
+
type: TokenType | undefined;
|
|
184
|
+
category: string | undefined;
|
|
185
|
+
/** A trusted `cssRecipe` governs the CSS, so the DTCG type need not match the profile. */
|
|
186
|
+
hasRecipe: boolean;
|
|
187
|
+
};
|
|
188
|
+
|
|
189
|
+
type ProfileRule = {
|
|
190
|
+
profile: string;
|
|
191
|
+
type?: TokenType;
|
|
192
|
+
category?: string;
|
|
193
|
+
path?: RegExp;
|
|
194
|
+
unless?: RegExp;
|
|
195
|
+
};
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* Every rule that matches must name the same profile. Rules are narrow on
|
|
199
|
+
* purpose: a new public token that none matches fails generation and has to be
|
|
200
|
+
* given a deliberate usage contract.
|
|
201
|
+
*/
|
|
202
|
+
export const PROFILE_RULES: readonly ProfileRule[] = [
|
|
203
|
+
{ type: 'color', profile: 'color' },
|
|
204
|
+
{ type: 'fontFamily', profile: 'font-family' },
|
|
205
|
+
{ type: 'cubicBezier', profile: 'easing' },
|
|
206
|
+
{ type: 'shadow', profile: 'shadow' },
|
|
207
|
+
{ type: 'border', profile: 'border' },
|
|
208
|
+
{ type: 'transition', profile: 'transition' },
|
|
209
|
+
{ type: 'gradient', profile: 'gradient' },
|
|
210
|
+
{ type: 'typography', profile: 'typography' },
|
|
211
|
+
{ type: 'strokeStyle', profile: 'stroke-style' },
|
|
212
|
+
{ type: 'fontWeight', profile: 'font-weight' },
|
|
213
|
+
{ type: 'duration', profile: 'duration' },
|
|
214
|
+
{ category: 'opacity', profile: 'opacity' },
|
|
215
|
+
{ category: 'z-index', profile: 'z-index' },
|
|
216
|
+
{ category: 'radius', profile: 'radius' },
|
|
217
|
+
{ category: 'spacing', path: /^space\./, profile: 'spacing' },
|
|
218
|
+
{ category: 'spacing', path: /(^|[.-])gap(\.|$)/, profile: 'gap' },
|
|
219
|
+
{ category: 'spacing', path: /(^|[.-])(pad|padding)(\.|-|$)|indent-step$/, profile: 'padding' },
|
|
220
|
+
{ category: 'spacing', path: /bleed$|overlap$/, profile: 'margin' },
|
|
221
|
+
{ category: 'size', path: /(^|[.-])height(\.|$)/, profile: 'height' },
|
|
222
|
+
{ category: 'size', path: /(^|\.)width(\.|$)/, unless: /^ring\./, profile: 'width' },
|
|
223
|
+
{ category: 'size', path: /^ring\.width$|thickness$/, profile: 'border-width' },
|
|
224
|
+
{ category: 'size', path: /(^|\.)offset$/, profile: 'offset' },
|
|
225
|
+
{ category: 'size', path: /(^|\.)blur$/, profile: 'blur' },
|
|
226
|
+
{ category: 'size', path: /(^|[.-])size$|target-min$/, profile: 'size' },
|
|
227
|
+
{ category: 'layout', path: /(^|[.-])(width|slide-size)(\.|$)/, profile: 'width' },
|
|
228
|
+
{ category: 'layout', path: /aspect-ratio$/, profile: 'aspect-ratio' },
|
|
229
|
+
{ category: 'typography', path: /(^|[.-])font[.-]size(\.|$)/, profile: 'font-size' },
|
|
230
|
+
{ category: 'typography', path: /^tracking\./, profile: 'letter-spacing' },
|
|
231
|
+
{ category: 'typography', path: /^leading\.|(^|\.)line-height$/, profile: 'line-height' },
|
|
232
|
+
{ category: 'typography', path: /tab-size$/, profile: 'tab-size' },
|
|
233
|
+
];
|
|
234
|
+
|
|
235
|
+
function matches(rule: ProfileRule, subject: ProfileSubject): boolean {
|
|
236
|
+
if (rule.type !== undefined && rule.type !== subject.type) return false;
|
|
237
|
+
if (rule.category !== undefined && rule.category !== subject.category) return false;
|
|
238
|
+
if (rule.path !== undefined && !rule.path.test(subject.path)) return false;
|
|
239
|
+
if (rule.unless !== undefined && rule.unless.test(subject.path)) return false;
|
|
240
|
+
return true;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
/**
|
|
244
|
+
* The usage profile for a token. Throws when no rule covers it, when rules
|
|
245
|
+
* disagree, or when the matched profile cannot describe the token's type.
|
|
246
|
+
*/
|
|
247
|
+
export function selectProfile(
|
|
248
|
+
subject: ProfileSubject,
|
|
249
|
+
rules: readonly ProfileRule[] = PROFILE_RULES,
|
|
250
|
+
profiles: Readonly<Record<string, ProfileDefinition>> = PROFILES,
|
|
251
|
+
): ProfileDefinition {
|
|
252
|
+
const matched = [
|
|
253
|
+
...new Set(rules.filter((rule) => matches(rule, subject)).map((r) => r.profile)),
|
|
254
|
+
];
|
|
255
|
+
const where = `Token "${subject.path}" (type ${subject.type ?? 'unknown'}, category ${subject.category ?? 'unknown'})`;
|
|
256
|
+
if (matched.length === 0) {
|
|
257
|
+
throw new Error(`${where} has no CSS usage profile; add a rule to authoring-profiles.ts.`);
|
|
258
|
+
}
|
|
259
|
+
if (matched.length > 1) {
|
|
260
|
+
throw new Error(`${where} has contradictory usage profiles: ${matched.join(', ')}.`);
|
|
261
|
+
}
|
|
262
|
+
const profile = profiles[matched[0]!];
|
|
263
|
+
if (!profile) throw new Error(`${where} maps to unknown profile "${matched[0]}".`);
|
|
264
|
+
if (!subject.hasRecipe && (subject.type === undefined || !profile.types.includes(subject.type))) {
|
|
265
|
+
throw new Error(
|
|
266
|
+
`${where} is contradictory with profile "${profile.id}", which accepts ${profile.types.join(', ')}.`,
|
|
267
|
+
);
|
|
268
|
+
}
|
|
269
|
+
return profile;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
/** The first reason `value` violates the profile's numeric range, or undefined when it fits. */
|
|
273
|
+
export function profileRangeViolation(
|
|
274
|
+
profile: ProfileDefinition,
|
|
275
|
+
value: number | string,
|
|
276
|
+
): string | undefined {
|
|
277
|
+
if (typeof value === 'string') {
|
|
278
|
+
return profile.keywords?.includes(value) ? undefined : `"${value}" is not a named value`;
|
|
279
|
+
}
|
|
280
|
+
const range = profile.range;
|
|
281
|
+
if (!range) return undefined;
|
|
282
|
+
if (!Number.isFinite(value)) return 'must be finite';
|
|
283
|
+
if (range.integer && !Number.isInteger(value)) return 'must be an integer';
|
|
284
|
+
if (range.min !== undefined && (range.minExclusive ? value <= range.min : value < range.min))
|
|
285
|
+
return `must be ${range.minExclusive ? '>' : '>='} ${range.min}`;
|
|
286
|
+
if (range.max !== undefined && value > range.max) return `must be <= ${range.max}`;
|
|
287
|
+
return undefined;
|
|
288
|
+
}
|