impeccable 3.4.0 → 3.6.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/README.md +3 -3
- package/README.npm.md +3 -3
- package/README.repo.md +26 -3
- package/cli/bin/cli.js +6 -0
- package/cli/bin/commands/skills.mjs +234 -29
- package/cli/engine/browser/injected/index.mjs +56 -6
- package/cli/engine/design-system.mjs +140 -2
- package/cli/engine/detect-antipatterns-browser.js +1174 -694
- package/cli/engine/detect-antipatterns.mjs +1 -0
- package/cli/engine/engines/browser/detect-url.mjs +35 -3
- package/cli/engine/engines/regex/detect-text.mjs +431 -45
- package/cli/engine/engines/static-html/css-cascade.mjs +10 -1
- package/cli/engine/engines/static-html/detect-html.mjs +47 -6
- package/cli/engine/node/file-system.mjs +23 -22
- package/cli/engine/registry/antipatterns.mjs +0 -10
- package/cli/engine/rules/checks.mjs +384 -384
- package/cli/engine/shared/color.mjs +466 -2
- package/cli/lib/download-providers.js +7 -0
- package/cli/lib/impeccable-config.mjs +44 -59
- package/package.json +5 -3
|
@@ -71,11 +71,43 @@ function contrastRatio(c1, c2) {
|
|
|
71
71
|
return (Math.max(l1, l2) + 0.05) / (Math.min(l1, l2) + 0.05);
|
|
72
72
|
}
|
|
73
73
|
|
|
74
|
+
// The CSS color functions worth pulling out of a longer declaration. The set
|
|
75
|
+
// is deliberately closed: `linear-gradient(` and `url(` also look like
|
|
76
|
+
// `name(` and must not be read as colors.
|
|
77
|
+
const COLOR_FUNCTION_NAMES = new Set([
|
|
78
|
+
'rgb', 'rgba', 'hsl', 'hsla', 'hwb', 'oklch', 'oklab', 'lch', 'lab', 'color', 'color-mix',
|
|
79
|
+
]);
|
|
80
|
+
|
|
81
|
+
// Pull every color-function token out of a value, with balanced-paren capture
|
|
82
|
+
// so nested forms (`color-mix(in oklab, oklch(...) 20%, transparent)`) survive
|
|
83
|
+
// whole. Returns the raw substrings in source order.
|
|
84
|
+
function extractColorFunctionTokens(value) {
|
|
85
|
+
const str = String(value || '');
|
|
86
|
+
const tokens = [];
|
|
87
|
+
const re = /([a-z][a-z-]*)\(/gi;
|
|
88
|
+
let m;
|
|
89
|
+
while ((m = re.exec(str)) !== null) {
|
|
90
|
+
if (!COLOR_FUNCTION_NAMES.has(m[1].toLowerCase())) continue;
|
|
91
|
+
let depth = 0, end = -1;
|
|
92
|
+
for (let i = m.index + m[0].length - 1; i < str.length; i++) {
|
|
93
|
+
if (str[i] === '(') depth++;
|
|
94
|
+
else if (str[i] === ')') { depth--; if (depth === 0) { end = i; break; } }
|
|
95
|
+
}
|
|
96
|
+
if (end < 0) break;
|
|
97
|
+
tokens.push(str.slice(m.index, end + 1));
|
|
98
|
+
re.lastIndex = end + 1;
|
|
99
|
+
}
|
|
100
|
+
return tokens;
|
|
101
|
+
}
|
|
102
|
+
|
|
74
103
|
function parseGradientColors(bgImage) {
|
|
75
104
|
if (!bgImage || !bgImage.includes('gradient')) return [];
|
|
76
105
|
const colors = [];
|
|
77
|
-
|
|
78
|
-
|
|
106
|
+
// Stops arrive in whatever syntax the author wrote and the browser kept.
|
|
107
|
+
// A dark ground painted as `linear-gradient(oklch(...), oklch(...))` used
|
|
108
|
+
// to read as a gradient with no stops at all.
|
|
109
|
+
for (const token of extractColorFunctionTokens(bgImage)) {
|
|
110
|
+
const c = parseAnyColor(token);
|
|
79
111
|
if (c) colors.push(c);
|
|
80
112
|
}
|
|
81
113
|
for (const m of bgImage.matchAll(/#([0-9a-f]{6}|[0-9a-f]{3})\b/gi)) {
|
|
@@ -112,13 +144,445 @@ function colorToHex(c) {
|
|
|
112
144
|
return '#' + [c.r, c.g, c.b].map(v => v.toString(16).padStart(2, '0')).join('');
|
|
113
145
|
}
|
|
114
146
|
|
|
147
|
+
// ─── Color-space conversions ────────────────────────────────────────────────
|
|
148
|
+
//
|
|
149
|
+
// Every function here lands on 8-bit sRGB, clamped to gamut. Chrome, Safari,
|
|
150
|
+
// and Firefox all keep the authored color space in getComputedStyle output
|
|
151
|
+
// (`oklch(0.84 0.19 80.46)`, `lch(20 5 60)`, `color(srgb 1.04 0.72 -0.21)`),
|
|
152
|
+
// so a detector that only reads rgb() is blind on any modern palette. The
|
|
153
|
+
// expected outputs are pinned in tests/detect-antipatterns.test.js against
|
|
154
|
+
// what Chrome itself paints for the same strings.
|
|
155
|
+
|
|
156
|
+
function clamp01(x) {
|
|
157
|
+
return Number.isFinite(x) ? Math.max(0, Math.min(1, x)) : 0;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
// Linear-light sRGB channel to the encoded 0-255 value.
|
|
161
|
+
function encodeSrgbChannel(x) {
|
|
162
|
+
const c = clamp01(x);
|
|
163
|
+
return Math.round((c <= 0.0031308 ? 12.92 * c : 1.055 * Math.pow(c, 1 / 2.4) - 0.055) * 255);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
function decodeSrgbChannel(x) {
|
|
167
|
+
const c = Number.isFinite(x) ? x : 0;
|
|
168
|
+
const sign = c < 0 ? -1 : 1;
|
|
169
|
+
const abs = Math.abs(c);
|
|
170
|
+
return sign * (abs <= 0.04045 ? abs / 12.92 : Math.pow((abs + 0.055) / 1.055, 2.4));
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
function linearSrgbToColor(r, g, b, a = 1) {
|
|
174
|
+
return { r: encodeSrgbChannel(r), g: encodeSrgbChannel(g), b: encodeSrgbChannel(b), a };
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
// OKLab to sRGB (Björn Ottosson's matrices). L in 0..1, a/b are signed axes.
|
|
178
|
+
function oklabToRgb(L, a, b) {
|
|
179
|
+
const l_ = L + 0.3963377774 * a + 0.2158037573 * b;
|
|
180
|
+
const m_ = L - 0.1055613458 * a - 0.0638541728 * b;
|
|
181
|
+
const s_ = L - 0.0894841775 * a - 1.2914855480 * b;
|
|
182
|
+
const lc = l_ * l_ * l_, mc = m_ * m_ * m_, sc = s_ * s_ * s_;
|
|
183
|
+
return linearSrgbToColor(
|
|
184
|
+
4.0767416621 * lc - 3.3077115913 * mc + 0.2309699292 * sc,
|
|
185
|
+
-1.2684380046 * lc + 2.6097574011 * mc - 0.3413193965 * sc,
|
|
186
|
+
-0.0041960863 * lc - 0.7034186147 * mc + 1.7076147010 * sc,
|
|
187
|
+
);
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
// OKLCH to sRGB. L in 0..1, C in 0..~0.4 typical, H in degrees. Chroma past
|
|
191
|
+
// the sRGB gamut clamps per channel rather than producing NaN.
|
|
192
|
+
function oklchToRgb(L, C, H) {
|
|
193
|
+
const hRad = (H * Math.PI) / 180;
|
|
194
|
+
return oklabToRgb(L, C * Math.cos(hRad), C * Math.sin(hRad));
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
// CIE Lab to sRGB. CSS lab()/lch() use the D50 white point; the matrix below
|
|
198
|
+
// is the Bradford-adapted XYZ-D50 to linear-sRGB transform from CSS Color 4.
|
|
199
|
+
function labToRgb(L, a, b) {
|
|
200
|
+
const kappa = 24389 / 27, epsilon = 216 / 24389;
|
|
201
|
+
const fy = (L + 16) / 116, fx = fy + a / 500, fz = fy - b / 200;
|
|
202
|
+
const invert = (t) => (t * t * t > epsilon ? t * t * t : (116 * t - 16) / kappa);
|
|
203
|
+
const yr = L > kappa * epsilon ? Math.pow((L + 16) / 116, 3) : L / kappa;
|
|
204
|
+
const Xn = 0.3457 / 0.3585, Zn = (1 - 0.3457 - 0.3585) / 0.3585;
|
|
205
|
+
const x = invert(fx) * Xn, y = yr, z = invert(fz) * Zn;
|
|
206
|
+
return linearSrgbToColor(
|
|
207
|
+
3.1341359569958707 * x - 1.6173863321612538 * y - 0.4906619460083532 * z,
|
|
208
|
+
-0.9787955029120890 * x + 1.9162545672595240 * y + 0.0334427311613195 * z,
|
|
209
|
+
0.0719553798841168 * x - 0.2289768264158322 * y + 1.4053860583241250 * z,
|
|
210
|
+
);
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
function lchToRgb(L, C, H) {
|
|
214
|
+
const hRad = (H * Math.PI) / 180;
|
|
215
|
+
return labToRgb(L, C * Math.cos(hRad), C * Math.sin(hRad));
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
// color(<space> c1 c2 c3) for the spaces that turn up in real stylesheets.
|
|
219
|
+
// `srgb` is what Chrome serializes most color-mix() results into, routinely
|
|
220
|
+
// with channels outside 0..1. Spaces we do not model return null so callers
|
|
221
|
+
// abstain instead of measuring against a color we invented.
|
|
222
|
+
function colorFunctionToRgb(space, c1, c2, c3) {
|
|
223
|
+
switch (space) {
|
|
224
|
+
case 'srgb':
|
|
225
|
+
return { r: Math.round(clamp01(c1) * 255), g: Math.round(clamp01(c2) * 255), b: Math.round(clamp01(c3) * 255), a: 1 };
|
|
226
|
+
case 'srgb-linear':
|
|
227
|
+
return linearSrgbToColor(c1, c2, c3);
|
|
228
|
+
case 'display-p3': {
|
|
229
|
+
const [R, G, B] = [decodeSrgbChannel(c1), decodeSrgbChannel(c2), decodeSrgbChannel(c3)];
|
|
230
|
+
return linearSrgbToColor(
|
|
231
|
+
1.2249401762805587 * R - 0.2249404646817506 * G + 0.0000002884022551 * B,
|
|
232
|
+
-0.0420569547096138 * R + 1.0420571661298634 * G - 0.0000002113202247 * B,
|
|
233
|
+
-0.0196375587040044 * R - 0.0786360772174755 * G + 1.0982736359214800 * B,
|
|
234
|
+
);
|
|
235
|
+
}
|
|
236
|
+
default:
|
|
237
|
+
return null;
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
function hslToRgb(h, s, l) {
|
|
242
|
+
h = ((h % 360) + 360) % 360;
|
|
243
|
+
const c = (1 - Math.abs(2 * l - 1)) * s;
|
|
244
|
+
const x = c * (1 - Math.abs(((h / 60) % 2) - 1));
|
|
245
|
+
const m0 = l - c / 2;
|
|
246
|
+
const [r, g, b] =
|
|
247
|
+
h < 60 ? [c, x, 0] :
|
|
248
|
+
h < 120 ? [x, c, 0] :
|
|
249
|
+
h < 180 ? [0, c, x] :
|
|
250
|
+
h < 240 ? [0, x, c] :
|
|
251
|
+
h < 300 ? [x, 0, c] : [c, 0, x];
|
|
252
|
+
return {
|
|
253
|
+
r: Math.round((r + m0) * 255),
|
|
254
|
+
g: Math.round((g + m0) * 255),
|
|
255
|
+
b: Math.round((b + m0) * 255),
|
|
256
|
+
a: 1,
|
|
257
|
+
};
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
function hwbToRgb(h, w, bl) {
|
|
261
|
+
if (w + bl >= 1) {
|
|
262
|
+
const g = Math.round((w / (w + bl)) * 255);
|
|
263
|
+
return { r: g, g, b: g, a: 1 };
|
|
264
|
+
}
|
|
265
|
+
const base = hslToRgb(h, 1, 0.5);
|
|
266
|
+
const mix = (c) => Math.round(((c / 255) * (1 - w - bl) + w) * 255);
|
|
267
|
+
return { r: mix(base.r), g: mix(base.g), b: mix(base.b), a: 1 };
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
// Common CSS named colors — the handful that actually show up in generated
|
|
271
|
+
// UIs, not the full 148-name spec list. Includes the achromatic names so a
|
|
272
|
+
// named gray parses (and correctly reads as no-chroma) instead of being
|
|
273
|
+
// treated as an unknown color.
|
|
274
|
+
const CSS_NAMED_COLORS = {
|
|
275
|
+
black: { r: 0, g: 0, b: 0 },
|
|
276
|
+
white: { r: 255, g: 255, b: 255 },
|
|
277
|
+
gray: { r: 128, g: 128, b: 128 },
|
|
278
|
+
grey: { r: 128, g: 128, b: 128 },
|
|
279
|
+
silver: { r: 192, g: 192, b: 192 },
|
|
280
|
+
dimgray: { r: 105, g: 105, b: 105 },
|
|
281
|
+
darkgray: { r: 169, g: 169, b: 169 },
|
|
282
|
+
lightgray: { r: 211, g: 211, b: 211 },
|
|
283
|
+
gainsboro: { r: 220, g: 220, b: 220 },
|
|
284
|
+
whitesmoke: { r: 245, g: 245, b: 245 },
|
|
285
|
+
red: { r: 255, g: 0, b: 0 },
|
|
286
|
+
crimson: { r: 220, g: 20, b: 60 },
|
|
287
|
+
tomato: { r: 255, g: 99, b: 71 },
|
|
288
|
+
coral: { r: 255, g: 127, b: 80 },
|
|
289
|
+
salmon: { r: 250, g: 128, b: 114 },
|
|
290
|
+
orange: { r: 255, g: 165, b: 0 },
|
|
291
|
+
gold: { r: 255, g: 215, b: 0 },
|
|
292
|
+
yellow: { r: 255, g: 255, b: 0 },
|
|
293
|
+
olive: { r: 128, g: 128, b: 0 },
|
|
294
|
+
lime: { r: 0, g: 255, b: 0 },
|
|
295
|
+
green: { r: 0, g: 128, b: 0 },
|
|
296
|
+
teal: { r: 0, g: 128, b: 128 },
|
|
297
|
+
turquoise: { r: 64, g: 224, b: 208 },
|
|
298
|
+
cyan: { r: 0, g: 255, b: 255 },
|
|
299
|
+
aqua: { r: 0, g: 255, b: 255 },
|
|
300
|
+
skyblue: { r: 135, g: 206, b: 235 },
|
|
301
|
+
dodgerblue: { r: 30, g: 144, b: 255 },
|
|
302
|
+
blue: { r: 0, g: 0, b: 255 },
|
|
303
|
+
navy: { r: 0, g: 0, b: 128 },
|
|
304
|
+
indigo: { r: 75, g: 0, b: 130 },
|
|
305
|
+
rebeccapurple: { r: 102, g: 51, b: 153 },
|
|
306
|
+
purple: { r: 128, g: 0, b: 128 },
|
|
307
|
+
violet: { r: 238, g: 130, b: 238 },
|
|
308
|
+
orchid: { r: 218, g: 112, b: 214 },
|
|
309
|
+
magenta: { r: 255, g: 0, b: 255 },
|
|
310
|
+
fuchsia: { r: 255, g: 0, b: 255 },
|
|
311
|
+
hotpink: { r: 255, g: 105, b: 180 },
|
|
312
|
+
pink: { r: 255, g: 192, b: 203 },
|
|
313
|
+
maroon: { r: 128, g: 0, b: 0 },
|
|
314
|
+
};
|
|
315
|
+
|
|
316
|
+
// Split a string on top-level commas (ignoring commas nested in parens).
|
|
317
|
+
function splitTopLevelCommas(str) {
|
|
318
|
+
const parts = [];
|
|
319
|
+
let depth = 0, start = 0;
|
|
320
|
+
for (let i = 0; i < str.length; i++) {
|
|
321
|
+
const ch = str[i];
|
|
322
|
+
if (ch === '(') depth++;
|
|
323
|
+
else if (ch === ')') depth = Math.max(0, depth - 1);
|
|
324
|
+
else if (ch === ',' && depth === 0) {
|
|
325
|
+
parts.push(str.slice(start, i).trim());
|
|
326
|
+
start = i + 1;
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
const tail = str.slice(start).trim();
|
|
330
|
+
if (tail) parts.push(tail);
|
|
331
|
+
return parts;
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
// Evaluate a CSS color-mix() expression to {r,g,b,a}. Returns null when
|
|
335
|
+
// the expression can't be resolved (unresolved var(), unknown colors).
|
|
336
|
+
//
|
|
337
|
+
// Mixing is done with premultiplied alpha in sRGB regardless of the
|
|
338
|
+
// declared interpolation space. That is exact for the dominant generated-UI
|
|
339
|
+
// pattern — `color-mix(in oklab, <color> N%, transparent)` — where the
|
|
340
|
+
// result is simply <color> at alpha N% in ANY rectangular space, and a
|
|
341
|
+
// close-enough approximation for opaque-opaque mixes (the detector only
|
|
342
|
+
// consumes these values for contrast/chroma thresholds, not for display).
|
|
343
|
+
function parseColorMix(str) {
|
|
344
|
+
const m = String(str).trim().match(/^color-mix\(/i);
|
|
345
|
+
if (!m) return null;
|
|
346
|
+
// Balanced-paren capture of the arguments.
|
|
347
|
+
let depth = 0, end = -1;
|
|
348
|
+
const open = str.indexOf('(');
|
|
349
|
+
for (let i = open; i < str.length; i++) {
|
|
350
|
+
if (str[i] === '(') depth++;
|
|
351
|
+
else if (str[i] === ')') { depth--; if (depth === 0) { end = i; break; } }
|
|
352
|
+
}
|
|
353
|
+
if (end < 0) return null;
|
|
354
|
+
const args = splitTopLevelCommas(str.slice(open + 1, end));
|
|
355
|
+
if (args.length !== 3 || !/^in\s/i.test(args[0])) return null;
|
|
356
|
+
|
|
357
|
+
const parseComponent = (component) => {
|
|
358
|
+
// Percentage may lead or trail the color per spec.
|
|
359
|
+
let pct = null;
|
|
360
|
+
let colorStr = component;
|
|
361
|
+
const trail = component.match(/\s+([\d.]+)%$/);
|
|
362
|
+
const lead = component.match(/^([\d.]+)%\s+/);
|
|
363
|
+
if (trail) { pct = parseFloat(trail[1]); colorStr = component.slice(0, trail.index).trim(); }
|
|
364
|
+
else if (lead) { pct = parseFloat(lead[1]); colorStr = component.slice(lead[0].length).trim(); }
|
|
365
|
+
let color;
|
|
366
|
+
if (/^transparent$/i.test(colorStr)) color = { r: 0, g: 0, b: 0, a: 0 };
|
|
367
|
+
else color = parseAnyColor(colorStr);
|
|
368
|
+
if (!color) return null;
|
|
369
|
+
return { color, pct };
|
|
370
|
+
};
|
|
371
|
+
|
|
372
|
+
const c1 = parseComponent(args[1]);
|
|
373
|
+
const c2 = parseComponent(args[2]);
|
|
374
|
+
if (!c1 || !c2) return null;
|
|
375
|
+
let p1 = c1.pct, p2 = c2.pct;
|
|
376
|
+
if (p1 == null && p2 == null) { p1 = 50; p2 = 50; }
|
|
377
|
+
else if (p1 == null) p1 = 100 - p2;
|
|
378
|
+
else if (p2 == null) p2 = 100 - p1;
|
|
379
|
+
const sum = p1 + p2;
|
|
380
|
+
if (sum <= 0) return null;
|
|
381
|
+
// Per spec: weights normalize to sum; when sum < 100 the result alpha is
|
|
382
|
+
// additionally scaled by sum/100.
|
|
383
|
+
const w1 = p1 / sum, w2 = p2 / sum;
|
|
384
|
+
const alphaScale = sum < 100 ? sum / 100 : 1;
|
|
385
|
+
const a1 = c1.color.a ?? 1, a2 = c2.color.a ?? 1;
|
|
386
|
+
const a = (a1 * w1 + a2 * w2) * alphaScale;
|
|
387
|
+
if (a <= 0) return { r: 0, g: 0, b: 0, a: 0 };
|
|
388
|
+
const mix = (ch) => Math.round((c1.color[ch] * a1 * w1 + c2.color[ch] * a2 * w2) / (a1 * w1 + a2 * w2));
|
|
389
|
+
return { r: mix('r'), g: mix('g'), b: mix('b'), a: Math.min(1, a) };
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
// Composite a translucent color over an opaque(ish) base (simple
|
|
393
|
+
// source-over in sRGB). Returns an opaque {r,g,b,a:1}.
|
|
394
|
+
function compositeColorOver(top, base) {
|
|
395
|
+
const a = top.a ?? 1;
|
|
396
|
+
return {
|
|
397
|
+
r: Math.round(top.r * a + base.r * (1 - a)),
|
|
398
|
+
g: Math.round(top.g * a + base.g * (1 - a)),
|
|
399
|
+
b: Math.round(top.b * a + base.b * (1 - a)),
|
|
400
|
+
a: 1,
|
|
401
|
+
};
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
// A color() / lab() / lch() component: a bare number, a percentage against
|
|
405
|
+
// `scale`, or the `none` keyword (which resolves to zero for our purposes).
|
|
406
|
+
function parseColorComponent(token, scale = 1) {
|
|
407
|
+
if (token == null) return null;
|
|
408
|
+
const t = String(token).trim();
|
|
409
|
+
if (/^none$/i.test(t)) return 0;
|
|
410
|
+
const num = parseFloat(t);
|
|
411
|
+
if (!Number.isFinite(num)) return null;
|
|
412
|
+
return t.endsWith('%') ? (num / 100) * scale : num;
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
function parseAlphaToken(token) {
|
|
416
|
+
if (token == null) return 1;
|
|
417
|
+
const t = String(token).trim();
|
|
418
|
+
if (/^none$/i.test(t)) return 1;
|
|
419
|
+
const num = parseFloat(t);
|
|
420
|
+
if (!Number.isFinite(num)) return 1;
|
|
421
|
+
return t.endsWith('%') ? num / 100 : num;
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
// Extended color parser: rgb/rgba/hex/oklch/oklab/lch/lab/hsl/hwb/color()/
|
|
425
|
+
// color-mix/common named colors. Returns null on no match. Use this when the
|
|
426
|
+
// input might be any CSS color form; use plain parseRgb when you only expect
|
|
427
|
+
// computed rgb() values from real browsers.
|
|
428
|
+
function parseAnyColor(s) {
|
|
429
|
+
if (!s || typeof s !== 'string') return null;
|
|
430
|
+
const str = s.trim();
|
|
431
|
+
if (str === 'transparent' || str === 'currentcolor' || str === 'inherit') return null;
|
|
432
|
+
if (/^color-mix\(/i.test(str)) return parseColorMix(str);
|
|
433
|
+
let m;
|
|
434
|
+
m = str.match(/rgba?\(\s*(\d+(?:\.\d+)?)\s*,?\s*(\d+(?:\.\d+)?)\s*,?\s*(\d+(?:\.\d+)?)(?:\s*[,/]\s*([\d.]+)(%)?)?\s*\)/);
|
|
435
|
+
if (m) {
|
|
436
|
+
const c = { r: Math.round(+m[1]), g: Math.round(+m[2]), b: Math.round(+m[3]), a: 1 };
|
|
437
|
+
if (m[4] !== undefined) c.a = m[5] === '%' ? parseFloat(m[4]) / 100 : +m[4];
|
|
438
|
+
return c;
|
|
439
|
+
}
|
|
440
|
+
m = str.match(/^#([0-9a-f]{3,8})$/i);
|
|
441
|
+
if (m) {
|
|
442
|
+
const h = m[1];
|
|
443
|
+
if (h.length === 3 || h.length === 4) {
|
|
444
|
+
return {
|
|
445
|
+
r: parseInt(h[0] + h[0], 16),
|
|
446
|
+
g: parseInt(h[1] + h[1], 16),
|
|
447
|
+
b: parseInt(h[2] + h[2], 16),
|
|
448
|
+
a: h.length === 4 ? parseInt(h[3] + h[3], 16) / 255 : 1,
|
|
449
|
+
};
|
|
450
|
+
}
|
|
451
|
+
if (h.length === 6 || h.length === 8) {
|
|
452
|
+
return {
|
|
453
|
+
r: parseInt(h.slice(0, 2), 16),
|
|
454
|
+
g: parseInt(h.slice(2, 4), 16),
|
|
455
|
+
b: parseInt(h.slice(4, 6), 16),
|
|
456
|
+
a: h.length === 8 ? parseInt(h.slice(6, 8), 16) / 255 : 1,
|
|
457
|
+
};
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
// OKLCH parser. Tailwind v4's CSS minifier squishes the space after
|
|
461
|
+
// `%` ("21.5%.02 50"), so the separator between L and C may be absent.
|
|
462
|
+
// Match L (with optional %), then C and H separated permissively.
|
|
463
|
+
m = str.match(/oklch\(\s*([\d.]+)(%?)\s*[\s,]*\s*([\d.]+)\s*[\s,]+\s*([-\d.]+)(?:deg)?(?:\s*\/\s*([\d.]+)(%)?)?\s*\)/i);
|
|
464
|
+
if (m) {
|
|
465
|
+
const Lnum = parseFloat(m[1]);
|
|
466
|
+
const L = m[2] === '%' ? Lnum / 100 : Lnum;
|
|
467
|
+
const rgb = oklchToRgb(L, parseFloat(m[3]), parseFloat(m[4]));
|
|
468
|
+
if (m[5] !== undefined) {
|
|
469
|
+
const alpha = parseFloat(m[5]);
|
|
470
|
+
rgb.a = m[6] === '%' ? alpha / 100 : alpha;
|
|
471
|
+
}
|
|
472
|
+
return rgb;
|
|
473
|
+
}
|
|
474
|
+
// OKLAB — a/b are signed axes; percentages map 100% → 0.4.
|
|
475
|
+
m = str.match(/oklab\(\s*([\d.]+)(%?)\s+(-?[\d.]+)(%?)\s+(-?[\d.]+)(%?)(?:\s*\/\s*([\d.]+)(%)?)?\s*\)/i);
|
|
476
|
+
if (m) {
|
|
477
|
+
const L = m[2] === '%' ? parseFloat(m[1]) / 100 : parseFloat(m[1]);
|
|
478
|
+
const a = m[4] === '%' ? parseFloat(m[3]) * 0.004 : parseFloat(m[3]);
|
|
479
|
+
const b = m[6] === '%' ? parseFloat(m[5]) * 0.004 : parseFloat(m[5]);
|
|
480
|
+
const rgb = oklabToRgb(L, a, b);
|
|
481
|
+
if (m[7] !== undefined) {
|
|
482
|
+
const alpha = parseFloat(m[7]);
|
|
483
|
+
rgb.a = m[8] === '%' ? alpha / 100 : alpha;
|
|
484
|
+
}
|
|
485
|
+
return rgb;
|
|
486
|
+
}
|
|
487
|
+
// LCH / LAB — CIE, D50 white point. Chrome serializes lch(20% 5 60) as
|
|
488
|
+
// `lch(20 5 60)`, so L arrives with or without its percent sign. In both
|
|
489
|
+
// spaces L runs 0..100 and 100% means 100.
|
|
490
|
+
m = str.match(/^lch\(\s*([\d.]+%?|none)\s+([\d.]+%?|none)\s+(-?[\d.]+)(?:deg)?(?:\s*\/\s*([\d.]+%?|none))?\s*\)$/i);
|
|
491
|
+
if (m) {
|
|
492
|
+
const L = parseColorComponent(m[1], 100);
|
|
493
|
+
const C = parseColorComponent(m[2], 150);
|
|
494
|
+
const H = parseFloat(m[3]);
|
|
495
|
+
if (L == null || C == null || !Number.isFinite(H)) return null;
|
|
496
|
+
const rgb = lchToRgb(L, C, H);
|
|
497
|
+
rgb.a = parseAlphaToken(m[4]);
|
|
498
|
+
return rgb;
|
|
499
|
+
}
|
|
500
|
+
m = str.match(/^lab\(\s*([\d.]+%?|none)\s+(-?[\d.]+%?|none)\s+(-?[\d.]+%?|none)(?:\s*\/\s*([\d.]+%?|none))?\s*\)$/i);
|
|
501
|
+
if (m) {
|
|
502
|
+
const L = parseColorComponent(m[1], 100);
|
|
503
|
+
const a = parseColorComponent(m[2], 125);
|
|
504
|
+
const b = parseColorComponent(m[3], 125);
|
|
505
|
+
if (L == null || a == null || b == null) return null;
|
|
506
|
+
const rgb = labToRgb(L, a, b);
|
|
507
|
+
rgb.a = parseAlphaToken(m[4]);
|
|
508
|
+
return rgb;
|
|
509
|
+
}
|
|
510
|
+
// color(<space> c1 c2 c3 [/ alpha]) — what Chrome hands back for most
|
|
511
|
+
// color-mix() results and for any wide-gamut color an author wrote.
|
|
512
|
+
m = str.match(/^color\(\s*([a-z0-9-]+)\s+(-?[\d.eE+-]+%?|none)\s+(-?[\d.eE+-]+%?|none)\s+(-?[\d.eE+-]+%?|none)(?:\s*\/\s*([\d.]+%?|none))?\s*\)$/i);
|
|
513
|
+
if (m) {
|
|
514
|
+
const c1 = parseColorComponent(m[2]);
|
|
515
|
+
const c2 = parseColorComponent(m[3]);
|
|
516
|
+
const c3 = parseColorComponent(m[4]);
|
|
517
|
+
if (c1 == null || c2 == null || c3 == null) return null;
|
|
518
|
+
const rgb = colorFunctionToRgb(m[1].toLowerCase(), c1, c2, c3);
|
|
519
|
+
if (!rgb) return null;
|
|
520
|
+
rgb.a = parseAlphaToken(m[5]);
|
|
521
|
+
return rgb;
|
|
522
|
+
}
|
|
523
|
+
// HSL/HSLA — comma or space syntax, optional deg on hue.
|
|
524
|
+
m = str.match(/hsla?\(\s*(-?[\d.]+)(?:deg)?\s*[,\s]\s*([\d.]+)%\s*[,\s]\s*([\d.]+)%(?:\s*[,/]\s*([\d.]+)(%)?)?\s*\)/i);
|
|
525
|
+
if (m) {
|
|
526
|
+
const rgb = hslToRgb(parseFloat(m[1]), parseFloat(m[2]) / 100, parseFloat(m[3]) / 100);
|
|
527
|
+
if (m[4] !== undefined) {
|
|
528
|
+
const alpha = parseFloat(m[4]);
|
|
529
|
+
rgb.a = m[5] === '%' ? alpha / 100 : alpha;
|
|
530
|
+
}
|
|
531
|
+
return rgb;
|
|
532
|
+
}
|
|
533
|
+
// HWB — hue whiteness% blackness%.
|
|
534
|
+
m = str.match(/hwb\(\s*(-?[\d.]+)(?:deg)?\s+([\d.]+)%\s+([\d.]+)%(?:\s*\/\s*([\d.]+)(%)?)?\s*\)/i);
|
|
535
|
+
if (m) {
|
|
536
|
+
const rgb = hwbToRgb(parseFloat(m[1]), parseFloat(m[2]) / 100, parseFloat(m[3]) / 100);
|
|
537
|
+
if (m[4] !== undefined) {
|
|
538
|
+
const alpha = parseFloat(m[4]);
|
|
539
|
+
rgb.a = m[5] === '%' ? alpha / 100 : alpha;
|
|
540
|
+
}
|
|
541
|
+
return rgb;
|
|
542
|
+
}
|
|
543
|
+
const named = CSS_NAMED_COLORS[str.toLowerCase()];
|
|
544
|
+
if (named) return { ...named, a: 1 };
|
|
545
|
+
return null;
|
|
546
|
+
}
|
|
547
|
+
|
|
548
|
+
// True when a computed background-color string names no paint at all. Used to
|
|
549
|
+
// tell "this layer is see-through" (walk on to the ancestor) apart from "this
|
|
550
|
+
// layer has a color we could not read" (stop and abstain).
|
|
551
|
+
//
|
|
552
|
+
// `inherit` belongs here even though it is not literally see-through: it means
|
|
553
|
+
// "paint with the parent's background-color", and walking on to the parent IS
|
|
554
|
+
// that resolution. Real browsers resolve the keyword before getComputedStyle
|
|
555
|
+
// output; only jsdom's partial cascade hands it through verbatim, and treating
|
|
556
|
+
// it as unreadable would make the walk abstain on a surface it can know.
|
|
557
|
+
// (`currentcolor` is NOT here — it is real paint in the element's own text
|
|
558
|
+
// color; resolveBackgroundInfo substitutes the computed color for it.)
|
|
559
|
+
function isNoPaintColorValue(value) {
|
|
560
|
+
const v = String(value || '').trim().toLowerCase();
|
|
561
|
+
if (!v) return true;
|
|
562
|
+
return v === 'transparent' || v === 'none' || v === 'initial' || v === 'inherit' || v === 'unset' || v === 'revert' || v === 'revert-layer';
|
|
563
|
+
}
|
|
564
|
+
|
|
115
565
|
export {
|
|
116
566
|
isNeutralColor,
|
|
117
567
|
parseRgb,
|
|
118
568
|
relativeLuminance,
|
|
119
569
|
contrastRatio,
|
|
120
570
|
parseGradientColors,
|
|
571
|
+
extractColorFunctionTokens,
|
|
121
572
|
hasChroma,
|
|
122
573
|
getHue,
|
|
123
574
|
colorToHex,
|
|
575
|
+
oklabToRgb,
|
|
576
|
+
oklchToRgb,
|
|
577
|
+
labToRgb,
|
|
578
|
+
lchToRgb,
|
|
579
|
+
colorFunctionToRgb,
|
|
580
|
+
hslToRgb,
|
|
581
|
+
hwbToRgb,
|
|
582
|
+
CSS_NAMED_COLORS,
|
|
583
|
+
splitTopLevelCommas,
|
|
584
|
+
parseColorMix,
|
|
585
|
+
parseAnyColor,
|
|
586
|
+
compositeColorOver,
|
|
587
|
+
isNoPaintColorValue,
|
|
124
588
|
};
|
|
@@ -1,11 +1,18 @@
|
|
|
1
|
+
// Nothing in this repo imports this module. Its consumers are the Cloudflare
|
|
2
|
+
// Pages Functions in the private impeccable-site repo (functions/api/download/
|
|
3
|
+
// bundle/[provider].js and [type]/[provider]/[id].js), which share cli/lib/.
|
|
4
|
+
// Do not remove it as dead code; keep the provider list in sync with the
|
|
5
|
+
// harness dirs the build emits.
|
|
1
6
|
export const FILE_DOWNLOAD_PROVIDER_CONFIG_DIRS = Object.freeze({
|
|
2
7
|
cursor: '.cursor',
|
|
3
8
|
'claude-code': '.claude',
|
|
4
9
|
gemini: '.gemini',
|
|
5
10
|
codex: '.codex',
|
|
6
11
|
agents: '.agents',
|
|
12
|
+
antigravity: '.agent',
|
|
7
13
|
github: '.github',
|
|
8
14
|
grok: '.grok',
|
|
15
|
+
hermes: '.hermes',
|
|
9
16
|
kiro: '.kiro',
|
|
10
17
|
opencode: '.opencode',
|
|
11
18
|
pi: '.pi',
|
|
@@ -206,10 +206,10 @@ function parseIgnoreColor(value) {
|
|
|
206
206
|
if (rgb) {
|
|
207
207
|
const parts = splitColorArgs(rgb[1]);
|
|
208
208
|
if (parts.length < 3 || parts.length > 4) return null;
|
|
209
|
-
const r =
|
|
210
|
-
const g =
|
|
211
|
-
const b =
|
|
212
|
-
const a = parts[3] === undefined ? 1 :
|
|
209
|
+
const r = parseColorChannel(parts[0], COLOR_CHANNEL_FORMATS.rgb);
|
|
210
|
+
const g = parseColorChannel(parts[1], COLOR_CHANNEL_FORMATS.rgb);
|
|
211
|
+
const b = parseColorChannel(parts[2], COLOR_CHANNEL_FORMATS.rgb);
|
|
212
|
+
const a = parts[3] === undefined ? 1 : parseColorChannel(parts[3], COLOR_CHANNEL_FORMATS.alpha);
|
|
213
213
|
if ([r, g, b, a].some((v) => v === null)) return null;
|
|
214
214
|
return { r, g, b, a };
|
|
215
215
|
}
|
|
@@ -218,10 +218,10 @@ function parseIgnoreColor(value) {
|
|
|
218
218
|
if (hsl) {
|
|
219
219
|
const parts = splitColorArgs(hsl[1]);
|
|
220
220
|
if (parts.length < 3 || parts.length > 4) return null;
|
|
221
|
-
const h =
|
|
222
|
-
const s =
|
|
223
|
-
const l =
|
|
224
|
-
const a = parts[3] === undefined ? 1 :
|
|
221
|
+
const h = parseColorChannel(parts[0], COLOR_CHANNEL_FORMATS.hue);
|
|
222
|
+
const s = parseColorChannel(parts[1], COLOR_CHANNEL_FORMATS.percent);
|
|
223
|
+
const l = parseColorChannel(parts[2], COLOR_CHANNEL_FORMATS.percent);
|
|
224
|
+
const a = parts[3] === undefined ? 1 : parseColorChannel(parts[3], COLOR_CHANNEL_FORMATS.alpha);
|
|
225
225
|
if ([h, s, l, a].some((v) => v === null)) return null;
|
|
226
226
|
return hslToRgb(h, s, l, a);
|
|
227
227
|
}
|
|
@@ -230,18 +230,13 @@ function parseIgnoreColor(value) {
|
|
|
230
230
|
}
|
|
231
231
|
|
|
232
232
|
function parseHexIgnoreColor(hex) {
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
}
|
|
240
|
-
const r = parseInt(hex.slice(0, 2), 16);
|
|
241
|
-
const g = parseInt(hex.slice(2, 4), 16);
|
|
242
|
-
const b = parseInt(hex.slice(4, 6), 16);
|
|
243
|
-
const a = hex.length === 8 ? parseInt(hex.slice(6, 8), 16) / 255 : 1;
|
|
244
|
-
return { r, g, b, a };
|
|
233
|
+
const expanded = hex.length <= 4
|
|
234
|
+
? [...hex].map((digit) => digit.repeat(2)).join('')
|
|
235
|
+
: hex;
|
|
236
|
+
const [r, g, b, alpha = 255] = expanded
|
|
237
|
+
.match(/../g)
|
|
238
|
+
.map((channel) => Number.parseInt(channel, 16));
|
|
239
|
+
return { r, g, b, a: alpha / 255 };
|
|
245
240
|
}
|
|
246
241
|
|
|
247
242
|
function splitColorArgs(body) {
|
|
@@ -259,47 +254,34 @@ function splitColorArgs(body) {
|
|
|
259
254
|
return text.replace(/\s*\/\s*/g, ' / ').split(/\s+/).filter((part) => part && part !== '/');
|
|
260
255
|
}
|
|
261
256
|
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
257
|
+
const CSS_NUMBER_RE = /^(-?\d*\.?\d+)(%|deg|rad|turn|grad)?$/;
|
|
258
|
+
const identity = (value) => value;
|
|
259
|
+
const COLOR_CHANNEL_FORMATS = {
|
|
260
|
+
rgb: { units: { '': identity, '%': (value) => value * 2.55 }, min: 0, max: 255, round: true },
|
|
261
|
+
alpha: { units: { '': identity, '%': (value) => value / 100 }, min: 0, max: 1 },
|
|
262
|
+
hue: {
|
|
263
|
+
units: {
|
|
264
|
+
'': identity,
|
|
265
|
+
deg: identity,
|
|
266
|
+
rad: (value) => value * (180 / Math.PI),
|
|
267
|
+
turn: (value) => value * 360,
|
|
268
|
+
grad: (value) => value * 0.9,
|
|
269
|
+
},
|
|
270
|
+
},
|
|
271
|
+
percent: { units: { '%': (value) => value / 100 }, min: 0, max: 1 },
|
|
272
|
+
};
|
|
273
|
+
|
|
274
|
+
function parseColorChannel(raw, { units, min = -Infinity, max = Infinity, round = false }) {
|
|
274
275
|
const text = String(raw || '').trim();
|
|
275
|
-
const match = text.match(
|
|
276
|
+
const match = text.match(CSS_NUMBER_RE);
|
|
276
277
|
if (!match) return null;
|
|
277
|
-
const
|
|
278
|
-
if (!
|
|
279
|
-
const
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
const text = String(raw || '').trim();
|
|
285
|
-
const match = text.match(/^(-?\d*\.?\d+)(deg|rad|turn|grad)?$/);
|
|
286
|
-
if (!match) return null;
|
|
287
|
-
const value = Number.parseFloat(match[1]);
|
|
288
|
-
if (!Number.isFinite(value)) return null;
|
|
289
|
-
const unit = match[2] || 'deg';
|
|
290
|
-
if (unit === 'turn') return value * 360;
|
|
291
|
-
if (unit === 'rad') return value * (180 / Math.PI);
|
|
292
|
-
if (unit === 'grad') return value * 0.9;
|
|
293
|
-
return value;
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
function parsePercentChannel(raw) {
|
|
297
|
-
const text = String(raw || '').trim();
|
|
298
|
-
const match = text.match(/^(-?\d*\.?\d+)%$/);
|
|
299
|
-
if (!match) return null;
|
|
300
|
-
const value = Number.parseFloat(match[1]);
|
|
301
|
-
if (!Number.isFinite(value)) return null;
|
|
302
|
-
return value >= 0 && value <= 100 ? value / 100 : null;
|
|
278
|
+
const convert = units[match[2] || ''];
|
|
279
|
+
if (!convert) return null;
|
|
280
|
+
const number = Number.parseFloat(match[1]);
|
|
281
|
+
if (!Number.isFinite(number)) return null;
|
|
282
|
+
const value = convert(number);
|
|
283
|
+
if (value < min || value > max) return null;
|
|
284
|
+
return round ? Math.round(value) : value;
|
|
303
285
|
}
|
|
304
286
|
|
|
305
287
|
function hslToRgb(hue, saturation, lightness, alpha) {
|
|
@@ -529,6 +511,9 @@ function extractFindingIgnoreValueRaw(finding, rule = normalizeIgnoreRule(findin
|
|
|
529
511
|
const primary = text.match(/Primary font:\s*([^()\n;]+)/i);
|
|
530
512
|
if (primary) return cleanIgnoreValueDisplay(primary[1]);
|
|
531
513
|
|
|
514
|
+
const googleLabel = text.match(/Google Fonts:\s*([^()\n;]+)/i);
|
|
515
|
+
if (googleLabel) return cleanIgnoreValueDisplay(googleLabel[1]);
|
|
516
|
+
|
|
532
517
|
const family = text.match(/font-family\s*:\s*["']?([^'",;\n]+)/i);
|
|
533
518
|
if (family) return cleanIgnoreValueDisplay(family[1]);
|
|
534
519
|
|