@lostgradient/cinder 0.27.1 → 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 +35 -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,324 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The token pairing table: which foreground token is painted on which background token, how
|
|
3
|
+
* the pairing is classified, and which rule scope owns its threshold.
|
|
4
|
+
*
|
|
5
|
+
* It is declarative data. The Cinder contrast suites and the theme builder's accessibility
|
|
6
|
+
* engine both read it, so a pairing is added in one place. A pairing names CSS custom
|
|
7
|
+
* properties because that is what a context's property map is keyed by; the registry maps them
|
|
8
|
+
* to token paths.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import { DECORATIVE_BORDER, FAINT_BORDER, type ContrastClassification } from './thresholds.ts';
|
|
12
|
+
|
|
13
|
+
/** `wcag` pairings set a normative threshold; `cinder-advisory` ones are Cinder's stricter floors. */
|
|
14
|
+
export type PairingScope = 'wcag' | 'cinder-advisory';
|
|
15
|
+
|
|
16
|
+
export type PairingGroup =
|
|
17
|
+
| 'text'
|
|
18
|
+
| 'accent'
|
|
19
|
+
| 'status'
|
|
20
|
+
| 'severity'
|
|
21
|
+
| 'terminal'
|
|
22
|
+
| 'focus'
|
|
23
|
+
| 'control-border'
|
|
24
|
+
| 'chart'
|
|
25
|
+
| 'decorative-border'
|
|
26
|
+
| 'polarity'
|
|
27
|
+
| 'disabled';
|
|
28
|
+
|
|
29
|
+
export type ContrastPairing = {
|
|
30
|
+
/** Stable across releases; findings key on it. */
|
|
31
|
+
id: string;
|
|
32
|
+
group: PairingGroup;
|
|
33
|
+
classification: ContrastClassification;
|
|
34
|
+
scope: PairingScope;
|
|
35
|
+
/** WCAG success criterion the pairing is evidence for, when it has one. */
|
|
36
|
+
criterion: '1.4.3' | '1.4.11' | null;
|
|
37
|
+
foreground: string;
|
|
38
|
+
background: string;
|
|
39
|
+
/** What a translucent background is painted over. Required when `background` has alpha. */
|
|
40
|
+
backdrop?: string;
|
|
41
|
+
/** A number property multiplying the foreground's alpha (the disabled opacity). */
|
|
42
|
+
opacity?: string;
|
|
43
|
+
/** Cinder's floor for an advisory pairing. WCAG pairings take theirs from the classification. */
|
|
44
|
+
advisoryFloor?: number;
|
|
45
|
+
description: string;
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
export const SURFACE_PROPERTIES = [
|
|
49
|
+
'--cinder-surface-canvas',
|
|
50
|
+
'--cinder-surface',
|
|
51
|
+
'--cinder-surface-raised',
|
|
52
|
+
'--cinder-surface-inset',
|
|
53
|
+
] as const;
|
|
54
|
+
|
|
55
|
+
const SURFACE_LABELS: Record<(typeof SURFACE_PROPERTIES)[number], string> = {
|
|
56
|
+
'--cinder-surface-canvas': 'canvas',
|
|
57
|
+
'--cinder-surface': 'surface',
|
|
58
|
+
'--cinder-surface-raised': 'raised',
|
|
59
|
+
'--cinder-surface-inset': 'inset',
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
export const STATUSES = ['info', 'success', 'warning', 'danger'] as const;
|
|
63
|
+
export const ANSI_COLORS = [
|
|
64
|
+
'black',
|
|
65
|
+
'red',
|
|
66
|
+
'green',
|
|
67
|
+
'yellow',
|
|
68
|
+
'blue',
|
|
69
|
+
'magenta',
|
|
70
|
+
'cyan',
|
|
71
|
+
'white',
|
|
72
|
+
'bright-black',
|
|
73
|
+
'bright-red',
|
|
74
|
+
'bright-green',
|
|
75
|
+
'bright-yellow',
|
|
76
|
+
'bright-blue',
|
|
77
|
+
'bright-magenta',
|
|
78
|
+
'bright-cyan',
|
|
79
|
+
'bright-white',
|
|
80
|
+
] as const;
|
|
81
|
+
export const CHART_SERIES_COUNT = 8;
|
|
82
|
+
|
|
83
|
+
const onSurfaces = (
|
|
84
|
+
idPrefix: string,
|
|
85
|
+
foreground: string,
|
|
86
|
+
base: Omit<ContrastPairing, 'id' | 'foreground' | 'background' | 'backdrop' | 'description'>,
|
|
87
|
+
description: string,
|
|
88
|
+
): ContrastPairing[] =>
|
|
89
|
+
SURFACE_PROPERTIES.map((surface) => ({
|
|
90
|
+
...base,
|
|
91
|
+
id: `${idPrefix}:${SURFACE_LABELS[surface]}`,
|
|
92
|
+
foreground,
|
|
93
|
+
background: surface,
|
|
94
|
+
description: `${description} on ${SURFACE_LABELS[surface]}`,
|
|
95
|
+
}));
|
|
96
|
+
|
|
97
|
+
const text = (
|
|
98
|
+
group: PairingGroup,
|
|
99
|
+
scope: PairingScope = 'wcag',
|
|
100
|
+
): Pick<ContrastPairing, 'group' | 'classification' | 'scope' | 'criterion'> => ({
|
|
101
|
+
group,
|
|
102
|
+
classification: 'text',
|
|
103
|
+
scope,
|
|
104
|
+
criterion: scope === 'wcag' ? '1.4.3' : null,
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
function statusPairings(): ContrastPairing[] {
|
|
108
|
+
const pairings: ContrastPairing[] = [
|
|
109
|
+
{
|
|
110
|
+
id: 'status:neutral:text',
|
|
111
|
+
...text('status'),
|
|
112
|
+
foreground: '--cinder-status-neutral-text',
|
|
113
|
+
background: '--cinder-status-neutral-background',
|
|
114
|
+
description: 'Neutral status text on its background',
|
|
115
|
+
},
|
|
116
|
+
];
|
|
117
|
+
for (const status of STATUSES) {
|
|
118
|
+
const prefix = `--cinder-status-${status}`;
|
|
119
|
+
pairings.push({
|
|
120
|
+
id: `status:${status}:text`,
|
|
121
|
+
...text('status'),
|
|
122
|
+
foreground: `${prefix}-text`,
|
|
123
|
+
background: `${prefix}-background`,
|
|
124
|
+
description: `${status} status text on its background`,
|
|
125
|
+
});
|
|
126
|
+
for (const state of ['solid', 'solid-hover', 'solid-active'] as const) {
|
|
127
|
+
pairings.push({
|
|
128
|
+
id: `status:${status}:contrast:${state}`,
|
|
129
|
+
...text('status'),
|
|
130
|
+
foreground: `${prefix}-contrast`,
|
|
131
|
+
background: `${prefix}-${state}`,
|
|
132
|
+
description: `${status} label on the ${state} fill`,
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
return pairings;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
function decorativePairings(): ContrastPairing[] {
|
|
140
|
+
const advisory = (floor: number, group: PairingGroup = 'decorative-border') => ({
|
|
141
|
+
group,
|
|
142
|
+
classification: 'decorative-separator' as const,
|
|
143
|
+
scope: 'cinder-advisory' as const,
|
|
144
|
+
criterion: null,
|
|
145
|
+
advisoryFloor: floor,
|
|
146
|
+
});
|
|
147
|
+
const pairings: ContrastPairing[] = [
|
|
148
|
+
...onSurfaces(
|
|
149
|
+
'border:muted',
|
|
150
|
+
'--cinder-border-muted',
|
|
151
|
+
advisory(DECORATIVE_BORDER),
|
|
152
|
+
'Muted border',
|
|
153
|
+
),
|
|
154
|
+
...onSurfaces('border:faint', '--cinder-border-faint', advisory(FAINT_BORDER), 'Faint border'),
|
|
155
|
+
{
|
|
156
|
+
id: 'border:accent',
|
|
157
|
+
...advisory(DECORATIVE_BORDER, 'accent'),
|
|
158
|
+
foreground: '--cinder-accent-border',
|
|
159
|
+
background: '--cinder-accent-background',
|
|
160
|
+
description: 'Accent border on the accent background',
|
|
161
|
+
},
|
|
162
|
+
{
|
|
163
|
+
id: 'border:neutral-status',
|
|
164
|
+
...advisory(DECORATIVE_BORDER, 'status'),
|
|
165
|
+
foreground: '--cinder-status-neutral-border',
|
|
166
|
+
background: '--cinder-status-neutral-background',
|
|
167
|
+
description: 'Neutral status border on its background',
|
|
168
|
+
},
|
|
169
|
+
];
|
|
170
|
+
for (const status of STATUSES) {
|
|
171
|
+
pairings.push({
|
|
172
|
+
id: `border:${status}-status`,
|
|
173
|
+
...advisory(DECORATIVE_BORDER, 'status'),
|
|
174
|
+
foreground: `--cinder-status-${status}-border`,
|
|
175
|
+
background: `--cinder-status-${status}-background`,
|
|
176
|
+
description: `${status} status border on its background`,
|
|
177
|
+
});
|
|
178
|
+
}
|
|
179
|
+
return pairings;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/*
|
|
183
|
+
* `--cinder-accent-solid-active` is deliberately not paired with the accent label: Cinder's buttons
|
|
184
|
+
* paint the pressed fill with `--cinder-accent-solid-active-on-fill` instead, because the general
|
|
185
|
+
* active tone drops the label below 4.5:1 in the dark arm.
|
|
186
|
+
*/
|
|
187
|
+
export const CONTRAST_PAIRINGS: readonly ContrastPairing[] = [
|
|
188
|
+
...onSurfaces('text:default', '--cinder-text-default', text('text'), 'Default text'),
|
|
189
|
+
...onSurfaces('text:muted', '--cinder-text-muted', text('text'), 'Muted text'),
|
|
190
|
+
...['--cinder-surface', '--cinder-surface-canvas', '--cinder-surface-inset'].map(
|
|
191
|
+
(background): ContrastPairing => ({
|
|
192
|
+
id: `accent:text:${background.replace('--cinder-', '')}`,
|
|
193
|
+
...text('accent'),
|
|
194
|
+
foreground: '--cinder-accent-text',
|
|
195
|
+
background,
|
|
196
|
+
description: `Accent text on ${background.replace('--cinder-', '')}`,
|
|
197
|
+
}),
|
|
198
|
+
),
|
|
199
|
+
...(['solid', 'solid-hover', 'solid-active-on-fill'] as const).map((state): ContrastPairing => ({
|
|
200
|
+
id: `accent:contrast:${state}`,
|
|
201
|
+
...text('accent'),
|
|
202
|
+
foreground: '--cinder-accent-contrast',
|
|
203
|
+
background: `--cinder-accent-${state}`,
|
|
204
|
+
description: `Accent label on the ${state} fill`,
|
|
205
|
+
})),
|
|
206
|
+
{
|
|
207
|
+
id: 'accent:soft:text',
|
|
208
|
+
...text('accent'),
|
|
209
|
+
foreground: '--cinder-accent-text',
|
|
210
|
+
background: '--cinder-accent-background',
|
|
211
|
+
description: 'Accent text on the soft accent background',
|
|
212
|
+
},
|
|
213
|
+
...statusPairings(),
|
|
214
|
+
{
|
|
215
|
+
id: 'severity:critical',
|
|
216
|
+
...text('severity'),
|
|
217
|
+
foreground: '--cinder-severity-critical',
|
|
218
|
+
background: '--cinder-severity-critical-background',
|
|
219
|
+
description: 'Critical severity text on its background',
|
|
220
|
+
},
|
|
221
|
+
{
|
|
222
|
+
id: 'text:status-dot',
|
|
223
|
+
...text('text'),
|
|
224
|
+
foreground: '--cinder-status-dot-color',
|
|
225
|
+
background: '--cinder-surface-inset',
|
|
226
|
+
description: 'Status dot color on the inset surface',
|
|
227
|
+
},
|
|
228
|
+
{
|
|
229
|
+
id: 'text:toc-link',
|
|
230
|
+
...text('text'),
|
|
231
|
+
foreground: '--cinder-table-of-contents-link-color',
|
|
232
|
+
background: '--cinder-surface-canvas',
|
|
233
|
+
description: 'Table of contents link on the canvas',
|
|
234
|
+
},
|
|
235
|
+
{
|
|
236
|
+
id: 'text:toc-link-active',
|
|
237
|
+
...text('text'),
|
|
238
|
+
foreground: '--cinder-table-of-contents-link-active-color',
|
|
239
|
+
background: '--cinder-surface-hover',
|
|
240
|
+
description: 'Active table of contents link on a hovered surface',
|
|
241
|
+
},
|
|
242
|
+
...ANSI_COLORS.map((name): ContrastPairing => ({
|
|
243
|
+
id: `terminal:ansi:${name}`,
|
|
244
|
+
...text('terminal'),
|
|
245
|
+
foreground: `--cinder-terminal-ansi-${name}`,
|
|
246
|
+
background: '--cinder-surface-inset',
|
|
247
|
+
description: `Terminal ${name} on the inset surface`,
|
|
248
|
+
})),
|
|
249
|
+
...(['--cinder-surface-canvas', '--cinder-surface'] as const).map(
|
|
250
|
+
(background): ContrastPairing => ({
|
|
251
|
+
id: `focus:ring:${SURFACE_LABELS[background]}`,
|
|
252
|
+
group: 'focus',
|
|
253
|
+
classification: 'non-text-indicator',
|
|
254
|
+
scope: 'wcag',
|
|
255
|
+
criterion: '1.4.11',
|
|
256
|
+
foreground: '--cinder-ring-color',
|
|
257
|
+
background,
|
|
258
|
+
description: `Focus ring against ${SURFACE_LABELS[background]}`,
|
|
259
|
+
}),
|
|
260
|
+
),
|
|
261
|
+
...onSurfaces(
|
|
262
|
+
'border:control',
|
|
263
|
+
'--cinder-border',
|
|
264
|
+
{
|
|
265
|
+
group: 'control-border',
|
|
266
|
+
classification: 'non-text-indicator',
|
|
267
|
+
scope: 'wcag',
|
|
268
|
+
criterion: '1.4.11',
|
|
269
|
+
},
|
|
270
|
+
'Control border',
|
|
271
|
+
),
|
|
272
|
+
...onSurfaces(
|
|
273
|
+
'border:strong',
|
|
274
|
+
'--cinder-border-strong',
|
|
275
|
+
{
|
|
276
|
+
group: 'control-border',
|
|
277
|
+
classification: 'non-text-indicator',
|
|
278
|
+
scope: 'wcag',
|
|
279
|
+
criterion: '1.4.11',
|
|
280
|
+
},
|
|
281
|
+
'Strong control border',
|
|
282
|
+
),
|
|
283
|
+
...Array.from({ length: CHART_SERIES_COUNT }, (_, index): ContrastPairing[] =>
|
|
284
|
+
(['--cinder-surface-canvas', '--cinder-surface'] as const).map((background) => ({
|
|
285
|
+
id: `chart:series-${index + 1}:${SURFACE_LABELS[background]}`,
|
|
286
|
+
group: 'chart',
|
|
287
|
+
classification: 'non-text-indicator',
|
|
288
|
+
scope: 'wcag',
|
|
289
|
+
criterion: '1.4.11',
|
|
290
|
+
foreground: `--cinder-chart-series-${index + 1}`,
|
|
291
|
+
background,
|
|
292
|
+
description: `Chart series ${index + 1} against ${SURFACE_LABELS[background]}`,
|
|
293
|
+
})),
|
|
294
|
+
).flat(),
|
|
295
|
+
...onSurfaces(
|
|
296
|
+
'polarity-ink',
|
|
297
|
+
'--cinder-polarity-ink',
|
|
298
|
+
{ ...text('polarity', 'cinder-advisory'), advisoryFloor: 4.5 },
|
|
299
|
+
'Polarity ink',
|
|
300
|
+
),
|
|
301
|
+
...decorativePairings(),
|
|
302
|
+
...onSurfaces(
|
|
303
|
+
'disabled:text',
|
|
304
|
+
'--cinder-text-disabled',
|
|
305
|
+
{
|
|
306
|
+
group: 'disabled',
|
|
307
|
+
classification: 'disabled',
|
|
308
|
+
scope: 'wcag',
|
|
309
|
+
criterion: null,
|
|
310
|
+
},
|
|
311
|
+
'Disabled text',
|
|
312
|
+
),
|
|
313
|
+
{
|
|
314
|
+
id: 'disabled:opacity',
|
|
315
|
+
group: 'disabled',
|
|
316
|
+
classification: 'disabled',
|
|
317
|
+
scope: 'wcag',
|
|
318
|
+
criterion: null,
|
|
319
|
+
foreground: '--cinder-text-default',
|
|
320
|
+
background: '--cinder-surface',
|
|
321
|
+
opacity: '--cinder-opacity-disabled',
|
|
322
|
+
description: 'Default text dimmed by the disabled opacity',
|
|
323
|
+
},
|
|
324
|
+
];
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WCAG 2.2 AA contrast thresholds and the Cinder floors that sit beside them.
|
|
3
|
+
*
|
|
4
|
+
* Every comparison here is on the unrounded ratio: 4.499 does not meet 4.5 and 2.999 does not
|
|
5
|
+
* meet 3. Nothing rounds before comparing.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
/** WCAG 1.4.3: body text. */
|
|
9
|
+
export const AA_TEXT = 4.5;
|
|
10
|
+
/** WCAG 1.4.3 for large text, and 1.4.11 for meaningful non-text contrast. */
|
|
11
|
+
export const NON_TEXT = 3.0;
|
|
12
|
+
/** The large-text threshold, named for what it applies to. */
|
|
13
|
+
export const AA_LARGE_TEXT = 3.0;
|
|
14
|
+
|
|
15
|
+
/** Large text is at least 24 CSS px, or at least 18.6667 CSS px (14pt) and bold. */
|
|
16
|
+
export const LARGE_TEXT_PX = 24;
|
|
17
|
+
export const LARGE_BOLD_TEXT_PX = 18.6667;
|
|
18
|
+
export const BOLD_FONT_WEIGHT = 700;
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Cinder's own floors for decorative separators. They are stricter than WCAG asks for (WCAG
|
|
22
|
+
* exempts a purely decorative separator), so they are advisory: visible, never able to change
|
|
23
|
+
* pass or fail.
|
|
24
|
+
*/
|
|
25
|
+
export const DECORATIVE_BORDER = 1.4;
|
|
26
|
+
export const FAINT_BORDER = 1.1;
|
|
27
|
+
/** Border tiers must read within this fraction of the larger ratio across the legal surfaces. */
|
|
28
|
+
export const BORDER_TIER_MAX_SPREAD = 0.15;
|
|
29
|
+
/** Chart series: the minimum pairwise CIEDE2000 distance and CIE L* separation. */
|
|
30
|
+
export const CHART_MIN_DELTA_E = 12;
|
|
31
|
+
export const CHART_MIN_DELTA_L = 4;
|
|
32
|
+
export const CHART_MIN_CHROMA = 0.06;
|
|
33
|
+
|
|
34
|
+
export const CONTRAST_CLASSIFICATIONS = [
|
|
35
|
+
'text',
|
|
36
|
+
'large-text',
|
|
37
|
+
'non-text-indicator',
|
|
38
|
+
'decorative-separator',
|
|
39
|
+
'disabled',
|
|
40
|
+
] as const;
|
|
41
|
+
export type ContrastClassification = (typeof CONTRAST_CLASSIFICATIONS)[number];
|
|
42
|
+
|
|
43
|
+
/** The rendered size of a piece of text, in CSS px and a numeric font weight. */
|
|
44
|
+
export type RenderedTextSize = { fontSizePx: number; fontWeight: number };
|
|
45
|
+
|
|
46
|
+
/** WCAG's large-text definition. An unknown size is never large. */
|
|
47
|
+
export function isLargeText(size: RenderedTextSize | undefined): boolean {
|
|
48
|
+
if (size === undefined) return false;
|
|
49
|
+
const { fontSizePx, fontWeight } = size;
|
|
50
|
+
if (!Number.isFinite(fontSizePx) || !Number.isFinite(fontWeight)) return false;
|
|
51
|
+
if (fontSizePx >= LARGE_TEXT_PX) return true;
|
|
52
|
+
return fontSizePx >= LARGE_BOLD_TEXT_PX && fontWeight >= BOLD_FONT_WEIGHT;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** The text classification a rendered size earns: only a known large size earns the 3:1 floor. */
|
|
56
|
+
export function classifyText(size: RenderedTextSize | undefined): 'text' | 'large-text' {
|
|
57
|
+
return isLargeText(size) ? 'large-text' : 'text';
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* The normative WCAG minimum for a classification, or `null` when WCAG sets none (a purely
|
|
62
|
+
* decorative separator and an inactive component are exempt from contrast requirements).
|
|
63
|
+
*/
|
|
64
|
+
export function requiredContrast(classification: ContrastClassification): number | null {
|
|
65
|
+
switch (classification) {
|
|
66
|
+
case 'text':
|
|
67
|
+
return AA_TEXT;
|
|
68
|
+
case 'large-text':
|
|
69
|
+
return AA_LARGE_TEXT;
|
|
70
|
+
case 'non-text-indicator':
|
|
71
|
+
return NON_TEXT;
|
|
72
|
+
case 'decorative-separator':
|
|
73
|
+
case 'disabled':
|
|
74
|
+
return null;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** Whether a measured ratio meets a required one. Both stay unrounded. */
|
|
79
|
+
export function meetsContrast(measured: number, required: number): boolean {
|
|
80
|
+
return measured >= required;
|
|
81
|
+
}
|
|
@@ -0,0 +1,289 @@
|
|
|
1
|
+
import {
|
|
2
|
+
validateModifierSetExpansionOrder,
|
|
3
|
+
validateModifierTokenPaths,
|
|
4
|
+
} from './corpus-validation.ts';
|
|
5
|
+
import { resolveDocuments } from './resolve.ts';
|
|
6
|
+
import {
|
|
7
|
+
buildContextSourcesIndex,
|
|
8
|
+
combinations,
|
|
9
|
+
expandContextSources,
|
|
10
|
+
expandSetSources,
|
|
11
|
+
normalizeSourcePath,
|
|
12
|
+
normalizedDocumentsByPath,
|
|
13
|
+
parseResolutionOrder,
|
|
14
|
+
validateExpandedSources,
|
|
15
|
+
} from './resolver-order.ts';
|
|
16
|
+
import { TokenValidationError, type ResolverDocument, type TokenDocument } from './types.ts';
|
|
17
|
+
import { validateResolverDocument } from './validate-resolver.ts';
|
|
18
|
+
import { assertSafeExtensionMetadata } from './validate-schema.ts';
|
|
19
|
+
import { validateResolvedToken } from './validate-tokens.ts';
|
|
20
|
+
import { assertValidTokenDocument } from './validate.ts';
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Expands a set's `sources` into plain token-document `$ref`s only, recursing
|
|
24
|
+
* through any resolver-internal `#/sets/<name>` entries (a set's own sources
|
|
25
|
+
* may themselves reference further sets) before any of those entries reach
|
|
26
|
+
* the file-existence check, `referencedPaths`, or a document-loading
|
|
27
|
+
* consumer (`sourcesForEntry`'s other callers -- `generate.ts`, `registry.ts`
|
|
28
|
+
* -- resolve source `$ref`s straight into `documentsByPath` lookups with no
|
|
29
|
+
* expansion of their own, so an unexpanded internal ref there is a
|
|
30
|
+
* non-null-assertion crash, not a validation error). `visiting` guards
|
|
31
|
+
* against a cycle between sets; per the vendored resolver schema's own
|
|
32
|
+
* stated prohibition (`set.json`'s `referenceObjectForSets` pattern), a set
|
|
33
|
+
* may not reference a modifier, which is rejected here with a named path and
|
|
34
|
+
* reason rather than falling through to a misleading "source does not
|
|
35
|
+
* exist".
|
|
36
|
+
*/
|
|
37
|
+
|
|
38
|
+
export function orderedTokenValidationContexts(
|
|
39
|
+
resolver: ResolverDocument,
|
|
40
|
+
documentsByPath: Map<string, unknown>,
|
|
41
|
+
): Map<string, unknown[][]> {
|
|
42
|
+
documentsByPath = normalizedDocumentsByPath(documentsByPath);
|
|
43
|
+
const expandedSets = Object.keys(resolver.sets).map((setName) => ({
|
|
44
|
+
setName,
|
|
45
|
+
sources: expandSetSources(resolver, setName),
|
|
46
|
+
}));
|
|
47
|
+
const expandedContexts = Object.entries(resolver.modifiers).flatMap(([modifierName, modifier]) =>
|
|
48
|
+
Object.keys(modifier.contexts).map((contextName) => ({
|
|
49
|
+
modifierName,
|
|
50
|
+
contextName,
|
|
51
|
+
sources: expandContextSources(resolver, modifierName, contextName),
|
|
52
|
+
})),
|
|
53
|
+
);
|
|
54
|
+
const resolutionOrder = parseResolutionOrder(resolver);
|
|
55
|
+
validateExpandedSources(expandedSets, expandedContexts, documentsByPath);
|
|
56
|
+
const setSourcesByName = new Map(expandedSets.map((entry) => [entry.setName, entry.sources]));
|
|
57
|
+
const contextSourcesByModifier = buildContextSourcesIndex(expandedContexts);
|
|
58
|
+
const pathByDocument = new Map(
|
|
59
|
+
[...documentsByPath.entries()].map(([path, document]) => [document, path]),
|
|
60
|
+
);
|
|
61
|
+
const contextsByPath = new Map<string, unknown[][]>();
|
|
62
|
+
for (const modifierValues of combinations(resolver)) {
|
|
63
|
+
const orderedDocuments = resolutionOrder.flatMap((entry) => {
|
|
64
|
+
const sources =
|
|
65
|
+
entry.kind === 'sets'
|
|
66
|
+
? setSourcesByName.get(entry.name)!
|
|
67
|
+
: contextSourcesByModifier.get(entry.name)!.get(modifierValues[entry.name]!)!;
|
|
68
|
+
return sources.map((source) => documentsByPath.get(normalizeSourcePath(source.$ref))!);
|
|
69
|
+
});
|
|
70
|
+
const uniqueDocuments = [...new Set(orderedDocuments)];
|
|
71
|
+
for (const document of uniqueDocuments) {
|
|
72
|
+
const path = pathByDocument.get(document);
|
|
73
|
+
if (path === undefined) continue;
|
|
74
|
+
const contexts = contextsByPath.get(path) ?? [];
|
|
75
|
+
if (
|
|
76
|
+
!contexts.some(
|
|
77
|
+
(context) =>
|
|
78
|
+
context.length === orderedDocuments.length &&
|
|
79
|
+
context.every((entry, index) => entry === orderedDocuments[index]),
|
|
80
|
+
)
|
|
81
|
+
)
|
|
82
|
+
contexts.push(orderedDocuments);
|
|
83
|
+
contextsByPath.set(path, contexts);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
return contextsByPath;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export function validateLoadedTokenDocuments(
|
|
90
|
+
resolver: ResolverDocument,
|
|
91
|
+
loaded: Array<{ path: string; document: unknown }>,
|
|
92
|
+
): Array<{ path: string; document: TokenDocument }> {
|
|
93
|
+
const rawDocumentsByPath = new Map<string, unknown>();
|
|
94
|
+
for (const { path, document } of loaded) {
|
|
95
|
+
if (rawDocumentsByPath.has(path))
|
|
96
|
+
throw new TokenValidationError([
|
|
97
|
+
{
|
|
98
|
+
path: '$',
|
|
99
|
+
reason: `duplicate loaded token document path after normalization: ${normalizeSourcePath(path)}`,
|
|
100
|
+
},
|
|
101
|
+
]);
|
|
102
|
+
rawDocumentsByPath.set(path, document);
|
|
103
|
+
}
|
|
104
|
+
const documentsByPath = normalizedDocumentsByPath(rawDocumentsByPath);
|
|
105
|
+
const sourceByDocument = new Map<object, string>();
|
|
106
|
+
for (const { path, document } of loaded) {
|
|
107
|
+
if (typeof document === 'object' && document !== null) sourceByDocument.set(document, path);
|
|
108
|
+
}
|
|
109
|
+
// Validate extension metadata against each document's owning path before
|
|
110
|
+
// composing contexts. The context array intentionally contains raw
|
|
111
|
+
// documents, so attributing a malformed lookup document to the candidate
|
|
112
|
+
// source would lose the provenance needed to repair it.
|
|
113
|
+
for (const { path, document } of loaded) assertSafeExtensionMetadata(document, '', path);
|
|
114
|
+
const contextsByPath = orderedTokenValidationContexts(resolver, documentsByPath);
|
|
115
|
+
return loaded.map(({ path, document }) => {
|
|
116
|
+
const candidate = document;
|
|
117
|
+
const contexts = contextsByPath.get(normalizeSourcePath(path)) ?? [];
|
|
118
|
+
assertValidLoadedDocument(candidate, path, contexts, sourceByDocument);
|
|
119
|
+
return { path, document: candidate };
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
export function assertValidLoadedDocument(
|
|
124
|
+
document: unknown,
|
|
125
|
+
path: string,
|
|
126
|
+
contexts: readonly unknown[][],
|
|
127
|
+
sourceByDocument: ReadonlyMap<object, string>,
|
|
128
|
+
): asserts document is TokenDocument {
|
|
129
|
+
if (contexts.length === 0) assertValidTokenDocument(document, path, [], sourceByDocument);
|
|
130
|
+
else
|
|
131
|
+
for (const context of contexts) assertValidInContext(document, path, context, sourceByDocument);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
const documentIds = new WeakMap<object, number>();
|
|
135
|
+
const validatedStacks = new Set<string>();
|
|
136
|
+
const REMEMBERED_STACKS = 96;
|
|
137
|
+
let nextDocumentId = 1;
|
|
138
|
+
|
|
139
|
+
function stackKey(documents: readonly object[]): string {
|
|
140
|
+
return documents
|
|
141
|
+
.map((document) => {
|
|
142
|
+
let id = documentIds.get(document);
|
|
143
|
+
if (id === undefined) {
|
|
144
|
+
id = nextDocumentId++;
|
|
145
|
+
documentIds.set(document, id);
|
|
146
|
+
}
|
|
147
|
+
return id;
|
|
148
|
+
})
|
|
149
|
+
.join(',');
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* Resolves one context's ordered documents and validates every resolved token. A pass is
|
|
154
|
+
* remembered by the identity of the ordered documents, so a context whose documents did not
|
|
155
|
+
* change since the last validation (the dark stacks after a light-only edit) is not resolved
|
|
156
|
+
* again. Only a pass is remembered. Sources must not be mutated after validation.
|
|
157
|
+
*/
|
|
158
|
+
function assertResolvedTokensValid(documents: TokenDocument[]): void {
|
|
159
|
+
const key = stackKey(documents);
|
|
160
|
+
if (validatedStacks.has(key)) return;
|
|
161
|
+
const resolved = resolveDocuments(documents);
|
|
162
|
+
for (const [path, token] of Object.entries(resolved)) validateResolvedToken(token, path);
|
|
163
|
+
validatedStacks.add(key);
|
|
164
|
+
if (validatedStacks.size > REMEMBERED_STACKS) {
|
|
165
|
+
const oldest = validatedStacks.values().next().value;
|
|
166
|
+
if (oldest !== undefined) validatedStacks.delete(oldest);
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/** Cap on remembered ordered prefixes per document; edits mint new override identities. */
|
|
171
|
+
const REMEMBERED_PREFIXES = 24;
|
|
172
|
+
const validatedPrefixes = new WeakMap<object, unknown[][]>();
|
|
173
|
+
const extendsFlags = new WeakMap<object, boolean>();
|
|
174
|
+
|
|
175
|
+
function mentionsExtends(value: unknown): boolean {
|
|
176
|
+
if (typeof value !== 'object' || value === null) return false;
|
|
177
|
+
const cached = extendsFlags.get(value);
|
|
178
|
+
if (cached !== undefined) return cached;
|
|
179
|
+
const found = Object.entries(value).some(
|
|
180
|
+
([key, child]) =>
|
|
181
|
+
key === '$extends' || ((key === '$root' || !key.startsWith('$')) && mentionsExtends(child)),
|
|
182
|
+
);
|
|
183
|
+
extendsFlags.set(value, found);
|
|
184
|
+
return found;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
const sameIdentities = (left: readonly unknown[], right: readonly unknown[]): boolean =>
|
|
188
|
+
left.length === right.length && left.every((entry, index) => entry === right[index]);
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* Validates one document in one context's ordered stack. A document's validity depends on the
|
|
192
|
+
* ordered prefix through itself (the lookup scope matters only through `$extends`), so a pass
|
|
193
|
+
* is remembered by document and prefix identity and not repeated while neither changes: the
|
|
194
|
+
* same document appears in several contexts with the same prefix, and an edit to an override
|
|
195
|
+
* document leaves every earlier document's prefix intact. Only a pass is remembered, and only
|
|
196
|
+
* when no document in the stack uses `$extends`. Sources must not be mutated after validation.
|
|
197
|
+
*/
|
|
198
|
+
function assertValidInContext(
|
|
199
|
+
document: unknown,
|
|
200
|
+
path: string,
|
|
201
|
+
context: readonly unknown[],
|
|
202
|
+
sourceByDocument: ReadonlyMap<object, string>,
|
|
203
|
+
): void {
|
|
204
|
+
const position = context.indexOf(document);
|
|
205
|
+
const memoizable =
|
|
206
|
+
position >= 0 &&
|
|
207
|
+
typeof document === 'object' &&
|
|
208
|
+
document !== null &&
|
|
209
|
+
!context.some(mentionsExtends);
|
|
210
|
+
if (!memoizable) {
|
|
211
|
+
assertValidTokenDocument(document, path, context, sourceByDocument);
|
|
212
|
+
return;
|
|
213
|
+
}
|
|
214
|
+
const prefix = context.slice(0, position + 1);
|
|
215
|
+
const remembered = validatedPrefixes.get(document) ?? [];
|
|
216
|
+
if (remembered.some((entry) => sameIdentities(entry, prefix))) return;
|
|
217
|
+
assertValidTokenDocument(document, path, context, sourceByDocument);
|
|
218
|
+
validatedPrefixes.set(document, [prefix, ...remembered].slice(0, REMEMBERED_PREFIXES));
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
* Validates a whole corpus in memory: the resolver, every token document
|
|
223
|
+
* (schema first, then semantic checks), the set and modifier expansion order,
|
|
224
|
+
* modifier token paths, and every concrete modifier-value combination's
|
|
225
|
+
* resolved tokens. Throws a `TokenValidationError` addressed by path. The
|
|
226
|
+
* caller supplies documents; this reads nothing.
|
|
227
|
+
*/
|
|
228
|
+
export function validateCorpus(
|
|
229
|
+
resolver: ResolverDocument,
|
|
230
|
+
loaded: Array<{ path: string; document: unknown }>,
|
|
231
|
+
): Map<string, TokenDocument> {
|
|
232
|
+
validateResolverDocument(resolver);
|
|
233
|
+
|
|
234
|
+
const expandedSets = Object.keys(resolver.sets).map((setName) => ({
|
|
235
|
+
setName,
|
|
236
|
+
sources: expandSetSources(resolver, setName),
|
|
237
|
+
}));
|
|
238
|
+
const expandedContexts = Object.entries(resolver.modifiers).flatMap(([modifierName, modifier]) =>
|
|
239
|
+
Object.keys(modifier.contexts).map((contextName) => ({
|
|
240
|
+
modifierName,
|
|
241
|
+
contextName,
|
|
242
|
+
sources: expandContextSources(resolver, modifierName, contextName),
|
|
243
|
+
})),
|
|
244
|
+
);
|
|
245
|
+
|
|
246
|
+
const referencedPaths = new Set<string>([
|
|
247
|
+
...expandedSets.flatMap(({ sources }) =>
|
|
248
|
+
sources.map((source) => normalizeSourcePath(source.$ref)),
|
|
249
|
+
),
|
|
250
|
+
...expandedContexts.flatMap(({ sources }) =>
|
|
251
|
+
sources.map((source) => normalizeSourcePath(source.$ref)),
|
|
252
|
+
),
|
|
253
|
+
]);
|
|
254
|
+
const unreferencedDocuments = loaded
|
|
255
|
+
.filter(({ path }) => !referencedPaths.has(normalizeSourcePath(path)))
|
|
256
|
+
.map(({ path }) => ({ path, reason: 'token document is not referenced by the resolver' }));
|
|
257
|
+
if (unreferencedDocuments.length > 0) throw new TokenValidationError(unreferencedDocuments);
|
|
258
|
+
|
|
259
|
+
const documents = validateLoadedTokenDocuments(resolver, loaded);
|
|
260
|
+
const documentsByPath = normalizedDocumentsByPath(
|
|
261
|
+
new Map(documents.map(({ path, document }) => [path, document])),
|
|
262
|
+
);
|
|
263
|
+
validateModifierSetExpansionOrder(resolver, documentsByPath);
|
|
264
|
+
validateModifierTokenPaths(resolver, documentsByPath);
|
|
265
|
+
|
|
266
|
+
const resolutionOrder = parseResolutionOrder(resolver);
|
|
267
|
+
// `expandedSets`/`expandedContexts` above already expanded every set's and
|
|
268
|
+
// every context's sources exactly once (internal `#/sets/<name>` refs
|
|
269
|
+
// recursively resolved). Indexing them here, rather than calling
|
|
270
|
+
// `sourcesForEntry` again per resolutionOrder entry, avoids re-running that
|
|
271
|
+
// recursive expansion once per modifier-value COMBINATION below -- this
|
|
272
|
+
// loop runs the cartesian product of every modifier's contexts, so an
|
|
273
|
+
// unmemoized re-expansion is a real multiplier as the corpus grows more
|
|
274
|
+
// modifiers or contexts, even though the expansion itself is invariant per
|
|
275
|
+
// set/context and only needs computing once.
|
|
276
|
+
const setSourcesByName = new Map(expandedSets.map(({ setName, sources }) => [setName, sources]));
|
|
277
|
+
const contextSourcesByModifier = buildContextSourcesIndex(expandedContexts);
|
|
278
|
+
for (const modifierValues of combinations(resolver)) {
|
|
279
|
+
const orderedDocuments = resolutionOrder.flatMap((entry) => {
|
|
280
|
+
const sources =
|
|
281
|
+
entry.kind === 'sets'
|
|
282
|
+
? setSourcesByName.get(entry.name)!
|
|
283
|
+
: contextSourcesByModifier.get(entry.name)!.get(modifierValues[entry.name]!)!;
|
|
284
|
+
return sources.map((source) => documentsByPath.get(normalizeSourcePath(source.$ref))!);
|
|
285
|
+
});
|
|
286
|
+
assertResolvedTokensValid(orderedDocuments);
|
|
287
|
+
}
|
|
288
|
+
return documentsByPath;
|
|
289
|
+
}
|