command-code 1.93.0 → 1.93.2
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 +10 -0
- package/dist/bundled/command-code-knowledge/reference/models.md +1 -0
- package/dist/bundled/command-code-knowledge/reference/product-help.md +1 -0
- package/dist/bundled/design/SKILL.md +7 -0
- package/dist/bundled/design/references/checkup.md +2 -0
- package/dist/bundled/design/references/deslop.md +2 -0
- package/dist/bundled/design/references/reflexes.md +132 -0
- package/dist/bundled/design/references/smell.md +2 -0
- package/dist/bundled/design/scripts/check/color.mjs +213 -0
- package/dist/bundled/design/scripts/check/contrast.mjs +141 -0
- package/dist/bundled/design/scripts/check/css.mjs +223 -0
- package/dist/bundled/design/scripts/check/named-colors.mjs +152 -0
- package/dist/bundled/design/scripts/check/pair.mjs +61 -0
- package/dist/bundled/design/scripts/check/preview.mjs +27 -0
- package/dist/bundled/design/scripts/check/reflexes.mjs +257 -0
- package/dist/bundled/design/scripts/check/sources.mjs +237 -0
- package/dist/bundled/design/scripts/check.mjs +165 -0
- package/dist/cli.mjs +5 -5
- package/package.json +1 -1
- package/vsix/commandcode-vscode.vsix +0 -0
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
// CSS reading for check.mjs: rules, declarations, tokens, and text size. A brace reader, not a CSS
|
|
2
|
+
// engine — it sees what each rule declares, not what cascades onto an element.
|
|
3
|
+
import {parseColor, resolveVars} from './color.mjs';
|
|
4
|
+
|
|
5
|
+
/** @typedef {{readonly selector: string, readonly body: string, readonly source: string}} Rule */
|
|
6
|
+
|
|
7
|
+
const BROWSER_ROOT_PX = 16;
|
|
8
|
+
const LARGE_TEXT_PX = 24;
|
|
9
|
+
const LARGE_BOLD_TEXT_PX = 18.66;
|
|
10
|
+
const BOLD_WEIGHT = 700;
|
|
11
|
+
// Where design tokens live: plain :root/html, and Tailwind v4's @theme block.
|
|
12
|
+
const TOKEN_SELECTORS = new Set([':root', 'html', '@theme']);
|
|
13
|
+
const PAGE_SELECTORS = new Set(['body', 'html', ':root']);
|
|
14
|
+
const IMPORTANT = /!important\s*$/i;
|
|
15
|
+
|
|
16
|
+
/** @param {string} css @returns {string} */
|
|
17
|
+
export function stripComments(css) {
|
|
18
|
+
return css.replace(/\/\*[\s\S]*?\*\//g, '');
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Depth-0 `prelude { body }` blocks. A `;` at depth 0 ends a statement at-rule (`@import …;`,
|
|
23
|
+
* `@layer a, b;`) — without that, the statement became the next rule's prelude and swallowed it.
|
|
24
|
+
* Quoted strings are skipped: a `}` in `content: "}"` once ended its rule early.
|
|
25
|
+
*/
|
|
26
|
+
function topLevelBlocks(css) {
|
|
27
|
+
const blocks = [];
|
|
28
|
+
let depth = 0;
|
|
29
|
+
let start = 0;
|
|
30
|
+
let bodyStart = 0;
|
|
31
|
+
let prelude = '';
|
|
32
|
+
let quote = '';
|
|
33
|
+
for (let index = 0; index < css.length; index++) {
|
|
34
|
+
const char = css[index];
|
|
35
|
+
if (quote) {
|
|
36
|
+
if (char === '\\') index++;
|
|
37
|
+
if (char === quote) quote = '';
|
|
38
|
+
continue;
|
|
39
|
+
}
|
|
40
|
+
if (char === '"' || char === "'") {
|
|
41
|
+
quote = char;
|
|
42
|
+
continue;
|
|
43
|
+
}
|
|
44
|
+
if (char === ';' && depth === 0) start = index + 1;
|
|
45
|
+
if (char === '{' && depth === 0) {
|
|
46
|
+
prelude = css.slice(start, index).trim();
|
|
47
|
+
bodyStart = index + 1;
|
|
48
|
+
}
|
|
49
|
+
if (char === '{') depth++;
|
|
50
|
+
if (char !== '}' || depth === 0) continue;
|
|
51
|
+
depth--;
|
|
52
|
+
if (depth !== 0) continue;
|
|
53
|
+
blocks.push({prelude, body: css.slice(bodyStart, index)});
|
|
54
|
+
start = index + 1;
|
|
55
|
+
}
|
|
56
|
+
return blocks;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* `@layer` is read through and `@theme` kept as a token block. `@media`, `@supports`,
|
|
61
|
+
* `@container`, and keyframes are dropped with their contents: a conditional rule is not what
|
|
62
|
+
* the page shows by default.
|
|
63
|
+
*/
|
|
64
|
+
function rulesFromBlock({prelude, body}) {
|
|
65
|
+
if (!prelude.startsWith('@')) return [{selector: prelude, body}];
|
|
66
|
+
if (/^@layer\b/i.test(prelude)) return parseRules(body);
|
|
67
|
+
if (/^@theme\b/i.test(prelude)) return [{selector: '@theme', body}];
|
|
68
|
+
return [];
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** Style rules in source order, without a source label. @param {string} css */
|
|
72
|
+
export function parseRules(css) {
|
|
73
|
+
return topLevelBlocks(css).flatMap(rulesFromBlock);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** @typedef {{readonly name: string, readonly value: string, readonly important: boolean}} Declaration */
|
|
77
|
+
|
|
78
|
+
/** @param {string} body @returns {readonly Declaration[]} */
|
|
79
|
+
function declarationList(body) {
|
|
80
|
+
return body.split(';').flatMap(part => {
|
|
81
|
+
const colon = part.indexOf(':');
|
|
82
|
+
if (colon === -1) return [];
|
|
83
|
+
const raw = part.slice(colon + 1);
|
|
84
|
+
return [
|
|
85
|
+
{
|
|
86
|
+
name: part.slice(0, colon).trim().toLowerCase(),
|
|
87
|
+
value: raw.replace(IMPORTANT, '').trim(),
|
|
88
|
+
important: IMPORTANT.test(raw),
|
|
89
|
+
},
|
|
90
|
+
];
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** @param {string} body @returns {Readonly<Record<string, string>>} */
|
|
95
|
+
export function declarations(body) {
|
|
96
|
+
return Object.fromEntries(
|
|
97
|
+
declarationList(body).map(({name, value}) => [name, value]),
|
|
98
|
+
);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/** @param {Rule} rule @returns {readonly string[]} */
|
|
102
|
+
function selectorsOf(rule) {
|
|
103
|
+
return rule.selector.split(',').map(selector => selector.trim());
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Root tokens as the cascade resolves them: a later value wins unless an earlier one was
|
|
108
|
+
* `!important` and it is not. @param {readonly Rule[]} rules
|
|
109
|
+
*/
|
|
110
|
+
export function rootVars(rules) {
|
|
111
|
+
const vars = {};
|
|
112
|
+
const important = new Set();
|
|
113
|
+
for (const rule of rules) {
|
|
114
|
+
if (!selectorsOf(rule).some(selector => TOKEN_SELECTORS.has(selector)))
|
|
115
|
+
continue;
|
|
116
|
+
for (const decl of declarationList(rule.body)) {
|
|
117
|
+
if (!decl.name.startsWith('--')) continue;
|
|
118
|
+
if (important.has(decl.name) && !decl.important) continue;
|
|
119
|
+
if (decl.important) important.add(decl.name);
|
|
120
|
+
vars[decl.name] = decl.value;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
return vars;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/** A root font-size in px; `%`, `em`, and `rem` there resolve against the browser's 16px. */
|
|
127
|
+
function rootSizePx(value) {
|
|
128
|
+
const match = value.match(/^([\d.]+)(px|%|r?em)$/);
|
|
129
|
+
if (!match) return null;
|
|
130
|
+
const size = Number.parseFloat(match[1] ?? '');
|
|
131
|
+
if (match[2] === 'px') return size;
|
|
132
|
+
if (match[2] === '%') return (BROWSER_ROOT_PX * size) / 100;
|
|
133
|
+
return BROWSER_ROOT_PX * size;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/** The root font size `rem` resolves against: the last html/:root font-size, else 16px. */
|
|
137
|
+
export function rootFontSize(rules) {
|
|
138
|
+
let px = BROWSER_ROOT_PX;
|
|
139
|
+
for (const rule of rules) {
|
|
140
|
+
if (
|
|
141
|
+
!selectorsOf(rule).some(selector => /^(html|:root)$/.test(selector))
|
|
142
|
+
)
|
|
143
|
+
continue;
|
|
144
|
+
px = rootSizePx(declarations(rule.body)['font-size'] ?? '') ?? px;
|
|
145
|
+
}
|
|
146
|
+
return px;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/** @param {{decl: Readonly<Record<string, string>>, vars: Readonly<Record<string, string>>}} params */
|
|
150
|
+
export function backgroundColor({decl, vars}) {
|
|
151
|
+
const value = decl['background-color'] ?? decl.background;
|
|
152
|
+
if (!value || /gradient|url\(/i.test(resolveVars({value, vars})))
|
|
153
|
+
return null;
|
|
154
|
+
return parseColor({value: value.split(/\s+(?![^(]*\))/)[0] ?? '', vars});
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/** @param {Readonly<Record<string, string>>} decl */
|
|
158
|
+
export function hasBackground(decl) {
|
|
159
|
+
const value =
|
|
160
|
+
decl['background-color'] ?? decl.background ?? decl['background-image'];
|
|
161
|
+
if (!value) return false;
|
|
162
|
+
return !/^(transparent|none|inherit|initial|unset)$/i.test(value.trim());
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* The last body/html/:root background, as the cascade resolves it, else white. `value` is what it
|
|
167
|
+
* was written as, so a page color read through an unknowable token can be reported as unverified.
|
|
168
|
+
* @returns {{readonly rgb: readonly number[], readonly value: string}}
|
|
169
|
+
*/
|
|
170
|
+
export function pageBackground({rules, vars}) {
|
|
171
|
+
let page = {rgb: [1, 1, 1], value: ''};
|
|
172
|
+
for (const rule of rules) {
|
|
173
|
+
if (!selectorsOf(rule).some(selector => PAGE_SELECTORS.has(selector)))
|
|
174
|
+
continue;
|
|
175
|
+
const decl = declarations(rule.body);
|
|
176
|
+
const value = decl['background-color'] ?? decl.background ?? '';
|
|
177
|
+
if (!value) continue;
|
|
178
|
+
const rgb = backgroundColor({decl, vars});
|
|
179
|
+
if (rgb) page = {rgb, value};
|
|
180
|
+
if (!rgb && /var\(/.test(value)) page = {...page, value};
|
|
181
|
+
}
|
|
182
|
+
return page;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* Tokens that sheets of unknown load order set to different values: which one wins is unknowable,
|
|
187
|
+
* so a pair measured through one is unverified. @param {readonly (readonly Rule[])[]} sheetRules
|
|
188
|
+
*/
|
|
189
|
+
export function conflictingVars(sheetRules) {
|
|
190
|
+
const values = new Map();
|
|
191
|
+
for (const rules of sheetRules) {
|
|
192
|
+
for (const [name, value] of Object.entries(rootVars(rules))) {
|
|
193
|
+
values.set(name, new Set([...(values.get(name) ?? []), value]));
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
return new Set(
|
|
197
|
+
[...values].filter(([, set]) => set.size > 1).map(([name]) => name),
|
|
198
|
+
);
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/** @param {string | undefined} value @returns {number} */
|
|
202
|
+
function fontWeight(value) {
|
|
203
|
+
if (value === 'bold' || value === 'bolder') return BOLD_WEIGHT;
|
|
204
|
+
return Number.parseInt(value ?? '', 10) || 400;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* WCAG text size: 'large' (24px, or 18.66px bold), 'normal', or 'unknown' when the size depends on
|
|
209
|
+
* the parent (`em`, `%`, keywords, `clamp()`). No font-size at all is body text: 'normal'.
|
|
210
|
+
* @param {{decl: Readonly<Record<string, string>>, rootPx: number}} params
|
|
211
|
+
* @returns {'large' | 'normal' | 'unknown'}
|
|
212
|
+
*/
|
|
213
|
+
export function textSize({decl, rootPx}) {
|
|
214
|
+
const value = decl['font-size'];
|
|
215
|
+
if (!value) return 'normal';
|
|
216
|
+
const match = value.match(/^([\d.]+)(px|rem)$/);
|
|
217
|
+
if (!match) return 'unknown';
|
|
218
|
+
const size = Number.parseFloat(match[1] ?? '');
|
|
219
|
+
const px = match[2] === 'px' ? size : size * rootPx;
|
|
220
|
+
if (px >= LARGE_TEXT_PX) return 'large';
|
|
221
|
+
const bold = fontWeight(decl['font-weight']) >= BOLD_WEIGHT;
|
|
222
|
+
return px >= LARGE_BOLD_TEXT_PX && bold ? 'large' : 'normal';
|
|
223
|
+
}
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
// The CSS named colors (CSS Color 4), as hex digits. `transparent` and `currentcolor` are left
|
|
2
|
+
// out on purpose: neither is a solid color this script can measure.
|
|
3
|
+
export const NAMED_COLORS = Object.freeze({
|
|
4
|
+
aliceblue: 'f0f8ff',
|
|
5
|
+
antiquewhite: 'faebd7',
|
|
6
|
+
aqua: '00ffff',
|
|
7
|
+
aquamarine: '7fffd4',
|
|
8
|
+
azure: 'f0ffff',
|
|
9
|
+
beige: 'f5f5dc',
|
|
10
|
+
bisque: 'ffe4c4',
|
|
11
|
+
black: '000000',
|
|
12
|
+
blanchedalmond: 'ffebcd',
|
|
13
|
+
blue: '0000ff',
|
|
14
|
+
blueviolet: '8a2be2',
|
|
15
|
+
brown: 'a52a2a',
|
|
16
|
+
burlywood: 'deb887',
|
|
17
|
+
cadetblue: '5f9ea0',
|
|
18
|
+
chartreuse: '7fff00',
|
|
19
|
+
chocolate: 'd2691e',
|
|
20
|
+
coral: 'ff7f50',
|
|
21
|
+
cornflowerblue: '6495ed',
|
|
22
|
+
cornsilk: 'fff8dc',
|
|
23
|
+
crimson: 'dc143c',
|
|
24
|
+
cyan: '00ffff',
|
|
25
|
+
darkblue: '00008b',
|
|
26
|
+
darkcyan: '008b8b',
|
|
27
|
+
darkgoldenrod: 'b8860b',
|
|
28
|
+
darkgray: 'a9a9a9',
|
|
29
|
+
darkgreen: '006400',
|
|
30
|
+
darkgrey: 'a9a9a9',
|
|
31
|
+
darkkhaki: 'bdb76b',
|
|
32
|
+
darkmagenta: '8b008b',
|
|
33
|
+
darkolivegreen: '556b2f',
|
|
34
|
+
darkorange: 'ff8c00',
|
|
35
|
+
darkorchid: '9932cc',
|
|
36
|
+
darkred: '8b0000',
|
|
37
|
+
darksalmon: 'e9967a',
|
|
38
|
+
darkseagreen: '8fbc8f',
|
|
39
|
+
darkslateblue: '483d8b',
|
|
40
|
+
darkslategray: '2f4f4f',
|
|
41
|
+
darkslategrey: '2f4f4f',
|
|
42
|
+
darkturquoise: '00ced1',
|
|
43
|
+
darkviolet: '9400d3',
|
|
44
|
+
deeppink: 'ff1493',
|
|
45
|
+
deepskyblue: '00bfff',
|
|
46
|
+
dimgray: '696969',
|
|
47
|
+
dimgrey: '696969',
|
|
48
|
+
dodgerblue: '1e90ff',
|
|
49
|
+
firebrick: 'b22222',
|
|
50
|
+
floralwhite: 'fffaf0',
|
|
51
|
+
forestgreen: '228b22',
|
|
52
|
+
fuchsia: 'ff00ff',
|
|
53
|
+
gainsboro: 'dcdcdc',
|
|
54
|
+
ghostwhite: 'f8f8ff',
|
|
55
|
+
gold: 'ffd700',
|
|
56
|
+
goldenrod: 'daa520',
|
|
57
|
+
gray: '808080',
|
|
58
|
+
green: '008000',
|
|
59
|
+
greenyellow: 'adff2f',
|
|
60
|
+
grey: '808080',
|
|
61
|
+
honeydew: 'f0fff0',
|
|
62
|
+
hotpink: 'ff69b4',
|
|
63
|
+
indianred: 'cd5c5c',
|
|
64
|
+
indigo: '4b0082',
|
|
65
|
+
ivory: 'fffff0',
|
|
66
|
+
khaki: 'f0e68c',
|
|
67
|
+
lavender: 'e6e6fa',
|
|
68
|
+
lavenderblush: 'fff0f5',
|
|
69
|
+
lawngreen: '7cfc00',
|
|
70
|
+
lemonchiffon: 'fffacd',
|
|
71
|
+
lightblue: 'add8e6',
|
|
72
|
+
lightcoral: 'f08080',
|
|
73
|
+
lightcyan: 'e0ffff',
|
|
74
|
+
lightgoldenrodyellow: 'fafad2',
|
|
75
|
+
lightgray: 'd3d3d3',
|
|
76
|
+
lightgreen: '90ee90',
|
|
77
|
+
lightgrey: 'd3d3d3',
|
|
78
|
+
lightpink: 'ffb6c1',
|
|
79
|
+
lightsalmon: 'ffa07a',
|
|
80
|
+
lightseagreen: '20b2aa',
|
|
81
|
+
lightskyblue: '87cefa',
|
|
82
|
+
lightslategray: '778899',
|
|
83
|
+
lightslategrey: '778899',
|
|
84
|
+
lightsteelblue: 'b0c4de',
|
|
85
|
+
lightyellow: 'ffffe0',
|
|
86
|
+
lime: '00ff00',
|
|
87
|
+
limegreen: '32cd32',
|
|
88
|
+
linen: 'faf0e6',
|
|
89
|
+
magenta: 'ff00ff',
|
|
90
|
+
maroon: '800000',
|
|
91
|
+
mediumaquamarine: '66cdaa',
|
|
92
|
+
mediumblue: '0000cd',
|
|
93
|
+
mediumorchid: 'ba55d3',
|
|
94
|
+
mediumpurple: '9370db',
|
|
95
|
+
mediumseagreen: '3cb371',
|
|
96
|
+
mediumslateblue: '7b68ee',
|
|
97
|
+
mediumspringgreen: '00fa9a',
|
|
98
|
+
mediumturquoise: '48d1cc',
|
|
99
|
+
mediumvioletred: 'c71585',
|
|
100
|
+
midnightblue: '191970',
|
|
101
|
+
mintcream: 'f5fffa',
|
|
102
|
+
mistyrose: 'ffe4e1',
|
|
103
|
+
moccasin: 'ffe4b5',
|
|
104
|
+
navajowhite: 'ffdead',
|
|
105
|
+
navy: '000080',
|
|
106
|
+
oldlace: 'fdf5e6',
|
|
107
|
+
olive: '808000',
|
|
108
|
+
olivedrab: '6b8e23',
|
|
109
|
+
orange: 'ffa500',
|
|
110
|
+
orangered: 'ff4500',
|
|
111
|
+
orchid: 'da70d6',
|
|
112
|
+
palegoldenrod: 'eee8aa',
|
|
113
|
+
palegreen: '98fb98',
|
|
114
|
+
paleturquoise: 'afeeee',
|
|
115
|
+
palevioletred: 'db7093',
|
|
116
|
+
papayawhip: 'ffefd5',
|
|
117
|
+
peachpuff: 'ffdab9',
|
|
118
|
+
peru: 'cd853f',
|
|
119
|
+
pink: 'ffc0cb',
|
|
120
|
+
plum: 'dda0dd',
|
|
121
|
+
powderblue: 'b0e0e6',
|
|
122
|
+
purple: '800080',
|
|
123
|
+
rebeccapurple: '663399',
|
|
124
|
+
red: 'ff0000',
|
|
125
|
+
rosybrown: 'bc8f8f',
|
|
126
|
+
royalblue: '4169e1',
|
|
127
|
+
saddlebrown: '8b4513',
|
|
128
|
+
salmon: 'fa8072',
|
|
129
|
+
sandybrown: 'f4a460',
|
|
130
|
+
seagreen: '2e8b57',
|
|
131
|
+
seashell: 'fff5ee',
|
|
132
|
+
sienna: 'a0522d',
|
|
133
|
+
silver: 'c0c0c0',
|
|
134
|
+
skyblue: '87ceeb',
|
|
135
|
+
slateblue: '6a5acd',
|
|
136
|
+
slategray: '708090',
|
|
137
|
+
slategrey: '708090',
|
|
138
|
+
snow: 'fffafa',
|
|
139
|
+
springgreen: '00ff7f',
|
|
140
|
+
steelblue: '4682b4',
|
|
141
|
+
tan: 'd2b48c',
|
|
142
|
+
teal: '008080',
|
|
143
|
+
thistle: 'd8bfd8',
|
|
144
|
+
tomato: 'ff6347',
|
|
145
|
+
turquoise: '40e0d0',
|
|
146
|
+
violet: 'ee82ee',
|
|
147
|
+
wheat: 'f5deb3',
|
|
148
|
+
white: 'ffffff',
|
|
149
|
+
whitesmoke: 'f5f5f5',
|
|
150
|
+
yellow: 'ffff00',
|
|
151
|
+
yellowgreen: '9acd32',
|
|
152
|
+
});
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
// `check.mjs --pair <text> <background> …`: contrast for candidate colors before they are written.
|
|
2
|
+
// Live recolor runs wrote their own OKLCH and APCA math to try palettes, spent several calls on it,
|
|
3
|
+
// and got APCA numbers that disagreed with this script's. This is the same math the check uses.
|
|
4
|
+
import {apcaLc, parseColor, wcagRatio} from './color.mjs';
|
|
5
|
+
import {
|
|
6
|
+
LARGE_APCA,
|
|
7
|
+
LARGE_RATIO,
|
|
8
|
+
NORMAL_APCA,
|
|
9
|
+
NORMAL_RATIO,
|
|
10
|
+
} from './contrast.mjs';
|
|
11
|
+
|
|
12
|
+
/** @typedef {{readonly text: string, readonly background: string}} PairInput */
|
|
13
|
+
|
|
14
|
+
/** @param {readonly string[]} values text, background, text, background, … */
|
|
15
|
+
export function pairsFrom(values) {
|
|
16
|
+
return values.flatMap((text, index) => {
|
|
17
|
+
const background = values[index + 1];
|
|
18
|
+
if (index % 2 !== 0 || background === undefined) return [];
|
|
19
|
+
return [{text, background}];
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Same levels as the full check: a WCAG miss is a FAIL, an APCA-only miss a WARN.
|
|
25
|
+
* @param {{label: string, ratio: number, lc: number, minRatio: number, minLc: number}} params
|
|
26
|
+
*/
|
|
27
|
+
function verdict({label, ratio, lc, minRatio, minLc}) {
|
|
28
|
+
if (ratio < minRatio) return `${label} FAIL (needs ${minRatio}:1)`;
|
|
29
|
+
if (lc < minLc) return `${label} WARN (APCA needs Lc ${minLc})`;
|
|
30
|
+
return `${label} pass`;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** @param {PairInput} pair @returns {string} */
|
|
34
|
+
export function describePair({text, background}) {
|
|
35
|
+
const textRgb = parseColor({value: text, vars: {}});
|
|
36
|
+
const backgroundRgb = parseColor({value: background, vars: {}});
|
|
37
|
+
const name = `${text} on ${background}`;
|
|
38
|
+
if (!textRgb || !backgroundRgb) {
|
|
39
|
+
return `${name}: not measured — use an opaque hex, rgb(), hsl(), oklch(), or named color`;
|
|
40
|
+
}
|
|
41
|
+
const pair = {text: textRgb, background: backgroundRgb};
|
|
42
|
+
const ratio = wcagRatio(pair);
|
|
43
|
+
const lc = Math.abs(apcaLc(pair));
|
|
44
|
+
return [
|
|
45
|
+
`${name}: ${ratio.toFixed(2)}:1, APCA Lc ${lc.toFixed(0)}`,
|
|
46
|
+
verdict({
|
|
47
|
+
label: 'body text',
|
|
48
|
+
ratio,
|
|
49
|
+
lc,
|
|
50
|
+
minRatio: NORMAL_RATIO,
|
|
51
|
+
minLc: NORMAL_APCA,
|
|
52
|
+
}),
|
|
53
|
+
verdict({
|
|
54
|
+
label: 'large text',
|
|
55
|
+
ratio,
|
|
56
|
+
lc,
|
|
57
|
+
minRatio: LARGE_RATIO,
|
|
58
|
+
minLc: LARGE_APCA,
|
|
59
|
+
}),
|
|
60
|
+
].join(' · ');
|
|
61
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
// design-system.html must render from the project's own stylesheet (system.md). A wrong relative
|
|
2
|
+
// link renders it unstyled, and live runs found that only by screenshotting the preview: a
|
|
3
|
+
// browser round-trip and a vision call per fix. A missing local stylesheet is checked directly.
|
|
4
|
+
import {existsSync, readFileSync} from 'node:fs';
|
|
5
|
+
import {dirname, join, resolve} from 'node:path';
|
|
6
|
+
|
|
7
|
+
const PREVIEW = join('.commandcode', 'design', 'design-system.html');
|
|
8
|
+
const LINKED_CSS = /<link\b[^>]*href=["']([^"']+\.css)["']/gi;
|
|
9
|
+
|
|
10
|
+
/** @typedef {import('./contrast.mjs').Finding} Finding */
|
|
11
|
+
|
|
12
|
+
/** @param {string} root the project directory @returns {readonly Finding[]} */
|
|
13
|
+
export function checkPreviewLinks(root) {
|
|
14
|
+
const preview = join(root, PREVIEW);
|
|
15
|
+
if (!existsSync(preview)) return [];
|
|
16
|
+
return [...readFileSync(preview, 'utf8').matchAll(LINKED_CSS)]
|
|
17
|
+
.map(([, href = '']) => href)
|
|
18
|
+
.filter(href => !/^(https?:)?\/\//.test(href))
|
|
19
|
+
.filter(href => !existsSync(resolve(dirname(preview), href)))
|
|
20
|
+
.map(href => ({
|
|
21
|
+
level: 'FAIL',
|
|
22
|
+
check: 'preview',
|
|
23
|
+
source: preview,
|
|
24
|
+
selector: `<link href="${href}">`,
|
|
25
|
+
detail: `links ${href}, which does not exist from .commandcode/design/ — the preview renders without the project's stylesheet`,
|
|
26
|
+
}));
|
|
27
|
+
}
|