Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
One-time setup that gathers your project's design context and saves it to CLAUDE.md for future sessions.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
scripts/detector/rules/checks.mjs
1import {2BORDER_SAFE_TAGS,3GENERIC_FONTS,4KNOWN_SERIF_FONTS,5OVERUSED_FONTS,6SAFE_TAGS,7WCAG_LARGE_BOLD_TEXT_PX,8WCAG_LARGE_TEXT_PX,9isBrandFontOnOwnDomain,10} from '../shared/constants.mjs';11import {12colorToHex,13contrastRatio,14getHue,15hasChroma,16isNeutralColor,17parseGradientColors,18parseRgb,19relativeLuminance,20} from '../shared/color.mjs';2122const DETECTOR_IS_BROWSER = typeof window !== 'undefined';2324// ─── Section 3: Pure Detection ──────────────────────────────────────────────2526function checkBorders(tag, widths, colors, radius) {27if (BORDER_SAFE_TAGS.has(tag)) return [];28const findings = [];29const sides = ['Top', 'Right', 'Bottom', 'Left'];3031for (const side of sides) {32const w = widths[side];33if (w < 1 || isNeutralColor(colors[side])) continue;3435const otherSides = sides.filter(s => s !== side);36const maxOther = Math.max(...otherSides.map(s => widths[s]));37if (!(w >= 2 && (maxOther <= 1 || w >= maxOther * 2))) continue;3839const sn = side.toLowerCase();40const isSide = side === 'Left' || side === 'Right';4142if (isSide) {43if (radius > 0) findings.push({ id: 'side-tab', snippet: `border-${sn}: ${w}px + border-radius: ${radius}px` });44else if (w >= 3) findings.push({ id: 'side-tab', snippet: `border-${sn}: ${w}px` });45} else {46if (radius > 0 && w >= 2) findings.push({ id: 'border-accent-on-rounded', snippet: `border-${sn}: ${w}px + border-radius: ${radius}px` });47}48}4950return findings;51}5253// Returns true if the given text is composed entirely of emoji characters54// (plus whitespace / variation selectors). Emojis render as multicolor glyphs55// regardless of CSS `color`, so contrast checks against the element's text56// color are meaningless for these nodes.57const EMOJI_CHAR_RE = /[\u{1F1E6}-\u{1F1FF}\u{1F300}-\u{1F9FF}\u{1FA00}-\u{1FAFF}\u{2600}-\u{27BF}\u{2300}-\u{23FF}\u{FE0F}\u{200D}\u{1F3FB}-\u{1F3FF}]/u;58const EMOJI_CHARS_GLOBAL = /[\u{1F1E6}-\u{1F1FF}\u{1F300}-\u{1F9FF}\u{1FA00}-\u{1FAFF}\u{2600}-\u{27BF}\u{2300}-\u{23FF}\u{FE0F}\u{200D}\u{1F3FB}-\u{1F3FF}]/gu;59function isEmojiOnlyText(text) {60if (!text) return false;61if (!EMOJI_CHAR_RE.test(text)) return false;62return text.replace(EMOJI_CHARS_GLOBAL, '').trim() === '';63}6465function checkColors(opts) {66const { tag, textColor, bgColor, effectiveBg, effectiveBgStops, fontSize, fontWeight, hasDirectText, isEmojiOnly, bgClip, bgImage, classList } = opts;67if (SAFE_TAGS.has(tag)) {68// Exception for <a> and <button> elements styled as buttons. SAFE_TAGS69// exists to suppress contrast noise on inline links and unstyled controls,70// where the element has no own background and the contrast against the71// ancestor surface is already the intended visual. When the element has72// its own opaque background and direct text, it is a styled button — and73// contrast on its own surface is a real, frequent bug worth flagging.74const isStyledButton = (tag === 'a' || tag === 'button')75&& hasDirectText76&& bgColor && bgColor.a > 0.5;77if (!isStyledButton) return [];78}79const findings = [];8081if (hasDirectText && textColor && !isEmojiOnly) {82// Run background-dependent checks against either a solid bg or, if the83// ancestor is a gradient, against every gradient stop (use the worst case).84const bgs = effectiveBg ? [effectiveBg] : (effectiveBgStops && effectiveBgStops.length ? effectiveBgStops : null);85if (bgs) {86// Gray on colored background — flag if every stop is chromatic87const textLum = relativeLuminance(textColor);88const isGray = !hasChroma(textColor, 20) && textLum > 0.05 && textLum < 0.85;89if (isGray && bgs.every(b => hasChroma(b, 40))) {90const bgLabel = effectiveBg ? colorToHex(effectiveBg) : `gradient(${bgs.map(colorToHex).join(', ')})`;91findings.push({ id: 'gray-on-color', snippet: `text ${colorToHex(textColor)} on bg ${bgLabel}` });92}9394// Low contrast (WCAG AA) — worst case across all bg stops95const ratios = bgs.map(b => contrastRatio(textColor, b));96let worstIdx = 0;97for (let i = 1; i < ratios.length; i++) if (ratios[i] < ratios[worstIdx]) worstIdx = i;98const ratio = ratios[worstIdx];99const isLargeText = fontSize >= WCAG_LARGE_TEXT_PX || (fontSize >= WCAG_LARGE_BOLD_TEXT_PX && fontWeight >= 700);100const threshold = isLargeText ? 3.0 : 4.5;101if (ratio < threshold) {102// Skip the false-positive class where text has alpha < 1 AND we103// couldn't find an opaque ancestor (effectiveBg is null, we're104// comparing against gradient-stop fallback). In jsdom mode the105// detector can't resolve `var(--X)` color tokens, so a dark106// section sitting between the text and the body's decorative107// gradient is invisible to us — we end up measuring contrast108// against the body's paper-grain noise instead of the real109// local bg. Real low-contrast bugs use alpha=1 and have a110// resolvable opaque ancestor; semi-transparent Tailwind tokens111// like `text-paper/60` on `bg-ink` sections are the FP pattern.112const isAlphaFallbackFP = !DETECTOR_IS_BROWSER && !effectiveBg && (textColor.a != null && textColor.a < 1);113if (!isAlphaFallbackFP) {114findings.push({ id: 'low-contrast', snippet: `${ratio.toFixed(1)}:1 (need ${threshold}:1) — text ${colorToHex(textColor)} on ${colorToHex(bgs[worstIdx])}` });115}116}117}118119// AI palette: purple/violet on headings120if (hasChroma(textColor, 50)) {121const hue = getHue(textColor);122if (hue >= 260 && hue <= 310 && (['h1', 'h2', 'h3'].includes(tag) || fontSize >= 20)) {123findings.push({ id: 'ai-color-palette', snippet: `Purple/violet text (${colorToHex(textColor)}) on heading` });124}125}126}127128// Gradient text129if (bgClip === 'text' && bgImage && bgImage.includes('gradient')) {130findings.push({ id: 'gradient-text', snippet: 'background-clip: text + gradient' });131}132133// Tailwind class checks134if (classList) {135const classStr = typeof classList === 'string' ? classList : Array.from(classList).join(' ');136137const grayMatch = classStr.match(/\btext-(?:gray|slate|zinc|neutral|stone)-\d+\b/);138const colorBgMatch = classStr.match(/\bbg-(?:red|orange|amber|yellow|lime|green|emerald|teal|cyan|sky|blue|indigo|violet|purple|fuchsia|pink|rose)-\d+\b/);139if (grayMatch && colorBgMatch) {140findings.push({ id: 'gray-on-color', snippet: `${grayMatch[0]} on ${colorBgMatch[0]}` });141}142143if (/\bbg-clip-text\b/.test(classStr) && /\bbg-gradient-to-/.test(classStr)) {144findings.push({ id: 'gradient-text', snippet: 'bg-clip-text + bg-gradient (Tailwind)' });145}146147const purpleText = classStr.match(/\btext-(?:purple|violet|indigo)-\d+\b/);148if (purpleText && (['h1', 'h2', 'h3'].includes(tag) || /\btext-(?:[2-9]xl)\b/.test(classStr))) {149findings.push({ id: 'ai-color-palette', snippet: `${purpleText[0]} on heading` });150}151152if (/\bfrom-(?:purple|violet|indigo)-\d+\b/.test(classStr) && /\bto-(?:purple|violet|indigo|blue|cyan|pink|fuchsia)-\d+\b/.test(classStr)) {153findings.push({ id: 'ai-color-palette', snippet: 'Purple/violet gradient (Tailwind)' });154}155}156157return findings;158}159160function isCardLikeFromProps(hasShadow, hasBorder, hasRadius, hasBg) {161if (!hasShadow && !hasBorder) return false;162return hasRadius || hasBg;163}164165const HEADING_TAGS = new Set(['h1', 'h2', 'h3', 'h4', 'h5', 'h6']);166167// Pure check: given a heading and metrics about its previousElementSibling,168// decide if the sibling is the canonical "icon-tile-stacked-above-heading" shape.169//170// Triggers when ALL of the following hold for the sibling:171// • size 32–128px on both axes (not too small, not a hero image)172// • aspect ratio 0.7–1.4 (squarish — excludes wide thumbnails / pill badges)173// • has a non-transparent background-color, background-image, OR a visible border174// (covers solid colors, white-with-border, gradients — anything that visually175// defines a tile)176// • border-radius < width/2 (excludes round avatars; rounded squares pass)177// • contains an <svg> or icon-class <i> element that's smaller than the tile178// • the tile sits above the heading (its bottom is above the heading's top)179function checkIconTile(opts) {180const { headingTag, headingText, headingTop,181siblingTag, siblingWidth, siblingHeight, siblingBottom,182siblingBgColor, siblingBgImage, siblingBorderWidth, siblingBorderRadius,183hasIconChild, iconChildWidth } = opts;184if (!HEADING_TAGS.has(headingTag)) return [];185if (!siblingTag) return [];186// Don't recurse into nested headings (e.g. h2 above h3 in a section header)187if (HEADING_TAGS.has(siblingTag)) return [];188189// Size window: 32–128px on each axis190if (!(siblingWidth >= 32 && siblingWidth <= 128)) return [];191if (!(siblingHeight >= 32 && siblingHeight <= 128)) return [];192193// Squarish aspect ratio194const ratio = siblingWidth / siblingHeight;195if (ratio < 0.7 || ratio > 1.4) return [];196197// Must have something that visually defines the tile198const bgVisible = (siblingBgColor && siblingBgColor.a > 0.1)199|| (siblingBgImage && siblingBgImage !== 'none' && siblingBgImage !== '');200const borderVisible = siblingBorderWidth > 0;201if (!bgVisible && !borderVisible) return [];202203// Exclude circles (avatars). Rounded squares pass.204if (siblingBorderRadius >= siblingWidth / 2) return [];205206// Must contain an icon element smaller than the tile207if (!hasIconChild) return [];208if (iconChildWidth && iconChildWidth >= siblingWidth * 0.95) return [];209210// Vertical stacking: tile must end above where the heading starts.211// (Allow the check to skip when both top/bottom are 0 — jsdom layout case.)212if (headingTop && siblingBottom && siblingBottom > headingTop + 4) return [];213214const text = (headingText || '').trim().slice(0, 60);215return [{216id: 'icon-tile-stack',217snippet: `${Math.round(siblingWidth)}x${Math.round(siblingHeight)}px icon tile above ${headingTag} "${text}"`,218}];219}220221// Resolve the primary (non-generic) face from a font-family string and return222// whether the resolved primary is serif. Two paths:223// 1. Primary face is in KNOWN_SERIF_FONTS → serif.224// 2. Primary face is unknown but the stack ends in the generic `serif`225// token → treat as serif. Authors who declare `font-family: 'X', serif`226// almost always have a serif primary; a sans declared with a serif227// fallback is a code smell, not the common case.228// Returns { primary, isSerif } so the snippet can name the face.229function resolveSerif(fontFamily) {230if (!fontFamily) return { primary: null, isSerif: false };231const tokens = fontFamily.split(',').map(f => f.trim().replace(/^['"]|['"]$/g, '').toLowerCase());232const primary = tokens.find(f => f && !GENERIC_FONTS.has(f)) || null;233if (!primary) return { primary: null, isSerif: false };234if (KNOWN_SERIF_FONTS.has(primary)) return { primary, isSerif: true };235if (tokens.includes('serif')) return { primary, isSerif: true };236return { primary, isSerif: false };237}238239function checkItalicSerif(opts) {240const { tag, fontStyle, fontFamily, fontSize, headingText } = opts;241if (fontStyle !== 'italic') return [];242// Anchor the rule on hero-scale text. h1 is the canonical hero element;243// h2 ≥ 48px catches the cases where the design demotes the visual hero244// to an h2 but keeps the size.245if (tag !== 'h1' && !(tag === 'h2' && fontSize >= 48)) return [];246if (fontSize < 48) return [];247const { primary, isSerif } = resolveSerif(fontFamily);248if (!isSerif) return [];249250const text = (headingText || '').trim().slice(0, 60);251return [{252id: 'italic-serif-display',253snippet: `italic serif ${tag} (${primary || 'serif'}) at ${Math.round(fontSize)}px "${text}"`,254}];255}256257// Color saturation check. Returns true when the color has visible258// chroma — i.e., it's an "accent color" rather than near-neutral.259// Handles rgb()/rgba(), #hex, oklch(), and hsl(). var() refs are260// expected to be pre-resolved by the caller.261function isAccentColor(cssColor) {262if (!cssColor) return false;263const s = String(cssColor).trim();264// rgb / rgba — direct channel-distance check.265const rgbM = /rgba?\(\s*(\d+)\s*,?\s+|\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)/.exec(s.replace(/rgba?\(\s*/, 'rgb(').replace(/,/g, ', '));266const rgbStrict = /rgba?\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)/.exec(s);267if (rgbStrict) {268const r = +rgbStrict[1], g = +rgbStrict[2], b = +rgbStrict[3];269return (Math.max(r, g, b) - Math.min(r, g, b)) >= 40;270}271// #hex — 3, 4, 6, or 8 digit.272const hexM = /^#([0-9a-f]{3,8})\b/i.exec(s);273if (hexM) {274let h = hexM[1];275if (h.length === 3 || h.length === 4) h = h.split('').map((c) => c + c).join('').slice(0, 6);276else h = h.slice(0, 6);277if (h.length === 6) {278const r = parseInt(h.slice(0, 2), 16);279const g = parseInt(h.slice(2, 4), 16);280const b = parseInt(h.slice(4, 6), 16);281return (Math.max(r, g, b) - Math.min(r, g, b)) >= 40;282}283}284// oklch(L C H) — chroma C is what matters. Typical neutral grays285// have C < 0.02; visible accents are 0.05+. CSS minification can286// collapse spaces between L% and C ("oklch(43%.15 34)"), so we287// extract all numbers and take the second rather than matching a288// strict L-then-whitespace-then-C pattern.289if (/^oklch\(/i.test(s)) {290const nums = s.match(/\d*\.\d+|\d+/g);291if (nums && nums.length >= 2) {292const c = parseFloat(nums[1]);293return !Number.isNaN(c) && c >= 0.05;294}295}296// hsl(H, S%, L%) — saturation > 20% reads as accent.297const hslM = /hsla?\(\s*[\d.]+\s*,\s*([\d.]+)%/i.exec(s);298if (hslM) {299const sat = parseFloat(hslM[1]);300return !Number.isNaN(sat) && sat >= 20;301}302return false;303}304305// Sibling-relationship rule. Anchor on a hero-scale h1, look at the306// previousElementSibling, and gate on EITHER the classic tracked-307// uppercase eyebrow OR the modern accent-colored bold eyebrow.308function checkHeroEyebrow(opts) {309const {310headingTag, headingText, headingFontSize,311siblingTag, siblingText, siblingTextTransform,312siblingFontSize, siblingLetterSpacing,313siblingFontWeight, siblingColor,314} = opts;315if (headingTag !== 'h1') return [];316// We previously gated on headingFontSize >= 48 to anchor "hero scale".317// But modern hero h1s use clamp() / vw / var(--text-*), none of which318// jsdom can resolve — the computed value comes back as "2em" or319// "var(--text-9xl)" and parseFloat returns 2 or NaN. The gate fails320// on virtually every Tailwind v4 / framework build. The other gates321// (sibling text 2-60 chars, font-size ≤ 14px, accent-bold OR322// tracked-caps) are tight enough to avoid false positives on non-323// hero h1s — a tiny tan label directly above any h1 is the324// antipattern regardless of how big the h1 ends up.325if (!siblingTag) return [];326// An h2 above an h1 is a different anti-pattern (heading hierarchy / dual327// headings) — never an eyebrow.328if (HEADING_TAGS.has(siblingTag)) return [];329330const text = (siblingText || '').trim();331if (text.length < 2 || text.length > 60) return [];332if (!(siblingFontSize > 0 && siblingFontSize <= 14)) return [];333334// Branch A: classic tracked-uppercase eyebrow.335const isUppercased = siblingTextTransform === 'uppercase'336|| (/[A-Z]/.test(text) && !/[a-z]/.test(text));337const isClassicTracked = isUppercased && siblingLetterSpacing >= 1.6;338339// Branch B: modern accent-bold eyebrow — sentence case, low340// tracking, but bold + accent-colored. The style choices changed;341// the pattern is the same kicker-above-headline anti-pattern.342const weight = Number(siblingFontWeight) || 400;343const isAccentBold = weight >= 700 && isAccentColor(siblingColor || '');344345if (!isClassicTracked && !isAccentBold) return [];346347const headingTextSnippet = (headingText || '').trim().slice(0, 60);348const eyebrowSnippet = text.slice(0, 40);349const style = isClassicTracked ? 'tracked-caps' : 'accent-bold';350return [{351id: 'hero-eyebrow-chip',352snippet: `eyebrow chip (${style}) "${eyebrowSnippet}" above ${headingTag} "${headingTextSnippet}"`,353}];354}355356function checkRepeatedSectionKickers(opts) {357const { candidates, minCount = 3 } = opts;358if (!Array.isArray(candidates) || candidates.length < minCount) return [];359return candidates.map(candidate => ({360id: 'repeated-section-kickers',361snippet: `repeated section kicker "${candidate.kickerText}" before ${candidate.headingTag} "${candidate.headingText}" (${candidates.length} on page)`,362}));363}364365const LAYOUT_TRANSITION_PROPS = new Set([366'width', 'height', 'padding', 'margin',367'max-height', 'max-width', 'min-height', 'min-width',368'padding-top', 'padding-right', 'padding-bottom', 'padding-left',369'margin-top', 'margin-right', 'margin-bottom', 'margin-left',370]);371372function checkMotion(opts) {373const { tag, transitionProperty, animationName, timingFunctions, classList } = opts;374if (SAFE_TAGS.has(tag)) return [];375const findings = [];376377// --- Bounce/elastic easing ---378if (animationName && animationName !== 'none' && /bounce|elastic|wobble|jiggle|spring/i.test(animationName)) {379findings.push({ id: 'bounce-easing', snippet: `animation: ${animationName}` });380}381if (classList && /\banimate-bounce\b/.test(classList)) {382findings.push({ id: 'bounce-easing', snippet: 'animate-bounce (Tailwind)' });383}384385// Check timing functions for overshoot cubic-bezier (y values outside [0, 1])386if (timingFunctions) {387const bezierRe = /cubic-bezier\(\s*([\d.-]+)\s*,\s*([\d.-]+)\s*,\s*([\d.-]+)\s*,\s*([\d.-]+)\s*\)/g;388let m;389while ((m = bezierRe.exec(timingFunctions)) !== null) {390const y1 = parseFloat(m[2]), y2 = parseFloat(m[4]);391if (y1 < -0.1 || y1 > 1.1 || y2 < -0.1 || y2 > 1.1) {392findings.push({ id: 'bounce-easing', snippet: `cubic-bezier(${m[1]}, ${m[2]}, ${m[3]}, ${m[4]})` });393break;394}395}396}397398// --- Layout property transition ---399if (transitionProperty && transitionProperty !== 'all' && transitionProperty !== 'none') {400const props = transitionProperty.split(',').map(p => p.trim().toLowerCase());401const layoutFound = props.filter(p => LAYOUT_TRANSITION_PROPS.has(p));402if (layoutFound.length > 0) {403findings.push({ id: 'layout-transition', snippet: `transition: ${layoutFound.join(', ')}` });404}405}406407return findings;408}409410function checkGlow(opts) {411const { boxShadow, effectiveBg } = opts;412if (!boxShadow || boxShadow === 'none') return [];413if (!effectiveBg) return [];414415// Only flag on dark backgrounds (luminance < 0.1)416const bgLum = relativeLuminance(effectiveBg);417if (bgLum >= 0.1) return [];418419// Split multiple shadows (commas not inside parentheses)420const parts = boxShadow.split(/,(?![^(]*\))/);421for (const shadow of parts) {422const colorMatch = shadow.match(/rgba?\([^)]+\)/);423if (!colorMatch) continue;424const color = parseRgb(colorMatch[0]);425if (!color || !hasChroma(color, 30)) continue;426427// Extract px values — in computed style: "color Xpx Ypx BLURpx [SPREADpx]"428const afterColor = shadow.substring(shadow.indexOf(colorMatch[0]) + colorMatch[0].length);429const beforeColor = shadow.substring(0, shadow.indexOf(colorMatch[0]));430const pxVals = [...beforeColor.matchAll(/([\d.]+)px/g), ...afterColor.matchAll(/([\d.]+)px/g)]431.map(m => parseFloat(m[1]));432433// Third value is blur (offset-x, offset-y, blur, [spread])434if (pxVals.length >= 3 && pxVals[2] > 4) {435return [{ id: 'dark-glow', snippet: `Colored glow (${colorToHex(color)}) on dark background` }];436}437}438439return [];440}441442/**443* Regex-on-HTML checks shared between browser and Node page-level detection.444* These don't need DOM access, just the raw HTML string.445*/446function checkHtmlPatterns(html) {447const findings = [];448449// --- Color ---450451// AI color palette: purple/violet452const purpleHexRe = /#(?:7c3aed|8b5cf6|a855f7|9333ea|7e22ce|6d28d9|6366f1|764ba2|667eea)\b/gi;453if (purpleHexRe.test(html)) {454const purpleTextRe = /(?:(?:^|;)\s*color\s*:\s*(?:.*?)(?:#(?:7c3aed|8b5cf6|a855f7|9333ea|7e22ce|6d28d9))|gradient.*?#(?:7c3aed|8b5cf6|a855f7|764ba2|667eea))/gi;455if (purpleTextRe.test(html)) {456findings.push({ id: 'ai-color-palette', snippet: 'Purple/violet accent colors detected' });457}458}459460// Gradient text (background-clip: text + gradient)461const gradientRe = /(?:-webkit-)?background-clip\s*:\s*text/gi;462let gm;463while ((gm = gradientRe.exec(html)) !== null) {464const start = Math.max(0, gm.index - 200);465const context = html.substring(start, gm.index + gm[0].length + 200);466if (/gradient/i.test(context)) {467findings.push({ id: 'gradient-text', snippet: 'background-clip: text + gradient' });468break;469}470}471if (/\bbg-clip-text\b/.test(html) && /\bbg-gradient-to-/.test(html)) {472findings.push({ id: 'gradient-text', snippet: 'bg-clip-text + bg-gradient (Tailwind)' });473}474475// --- Layout ---476477// Monotonous spacing478const spacingValues = [];479const spacingRe = /(?:padding|margin)(?:-(?:top|right|bottom|left))?\s*:\s*(\d+)px/gi;480let sm;481while ((sm = spacingRe.exec(html)) !== null) {482const v = parseInt(sm[1], 10);483if (v > 0 && v < 200) spacingValues.push(v);484}485const gapRe = /gap\s*:\s*(\d+)px/gi;486while ((sm = gapRe.exec(html)) !== null) {487spacingValues.push(parseInt(sm[1], 10));488}489const twSpaceRe = /\b(?:p|px|py|pt|pb|pl|pr|m|mx|my|mt|mb|ml|mr|gap)-(\d+)\b/g;490while ((sm = twSpaceRe.exec(html)) !== null) {491spacingValues.push(parseInt(sm[1], 10) * 4);492}493const remSpacingRe = /(?:padding|margin)(?:-(?:top|right|bottom|left))?\s*:\s*([\d.]+)rem/gi;494while ((sm = remSpacingRe.exec(html)) !== null) {495const v = Math.round(parseFloat(sm[1]) * 16);496if (v > 0 && v < 200) spacingValues.push(v);497}498const roundedSpacing = spacingValues.map(v => Math.round(v / 4) * 4);499if (roundedSpacing.length >= 10) {500const counts = {};501for (const v of roundedSpacing) counts[v] = (counts[v] || 0) + 1;502const maxCount = Math.max(...Object.values(counts));503const dominantPct = maxCount / roundedSpacing.length;504const unique = [...new Set(roundedSpacing)].filter(v => v > 0);505if (dominantPct > 0.6 && unique.length <= 3) {506const dominant = Object.entries(counts).sort((a, b) => b[1] - a[1])[0][0];507findings.push({508id: 'monotonous-spacing',509snippet: `~${dominant}px used ${maxCount}/${roundedSpacing.length} times (${Math.round(dominantPct * 100)}%)`,510});511}512}513514// --- Motion ---515516// Bounce/elastic animation names517const bounceRe = /animation(?:-name)?\s*:\s*[^;]*\b(bounce|elastic|wobble|jiggle|spring)\b/gi;518if (bounceRe.test(html)) {519findings.push({ id: 'bounce-easing', snippet: 'Bounce/elastic animation in CSS' });520}521522// Overshoot cubic-bezier523const bezierRe = /cubic-bezier\(\s*([\d.-]+)\s*,\s*([\d.-]+)\s*,\s*([\d.-]+)\s*,\s*([\d.-]+)\s*\)/g;524let bm;525while ((bm = bezierRe.exec(html)) !== null) {526const y1 = parseFloat(bm[2]), y2 = parseFloat(bm[4]);527if (y1 < -0.1 || y1 > 1.1 || y2 < -0.1 || y2 > 1.1) {528findings.push({ id: 'bounce-easing', snippet: `cubic-bezier(${bm[1]}, ${bm[2]}, ${bm[3]}, ${bm[4]})` });529break;530}531}532533// Layout property transitions534const transRe = /transition(?:-property)?\s*:\s*([^;{}]+)/gi;535let tm;536while ((tm = transRe.exec(html)) !== null) {537const val = tm[1].toLowerCase();538if (/\ball\b/.test(val)) continue;539const found = val.match(/\b(?:(?:max|min)-)?(?:width|height)\b|\bpadding(?:-(?:top|right|bottom|left))?\b|\bmargin(?:-(?:top|right|bottom|left))?\b/gi);540if (found) {541findings.push({ id: 'layout-transition', snippet: `transition: ${found.join(', ')}` });542break;543}544}545546// --- Dark glow ---547548const darkBgRe = /background(?:-color)?\s*:\s*(?:#(?:0[0-9a-f]|1[0-9a-f]|2[0-3])[0-9a-f]{4}\b|#(?:0|1)[0-9a-f]{2}\b|rgb\(\s*(\d{1,2})\s*,\s*(\d{1,2})\s*,\s*(\d{1,2})\s*\))/gi;549const twDarkBg = /\bbg-(?:gray|slate|zinc|neutral|stone)-(?:9\d{2}|800)\b/;550if (darkBgRe.test(html) || twDarkBg.test(html)) {551const shadowRe = /box-shadow\s*:\s*([^;{}]+)/gi;552let shm;553while ((shm = shadowRe.exec(html)) !== null) {554const val = shm[1];555const colorMatch = val.match(/rgba?\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)/);556if (!colorMatch) continue;557const [r, g, b] = [+colorMatch[1], +colorMatch[2], +colorMatch[3]];558if ((Math.max(r, g, b) - Math.min(r, g, b)) < 30) continue;559const pxVals = [...val.matchAll(/(\d+)px|(?<![.\d])\b(0)\b(?![.\d])/g)].map(p => +(p[1] || p[2]));560if (pxVals.length >= 3 && pxVals[2] > 4) {561findings.push({ id: 'dark-glow', snippet: `Colored glow (rgb(${r},${g},${b})) on dark page` });562break;563}564}565}566567// --- Provider tells (gated): repeating-gradient stripes (GPT) ---568if (/repeating-(?:linear|radial|conic)-gradient\s*\(/i.test(html)) {569findings.push({ id: 'repeating-stripes-gradient', snippet: 'repeating-gradient decorative stripes' });570}571572// --- Provider tells (gated): "X theater" framing copy (GPT) ---573// Lives here (regex-on-HTML) rather than in the text-content analyzers so it574// runs in the bundled browser path too, not just the CLI/static path.575{576const bodyText = html577.replace(/<script\b[^>]*>[\s\S]*?<\/script>/gi, ' ')578.replace(/<style\b[^>]*>[\s\S]*?<\/style>/gi, ' ')579.replace(/<[^>]+>/g, ' ');580const tm = /\b(\w+)\s+theater\b/i.exec(bodyText);581if (tm) findings.push({ id: 'theater-slop-phrase', snippet: `"${tm[0].trim()}"` });582}583584// --- Provider tells (gated): image hover transform (Gemini) ---585// A CSS `img...:hover { transform: ... }` rule, or a Tailwind hover:scale /586// hover:rotate / hover:translate utility on an <img>. Each distinct587// mechanism is its own finding.588const imgHoverCss = /\bimg\b[^,{}]*:hover\b[^{}]*\{[^}]*\btransform\s*:\s*(?:scale|rotate|translate|matrix|skew)/i;589if (imgHoverCss.test(html)) {590findings.push({ id: 'image-hover-transform', snippet: 'img:hover { transform } rule' });591}592const imgTagRe = /<img\b[^>]*\bclass\s*=\s*"([^"]*)"/gi;593let im;594while ((im = imgTagRe.exec(html)) !== null) {595if (/\bhover:(?:scale|rotate|translate|skew)-/.test(im[1])) {596findings.push({ id: 'image-hover-transform', snippet: 'Tailwind hover transform on <img>' });597}598}599600return findings;601}602603// ─── Section 4: resolveBackground (unified) ─────────────────────────────────604605// Read the element's own background color, computed-style first, with a606// jsdom-friendly fallback that parses the inline `background:` shorthand607// from the raw style attribute. jsdom (~v29) does not decompose the608// shorthand into `backgroundColor`, so without this fallback the CLI silently609// returns null for any element styled via `background: rgb(...)` or610// `background: #abc`. Real browsers always decompose, so the fallback is611// a no-op there.612function readOwnBackgroundColor(el, computedStyle) {613const bg = parseRgb(computedStyle.backgroundColor);614if (DETECTOR_IS_BROWSER || (bg && bg.a >= 0.1)) return bg;615const rawStyle = el.getAttribute?.('style') || '';616const bgMatch = rawStyle.match(/background(?:-color)?\s*:\s*([^;]+)/i);617const inlineBg = bgMatch ? bgMatch[1].trim() : '';618if (!inlineBg) return bg;619if (/gradient/i.test(inlineBg) || /url\s*\(/i.test(inlineBg)) return bg;620const fromRgb = parseRgb(inlineBg);621if (fromRgb) return fromRgb;622const hexMatch = inlineBg.match(/#([0-9a-f]{6}|[0-9a-f]{3})\b/i);623if (hexMatch) {624const h = hexMatch[1];625if (h.length === 6) {626return { r: parseInt(h.slice(0, 2), 16), g: parseInt(h.slice(2, 4), 16), b: parseInt(h.slice(4, 6), 16), a: 1 };627}628return { r: parseInt(h[0] + h[0], 16), g: parseInt(h[1] + h[1], 16), b: parseInt(h[2] + h[2], 16), a: 1 };629}630return bg;631}632633function resolveBackground(el, win, customPropMap) {634let current = el;635while (current && current.nodeType === 1) {636const style = DETECTOR_IS_BROWSER ? getComputedStyle(current) : win.getComputedStyle(current);637const bgImage = style.backgroundImage || '';638const hasGradientOrUrl = bgImage && bgImage !== 'none' && (/gradient/i.test(bgImage) || /url\s*\(/i.test(bgImage));639640// Try the solid bg-color FIRST. If the element has both a solid color641// and a gradient/url overlay (a common pattern: `background: var(--paper)642// radial-gradient(...)` for paper-grain texture), the solid color is the643// dominant visible surface for contrast purposes; the overlay is644// decorative. The old behavior bailed on any gradient ancestor, which645// caused massive false-positive contrast findings on grain-textured646// body backgrounds.647let bg = parseRgb(style.backgroundColor);648if (!DETECTOR_IS_BROWSER && (!bg || bg.a < 0.1)) {649// jsdom returns literal "var(--X)" / "oklch(...)" strings. Resolve650// through customPropMap so Tailwind v4 color tokens become RGB.651if (customPropMap) {652bg = parseColorResolved(style.backgroundColor, customPropMap);653}654if (!bg || bg.a < 0.1) {655// Inline-style fallback. jsdom doesn't decompose background656// shorthand, so colors set via inline style are otherwise invisible.657const rawStyle = current.getAttribute?.('style') || '';658const bgMatch = rawStyle.match(/background(?:-color)?\s*:\s*([^;]+)/i);659const inlineBg = bgMatch ? bgMatch[1].trim() : '';660if (inlineBg && !/gradient/i.test(inlineBg) && !/url\s*\(/i.test(inlineBg)) {661bg = parseColorResolved(inlineBg, customPropMap) || parseAnyColor(inlineBg);662}663}664}665666if (bg && bg.a > 0.1) {667if (DETECTOR_IS_BROWSER || bg.a >= 0.5) return bg;668}669// No solid bg-color at this level. If THIS level has a gradient/url670// with no underlying solid color we can read:671// • on body/html: assume white. Body-level gradients are almost672// always decorative texture (paper grain, noise) on top of a673// solid bg-color the page set via `background: var(--paper)`674// shorthand — which jsdom can't decompose into bg-color. The675// downstream gradient-stops fallback path produces catastrophic676// false positives in this case (gradient noise stops have677// accidental browns/blacks that look like card backgrounds).678// • on other elements: bail to null and let the caller fall back679// to gradient stops (gradient buttons / hero sections are real680// bgs worth checking against).681if (hasGradientOrUrl) {682if (current.tagName === 'BODY' || current.tagName === 'HTML') {683return { r: 255, g: 255, b: 255, a: 1 };684}685return null;686}687current = current.parentElement;688}689return { r: 255, g: 255, b: 255 };690}691692// Walk parents looking for a gradient background and return its color stops.693// Used as a fallback when resolveBackground() returns null because the694// effective background is a gradient (no single solid color to compare against).695function resolveGradientStops(el, win) {696let current = el;697while (current && current.nodeType === 1) {698const style = DETECTOR_IS_BROWSER ? getComputedStyle(current) : win.getComputedStyle(current);699const bgImage = style.backgroundImage || '';700if (bgImage && bgImage !== 'none' && /gradient/i.test(bgImage)) {701const stops = parseGradientColors(bgImage);702if (stops.length > 0) return stops;703}704if (!DETECTOR_IS_BROWSER) {705// jsdom doesn't decompose `background:` shorthand — peek at the raw inline style706const rawStyle = current.getAttribute?.('style') || '';707const bgMatch = rawStyle.match(/background(?:-image)?\s*:\s*([^;]+)/i);708if (bgMatch && /gradient/i.test(bgMatch[1])) {709const stops = parseGradientColors(bgMatch[1]);710if (stops.length > 0) return stops;711}712}713current = current.parentElement;714}715return null;716}717718// Parse a single CSS length token to pixels. Accepts "12px", "50%", a719// shorthand like "12px 4px" (uses the first value), or empty / null.720// Returns the pixel value, or null when the input is unparseable.721// Percentages convert against `widthPx` when one is supplied. Without a722// usable width (jsdom returns "auto" for many real-world elements,723// which parseFloat collapses to 0), fall back to the raw percentage724// number so callers gating on `> 0` (border-accent-on-rounded,725// isCardLike's hasRadius) still see a positive value, matching the726// original parseFloat("50%") === 50 behavior.727function parseRadiusToPx(value, widthPx) {728if (!value || typeof value !== 'string') return null;729const trimmed = value.trim();730if (!trimmed) return null;731const first = trimmed.split(/\s+/)[0];732const num = parseFloat(first);733if (Number.isNaN(num)) return null;734if (/%$/.test(first)) {735if (widthPx && widthPx > 0) return (num / 100) * widthPx;736return num;737}738return num;739}740741function resolveBorderRadiusPx(el, style, widthPx, win) {742const fromComputed = parseRadiusToPx(style.borderRadius, widthPx);743if (fromComputed !== null) return fromComputed;744return 0;745}746747// ─── Section 5: Element Adapters ────────────────────────────────────────────748749// Browser adapters — call getComputedStyle/getBoundingClientRect on live DOM750751function checkElementBordersDOM(el) {752const tag = el.tagName.toLowerCase();753if (BORDER_SAFE_TAGS.has(tag)) return [];754const rect = el.getBoundingClientRect();755if (rect.width < 20 || rect.height < 20) return [];756const style = getComputedStyle(el);757const sides = ['Top', 'Right', 'Bottom', 'Left'];758const widths = {}, colors = {};759for (const s of sides) {760widths[s] = parseFloat(style[`border${s}Width`]) || 0;761colors[s] = style[`border${s}Color`] || '';762}763return checkBorders(tag, widths, colors, parseFloat(style.borderRadius) || 0);764}765766function checkElementColorsDOM(el) {767const tag = el.tagName.toLowerCase();768// No early SAFE_TAGS bail here — checkColors() does its own gating that769// includes the styled-button exception for <a> / <button> with their own770// opaque background. Bailing here would prevent that exception from firing.771const rect = el.getBoundingClientRect();772if (rect.width < 10 || rect.height < 10) return [];773const style = getComputedStyle(el);774const directText = [...el.childNodes].filter(n => n.nodeType === 3).map(n => n.textContent).join('');775const hasDirectText = directText.trim().length > 0;776const effectiveBg = resolveBackground(el);777return checkColors({778tag,779textColor: parseRgb(style.color),780bgColor: readOwnBackgroundColor(el, style),781effectiveBg,782effectiveBgStops: effectiveBg ? null : resolveGradientStops(el),783fontSize: parseFloat(style.fontSize) || 16,784fontWeight: parseInt(style.fontWeight) || 400,785hasDirectText,786isEmojiOnly: isEmojiOnlyText(directText),787bgClip: style.webkitBackgroundClip || style.backgroundClip || '',788bgImage: style.backgroundImage || '',789classList: el.getAttribute('class') || '',790});791}792793function checkElementIconTileDOM(el) {794const tag = el.tagName.toLowerCase();795if (!HEADING_TAGS.has(tag)) return [];796const sibling = el.previousElementSibling;797if (!sibling) return [];798799const sibRect = sibling.getBoundingClientRect();800const headRect = el.getBoundingClientRect();801const sibStyle = getComputedStyle(sibling);802803// The tile may either contain an <svg>/<i> icon child, OR the tile itself804// may contain an emoji/symbol character directly as its only text content805// (the "card-icon" pattern from many AI-generated demos).806const iconChild = sibling.querySelector('svg, i[data-lucide], i[class*="fa-"], i[class*="icon"]');807const iconRect = iconChild?.getBoundingClientRect();808const sibDirectText = [...sibling.childNodes].filter(n => n.nodeType === 3).map(n => n.textContent).join('');809const hasInlineEmojiIcon = sibling.children.length === 0 && isEmojiOnlyText(sibDirectText);810811return checkIconTile({812headingTag: tag,813headingText: el.textContent || '',814headingTop: headRect.top,815siblingTag: sibling.tagName.toLowerCase(),816siblingWidth: sibRect.width,817siblingHeight: sibRect.height,818siblingBottom: sibRect.bottom,819siblingBgColor: parseRgb(sibStyle.backgroundColor),820siblingBgImage: sibStyle.backgroundImage || '',821siblingBorderWidth: parseFloat(sibStyle.borderTopWidth) || 0,822siblingBorderRadius: parseFloat(sibStyle.borderRadius) || 0,823hasIconChild: !!iconChild || hasInlineEmojiIcon,824iconChildWidth: iconRect?.width || 0,825});826}827828function checkElementItalicSerifDOM(el) {829const tag = el.tagName.toLowerCase();830if (tag !== 'h1' && tag !== 'h2') return [];831const style = getComputedStyle(el);832return checkItalicSerif({833tag,834fontStyle: style.fontStyle || '',835fontFamily: style.fontFamily || '',836fontSize: parseFloat(style.fontSize) || 0,837headingText: el.textContent || '',838});839}840841function checkElementHeroEyebrowDOM(el) {842const tag = el.tagName.toLowerCase();843if (tag !== 'h1') return [];844const sibling = el.previousElementSibling;845if (!sibling) return [];846const headStyle = getComputedStyle(el);847const sibStyle = getComputedStyle(sibling);848return checkHeroEyebrow({849headingTag: tag,850headingText: el.textContent || '',851headingFontSize: parseFloat(headStyle.fontSize) || 0,852siblingTag: sibling.tagName.toLowerCase(),853siblingText: sibling.textContent || '',854siblingTextTransform: sibStyle.textTransform || '',855siblingFontSize: parseFloat(sibStyle.fontSize) || 0,856siblingLetterSpacing: parseFloat(sibStyle.letterSpacing) || 0,857siblingFontWeight: sibStyle.fontWeight || '',858siblingColor: sibStyle.color || '',859});860}861862// Build a map of CSS custom properties declared on :root / :host / html.863// Used to resolve var(--X) refs that jsdom returns verbatim in864// getComputedStyle. Tailwind v4 routes every utility class through865// CSS vars (font-weight: var(--font-weight-bold), font-size:866// var(--text-xs), letter-spacing: var(--tracking-widest)), so without867// resolution every style-based check silently fails on Tailwind v4868// builds — the values come back as literal "var(--font-weight-bold)"869// strings and parseFloat returns NaN.870function buildCustomPropMap(document) {871const map = new Map();872let sheets;873try { sheets = Array.from(document.styleSheets || []); }874catch { return map; }875for (const sheet of sheets) {876let rules;877try { rules = Array.from(sheet.cssRules || []); }878catch { continue; }879for (const rule of rules) {880// Style rules only (type 1). Walk @media / @supports if present.881if (rule.type === 4 /* MEDIA_RULE */ || rule.type === 12 /* SUPPORTS_RULE */) {882try { rules.push(...Array.from(rule.cssRules || [])); } catch { /* ignore */ }883continue;884}885if (rule.type !== 1 /* STYLE_RULE */) continue;886const sel = rule.selectorText || '';887if (!/(^|,\s*)(:root|html|:host)\b/i.test(sel)) continue;888const style = rule.style;889if (!style) continue;890for (let i = 0; i < style.length; i++) {891const prop = style[i];892if (!prop || !prop.startsWith('--')) continue;893const val = style.getPropertyValue(prop).trim();894if (val) map.set(prop, val);895}896}897}898return map;899}900901// Resolve var(--X[, fallback]) refs in a computed-style value string.902// Recurses up to 8 levels for chained refs (--a: var(--b)). Returns903// the original string when no refs are present or the chain doesn't904// resolve. Safe to call on already-resolved values.905function resolveVarRefs(raw, customPropMap, depth = 0) {906if (typeof raw !== 'string' || !raw.includes('var(')) return raw;907if (depth > 8) return raw;908return raw.replace(/var\(\s*(--[a-zA-Z0-9_-]+)\s*(?:,\s*([^)]+))?\)/g, (_m, name, fallback) => {909const v = customPropMap.get(name);910if (v != null) return resolveVarRefs(v, customPropMap, depth + 1);911return fallback ? resolveVarRefs(fallback.trim(), customPropMap, depth + 1) : _m;912});913}914915// OKLCH → sRGB conversion (Björn Ottosson's matrices). L in 0..1 (or %),916// C in 0..~0.4 typical, H in degrees. Returns clamped {r,g,b,a:1} in 0..255.917// Needed because jsdom doesn't compute oklch() values — getComputedStyle918// returns the literal "oklch(...)" string. Without this, the entire919// Tailwind v4 color palette (which is OKLCH-based) is invisible to the920// detector's contrast / color checks.921function oklchToRgb(L, C, H) {922const hRad = (H * Math.PI) / 180;923const a = C * Math.cos(hRad);924const b = C * Math.sin(hRad);925const l_ = L + 0.3963377774 * a + 0.2158037573 * b;926const m_ = L - 0.1055613458 * a - 0.0638541728 * b;927const s_ = L - 0.0894841775 * a - 1.2914855480 * b;928const lc = l_ * l_ * l_, mc = m_ * m_ * m_, sc = s_ * s_ * s_;929const rLin = 4.0767416621 * lc - 3.3077115913 * mc + 0.2309699292 * sc;930const gLin = -1.2684380046 * lc + 2.6097574011 * mc - 0.3413193965 * sc;931const bLin = -0.0041960863 * lc - 0.7034186147 * mc + 1.7076147010 * sc;932const enc = (x) => {933const c = Math.max(0, Math.min(1, x));934return c <= 0.0031308 ? 12.92 * c : 1.055 * Math.pow(c, 1 / 2.4) - 0.055;935};936return {937r: Math.round(enc(rLin) * 255),938g: Math.round(enc(gLin) * 255),939b: Math.round(enc(bLin) * 255),940a: 1,941};942}943944// Extended color parser: rgb/rgba/hex/oklch. Returns null on no match.945// Use this when the input might be any CSS color form; use plain parseRgb946// when you only expect computed rgb() values from real browsers.947function parseAnyColor(s) {948if (!s || typeof s !== 'string') return null;949const str = s.trim();950if (str === 'transparent' || str === 'currentcolor' || str === 'inherit') return null;951let m;952m = str.match(/rgba?\(\s*(\d+(?:\.\d+)?)\s*,?\s*(\d+(?:\.\d+)?)\s*,?\s*(\d+(?:\.\d+)?)(?:\s*[,/]\s*([\d.]+))?\s*\)/);953if (m) return { r: Math.round(+m[1]), g: Math.round(+m[2]), b: Math.round(+m[3]), a: m[4] !== undefined ? +m[4] : 1 };954m = str.match(/^#([0-9a-f]{3,8})$/i);955if (m) {956const h = m[1];957if (h.length === 3 || h.length === 4) {958return {959r: parseInt(h[0] + h[0], 16),960g: parseInt(h[1] + h[1], 16),961b: parseInt(h[2] + h[2], 16),962a: h.length === 4 ? parseInt(h[3] + h[3], 16) / 255 : 1,963};964}965if (h.length === 6 || h.length === 8) {966return {967r: parseInt(h.slice(0, 2), 16),968g: parseInt(h.slice(2, 4), 16),969b: parseInt(h.slice(4, 6), 16),970a: h.length === 8 ? parseInt(h.slice(6, 8), 16) / 255 : 1,971};972}973}974// OKLCH parser. Tailwind v4's CSS minifier squishes the space after975// `%` ("21.5%.02 50"), so the separator between L and C may be absent.976// Match L (with optional %), then C and H separated permissively.977m = str.match(/oklch\(\s*([\d.]+)(%?)\s*[\s,]*\s*([\d.]+)\s*[\s,]+\s*([-\d.]+)(?:deg)?\s*\)/i);978if (m) {979const Lnum = parseFloat(m[1]);980const L = m[2] === '%' ? Lnum / 100 : Lnum;981return oklchToRgb(L, parseFloat(m[3]), parseFloat(m[4]));982}983return null;984}985986// Resolve var() refs in a color string (via customPropMap), then parse.987// Returns null on any failure. Used in jsdom-mode paths where988// getComputedStyle returns literal "var(--X)" or "oklch(...)" strings.989function parseColorResolved(str, customPropMap) {990if (!str) return null;991const resolved = customPropMap ? resolveVarRefs(str, customPropMap) : str;992return parseAnyColor(resolved);993}994995const REPEATED_KICKER_SKIP_SELECTOR = [996'nav',997'form',998'table',999'thead',1000'tbody',1001'tfoot',1002'figure',1003'figcaption',1004'ol',1005'ul',1006'li',1007'[role="navigation"]',1008'[aria-label*="breadcrumb" i]',1009'[class*="breadcrumb" i]',1010'[data-impeccable-allow-kickers]',1011].join(',');10121013function cleanInlineText(el) {1014return [...el.childNodes]1015.filter(n => n.nodeType === 3)1016.map(n => n.textContent)1017.join(' ')1018.replace(/\s+/g, ' ')1019.trim();1020}10211022function isRepeatedKickerCandidate(opts) {1023const {1024headingTag,1025headingText,1026headingFontSize,1027kickerTag,1028kickerText,1029kickerTextTransform,1030kickerFontSize,1031kickerLetterSpacing,1032} = opts;1033if (!['h2', 'h3', 'h4'].includes(headingTag)) return false;1034if (!headingText || headingText.length < 3) return false;1035if (!(headingFontSize >= 20)) return false;1036if (!kickerTag || HEADING_TAGS.has(kickerTag)) return false;1037if (!['p', 'span', 'div', 'small'].includes(kickerTag)) return false;1038if (!kickerText || kickerText.length < 2 || kickerText.length > 34) return false;1039if (/^step\s*\d+/i.test(kickerText) || /^\d{1,2}$/.test(kickerText)) return false;10401041const isUppercased = kickerTextTransform === 'uppercase'1042|| (/[A-Z]/.test(kickerText) && !/[a-z]/.test(kickerText));1043if (!isUppercased) return false;1044if (!(kickerFontSize > 0 && kickerFontSize <= 14)) return false;1045const minTrackedSpacing = Math.max(1, kickerFontSize * 0.08);1046if (!(kickerLetterSpacing >= minTrackedSpacing)) return false;1047return true;1048}10491050function collectRepeatedSectionKickerCandidates(doc, getStyle, resolveLetterSpacing) {1051const candidates = [];1052for (const heading of doc.querySelectorAll('h2, h3, h4')) {1053if (heading.closest?.(REPEATED_KICKER_SKIP_SELECTOR)) continue;1054const kicker = heading.previousElementSibling;1055if (!kicker || kicker.closest?.(REPEATED_KICKER_SKIP_SELECTOR)) continue;10561057const headingStyle = getStyle(heading);1058const kickerStyle = getStyle(kicker);1059const headingText = (heading.textContent || '').replace(/\s+/g, ' ').trim();1060const kickerText = cleanInlineText(kicker) || (kicker.textContent || '').replace(/\s+/g, ' ').trim();1061const headingFontSize = resolveLetterSpacing(headingStyle.fontSize || '', 16) || parseFloat(headingStyle.fontSize) || 0;1062const kickerFontSize = resolveLetterSpacing(kickerStyle.fontSize || '', 16) || parseFloat(kickerStyle.fontSize) || 0;1063const kickerLetterSpacing = resolveLetterSpacing(kickerStyle.letterSpacing || '', kickerFontSize);10641065if (!isRepeatedKickerCandidate({1066headingTag: heading.tagName.toLowerCase(),1067headingText,1068headingFontSize,1069kickerTag: kicker.tagName.toLowerCase(),1070kickerText,1071kickerTextTransform: kickerStyle.textTransform || '',1072kickerFontSize,1073kickerLetterSpacing,1074})) {1075continue;1076}10771078candidates.push({1079headingTag: heading.tagName.toLowerCase(),1080headingText: headingText.replace(/^"|"$/g, '').slice(0, 60),1081kickerText: kickerText.slice(0, 40),1082});1083}1084return candidates;1085}10861087function checkRepeatedSectionKickersDOM() {1088const candidates = collectRepeatedSectionKickerCandidates(1089document,1090(el) => getComputedStyle(el),1091(value, fontSize) => resolveLengthPx(value, fontSize) || 0,1092);1093return checkRepeatedSectionKickers({ candidates });1094}10951096function checkElementMotionDOM(el) {1097const tag = el.tagName.toLowerCase();1098if (SAFE_TAGS.has(tag)) return [];1099const style = getComputedStyle(el);1100return checkMotion({1101tag,1102transitionProperty: style.transitionProperty || '',1103animationName: style.animationName || '',1104timingFunctions: [style.animationTimingFunction, style.transitionTimingFunction].filter(Boolean).join(' '),1105classList: el.getAttribute('class') || '',1106});1107}11081109function checkElementGlowDOM(el) {1110const tag = el.tagName.toLowerCase();1111const style = getComputedStyle(el);1112if (!style.boxShadow || style.boxShadow === 'none') return [];1113// Use parent's background — glow radiates outward, so the surrounding context matters1114// If resolveBackground returns null (gradient), try to infer from the gradient colors1115let parentBg = el.parentElement ? resolveBackground(el.parentElement) : resolveBackground(el);1116if (!parentBg) {1117// Gradient background — sample its colors to determine if it's dark1118let cur = el.parentElement;1119while (cur && cur.nodeType === 1) {1120const bgImage = getComputedStyle(cur).backgroundImage || '';1121const gradColors = parseGradientColors(bgImage);1122if (gradColors.length > 0) {1123// Average the gradient colors1124const avg = { r: 0, g: 0, b: 0 };1125for (const c of gradColors) { avg.r += c.r; avg.g += c.g; avg.b += c.b; }1126avg.r = Math.round(avg.r / gradColors.length);1127avg.g = Math.round(avg.g / gradColors.length);1128avg.b = Math.round(avg.b / gradColors.length);1129parentBg = avg;1130break;1131}1132cur = cur.parentElement;1133}1134}1135return checkGlow({ tag, boxShadow: style.boxShadow, effectiveBg: parentBg });1136}11371138function checkElementAIPaletteDOM(el) {1139const style = getComputedStyle(el);1140const findings = [];11411142// Check gradient backgrounds for purple/violet or cyan1143const bgImage = style.backgroundImage || '';1144const gradColors = parseGradientColors(bgImage);1145for (const c of gradColors) {1146if (hasChroma(c, 50)) {1147const hue = getHue(c);1148if (hue >= 260 && hue <= 310) {1149findings.push({ id: 'ai-color-palette', snippet: 'Purple/violet gradient background' });1150break;1151}1152if (hue >= 160 && hue <= 200) {1153findings.push({ id: 'ai-color-palette', snippet: 'Cyan gradient background' });1154break;1155}1156}1157}11581159// Check for neon text (vivid cyan/purple color on dark background)1160const textColor = parseRgb(style.color);1161if (textColor && hasChroma(textColor, 80)) {1162const hue = getHue(textColor);1163const isAIPalette = (hue >= 160 && hue <= 200) || (hue >= 260 && hue <= 310);1164if (isAIPalette) {1165const parentBg = el.parentElement ? resolveBackground(el.parentElement) : null;1166// Also check gradient parents1167let effectiveBg = parentBg;1168if (!effectiveBg) {1169let cur = el.parentElement;1170while (cur && cur.nodeType === 1) {1171const gi = getComputedStyle(cur).backgroundImage || '';1172const gc = parseGradientColors(gi);1173if (gc.length > 0) {1174const avg = { r: 0, g: 0, b: 0 };1175for (const c of gc) { avg.r += c.r; avg.g += c.g; avg.b += c.b; }1176avg.r = Math.round(avg.r / gc.length);1177avg.g = Math.round(avg.g / gc.length);1178avg.b = Math.round(avg.b / gc.length);1179effectiveBg = avg;1180break;1181}1182cur = cur.parentElement;1183}1184}1185if (effectiveBg && relativeLuminance(effectiveBg) < 0.1) {1186const label = hue >= 260 ? 'Purple/violet' : 'Cyan';1187findings.push({ id: 'ai-color-palette', snippet: `${label} neon text on dark background` });1188}1189}1190}11911192return findings;1193}11941195const QUALITY_TEXT_TAGS = new Set(['p', 'li', 'td', 'th', 'dd', 'blockquote', 'figcaption']);11961197// Resolve a CSS font-size value to pixels by walking up the parent chain.1198// Browsers resolve em/rem/% to px in getComputedStyle, but jsdom returns the1199// specified value verbatim — so for the Node path we walk parents ourselves.1200function resolveFontSizePx(el, win) {1201const chain = []; // raw font-size strings, leaf → root1202let cur = el;1203while (cur && cur.nodeType === 1) {1204const fs = (win ? win.getComputedStyle(cur) : getComputedStyle(cur)).fontSize;1205chain.push(fs || '');1206cur = cur.parentElement;1207}1208// Walk root → leaf, resolving each value relative to its parent context.1209let px = 16; // root default1210for (let i = chain.length - 1; i >= 0; i--) {1211const v = chain[i];1212if (!v || v === 'inherit') continue;1213const num = parseFloat(v);1214if (isNaN(num)) continue;1215if (v.endsWith('px')) px = num;1216else if (v.endsWith('rem')) px = num * 16;1217else if (v.endsWith('em')) px = num * px;1218else if (v.endsWith('%')) px = (num / 100) * px;1219else px = num; // unitless — already resolved1220}1221return px;1222}12231224// Resolve a CSS length value (line-height, letter-spacing, etc.) given a1225// known font-size context. Returns null for "normal" / unparseable values.1226function resolveLengthPx(value, fontSizePx) {1227if (!value || value === 'normal' || value === 'auto' || value === 'inherit') return null;1228const num = parseFloat(value);1229if (isNaN(num)) return null;1230if (value.endsWith('px')) return num;1231if (value.endsWith('rem')) return num * 16;1232if (value.endsWith('em')) return num * fontSizePx;1233if (value.endsWith('%')) return (num / 100) * fontSizePx;1234// Unitless line-height = multiplier, return px equivalent1235return num * fontSizePx;1236}12371238// Pure quality checks. Most run on computed CSS and DOM-only inputs (work in1239// jsdom and the browser). Two checks (line-length, cramped-padding) gate on1240// element rect dimensions, which jsdom can't compute — pass `rect: null` from1241// the Node adapter to skip those.1242//1243// Both adapters resolve font-size, line-height and letter-spacing to pixels1244// before calling this so the pure function only deals with numbers.1245function checkQuality(opts) {1246const { el, tag, style, hasDirectText, textLen, fontSize, lineHeightPx, letterSpacingPx, rect, lineMax = 80, viewportWidth = 0, win = null } = opts;1247const findings = [];1248// Skip browser extension injected elements1249const elId = el.id || '';1250if (elId.startsWith('claude-') || elId.startsWith('cic-')) return findings;12511252// --- Line length too long --- (browser-only: needs rect.width)1253if (rect && hasDirectText && QUALITY_TEXT_TAGS.has(tag) && rect.width > 0 && textLen > lineMax) {1254const charsPerLine = rect.width / (fontSize * 0.5);1255if (charsPerLine > lineMax + 5) {1256findings.push({ id: 'line-length', snippet: `~${Math.round(charsPerLine)} chars/line (aim for <${lineMax})` });1257}1258}12591260// --- Cramped padding --- (browser-only: needs rect to skip small badges/labels)1261// Vertical and horizontal thresholds are independent because line-height1262// already provides built-in vertical breathing room (the line box is taller1263// than the cap height), but horizontal has no equivalent. Both scale with1264// font-size — bigger text demands proportionally more padding.1265// vertical: max(4px, fontSize × 0.3)1266// horizontal: max(8px, fontSize × 0.5)1267if (rect && hasDirectText && textLen > 20 && rect.width > 100 && rect.height > 30) {1268const borders = {1269top: parseFloat(style.borderTopWidth) || 0,1270right: parseFloat(style.borderRightWidth) || 0,1271bottom: parseFloat(style.borderBottomWidth) || 0,1272left: parseFloat(style.borderLeftWidth) || 0,1273};1274const borderCount = Object.values(borders).filter(w => w > 0).length;1275const hasBg = style.backgroundColor && style.backgroundColor !== 'rgba(0, 0, 0, 0)';1276if (borderCount >= 2 || hasBg) {1277const vPads = [], hPads = [];1278if (hasBg || borders.top > 0) vPads.push(parseFloat(style.paddingTop) || 0);1279if (hasBg || borders.bottom > 0) vPads.push(parseFloat(style.paddingBottom) || 0);1280if (hasBg || borders.left > 0) hPads.push(parseFloat(style.paddingLeft) || 0);1281if (hasBg || borders.right > 0) hPads.push(parseFloat(style.paddingRight) || 0);12821283const vMin = vPads.length ? Math.min(...vPads) : Infinity;1284const hMin = hPads.length ? Math.min(...hPads) : Infinity;1285const vThresh = Math.max(4, fontSize * 0.3);1286const hThresh = Math.max(8, fontSize * 0.5);12871288// Emit at most one finding per element — pick whichever axis is worse.1289if (vMin < vThresh) {1290findings.push({ id: 'cramped-padding', snippet: `${vMin}px vertical padding (need ≥${vThresh.toFixed(1)}px for ${fontSize}px text)` });1291} else if (hMin < hThresh) {1292findings.push({ id: 'cramped-padding', snippet: `${hMin}px horizontal padding (need ≥${hThresh.toFixed(1)}px for ${fontSize}px text)` });1293}1294}1295}12961297// --- Flush against a visible boundary ---1298// Fires when a container has a visible boundary (border, outline, OR a1299// non-transparent background) AND near-zero padding on the bounded1300// side(s) AND text-bearing children land flush against the boundary.1301//1302// Distinct from cramped-padding: that rule needs the element itself to1303// have direct text (hasDirectText). This rule targets the OPPOSITE1304// shape — a container with NO direct text, only children — which is1305// exactly what cramped-padding misses (a section wrapping a label +1306// list lands a free pass).1307//1308// The classic shape: agent writes `padding: 28px 0 0` shorthand on a1309// section that also has a border, zeroing horizontal padding so the1310// text-bearing children touch the side borders. Background and1311// outline count too: a colored card with zero padding has the same1312// visual failure mode.1313{1314const FLUSH_SKIP_TAGS = new Set(['HTML', 'BODY', 'MAIN', 'HEADER', 'FOOTER', 'NAV', 'ARTICLE', 'ASIDE', 'BUTTON', 'A', 'LABEL', 'SUMMARY', 'CODE', 'PRE', 'INPUT', 'TEXTAREA', 'SELECT', 'FORM', 'FIGURE', 'TABLE', 'TBODY', 'THEAD', 'TR', 'TD', 'TH']);1315const upperTag = tag ? tag.toUpperCase() : '';1316const elPosition = style.position || '';1317if (1318!FLUSH_SKIP_TAGS.has(upperTag) &&1319!hasDirectText &&1320!['fixed', 'absolute'].includes(elPosition) &&1321el.children && el.children.length > 01322) {1323const isTransparent = (c) =>1324!c || c === 'transparent' || c === 'rgba(0, 0, 0, 0)' ||1325/^rgba\(\s*\d+\s*,\s*\d+\s*,\s*\d+\s*,\s*0(?:\.0+)?\s*\)$/.test(c);13261327const borderW = {1328top: parseFloat(style.borderTopWidth) || 0,1329right: parseFloat(style.borderRightWidth) || 0,1330bottom: parseFloat(style.borderBottomWidth) || 0,1331left: parseFloat(style.borderLeftWidth) || 0,1332};1333const borderVisible = {1334top: borderW.top > 0 && !isTransparent(style.borderTopColor),1335right: borderW.right > 0 && !isTransparent(style.borderRightColor),1336bottom: borderW.bottom > 0 && !isTransparent(style.borderBottomColor),1337left: borderW.left > 0 && !isTransparent(style.borderLeftColor),1338};1339// Outline detection. jsdom decomposes `border` shorthand into1340// border{Top,…}Width/Color but does NOT decompose `outline` —1341// the longhands come back empty when the value was set via the1342// shorthand. Fall back to parsing `style.outline` ourselves.1343let outlineW = parseFloat(style.outlineWidth) || 0;1344let outlineStyleVal = style.outlineStyle || '';1345let outlineColorVal = style.outlineColor || '';1346if (!outlineW && style.outline) {1347const wMatch = style.outline.match(/(\d+(?:\.\d+)?)\s*px/);1348if (wMatch) outlineW = parseFloat(wMatch[1]) || 0;1349if (!outlineStyleVal) {1350outlineStyleVal = /\b(solid|dashed|dotted|double|groove|ridge|inset|outset)\b/.test(style.outline) ? 'solid' : '';1351}1352if (!outlineColorVal) {1353const cMatch = style.outline.match(/(rgba?\([^)]+\)|#[0-9a-fA-F]{3,8}|[a-zA-Z]+)\s*$/);1354if (cMatch) outlineColorVal = cMatch[1];1355}1356}1357const outlineVisible = outlineW > 0 && !isTransparent(outlineColorVal) && outlineStyleVal && outlineStyleVal !== 'none';1358const bgVisible = !isTransparent(style.backgroundColor);13591360const anyVisible = borderVisible.top || borderVisible.right || borderVisible.bottom || borderVisible.left || outlineVisible || bgVisible;1361if (anyVisible) {1362// Resolve padding to px (jsdom returns raw "1.5rem" etc., not the1363// computed px value; parseFloat would strip the unit and treat1364// 1.5rem as 1.5px, false-flagging legitimate insets).1365const pad = {1366top: resolveLengthPx(style.paddingTop, fontSize) ?? 0,1367right: resolveLengthPx(style.paddingRight, fontSize) ?? 0,1368bottom: resolveLengthPx(style.paddingBottom, fontSize) ?? 0,1369left: resolveLengthPx(style.paddingLeft, fontSize) ?? 0,1370};1371const PAD_THRESHOLD = 2;1372// Children-insulate-this-side: a side is insulated if ANY direct1373// child has its own padding ≥ 4px on that side. Rationale: in1374// typical flow, only the first/last (or leftmost/rightmost)1375// children actually sit at the parent's edges. If even one of1376// them has its own padding, the visual flush is broken on that1377// side. Classic example: a column-flow card frame where the1378// top child (header) has padding-top:12 and the bottom child1379// (footer) has padding-bottom:8 — the parent's padding:0 doesn't1380// matter; nothing is actually flush. The `any-child-insulates`1381// heuristic accepts some false negatives (a card with one heavily1382// padded middle child won't flag) for far fewer false positives.1383const CHILD_INSULATE_THRESHOLD = 4;1384const childrenInsulate = { top: false, right: false, bottom: false, left: false };1385for (const child of el.children) {1386let childStyle = null;1387if (win && typeof win.getComputedStyle === 'function') {1388try { childStyle = win.getComputedStyle(child); } catch {}1389}1390if (!childStyle && typeof getComputedStyle === 'function') {1391try { childStyle = getComputedStyle(child); } catch {}1392}1393if (!childStyle) continue;1394const childPad = {1395top: resolveLengthPx(childStyle.paddingTop, fontSize) ?? 0,1396right: resolveLengthPx(childStyle.paddingRight, fontSize) ?? 0,1397bottom: resolveLengthPx(childStyle.paddingBottom, fontSize) ?? 0,1398left: resolveLengthPx(childStyle.paddingLeft, fontSize) ?? 0,1399};1400for (const s of ['top', 'right', 'bottom', 'left']) {1401if (childPad[s] >= CHILD_INSULATE_THRESHOLD) childrenInsulate[s] = true;1402}1403}14041405const flushSides = [];1406for (const side of ['top', 'right', 'bottom', 'left']) {1407const sideBounded = borderVisible[side] || outlineVisible || bgVisible;1408if (sideBounded && pad[side] <= PAD_THRESHOLD && !childrenInsulate[side]) {1409flushSides.push(side);1410}1411}14121413if (flushSides.length > 0) {1414// Confirm at least one direct child has substantial text content1415// (> 4 chars). Without this, the flush is harmless: e.g. an1416// image-only card.1417let hasTextChild = false;1418for (const child of el.children) {1419const childText = (child.textContent || '').trim();1420if (childText.length > 4) { hasTextChild = true; break; }1421}1422if (hasTextChild) {1423const cls = (typeof el.className === 'string' && el.className.trim())1424? el.className.trim().split(/\s+/)[0]1425: '';1426const boundaryParts = [];1427const borderSidesVisible = ['top', 'right', 'bottom', 'left'].filter(s => borderVisible[s]);1428if (borderSidesVisible.length === 4) boundaryParts.push('border');1429else if (borderSidesVisible.length > 0) boundaryParts.push(`border-${borderSidesVisible.join('/')}`);1430if (outlineVisible) boundaryParts.push('outline');1431if (bgVisible) boundaryParts.push('bg');1432const sidesLabel = flushSides.length === 4 ? 'all sides' : flushSides.join('/');1433const ident = cls1434? `<${tag.toLowerCase()}> "${cls}"`1435: `<${tag.toLowerCase()}>`;1436findings.push({1437id: 'cramped-padding',1438snippet: `${ident}: children flush against ${boundaryParts.join('+')} on ${sidesLabel} (no inset)`,1439});1440}1441}1442}1443}1444}14451446// --- Body text touching viewport edge --- (browser-only: needs rect)1447// Catches the failure mode where the agent ships body paragraphs1448// with NO container providing horizontal padding — text bleeds1449// directly to the viewport edge. Different from cramped-padding,1450// which requires a colored/bordered container. Here the failure1451// is the absence of the container entirely.1452//1453// Gate aggressively to avoid false positives:1454// - <p> or <li> only (body content; not headings, not nav, not1455// wrappers)1456// - text > 40 chars (paragraph-like, not a label)1457// - rect.width > 50% of viewport (real body, not a pull-quote)1458// - rect.left < 16 OR rect.right > viewport - 16 (actually1459// touching the edge)1460// - not inside <nav> or <header> (those legitimately bleed)1461// - element itself has no background-color (intentional full-bleed1462// sections set a bg-color and provide their own internal padding)1463if (rect && hasDirectText && textLen > 40 && ['P', 'LI'].includes(tag.toUpperCase()) && viewportWidth > 0) {1464const inNavHeader = el.closest && (el.closest('nav') || el.closest('header'));1465const hasOwnBg = style.backgroundColor && style.backgroundColor !== 'rgba(0, 0, 0, 0)' && style.backgroundColor !== 'transparent';1466const isPositioned = ['fixed', 'absolute'].includes(style.position || '');1467const widthRatio = rect.width / viewportWidth;1468const leftClose = rect.left < 16;1469const rightClose = rect.right > viewportWidth - 16;1470if (!inNavHeader && !hasOwnBg && !isPositioned && widthRatio > 0.5 && (leftClose || rightClose)) {1471const which = leftClose && rightClose1472? `left ${Math.round(rect.left)}px / right ${Math.round(viewportWidth - rect.right)}px`1473: leftClose1474? `left ${Math.round(rect.left)}px`1475: `right ${Math.round(viewportWidth - rect.right)}px`;1476findings.push({ id: 'body-text-viewport-edge', snippet: `<${tag.toLowerCase()}> with ${textLen}-char body bleeds to viewport edge (${which})` });1477}1478}14791480// --- Tight line height ---1481if (hasDirectText && textLen > 50 && !['h1','h2','h3','h4','h5','h6'].includes(tag)) {1482if (lineHeightPx != null && fontSize > 0) {1483const ratio = lineHeightPx / fontSize;1484if (ratio > 0 && ratio < 1.3) {1485findings.push({ id: 'tight-leading', snippet: `line-height ${ratio.toFixed(2)}x (need >=1.3)` });1486}1487}1488}14891490// --- Justified text (without hyphens) ---1491if (hasDirectText && style.textAlign === 'justify') {1492const hyphens = style.hyphens || style.webkitHyphens || '';1493if (hyphens !== 'auto') {1494findings.push({ id: 'justified-text', snippet: 'text-align: justify without hyphens: auto' });1495}1496}14971498// --- Tiny body text ---1499// Only flag actual body content, not UI labels (buttons, tabs, badges, captions, footer text, etc.)1500if (hasDirectText && textLen > 20 && fontSize < 12) {1501const skipTags = ['sub', 'sup', 'code', 'kbd', 'samp', 'var', 'caption', 'figcaption'];1502const inUIContext = el.closest && el.closest('button, a, label, summary, [role="button"], [role="link"], [role="tab"], [role="menuitem"], [role="option"], nav, footer, [class*="badge" i], [class*="chip" i], [class*="pill" i], [class*="tag" i], [class*="label" i], [class*="caption" i]');1503const isUppercase = style.textTransform === 'uppercase';1504if (!skipTags.includes(tag) && !inUIContext && !isUppercase) {1505findings.push({ id: 'tiny-text', snippet: `${fontSize}px body text` });1506}1507}15081509// --- All-caps body text ---1510if (hasDirectText && textLen > 30 && style.textTransform === 'uppercase') {1511if (!['h1','h2','h3','h4','h5','h6'].includes(tag)) {1512findings.push({ id: 'all-caps-body', snippet: `text-transform: uppercase on ${textLen} chars of body text` });1513}1514}15151516// --- Wide letter spacing on body text ---1517if (hasDirectText && textLen > 20 && style.textTransform !== 'uppercase') {1518if (letterSpacingPx != null && letterSpacingPx > 0 && fontSize > 0) {1519const trackingEm = letterSpacingPx / fontSize;1520if (trackingEm > 0.05) {1521findings.push({ id: 'wide-tracking', snippet: `letter-spacing: ${trackingEm.toFixed(2)}em on body text` });1522}1523}1524}15251526// --- Crushed letter spacing (mirror of wide-tracking) ---1527// Tracking pulled tighter than ~-0.05em crushes characters into each other.1528// Optical tightening that display type legitimately wants (around -0.02em)1529// stays well above this floor.1530if (hasDirectText && textLen > 20 && fontSize > 0) {1531if (letterSpacingPx != null && letterSpacingPx < 0) {1532const trackingEm = letterSpacingPx / fontSize;1533if (trackingEm <= -0.05) {1534const excerpt = (el.textContent || '').trim().replace(/\s+/g, ' ').slice(0, 40);1535findings.push({ id: 'extreme-negative-tracking', snippet: `letter-spacing: ${trackingEm.toFixed(2)}em — "${excerpt}"` });1536}1537}1538}15391540return findings;1541}15421543function checkElementQualityDOM(el) {1544const tag = el.tagName.toLowerCase();1545const style = getComputedStyle(el);1546const hasDirectText = [...el.childNodes].some(n => n.nodeType === 3 && n.textContent.trim().length > 10);1547const textLen = el.textContent?.trim().length || 0;1548// Browser getComputedStyle resolves everything to px — direct parseFloat1549// works.1550const fontSize = parseFloat(style.fontSize) || 16;1551const lineHeightPx = resolveLengthPx(style.lineHeight, fontSize);1552const letterSpacingPx = resolveLengthPx(style.letterSpacing, fontSize);1553const rect = el.getBoundingClientRect();1554const lineMax = (typeof window !== 'undefined' && window.__IMPECCABLE_CONFIG__?.lineLengthMax) || 80;1555const viewportWidth = (typeof window !== 'undefined' ? window.innerWidth : 0) || 0;1556return checkQuality({ el, tag, style, hasDirectText, textLen, fontSize, lineHeightPx, letterSpacingPx, rect, lineMax, viewportWidth, win: typeof window !== 'undefined' ? window : null });1557}15581559// Pure page-level skipped-heading walk. Takes a Document so it works in both1560// the browser and jsdom.1561function checkPageQualityFromDoc(doc) {1562const findings = [];1563const headings = doc.querySelectorAll('h1, h2, h3, h4, h5, h6');1564let prevLevel = 0;1565let prevText = '';1566for (const h of headings) {1567const level = parseInt(h.tagName[1]);1568const text = (h.textContent || '').trim().replace(/\s+/g, ' ').slice(0, 60);1569if (prevLevel > 0 && level > prevLevel + 1) {1570findings.push({1571id: 'skipped-heading',1572snippet: `<h${prevLevel}> "${prevText}" followed by <h${level}> "${text}" (missing h${prevLevel + 1})`,1573});1574}1575prevLevel = level;1576prevText = text;1577}1578return findings;1579}15801581// Browser adapter (returns the legacy { type, detail } shape used by the overlay loop)1582function checkPageQualityDOM() {1583return checkPageQualityFromDoc(document).map(f => ({ type: f.id, detail: f.snippet }));1584}15851586// Node adapters — take pre-extracted jsdom computed style15871588// jsdom doesn't lay out OR resolve em/rem/% to px — so we pre-resolve every1589// CSS length the rule needs ourselves (walking the parent chain for1590// font-size inheritance), and pass `rect: null` to skip the two rules that1591// genuinely need element rects (line-length, cramped-padding).1592function checkElementQuality(el, style, tag, window) {1593const hasDirectText = [...el.childNodes].some(n => n.nodeType === 3 && n.textContent.trim().length > 10);1594const textLen = el.textContent?.trim().length || 0;1595const fontSize = resolveFontSizePx(el, window);1596const lineHeightPx = resolveLengthPx(style.lineHeight, fontSize);1597const letterSpacingPx = resolveLengthPx(style.letterSpacing, fontSize);1598return checkQuality({ el, tag, style, hasDirectText, textLen, fontSize, lineHeightPx, letterSpacingPx, rect: null, win: window });1599}16001601function checkElementBorders(tag, style, overrides, resolvedRadius) {1602const sides = ['Top', 'Right', 'Bottom', 'Left'];1603const widths = {}, colors = {};1604for (const s of sides) {1605widths[s] = parseFloat(style[`border${s}Width`]) || 0;1606colors[s] = style[`border${s}Color`] || '';1607// jsdom silently drops any border shorthand containing var(), leaving1608// both width and color empty on the computed style. When the detectHtml1609// pre-pass pulled a resolved value off the rule, use it to fill in the1610// missing side so the side-tab check can run. Real browsers resolve1611// var() natively, so this fallback is a no-op in the browser path.1612if (widths[s] === 0 && overrides && overrides[s]) {1613widths[s] = overrides[s].width;1614colors[s] = overrides[s].color;1615} else if (colors[s] && colors[s].startsWith('var(') && overrides && overrides[s]) {1616// Longhand case: jsdom kept the width but left the color as the1617// literal `var(...)` string. Substitute the resolved color.1618colors[s] = overrides[s].color;1619}1620}1621// resolvedRadius lets the caller pre-resolve the radius via1622// resolveBorderRadiusPx so the value survives jsdom 29.1.0's broken1623// shorthand serialization. Falls back to the computed value for tests1624// and browser callers that don't pre-resolve.1625const radius = resolvedRadius != null1626? resolvedRadius1627: (parseFloat(style.borderRadius) || 0);1628return checkBorders(tag, widths, colors, radius);1629}16301631function checkElementColors(el, style, tag, window, customPropMap, hasAnchorInheritRule) {1632const directText = [...el.childNodes].filter(n => n.nodeType === 3).map(n => n.textContent).join('');1633const hasDirectText = directText.trim().length > 0;16341635const effectiveBg = resolveBackground(el, window, customPropMap);1636// jsdom returns literal "var(--X)" / "oklch(...)" for color, so plain1637// parseRgb misses Tailwind-tokenized text colors. Resolve through the1638// customPropMap first; fall back to parseRgb for vanilla rgb() pages.1639let textColor = customPropMap ? parseColorResolved(style.color, customPropMap) : null;1640if (!textColor) textColor = parseRgb(style.color);16411642// Anchor-inherit FP workaround: jsdom's UA stylesheet has `:link { color:1643// blue }` at high specificity. The page's `a { color: inherit }` rule1644// (Tailwind v4 preflight) loses to jsdom even though it WINS in real1645// browsers (Chrome's UA wraps :link in :where() — zero specificity).1646// When the page declares the inherit rule AND we see jsdom's default1647// link blue on an anchor, walk to the nearest non-anchor ancestor and1648// use its color instead.1649if (1650hasAnchorInheritRule &&1651textColor &&1652textColor.r === 0 && textColor.g === 0 && textColor.b === 238 &&1653(tag === 'a' || el.closest?.('a'))1654) {1655let cur = el.parentElement;1656while (cur && cur.tagName !== 'HTML') {1657if (cur.tagName !== 'A') {1658const ps = window.getComputedStyle(cur);1659const inh = (customPropMap ? parseColorResolved(ps.color, customPropMap) : null) || parseRgb(ps.color);1660if (inh && !(inh.r === 0 && inh.g === 0 && inh.b === 238)) {1661textColor = inh;1662break;1663}1664}1665cur = cur.parentElement;1666}1667}16681669return checkColors({1670tag,1671textColor,1672bgColor: readOwnBackgroundColor(el, style),1673effectiveBg,1674effectiveBgStops: effectiveBg ? null : resolveGradientStops(el, window),1675fontSize: parseFloat(style.fontSize) || 16,1676fontWeight: parseInt(style.fontWeight) || 400,1677hasDirectText,1678isEmojiOnly: isEmojiOnlyText(directText),1679bgClip: style.webkitBackgroundClip || style.backgroundClip || '',1680bgImage: style.backgroundImage || '',1681classList: el.getAttribute?.('class') || el.className || '',1682});1683}16841685function checkElementIconTile(el, tag, window) {1686if (!HEADING_TAGS.has(tag)) return [];1687const sibling = el.previousElementSibling;1688if (!sibling) return [];16891690const sibStyle = window.getComputedStyle(sibling);1691// jsdom doesn't lay out — read explicit pixel dimensions from CSS instead.1692const sibWidth = parseFloat(sibStyle.width) || 0;1693const sibHeight = parseFloat(sibStyle.height) || 0;16941695const iconChild = sibling.querySelector('svg, i[data-lucide], i[class*="fa-"], i[class*="icon"]');1696let iconWidth = 0;1697if (iconChild) {1698const iconStyle = window.getComputedStyle(iconChild);1699iconWidth = parseFloat(iconStyle.width) || parseFloat(iconChild.getAttribute('width')) || 0;1700}1701// Or: tile contains an emoji/symbol character directly as its only content1702const sibDirectText = [...sibling.childNodes].filter(n => n.nodeType === 3).map(n => n.textContent).join('');1703const hasInlineEmojiIcon = sibling.children.length === 0 && isEmojiOnlyText(sibDirectText);17041705return checkIconTile({1706headingTag: tag,1707headingText: el.textContent || '',1708headingTop: 0, // jsdom: no layout, skip vertical-stacking gate1709siblingTag: sibling.tagName.toLowerCase(),1710siblingWidth: sibWidth,1711siblingHeight: sibHeight,1712siblingBottom: 0,1713siblingBgColor: parseRgb(sibStyle.backgroundColor),1714siblingBgImage: sibStyle.backgroundImage || '',1715siblingBorderWidth: parseFloat(sibStyle.borderTopWidth) || 0,1716siblingBorderRadius: resolveBorderRadiusPx(sibling, sibStyle, sibWidth, window),1717hasIconChild: !!iconChild || hasInlineEmojiIcon,1718iconChildWidth: iconWidth,1719});1720}17211722function checkElementItalicSerif(el, style, tag) {1723if (tag !== 'h1' && tag !== 'h2') return [];1724return checkItalicSerif({1725tag,1726fontStyle: style.fontStyle || '',1727fontFamily: style.fontFamily || '',1728fontSize: parseFloat(style.fontSize) || 0,1729headingText: el.textContent || '',1730});1731}17321733function checkElementHeroEyebrow(el, style, tag, window, customPropMap) {1734if (tag !== 'h1') return [];1735const sibling = el.previousElementSibling;1736if (!sibling) return [];1737const sibStyle = window.getComputedStyle(sibling);1738// Resolve Tailwind v4 CSS-variable wrappers (font-weight:var(--font-weight-bold)1739// etc.) before parsing. jsdom returns these verbatim from getComputedStyle;1740// without resolution every style-based gate fails silently on Tailwind v4 builds.1741const fontSizeRaw = customPropMap ? resolveVarRefs(sibStyle.fontSize, customPropMap) : sibStyle.fontSize;1742const fontWeightRaw = customPropMap ? resolveVarRefs(sibStyle.fontWeight, customPropMap) : sibStyle.fontWeight;1743const letterSpacingRaw = customPropMap ? resolveVarRefs(sibStyle.letterSpacing, customPropMap) : sibStyle.letterSpacing;1744const colorRaw = customPropMap ? resolveVarRefs(sibStyle.color, customPropMap) : sibStyle.color;1745const headingFontSizeRaw = customPropMap ? resolveVarRefs(style.fontSize, customPropMap) : style.fontSize;1746const siblingFontSize = parseFloat(fontSizeRaw) || 0;1747// resolveLengthPx returns null for 'normal' / 'auto'; coerce to 0 so the1748// gate falls through cleanly. jsdom returns letter-spacing verbatim1749// (e.g. '0.15em'), unlike real browsers, so this conversion is required.1750return checkHeroEyebrow({1751headingTag: tag,1752headingText: el.textContent || '',1753headingFontSize: parseFloat(headingFontSizeRaw) || 0,1754siblingTag: sibling.tagName.toLowerCase(),1755siblingText: sibling.textContent || '',1756siblingTextTransform: sibStyle.textTransform || '',1757siblingFontSize,1758siblingLetterSpacing: resolveLengthPx(letterSpacingRaw, siblingFontSize) || 0,1759siblingFontWeight: fontWeightRaw || '',1760siblingColor: colorRaw || '',1761});1762}17631764function checkRepeatedSectionKickersFromDoc(doc, win) {1765const candidates = collectRepeatedSectionKickerCandidates(1766doc,1767(el) => win.getComputedStyle(el),1768(value, fontSize) => resolveLengthPx(value, fontSize) || 0,1769);1770return checkRepeatedSectionKickers({ candidates });1771}17721773function checkElementMotion(tag, style) {1774return checkMotion({1775tag,1776transitionProperty: style.transitionProperty || '',1777animationName: style.animationName || '',1778timingFunctions: [style.animationTimingFunction, style.transitionTimingFunction].filter(Boolean).join(' '),1779classList: '',1780});1781}17821783function checkElementGlow(tag, style, effectiveBg) {1784if (!style.boxShadow || style.boxShadow === 'none') return [];1785return checkGlow({ tag, boxShadow: style.boxShadow, effectiveBg });1786}17871788// ─── Section 6: Page-Level Checks ───────────────────────────────────────────17891790// Browser page-level checks — use document/getComputedStyle globals17911792function checkTypography() {1793const findings = [];17941795// Walk actual text-bearing elements and tally font usage by *computed style*.1796// This is much more accurate than scanning CSS rules — it ignores rules that1797// exist in the stylesheet but apply to nothing (e.g. demo classes showing1798// anti-patterns), and counts what the user actually sees.1799const fontUsage = new Map(); // primary font name → count of elements1800let totalTextElements = 0;1801for (const el of document.querySelectorAll('p, h1, h2, h3, h4, h5, h6, li, td, th, dd, blockquote, figcaption, a, button, label, span')) {1802// Skip impeccable's own elements1803if (el.closest && el.closest('.impeccable-overlay, .impeccable-label, .impeccable-banner, .impeccable-tooltip')) continue;1804// Only count elements that actually have visible direct text1805const hasText = [...el.childNodes].some(n => n.nodeType === 3 && n.textContent.trim().length > 0);1806if (!hasText) continue;1807const style = getComputedStyle(el);1808const ff = style.fontFamily;1809if (!ff) continue;1810const stack = ff.split(',').map(f => f.trim().replace(/^['"]|['"]$/g, '').toLowerCase());1811const primary = stack.find(f => f && !GENERIC_FONTS.has(f));1812if (!primary) continue;1813fontUsage.set(primary, (fontUsage.get(primary) || 0) + 1);1814totalTextElements++;1815}18161817if (totalTextElements >= 20) {1818// A font is "primary" if it's used by at least 15% of text elements1819const PRIMARY_THRESHOLD = 0.15;1820for (const [font, count] of fontUsage) {1821const share = count / totalTextElements;1822if (share < PRIMARY_THRESHOLD) continue;1823if (!OVERUSED_FONTS.has(font)) continue;1824if (isBrandFontOnOwnDomain(font)) continue;1825findings.push({ type: 'overused-font', detail: `Primary font: ${font} (${Math.round(share * 100)}% of text)` });1826}18271828// Single-font check: only one distinct primary font across all text1829if (fontUsage.size === 1) {1830const only = [...fontUsage.keys()][0];1831findings.push({ type: 'single-font', detail: `only font used is ${only}` });1832}1833}18341835const sizes = new Set();1836for (const el of document.querySelectorAll('h1,h2,h3,h4,h5,h6,p,span,a,li,td,th,label,button,div')) {1837const fs = parseFloat(getComputedStyle(el).fontSize);1838if (fs > 0 && fs < 200) sizes.add(Math.round(fs * 10) / 10);1839}1840if (sizes.size >= 3) {1841const sorted = [...sizes].sort((a, b) => a - b);1842const ratio = sorted[sorted.length - 1] / sorted[0];1843if (ratio < 2.0) {1844findings.push({ type: 'flat-type-hierarchy', detail: `Sizes: ${sorted.map(s => s + 'px').join(', ')} (ratio ${ratio.toFixed(1)}:1)` });1845}1846}18471848return findings;1849}18501851function isCardLikeDOM(el) {1852const tag = el.tagName.toLowerCase();1853if (SAFE_TAGS.has(tag) || ['input','select','textarea','img','video','canvas','picture'].includes(tag)) return false;1854const style = getComputedStyle(el);1855const cls = el.getAttribute('class') || '';1856const hasShadow = (style.boxShadow && style.boxShadow !== 'none') || /\bshadow(?:-sm|-md|-lg|-xl|-2xl)?\b/.test(cls);1857const hasBorder = /\bborder\b/.test(cls);1858const hasRadius = parseFloat(style.borderRadius) > 0 || /\brounded(?:-sm|-md|-lg|-xl|-2xl|-full)?\b/.test(cls);1859const hasBg = (style.backgroundColor && style.backgroundColor !== 'rgba(0, 0, 0, 0)') || /\bbg-(?:white|gray-\d+|slate-\d+)\b/.test(cls);1860return isCardLikeFromProps(hasShadow, hasBorder, hasRadius, hasBg);1861}18621863function checkLayout() {1864const findings = [];1865const flaggedEls = new Set();18661867for (const el of document.querySelectorAll('*')) {1868if (!isCardLikeDOM(el) || flaggedEls.has(el)) continue;1869const cls = el.getAttribute('class') || '';1870const style = getComputedStyle(el);1871if (style.position === 'absolute' || style.position === 'fixed') continue;1872if (/\b(?:dropdown|popover|tooltip|menu|modal|dialog)\b/i.test(cls)) continue;1873if ((el.textContent?.trim().length || 0) < 10) continue;1874const rect = el.getBoundingClientRect();1875if (rect.width < 50 || rect.height < 30) continue;18761877let parent = el.parentElement;1878while (parent) {1879if (isCardLikeDOM(parent)) { flaggedEls.add(el); break; }1880parent = parent.parentElement;1881}1882}18831884for (const el of flaggedEls) {1885let isAncestor = false;1886for (const other of flaggedEls) {1887if (other !== el && el.contains(other)) { isAncestor = true; break; }1888}1889if (!isAncestor) findings.push({ type: 'nested-cards', detail: 'Card inside card', el });1890}18911892return findings;1893}18941895// Node page-level checks — take document/window as parameters18961897function checkPageTypography(doc, win) {1898const findings = [];18991900const fonts = new Set();1901const overusedFound = new Set();19021903for (const sheet of doc.styleSheets) {1904let rules;1905try { rules = sheet.cssRules || sheet.rules; } catch { continue; }1906if (!rules) continue;1907for (const rule of rules) {1908if (rule.type !== 1) continue;1909const ff = rule.style?.fontFamily;1910if (!ff) continue;1911const stack = ff.split(',').map(f => f.trim().replace(/^['"]|['"]$/g, '').toLowerCase());1912const primary = stack.find(f => f && !GENERIC_FONTS.has(f));1913if (primary) {1914fonts.add(primary);1915if (OVERUSED_FONTS.has(primary)) overusedFound.add(primary);1916}1917}1918}19191920// Check Google Fonts links in HTML1921const html = doc.documentElement?.outerHTML || '';1922const gfRe = /fonts\.googleapis\.com\/css2?\?family=([^&"'\s]+)/gi;1923let m;1924while ((m = gfRe.exec(html)) !== null) {1925const families = m[1].split('|').map(f => f.split(':')[0].replace(/\+/g, ' ').toLowerCase());1926for (const f of families) {1927fonts.add(f);1928if (OVERUSED_FONTS.has(f)) overusedFound.add(f);1929}1930}19311932// Also parse raw HTML/style content for font-family (jsdom may not expose all via CSSOM)1933const ffRe = /font-family\s*:\s*([^;}]+)/gi;1934let fm;1935while ((fm = ffRe.exec(html)) !== null) {1936for (const f of fm[1].split(',').map(f => f.trim().replace(/^['"]|['"]$/g, '').toLowerCase())) {1937if (f && !GENERIC_FONTS.has(f)) {1938fonts.add(f);1939if (OVERUSED_FONTS.has(f)) overusedFound.add(f);1940}1941}1942}19431944for (const font of overusedFound) {1945findings.push({ id: 'overused-font', snippet: `Primary font: ${font}` });1946}19471948// Single font1949if (fonts.size === 1) {1950const els = doc.querySelectorAll('*');1951if (els.length >= 20) {1952findings.push({ id: 'single-font', snippet: `only font used is ${[...fonts][0]}` });1953}1954}19551956// Flat type hierarchy1957const sizes = new Set();1958const textEls = doc.querySelectorAll('h1, h2, h3, h4, h5, h6, p, span, a, li, td, th, label, button, div');1959for (const el of textEls) {1960const fontSize = parseFloat(win.getComputedStyle(el).fontSize);1961// Filter out sub-8px values (jsdom doesn't resolve relative units properly)1962if (fontSize >= 8 && fontSize < 200) sizes.add(Math.round(fontSize * 10) / 10);1963}1964if (sizes.size >= 3) {1965const sorted = [...sizes].sort((a, b) => a - b);1966const ratio = sorted[sorted.length - 1] / sorted[0];1967if (ratio < 2.0) {1968findings.push({ id: 'flat-type-hierarchy', snippet: `Sizes: ${sorted.map(s => s + 'px').join(', ')} (ratio ${ratio.toFixed(1)}:1)` });1969}1970}19711972return findings;1973}19741975function isCardLike(el, win) {1976const tag = el.tagName.toLowerCase();1977if (SAFE_TAGS.has(tag) || ['input', 'select', 'textarea', 'img', 'video', 'canvas', 'picture'].includes(tag)) return false;19781979const style = win.getComputedStyle(el);1980const rawStyle = el.getAttribute?.('style') || '';1981const cls = el.getAttribute?.('class') || '';19821983const hasShadow = (style.boxShadow && style.boxShadow !== 'none') ||1984/\bshadow(?:-sm|-md|-lg|-xl|-2xl)?\b/.test(cls) || /box-shadow/i.test(rawStyle);1985const hasBorder = /\bborder\b/.test(cls);1986const widthPx = parseFloat(style.width) || 0;1987const hasRadius = resolveBorderRadiusPx(el, style, widthPx, win) > 0 ||1988/\brounded(?:-sm|-md|-lg|-xl|-2xl|-full)?\b/.test(cls) || /border-radius/i.test(rawStyle);1989const hasBg = /\bbg-(?:white|gray-\d+|slate-\d+)\b/.test(cls) ||1990/background(?:-color)?\s*:\s*(?!transparent)/i.test(rawStyle);19911992return isCardLikeFromProps(hasShadow, hasBorder, hasRadius, hasBg);1993}19941995function checkPageLayout(doc, win) {1996const findings = [];19971998// Nested cards1999const allEls = doc.querySelectorAll('*');2000const flaggedEls = new Set();2001for (const el of allEls) {2002if (!isCardLike(el, win)) continue;2003if (flaggedEls.has(el)) continue;20042005const tag = el.tagName.toLowerCase();2006const cls = el.getAttribute?.('class') || '';2007const rawStyle = el.getAttribute?.('style') || '';20082009if (['pre', 'code'].includes(tag)) continue;2010if (/\b(?:absolute|fixed)\b/.test(cls) || /position\s*:\s*(?:absolute|fixed)/i.test(rawStyle)) continue;2011if ((el.textContent?.trim().length || 0) < 10) continue;2012if (/\b(?:dropdown|popover|tooltip|menu|modal|dialog)\b/i.test(cls)) continue;20132014// Walk up to find card-like ancestor2015let parent = el.parentElement;2016while (parent) {2017if (isCardLike(parent, win)) {2018flaggedEls.add(el);2019break;2020}2021parent = parent.parentElement;2022}2023}20242025// Only report innermost nested cards2026for (const el of flaggedEls) {2027let isAncestorOfFlagged = false;2028for (const other of flaggedEls) {2029if (other !== el && el.contains(other)) {2030isAncestorOfFlagged = true;2031break;2032}2033}2034if (!isAncestorOfFlagged) {2035findings.push({ id: 'nested-cards', snippet: `Card inside card (${el.tagName.toLowerCase()})` });2036}2037}20382039return findings;2040}20412042// ─── Cream / beige palette (the default "tasteful" AI surface) ────────────────2043// A warm, lightly-tinted off-white page background — light, with R≥G≥B and a2044// small warm tint (not white, not a strong color). The current reflex surface.2045function isCreamColor(rgb) {2046if (!rgb) return false;2047const { r, g, b } = rgb;2048if (Math.min(r, g, b) < 209) return false; // must be light2049if (!(r >= g && g >= b)) return false; // warm ordering2050const warmth = r - b;2051return warmth >= 6 && warmth <= 48; // tinted, not white, not strong2052}20532054// Tailwind background utilities that render as a warm off-white surface. The2055// static engine doesn't fetch Tailwind's CSS, so a `bg-amber-50` on <body>2056// resolves to nothing in computed style — catch it from the class list2057// instead. Candidate tokens map to their actual Tailwind hex and are still2058// filtered through isCreamColor, so neutral grays (stone) and over-saturated2059// shades drop out on their own.2060const TAILWIND_BG_HEX = {2061'bg-amber-50': '#fffbeb', 'bg-amber-100': '#fef3c7',2062'bg-orange-50': '#fff7ed', 'bg-orange-100': '#ffedd5',2063'bg-yellow-50': '#fefce8',2064'bg-stone-50': '#fafaf9', 'bg-stone-100': '#f5f5f4', 'bg-stone-200': '#e7e5e4',2065};20662067function creamFromClassList(cls) {2068if (!cls) return null;2069// Arbitrary value: bg-[#f5f0e6] / bg-[rgb(245_240_230)] (underscores = spaces).2070const arb = cls.match(/\bbg-\[([^\]]+)\]/);2071if (arb && isCreamColor(parseAnyColor(arb[1].replace(/_/g, ' ')))) return `bg-[${arb[1]}]`;2072// Named warm-light utilities.2073for (const [tok, hex] of Object.entries(TAILWIND_BG_HEX)) {2074if (new RegExp(`(^|\\s)${tok}($|\\s)`).test(cls) && isCreamColor(parseAnyColor(hex))) return tok;2075}2076return null;2077}20782079function checkCreamPalette(doc, win) {2080const findings = [];2081const body = doc.body || (doc.querySelector ? doc.querySelector('body') : null);2082if (!body) return findings;2083const html = doc.documentElement;2084const getCS = (el) => (win ? win.getComputedStyle(el) : getComputedStyle(el));20852086// 1. Computed background — covers inline / <style> / linked CSS, and Tailwind2087// once it's actually rendered (browser path).2088let bg = readOwnBackgroundColor(body, getCS(body));2089if (!bg || bg.a === 0) {2090if (html) bg = readOwnBackgroundColor(html, getCS(html));2091}2092if (isCreamColor(bg)) {2093findings.push({ id: 'cream-palette', snippet: `cream/beige page background rgb(${bg.r}, ${bg.g}, ${bg.b})` });2094return findings;2095}20962097// 2. Tailwind class fallback — for the static path, where utility classes2098// never resolve to computed CSS.2099for (const el of [body, html]) {2100const tok = creamFromClassList(el && el.getAttribute ? el.getAttribute('class') : '');2101if (tok) {2102findings.push({ id: 'cream-palette', snippet: `cream/beige page background (Tailwind ${tok})` });2103break;2104}2105}2106return findings;2107}21082109// ─── Oversized hero headline ────────────────────────────────────────────────2110// Fires when a *long* headline is set at display size, so a full sentence ends2111// up dominating the viewport. A punchy one- or two-word headline at the same2112// size is a legitimate stylistic choice and must pass — length, not size2113// alone, is the tell.2114const OVERSIZED_H1_FONT_PX = 72;2115const OVERSIZED_H1_MIN_CHARS = 40;2116function checkOversizedH1({ tag, fontSize, headingText }) {2117if (tag !== 'h1') return [];2118const textLen = headingText.length;2119if (fontSize >= OVERSIZED_H1_FONT_PX && textLen >= OVERSIZED_H1_MIN_CHARS) {2120return [{ id: 'oversized-h1', snippet: `${Math.round(fontSize)}px h1, ${textLen} chars "${headingText.slice(0, 60)}"` }];2121}2122return [];2123}21242125function checkElementOversizedH1(el, style, tag, window) {2126if (tag !== 'h1') return [];2127const fontSize = resolveFontSizePx(el, window);2128const headingText = (el.textContent || '').trim().replace(/\s+/g, ' ');2129return checkOversizedH1({ tag, fontSize, headingText });2130}21312132function checkElementOversizedH1DOM(el) {2133const tag = el.tagName.toLowerCase();2134if (tag !== 'h1') return [];2135const style = getComputedStyle(el);2136const fontSize = parseFloat(style.fontSize) || 0;2137const headingText = (el.textContent || '').trim().replace(/\s+/g, ' ');2138return checkOversizedH1({ tag, fontSize, headingText });2139}21402141// ─── GPT tell: hairline border + wide diffuse shadow (gated --gpt) ────────────2142function shadowMaxBlurPx(boxShadow) {2143if (!boxShadow || boxShadow === 'none') return 0;2144let maxBlur = 0;2145// Split into layers on commas not inside parentheses (rgba(...) etc.).2146for (const layer of boxShadow.split(/,(?![^()]*\))/)) {2147// Strip colors and keywords (rgba()/hsl()/hex/named/inset/px), leaving the2148// ordered length tokens: offsetX offsetY blur [spread]. Static jsdom keeps2149// unitless zeros ("0 0 24px"); browsers normalize to px ("0px 0px 24px") —2150// both reduce to the same numbers here.2151const cleaned = layer.replace(/rgba?\([^)]*\)|hsla?\([^)]*\)|#[0-9a-f]+|\b[a-z]+\b/gi, ' ');2152const nums = [...cleaned.matchAll(/-?\d*\.?\d+/g)].map(m => parseFloat(m[0]));2153if (nums.length >= 3) maxBlur = Math.max(maxBlur, nums[2]);2154}2155return maxBlur;2156}21572158function checkGptThinBorderWideShadow({ borderWidths, boxShadow }) {2159const maxBorder = Math.max(0, ...borderWidths);2160const hasThinBorder = maxBorder > 0 && maxBorder <= 1.5;2161const blur = shadowMaxBlurPx(boxShadow);2162if (hasThinBorder && blur >= 16) {2163return [{ id: 'gpt-thin-border-wide-shadow', snippet: `${maxBorder}px border + ${Math.round(blur)}px shadow blur` }];2164}2165return [];2166}21672168function borderWidthsFromStyle(style) {2169return [2170parseFloat(style.borderTopWidth) || 0,2171parseFloat(style.borderRightWidth) || 0,2172parseFloat(style.borderBottomWidth) || 0,2173parseFloat(style.borderLeftWidth) || 0,2174];2175}21762177function checkElementGptBorderShadow(el, style) {2178return checkGptThinBorderWideShadow({ borderWidths: borderWidthsFromStyle(style), boxShadow: style.boxShadow || '' });2179}21802181function checkElementGptBorderShadowDOM(el) {2182const style = getComputedStyle(el);2183return checkGptThinBorderWideShadow({ borderWidths: borderWidthsFromStyle(style), boxShadow: style.boxShadow || '' });2184}21852186// ─── Clipped overflow container ───────────────────────────────────────────────2187// A clipping container (overflow hidden/clip, not a scroll region) wrapping an2188// absolutely/fixed-positioned descendant clips popovers/menus that must escape.2189function classSelector(el) {2190const cls = (el.getAttribute ? el.getAttribute('class') : el.className) || '';2191const tokens = String(cls).trim().split(/\s+/).filter(Boolean);2192const tag = el.tagName ? el.tagName.toLowerCase() : 'el';2193return tokens.length ? `${tag}.${tokens.join('.')}` : tag;2194}21952196function checkClippedOverflow(el, style, getStyle) {2197const clips = (v) => v === 'hidden' || v === 'clip';2198const scrolls = (v) => v === 'auto' || v === 'scroll';2199const ox = style.overflowX || '', oy = style.overflowY || '', ov = style.overflow || '';2200const anyClip = clips(ox) || clips(oy) || clips(ov);2201const anyScroll = scrolls(ox) || scrolls(oy) || scrolls(ov);2202if (!anyClip || anyScroll) return [];2203if (!el.querySelectorAll) return [];2204for (const child of el.querySelectorAll('*')) {2205const pos = (getStyle(child).position) || '';2206if (pos === 'absolute' || pos === 'fixed') {2207return [{ id: 'clipped-overflow-container', snippet: `${classSelector(el)} clips a positioned child` }];2208}2209}2210return [];2211}22122213function checkElementClippedOverflow(el, style, tag, window) {2214return checkClippedOverflow(el, style, (n) => window.getComputedStyle(n));2215}22162217function checkElementClippedOverflowDOM(el) {2218const style = getComputedStyle(el);2219return checkClippedOverflow(el, style, (n) => getComputedStyle(n));2220}22212222// ─── Text overflow (browser-only: needs scrollWidth/clientWidth) ──────────────2223const TEXT_OVERFLOW_SKIP_TAGS = new Set(['pre', 'code', 'textarea', 'svg', 'canvas', 'select', 'option', 'marquee']);22242225function metricLengthPx(value, fontSizePx = 16) {2226if (typeof value === 'number' && Number.isFinite(value)) return value;2227if (typeof value !== 'string') return null;2228return resolveLengthPx(value, fontSizePx);2229}22302231function firstMetricLengthPx(fontSizePx, ...values) {2232for (const value of values) {2233const parsed = metricLengthPx(value, fontSizePx);2234if (parsed !== null) return parsed;2235}2236return null;2237}22382239function expandBoxShorthand(parts) {2240if (parts.length === 1) return [parts[0], parts[0], parts[0], parts[0]];2241if (parts.length === 2) return [parts[0], parts[1], parts[0], parts[1]];2242if (parts.length === 3) return [parts[0], parts[1], parts[2], parts[1]];2243return [parts[0], parts[1], parts[2], parts[3]];2244}22452246function clippedByInset(clipPath) {2247const match = String(clipPath || '').trim().toLowerCase().match(/^inset\s*\(([^)]*)\)$/);2248if (!match) return false;2249const beforeRound = match[1].split(/\s+round\s+/)[0].trim();2250if (!beforeRound) return false;2251const values = expandBoxShorthand(beforeRound.split(/\s+/).slice(0, 4));2252const percents = values.map(value => String(value).trim().match(/^(-?\d+(?:\.\d+)?)%$/));2253if (percents.some(match => !match)) return false;2254const [top, right, bottom, left] = percents.map(match => parseFloat(match[1]));2255return top + bottom >= 100 || left + right >= 100;2256}22572258function clippedByRect(clip) {2259const match = String(clip || '').trim().toLowerCase().match(/^rect\s*\(([^)]*)\)$/);2260if (!match) return false;2261const values = match[1].split(/[,\s]+/).map(value => value.trim()).filter(Boolean);2262if (values.length !== 4) return false;2263const [top, right, bottom, left] = values.map(value => metricLengthPx(value, 16));2264if ([top, right, bottom, left].some(value => value === null)) return false;2265return bottom <= top || right <= left;2266}22672268function isScreenReaderOnlyTextStyle(style, metrics = {}) {2269if (!style) return false;2270const overflowValues = [style.overflow, style.overflowX, style.overflowY]2271.map(value => String(value || '').toLowerCase());2272const clipsOverflow = overflowValues.some(value => value === 'hidden' || value === 'clip');22732274const fontSize = metricLengthPx(style.fontSize, 16) || 16;2275const width = firstMetricLengthPx(fontSize, metrics.width, metrics.clientWidth, style.width, style.inlineSize);2276const height = firstMetricLengthPx(fontSize, metrics.height, metrics.clientHeight, style.height, style.blockSize);2277const isTiny = width !== null && height !== null && width <= 2 && height <= 2;2278const isAbsolutelyHidden = String(style.position || '').toLowerCase() === 'absolute' && isTiny && clipsOverflow;22792280const clipPath = String(style.clipPath || style.webkitClipPath || '').trim();2281const clip = String(style.clip || '').trim();2282return isAbsolutelyHidden || clippedByInset(clipPath) || clippedByRect(clip);2283}22842285function checkElementTextOverflowDOM(el) {2286const tag = el.tagName.toLowerCase();2287if (TEXT_OVERFLOW_SKIP_TAGS.has(tag)) return [];2288// Only the element that actually owns overflowing text — not its ancestors,2289// which inherit a wider scrollWidth from the spilling descendant.2290const hasDirectText = [...el.childNodes].some(n => n.nodeType === 3 && n.textContent.trim().length > 0);2291if (!hasDirectText) return [];2292const style = getComputedStyle(el);2293const rect = el.getBoundingClientRect ? el.getBoundingClientRect() : null;2294if (isScreenReaderOnlyTextStyle(style, {2295width: rect?.width,2296height: rect?.height,2297clientWidth: el.clientWidth,2298clientHeight: el.clientHeight,2299})) return [];2300const isScrollRegion = (s) => /(auto|scroll)/.test(s.overflowX || '') || /(auto|scroll)/.test(s.overflow || '');2301if (isScrollRegion(style)) return [];2302// A scrollable ancestor means this overflow is intentional and scrollable.2303for (let p = el.parentElement; p; p = p.parentElement) {2304if (isScrollRegion(getComputedStyle(p))) return [];2305}2306const delta = el.scrollWidth - el.clientWidth;2307if (el.clientWidth > 0 && delta >= 16) {2308return [{ id: 'text-overflow', snippet: `${classSelector(el)} overflows its box by ${Math.round(delta)}px` }];2309}2310return [];2311}23122313export {2314checkBorders,2315isEmojiOnlyText,2316checkColors,2317isCardLikeFromProps,2318checkIconTile,2319resolveSerif,2320checkItalicSerif,2321isAccentColor,2322checkHeroEyebrow,2323checkRepeatedSectionKickers,2324checkMotion,2325checkGlow,2326checkHtmlPatterns,2327readOwnBackgroundColor,2328resolveBackground,2329resolveGradientStops,2330parseRadiusToPx,2331resolveBorderRadiusPx,2332checkElementBordersDOM,2333checkElementColorsDOM,2334checkElementIconTileDOM,2335checkElementItalicSerifDOM,2336checkElementHeroEyebrowDOM,2337buildCustomPropMap,2338resolveVarRefs,2339oklchToRgb,2340parseAnyColor,2341parseColorResolved,2342cleanInlineText,2343isRepeatedKickerCandidate,2344collectRepeatedSectionKickerCandidates,2345checkRepeatedSectionKickersDOM,2346checkElementMotionDOM,2347checkElementGlowDOM,2348checkElementAIPaletteDOM,2349resolveFontSizePx,2350resolveLengthPx,2351checkQuality,2352checkElementQualityDOM,2353checkPageQualityFromDoc,2354checkPageQualityDOM,2355checkElementQuality,2356checkElementBorders,2357checkElementColors,2358checkElementIconTile,2359checkElementItalicSerif,2360checkElementHeroEyebrow,2361checkRepeatedSectionKickersFromDoc,2362checkElementMotion,2363checkElementGlow,2364checkTypography,2365isCardLikeDOM,2366checkLayout,2367checkPageTypography,2368isCardLike,2369checkPageLayout,2370isCreamColor,2371checkCreamPalette,2372checkOversizedH1,2373checkElementOversizedH1,2374checkElementOversizedH1DOM,2375shadowMaxBlurPx,2376checkGptThinBorderWideShadow,2377checkElementGptBorderShadow,2378checkElementGptBorderShadowDOM,2379checkClippedOverflow,2380checkElementClippedOverflow,2381checkElementClippedOverflowDOM,2382isScreenReaderOnlyTextStyle,2383checkElementTextOverflowDOM,2384};2385