audit-page.js 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657
  1. (async () => {
  2. // In-page design audit: one expression resolving to a report. Evaluate the file as is, or, where a tool only takes a function,
  3. // as `() => <file>` or `return (<file>)`. Without audit.mjs, run it at 1440 and 390 with no baseline.
  4. // mustFix: measured defects. advisory: heuristics to look at, not to obey; equal items (pricing tiers, galleries, options, tags) are legitimate.
  5. // Visually hidden (sr-only) text and folded text (clipped away by an ancestor, off-screen drawers, tooltips waiting for a trigger)
  6. // are skipped. Painted text is measured even under [aria-hidden="true"] or [data-audit-skip] — both mark intent, not pixels, so they
  7. // mute only the heuristics that read intent (textWall, stretchedBox, leftEdge, transitions) and never the echo family, where the
  8. // marker is the evidence; aria-hidden text is exempt from contrast alone, which is the one check WCAG itself exempts for text
  9. // removed from the accessibility tree. out.suppressed reports every marker honoured, with counts and selectors.
  10. // Media that spans the whole viewport width is read as full-bleed and keeps no gutter; [data-full-bleed] frees media that
  11. // bleeds off one edge only, and in neither case its text.
  12. // Reading surfaces (<article>, [data-reading]) are exempt from textWall.
  13. // It first scrolls the page through once (as audit.mjs does) so scroll-triggered reveals fire; prescrolled says whether it did.
  14. // Each key has count, up to 5 samples and sigs (signature -> count). window.__designAuditFull = true adds every hit as
  15. // [sig, message, contrast ratio?] in items. A fixed canvas (poster, invoice, infinite canvas): set window.__designAuditCanvas =
  16. // "<its selector>" first; a deck: window.__designAuditDeck = true and evaluate once per slide with __designAuditCanvas naming it.
  17. const wait = (ms) => new Promise((r) => setTimeout(r, ms));
  18. let prescrolled = !!window.__designAuditPrescrolled;
  19. if (!prescrolled) {
  20. // Follows the page as it grows (late async loads included), up to 50 screens, so an infinite feed cannot keep this running.
  21. // Content never in view by the end (past 50 screens, or arriving after the last check) marks the scan partial.
  22. const high = () => Math.min(document.documentElement.scrollHeight, innerHeight * 50);
  23. let y = 0, seen = 0;
  24. do { for (; y < high(); y += innerHeight / 2) { scrollTo(0, y); seen = Math.max(seen, Math.min(y + innerHeight, document.documentElement.scrollHeight)); await wait(60); } await wait(400); } while (y < high());
  25. // The browser clamps scrollTo at the bottom, so judge by where the viewport bottom really reached, not by y.
  26. // Still at the bottom: a page taller than that hit the 50-screen cap.
  27. const end = document.documentElement.scrollHeight;
  28. let capped = end > seen + 1;
  29. scrollTo(0, 0); await wait(800);
  30. // Taller at the top can be a header that collapses on scroll, not new content: measure again at the bottom,
  31. // in the same scroll state as end, and only real growth there marks the scan partial.
  32. if (!capped && document.documentElement.scrollHeight > end + 1) {
  33. scrollTo(0, end); await wait(400);
  34. capped = document.documentElement.scrollHeight > end + 1;
  35. scrollTo(0, 0); await wait(800);
  36. }
  37. window.__designAuditScrollCapped = capped;
  38. for (const a of document.getAnimations()) { try { if (a.effect?.getTiming().iterations !== Infinity) a.finish(); } catch {} }
  39. await document.fonts.ready;
  40. window.__designAuditPrescrolled = prescrolled = true;
  41. }
  42. const vw = document.documentElement.clientWidth, vh = window.innerHeight, y0 = window.scrollY;
  43. const cv = document.createElement("canvas").getContext("2d", { willReadFrequently: true });
  44. const cache = new Map();
  45. const rgba = (c) => {
  46. if (cache.has(c)) return cache.get(c);
  47. cv.clearRect(0, 0, 1, 1); cv.fillStyle = "#000"; cv.fillStyle = c; cv.fillRect(0, 0, 1, 1);
  48. const [r, g, b, a] = cv.getImageData(0, 0, 1, 1).data, v = [r, g, b, a / 255];
  49. cache.set(c, v); return v;
  50. };
  51. const over = (f, b) => [0, 1, 2].map((i) => f[i] * f[3] + b[i] * (1 - f[3])).concat(1);
  52. const lum = (c) => { const l = c.slice(0, 3).map((v) => { v /= 255; return v <= 0.03928 ? v / 12.92 : ((v + 0.055) / 1.055) ** 2.4; }); return 0.2126 * l[0] + 0.7152 * l[1] + 0.0722 * l[2]; };
  53. const ratio = (a, b) => { const x = lum(a), y = lum(b); return (Math.max(x, y) + 0.05) / (Math.min(x, y) + 0.05); };
  54. const cs = (el, p) => getComputedStyle(el, p);
  55. const absY = (r) => r.top + window.scrollY;
  56. const sel = (el) => { const c = (el.getAttribute("class") || "").trim().split(/\s+/)[0]; return el.tagName.toLowerCase() + (c ? "." + c : ""); };
  57. const label = (el, r = el.getBoundingClientRect()) => `${sel(el)} "${(el.textContent || "").trim().replace(/\s+/g, " ").slice(0, 28)}" @${Math.round(r.left)},${Math.round(absY(r))}`;
  58. // Core text: running copy the reader has to read (a sentence or more at reading size) and a control's own label. A size or
  59. // contrast hit on those is the deliverable's to fix even when a baseline has the same one, so the signature carries "core"
  60. // and audit.mjs never inherits it; a skeleton's decorative typesetting, eyebrows, chrome and code blocks still inherit. Icon
  61. // glyphs, carets and shortcut badges inside a control are not its label. The text inside a block card link is the item itself.
  62. const LABELLED = 'a[href],button,summary,input,select,textarea,label,[role="button"],[role="link"],[role="menuitem"],[role="tab"],[role="option"],[role="switch"],[role="checkbox"]';
  63. const core = (el, text, fs) => {
  64. if (!/[\p{L}\p{N}]/u.test(text)) return "";
  65. if (text.length >= 60 && fs <= 20 && !el.closest("pre,code,kbd,samp")) return "|core";
  66. const ctl = el.closest(LABELLED);
  67. if (!ctl || el.closest("kbd")) return "";
  68. const cardLink = ctl.tagName === "A" && !/^inline$/.test(cs(ctl).display) && !!ctl.querySelector("h1,h2,h3,h4,h5,h6,p,ul,ol,dl,figure,blockquote");
  69. return cardLink ? "" : "|core";
  70. };
  71. const opacityOf = (el) => { let o = 1; for (let e = el; e && e.nodeType === 1; e = e.parentElement) o *= +cs(e).opacity; return o; };
  72. const closed = (el) => { const d = el.closest("details:not([open])"); return d && !el.closest("summary"); };
  73. // Visually hidden accessible text (sr-only: 1px clipped box, clip rect(0...), clip-path inset(50%)) is not painted text.
  74. const srOnly = (el) => { for (let e = el; e && e !== document.body; e = e.parentElement) { const s = cs(e), r = e.getBoundingClientRect(); if (/^rect\(0/.test(s.clip) || /inset\(50%/.test(s.clipPath) || (r.width <= 1 && r.height <= 1 && /hidden|clip/.test(s.overflowX))) return true; } return false; };
  75. const shown = (el) => { const r = el.getBoundingClientRect(), s = cs(el); return r.width > 0 && r.height > 0 && s.visibility !== "hidden" && opacityOf(el) > 0.1 && !closed(el) && !srOnly(el); };
  76. const norm = (t) => t.toLowerCase().replace(/[\s\p{P}]+/gu, " ").trim();
  77. const CJK = /[぀-ヿ㐀-鿿가-힯]/;
  78. const EMOJI = /^(?:\p{Extended_Pictographic}|[‍️⃣]|[\u{1F1E6}-\u{1F1FF}\u{1F3FB}-\u{1F3FF}]|\s)+$/u;
  79. const out = { url: location.href, viewport: `${vw}x${vh}`, prescrolled, scrollCapped: !!window.__designAuditScrollCapped, mustFix: {}, advisory: {}, unverifiedContrast: 0 };
  80. const SLIDE = ".slide,[data-slide],[data-screen-label],[data-slide-index],.reveal .slides>section,.swiper-slide";
  81. const canvas = window.__designAuditCanvas && document.querySelector(window.__designAuditCanvas);
  82. // Class-only slides (.slide, .swiper-slide) make a deck only when roughly viewport-sized; carousels stay pages.
  83. const isDeck = !!window.__designAuditDeck || [...document.querySelectorAll(SLIDE)].filter((s) => !s.parentElement?.closest(SLIDE) && (!s.matches(".slide,.swiper-slide") || (s.offsetWidth >= vw * 0.85 && s.offsetHeight >= vh * 0.7))).length > 1;
  84. const add = (bucket, key, sig, msg, v) => { (out[bucket][key] ||= []).push([`${key}|${sig}`, msg, ...(v === undefined ? [] : [v])]); };
  85. // Text owners: elements with their own visible text node, with that text's line boxes.
  86. const walker = document.createTreeWalker(document.body, NodeFilter.SHOW_TEXT, { acceptNode: (n) => n.data.trim() && !n.parentElement.closest("script,style,noscript,template") ? 1 : 2 });
  87. const texts = [], own = new Map();
  88. const invisible = [];
  89. // Tooltips and popovers waiting for a trigger: positioned, pointer-events:none, and role=tooltip, named by aria-describedby or
  90. // revealed by a :hover/:focus rule. Any other opacity-0 text is a reveal that never ran.
  91. const reveal = [];
  92. for (const sh of document.styleSheets) { try { for (const r of sh.cssRules) if (/:(hover|focus)/.test(r.selectorText)) reveal.push(r.selectorText.replace(/:(hover|focus-within|focus-visible|focus)\b/g, "")); } catch {} }
  93. const tip = (el) => {
  94. let pos = false, trig = false;
  95. for (let e = el; e && e !== document.body; e = e.parentElement) {
  96. pos ||= /absolute|fixed/.test(cs(e).position);
  97. trig ||= e.getAttribute("role") === "tooltip" || (!!e.id && !!document.querySelector(`[aria-describedby~="${CSS.escape(e.id)}"]`)) || reveal.some((q) => { try { return e.matches(q); } catch { return false; } });
  98. }
  99. return cs(el).pointerEvents === "none" && pos && trig;
  100. };
  101. const fixedIn = (el) => { for (let e = el; e && e !== document.body; e = e.parentElement) if (cs(e).position === "fixed") return true; return false; };
  102. const clipsX = (el) => { for (let e = el.parentElement; e; e = e.parentElement) if (/hidden|clip/.test(cs(e).overflowX)) return true; return false; };
  103. const scrollerIn = (el) => { for (let e = el.parentElement; e && e !== document.body; e = e.parentElement) if (/auto|scroll/.test(cs(e).overflowX)) return true; return false; };
  104. for (let n; (n = walker.nextNode()); ) {
  105. const el = n.parentElement;
  106. if (!shown(el)) {
  107. const r = el.getBoundingClientRect();
  108. if (r.width > 0 && r.height > 0 && cs(el).visibility !== "hidden" && !closed(el) && !srOnly(el) && !tip(el) && !invisible.includes(el)) invisible.push(el);
  109. continue;
  110. }
  111. const range = document.createRange(); range.selectNodeContents(n);
  112. const rects = [...range.getClientRects()].filter((r) => r.width > 0.5 && r.height > 0.5);
  113. if (!rects.length) continue;
  114. const s = cs(el), lh = el instanceof SVGElement || s.lineHeight === "normal" ? Infinity : parseFloat(s.lineHeight);
  115. // Line boxes: glyph rects trimmed to the line height, so tight display type is not read as overlap.
  116. // Clipped by an overflow:hidden/clip ancestor (ellipsis cells, masks): only the painted part can collide.
  117. let clip = { left: -Infinity, right: Infinity, top: -Infinity, bottom: Infinity };
  118. for (let e = el; e && e !== document.body; e = e.parentElement) {
  119. const es = cs(e);
  120. if (/hidden|clip/.test(es.overflowX + es.overflowY)) { const er = e.getBoundingClientRect(); clip = { left: Math.max(clip.left, er.left), right: Math.min(clip.right, er.right), top: Math.max(clip.top, er.top), bottom: Math.min(clip.bottom, er.bottom) }; }
  121. }
  122. const lines = rects.map((r) => {
  123. const top = lh < r.height ? r.top + (r.height - lh) / 2 : r.top, bottom = lh < r.height ? r.bottom - (r.height - lh) / 2 : r.bottom;
  124. const b = { left: Math.max(r.left, clip.left), right: Math.min(r.right, clip.right), top: Math.max(top, clip.top), bottom: Math.min(bottom, clip.bottom) };
  125. return { ...b, width: Math.max(0, b.right - b.left), height: Math.max(0, b.bottom - b.top) };
  126. });
  127. const box = range.getBoundingClientRect();
  128. // Folded: clipped away by an ancestor (max-height:0 accordion, carousel slide) or entirely off-screen (transformed drawer).
  129. if (lines.every((l) => l.width <= 0 || l.height <= 0)) continue;
  130. // Past the side only when clipped (by an ancestor or a fixed drawer) or in a scroller; text pushed out by real overflow is measured.
  131. if ((((box.right <= 0 || box.left >= vw) && (clipsX(el) || fixedIn(el))) || ((box.bottom <= 0 || box.top >= vh) && fixedIn(el))) && !scrollerIn(el)) continue;
  132. texts.push({ n, el, rects, lines, box });
  133. own.set(el, (own.get(el) || []).concat({ n, rects }));
  134. }
  135. const owners = [...own.keys()];
  136. for (const el of invisible) add("advisory", "invisibleText", sel(el), `opacity ${opacityOf(el).toFixed(2)} ${label(el)}`);
  137. const scaleOf = (el) => { const m = el instanceof SVGGraphicsElement && el.getScreenCTM(); return m ? Math.hypot(m.a, m.b) : 1; };
  138. for (const el of owners) {
  139. const s = cs(el), isSvg = el instanceof SVGElement, fs = parseFloat(s.fontSize) * (isSvg ? scaleOf(el) : 1), text = el.textContent.trim();
  140. const px = Math.round(fs * 10) / 10;
  141. // An 11px eyebrow (short, uppercase or letter-spaced) is advisory; under 11px, or 11px copy, must be fixed. sup/sub are exempt.
  142. // A short count badge ("3", "12+") of 9px or more is advisory too.
  143. const eyebrow = (fs >= 11 && text.length <= 40 && (s.textTransform === "uppercase" || parseFloat(s.letterSpacing) >= fs * 0.05)) || (fs >= 9 && /^\d{1,3}\+?$/.test(text));
  144. if (el.closest("sup,sub")) {} else if (fs < 12 && eyebrow) add("advisory", "smallLabel", `${sel(el)}|${px}`, `${px}px ${label(el)}`);
  145. else if (fs < 12 || (fs < 14 && text.length > 80)) add("mustFix", "smallText", `${sel(el)}|${px}${core(el, text, fs)}`, `${px}px ${label(el)}`);
  146. if (!isSvg) {
  147. const lh = s.lineHeight === "normal" ? fs * 1.2 : parseFloat(s.lineHeight), cjk = CJK.test(text), tops = [];
  148. for (const t of own.get(el)) for (const r of t.rects) if (!tops.some((y) => Math.abs(y - r.top) < fs / 2)) tops.push(r.top);
  149. // Under 18px: 1.35 (1.5 for CJK); 18-20px headings and lead text: 1.25; larger display type is not checked.
  150. if (tops.length >= 2 && fs <= 20 && lh / fs < (fs < 18 ? (cjk ? 1.5 : 1.35) : 1.25)) add("mustFix", "tightLeading", `${sel(el)}|${fs < 18 ? "body" : "lead"}`, `${(lh / fs).toFixed(2)} ${fs}px ${label(el)}`);
  151. // Truncated: an ellipsis or line clamp that cuts text, or overflow hidden hiding real content (not glyph overhang of tight display type).
  152. const clamp = s.webkitLineClamp && s.webkitLineClamp !== "none", hidX = el.scrollWidth - el.clientWidth, hidY = el.scrollHeight - el.clientHeight;
  153. // Reachable: a title or aria-label holding the full text on it or a near ancestor: not a hit. A separate link or button beside it
  154. // in a small parent (an "Open" link in a card with a clamped summary) probably reaches it: advisory. A control's own label: must-fix.
  155. if ((s.textOverflow === "ellipsis" && hidX > 1) || (clamp && hidY > 2) || (/hidden|clip/.test(s.overflowX + s.overflowY) && (hidX > fs / 2 || hidY > lh / 2))) {
  156. const full = norm(text), ctl = el.closest('a[href],button,summary,[role=button],[role=link],[role=menuitem],[role=tab],[role=option]');
  157. // A block-level card link (an <a href> holding a heading or other blocks) opens the full item: its text is content, not a label.
  158. const card = !!ctl && ctl !== el && ctl.tagName === "A" && !/^inline$/.test(cs(ctl).display) && !!ctl.querySelector("h1,h2,h3,h4,h5,h6,p,ul,ol,dl,figure,img,picture,blockquote,div,section,article");
  159. let reach = false, near = card;
  160. for (let e = el, i = 0; e && e !== document.body && i < 4; e = e.parentElement, i++) if (["title", "aria-label"].some((a) => norm(e.getAttribute(a) || "").includes(full))) reach = true;
  161. const p = (ctl || el).parentElement;
  162. if (p && p.children.length <= 6 && !/^(MAIN|SECTION|ARTICLE|BODY)$/.test(p.tagName) && [...p.querySelectorAll("a[href],button")].some((c) => !c.contains(el))) near = true;
  163. if (!reach) add(near ? "advisory" : "mustFix", "truncated", sel(el), label(el));
  164. }
  165. if (cjk && /break-all/.test(s.wordBreak)) add("mustFix", "cjkBreak", `${sel(el)}|break-all`, `word-break:break-all ${label(el)}`);
  166. }
  167. }
  168. // CJK: a line starting with closing punctuation is a defect; a short orphaned last line in a heading is advisory.
  169. for (const t of texts) {
  170. if (!CJK.test(t.n.data) || t.rects.length < 2) continue;
  171. const fs = parseFloat(cs(t.el).fontSize), lines = [];
  172. for (const r of t.rects) { const l = lines.find((x) => Math.abs(x.top - r.top) < fs / 2); l ? (l.w += r.width) : lines.push({ top: r.top, w: r.width }); }
  173. if (lines.length > 1 && fs >= 18 && lines[lines.length - 1].w < fs * 2.5) add("advisory", "cjkOrphan", sel(t.el), label(t.el));
  174. const range = document.createRange(), p = document.createRange();
  175. for (let i = 1; i < t.n.data.length; i++) {
  176. if (!/[,。、;:!?)」』》,.;:!?)]/.test(t.n.data[i])) continue;
  177. range.setStart(t.n, i); range.setEnd(t.n, i + 1); p.setStart(t.n, i - 1); p.setEnd(t.n, i);
  178. if (range.getBoundingClientRect().top - p.getBoundingClientRect().top > fs / 2) { add("mustFix", "cjkBreak", `${sel(t.el)}|punct`, `line starts with "${t.n.data[i]}" ${label(t.el)}`); break; }
  179. }
  180. }
  181. // Colliding text: glyph boxes of different owners intersecting (chart labels, overlaid copy).
  182. const inter = (a, b) => Math.max(0, Math.min(a.right, b.right) - Math.max(a.left, b.left)) * Math.max(0, Math.min(a.bottom, b.bottom) - Math.max(a.top, b.top));
  183. const seen = new Set();
  184. for (let i = 0; i < texts.length; i++) for (let j = i + 1; j < texts.length; j++) {
  185. const a = texts[i], b = texts[j];
  186. if (a.el === b.el || a.el.contains(b.el) || b.el.contains(a.el) || !inter(a.box, b.box) || a.box.right <= 0 || a.box.left >= vw) continue;
  187. if (a.lines.some((ra) => b.lines.some((rb) => inter(ra, rb) > 0.15 * Math.min(ra.width * ra.height, rb.width * rb.height)))) {
  188. const k = label(a.el) + label(b.el);
  189. if (!seen.has(k)) { seen.add(k); add("mustFix", "textCollision", `${sel(a.el)}|${sel(b.el)}`, `${label(a.el)} x ${label(b.el)}`); }
  190. }
  191. }
  192. // Gutters: text, controls and media keep 16px (mobile) / 24px (desktop) from the viewport edges. Horizontal scrollers are exempt;
  193. // inside a full-height sidebar or nav rail at the edge, 16px applies. On a fixed canvas, content stays inside the canvas instead.
  194. const gutterOf = (el) => {
  195. if (vw < 768) return 16;
  196. for (let e = el; e && e !== document.body; e = e.parentElement) {
  197. const r = e.getBoundingClientRect(), s = cs(e);
  198. if ((r.left <= 1 || r.right >= vw - 1) && r.width <= vw * 0.4 && (r.height >= vh * 0.9 || /fixed|sticky/.test(s.position)) && (/^(ASIDE|NAV)$/.test(e.tagName) || /navigation|complementary/.test(e.getAttribute("role") || "") || /fixed|sticky/.test(s.position))) return 16;
  199. }
  200. return 24;
  201. };
  202. const inScroller = (el) => { for (let e = el.parentElement; e && e !== document.body; e = e.parentElement) { if (/auto|scroll/.test(cs(e).overflowX) && e.scrollWidth > e.clientWidth + 1) return true; } return false; };
  203. const cb = canvas && canvas.getBoundingClientRect(), textIn = new Map();
  204. // Painted text only: line boxes clipped to overflow-hidden ancestors (an ellipsis cell is measured by what it shows).
  205. for (const t of texts) for (let e = t.el; e && e !== document.body; e = e.parentElement) { if (!textIn.has(e)) textIn.set(e, []); textIn.get(e).push(...t.lines.filter((l) => l.width > 0)); }
  206. for (const el of (canvas || document).querySelectorAll("h1,h2,h3,h4,h5,h6,p,li,a,button,input,label,img,svg,video,canvas,td,th")) {
  207. if (!shown(el) || el.parentElement.closest("svg")) continue;
  208. let r = el.getBoundingClientRect();
  209. if (cb) {
  210. if (/^(IMG|svg|VIDEO|CANVAS)$/.test(el.tagName)) continue; // media may bleed off a canvas; text and controls may not
  211. if (r.left < cb.left - 0.5 || r.right > cb.right + 0.5 || r.top < cb.top - 0.5 || r.bottom > cb.bottom + 0.5) add("mustFix", "canvasBounds", sel(el), `past the canvas edge by ${[["L", cb.left - r.left], ["R", r.right - cb.right], ["T", cb.top - r.top], ["B", r.bottom - cb.bottom]].filter((d) => d[1] > 0.5).map((d) => d[0] + Math.round(d[1])).join(" ")} ${label(el, r)}`);
  212. continue;
  213. }
  214. const media = /^(IMG|svg|VIDEO|CANVAS)$/.test(el.tagName); // a <picture> is measured through its <img>
  215. // Media running the whole viewport width is a full-bleed image, not a gutter defect: no marker needed. [data-full-bleed]
  216. // frees media that bleeds off one edge only; text inside it still keeps the gutter.
  217. if (media && (el.closest("[data-full-bleed]") || (r.left <= 0.5 && r.right >= vw - 0.5))) continue;
  218. // Text is measured by its painted text, so padded rows and padded paragraphs are not flagged; controls and media by their box.
  219. if (!media && !/^(BUTTON|INPUT)$/.test(el.tagName)) { const rs = textIn.get(el) || []; if (!rs.length) continue; r = { left: Math.min(...rs.map((x) => x.left)), right: Math.max(...rs.map((x) => x.right)), top: Math.min(...rs.map((x) => x.top)) }; }
  220. const g = gutterOf(el);
  221. // Name the side that failed: "L4 R1325" read as two violations when only the left one was too close.
  222. const sides = [["left", r.left], ["right", vw - r.right]].filter((d) => d[1] < g - 0.5);
  223. if (r.right > 0 && (r.left < vw || !(media || /^(BUTTON|INPUT)$/.test(el.tagName))) && sides.length && !inScroller(el)) add("mustFix", "edge", `${sel(el)}|${sides.map((d) => d[0][0].toUpperCase()).join("")}`, `${sides.map((d) => `${Math.round(d[1])}px from the ${d[0]}`).join(" and ")} ${label(el, r)}`);
  224. }
  225. if (!cb && document.documentElement.scrollWidth > vw + 1) add("mustFix", "horizontalOverflow", vw < 768 ? "page|core" : "page", `scrollWidth ${document.documentElement.scrollWidth} > ${vw}`);
  226. // External resources: requested ones plus referencing tags (requests can fail offline).
  227. const ext = new Set(performance.getEntriesByType("resource").map((e) => e.name));
  228. for (const e of document.querySelectorAll('script[src],img[src],source[src],video[src],audio[src],iframe[src],link[rel~="stylesheet"][href],link[rel~="preload"][href],link[rel~="icon"][href]')) ext.add(e.src || e.href);
  229. for (const name of [...ext].sort()) {
  230. let u; try { u = new URL(name, location.href); } catch { continue; }
  231. if (/^https?:$/.test(u.protocol) && u.origin !== location.origin) add("mustFix", "externalResource", u.host, name.slice(0, 100));
  232. }
  233. // A standalone file: local assets resolve beside it, never through ../, absolute or file:// paths; every image loads; zoom stays on.
  234. if (location.protocol === "file:") for (const e of document.querySelectorAll("script[src],img[src],source[src],source[srcset],video[src],video[poster],audio[src],iframe[src],link[href]")) {
  235. const v = e.getAttribute("src") || e.getAttribute("href") || e.getAttribute("srcset") || e.getAttribute("poster") || "";
  236. if (/^(\.\.[\\/]|[\\/](?![\\/])|file:|[a-z]:[\\/])/i.test(v.trim())) add("mustFix", "localPath", v, `${e.tagName.toLowerCase()} ${v.slice(0, 100)}`);
  237. }
  238. for (const img of document.querySelectorAll("img")) {
  239. if (img.getAttribute("src") && img.complete && img.naturalWidth === 0 && !img.closest("template")) add("mustFix", "brokenImage", img.getAttribute("src").slice(0, 100), `img ${img.getAttribute("src").slice(0, 100)}`);
  240. }
  241. const vp = document.querySelector('meta[name="viewport"]')?.getAttribute("content") || "";
  242. if (/user-scalable\s*=\s*(no|0)\b|maximum-scale\s*=\s*1(\.0*)?(?![\d.])/i.test(vp)) add("mustFix", "noZoom", "viewport", vp.slice(0, 100));
  243. // Touch targets on a phone-width view: a control under 24x24 px that is not an inline link in running text (WCAG 2.5.8).
  244. if (vw < 768) for (const el of document.querySelectorAll('button,a[href],input:not([type=hidden]),select,[role="button"],[role="tab"],[role="switch"],[role="checkbox"]')) {
  245. if (!shown(el) || el.closest("[data-audit-skip]") || (el.tagName === "A" && /^inline/.test(cs(el).display) && el.parentElement.closest("p,li,td,dd,blockquote"))) continue;
  246. const r = el.getBoundingClientRect();
  247. if (r.width < 24 || r.height < 24) add("advisory", "smallTarget", sel(el), `${Math.round(r.width)}x${Math.round(r.height)} ${label(el, r)}`);
  248. }
  249. // Heading levels go in order: a jump of two or more levels down (h1 to h3) is advisory.
  250. let lastLevel = 0;
  251. for (const h of document.querySelectorAll("h1,h2,h3,h4,h5,h6")) {
  252. if (!shown(h)) continue;
  253. const lv = +h.tagName[1];
  254. if (lastLevel && lv > lastLevel + 1) add("advisory", "headingSkip", `${sel(h)}|${lastLevel}>${lv}`, `h${lastLevel} then h${lv} ${label(h)}`);
  255. lastLevel = lv;
  256. }
  257. // Advisory: boxes stretched by their row; a lone box beside a void; identical or many peer boxes.
  258. const all = [...document.body.querySelectorAll("*")].filter((el) => !(el instanceof SVGElement) && shown(el));
  259. const boxy = (el) => { const s = cs(el); return s.boxShadow !== "none" || parseFloat(s.borderTopWidth) + parseFloat(s.borderBottomWidth) > 0 && parseFloat(s.borderLeftWidth) > 0 || (rgba(s.backgroundColor)[3] > 0 && s.backgroundColor !== cs(el.parentElement).backgroundColor); };
  260. const contentBottom = (el) => { let b = -Infinity; const range = document.createRange(); range.selectNodeContents(el); for (const r of range.getClientRects()) if (r.height > 0) b = Math.max(b, r.bottom); for (const c of el.querySelectorAll("img,svg,video,canvas,input,button,select,textarea,iframe")) if (shown(c)) b = Math.max(b, c.getBoundingClientRect().bottom); return b; };
  261. for (const el of all) {
  262. const r = el.getBoundingClientRect();
  263. if (r.height < 120 || r.height > vh * 0.8 || r.width > vw * 0.9 || /^(NAV|ASIDE|HEADER|FOOTER|MAIN|DIALOG)$/.test(el.tagName) || !boxy(el) || el.closest("[data-audit-skip]")) continue;
  264. const s = cs(el), inner = r.height - parseFloat(s.paddingTop) - parseFloat(s.paddingBottom) - parseFloat(s.borderTopWidth) - parseFloat(s.borderBottomWidth);
  265. const cb = contentBottom(el);
  266. if (cb === -Infinity) continue;
  267. const empty = r.bottom - parseFloat(s.paddingBottom) - parseFloat(s.borderBottomWidth) - cb;
  268. if (empty > Math.max(96, inner * 0.4)) add("advisory", "stretchedBox", sel(el), `${Math.round(empty)}px empty of ${Math.round(r.height)} ${label(el, r)}`);
  269. }
  270. for (const el of all) {
  271. const s = cs(el);
  272. if (!/grid|flex/.test(s.display) || (s.display.includes("flex") && !s.flexDirection.startsWith("row"))) continue;
  273. const kids = [...el.children].filter(shown);
  274. if (!kids.length) continue;
  275. const r = el.getBoundingClientRect(), rows = [];
  276. for (const k of kids) { const kr = k.getBoundingClientRect(), row = rows.find((x) => Math.abs(x.top - kr.top) < 4); row ? row.items.push(kr) : rows.push({ top: kr.top, items: [kr] }); }
  277. const cards = kids.filter((k) => boxy(k) && k.getBoundingClientRect().height >= 80);
  278. const multiCol = s.display.includes("grid") ? s.gridTemplateColumns.split(" ").length > 1 : s.flexWrap === "wrap" && rows.length > 1;
  279. const last = rows[rows.length - 1], lone = kids[kids.length - 1];
  280. if (multiCol && cards.length === kids.length && rows.length > 1 && last.items.length === 1 && last.items[0].width < r.width * 0.6) add("advisory", "loneItem", sel(lone), `${Math.round(r.width - last.items[0].width)}px empty beside ${label(lone)}`);
  281. if (cards.length >= 3 && cards.length === kids.length) {
  282. if (cards.length > 6 && rows.length < 3) add("advisory", "manyPeers", sel(el), `${cards.length} peer boxes in one group: ${label(el)}`);
  283. }
  284. }
  285. // Echo family: one view saying the same thing twice. decorativeEcho (decoration repeating what it sits beside), echoLabel
  286. // (a line whose content words a heading, control label or chart label nearby already says) and repeatedText (the same
  287. // string painted twice). audit.mjs numbers these into one worklist, so each is dispositioned instead of glanced at, and
  288. // no marker the model writes itself takes an item off it: these read painted text under [aria-hidden="true"] and
  289. // [data-audit-skip] too, because both declare intent and neither moves a pixel. out.suppressed reports every marker the
  290. // audit does still honour. One defect is listed once: decorativeEcho is the most specific, then echoLabel, then
  291. // repeatedText, and each key skips what an earlier one already listed. What a script cannot read — paraphrase, a line
  292. // restating a trend a chart shows, whether an eyebrow adds a category — stays with check.md's cut pass.
  293. const HEAD = 'h1,h2,h3,h4,h5,h6,[role="heading"]';
  294. const SECT = 'section,article,[data-audit-on],[data-slide],[role="region"],main';
  295. const STOP = new Set("a an the our your my we us you its it this that these those and or of for in on at to by with from about how what why who all more new is are be".split(" "));
  296. const words = (t) => norm(t).split(" ").filter((w) => w && !STOP.has(w));
  297. // Two letters or digits at least, and at least one letter: a bare number is a counter, a score, an axis tick or a page
  298. // number ("04 / 12", "18", "12.5×"), and the same number twice in a view is normal, not a second expression of a fact.
  299. const substance = (t) => (t.match(/[\p{L}\p{N}]/gu) || []).length >= 2 && /\p{L}/u.test(t);
  300. const fsOf = (el) => parseFloat(cs(el).fontSize) * (el instanceof SVGElement ? scaleOf(el) : 1);
  301. // Painted extent: the line boxes already clipped to overflow-hidden ancestors, so a masked or ellipsised copy is measured by what shows.
  302. const pbox = (el) => {
  303. const rs = (textIn.get(el) || []).filter((l) => l.width > 0);
  304. return rs.length ? { left: Math.min(...rs.map((r) => r.left)), right: Math.max(...rs.map((r) => r.right)), top: Math.min(...rs.map((r) => r.top)), bottom: Math.max(...rs.map((r) => r.bottom)) } : null;
  305. };
  306. const painted = [];
  307. for (const el of owners) {
  308. const box = pbox(el);
  309. if (box) painted.push({ el, t: norm(el.textContent), box, fs: fsOf(el), sect: el.closest(SECT) || document.body });
  310. }
  311. // One view: a deck slide (audited one at a time) or any one viewport of a page.
  312. const oneView = (a, b) => Math.max(a.bottom, b.bottom) - Math.min(a.top, b.top) <= vh;
  313. const area = (r) => (r.right - r.left) * (r.bottom - r.top);
  314. const stacked = (a, b) => inter(a, b) > 0.4 * Math.min(area(a), area(b));
  315. // Equal items: the text sits in a set of three or more alike siblings (cards, tiles, chips, rows), where a repeat is the
  316. // pattern, not a defect. Sectioning elements and full-width tall bands are not the repeated unit; anything smaller can be.
  317. const inSet = (el) => {
  318. for (let e = el, i = 0; e && e !== document.body && i < 4; e = e.parentElement, i++) {
  319. const p = e.parentElement, r = e.getBoundingClientRect(), cls = e.getAttribute("class") || "";
  320. if (!p || e.matches(SECT) || (r.width > vw * 0.9 && r.height > vh * 0.6)) continue;
  321. if ([...p.children].filter((k) => k.tagName === e.tagName && (k.getAttribute("class") || "") === cls).length >= 3) return true;
  322. }
  323. return false;
  324. };
  325. const listed = new Set();
  326. // decorativeEcho: the decoration's whole string is already said in the view, exact or as a fragment of a longer one (on a
  327. // space or CJK boundary). It is not a hit when the decoration says more than the text it echoes, which is a title set
  328. // large, not a repeat. Decorative intent is taken from aria-hidden="true" (the author declared it carries nothing) or from
  329. // no semantic role plus the largest type in the view; it must also be set at least 1.2x the text it repeats, so only
  330. // decoration shouting over the real content counts. Not reported: controls, navigation, tabs, running footers, legends,
  331. // table cells, figures, SVG and chart text, on either side; a value whose accessible name sits on an ancestor; a copy
  332. // stacked on the original (layered type); equal items; a prose twin.
  333. const CHROME = 'nav,[role="navigation"],[role="tablist"],[role="tab"],[role="menu"],[role="menuitem"],[role="listbox"],[role="option"],footer,[role="banner"],[role="contentinfo"],a[href],button,[role="button"],[role="link"],label,summary,select,option,legend,figcaption,caption,th,td,dt,figure,svg,[class*="legend" i],[class*="chart" i],[class*="axis" i]';
  334. const NAMED = 'h1,h2,h3,h4,h5,h6,p,li,dd,blockquote,q,time,abbr,code,kbd,[role]:not([role="presentation"]):not([role="none"])';
  335. const NOTREAL = '[aria-hidden="true"],svg,footer,[role="contentinfo"],[class*="legend" i],[class*="chart" i]';
  336. // A decorative repeat needs more body than a drop cap, a step number ("01") or a bullet glyph.
  337. const solid = (t) => t.length >= 3 && substance(t);
  338. // A value whose accessible name sits on an ancestor is that element's visual form, not a second expression of the fact.
  339. const spoken = (el, t) => { for (let e = el, i = 0; e && e !== document.body && i < 4; e = e.parentElement, i++) for (const a of ["aria-label", "title"]) if (norm(e.getAttribute(a) || "").includes(t)) return true; return false; };
  340. // The real expression is a heading, a label or a short line — not prose: a value next to its own description is not a copy of it.
  341. const twins = painted.filter((x) => !x.el.closest(NOTREAL) && solid(x.t) && (x.el.matches(HEAD) || !!x.el.closest(HEAD) || (x.t.length <= 60 && !x.el.closest("p,li,dd,blockquote"))));
  342. const topFs = Math.max(0, ...twins.map((x) => x.fs));
  343. const deco = painted.filter((x) => x.fs >= 24 && solid(x.t) && !x.el.closest(CHROME) && !inSet(x.el) && !spoken(x.el, x.t)
  344. && (!!x.el.closest('[aria-hidden="true"]') || (x.fs >= 48 && x.fs >= topFs && !x.el.closest(NAMED))));
  345. const bnd = (s, i, len) => [s[i - 1], s[i + len]].every((c) => c === undefined || c === " " || CJK.test(c));
  346. const covers = (big, small) => { for (let i = big.indexOf(small); i >= 0; i = big.indexOf(small, i + 1)) if (bnd(big, i, small.length)) return true; return false; };
  347. for (const d of deco) {
  348. if ([...twins, ...deco].filter((x) => x.t === d.t).length >= 3) continue;
  349. const m = twins.find((x) => x.el !== d.el && !x.el.contains(d.el) && !d.el.contains(x.el) && d.fs >= x.fs * 1.2
  350. && covers(x.t, d.t) && oneView(d.box, x.box) && !stacked(d.box, x.box));
  351. if (m) { listed.add(d.el); add("advisory", "decorativeEcho", `${sel(d.el)}|${sel(m.el)}`, `${Math.round(d.fs)}px ${label(d.el)} repeats ${label(m.el)}`); }
  352. }
  353. // echoLabel: a line of at most six content words, all of which a heading, a control label or a chart label in the same
  354. // section and the same view already says. Not listed: the carriers themselves, text that has to stay readable to act
  355. // (KEEP, the list check.md keeps) and equal items. A sentence is not a carrier and a run inside a sentence is not a
  356. // candidate, so a word two sentences share is not a hit. An accessible name is not a carrier either: it paints nothing,
  357. // so it leaves nothing on screen for a visible line to duplicate, and check.md keeps accessible names on purpose.
  358. const KEEP = 'a[href],button,[role="button"],[role="link"],[role="tab"],[role="menuitem"],[role="option"],label,summary,select,option,input,textarea,th,legend,nav,[role="navigation"],[role="alert"],[role="status"],footer,[role="contentinfo"],svg,[class*="legend" i],[class*="axis" i]';
  359. const CARRY = 'a[href],button,[role="button"],summary,label,th,svg';
  360. const wordSet = new Map(), setOf = (x) => { if (!wordSet.has(x)) wordSet.set(x, new Set(words(x.t))); return wordSet.get(x); };
  361. const carrier = (x) => x.el.matches(HEAD) || !!x.el.closest(HEAD) || !!x.el.closest(CARRY);
  362. const subset = (ws, cw, ct) => [...ws].every((w) => cw.has(w) || (CJK.test(w) && w.length >= 2 && ct.includes(w)));
  363. // An inline run inside a sentence (<strong>, <em>, a span in a paragraph) is part of that sentence, not a line of its own.
  364. const inProse = (el) => {
  365. let b = el;
  366. while (b.parentElement && b !== document.body && /^inline/.test(cs(b).display)) b = b.parentElement;
  367. return b !== el && words(norm(b.textContent)).length > 8;
  368. };
  369. for (const x of painted) {
  370. if (listed.has(x.el) || x.el.matches(HEAD) || x.el.closest(KEEP) || inSet(x.el) || !substance(x.t) || inProse(x.el)) continue;
  371. const xw = setOf(x);
  372. if (!xw.size || xw.size > 6) continue;
  373. const c = painted.find((y) => y !== x && !y.el.contains(x.el) && !x.el.contains(y.el) && y.sect === x.sect && oneView(x.box, y.box) && carrier(y) && subset(xw, setOf(y), y.t));
  374. if (!c) continue;
  375. listed.add(x.el);
  376. // Two elements saying exactly the same thing echo each other: list the pair once, not once per direction.
  377. if (c.t === x.t) listed.add(c.el);
  378. add("advisory", "echoLabel", `${sel(x.el)}|${sel(c.el)}`, `"${x.el.textContent.trim().slice(0, 30)}" repeats ${sel(c.el)} "${c.el.textContent.trim().slice(0, 30)}"`);
  379. }
  380. // repeatedText: the same string painted twice. Not repetition: navigation and in-page links, tab and menu labels, table
  381. // and control labels, running headers and footers (chrome repeats by design), a copy stacked on another (layered type
  382. // reads as one string), and equal items, where a status word or tag on every row is the pattern.
  383. const navish = (el) => el.closest('nav,[role="navigation"],[role="tablist"],[role="tab"],[role="menu"],[role="menuitem"],[role="option"],a[href^="#"],button,summary,label,option,select,td,th,header,footer,[role="banner"],[role="contentinfo"]');
  384. const counts = new Map();
  385. for (const x of painted) if (x.t.length >= 4 && substance(x.t)) counts.set(x.t, (counts.get(x.t) || []).concat(x));
  386. // Copies in one component slot (same tag and class, same kind of parent) are one component repeated — a byline on each
  387. // card, a status tag on each row, a counter in each corner — so each slot counts once. Two slots saying it is the hit.
  388. const slot = (el) => `${sel(el)}|${el.parentElement ? sel(el.parentElement) : ""}`;
  389. for (const [t, xs] of counts) {
  390. const rest = new Map();
  391. for (const x of xs) {
  392. if (navish(x.el) || listed.has(x.el) || inSet(x.el) || [...rest.values()].some((y) => stacked(x.box, y.box))) continue;
  393. if (!rest.has(slot(x.el))) rest.set(slot(x.el), x);
  394. }
  395. const at = [...rest.values()];
  396. if (at.length > 1) add("advisory", "repeatedText", t.slice(0, 40), `x${at.length} "${t.slice(0, 40)}" at y=${at.map((x) => Math.round(x.box.top + window.scrollY)).join(",")}`);
  397. }
  398. // Suppression report: every marker this audit still honours, so silencing shows up in the report instead of hiding in the
  399. // HTML. The echo family above ignores aria-hidden and data-audit-skip; the rest of the audit does not.
  400. const supp = {};
  401. for (const [k, q] of [["ariaHidden", '[aria-hidden="true"]'], ["auditSkip", "[data-audit-skip]"], ["fullBleed", "[data-full-bleed]"], ["readingSurface", 'article,[role="article"],[data-reading]']]) {
  402. const els = [...document.querySelectorAll(q)].filter((el) => !el.parentElement?.closest(q) && (el.textContent.trim() || el.querySelector("img,svg,video,canvas,picture")));
  403. if (els.length) supp[k] = { count: els.length, at: els.slice(0, 5).map((el) => label(el)) };
  404. }
  405. if (Object.keys(supp).length) out.suppressed = supp;
  406. // Advisory: numeric table columns right-aligned with tabular figures.
  407. const NUM = /^[-+−±]?[$€¥£]?\s?\d[\d,.\s]*\s?(%|[kKmMbB万亿元x×]|pts?|ms|s)?$/;
  408. const cols = new Set();
  409. for (const td of document.querySelectorAll("td")) {
  410. if (!shown(td) || !NUM.test(td.textContent.trim())) continue;
  411. const s = cs(td), table = td.closest("table"), key = `${table ? sel(table) : "table"} col ${td.cellIndex + 1}`;
  412. const tab = /tabular-nums/.test(s.fontVariantNumeric) || /tnum/.test(s.fontFeatureSettings);
  413. if ((!/right|end/.test(s.textAlign) || !tab) && !cols.has(key)) { cols.add(key); add("advisory", "tableNumbers", key, `${tab ? "" : "no tabular-nums "}${/right|end/.test(s.textAlign) ? "" : "align " + s.textAlign + " "}${key}`); }
  414. }
  415. // Advisory: blocks stacked in one column whose content starts at different x (primary blocks share one left edge).
  416. const minL = new Map(), maxR = new Map();
  417. for (const t of texts) for (let e = t.el; e && e !== document.documentElement; e = e.parentElement) {
  418. minL.set(e, Math.min(minL.get(e) ?? Infinity, t.box.left)); maxR.set(e, Math.max(maxR.get(e) ?? -Infinity, t.box.right));
  419. }
  420. for (const el of all) {
  421. const s = cs(el);
  422. if ((/flex|grid/.test(s.display) && !(s.display.includes("flex") && s.flexDirection.startsWith("column"))) || el.closest("[data-audit-skip]")) continue;
  423. const r = el.getBoundingClientRect(), pl = r.left + parseFloat(s.paddingLeft) + parseFloat(s.borderLeftWidth), pr = r.right - parseFloat(s.paddingRight) - parseFloat(s.borderRightWidth);
  424. const kids = [...el.children].filter((k) => { const kr = k.getBoundingClientRect(), ks = cs(k); return shown(k) && minL.has(k) && kr.height >= 64 && kr.width >= (pr - pl) * 0.6 && !/absolute|fixed/.test(ks.position) && ks.float === "none" && !/^(UL|OL|DL|BLOCKQUOTE|PRE|FIGURE|TABLE|NAV|ASIDE)$/.test(k.tagName); });
  425. if (kids.length < 3 || kids.some((k, i) => i && k.getBoundingClientRect().top < kids[i - 1].getBoundingClientRect().bottom - 2)) continue;
  426. const lefts = kids.filter((k) => { const a = minL.get(k) - pl, b = pr - maxR.get(k); return cs(k).textAlign !== "center" && !(a > 8 && Math.abs(a - b) < 4); }).map((k) => [Math.round(minL.get(k)), k]);
  427. const xs = [...new Set(lefts.map((l) => l[0]))].sort((a, b) => a - b).filter((x, i, a) => !i || x - a[i - 1] > 3);
  428. if (xs.length > 1 && xs[xs.length - 1] - xs[0] < (pr - pl) * 0.4) { const odd = lefts.find((l) => Math.abs(l[0] - xs[0]) > 3 && Math.abs(l[0] - xs[xs.length - 1]) > 3) || lefts.find((l) => l[0] === xs[xs.length - 1]); add("advisory", "leftEdge", sel(el), `content starts at x=${xs.join("/")} in one column: ${label(odd[1])}`); }
  429. }
  430. // Advisory: long pages need a way to reach their sections; existing section navigation should mark the section in view.
  431. // A fragment may hold a literal % ("#100%-growth"), which decodeURIComponent rejects: fall back to the raw id.
  432. const frag = (href) => { const id = href.slice(1); try { return decodeURIComponent(id); } catch { return id; } };
  433. const links = [...document.querySelectorAll('a[href^="#"]')].filter((a) => { const id = frag(a.getAttribute("href")); return id && shown(a) && document.getElementById(id); });
  434. const targets = [...new Set(links.map((a) => a.getAttribute("href")))];
  435. const pageH = document.documentElement.scrollHeight;
  436. // App and board surfaces (kanban columns, dashboards, app shells) are operated, not read through: no section links expected.
  437. // Signals: an application or grid role; 2+ columns of draggable (draggable, cursor grab) items or 3+ side-by-side titled columns of
  438. // stacked item boxes (a short column title such as "To do (3)" above the first item; quotes and figures are not items); or a
  439. // full-height side rail plus a toolbar (2+ controls in the first 200px). Repeated logos, cards or a testimonial wall are not a board.
  440. const stacks = new Map();
  441. for (const el of all) { const d = el.getAttribute("draggable") === "true" || /^(grab|move)$/.test(cs(el).cursor); if ((d || (boxy(el) && el.getBoundingClientRect().height >= 40)) && el.parentElement) { const k = el.parentElement, v = stacks.get(k) || { n: 0, d: 0, tops: new Set(), items: [] }; v.n++; v.d += d; v.items.push(el); v.tops.add(Math.round(el.getBoundingClientRect().top)); stacks.set(k, v); } }
  442. const titled = (k, v) => { const h = k.firstElementChild, t = (h?.textContent || "").trim(); return !!h && !v.items.includes(h) && shown(h) && t.length > 0 && t.length <= 40 && h.getBoundingClientRect().bottom <= v.items[0].getBoundingClientRect().top + 1 && !v.items.some((i) => i.matches("blockquote,figure,q") || i.querySelector("blockquote,q")); };
  443. const lanes = [...stacks].filter(([, v]) => v.n >= 2 && v.tops.size === v.n).map(([k, v]) => [k.getBoundingClientRect(), v.d >= 2, titled(k, v)]);
  444. const titledLanes = lanes.filter(([, , t]) => t);
  445. const columns = lanes.filter(([, d]) => d).length >= 2 || titledLanes.some(([r]) => titledLanes.filter(([q]) => Math.abs(q.top - r.top) < 80 && (q.right <= r.left || q.left >= r.right || q === r)).length >= 3);
  446. const rail = all.some((el) => { const r = el.getBoundingClientRect(); return /^(ASIDE|NAV)$/.test(el.tagName) && (r.left <= 1 || r.right >= vw - 1) && r.width <= vw * 0.4 && r.height >= Math.min(pageH, vh * 3) * 0.9; });
  447. const toolbar = !!document.querySelector('[role="toolbar"]') || [...document.querySelectorAll("button,input,select,[role=button]")].filter((c) => shown(c) && absY(c.getBoundingClientRect()) < 200 && !c.closest("aside,nav")).length >= 2;
  448. const app = !!document.querySelector('[role="application"],[role="grid"]') || columns, board = app || (rail && toolbar);
  449. if (!isDeck && !canvas && !board && pageH > vh * 3 && targets.length < 3) add("advisory", "noSectionNav", "page", `${(pageH / vh).toFixed(1)} screens tall with no section links (${targets.length} in-page targets)`);
  450. if (!isDeck && targets.length >= 3 && pageH > vh * 2.5) {
  451. const sig = (a) => { const s = cs(a); return [a.className, a.getAttribute("aria-current"), s.color, s.fontWeight, s.backgroundColor, s.borderBottomColor, s.textDecorationLine, s.opacity].join("|"); };
  452. const goal = targets[Math.max(1, targets.length - 2)], goalLinks = links.filter((a) => a.getAttribute("href") === goal), before = goalLinks.map(sig);
  453. document.getElementById(frag(goal))?.scrollIntoView({ behavior: "instant", block: "start" });
  454. window.scrollBy(0, 40);
  455. await new Promise((r) => setTimeout(r, 700));
  456. const inView = goalLinks.some((a) => { const r = a.getBoundingClientRect(); return r.bottom > 0 && r.top < vh; });
  457. if (inView && !goalLinks.some((a, i) => a.getAttribute("aria-current") || sig(a) !== before[i])) add("advisory", "navCurrent", goal, `link to ${goal} unchanged when its section is in view`);
  458. }
  459. // Advisory: text walls outside reading surfaces (article, [role=article], .prose, [data-reading]; quotes, code, footers, tables,
  460. // controls, dialogs and alerts exempt). Size = Latin words + CJK characters / 2. A block over 60, a run of plain sibling blocks
  461. // over 120, or over 100 in prose blocks (12+) in the first screen. Folded content (closed details, hidden panels) is not counted.
  462. const READ = 'article,[role="article"],.prose,.markdown-body,[data-reading],blockquote,pre,code,figcaption,footer,small,dialog,[role="dialog"],[role="alert"],[role="tooltip"],td,th,label,button,nav,[data-audit-skip]';
  463. const CJKG = /[぀-ヿ㐀-鿿가-힯]/g, size = (t) => (t.replace(CJKG, " ").match(/[\p{L}\p{N}]+/gu) || []).length + (t.match(CJKG) || []).length / 2;
  464. const blocks = new Map();
  465. for (const el of owners) {
  466. let b = el;
  467. while (b.parentElement && b !== document.body && /^inline/.test(cs(b).display)) b = b.parentElement;
  468. if (b !== document.body && !(b instanceof SVGElement) && !b.closest(READ) && !blocks.has(b)) blocks.set(b, 0);
  469. }
  470. const containers = new Set();
  471. for (const b of blocks.keys()) for (let e = b.parentElement; e; e = e.parentElement) containers.add(e);
  472. for (const b of [...blocks.keys()]) containers.has(b) ? blocks.delete(b) : blocks.set(b, size(texts.filter((t) => b.contains(t.el)).map((t) => t.n.data).join(" ")));
  473. let firstView = 0;
  474. for (const [b, n] of blocks) {
  475. if (n > 60) add("advisory", "textWall", sel(b), `${Math.round(n)} words in one block ${label(b)}`);
  476. const r = b.getBoundingClientRect();
  477. if (n >= 12 && absY(r) < vh && (!canvas || isDeck)) firstView += n;
  478. }
  479. for (const p of new Set([...blocks.keys()].map((b) => b.parentElement))) {
  480. let run = [], n = 0;
  481. for (const k of [...p.children, null]) {
  482. if (k && blocks.get(k) >= 12 && shown(k)) { run.push(k); n += blocks.get(k); continue; }
  483. if (run.length > 1 && n > 120) add("advisory", "textWall", `${sel(p)}|run`, `${run.length} plain blocks, ${Math.round(n)} words with no heading, list, table or control between ${label(run[0])}`);
  484. run = []; n = 0;
  485. }
  486. }
  487. if (firstView > 100) add("advisory", "denseFirstView", "page", `${Math.round(firstView)} words of prose in the first screen`);
  488. // Motion (references/motion.md). Transitions by computed style: `all` and layout properties are advisory (unintended motion and
  489. // jank, not a rule-layer defect); UI controls over 300 ms are must-fix on an Operate surface, where motion must not make the user wait.
  490. // Keyframes in use: layout properties, and moving keyframes with no prefers-reduced-motion rule anywhere, are advisory.
  491. // Infinite animations: more than one marquee (a translating strip wider than its box), a loop whose last frame differs from its
  492. // first (not alternate, not a marquee) and a large moving loop with no pause control are advisory. How motion is drawn (easing,
  493. // entry scale, origin) is the designer's call and is not checked. reducedMotion (audit.mjs) re-runs the page under
  494. // prefers-reduced-motion: reduce.
  495. const list = (v) => v.split(/,(?![^(]*\))/).map((x) => x.trim());
  496. const secs = (v) => (/ms$/.test(v) ? parseFloat(v) / 1000 : parseFloat(v));
  497. const LAYOUT = /^(width|height|min-width|min-height|max-width|max-height|top|left|right|bottom|inset|margin|padding|grid-template|flex-basis)/;
  498. const MOVE = /^(transform|translate|rotate|scale|left|top|right|bottom|offset|background-position|backgroundPosition|margin)/;
  499. const COLOR = /(^|-)(color|fill|stroke|text-decoration|text-shadow)/;
  500. const CTL = 'a[href],button,input,select,textarea,summary,[role="button"],[role="tab"],[role="switch"],[role="menuitem"],[role="option"]';
  501. const slow = [], once = new Set(), hit = (bucket, key, s, msg) => { if (!once.has(key + s)) { once.add(key + s); add(bucket, key, s, msg); } };
  502. for (const el of document.body.querySelectorAll("*")) {
  503. if (el.closest("[data-audit-skip]")) continue;
  504. const s = cs(el), props = list(s.transitionProperty), durs = list(s.transitionDuration).map(secs);
  505. props.forEach((p, i) => {
  506. const d = durs[i % durs.length];
  507. if (!(d > 0) || p === "none") return;
  508. if (p === "all") hit("advisory", "transitionAll", sel(el), `transition: all ${d}s ${label(el)}`);
  509. else if (LAYOUT.test(p)) hit("advisory", "layoutAnimation", `${sel(el)}|${p}`, `transition ${p} ${label(el)}`);
  510. if (d > 0.3 && p !== "visibility" && el.matches(CTL)) { const k = sel(el), j = slow.findIndex((x) => x[0] === k), m = !COLOR.test(p); if (j < 0) slow.push([k, `${p} ${Math.round(d * 1000)}ms ${label(el)}`, m]); else if (m && !slow[j][2]) slow[j] = [k, `${p} ${Math.round(d * 1000)}ms ${label(el)}`, m]; }
  511. });
  512. }
  513. // slowTransition is must-fix on an Operate surface (app UI): window.__designAuditSurface = "operate" (audit.mjs --surface operate), or, when
  514. // no surface is given, an inferred app (application or grid role, board columns; a side rail plus a toolbar is not enough, docs have
  515. // both). Color fades (links, hover tints) and other surfaces (Read, Persuade, decks, canvases): advisory.
  516. const surface = window.__designAuditSurface, operate = surface ? surface === "operate" : app && !isDeck && !canvas;
  517. for (const [s, m, moves] of slow) add(operate && moves ? "mustFix" : "advisory", "slowTransition", s, m);
  518. const frames = new Map();
  519. let reduceRule = [...document.scripts].some((sc) => /prefers-reduced-motion/.test(sc.textContent));
  520. const walk = (rules) => { for (const r of rules) {
  521. if (r.type === CSSRule.KEYFRAMES_RULE) frames.set(r.name, r);
  522. if (r.conditionText && /prefers-reduced-motion/.test(r.conditionText)) reduceRule = true;
  523. if (r.cssRules) walk(r.cssRules);
  524. } };
  525. for (const sh of document.styleSheets) { try { walk(sh.cssRules); } catch {} }
  526. let moving = false;
  527. for (const name of new Set([...document.body.querySelectorAll("*")].flatMap((el) => list(cs(el).animationName)).filter((n) => n !== "none"))) {
  528. const r = frames.get(name);
  529. if (!r) continue;
  530. for (const k of r.cssRules) {
  531. const ps = [...k.style];
  532. const lay = ps.find((p) => LAYOUT.test(p));
  533. if (lay) hit("advisory", "layoutAnimation", `${name}|${lay}`, `@keyframes ${name} animates ${lay}`);
  534. if (ps.some((p) => MOVE.test(p))) moving = true;
  535. }
  536. }
  537. if (moving && !reduceRule) hit("advisory", "noReducedMotion", "page", "moving @keyframes and no prefers-reduced-motion rule in CSS or scripts");
  538. const loops = document.getAnimations().filter((a) => a.effect?.target && a.effect.getComputedTiming().iterations === Infinity);
  539. const strips = new Set(document.querySelectorAll("marquee")), seams = [];
  540. for (const a of loops) {
  541. const el = a.effect.target, pseudo = a.effect.pseudoElement || null, kf = a.effect.getKeyframes();
  542. const r = el.getBoundingClientRect(), box = el.parentElement?.getBoundingClientRect();
  543. if (kf.some((k) => /translate|matrix/.test(k.transform || "") || k.translate) && box && (r.width > box.width * 1.3 || r.width > vw)) { strips.add(el); continue; }
  544. const t = a.effect.getComputedTiming(), dir = a.effect.getTiming().direction;
  545. if (/alternate/.test(dir) || !(t.duration > 0)) continue;
  546. const snap = () => { const s = cs(el, pseudo), m = new DOMMatrix(s.transform === "none" ? "" : s.transform); return [m.a, m.b, m.c, m.d, m.e / 100, m.f / 100, +s.opacity, (parseFloat(s.rotate) || 0) % 360 / 360, ...rgba(s.color).slice(0, 3).map((c) => c / 255), ...rgba(s.backgroundColor).slice(0, 3).map((c) => c / 255)]; };
  547. const was = a.playState, at = a.currentTime, delay = a.effect.getTiming().delay || 0;
  548. try {
  549. a.pause(); a.currentTime = delay; const s0 = snap(); a.currentTime = delay + t.duration - 1; const s1 = snap();
  550. if (s0.some((v, i) => Math.abs(v - s1[i]) > 0.03 && Math.abs(Math.abs(v - s1[i]) - 1) > 0.03 * (i === 7))) seams.push(el);
  551. } catch {} finally { a.currentTime = at ?? 0; if (was === "running") a.play(); }
  552. }
  553. if (strips.size > 1) add("advisory", "marquee", "page", `${strips.size} marquees: ${[...strips].slice(0, 3).map((e) => sel(e)).join(", ")}`);
  554. for (const el of new Set(seams)) hit("advisory", "loopSeam", sel(el), `loop ends on a different frame than it starts ${label(el)}`);
  555. const big = loops.find((a) => { const r = a.effect.target.getBoundingClientRect(); return r.width * r.height >= vw * vh * 0.1 && a.effect.getKeyframes().some((k) => Object.keys(k).some((p) => MOVE.test(p))); });
  556. const pause = [...document.querySelectorAll('button,[role="button"],input[type="checkbox"]')].some((b) => /pause|stop|暂停|停止/i.test(`${b.textContent} ${b.getAttribute("aria-label") || ""} ${b.title}`));
  557. if (big && !pause) hit("advisory", "noPauseControl", sel(big.effect.target), `large moving loop over 5 s with no pause control ${label(big.effect.target)}`);
  558. // Contrast: composite the layers actually under the text (elementsFromPoint), so positioned overlays count.
  559. // Images, gradients, media and positioned pseudo-element layers make it unverifiable (counted, checked on the screenshot).
  560. const ancestorBg = (el) => {
  561. const layers = [];
  562. for (let e = el; e; e = e.parentElement) {
  563. const s = cs(e);
  564. if (s.backgroundImage !== "none") return null;
  565. const c = rgba(s.backgroundColor);
  566. if (c[3] > 0) layers.push(c);
  567. if (c[3] >= 1) break;
  568. }
  569. return layers;
  570. };
  571. const pseudoLayer = (e) => ["::before", "::after"].some((p) => { const s = cs(e, p); return s.content !== "none" && /absolute|fixed/.test(s.position) && (s.backgroundImage !== "none" || rgba(s.backgroundColor)[3] > 0); });
  572. const stackBg = (el, x, y) => {
  573. const stack = document.elementsFromPoint(x, y), i = stack.indexOf(el);
  574. if (i < 0) return undefined;
  575. const layers = [];
  576. for (const e of stack.slice(i)) {
  577. const anc = e.contains(el), s = cs(e);
  578. if (!anc && e instanceof SVGTextContentElement) continue;
  579. if (!anc && e instanceof SVGElement && e.tagName.toLowerCase() !== "svg") {
  580. if (/^url|none/.test(s.fill)) return null;
  581. const f = rgba(s.fill), a = f[3] * +s.fillOpacity * +s.opacity;
  582. layers.push([f[0], f[1], f[2], a]);
  583. if (a >= 1) break;
  584. continue;
  585. }
  586. if (!anc && /^(IMG|VIDEO|CANVAS|IFRAME|PICTURE|OBJECT|EMBED)$/.test(e.tagName)) return null;
  587. if (s.backgroundImage !== "none" || pseudoLayer(e)) return null;
  588. const c = rgba(s.backgroundColor), a = c[3] * (anc ? 1 : +s.opacity);
  589. if (a > 0) layers.push([c[0], c[1], c[2], a]);
  590. if (a >= 1) break;
  591. }
  592. return layers;
  593. };
  594. const byY = owners.map((el) => [el, absY(el.getBoundingClientRect())]).sort((a, b) => a[1] - b[1]);
  595. // Hit testing skips pointer-events:none layers that still paint; count them during the contrast pass.
  596. const pe = document.createElement("style");
  597. pe.textContent = "*,*::before,*::after{pointer-events:auto!important}";
  598. document.head.append(pe);
  599. for (const [el] of byY) {
  600. const text = el.textContent.trim();
  601. if (EMOJI.test(text) || el.closest(':disabled,[aria-disabled="true"],[aria-hidden="true"]')) continue;
  602. const s = cs(el), isSvg = el instanceof SVGElement, fs = parseFloat(s.fontSize) * (isSvg ? scaleOf(el) : 1);
  603. const range = document.createRange(); range.selectNodeContents(own.get(el)[0].n);
  604. let r = range.getClientRects()[0];
  605. if (!r) continue;
  606. if (r.top < 0 || r.bottom > vh) { window.scrollTo(0, absY(r) - vh / 3); r = range.getClientRects()[0]; }
  607. // Sample the start and the middle of the first line; the better surface counts (organic shapes, partial overlays).
  608. const y = r.top + r.height / 2, fg = rgba(isSvg && s.fill && s.fill !== "none" && !s.fill.startsWith("url") ? s.fill : s.color), fa = fg[3] * opacityOf(el);
  609. const surfaces = [r.left + Math.min(r.width / 2, 4), r.left + r.width / 2].map((x) => (x >= 0 && x < vw && y >= 0 && y < vh ? stackBg(el, x, y) : undefined));
  610. const found = surfaces.filter((l) => l !== undefined), all_ = found.length ? found : [ancestorBg(el)];
  611. if (all_.some((l) => !l)) { out.unverifiedContrast++; continue; }
  612. const c = Math.max(...all_.map((layers) => { const bg = layers.reduceRight((acc, c) => over(c, acc), [255, 255, 255, 1]); return ratio(over([fg[0], fg[1], fg[2], fa], bg), bg); }));
  613. const large = fs >= 24 || (fs >= 18.66 && +s.fontWeight >= 700);
  614. if (c < (large ? 3 : 4.5)) add("mustFix", "lowContrast", `${sel(el)}${core(el, text, fs)}`, `${c.toFixed(2)}:1 ${Math.round(fs)}px ${label(el)}`, +c.toFixed(2));
  615. }
  616. // Placeholder text of empty enabled fields, against the field's own surface.
  617. for (const el of document.querySelectorAll("input[placeholder],textarea[placeholder]")) {
  618. if (!el.placeholder.trim() || el.value || el.disabled || !shown(el)) continue;
  619. let r = el.getBoundingClientRect();
  620. if (r.top < 0 || r.bottom > vh) { window.scrollTo(0, absY(r) - vh / 3); r = el.getBoundingClientRect(); }
  621. const x = r.left + Math.min(r.width / 2, 8), y = r.top + r.height / 2;
  622. const layers = x >= 0 && x < vw && y >= 0 && y < vh ? stackBg(el, x, y) : ancestorBg(el);
  623. if (!layers) { out.unverifiedContrast++; continue; }
  624. const bg = layers.reduceRight((acc, c) => over(c, acc), [255, 255, 255, 1]), ps = cs(el, "::placeholder"), fg = rgba(ps.color);
  625. const c = ratio(over([fg[0], fg[1], fg[2], fg[3] * +ps.opacity * opacityOf(el)], bg), bg);
  626. if (c < 4.5) add("mustFix", "lowContrast", `${sel(el)}|placeholder|core`, `${c.toFixed(2)}:1 placeholder "${el.placeholder.slice(0, 28)}" ${label(el, r)}`, +c.toFixed(2));
  627. }
  628. pe.remove();
  629. window.scrollTo(0, y0);
  630. const full = window.__designAuditFull;
  631. for (const b of ["mustFix", "advisory"]) for (const k in out[b]) {
  632. const items = out[b][k], sigs = {};
  633. for (const [s] of items) sigs[s] = (sigs[s] || 0) + 1;
  634. out[b][k] = { count: items.length, samples: items.slice(0, 5).map((i) => i[1]), sigs, ...(full ? { items } : {}) };
  635. }
  636. return out;
  637. })()