@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,309 @@
|
|
|
1
|
+
import valueParser from 'postcss-value-parser';
|
|
2
|
+
|
|
3
|
+
/** A node from a parsed CSS value -- see `postcss-value-parser`'s `ParsedValue.nodes`. */
|
|
4
|
+
export type ColorValueNode = ReturnType<typeof valueParser>['nodes'][number];
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* CSS color functions that are a COMPLETE color on their own, so their body is
|
|
8
|
+
* never re-examined -- `oklch()`, `rgb()`, and the rest all take numbers (and,
|
|
9
|
+
* for `oklch(from … l c h)`, a nested relative-color source) as their own
|
|
10
|
+
* arguments, so descending into one would flag the bare component list it is
|
|
11
|
+
* SUPPOSED to contain.
|
|
12
|
+
*
|
|
13
|
+
* This is a CLOSED allowlist, drawn from the CSS Color Module functions plus
|
|
14
|
+
* every function the real corpus recipes a color with (verified by
|
|
15
|
+
* `generate.test.ts`'s "every color declaration the corpus emits" case, which
|
|
16
|
+
* runs {@link findBareColorComponents} over the committed `tokens-base.css`
|
|
17
|
+
* and fails if a real function name is missing here). CSS adds new color
|
|
18
|
+
* functions rarely and deliberately, so growing this set is a deliberate,
|
|
19
|
+
* reviewable edit -- unlike the open set this replaces, which treated ANY
|
|
20
|
+
* unrecognised function as a complete color and so silently accepted whatever
|
|
21
|
+
* CSS (or a typo) introduced next. Anything not in this set, and not one of
|
|
22
|
+
* the two functions below, is presumed NOT a complete color: fail closed.
|
|
23
|
+
*/
|
|
24
|
+
export const COLOR_FUNCTIONS = new Set([
|
|
25
|
+
'rgb',
|
|
26
|
+
'rgba',
|
|
27
|
+
'hsl',
|
|
28
|
+
'hsla',
|
|
29
|
+
'hwb',
|
|
30
|
+
'lab',
|
|
31
|
+
'lch',
|
|
32
|
+
'oklab',
|
|
33
|
+
'oklch',
|
|
34
|
+
'color',
|
|
35
|
+
]);
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* The two color functions whose ARGUMENTS are themselves `<color>` values, and
|
|
39
|
+
* so have to be checked recursively by {@link findBareColorComponents}.
|
|
40
|
+
*/
|
|
41
|
+
export const COLOR_ARGUMENT_FUNCTIONS = new Set(['light-dark', 'color-mix']);
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* CSS math functions CAPABLE of standing in for the `<percentage>` mix weight
|
|
45
|
+
* in a `color-mix()` argument. `color-mix()` accepts a general `<percentage>`,
|
|
46
|
+
* not only a literal, and the weight may sit on either side of the color, so
|
|
47
|
+
* the weight has to be recognised in computed form too -- otherwise a recipe
|
|
48
|
+
* the browser accepts reads as a bare component list and fails generation.
|
|
49
|
+
*
|
|
50
|
+
* This set is deliberately narrower than "every CSS Values L4 math function":
|
|
51
|
+
* only the functions the spec defines as TYPE-PRESERVING (a `<percentage>`
|
|
52
|
+
* argument yields a `<percentage>` result, the same way a `<length>` argument
|
|
53
|
+
* yields a `<length>` result) belong here. `calc()`, `min()`, `max()`,
|
|
54
|
+
* `clamp()`, `round()`, `mod()`, `rem()`, and `abs()` are all type-preserving
|
|
55
|
+
* per the spec's calculation-type rules. `hypot()` is too -- its arguments
|
|
56
|
+
* "must be the same type... either <number>, <percentage>, or <dimension>"
|
|
57
|
+
* and the result matches that type, so `hypot(1%, 2%)` is itself a
|
|
58
|
+
* `<percentage>`, exactly like a `color-mix()` weight the browser accepts.
|
|
59
|
+
*
|
|
60
|
+
* Membership here says only that a CALL to the function is CAPABLE of
|
|
61
|
+
* producing a percentage -- it says nothing about any particular call, since
|
|
62
|
+
* every one of these functions produces whatever type its arguments do:
|
|
63
|
+
* `hypot(1px, 2px)` is a `<length>`, not a `<percentage>`, every bit as much
|
|
64
|
+
* as `hypot(1%, 2%)` is a percentage. Whether a given call actually resolves
|
|
65
|
+
* to a percentage is decided per-call by
|
|
66
|
+
* {@link mathFunctionType}, which is why this set alone is
|
|
67
|
+
* never enough to accept a node -- see {@link isUnambiguousPercentageNode}.
|
|
68
|
+
*
|
|
69
|
+
* `sign()` is deliberately EXCLUDED even though it takes the same argument
|
|
70
|
+
* types as `abs()`: unlike `abs()`, the spec defines `sign()`'s result as
|
|
71
|
+
* ALWAYS a plain `<number>` (1, -1, 0, or NaN), regardless of its argument's
|
|
72
|
+
* type -- `sign(-10%)` is `-1`, a number, never a percentage. Including it
|
|
73
|
+
* would make every call, not just a mistyped one, fail open.
|
|
74
|
+
*
|
|
75
|
+
* The exponential and trigonometric functions `pow()`, `sqrt()`, `log()`, and
|
|
76
|
+
* `exp()` are NOT included either: the spec requires their arguments (and
|
|
77
|
+
* result) to be a plain `<number>`, never a `<percentage>`, so they can never
|
|
78
|
+
* legitimately stand in for a mix weight. The trigonometric functions
|
|
79
|
+
* `sin()`, `cos()`, and `tan()` always return a `<number>`, and `asin()`,
|
|
80
|
+
* `acos()`, `atan()`, and `atan2()` always return an `<angle>` -- none of the
|
|
81
|
+
* seven ever produce a `<percentage>` either, so they are excluded for the
|
|
82
|
+
* same reason `sign()` is.
|
|
83
|
+
*
|
|
84
|
+
* `var()` is deliberately absent. A bare `var()` is ambiguous between the color
|
|
85
|
+
* and the weight, and reading it as the color is the safe direction: treating
|
|
86
|
+
* it as a weight would strip the only complete value out of the argument and
|
|
87
|
+
* flag what remains.
|
|
88
|
+
*/
|
|
89
|
+
export const PERCENTAGE_FUNCTIONS = new Set([
|
|
90
|
+
'calc',
|
|
91
|
+
'clamp',
|
|
92
|
+
'min',
|
|
93
|
+
'max',
|
|
94
|
+
'round',
|
|
95
|
+
'mod',
|
|
96
|
+
'rem',
|
|
97
|
+
'abs',
|
|
98
|
+
'hypot',
|
|
99
|
+
]);
|
|
100
|
+
|
|
101
|
+
/** `nodes` with every whitespace divider removed, so only meaningful tokens remain. */
|
|
102
|
+
export function stripSpaces(nodes: ColorValueNode[]): ColorValueNode[] {
|
|
103
|
+
return nodes.filter((node) => node.type !== 'space');
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* `nodes` split into top-level comma-separated argument groups. Nested
|
|
108
|
+
* function arguments stay whole automatically, because `postcss-value-parser`
|
|
109
|
+
* already parsed them into their own `FunctionNode.nodes` -- a comma inside
|
|
110
|
+
* `color-mix(...)` never appears in the outer array this walks.
|
|
111
|
+
*/
|
|
112
|
+
export function splitArguments(nodes: ColorValueNode[]): ColorValueNode[][] {
|
|
113
|
+
const groups: ColorValueNode[][] = [[]];
|
|
114
|
+
for (const node of nodes) {
|
|
115
|
+
if (node.type === 'div' && node.value === ',') {
|
|
116
|
+
groups.push([]);
|
|
117
|
+
continue;
|
|
118
|
+
}
|
|
119
|
+
groups[groups.length - 1]!.push(node);
|
|
120
|
+
}
|
|
121
|
+
return groups;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Everything in a `var()` call's nodes after its first top-level comma -- the
|
|
126
|
+
* fallback, verbatim, including any further commas it may itself contain
|
|
127
|
+
* (`var()`'s fallback is not re-parsed by CSS, so neither is it here). `undefined`
|
|
128
|
+
* when there is no fallback at all.
|
|
129
|
+
*/
|
|
130
|
+
export function fallbackNodes(callNodes: ColorValueNode[]): ColorValueNode[] | undefined {
|
|
131
|
+
const index = callNodes.findIndex((node) => node.type === 'div' && node.value === ',');
|
|
132
|
+
return index === -1 ? undefined : callNodes.slice(index + 1);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/** Unitless CSS calculation constants, such as the scalar in `calc(1% * pi)`. */
|
|
136
|
+
export const CALC_NUMBER_KEYWORDS = new Set(['pi', 'e', 'infinity', '-infinity', 'nan']);
|
|
137
|
+
|
|
138
|
+
// Track the percentage exponent: a number is 0, a percentage is 1. Intermediate
|
|
139
|
+
// products can have other exponents; division can cancel them again. Unsupported
|
|
140
|
+
// dimensions and malformed expressions are null, distinct from a bare variable.
|
|
141
|
+
export type MathValueType = number | 'variable' | null;
|
|
142
|
+
|
|
143
|
+
export function mathValueType(nodes: ColorValueNode[]): MathValueType {
|
|
144
|
+
const parser = {
|
|
145
|
+
items: nodes.filter((node) => node.type !== 'space' && node.type !== 'comment'),
|
|
146
|
+
position: 0,
|
|
147
|
+
};
|
|
148
|
+
return mathExpression(parser, nodes);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
export type MathParser = { items: ColorValueNode[]; position: number };
|
|
152
|
+
|
|
153
|
+
export function mathExpression(parser: MathParser, original: ColorValueNode[]): MathValueType {
|
|
154
|
+
let result = mathProduct(parser);
|
|
155
|
+
while (hasAddOperator(parser)) {
|
|
156
|
+
const operator = parser.items[parser.position++];
|
|
157
|
+
const originalIndex = original.indexOf(operator!);
|
|
158
|
+
if (!isSpacedOperator(original, originalIndex)) return null;
|
|
159
|
+
const right = mathProduct(parser);
|
|
160
|
+
if (!sameNumericType(result, right)) return null;
|
|
161
|
+
}
|
|
162
|
+
return parser.position === parser.items.length ? result : null;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
export function hasAddOperator(parser: MathParser): boolean {
|
|
166
|
+
const value = parser.items[parser.position]?.value;
|
|
167
|
+
return value === '+' || value === '-';
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
export function isSpacedOperator(nodes: ColorValueNode[], index: number): boolean {
|
|
171
|
+
return nodes[index - 1]?.type === 'space' && nodes[index + 1]?.type === 'space';
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
export function sameNumericType(left: MathValueType, right: MathValueType): boolean {
|
|
175
|
+
return typeof left === 'number' && left === right;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
export function mathProduct(parser: MathParser): MathValueType {
|
|
179
|
+
let left = mathPrimary(parser);
|
|
180
|
+
while (hasProductOperator(parser)) {
|
|
181
|
+
const operator = parser.items[parser.position++]!.value;
|
|
182
|
+
const right = mathPrimary(parser);
|
|
183
|
+
const combined = combineProduct(operator, left, right);
|
|
184
|
+
if (combined === null) return null;
|
|
185
|
+
left = combined;
|
|
186
|
+
}
|
|
187
|
+
return left;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
export function hasProductOperator(parser: MathParser): boolean {
|
|
191
|
+
const value = parser.items[parser.position]?.value;
|
|
192
|
+
return value === '*' || value === '/';
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
export function combineProduct(
|
|
196
|
+
operator: string,
|
|
197
|
+
left: MathValueType,
|
|
198
|
+
right: MathValueType,
|
|
199
|
+
): MathValueType {
|
|
200
|
+
if (left === null || right === null) return null;
|
|
201
|
+
if (operator === '*' && isVariablePercentagePair(left, right)) return 1;
|
|
202
|
+
if (typeof left !== 'number' || typeof right !== 'number') return null;
|
|
203
|
+
return operator === '*' ? left + right : left - right;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
export function isVariablePercentagePair(left: MathValueType, right: MathValueType): boolean {
|
|
207
|
+
return (left === 'variable' && right === 1) || (right === 'variable' && left === 1);
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
export function mathPrimary(parser: MathParser): MathValueType {
|
|
211
|
+
const node = parser.items[parser.position++];
|
|
212
|
+
if (!node) return null;
|
|
213
|
+
if (node.type === 'word') return mathWordType(node.value);
|
|
214
|
+
if (node.type !== 'function' || node.unclosed) return null;
|
|
215
|
+
if (node.value === '') return mathValueType(node.nodes);
|
|
216
|
+
if (node.value.toLowerCase() === 'var') return mathVariableType(node);
|
|
217
|
+
return mathFunctionType(node);
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
export function mathWordType(value: string): MathValueType {
|
|
221
|
+
const unit = valueParser.unit(value);
|
|
222
|
+
if (unit !== false) return unit.unit === '%' ? 1 : unit.unit === '' ? 0 : null;
|
|
223
|
+
return CALC_NUMBER_KEYWORDS.has(value.toLowerCase()) ? 0 : null;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
export function mathVariableType(
|
|
227
|
+
node: Extract<ColorValueNode, { type: 'function' }>,
|
|
228
|
+
): MathValueType {
|
|
229
|
+
const argumentGroups = splitArguments(node.nodes);
|
|
230
|
+
const reference = stripSpaces(argumentGroups[0]!);
|
|
231
|
+
if (
|
|
232
|
+
reference.length !== 1 ||
|
|
233
|
+
reference[0]?.type !== 'word' ||
|
|
234
|
+
!/^--[^\s,()]+$/.test(reference[0].value)
|
|
235
|
+
)
|
|
236
|
+
return null;
|
|
237
|
+
const fallback = fallbackNodes(node.nodes);
|
|
238
|
+
return fallback === undefined ? 'variable' : mathValueType(fallback);
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
export function mathFunctionType(
|
|
242
|
+
node: Extract<ColorValueNode, { type: 'function' }>,
|
|
243
|
+
): MathValueType {
|
|
244
|
+
const name = node.value.toLowerCase();
|
|
245
|
+
if (node.unclosed || !PERCENTAGE_FUNCTIONS.has(name)) return null;
|
|
246
|
+
const argumentGroups = normalizedMathArguments(name, splitArguments(node.nodes));
|
|
247
|
+
if (name === 'round' && argumentGroups.length === 1) return roundMathType(argumentGroups[0]!);
|
|
248
|
+
const arity = mathArity(name);
|
|
249
|
+
if (arity !== undefined && argumentGroups.length !== arity) return null;
|
|
250
|
+
if (name === 'clamp') return clampMathType(argumentGroups);
|
|
251
|
+
return uniformMathType(argumentGroups);
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
export function normalizedMathArguments(
|
|
255
|
+
name: string,
|
|
256
|
+
argumentGroups: ColorValueNode[][],
|
|
257
|
+
): ColorValueNode[][] {
|
|
258
|
+
if (name !== 'round') return argumentGroups;
|
|
259
|
+
const strategy = stripSpaces(argumentGroups[0]!);
|
|
260
|
+
const isStrategy =
|
|
261
|
+
strategy.length === 1 &&
|
|
262
|
+
strategy[0]?.type === 'word' &&
|
|
263
|
+
['nearest', 'up', 'down', 'to-zero'].includes(strategy[0].value.toLowerCase());
|
|
264
|
+
return isStrategy ? argumentGroups.slice(1) : argumentGroups;
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
export function mathArity(name: string): number | undefined {
|
|
268
|
+
if (name === 'calc' || name === 'abs') return 1;
|
|
269
|
+
if (name === 'clamp') return 3;
|
|
270
|
+
return ['round', 'mod', 'rem'].includes(name) ? 2 : undefined;
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
export function roundMathType(nodes: ColorValueNode[]): MathValueType {
|
|
274
|
+
return mathValueType(nodes) === 0 ? 0 : null;
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
export function clampMathType(argumentGroups: ColorValueNode[][]): MathValueType {
|
|
278
|
+
const middle = mathValueType(argumentGroups[1]!);
|
|
279
|
+
const validBound = (nodes: ColorValueNode[]) => {
|
|
280
|
+
const items = stripSpaces(nodes);
|
|
281
|
+
return (
|
|
282
|
+
(items.length === 1 &&
|
|
283
|
+
items[0]?.type === 'word' &&
|
|
284
|
+
items[0].value.toLowerCase() === 'none') ||
|
|
285
|
+
mathValueType(nodes) === middle
|
|
286
|
+
);
|
|
287
|
+
};
|
|
288
|
+
return typeof middle === 'number' &&
|
|
289
|
+
validBound(argumentGroups[0]!) &&
|
|
290
|
+
validBound(argumentGroups[2]!)
|
|
291
|
+
? middle
|
|
292
|
+
: null;
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
export function uniformMathType(argumentGroups: ColorValueNode[][]): MathValueType {
|
|
296
|
+
const types = argumentGroups.map(mathValueType);
|
|
297
|
+
const first = types[0];
|
|
298
|
+
return typeof first === 'number' && types.every((type) => type === first) ? first : null;
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
/**
|
|
302
|
+
* A CSS `<percentage-token>`: a `<number-token>` followed by `%`, or a call to
|
|
303
|
+
* a {@link PERCENTAGE_FUNCTIONS} member whose arguments DEMONSTRABLY resolve
|
|
304
|
+
* to a percentage, including its operator types and argument count.
|
|
305
|
+
* `postcss-value-parser`'s `unit` helper implements the actual
|
|
306
|
+
* `<number-token>` grammar -- a number may be signed and may carry an
|
|
307
|
+
* exponent, so `+40%`, `-0%`, `.5%`, and `4e1%` are all valid weights, and a
|
|
308
|
+
* hand-rolled digits-and-dots pattern misses three of those four.
|
|
309
|
+
*/
|
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
import valueParser from 'postcss-value-parser';
|
|
2
|
+
import type { CorpusEntry } from './corpus.ts';
|
|
3
|
+
import { tokenPathFromReference } from './resolve-shared.ts';
|
|
4
|
+
import {
|
|
5
|
+
COLOR_ARGUMENT_FUNCTIONS,
|
|
6
|
+
COLOR_FUNCTIONS,
|
|
7
|
+
type ColorValueNode,
|
|
8
|
+
fallbackNodes,
|
|
9
|
+
mathFunctionType,
|
|
10
|
+
splitArguments,
|
|
11
|
+
stripSpaces,
|
|
12
|
+
} from './value-color-parse.ts';
|
|
13
|
+
|
|
14
|
+
export function wholeTokenIndexPath(reference: string): string {
|
|
15
|
+
let path = tokenPathFromReference(reference);
|
|
16
|
+
if (!reference.startsWith('#/')) return path;
|
|
17
|
+
if (path.endsWith('.$value')) path = path.slice(0, -'.$value'.length);
|
|
18
|
+
if (path === '$root') return '';
|
|
19
|
+
return path.endsWith('.$root') ? path.slice(0, -'.$root'.length) : path;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function resolveAlias(reference: string, baseIndex: Map<string, CorpusEntry>): string {
|
|
23
|
+
const path = wholeTokenIndexPath(reference);
|
|
24
|
+
const target = baseIndex.get(path);
|
|
25
|
+
if (!target?.cssProperty) {
|
|
26
|
+
throw new Error(
|
|
27
|
+
`Alias reference "${reference}" does not resolve to a base token with a cssProperty ` +
|
|
28
|
+
`(looked up path "${path}").`,
|
|
29
|
+
);
|
|
30
|
+
}
|
|
31
|
+
return `var(${target.cssProperty})`;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Whether `reference` names a whole token that `resolveAlias` can turn into a
|
|
36
|
+
* `var(--property)` reference -- used by `serializeEntryValue` to decide
|
|
37
|
+
* whether a `$ref` alias should take that path or fall through to typed
|
|
38
|
+
* serialization instead of throwing (see `serializeEntryValue`'s doc comment).
|
|
39
|
+
*/
|
|
40
|
+
export function isWholeTokenAlias(reference: string, baseIndex: Map<string, CorpusEntry>): boolean {
|
|
41
|
+
return baseIndex.get(wholeTokenIndexPath(reference))?.cssProperty !== undefined;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export function isUnambiguousPercentageNode(node: ColorValueNode): boolean {
|
|
45
|
+
if (node.type === 'function') {
|
|
46
|
+
if (node.unclosed) return false;
|
|
47
|
+
return mathFunctionType(node) === 1;
|
|
48
|
+
}
|
|
49
|
+
if (node.type === 'word') {
|
|
50
|
+
const unit = valueParser.unit(node.value);
|
|
51
|
+
return unit !== false && unit.unit === '%';
|
|
52
|
+
}
|
|
53
|
+
return false;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* A `var()` node whose FALLBACK is a percentage, so the reference resolves to
|
|
58
|
+
* one either way: `var(--weight, 40%)`.
|
|
59
|
+
*
|
|
60
|
+
* In a `color-mix()` argument a bare `var()` is ambiguous between the color and
|
|
61
|
+
* the weight, and this is the case where it stops being ambiguous. Without it,
|
|
62
|
+
* checking the `var()` as a color position descends into the fallback and
|
|
63
|
+
* reports `40%` as a bare component list -- rejecting a recipe the browser
|
|
64
|
+
* accepts.
|
|
65
|
+
*/
|
|
66
|
+
export function isPercentageValuedVarNode(node: ColorValueNode): boolean {
|
|
67
|
+
if (node.type !== 'function' || node.value.toLowerCase() !== 'var') return false;
|
|
68
|
+
const fallback = fallbackNodes(node.nodes);
|
|
69
|
+
if (fallback === undefined) return false;
|
|
70
|
+
const items = stripSpaces(fallback);
|
|
71
|
+
return items.length === 1 && isUnambiguousPercentageNode(items[0]!);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* The node groups of a `color-mix()` argument that might be its `<color>`,
|
|
76
|
+
* each wrapped as its own single-node candidate.
|
|
77
|
+
*
|
|
78
|
+
* The argument is `<color> && <percentage>?` in either order, so the weight has
|
|
79
|
+
* to be set aside before the color can be checked. The safe way to do that is
|
|
80
|
+
* to drop only what CANNOT be a color -- a literal percentage or a math
|
|
81
|
+
* function -- and check everything else.
|
|
82
|
+
*
|
|
83
|
+
* Guessing which token is the weight is what went wrong before. An earlier
|
|
84
|
+
* version picked "whichever token is not a complete color", but the checker
|
|
85
|
+
* returned `undefined` (complete) for any function it did not recognise,
|
|
86
|
+
* `calc()` included, so a computed weight read as a complete color and the
|
|
87
|
+
* REAL color was discarded as the weight. That let the exact value CIN-242
|
|
88
|
+
* exists to reject --
|
|
89
|
+
* `color-mix(in oklch, calc(var(--w) * 1%) light-dark(100% 0 0, 0% 0 0), transparent)`
|
|
90
|
+
* -- through the gate untouched.
|
|
91
|
+
*
|
|
92
|
+
* Returning every candidate costs nothing: a `var()` passes the check anyway,
|
|
93
|
+
* so including an ambiguous one is free, while every genuine color is checked.
|
|
94
|
+
*/
|
|
95
|
+
export function mixColorCandidates(argumentNodes: ColorValueNode[]): ColorValueNode[][] {
|
|
96
|
+
const items = stripSpaces(argumentNodes);
|
|
97
|
+
if (items.length < 2) return [argumentNodes];
|
|
98
|
+
const candidates = items.filter(
|
|
99
|
+
(item) => !isUnambiguousPercentageNode(item) && !isPercentageValuedVarNode(item),
|
|
100
|
+
);
|
|
101
|
+
return candidates.length === 0 ? [argumentNodes] : candidates.map((item) => [item]);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* The first color position within `nodes` that is not a complete CSS color, as
|
|
106
|
+
* the node group to report -- or `undefined` when every position is complete.
|
|
107
|
+
*
|
|
108
|
+
* A color position is complete when it is a single hex literal or bare keyword
|
|
109
|
+
* (`transparent`, `currentColor`), a single call to one of
|
|
110
|
+
* {@link COLOR_FUNCTIONS}, a `var()` whose fallback (if any) is itself
|
|
111
|
+
* complete, or a `light-dark()`/`color-mix()` whose own color arguments are
|
|
112
|
+
* all complete. A component list is more than one top-level token -- `0 0 0`
|
|
113
|
+
* is three -- or a call to any OTHER function, which this walker has never
|
|
114
|
+
* seen before and so cannot assume is a color: FAIL CLOSED. That is the one
|
|
115
|
+
* behavior change from the string-matching version this replaces, which
|
|
116
|
+
* treated an unrecognised function as automatically complete.
|
|
117
|
+
*/
|
|
118
|
+
export function findBareInNodes(nodes: ColorValueNode[]): ColorValueNode[] | undefined {
|
|
119
|
+
const items = stripSpaces(nodes);
|
|
120
|
+
if (items.length === 0) return undefined;
|
|
121
|
+
if (items.length > 1) return nodes;
|
|
122
|
+
const node = items[0]!;
|
|
123
|
+
if (node.type === 'function') return findBareInFunction(node);
|
|
124
|
+
if (node.type === 'word') {
|
|
125
|
+
if (/^#[0-9a-fA-F]{3,8}$/.test(node.value)) return undefined;
|
|
126
|
+
if (/^[a-zA-Z][a-zA-Z0-9-]*$/.test(node.value)) return undefined;
|
|
127
|
+
return [node];
|
|
128
|
+
}
|
|
129
|
+
// A quoted string, a comment, a unicode-range, or a stray comma: never a
|
|
130
|
+
// valid color position.
|
|
131
|
+
return [node];
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/** {@link findBareInNodes} for a single function-call node. */
|
|
135
|
+
export function findBareInFunction(
|
|
136
|
+
node: Extract<ColorValueNode, { type: 'function' }>,
|
|
137
|
+
): ColorValueNode[] | undefined {
|
|
138
|
+
const name = node.value.toLowerCase();
|
|
139
|
+
// `postcss-value-parser` still produces a function node for a call missing
|
|
140
|
+
// its closing `)`, marked `unclosed`, with everything up to the end of the
|
|
141
|
+
// declaration swept in as its arguments. The browser discards a declaration
|
|
142
|
+
// containing such a value as invalid CSS -- silently, the exact failure
|
|
143
|
+
// mode this gate exists to catch -- so an unclosed call is NEVER a complete
|
|
144
|
+
// color, regardless of which function it names or how deep it sits (a
|
|
145
|
+
// `var()` reference, an allowlisted color function, or one nested inside
|
|
146
|
+
// `light-dark()`/`color-mix()` all reach this same check). Checking this
|
|
147
|
+
// before every acceptance path below, rather than only the allowlist one,
|
|
148
|
+
// is what keeps `oklch(50% 0.1 30` from passing the way the old
|
|
149
|
+
// whole-string regex correctly rejected it.
|
|
150
|
+
if (node.unclosed) return [node];
|
|
151
|
+
// `var()`'s FALLBACK sits in the same color position as the reference
|
|
152
|
+
// itself, so a bare list there is the same defect one level down:
|
|
153
|
+
// `var(--x, 0% 0 0)` is a silently dropped declaration whenever `--x` is
|
|
154
|
+
// unset. The custom-property name is not a color and is skipped. No
|
|
155
|
+
// fallback at all is a bare reference, which is ambiguous and accepted --
|
|
156
|
+
// see {@link mixColorCandidates}'s doc comment on the safe direction.
|
|
157
|
+
if (name === 'var') {
|
|
158
|
+
const fallback = fallbackNodes(node.nodes);
|
|
159
|
+
return fallback === undefined ? undefined : findBareInNodes(fallback);
|
|
160
|
+
}
|
|
161
|
+
if (COLOR_FUNCTIONS.has(name)) return undefined;
|
|
162
|
+
if (!COLOR_ARGUMENT_FUNCTIONS.has(name)) return [node];
|
|
163
|
+
return findBareInArguments(name, splitArguments(node.nodes));
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
export function findBareInArguments(
|
|
167
|
+
name: string,
|
|
168
|
+
args: ColorValueNode[][],
|
|
169
|
+
): ColorValueNode[] | undefined {
|
|
170
|
+
// `color-mix()`'s first argument is its interpolation method (`in oklch`),
|
|
171
|
+
// not a color; `light-dark()`'s arguments are all colors.
|
|
172
|
+
const colorArguments = name === 'color-mix' ? args.slice(1) : args;
|
|
173
|
+
return findBareInColorArguments(name === 'color-mix', colorArguments);
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
export function findBareInColorArguments(
|
|
177
|
+
isMix: boolean,
|
|
178
|
+
colorArguments: ColorValueNode[][],
|
|
179
|
+
): ColorValueNode[] | undefined {
|
|
180
|
+
for (const argument of colorArguments) {
|
|
181
|
+
// Only `color-mix()` arguments carry a weight -- `light-dark()` takes two
|
|
182
|
+
// colors and nothing else, so its arguments are checked whole. Splitting
|
|
183
|
+
// them would report the wrong fragment: a bare `100% 0 0` would be
|
|
184
|
+
// dissected into `0` rather than named as the component list it is.
|
|
185
|
+
const candidates = isMix ? mixColorCandidates(argument) : [argument];
|
|
186
|
+
for (const candidate of candidates) {
|
|
187
|
+
const bare = findBareInNodes(candidate);
|
|
188
|
+
if (bare !== undefined) return bare;
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
return undefined;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* The first fragment of `value` that sits in a `<color>` position but is not a
|
|
196
|
+
* complete CSS color -- or `undefined` when every color position is complete.
|
|
197
|
+
*
|
|
198
|
+
* This exists for one failure mode, from CIN-242's decision record: a token
|
|
199
|
+
* authored as a BARE OKLCH COMPONENT TRIPLET (`light-dark(0% 0 0, 100% 0 0)`,
|
|
200
|
+
* so a call site can staple its own alpha on with `oklch(var(--token) / 0.4)`)
|
|
201
|
+
* is not a color. Assigned directly to `color`/`background-color`/
|
|
202
|
+
* `border-color` it produces an invalid declaration, which CSS drops SILENTLY
|
|
203
|
+
* -- the element keeps its inherited or initial color and nothing anywhere
|
|
204
|
+
* reports a problem. A complete value that is mis-referenced instead paints a
|
|
205
|
+
* visibly wrong color, which is catchable. Hence: complete values only.
|
|
206
|
+
*
|
|
207
|
+
* Completeness is decided from a PARSED value tree (`postcss-value-parser`),
|
|
208
|
+
* not a function-name allowlist matched against text -- see
|
|
209
|
+
* {@link findBareInNodes} for the actual rule. A component list starts with a
|
|
210
|
+
* number, so it is exactly what falls through.
|
|
211
|
+
*/
|
|
212
|
+
export function findBareColorComponents(value: string): string | undefined {
|
|
213
|
+
const trimmed = value.trim();
|
|
214
|
+
if (trimmed === '') return undefined;
|
|
215
|
+
const bare = findBareInNodes(valueParser(trimmed).nodes);
|
|
216
|
+
return bare === undefined ? undefined : valueParser.stringify(bare).trim();
|
|
217
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import type { CorpusEntry } from './corpus.ts';
|
|
2
|
+
import type { ValueResolver } from './resolve.ts';
|
|
3
|
+
import { findBareColorComponents, isWholeTokenAlias, resolveAlias } from './value-color.ts';
|
|
4
|
+
import { isAliasReference, serializeTypedValue } from './value-formatters.ts';
|
|
5
|
+
|
|
6
|
+
export function serializeEntryValue(
|
|
7
|
+
entry: CorpusEntry,
|
|
8
|
+
baseIndex: Map<string, CorpusEntry>,
|
|
9
|
+
resolveReferences: ValueResolver = (raw) => raw,
|
|
10
|
+
): string {
|
|
11
|
+
const serialized = serializeEntryValueUnchecked(entry, baseIndex, resolveReferences);
|
|
12
|
+
if (entry.type === 'color') {
|
|
13
|
+
const bare = findBareColorComponents(serialized);
|
|
14
|
+
if (bare !== undefined) {
|
|
15
|
+
throw new Error(
|
|
16
|
+
`Color token at "${entry.path}" serializes to "${serialized}", whose color position ` +
|
|
17
|
+
`"${bare}" is a bare component list rather than a complete CSS color. A bare triplet ` +
|
|
18
|
+
'is an invalid declaration that the browser drops silently; author the token as a ' +
|
|
19
|
+
'complete `light-dark()`/`color-mix()`/`oklch()` value instead (CIN-242).',
|
|
20
|
+
);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
return serialized;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function serializeEntryValueUnchecked(
|
|
27
|
+
entry: CorpusEntry,
|
|
28
|
+
baseIndex: Map<string, CorpusEntry>,
|
|
29
|
+
resolveReferences: ValueResolver,
|
|
30
|
+
): string {
|
|
31
|
+
if (typeof entry.cssRecipe === 'string') return entry.cssRecipe;
|
|
32
|
+
if (isAliasReference(entry.value)) {
|
|
33
|
+
// An ordinary bare-alias `$value` has always been required to name a
|
|
34
|
+
// WHOLE token here -- a deliberate restriction (see `wholeTokenIndexPath`'s
|
|
35
|
+
// doc comment) -- so a miss still throws via `resolveAlias` below. `$ref`
|
|
36
|
+
// is a generic JSON Pointer with no such requirement: a property-level
|
|
37
|
+
// `$ref` (e.g. `#/dimension/hairline/$value/value`, aliasing one scalar
|
|
38
|
+
// member of another token rather than the whole token) resolves fine at
|
|
39
|
+
// `tokens:validate` time but is not a whole-token identity, so it falls
|
|
40
|
+
// through to typed serialization instead, which resolves it (and any
|
|
41
|
+
// further nested references) via `resolveReferences` and formats the
|
|
42
|
+
// result per `entry.type` -- the same path an embedded property-level
|
|
43
|
+
// reference inside a composite `$value` already takes.
|
|
44
|
+
if (!entry.isRefAlias || isWholeTokenAlias(entry.value, baseIndex)) {
|
|
45
|
+
return resolveAlias(entry.value, baseIndex);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
if (entry.type === undefined) {
|
|
49
|
+
throw new Error(`Token at "${entry.path}" has no $type and no cssRecipe; cannot serialize.`);
|
|
50
|
+
}
|
|
51
|
+
return serializeTypedValue(entry.type, entry.value, entry.path, resolveReferences);
|
|
52
|
+
}
|