nuif-css-tokens-0 CSS custom-property token profile
Status: experimental executable profile in crates/nuif-css-tokens, checked by
cargo xtask gate-css-tokens. Directions: import and synchronize. It produces
RFC 0015 token collections in a version-2 document (RFC 0014).
Declaration
A profile declares one or more collections, each with ordered modes (the first
is the default), and a list of scopes. A scope is an exact top-level prelude
(a selector such as .dark or an at-rule prelude such as @theme), the
collection it belongs to (the first collection when omitted) and either a mode
or no mode. A scope without a mode is a base scope: its values apply to every
mode of its collection that has no mode-specific declaration later in cascade
order. Preludes are compared after comment removal and whitespace
normalization; no selector matching takes place.
{
"collections": [{"name": "Colour", "modes": ["Dark", "Light"]}],
"scopes": [
{"selector": ":root", "mode": null},
{"selector": ":root:not(.dark)", "mode": "Light"},
{"selector": ".dark", "mode": "Dark"}
],
"root_font_size_px": 16
}
Several sources may be imported together. Later sources follow earlier ones in cascade order.
string_tokens lists token names (exact, or a prefix ending in *) whose
non-alias values import as string tokens holding the exact declaration text,
for values without a typed form such as durations, easing functions, font
stacks and shadows. Such text must stay one balanced declaration value; edits
that would add ;, {, }, comments, newlines or unbalanced quotes or
parentheses are refused.
Cascade and identity
For each token and mode, the governing declaration is the last mode-specific declaration, else the last base declaration, ordered by source and then byte offset. A token belongs to exactly one collection. Identities are SHA-256-derived from the profile name, the collection name and the token or mode name, so a re-import of the same names keeps the same ids. A rename is a new token. A mode value equal to the default-mode value is omitted.
Values
| CSS | NUIF |
|---|---|
#rgb, #rgba, #rrggbb, #rrggbbaa | color, components byte / 255 |
rgb() / rgba() with integer channels and a decimal or percent alpha | color |
<number>px | dimension |
<number>rem with a declared root size | dimension in pixels |
| plain decimal number | number |
any value of a token matching string_tokens | string with the exact text |
var(--other) or var(--other, fallback), --other declared in the sources | alias to other |
var(--other, fallback), --other declared nowhere in the sources | the fallback literal, approximated |
A cross-collection alias must target a collection with one mode, so that CSS context resolution and NUIF mode resolution agree.
Unsupported and retained
Every byte outside a mapped value span is retained. Each of the following
produces an unsupported fidelity entry and the token is not imported:
values outside the table (for example oklch(), calc(), durations, easing
functions, font stacks and shadows), declarations in undeclared scopes,
at-rules or nested blocks, a token without a value in some mode of its
collection, same-source duplicates within one scope kind, a mode-specific
declaration that precedes the base declaration in cascade order, a token
declared in scopes of two collections, mixed value types, aliases to
unsupported tokens, cross-collection aliases to multi-mode collections and
alias cycles.
Synchronization
synchronize_sources first re-imports the retained sources and refuses them if
they no longer produce the retained document. Then:
- a changed value replaces exactly its governing declaration’s value span
(only the fallback span for a fallback-imported value), in the slot’s unit
(
px,rem) and keeping an alias’s fallback text; - a mode whose value diverges from a shared base declaration receives a new declaration in the last block of that mode’s scope after the base declaration;
- a new token is inserted into the last base-scope block of its collection
(or each mode’s block when the collection has no base scope); its id is
remapped to the id the CSS assigns and returned in
remapped; - removals, renames, type changes, collection or mode changes and non-token content are refused, as are values without an exact representation, insertions without a receiving block, and blocks whose last declaration lacks a semicolon.
The output must re-import to the edited document (mode values collapsed),
otherwise nothing is returned. Canonical text is lower-case hex when every
channel is a byte value, otherwise rgb(r g b / a); Npx or Nrem; the
shortest decimal; var(--name).
Command line
nuif css-tokens import --profile profile.json --output tokens.nuif.json \
[--report report.json] tokens.css overrides.css
nuif css-tokens sync --profile profile.json --edited edited.nuif.json \
--output-dir out/ [--report report.json] tokens.css overrides.css
sync writes each synchronized source under its file name in --output-dir
and prints the edit counts and the id remapping.
Limits
1 MiB per source, 16 sources, 4,096 tokens, 64 collections, scopes and modes, and block nesting depth 64.
Evidence
crates/nuif-css-tokens/src/tests.rs: import with independent expected
values; equal-value collapse; base and per-mode overrides in cascade order;
fallbacks and rem; multi-source override; byte fixpoint; single-span value,
alias and dimension edits; override and new-token insertion with remapping;
separate primitive and semantic collections with cross-collection aliases;
declared string tokens with exact text and refused structure-breaking edits;
stale, structural and unrepresentable refusals; ambiguous and cyclic
declarations; hostile and limit inputs. crates/nuif-cli/tests/token_commands.rs
checks the command-line wiring.