Skip to content

Write HTML. Render video. Built for agents.

fix(layout): flag nowrap text that still sticks out of a container that does not clip (#4379)

Xuanru Li549a2f51bb26
diff --git a/packages/cli/src/commands/layout-audit.browser.js b/packages/cli/src/commands/layout-audit.browser.js
index c3f175578c..b3683bcfd5 100644
--- a/packages/cli/src/commands/layout-audit.browser.js
+++ b/packages/cli/src/commands/layout-audit.browser.js
@@ -27,6 +27,16 @@
return Math.round(value * 100) / 100;
}
+ function horizontalOverflow(subject, container, tolerance) {
+ if (subject.width <= container.width + tolerance) return null;
+ const overflow = overflowFor(subject, container, tolerance);
+ if (!overflow) return null;
+ const horizontal = {};
+ if (overflow.left != null) horizontal.left = overflow.left;
+ if (overflow.right != null) horizontal.right = overflow.right;
+ return Object.keys(horizontal).length > 0 ? horizontal : null;
+ }
+
function overflowFor(subject, container, tolerance, vTolerance) {
// Horizontal axis uses `tolerance`; vertical axis uses `vTolerance` (defaults to the same).
// A separate vertical tolerance lets text overflow checks absorb glyph ink that exceeds a
@@ -550,20 +560,40 @@
return issues;
}
+ function isNowrapTextChild(child) {
+ if (!isVisibleElement(child) || hasAllowOverflowFlag(child)) return false;
+ if (getComputedStyle(child).whiteSpace !== "nowrap") return false;
+ return (child.textContent || "").trim().length > 0;
+ }
+
+ function hasNowrapTextChild(element) {
+ return Array.from(element.children).some(isNowrapTextChild);
+ }
+
function containerOverflowIssues(root, time, tolerance) {
const issues = [];
const containers = Array.from(root.querySelectorAll("*")).filter((element) => {
if (!isVisibleElement(element) || hasAllowOverflowFlag(element)) return false;
const style = getComputedStyle(element);
- return clipsOverflow(style) || element.hasAttribute("data-layout-boundary");
+ return (
+ clipsOverflow(style) ||
+ element.hasAttribute("data-layout-boundary") ||
+ hasNowrapTextChild(element)
+ );
});
for (const container of containers) {
+ const style = getComputedStyle(container);
+ const checksEveryChild =
+ clipsOverflow(style) || container.hasAttribute("data-layout-boundary");
const containerRect = toRect(container.getBoundingClientRect());
for (const child of Array.from(container.children)) {
if (!isVisibleElement(child) || hasAllowOverflowFlag(child)) continue;
+ if (!checksEveryChild && !isNowrapTextChild(child)) continue;
const childRect = toRect(child.getBoundingClientRect());
- const overflow = overflowFor(childRect, containerRect, tolerance);
+ const overflow = checksEveryChild
+ ? overflowFor(childRect, containerRect, tolerance)
+ : horizontalOverflow(childRect, containerRect, tolerance);
if (!overflow) continue;
issues.push({
code: "container_overflow",
@@ -571,7 +601,9 @@
time,
selector: selectorFor(child),
containerSelector: selectorFor(container),
- message: "Element extends outside a clipping layout container.",
+ message: checksEveryChild
+ ? "Element extends outside a clipping layout container."
+ : "Nowrap text is wider than its container.",
rect: childRect,
containerRect,
overflow,
diff --git a/packages/cli/src/commands/layout-audit.browser.test.ts b/packages/cli/src/commands/layout-audit.browser.test.ts
index 29825e6cef..0a11d2f7fa 100644
--- a/packages/cli/src/commands/layout-audit.browser.test.ts
+++ b/packages/cli/src/commands/layout-audit.browser.test.ts
@@ -272,6 +272,172 @@ describe("layout-audit.browser", () => {
expect(issues[0]?.selector).toBe('[data-layout-name="headline"]');
});
+ it("flags nowrap text wider than a container that does not clip", () => {
+ document.body.innerHTML = `
+ <div id="root" data-composition-id="main" data-width="1920" data-height="1080">
+ <div id="box">
+ <span id="word">OVERPAYING</span>
+ </div>
+ </div>
+ `;
+
+ installGeometry(
+ {
+ root: rect({ left: 0, top: 0, width: 1920, height: 1080 }),
+ box: rect({ left: 96, top: 533, width: 576, height: 140 }),
+ word: rect({ left: 96, top: 533, width: 823, height: 140 }),
+ },
+ { word: { whiteSpace: "nowrap" } },
+ );
+
+ installAuditScript();
+
+ const overflow = runAudit().find((issue) => issue.code === "container_overflow");
+ expect(overflow).toMatchObject({
+ selector: "#word",
+ containerSelector: "#box",
+ });
+ });
+
+ it("clears nowrap container overflow when the container allows overflow", () => {
+ document.body.innerHTML = `
+ <div id="root" data-composition-id="main" data-width="1920" data-height="1080">
+ <div id="box" data-layout-allow-overflow>
+ <span id="word">OVERPAYING</span>
+ </div>
+ </div>
+ `;
+
+ installGeometry(
+ {
+ root: rect({ left: 0, top: 0, width: 1920, height: 1080 }),
+ box: rect({ left: 96, top: 533, width: 576, height: 140 }),
+ word: rect({ left: 96, top: 533, width: 823, height: 140 }),
+ },
+ { word: { whiteSpace: "nowrap" } },
+ );
+
+ installAuditScript();
+
+ expect(runAudit().some((issue) => issue.code === "container_overflow")).toBe(false);
+ });
+
+ it("does not flag font-height spill from nowrap text that fits its width", () => {
+ document.body.innerHTML = `
+ <div id="root" data-composition-id="main" data-width="1920" data-height="1080">
+ <div id="box">
+ <span id="hi">Hi</span>
+ </div>
+ </div>
+ `;
+
+ installGeometry(
+ {
+ root: rect({ left: 0, top: 0, width: 1920, height: 1080 }),
+ box: rect({ left: 0, top: 0, width: 150, height: 64 }),
+ hi: rect({ left: 0, top: -4, width: 60.44, height: 72 }),
+ },
+ { hi: { whiteSpace: "nowrap", fontSize: "64px" } },
+ );
+
+ installAuditScript();
+
+ expect(runAudit().some((issue) => issue.code === "container_overflow")).toBe(false);
+ });
+
+ it("does not flag a narrow nowrap label that sits outside a box that does not clip", () => {
+ document.body.innerHTML = `
+ <div id="root" data-composition-id="main" data-width="1920" data-height="1080">
+ <div id="box">
+ <span id="badge">NEW</span>
+ </div>
+ </div>
+ `;
+
+ installGeometry(
+ {
+ root: rect({ left: 0, top: 0, width: 1920, height: 1080 }),
+ box: rect({ left: 0, top: 0, width: 200, height: 80 }),
+ badge: rect({ left: 184, top: 0, width: 32, height: 20 }),
+ },
+ { badge: { whiteSpace: "nowrap" } },
+ );
+
+ installAuditScript();
+
+ expect(runAudit().some((issue) => issue.code === "container_overflow")).toBe(false);
+ });
+
+ it("does not flag an empty nowrap decoration beside a fitting label", () => {
+ document.body.innerHTML = `
+ <div id="root" data-composition-id="main" data-width="1920" data-height="1080">
+ <div id="box">
+ <span id="label">OK</span>
+ <div id="decoration"></div>
+ </div>
+ </div>
+ `;
+
+ installGeometry(
+ {
+ root: rect({ left: 0, top: 0, width: 1920, height: 1080 }),
+ box: rect({ left: 0, top: 0, width: 150, height: 40 }),
+ label: rect({ left: 0, top: 0, width: 40, height: 20 }),
+ decoration: rect({ left: 0, top: 0, width: 240, height: 20 }),
+ },
+ { label: { whiteSpace: "nowrap" }, decoration: { whiteSpace: "nowrap" } },
+ );
+
+ installAuditScript();
+
+ expect(runAudit().some((issue) => issue.code === "container_overflow")).toBe(false);
+ });
+
+ it("does not flag a wrapping sibling because another child is nowrap", () => {
+ document.body.innerHTML = `
+ <div id="root" data-composition-id="main" data-width="1920" data-height="1080">
+ <div id="box">
+ <span id="label">OK</span>
+ <span id="body">a wrapping line</span>
+ </div>
+ </div>
+ `;
+
+ installGeometry(
+ {
+ root: rect({ left: 0, top: 0, width: 1920, height: 1080 }),
+ box: rect({ left: 0, top: 0, width: 150, height: 80 }),
+ label: rect({ left: 0, top: 0, width: 40, height: 20 }),
+ body: rect({ left: 0, top: 24, width: 240, height: 40 }),
+ },
+ { label: { whiteSpace: "nowrap" }, body: { whiteSpace: "normal" } },
+ );
+
+ installAuditScript();
+
+ expect(runAudit().some((issue) => issue.code === "container_overflow")).toBe(false);
+ });
+
+ it("does not flag a container that does not clip when its text wraps", () => {
+ document.body.innerHTML = `
+ <div id="root" data-composition-id="main" data-width="1920" data-height="1080">
+ <div id="box">
+ <span id="word">OVERPAYING</span>
+ </div>
+ </div>
+ `;
+
+ installGeometry({
+ root: rect({ left: 0, top: 0, width: 1920, height: 1080 }),
+ box: rect({ left: 96, top: 533, width: 576, height: 140 }),
+ word: rect({ left: 96, top: 533, width: 823, height: 140 }),
+ });
+
+ installAuditScript();
+
+ expect(runAudit().some((issue) => issue.code === "container_overflow")).toBe(false);
+ });
+
it("respects layout ignore and allow-overflow opt-outs", () => {
document.body.innerHTML = `
<div data-composition-id="main" data-width="640" data-height="360">