@astl/tokens 0.0.0 → 0.1.1
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 +14 -0
- package/LICENSE +202 -0
- package/README.md +39 -0
- package/dist/built-in-templates.d.ts +4 -0
- package/dist/built-in-templates.d.ts.map +1 -0
- package/dist/built-in-templates.js +189 -0
- package/dist/built-in-templates.js.map +1 -0
- package/dist/css.d.ts +7 -0
- package/dist/css.d.ts.map +1 -0
- package/dist/css.js +46 -0
- package/dist/css.js.map +1 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +5 -0
- package/dist/index.js.map +1 -0
- package/dist/messages/en.json +6 -0
- package/dist/messages/pl.json +6 -0
- package/dist/templates.d.ts +92 -0
- package/dist/templates.d.ts.map +1 -0
- package/dist/templates.js +324 -0
- package/dist/templates.js.map +1 -0
- package/dist/tokens.css +238 -0
- package/dist/tokens.d.ts +56 -0
- package/dist/tokens.d.ts.map +1 -0
- package/dist/tokens.js +212 -0
- package/dist/tokens.js.map +1 -0
- package/package.json +51 -4
- package/src/built-in-templates.ts +204 -0
- package/src/css.ts +48 -0
- package/src/index.ts +32 -0
- package/src/messages/en.json +6 -0
- package/src/messages/pl.json +6 -0
- package/src/templates.ts +395 -0
- package/src/tokens.ts +319 -0
package/src/templates.ts
ADDED
|
@@ -0,0 +1,395 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Style templates on the token side: the shape of `@astl/core`'s StyleTemplate restated structurally (this
|
|
3
|
+
* package depends on nothing; a type-checked test keeps both in step), WCAG contrast, the colours a theme gets with
|
|
4
|
+
* templates applied, and `checkStyleTemplate`, the check built-in templates pass and hosts can run in CI.
|
|
5
|
+
*/
|
|
6
|
+
import { type ColorScheme, type ColorToken, TEXT_PAIRS, type TokenName, tokens, UI_PAIRS } from './tokens.js';
|
|
7
|
+
|
|
8
|
+
/** = core `STYLE_TEMPLATE_NAME`: lowercase kebab-case, 1 to 40 characters, starting with a letter. */
|
|
9
|
+
export const TEMPLATE_NAME: RegExp = /^(?=.{1,40}$)[a-z][a-z0-9]*(?:-[a-z0-9]+)*$/;
|
|
10
|
+
|
|
11
|
+
/** = core `StyleScope`. */
|
|
12
|
+
export type TemplateScope = 'document' | 'component';
|
|
13
|
+
|
|
14
|
+
/** = core `MessageCatalog`. */
|
|
15
|
+
export interface TemplateCatalog {
|
|
16
|
+
readonly namespace: string;
|
|
17
|
+
readonly locale: string;
|
|
18
|
+
readonly messages: Readonly<Record<string, string>>;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** The shape of core's `StyleTemplate` (see `@astl/core` style.ts for the meaning of each field). */
|
|
22
|
+
export interface StyleTemplateShape {
|
|
23
|
+
readonly name: string;
|
|
24
|
+
readonly version: number;
|
|
25
|
+
readonly scopes: readonly TemplateScope[];
|
|
26
|
+
readonly components?: readonly string[];
|
|
27
|
+
readonly messages: string;
|
|
28
|
+
readonly catalogs?: readonly TemplateCatalog[];
|
|
29
|
+
readonly tokens?: Readonly<Record<string, string>>;
|
|
30
|
+
readonly light?: Readonly<Record<string, string>>;
|
|
31
|
+
readonly dark?: Readonly<Record<string, string>>;
|
|
32
|
+
readonly parts?: Readonly<Record<string, Readonly<Record<string, Readonly<Record<string, string>>>>>>;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** Themes a template changes: never `hc`. */
|
|
36
|
+
export type TemplateTheme = 'light' | 'dark';
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* CSS properties a template may set on `::part()`: shape, spacing and type. Outer `::part()` rules beat a component's
|
|
40
|
+
* own styles, so borders, outlines, colours, display and text size stay out: they carry verdicts, focus rings,
|
|
41
|
+
* selection and contrast. The player drops every other property.
|
|
42
|
+
*/
|
|
43
|
+
export const PART_PROPERTIES: ReadonlySet<string> = new Set([
|
|
44
|
+
'border-radius',
|
|
45
|
+
'border-start-start-radius',
|
|
46
|
+
'border-start-end-radius',
|
|
47
|
+
'border-end-start-radius',
|
|
48
|
+
'border-end-end-radius',
|
|
49
|
+
'box-shadow',
|
|
50
|
+
'padding',
|
|
51
|
+
'padding-block',
|
|
52
|
+
'padding-block-start',
|
|
53
|
+
'padding-block-end',
|
|
54
|
+
'padding-inline',
|
|
55
|
+
'padding-inline-start',
|
|
56
|
+
'padding-inline-end',
|
|
57
|
+
'gap',
|
|
58
|
+
'row-gap',
|
|
59
|
+
'column-gap',
|
|
60
|
+
'font-family',
|
|
61
|
+
'font-style',
|
|
62
|
+
'letter-spacing',
|
|
63
|
+
'line-height',
|
|
64
|
+
]);
|
|
65
|
+
|
|
66
|
+
/** Token names (without `--astl-`): what templates may override. */
|
|
67
|
+
// Pure, so a bundle that only takes isSafeStyleValue and PART_PROPERTIES from here (the player) drops them.
|
|
68
|
+
const TOKEN_NAMES: ReadonlySet<string> = /* @__PURE__ */ (() =>
|
|
69
|
+
new Set<TokenName>([
|
|
70
|
+
...(Object.keys(tokens.scale) as TokenName[]),
|
|
71
|
+
...(Object.keys(tokens.themes.light) as TokenName[]),
|
|
72
|
+
...(Object.keys(tokens.themeScale.light) as TokenName[]),
|
|
73
|
+
]))();
|
|
74
|
+
const COLOR_NAMES: ReadonlySet<string> = /* @__PURE__ */ (() => new Set(Object.keys(tokens.themes.light)))();
|
|
75
|
+
|
|
76
|
+
/** Tokens with an accessible floor, in px (1rem = 16px): 44px targets, 2px focus rings, 14px readable text. */
|
|
77
|
+
const MINIMUM_PX: Readonly<Record<string, number>> = {
|
|
78
|
+
'target-min': 44,
|
|
79
|
+
'focus-width': 2,
|
|
80
|
+
'outline-width': 1,
|
|
81
|
+
'size-content': 14,
|
|
82
|
+
'size-ui': 14,
|
|
83
|
+
'size-small': 14,
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
/** `12px` or `0.75rem` in px, or null for any other form. */
|
|
87
|
+
function pixels(value: string): number | null {
|
|
88
|
+
const match = /^(\d+(?:\.\d+)?|\.\d+)(px|rem)$/.exec(value.trim());
|
|
89
|
+
return match === null ? null : Number(match[1]) * (match[2] === 'rem' ? 16 : 1);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** Backgrounds a component shows through where it paints none (its text sits on the enclosing scope's colours). */
|
|
93
|
+
const PAGE_BACKGROUNDS: ReadonlySet<ColorToken> = new Set(['canvas', 'surface', 'surface-raised']);
|
|
94
|
+
|
|
95
|
+
const TAG = /^[a-z][a-z0-9]*-[a-z0-9-]*$/;
|
|
96
|
+
const PART_NAME = /^[a-z][a-z0-9-]*$/;
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Whether a value can go into a generated declaration without ending it, its rule or a `<style>`: no `{ } ; < >`,
|
|
100
|
+
* no comment opener, no backslash (an escape could swallow the `;` after it), no line breaks, closed quotes and
|
|
101
|
+
* balanced `( )` and `[ ]` outside them (an open `calc(` would swallow everything after it), not blank.
|
|
102
|
+
*/
|
|
103
|
+
export function isSafeStyleValue(value: string): boolean {
|
|
104
|
+
if (typeof value !== 'string' || value.trim() === '' || /[{};<>\\\n\r\f]|\/\*/.test(value)) return false;
|
|
105
|
+
const open: string[] = [];
|
|
106
|
+
let quote: string | null = null;
|
|
107
|
+
for (const ch of value) {
|
|
108
|
+
if (quote !== null) {
|
|
109
|
+
if (ch === quote) quote = null;
|
|
110
|
+
} else if (ch === '"' || ch === "'") {
|
|
111
|
+
quote = ch;
|
|
112
|
+
} else if (ch === '(' || ch === '[') {
|
|
113
|
+
open.push(ch === '(' ? ')' : ']');
|
|
114
|
+
} else if ((ch === ')' || ch === ']') && open.pop() !== ch) {
|
|
115
|
+
return false;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
return quote === null && open.length === 0;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/** `#rgb` or `#rrggbb` as 0-255 channels, or null. */
|
|
122
|
+
function channels(color: string): readonly [number, number, number] | null {
|
|
123
|
+
const hex = /^#([0-9a-f]{3}|[0-9a-f]{6})$/i.exec(color.trim())?.[1];
|
|
124
|
+
if (hex === undefined) return null;
|
|
125
|
+
const full = hex.length === 3 ? [...hex].map((c) => c + c).join('') : hex;
|
|
126
|
+
return [0, 2, 4].map((i) => Number.parseInt(full.slice(i, i + 2), 16)) as [number, number, number];
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/** WCAG 2 relative luminance. */
|
|
130
|
+
function luminance([r, g, b]: readonly [number, number, number]): number {
|
|
131
|
+
const linear = (c: number) => {
|
|
132
|
+
const s = c / 255;
|
|
133
|
+
return s <= 0.04045 ? s / 12.92 : ((s + 0.055) / 1.055) ** 2.4;
|
|
134
|
+
};
|
|
135
|
+
return 0.2126 * linear(r) + 0.7152 * linear(g) + 0.0722 * linear(b);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/** WCAG 2 contrast ratio (1 to 21) of two `#rgb` or `#rrggbb` colours. Throws a TypeError for other formats. */
|
|
139
|
+
export function contrastRatio(a: string, b: string): number {
|
|
140
|
+
const x = channels(a);
|
|
141
|
+
const y = channels(b);
|
|
142
|
+
if (x === null || y === null) throw new TypeError(`Not a #rgb or #rrggbb colour: ${x === null ? a : b}`);
|
|
143
|
+
const [hi, lo] = [luminance(x), luminance(y)].sort((p, q) => q - p) as [number, number];
|
|
144
|
+
return (hi + 0.05) / (lo + 0.05);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* The colours of the light or dark theme with templates applied in order, e.g. `[documentTemplate,
|
|
149
|
+
* componentTemplate]`: each template's `tokens`, then its `light` or `dark` values. Non-colour tokens are ignored.
|
|
150
|
+
*/
|
|
151
|
+
export function templateColors(theme: TemplateTheme, templates: readonly StyleTemplateShape[]): ColorScheme {
|
|
152
|
+
const colors: Record<string, string> = { ...tokens.themes[theme] };
|
|
153
|
+
for (const template of templates) {
|
|
154
|
+
for (const [name, value] of Object.entries({ ...template.tokens, ...template[theme] })) {
|
|
155
|
+
if (COLOR_NAMES.has(name)) colors[name] = value;
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
return colors as ColorScheme;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
export interface StyleTemplateProblem {
|
|
162
|
+
readonly kind:
|
|
163
|
+
| 'name'
|
|
164
|
+
| 'version'
|
|
165
|
+
| 'scopes'
|
|
166
|
+
| 'components'
|
|
167
|
+
| 'messages'
|
|
168
|
+
| 'token'
|
|
169
|
+
| 'value'
|
|
170
|
+
| 'color'
|
|
171
|
+
| 'property'
|
|
172
|
+
| 'part'
|
|
173
|
+
| 'text-contrast'
|
|
174
|
+
| 'ui-contrast'
|
|
175
|
+
| 'pick-distance';
|
|
176
|
+
/** Where, e.g. `name`, `dark.ink`, `parts.astl-choice.option.box-shadow`, `light: ink-muted on surface`. */
|
|
177
|
+
readonly at: string;
|
|
178
|
+
/** For developers (like `checkCatalog` in core): English, never shown to students or authors. */
|
|
179
|
+
readonly detail: string;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
export interface CheckStyleTemplateOptions {
|
|
183
|
+
/** Component manifests (tag and `cssParts` are enough): `components` and `parts` must name their tags and parts. */
|
|
184
|
+
readonly manifests?: readonly { readonly tag: string; readonly cssParts: readonly string[] }[];
|
|
185
|
+
/**
|
|
186
|
+
* Templates already applied around this one, e.g. the document's when checking a component template. Default
|
|
187
|
+
* none: the template on top of the plain light and dark themes.
|
|
188
|
+
*/
|
|
189
|
+
readonly within?: readonly StyleTemplateShape[];
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* How far (CIEDE2000) a pick stays from the verdict colours, so a picked box never looks checked: its edge
|
|
194
|
+
* (`selected`) from `success` and `danger`, its fill (`selected-soft`) from their soft fills. A pick's edge sits where a
|
|
195
|
+
* verdict's edge will; near-white fills differ less, hence the lower bound for them.
|
|
196
|
+
*/
|
|
197
|
+
export const PICK_DISTANCE = 25;
|
|
198
|
+
export const PICK_FILL_DISTANCE = 7;
|
|
199
|
+
const PICKS = [
|
|
200
|
+
['selected', 'success', PICK_DISTANCE],
|
|
201
|
+
['selected', 'danger', PICK_DISTANCE],
|
|
202
|
+
['selected-soft', 'success-soft', PICK_FILL_DISTANCE],
|
|
203
|
+
['selected-soft', 'danger-soft', PICK_FILL_DISTANCE],
|
|
204
|
+
] as const;
|
|
205
|
+
|
|
206
|
+
/** `#rgb` or `#rrggbb` to CIELAB (D65), through linear-light sRGB. */
|
|
207
|
+
function lab(color: readonly [number, number, number]): readonly [number, number, number] {
|
|
208
|
+
const [r, g, b] = color.map((c) => {
|
|
209
|
+
const s = c / 255;
|
|
210
|
+
return s <= 0.04045 ? s / 12.92 : ((s + 0.055) / 1.055) ** 2.4;
|
|
211
|
+
}) as [number, number, number];
|
|
212
|
+
const xyz = [
|
|
213
|
+
(0.4124564 * r + 0.3575761 * g + 0.1804375 * b) / 0.95047,
|
|
214
|
+
0.2126729 * r + 0.7151522 * g + 0.072175 * b,
|
|
215
|
+
(0.0193339 * r + 0.119192 * g + 0.9503041 * b) / 1.08883,
|
|
216
|
+
];
|
|
217
|
+
const [fx, fy, fz] = xyz.map((t) => (t > 216 / 24389 ? Math.cbrt(t) : ((24389 / 27) * t + 16) / 116)) as [
|
|
218
|
+
number,
|
|
219
|
+
number,
|
|
220
|
+
number,
|
|
221
|
+
];
|
|
222
|
+
return [116 * fy - 16, 500 * (fx - fy), 200 * (fy - fz)];
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
/**
|
|
226
|
+
* CIEDE2000 colour difference of two `#rgb` or `#rrggbb` colours (kL = kC = kH = 1), after G. Sharma, W. Wu and E. N.
|
|
227
|
+
* Dalal (2005), as the palette tests check it. Throws a TypeError for other formats.
|
|
228
|
+
*/
|
|
229
|
+
export function colorDistance(a: string, b: string): number {
|
|
230
|
+
const x = channels(a);
|
|
231
|
+
const y = channels(b);
|
|
232
|
+
if (x === null || y === null) throw new TypeError(`Not a #rgb or #rrggbb colour: ${x === null ? a : b}`);
|
|
233
|
+
const [L1, a1, b1] = lab(x);
|
|
234
|
+
const [L2, a2, b2] = lab(y);
|
|
235
|
+
const rad = Math.PI / 180;
|
|
236
|
+
const meanC = (Math.hypot(a1, b1) + Math.hypot(a2, b2)) / 2;
|
|
237
|
+
const G = 0.5 * (1 - Math.sqrt(meanC ** 7 / (meanC ** 7 + 25 ** 7)));
|
|
238
|
+
const [ap1, ap2] = [(1 + G) * a1, (1 + G) * a2];
|
|
239
|
+
const [C1, C2] = [Math.hypot(ap1, b1), Math.hypot(ap2, b2)];
|
|
240
|
+
const hue = (bb: number, ap: number) => (bb === 0 && ap === 0 ? 0 : (Math.atan2(bb, ap) / rad + 360) % 360);
|
|
241
|
+
const [h1, h2] = [hue(b1, ap1), hue(b2, ap2)];
|
|
242
|
+
let dh = 0;
|
|
243
|
+
if (C1 * C2 !== 0) dh = h2 - h1 > 180 ? h2 - h1 - 360 : h2 - h1 < -180 ? h2 - h1 + 360 : h2 - h1;
|
|
244
|
+
const dH = 2 * Math.sqrt(C1 * C2) * Math.sin((dh / 2) * rad);
|
|
245
|
+
const L = (L1 + L2) / 2;
|
|
246
|
+
const C = (C1 + C2) / 2;
|
|
247
|
+
let h = h1 + h2;
|
|
248
|
+
if (C1 * C2 !== 0) h = Math.abs(h1 - h2) <= 180 ? h / 2 : h < 360 ? (h + 360) / 2 : (h - 360) / 2;
|
|
249
|
+
const T =
|
|
250
|
+
1 -
|
|
251
|
+
0.17 * Math.cos((h - 30) * rad) +
|
|
252
|
+
0.24 * Math.cos(2 * h * rad) +
|
|
253
|
+
0.32 * Math.cos((3 * h + 6) * rad) -
|
|
254
|
+
0.2 * Math.cos((4 * h - 63) * rad);
|
|
255
|
+
const RT =
|
|
256
|
+
-2 * Math.sqrt(C ** 7 / (C ** 7 + 25 ** 7)) * Math.sin(60 * Math.exp(-(((h - 275) / 25) ** 2)) * rad);
|
|
257
|
+
const SL = 1 + (0.015 * (L - 50) ** 2) / Math.sqrt(20 + (L - 50) ** 2);
|
|
258
|
+
const SC = 1 + 0.045 * C;
|
|
259
|
+
const SH = 1 + 0.015 * C * T;
|
|
260
|
+
return Math.sqrt(
|
|
261
|
+
((L2 - L1) / SL) ** 2 + ((C2 - C1) / SC) ** 2 + (dH / SH) ** 2 + RT * ((C2 - C1) / SC) * (dH / SH),
|
|
262
|
+
);
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
/** The pairs every template keeps, with their minimum ratio (WCAG 2.2 AA: 1.4.3 text, 1.4.11 non-text). */
|
|
266
|
+
const CONTRAST = [
|
|
267
|
+
['text-contrast', TEXT_PAIRS, 4.5],
|
|
268
|
+
['ui-contrast', UI_PAIRS, 3],
|
|
269
|
+
] as const;
|
|
270
|
+
|
|
271
|
+
/**
|
|
272
|
+
* Problems of a template: name, version, scopes, component tags, messages, token names, values that are unsafe,
|
|
273
|
+
* colours that are not `#rgb` / `#rrggbb`, targets, focus rings, control borders and text sizes under their
|
|
274
|
+
* accessible minimum (MINIMUM_PX), part properties and names, picks next to the verdict colours (PICK_DISTANCE),
|
|
275
|
+
* and contrast. Contrast: every TEXT_PAIRS pair at
|
|
276
|
+
* least 4.5:1 and every UI_PAIRS pair at least 3:1 in light and dark after applying the template (on top of
|
|
277
|
+
* `within`); for templates with the `component` scope also against the enclosing scope's backgrounds, which show
|
|
278
|
+
* through where a component paints none. An empty list means the template is fine.
|
|
279
|
+
*/
|
|
280
|
+
export function checkStyleTemplate(
|
|
281
|
+
template: StyleTemplateShape,
|
|
282
|
+
options: CheckStyleTemplateOptions = {},
|
|
283
|
+
): StyleTemplateProblem[] {
|
|
284
|
+
const problems: StyleTemplateProblem[] = [];
|
|
285
|
+
const add = (kind: StyleTemplateProblem['kind'], at: string, detail: string) =>
|
|
286
|
+
problems.push({ kind, at, detail });
|
|
287
|
+
const known = options.manifests === undefined ? null : new Map(options.manifests.map((m) => [m.tag, m]));
|
|
288
|
+
|
|
289
|
+
if (!TEMPLATE_NAME.test(template.name)) {
|
|
290
|
+
add('name', 'name', `"${template.name}" is not lowercase kebab-case of 1 to 40 characters`);
|
|
291
|
+
}
|
|
292
|
+
if (!Number.isInteger(template.version) || template.version < 1) {
|
|
293
|
+
add('version', 'version', 'version must be a positive integer');
|
|
294
|
+
}
|
|
295
|
+
const scopes = template.scopes ?? [];
|
|
296
|
+
if (
|
|
297
|
+
scopes.length === 0 ||
|
|
298
|
+
new Set(scopes).size !== scopes.length ||
|
|
299
|
+
scopes.some((s) => s !== 'document' && s !== 'component')
|
|
300
|
+
) {
|
|
301
|
+
add('scopes', 'scopes', 'scopes must list "document" and/or "component", each once');
|
|
302
|
+
}
|
|
303
|
+
for (const tag of template.components ?? []) {
|
|
304
|
+
if (!TAG.test(tag)) add('components', `components.${tag}`, `"${tag}" is not a custom element name`);
|
|
305
|
+
else if (known !== null && !known.has(tag))
|
|
306
|
+
add('components', `components.${tag}`, `no manifest for ${tag}`);
|
|
307
|
+
}
|
|
308
|
+
if (template.components !== undefined && template.components.length === 0) {
|
|
309
|
+
add('components', 'components', 'leave components out instead of listing none');
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
if (typeof template.messages !== 'string' || template.messages === '') {
|
|
313
|
+
add('messages', 'messages', 'messages must name the i18n namespace of the label and description');
|
|
314
|
+
}
|
|
315
|
+
for (const catalog of template.catalogs ?? []) {
|
|
316
|
+
const at = `catalogs.${catalog.locale}`;
|
|
317
|
+
if (catalog.namespace !== template.messages) {
|
|
318
|
+
add('messages', at, `namespace ${catalog.namespace} is not ${template.messages}`);
|
|
319
|
+
}
|
|
320
|
+
for (const key of ['label', 'description']) {
|
|
321
|
+
const text = Object.hasOwn(catalog.messages, key) ? catalog.messages[key] : undefined;
|
|
322
|
+
if (typeof text !== 'string' || text.trim() === '')
|
|
323
|
+
add('messages', `${at}.${key}`, `${key} is missing`);
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
let colorsOk = true;
|
|
328
|
+
for (const group of ['tokens', 'light', 'dark'] as const) {
|
|
329
|
+
for (const [name, value] of Object.entries(template[group] ?? {})) {
|
|
330
|
+
const at = `${group}.${name}`;
|
|
331
|
+
if (!TOKEN_NAMES.has(name)) add('token', at, `--astl-${name} is not a token`);
|
|
332
|
+
if (!isSafeStyleValue(value)) {
|
|
333
|
+
add('value', at, `unsafe or empty value: ${JSON.stringify(value)}`);
|
|
334
|
+
if (COLOR_NAMES.has(name)) colorsOk = false;
|
|
335
|
+
} else if (COLOR_NAMES.has(name) && channels(value) === null) {
|
|
336
|
+
add('color', at, `${value} is not #rgb or #rrggbb, so its contrast cannot be checked`);
|
|
337
|
+
colorsOk = false;
|
|
338
|
+
} else if (Object.hasOwn(MINIMUM_PX, name)) {
|
|
339
|
+
const min = MINIMUM_PX[name] ?? 0;
|
|
340
|
+
const px = pixels(value);
|
|
341
|
+
if (px === null) add('value', at, `write px or rem, so the minimum of ${min}px can be checked`);
|
|
342
|
+
else if (px < min) add('value', at, `${value} is below the accessible minimum of ${min}px`);
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
for (const [tag, byPart] of Object.entries(template.parts ?? {})) {
|
|
348
|
+
const manifest = known?.get(tag);
|
|
349
|
+
if (!TAG.test(tag)) add('part', `parts.${tag}`, `"${tag}" is not a custom element name`);
|
|
350
|
+
else if (known !== null && manifest === undefined) add('part', `parts.${tag}`, `no manifest for ${tag}`);
|
|
351
|
+
for (const [part, declarations] of Object.entries(byPart)) {
|
|
352
|
+
const at = `parts.${tag}.${part}`;
|
|
353
|
+
if (!PART_NAME.test(part)) add('part', at, `"${part}" is not a part name`);
|
|
354
|
+
else if (manifest !== undefined && !manifest.cssParts.includes(part)) {
|
|
355
|
+
add('part', at, `${tag} has no part "${part}" (cssParts: ${manifest.cssParts.join(', ')})`);
|
|
356
|
+
}
|
|
357
|
+
for (const [property, value] of Object.entries(declarations)) {
|
|
358
|
+
if (!PART_PROPERTIES.has(property)) {
|
|
359
|
+
add('property', `${at}.${property}`, `${property} is not allowed on parts (see PART_PROPERTIES)`);
|
|
360
|
+
}
|
|
361
|
+
if (!isSafeStyleValue(value)) {
|
|
362
|
+
add('value', `${at}.${property}`, `unsafe or empty value: ${JSON.stringify(value)}`);
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
const within = options.within ?? [];
|
|
369
|
+
const component = scopes.includes('component');
|
|
370
|
+
for (const theme of colorsOk ? (['light', 'dark'] as const) : []) {
|
|
371
|
+
const enclosing = templateColors(theme, within);
|
|
372
|
+
const colors = templateColors(theme, [...within, template]);
|
|
373
|
+
if (Object.values(enclosing).some((c) => channels(c) === null)) {
|
|
374
|
+
add('color', `within (${theme})`, 'a template in within has a colour that is not #rgb or #rrggbb');
|
|
375
|
+
continue;
|
|
376
|
+
}
|
|
377
|
+
for (const [pick, verdict, min] of PICKS) {
|
|
378
|
+
const d = colorDistance(colors[pick], colors[verdict]);
|
|
379
|
+
if (d < min)
|
|
380
|
+
add('pick-distance', `${theme}: ${pick} and ${verdict}`, `${d.toFixed(1)} apart, needs ${min}`);
|
|
381
|
+
}
|
|
382
|
+
for (const [kind, list, min] of CONTRAST) {
|
|
383
|
+
for (const [fg, bg] of list) {
|
|
384
|
+
const own = contrastRatio(colors[fg], colors[bg]);
|
|
385
|
+
if (own < min) add(kind, `${theme}: ${fg} on ${bg}`, `${own.toFixed(2)}:1, needs ${min}:1`);
|
|
386
|
+
if (!component || !PAGE_BACKGROUNDS.has(bg)) continue;
|
|
387
|
+
const around = contrastRatio(colors[fg], enclosing[bg]);
|
|
388
|
+
if (around < min) {
|
|
389
|
+
add(kind, `${theme}: ${fg} on the enclosing ${bg}`, `${around.toFixed(2)}:1, needs ${min}:1`);
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
return problems;
|
|
395
|
+
}
|