// Layout check for the current page. Paste as a browser expression
// (T3 preview_evaluate, Playwright page.evaluate, or DevTools console), or let
// scripts/run.mjs inject it. Returns a JSON string; reads the DOM only.
// Outside a browser (no `document`) it returns the pure rule functions so
// `node --test` can exercise them with plain rect/style objects.
(() => {
const MAX = 12;
const TARGET = 44;
const num = (v) => parseFloat(v);
const rules = {
// Computed style + bounding rect -> rendered at all?
isVisible: (st, r) =>
st.display !== "none" && st.visibility !== "hidden" && Number(st.opacity) !== 0 && r.width > 0 && r.height > 0,
// Element sticks out of the viewport horizontally.
pastEdge: (r, vw) => r.right > vw + 1 || r.left < -1,
// Keep only the deepest items: drop any item that contains another one.
deepest: (items, contains) => items.filter((a) => !items.some((b) => b !== a && contains(a, b))),
// An ancestor with this overflow-x hides or scrolls what sticks out.
containsX: (overflowX) => /^(auto|scroll|hidden|clip)$/.test(overflowX),
// Hidden/scrolled away by an ancestor? Fixed elements are placed against the
// viewport, so an ancestor's overflow does not contain them.
isContained: (position, ancestorOverflowXs) =>
position !== "fixed" && ancestorOverflowXs.some((o) => /^(auto|scroll|hidden|clip)$/.test(o)),
// Box fits but its content spills out (wide <pre>, wide table in a plain block).
// Scroll containers are excluded: their overflow is reachable by scrolling.
contentOverflows: (st, scrollWidth, clientWidth) =>
st.overflowX === "visible" && !/^inline$/.test(st.display) && scrollWidth > clientWidth + 2,
pageOverflows: (docW, vw) => docW > vw + 1,
// Leaf text or a cropped image cut off by overflow hidden/clip, per axis.
// Note: CSS turns `visible` into `auto` when the other axis is hidden, so a
// scrolling axis is not a clipping axis.
isClipped: (st, box) => {
const cutsX = /^(hidden|clip)$/.test(st.overflowX);
const cutsY = /^(hidden|clip)$/.test(st.overflowY);
if (!cutsX && !cutsY) return false;
if (st.textOverflow === "ellipsis") return false;
if (st.webkitLineClamp && st.webkitLineClamp !== "none") return false;
const cutText = box.leafText &&
((cutsX && box.scrollWidth > box.clientWidth + 2) || (cutsY && box.scrollHeight > box.clientHeight + 2));
const img = box.img;
const cutImg = !!img && img.objectFit === "cover" && !box.cropOk &&
img.naturalWidth > 0 && img.naturalHeight > 0 && box.clientWidth > 0 && box.clientHeight > 0 &&
Math.abs(img.naturalWidth / img.naturalHeight - box.clientWidth / box.clientHeight) > 0.35;
return !!(cutText || cutImg);
},
// A box whose overflow-y lets it scroll and that has more content than room.
scrollsY: (overflowY, scrollHeight, clientHeight) =>
/^(auto|scroll)$/.test(overflowY) && scrollHeight > clientHeight,
// Dialog leaves the viewport and nothing (itself, a child, or an ancestor overlay) scrolls.
isTallDialog: (r, vw, vh, canScroll) => (r.bottom > vh + 1 || r.top < -1 || r.right > vw + 1) && !canScroll,
// Tap target under 44 px in either dimension; inline text links are exempt (WCAG 2.5.5 exception).
isSmallTarget: (r, st, tag) => {
if (tag === "a" && st.display === "inline") return false;
return r.width < TARGET || r.height < TARGET;
},
// Sticky/fixed bar that should touch an edge but leaves a gap.
// Sticky bars only stick once scrolled past, so their top gap counts only when scrollY > 0.
stickyGap: (st, r, vw, vh, scrollY) => {
const fixed = st.position === "fixed";
const sticky = st.position === "sticky";
if (!fixed && !sticky) return null;
const wantsTop = st.top !== "auto" && num(st.top) === 0;
const wantsBottom = fixed && st.bottom !== "auto" && num(st.bottom) === 0;
const judgeTop = fixed || scrollY > 0;
const gapTop = wantsTop && judgeTop && r.top > 1 && r.top < 200 ? Math.round(r.top) : 0;
const gapBottom = wantsBottom && vh - r.bottom > 1 && vh - r.bottom < 200 ? Math.round(vh - r.bottom) : 0;
const fullWidth = r.width >= vw - 2;
const gapSides = fullWidth || r.width < vw * 0.8 ? 0 : Math.round(vw - r.width);
return gapTop || gapBottom || gapSides ? { gapTop, gapBottom, gapSides } : null;
},
passes: (res) => !res.overflowX && !res.offscreen.length && !res.clipped.length &&
!res.tallDialogs.length && !res.stickyGaps.length,
};
if (typeof document === "undefined") return rules;
const vw = document.documentElement.clientWidth;
const vh = window.innerHeight;
const touch = vw <= 1024;
const cs = (el) => getComputedStyle(el);
const rect = (el) => el.getBoundingClientRect();
const describe = (el) => {
let s = el.tagName.toLowerCase();
if (el.id) s += "#" + el.id;
const cls = typeof el.className === "string" ? el.className.trim().split(/\s+/).slice(0, 3) : [];
if (cls.length && cls[0]) s += "." + cls.join(".");
const text = (el.innerText || el.getAttribute("aria-label") || "").trim().replace(/\s+/g, " ");
return text ? `${s} "${text.slice(0, 40)}"` : s;
};
const visible = (el) => rules.isVisible(cs(el), rect(el));
const all = Array.from(document.body.querySelectorAll("*")).filter(visible);
const contains = (a, b) => a.contains(b);
const ancestors = (el) => {
const out = [];
for (let p = el.parentElement; p && p !== document.body; p = p.parentElement) out.push(p);
return out;
};
// Horizontal overflow and its culprits (deepest elements past the edge, not inside a scroller).
const docW = document.documentElement.scrollWidth;
const isLoose = (el) => !rules.isContained(cs(el).position, ancestors(el).map((p) => cs(p).overflowX));
const past = all.filter((el) => rules.pastEdge(rect(el), vw));
const spill = all.filter((el) => rules.contentOverflows(cs(el), el.scrollWidth, el.clientWidth));
const culprits = rules.deepest(Array.from(new Set([...past, ...spill])), contains);
const loose = culprits.filter(isLoose);
const offscreen = rules.deepest(past, contains).filter(isLoose).slice(0, MAX).map((el) => {
const r = rect(el);
return { el: describe(el), left: Math.round(r.left), right: Math.round(r.right) };
});
const clipped = all.filter((el) => {
const img = el.querySelector(":scope > img, :scope > video");
return rules.isClipped(cs(el), {
leafText: el.children.length === 0 && (el.innerText || "").trim().length > 0,
scrollWidth: el.scrollWidth, scrollHeight: el.scrollHeight,
clientWidth: el.clientWidth, clientHeight: el.clientHeight,
cropOk: el.dataset && el.dataset.cropOk !== undefined,
img: img && { objectFit: cs(img).objectFit, naturalWidth: img.naturalWidth || img.videoWidth || 0,
naturalHeight: img.naturalHeight || img.videoHeight || 0 },
});
}).slice(0, MAX).map((el) => ({ el: describe(el), scroll: [el.scrollWidth, el.scrollHeight], box: [el.clientWidth, el.clientHeight] }));
const scrolls = (n) => rules.scrollsY(cs(n).overflowY, n.scrollHeight, n.clientHeight);
const dialogs = Array.from(document.querySelectorAll('dialog[open], [role="dialog"], [role="alertdialog"], [data-state="open"][data-radix-dialog-content], [data-vaul-drawer]')).filter(visible);
const tallDialogs = dialogs.filter((d) => {
const canScroll = scrolls(d) || Array.from(d.querySelectorAll("*")).some(scrolls) || ancestors(d).some(scrolls);
return rules.isTallDialog(rect(d), vw, vh, canScroll);
}).map((d) => { const r = rect(d); return { el: describe(d), top: Math.round(r.top), bottom: Math.round(r.bottom), height: Math.round(r.height), viewport: vh }; });
const smallTargets = touch ? Array.from(document.querySelectorAll('a[href], button, [role="button"], input:not([type="hidden"]), select, summary'))
.filter(visible).filter((el) => rules.isSmallTarget(rect(el), cs(el), el.tagName.toLowerCase()))
.slice(0, MAX).map((el) => { const r = rect(el); return { el: describe(el), size: [Math.round(r.width), Math.round(r.height)] }; }) : [];
const stickyGaps = all.map((el) => {
const g = rules.stickyGap(cs(el), rect(el), vw, vh, window.scrollY);
return g && { el: describe(el), ...g };
}).filter(Boolean).slice(0, MAX);
const result = {
url: location.pathname,
viewport: [vw, vh],
scrollY: Math.round(window.scrollY),
overflowX: rules.pageOverflows(docW, vw) ? { docWidth: docW, culprits: (loose.length ? loose : culprits).slice(0, MAX).map(describe) } : null,
offscreen, clipped, tallDialogs, smallTargets, stickyGaps,
};
result.pass = rules.passes(result);
return JSON.stringify(result);
})()