impeccable 3.6.1 → 4.0.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/README.md +24 -19
- package/README.npm.md +24 -19
- package/README.repo.md +13 -7
- package/cli/bin/cli.js +84 -91
- package/package.json +15 -21
- package/cli/bin/commands/ignores.mjs +0 -355
- package/cli/bin/commands/skills.mjs +0 -2415
- package/cli/engine/browser/injected/index.mjs +0 -2204
- package/cli/engine/cli/main.mjs +0 -432
- package/cli/engine/design-system.mjs +0 -1311
- package/cli/engine/detect-antipatterns-browser.js +0 -9043
- package/cli/engine/detect-antipatterns.mjs +0 -51
- package/cli/engine/engines/browser/detect-url.mjs +0 -434
- package/cli/engine/engines/regex/detect-text.mjs +0 -1319
- package/cli/engine/engines/static-html/css-cascade.mjs +0 -1241
- package/cli/engine/engines/static-html/detect-html.mjs +0 -288
- package/cli/engine/engines/visual/screenshot-contrast.mjs +0 -189
- package/cli/engine/findings.mjs +0 -18
- package/cli/engine/node/file-system.mjs +0 -213
- package/cli/engine/profile/profiler.mjs +0 -166
- package/cli/engine/registry/antipatterns.mjs +0 -635
- package/cli/engine/rules/checks.mjs +0 -5681
- package/cli/engine/shared/color.mjs +0 -596
- package/cli/engine/shared/constants.mjs +0 -127
- package/cli/engine/shared/fonts.mjs +0 -30
- package/cli/engine/shared/inline-ignores.mjs +0 -148
- package/cli/engine/shared/page.mjs +0 -7
- package/cli/lib/download-providers.js +0 -35
- package/cli/lib/impeccable-config.mjs +0 -640
|
@@ -1,288 +0,0 @@
|
|
|
1
|
-
import fs from 'node:fs';
|
|
2
|
-
import path from 'node:path';
|
|
3
|
-
|
|
4
|
-
import { OVERUSED_FONTS, primaryFontFace } from '../../shared/constants.mjs';
|
|
5
|
-
import {
|
|
6
|
-
checkSourceDesignSystem,
|
|
7
|
-
collectStaticDesignSystemFindings,
|
|
8
|
-
mergeDesignSystemFindings,
|
|
9
|
-
} from '../../design-system.mjs';
|
|
10
|
-
import { isFullPage } from '../../shared/page.mjs';
|
|
11
|
-
import { applyInlineIgnores } from '../../shared/inline-ignores.mjs';
|
|
12
|
-
import { finding } from '../../findings.mjs';
|
|
13
|
-
import { profileFindings, profileStep, profileStepAsync } from '../../profile/profiler.mjs';
|
|
14
|
-
import {
|
|
15
|
-
checkElementBorders,
|
|
16
|
-
checkElementClippedOverflow,
|
|
17
|
-
checkElementColors,
|
|
18
|
-
checkElementGlow,
|
|
19
|
-
checkElementGptBorderShadow,
|
|
20
|
-
checkElementHeroEyebrow,
|
|
21
|
-
checkElementHoverContrast,
|
|
22
|
-
checkElementIconTile,
|
|
23
|
-
checkElementItalicSerif,
|
|
24
|
-
checkElementMotion,
|
|
25
|
-
checkElementOversizedH1,
|
|
26
|
-
checkElementQuality,
|
|
27
|
-
checkElementRadialSpotlight,
|
|
28
|
-
checkCreamPalette,
|
|
29
|
-
checkHtmlPatterns,
|
|
30
|
-
checkKickerAboveHeadingFromDoc,
|
|
31
|
-
scopedIgnoreActive,
|
|
32
|
-
checkNumberedSectionLabelsFromDoc,
|
|
33
|
-
checkPageLayout,
|
|
34
|
-
checkPageQualityFromDoc,
|
|
35
|
-
checkRepeatedContainerTextFromDoc,
|
|
36
|
-
resolveBackground,
|
|
37
|
-
resolveBorderRadiusPx,
|
|
38
|
-
} from '../../rules/checks.mjs';
|
|
39
|
-
import { detectText, runTextContentAnalyzers } from '../regex/detect-text.mjs';
|
|
40
|
-
import {
|
|
41
|
-
StaticDocument,
|
|
42
|
-
buildStaticStyleMap,
|
|
43
|
-
buildStaticWindow,
|
|
44
|
-
collectStaticCssText,
|
|
45
|
-
} from './css-cascade.mjs';
|
|
46
|
-
|
|
47
|
-
function checkStaticPageTypography(document, window) {
|
|
48
|
-
const findings = [];
|
|
49
|
-
const fonts = new Set();
|
|
50
|
-
const overusedFound = new Set();
|
|
51
|
-
for (const el of document.querySelectorAll('p, h1, h2, h3, h4, h5, h6, li, td, th, dd, blockquote, figcaption, a, button, label, span, div')) {
|
|
52
|
-
const hasText = el.childNodes.some(n => n.nodeType === 3 && n.textContent.trim().length > 0);
|
|
53
|
-
if (!hasText) continue;
|
|
54
|
-
const primary = primaryFontFace(window.getComputedStyle(el).fontFamily);
|
|
55
|
-
if (!primary) continue;
|
|
56
|
-
fonts.add(primary);
|
|
57
|
-
if (OVERUSED_FONTS.has(primary)) overusedFound.add(primary);
|
|
58
|
-
}
|
|
59
|
-
for (const font of overusedFound) {
|
|
60
|
-
findings.push({ id: 'overused-font', snippet: `Primary font: ${font}` });
|
|
61
|
-
}
|
|
62
|
-
const sizes = new Set();
|
|
63
|
-
for (const el of document.querySelectorAll('h1, h2, h3, h4, h5, h6, p, span, a, li, td, th, label, button, div')) {
|
|
64
|
-
const fontSize = parseFloat(window.getComputedStyle(el).fontSize);
|
|
65
|
-
if (fontSize >= 8 && fontSize < 200) sizes.add(Math.round(fontSize * 10) / 10);
|
|
66
|
-
}
|
|
67
|
-
if (sizes.size >= 3) {
|
|
68
|
-
const sorted = [...sizes].sort((a, b) => a - b);
|
|
69
|
-
const ratio = sorted[sorted.length - 1] / sorted[0];
|
|
70
|
-
if (ratio < 2.0) {
|
|
71
|
-
findings.push({ id: 'flat-type-hierarchy', snippet: `Sizes: ${sorted.map(s => s + 'px').join(', ')} (ratio ${ratio.toFixed(1)}:1)` });
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
return findings;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
function checkElementBrokenImage(el) {
|
|
78
|
-
const src = (el.getAttribute && el.getAttribute('src')) ?? el.attribs?.src;
|
|
79
|
-
// Missing src attribute entirely
|
|
80
|
-
if (src === undefined || src === null) {
|
|
81
|
-
return [{ id: 'broken-image', snippet: '<img> with no src attribute' }];
|
|
82
|
-
}
|
|
83
|
-
const trimmed = String(src).trim();
|
|
84
|
-
// Empty or placeholder-only src values
|
|
85
|
-
if (trimmed === '' || trimmed === '#') {
|
|
86
|
-
return [{ id: 'broken-image', snippet: `<img src="${src}">` }];
|
|
87
|
-
}
|
|
88
|
-
return [];
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
const STATIC_ELEMENT_RULES = [
|
|
92
|
-
{ id: 'border-rules', selector: '*', run: (el, tag, style, window, customPropMap) => checkElementBorders(tag, style, null, resolveBorderRadiusPx(el, style, parseFloat(style.width) || 0, window), el) },
|
|
93
|
-
{ id: 'color-rules', selector: '*', run: (el, tag, style, window, customPropMap) => checkElementColors(el, style, tag, window, customPropMap, false) },
|
|
94
|
-
{ id: 'hover-color-rules', selector: '*', run: (el, tag, style, window) => checkElementHoverContrast(el, style, tag, window) },
|
|
95
|
-
{ id: 'dark-glow', selector: '*', run: (el, tag, style, window, customPropMap) => checkElementGlow(tag, style, resolveBackground(el.parentElement || el, window, customPropMap)) },
|
|
96
|
-
{ id: 'motion-rules', selector: '*', run: (el, tag, style) => checkElementMotion(tag, style) },
|
|
97
|
-
{ id: 'icon-tile-stack', selector: 'h1,h2,h3,h4,h5,h6', run: (el, tag, _style, window) => checkElementIconTile(el, tag, window) },
|
|
98
|
-
{ id: 'italic-serif-display', selector: 'h1,h2', run: (el, tag, style) => checkElementItalicSerif(el, style, tag) },
|
|
99
|
-
{ id: 'hero-eyebrow-chip', selector: 'h1', run: (el, tag, style, window, customPropMap) => checkElementHeroEyebrow(el, style, tag, window, customPropMap) },
|
|
100
|
-
{ id: 'broken-image', selector: 'img', run: (el) => checkElementBrokenImage(el) },
|
|
101
|
-
{ id: 'quality-rules', selector: '*', run: (el, tag, style, window) => checkElementQuality(el, style, tag, window) },
|
|
102
|
-
{ id: 'oversized-h1', selector: 'h1', run: (el, tag, style, window) => checkElementOversizedH1(el, style, tag, window) },
|
|
103
|
-
{ id: 'clipped-overflow-container', selector: '*', run: (el, tag, style, window) => checkElementClippedOverflow(el, style, tag, window) },
|
|
104
|
-
{ id: 'gpt-thin-border-wide-shadow', selector: '*', run: (el, tag, style) => checkElementGptBorderShadow(el, style) },
|
|
105
|
-
{ id: 'radial-spotlight-glow', selector: '*', run: (el, tag, style, window) => checkElementRadialSpotlight(el, style, tag, window) },
|
|
106
|
-
];
|
|
107
|
-
|
|
108
|
-
async function detectHtml(filePath, options = {}) {
|
|
109
|
-
const profile = options?.profile;
|
|
110
|
-
const html = profileStep(profile, {
|
|
111
|
-
engine: 'static-html',
|
|
112
|
-
phase: 'setup',
|
|
113
|
-
ruleId: 'read-html',
|
|
114
|
-
target: filePath,
|
|
115
|
-
}, () => fs.readFileSync(filePath, 'utf-8'));
|
|
116
|
-
|
|
117
|
-
let modules;
|
|
118
|
-
try {
|
|
119
|
-
modules = await profileStepAsync(profile, {
|
|
120
|
-
engine: 'static-html',
|
|
121
|
-
phase: 'setup',
|
|
122
|
-
ruleId: 'import-static-parser',
|
|
123
|
-
target: filePath,
|
|
124
|
-
}, async () => {
|
|
125
|
-
const [htmlparser2, cssSelect, csstree, domutils] = await Promise.all([
|
|
126
|
-
import('htmlparser2'),
|
|
127
|
-
import('css-select'),
|
|
128
|
-
import('css-tree'),
|
|
129
|
-
import('domutils'),
|
|
130
|
-
]);
|
|
131
|
-
return {
|
|
132
|
-
parseDocument: htmlparser2.parseDocument,
|
|
133
|
-
selectAll: cssSelect.selectAll,
|
|
134
|
-
selectOne: cssSelect.selectOne,
|
|
135
|
-
compile: cssSelect.compile,
|
|
136
|
-
csstree,
|
|
137
|
-
domutils,
|
|
138
|
-
};
|
|
139
|
-
});
|
|
140
|
-
} catch (err) {
|
|
141
|
-
if (!globalThis.__impeccableStaticHtmlWarned) {
|
|
142
|
-
globalThis.__impeccableStaticHtmlWarned = true;
|
|
143
|
-
|
|
144
|
-
process.stderr.write(
|
|
145
|
-
'impeccable detect: DEGRADED - HTML parser modules unavailable ' +
|
|
146
|
-
'(htmlparser2, css-select, css-tree, domutils).\n' +
|
|
147
|
-
'Falling back to regex matching. Custom properties, selector matching and computed ' +
|
|
148
|
-
'contrast are NOT evaluated; findings are an undercount, not a clean bill of health.\n'
|
|
149
|
-
);
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
return detectText(html, filePath, options);
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
const resolvedPath = path.resolve(filePath);
|
|
156
|
-
const fileDir = path.dirname(resolvedPath);
|
|
157
|
-
const root = profileStep(profile, {
|
|
158
|
-
engine: 'static-html',
|
|
159
|
-
phase: 'parse-html',
|
|
160
|
-
ruleId: 'parse-document',
|
|
161
|
-
target: filePath,
|
|
162
|
-
}, () => modules.parseDocument(html, { lowerCaseAttributeNames: false, lowerCaseTags: true }));
|
|
163
|
-
|
|
164
|
-
const cssText = collectStaticCssText(root, fileDir, profile, filePath, modules);
|
|
165
|
-
const document = new StaticDocument(root, modules);
|
|
166
|
-
buildStaticStyleMap(root, document, cssText, modules, profile, filePath);
|
|
167
|
-
const window = buildStaticWindow(document);
|
|
168
|
-
|
|
169
|
-
const customPropMap = null;
|
|
170
|
-
|
|
171
|
-
const findings = [];
|
|
172
|
-
const runElementCheck = (ruleId, callback) => profile
|
|
173
|
-
? profileFindings(profile, { engine: 'static-html', phase: 'element', ruleId, target: filePath }, callback)
|
|
174
|
-
: callback();
|
|
175
|
-
|
|
176
|
-
const visitedByRule = new Map();
|
|
177
|
-
for (const rule of STATIC_ELEMENT_RULES) {
|
|
178
|
-
const elements = document.querySelectorAll(rule.selector);
|
|
179
|
-
visitedByRule.set(rule.id, elements.length);
|
|
180
|
-
for (const el of elements) {
|
|
181
|
-
const tag = el.tagName.toLowerCase();
|
|
182
|
-
const style = window.getComputedStyle(el);
|
|
183
|
-
for (const f of runElementCheck(rule.id, () => rule.run(el, tag, style, window, customPropMap))) {
|
|
184
|
-
// Element-scoped waivers: a data-impeccable-ignore ancestor suppresses
|
|
185
|
-
// matching findings for its subtree, same as the browser walk.
|
|
186
|
-
if (scopedIgnoreActive(el, f.id)) continue;
|
|
187
|
-
findings.push(finding(f.id, filePath, f.snippet));
|
|
188
|
-
}
|
|
189
|
-
}
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
if (options?.designSystem) {
|
|
193
|
-
const sourceDesignFindings = profileFindings(profile, {
|
|
194
|
-
engine: 'static-html',
|
|
195
|
-
phase: 'source',
|
|
196
|
-
ruleId: 'design-system',
|
|
197
|
-
target: filePath,
|
|
198
|
-
}, () => checkSourceDesignSystem(html, filePath, { designSystem: options.designSystem }));
|
|
199
|
-
const staticDesignFindings = profileFindings(profile, {
|
|
200
|
-
engine: 'static-html',
|
|
201
|
-
phase: 'page',
|
|
202
|
-
ruleId: 'design-system',
|
|
203
|
-
target: filePath,
|
|
204
|
-
}, () => collectStaticDesignSystemFindings(document, window, filePath, options.designSystem));
|
|
205
|
-
findings.push(...mergeDesignSystemFindings(staticDesignFindings, sourceDesignFindings));
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
if (isFullPage(html)) {
|
|
209
|
-
const runPageCheck = (ruleId, callback) => profile
|
|
210
|
-
? profileFindings(profile, { engine: 'static-html', phase: 'page', ruleId, target: filePath }, callback)
|
|
211
|
-
: callback();
|
|
212
|
-
for (const f of runPageCheck('typography-rules', () => checkStaticPageTypography(document, window))) {
|
|
213
|
-
findings.push(finding(f.id, filePath, f.snippet));
|
|
214
|
-
}
|
|
215
|
-
for (const f of runPageCheck('kicker-above-heading', () => checkKickerAboveHeadingFromDoc(document, window))) {
|
|
216
|
-
findings.push(finding(f.id, filePath, f.snippet));
|
|
217
|
-
}
|
|
218
|
-
for (const f of runPageCheck('numbered-section-labels', () => checkNumberedSectionLabelsFromDoc(document, window))) {
|
|
219
|
-
findings.push(finding(f.id, filePath, f.snippet));
|
|
220
|
-
}
|
|
221
|
-
for (const f of runPageCheck('repeated-container-text', () => checkRepeatedContainerTextFromDoc(document, window))) {
|
|
222
|
-
findings.push(finding(f.id, filePath, f.snippet));
|
|
223
|
-
}
|
|
224
|
-
for (const f of runPageCheck('layout-rules', () => checkPageLayout(document, window))) {
|
|
225
|
-
findings.push(finding(f.id, filePath, f.snippet));
|
|
226
|
-
}
|
|
227
|
-
for (const f of runPageCheck('cream-palette', () => checkCreamPalette(document, window))) {
|
|
228
|
-
findings.push(finding(f.id, filePath, f.snippet));
|
|
229
|
-
}
|
|
230
|
-
for (const f of runPageCheck('skipped-heading', () => checkPageQualityFromDoc(document))) {
|
|
231
|
-
findings.push(finding(f.id, filePath, f.snippet));
|
|
232
|
-
}
|
|
233
|
-
// Scoped corpora for the pattern checks (see buildHtmlPatternCorpora in
|
|
234
|
-
// rules/checks.mjs): CSS-property regexes must not fire on prose ABOUT
|
|
235
|
-
// css — `<code>background-clip: text</code>` in a changelog is
|
|
236
|
-
// documentation, not styling. cssText already carries the <style>
|
|
237
|
-
// blocks and any linked local stylesheets; style/class attributes come
|
|
238
|
-
// from the parsed document, so escaped code samples never contribute.
|
|
239
|
-
const styleAttrParts = [];
|
|
240
|
-
const classAttrParts = [];
|
|
241
|
-
for (const el of document.querySelectorAll('*')) {
|
|
242
|
-
const styleAttr = el.getAttribute('style');
|
|
243
|
-
if (styleAttr) styleAttrParts.push(`style="${styleAttr}"`);
|
|
244
|
-
const classAttr = el.getAttribute('class');
|
|
245
|
-
if (classAttr) classAttrParts.push(classAttr);
|
|
246
|
-
}
|
|
247
|
-
const patternCorpora = {
|
|
248
|
-
styleText: [cssText, ...styleAttrParts].join('\n'),
|
|
249
|
-
classText: classAttrParts.join('\n'),
|
|
250
|
-
};
|
|
251
|
-
for (const f of runPageCheck('html-patterns', () => checkHtmlPatterns(html, patternCorpora).filter(item =>
|
|
252
|
-
item.id !== 'bounce-easing' && item.id !== 'layout-transition'
|
|
253
|
-
))) {
|
|
254
|
-
// Selector-backed page findings honor scoped waivers here too, matching
|
|
255
|
-
// the browser pass: resolve the selector and drop the finding when an
|
|
256
|
-
// ignoring ancestor covers a match. Unlike the browser, an unmatched
|
|
257
|
-
// selector keeps the finding — static scans see partial documents.
|
|
258
|
-
if (f.selector) {
|
|
259
|
-
let matches = null;
|
|
260
|
-
try {
|
|
261
|
-
matches = document.querySelectorAll(String(f.selector).replace(/::?[a-zA-Z-]+(\([^)]*\))?/g, '').trim());
|
|
262
|
-
} catch { matches = null; }
|
|
263
|
-
if (matches && matches.length > 0 && [...matches].every(el => scopedIgnoreActive(el, f.id))) continue;
|
|
264
|
-
}
|
|
265
|
-
const item = finding(f.id, filePath, f.snippet);
|
|
266
|
-
// Position-aware severity promotion: checks may attach a per-finding
|
|
267
|
-
// severity (e.g. a pulsing dot inside a header/nav landmark) that
|
|
268
|
-
// overrides the registry default.
|
|
269
|
-
if (f.severity) item.severity = f.severity;
|
|
270
|
-
findings.push(item);
|
|
271
|
-
}
|
|
272
|
-
// Text-content analyzers (em-dash overuse, marketing buzzwords,
|
|
273
|
-
// numbered section markers, aphoristic cadence) live in the regex
|
|
274
|
-
// engine. Call them from here so .html files get the same coverage
|
|
275
|
-
// as .css/.tsx files. These are scoped to text content only and
|
|
276
|
-
// don't overlap with static-html's element/page rules.
|
|
277
|
-
for (const f of runPageCheck('text-content', () => runTextContentAnalyzers(html, filePath, options))) {
|
|
278
|
-
findings.push(finding(f.antipattern, filePath, f.snippet));
|
|
279
|
-
}
|
|
280
|
-
}
|
|
281
|
-
|
|
282
|
-
// Static-HTML findings carry no line number, so only whole-file
|
|
283
|
-
// `impeccable-disable` directives apply here — exactly the standalone-document
|
|
284
|
-
// waiver this primitive targets. Bypassed by `--no-config` / `--no-inline-ignores`.
|
|
285
|
-
return options?.inlineIgnores === false ? findings : applyInlineIgnores(findings, html);
|
|
286
|
-
}
|
|
287
|
-
|
|
288
|
-
export { checkStaticPageTypography, STATIC_ELEMENT_RULES, detectHtml };
|
|
@@ -1,189 +0,0 @@
|
|
|
1
|
-
function sanitizeScreenshotClip(clip, viewport) {
|
|
2
|
-
if (!clip) return null;
|
|
3
|
-
const x = Math.max(0, Math.floor(clip.x || 0));
|
|
4
|
-
const y = Math.max(0, Math.floor(clip.y || 0));
|
|
5
|
-
const width = Math.min(
|
|
6
|
-
Math.max(1, Math.ceil(clip.width || 0)),
|
|
7
|
-
Math.max(1, viewport?.width || 1600),
|
|
8
|
-
);
|
|
9
|
-
const height = Math.min(
|
|
10
|
-
Math.max(1, Math.ceil(clip.height || 0)),
|
|
11
|
-
320,
|
|
12
|
-
);
|
|
13
|
-
if (width < 1 || height < 1) return null;
|
|
14
|
-
return { x, y, width, height };
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
async function compareScreenshotContrast(page, beforeBase64, afterBase64, candidate) {
|
|
18
|
-
return page.evaluate(async ({ beforeBase64, afterBase64, candidate }) => {
|
|
19
|
-
const loadImage = (base64) => new Promise((resolve, reject) => {
|
|
20
|
-
const img = new Image();
|
|
21
|
-
img.onload = () => resolve(img);
|
|
22
|
-
img.onerror = () => reject(new Error('Could not decode contrast screenshot'));
|
|
23
|
-
img.src = `data:image/png;base64,${base64}`;
|
|
24
|
-
});
|
|
25
|
-
const [before, after] = await Promise.all([loadImage(beforeBase64), loadImage(afterBase64)]);
|
|
26
|
-
const width = Math.min(before.width, after.width);
|
|
27
|
-
const height = Math.min(before.height, after.height);
|
|
28
|
-
if (width < 1 || height < 1) return null;
|
|
29
|
-
|
|
30
|
-
const canvas = document.createElement('canvas');
|
|
31
|
-
canvas.width = width;
|
|
32
|
-
canvas.height = height;
|
|
33
|
-
const ctx = canvas.getContext('2d', { willReadFrequently: true });
|
|
34
|
-
if (!ctx) return null;
|
|
35
|
-
|
|
36
|
-
ctx.drawImage(before, 0, 0, width, height);
|
|
37
|
-
const beforePixels = ctx.getImageData(0, 0, width, height).data;
|
|
38
|
-
ctx.clearRect(0, 0, width, height);
|
|
39
|
-
ctx.drawImage(after, 0, 0, width, height);
|
|
40
|
-
const afterPixels = ctx.getImageData(0, 0, width, height).data;
|
|
41
|
-
|
|
42
|
-
const luminance = ({ r, g, b }) => {
|
|
43
|
-
const convert = c => {
|
|
44
|
-
const v = c / 255;
|
|
45
|
-
return v <= 0.03928 ? v / 12.92 : ((v + 0.055) / 1.055) ** 2.4;
|
|
46
|
-
};
|
|
47
|
-
return 0.2126 * convert(r) + 0.7152 * convert(g) + 0.0722 * convert(b);
|
|
48
|
-
};
|
|
49
|
-
const ratio = (a, b) => {
|
|
50
|
-
const l1 = luminance(a);
|
|
51
|
-
const l2 = luminance(b);
|
|
52
|
-
return (Math.max(l1, l2) + 0.05) / (Math.min(l1, l2) + 0.05);
|
|
53
|
-
};
|
|
54
|
-
|
|
55
|
-
const cssTextColor = candidate.textColor && !candidate.preferRenderedForeground
|
|
56
|
-
? {
|
|
57
|
-
r: candidate.textColor.r,
|
|
58
|
-
g: candidate.textColor.g,
|
|
59
|
-
b: candidate.textColor.b,
|
|
60
|
-
}
|
|
61
|
-
: null;
|
|
62
|
-
const ratios = [];
|
|
63
|
-
let glyphPixels = 0;
|
|
64
|
-
let strongestDelta = 0;
|
|
65
|
-
for (let i = 0; i < beforePixels.length; i += 4) {
|
|
66
|
-
const delta = Math.abs(beforePixels[i] - afterPixels[i])
|
|
67
|
-
+ Math.abs(beforePixels[i + 1] - afterPixels[i + 1])
|
|
68
|
-
+ Math.abs(beforePixels[i + 2] - afterPixels[i + 2])
|
|
69
|
-
+ Math.abs(beforePixels[i + 3] - afterPixels[i + 3]);
|
|
70
|
-
strongestDelta = Math.max(strongestDelta, delta);
|
|
71
|
-
if (delta < 10) continue;
|
|
72
|
-
glyphPixels++;
|
|
73
|
-
const fg = cssTextColor || {
|
|
74
|
-
r: beforePixels[i],
|
|
75
|
-
g: beforePixels[i + 1],
|
|
76
|
-
b: beforePixels[i + 2],
|
|
77
|
-
};
|
|
78
|
-
const bg = {
|
|
79
|
-
r: afterPixels[i],
|
|
80
|
-
g: afterPixels[i + 1],
|
|
81
|
-
b: afterPixels[i + 2],
|
|
82
|
-
};
|
|
83
|
-
ratios.push(ratio(fg, bg));
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
if (ratios.length < 8) {
|
|
87
|
-
return {
|
|
88
|
-
glyphPixels,
|
|
89
|
-
strongestDelta,
|
|
90
|
-
worstRatio: null,
|
|
91
|
-
p10Ratio: null,
|
|
92
|
-
medianRatio: null,
|
|
93
|
-
};
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
ratios.sort((a, b) => a - b);
|
|
97
|
-
const pick = pct => ratios[Math.min(ratios.length - 1, Math.max(0, Math.floor((pct / 100) * ratios.length)))];
|
|
98
|
-
return {
|
|
99
|
-
glyphPixels,
|
|
100
|
-
strongestDelta,
|
|
101
|
-
worstRatio: ratios[0],
|
|
102
|
-
p10Ratio: pick(10),
|
|
103
|
-
medianRatio: pick(50),
|
|
104
|
-
};
|
|
105
|
-
}, { beforeBase64, afterBase64, candidate });
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
async function captureVisualContrastCandidate(page, candidate, viewport) {
|
|
109
|
-
const clip = sanitizeScreenshotClip(candidate.clip, viewport);
|
|
110
|
-
if (!clip) return null;
|
|
111
|
-
|
|
112
|
-
const beforeBase64 = await page.screenshot({
|
|
113
|
-
encoding: 'base64',
|
|
114
|
-
clip,
|
|
115
|
-
captureBeyondViewport: true,
|
|
116
|
-
});
|
|
117
|
-
const token = `impeccable-contrast-${Date.now()}-${Math.random().toString(36).slice(2)}`;
|
|
118
|
-
const applied = await page.evaluate(({ selector, token, backgroundClipText }) => {
|
|
119
|
-
let el;
|
|
120
|
-
try {
|
|
121
|
-
el = document.querySelector(selector);
|
|
122
|
-
} catch {
|
|
123
|
-
return false;
|
|
124
|
-
}
|
|
125
|
-
if (!el) return false;
|
|
126
|
-
let style = document.getElementById('impeccable-visual-contrast-hide-style');
|
|
127
|
-
if (!style) {
|
|
128
|
-
style = document.createElement('style');
|
|
129
|
-
style.id = 'impeccable-visual-contrast-hide-style';
|
|
130
|
-
style.textContent = [
|
|
131
|
-
'[data-impeccable-visual-contrast-target] {',
|
|
132
|
-
' color: transparent !important;',
|
|
133
|
-
' -webkit-text-fill-color: transparent !important;',
|
|
134
|
-
' text-shadow: none !important;',
|
|
135
|
-
'}',
|
|
136
|
-
'[data-impeccable-visual-contrast-target][data-impeccable-bgclip-text="true"] {',
|
|
137
|
-
' background-image: none !important;',
|
|
138
|
-
'}',
|
|
139
|
-
].join('\n');
|
|
140
|
-
document.head.appendChild(style);
|
|
141
|
-
}
|
|
142
|
-
el.setAttribute('data-impeccable-visual-contrast-target', token);
|
|
143
|
-
if (backgroundClipText) el.setAttribute('data-impeccable-bgclip-text', 'true');
|
|
144
|
-
return true;
|
|
145
|
-
}, {
|
|
146
|
-
selector: candidate.selector,
|
|
147
|
-
token,
|
|
148
|
-
backgroundClipText: candidate.backgroundClipText,
|
|
149
|
-
});
|
|
150
|
-
if (!applied) return null;
|
|
151
|
-
|
|
152
|
-
let afterBase64;
|
|
153
|
-
try {
|
|
154
|
-
afterBase64 = await page.screenshot({
|
|
155
|
-
encoding: 'base64',
|
|
156
|
-
clip,
|
|
157
|
-
captureBeyondViewport: true,
|
|
158
|
-
});
|
|
159
|
-
} finally {
|
|
160
|
-
await page.evaluate(({ selector }) => {
|
|
161
|
-
try {
|
|
162
|
-
const el = document.querySelector(selector);
|
|
163
|
-
if (el) {
|
|
164
|
-
el.removeAttribute('data-impeccable-visual-contrast-target');
|
|
165
|
-
el.removeAttribute('data-impeccable-bgclip-text');
|
|
166
|
-
}
|
|
167
|
-
} catch {
|
|
168
|
-
// Ignore invalid or stale selectors during cleanup.
|
|
169
|
-
}
|
|
170
|
-
}, { selector: candidate.selector }).catch(() => {});
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
const metrics = await compareScreenshotContrast(page, beforeBase64, afterBase64, candidate);
|
|
174
|
-
if (!metrics || !Number.isFinite(metrics.p10Ratio) || metrics.glyphPixels < 8) return null;
|
|
175
|
-
const measuredRatio = metrics.p10Ratio;
|
|
176
|
-
if (measuredRatio >= candidate.threshold) return null;
|
|
177
|
-
const textLabel = candidate.text ? ` "${candidate.text}"` : '';
|
|
178
|
-
const reasonLabel = (candidate.reasons || []).slice(0, 3).join(', ') || 'visual background';
|
|
179
|
-
return {
|
|
180
|
-
id: 'low-contrast',
|
|
181
|
-
snippet: `pixel contrast ${measuredRatio.toFixed(1)}:1 median ${metrics.medianRatio.toFixed(1)}:1 (need ${candidate.threshold}:1) on ${reasonLabel}${textLabel}`,
|
|
182
|
-
};
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
export {
|
|
186
|
-
sanitizeScreenshotClip,
|
|
187
|
-
compareScreenshotContrast,
|
|
188
|
-
captureVisualContrastCandidate,
|
|
189
|
-
};
|
package/cli/engine/findings.mjs
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import { getAntipattern } from './registry/antipatterns.mjs';
|
|
2
|
-
|
|
3
|
-
function getAP(id) {
|
|
4
|
-
return getAntipattern(id);
|
|
5
|
-
}
|
|
6
|
-
|
|
7
|
-
function finding(id, filePath, snippet, line = 0) {
|
|
8
|
-
const ap = getAP(id);
|
|
9
|
-
const base = { antipattern: id, name: ap.name, description: ap.description, severity: ap.severity || 'warning', category: ap.category || null, file: filePath, line, snippet };
|
|
10
|
-
// Advisory findings are detected but reported separately and never counted as
|
|
11
|
-
// failures. Carry the flag on the finding so every consumer (CLI, JSON, hook)
|
|
12
|
-
// can partition without a registry lookup. Only stamped when true to keep the
|
|
13
|
-
// finding shape stable for the vast majority of rules.
|
|
14
|
-
if (ap.advisory === true) base.advisory = true;
|
|
15
|
-
return base;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
export { getAP, finding };
|