Skip to content

Commit 12c4bdf

Browse files
authored
fix(ui): prevent overlapping text in collapsed user messages (#134833)
1 parent b386a5f commit 12c4bdf

2 files changed

Lines changed: 171 additions & 10 deletions

File tree

‎ui/src/e2e/chat-user-bubble-disclosure.e2e.test.ts‎

Lines changed: 168 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { writeFile } from "node:fs/promises";
12
import path from "node:path";
23
import { expect, it } from "vitest";
34
import {
@@ -74,11 +75,12 @@ suite.define(() => {
7475
const toggle = bubble.getByRole("button", { name: "Show more" });
7576

7677
expect(await toggle.getAttribute("aria-expanded")).toBe("false");
77-
expect(await content.evaluate((element) => getComputedStyle(element).webkitLineClamp)).toBe(
78-
"5",
79-
);
80-
expect(await content.textContent()).toContain(text.slice(-18));
78+
const lineHeight = await content
79+
.locator(".chat-text")
80+
.evaluate((element) => Number.parseFloat(getComputedStyle(element).lineHeight));
81+
expect((await content.textContent())?.trim()).toBe(text);
8182
const collapsedHeight = await content.evaluate((element) => element.clientHeight);
83+
expect(collapsedHeight).toBeLessThanOrEqual(5 * lineHeight + 1);
8284
expect(await content.evaluate((element) => element.scrollHeight > element.clientHeight)).toBe(
8385
true,
8486
);
@@ -91,14 +93,173 @@ suite.define(() => {
9193
await toggle.click();
9294
const collapse = bubble.getByRole("button", { name: "Show less" });
9395
expect(await collapse.getAttribute("aria-expanded")).toBe("true");
94-
expect(await content.evaluate((element) => getComputedStyle(element).webkitLineClamp)).toBe(
95-
"none",
96-
);
9796
expect(await content.evaluate((element) => element.clientHeight)).toBeGreaterThan(
9897
collapsedHeight,
9998
);
10099
} finally {
101100
await suite.closeBrowserContext(context);
102101
}
103102
});
103+
104+
it.each([
105+
{ name: "desktop", width: 1280, height: 900 },
106+
{ name: "mobile", width: 390, height: 844 },
107+
])(
108+
"keeps collapsed paragraphs readable after reload and browser reveal ($name)",
109+
async (viewport) => {
110+
const paragraphs = [
111+
...Array.from(
112+
{ length: 8 },
113+
(_, index) =>
114+
`Background paragraph ${index + 1}. Please review the synthetic project notes and summarize the next steps. Keep the explanation clear enough for someone reading the conversation later.`,
115+
),
116+
...["First", "Second"].map((label) =>
117+
[
118+
`${label} request:`,
119+
"<review_request>",
120+
"<input>Read the sample notes and explain the next step,",
121+
"then describe the expected result.</input>",
122+
"<context>The sample is ready for review.</context>",
123+
"</review_request>",
124+
].join("\n"),
125+
),
126+
"Final prompt tail: the complete request remains available.",
127+
];
128+
const context = await suite.newBrowserContext({
129+
locale: "en-US",
130+
colorScheme: "light",
131+
serviceWorkers: "block",
132+
viewport: { width: viewport.width, height: viewport.height },
133+
});
134+
const page = await context.newPage();
135+
await installMockGateway(page, {
136+
historyMessages: [
137+
{ role: "user", content: paragraphs.join("\n\n"), timestamp: 1 },
138+
{ role: "user", content: "Short follow-up request.", timestamp: 2 },
139+
{ role: "assistant", content: "The sample review is complete.", timestamp: 3 },
140+
],
141+
});
142+
143+
try {
144+
await page.goto(`${suite.server.baseUrl}chat`);
145+
const bubble = page.locator(".chat-group.user .chat-bubble").first();
146+
const toggle = bubble.getByRole("button", { name: "Show more", exact: true });
147+
await toggle.waitFor({ state: "visible" });
148+
await page.reload();
149+
await toggle.waitFor({ state: "visible" });
150+
const content = bubble.locator(".chat-message-disclosure__content");
151+
const markdown = content.locator(".chat-text");
152+
const lineHeight = await markdown.evaluate((element) =>
153+
Number.parseFloat(getComputedStyle(element).lineHeight),
154+
);
155+
expect(await content.evaluate((element) => element.scrollTop)).toBe(0);
156+
expect(await markdown.locator("p").allTextContents()).toEqual(paragraphs);
157+
158+
// Browser descendant reveal can scroll an overflow-hidden preview without expanding it.
159+
const revealedParagraph = markdown.locator("p").nth(9);
160+
await revealedParagraph.scrollIntoViewIfNeeded();
161+
const geometry = await revealedParagraph.evaluate((paragraph) => {
162+
const clip = paragraph.closest(".chat-message-disclosure__content");
163+
if (!clip) {
164+
throw new Error("Missing user message preview");
165+
}
166+
const clipRect = clip.getBoundingClientRect();
167+
const paragraphRect = paragraph.getBoundingClientRect();
168+
// Read individual text nodes so BR boxes and duplicate element fragments are excluded.
169+
const walker = document.createTreeWalker(paragraph, NodeFilter.SHOW_TEXT);
170+
const lines: Array<{ top: number; bottom: number }> = [];
171+
for (let node = walker.nextNode(); node; node = walker.nextNode()) {
172+
if (!node.textContent?.trim()) {
173+
continue;
174+
}
175+
const range = document.createRange();
176+
range.selectNodeContents(node);
177+
for (const rect of range.getClientRects()) {
178+
if (rect.width > 0 && rect.height > 0) {
179+
lines.push({ top: rect.top, bottom: rect.bottom });
180+
}
181+
}
182+
}
183+
return {
184+
scrollTop: clip.scrollTop,
185+
clip: { top: clipRect.top, bottom: clipRect.bottom },
186+
paragraph: { top: paragraphRect.top, bottom: paragraphRect.bottom },
187+
lines,
188+
};
189+
});
190+
if (captureUiProofEnabled) {
191+
const artifactDir = path.join(suite.artifactDir, "user-bubble-clamp");
192+
await page.screenshot({ path: path.join(artifactDir, `${viewport.name}-revealed.png`) });
193+
await writeFile(
194+
path.join(artifactDir, `${viewport.name}-geometry.json`),
195+
`${JSON.stringify(geometry, null, 2)}\n`,
196+
);
197+
}
198+
expect(await toggle.getAttribute("aria-expanded")).toBe("false");
199+
expect(geometry.scrollTop).toBeGreaterThan(0);
200+
expect(
201+
geometry.lines.filter(
202+
(line) => line.top >= geometry.clip.top && line.bottom <= geometry.clip.bottom,
203+
).length,
204+
).toBeGreaterThan(0);
205+
expect(geometry.lines.length).toBeGreaterThanOrEqual(6);
206+
for (const line of geometry.lines) {
207+
expect(line.top, "text starts within its own paragraph").toBeGreaterThanOrEqual(
208+
geometry.paragraph.top - 1,
209+
);
210+
expect(
211+
line.bottom,
212+
"text ends within its own paragraph without overlapping the next",
213+
).toBeLessThanOrEqual(geometry.paragraph.bottom + 1);
214+
}
215+
const collapsedHeight = await content.evaluate((element) => element.clientHeight);
216+
expect(collapsedHeight).toBeLessThanOrEqual(5 * lineHeight + 1);
217+
218+
await toggle.click();
219+
const collapse = bubble.getByRole("button", { name: "Show less", exact: true });
220+
expect(await collapse.getAttribute("aria-expanded")).toBe("true");
221+
expect(await content.evaluate((element) => element.clientHeight)).toBeGreaterThan(
222+
collapsedHeight,
223+
);
224+
expect(
225+
await content.evaluate((element) => element.scrollHeight - element.clientHeight),
226+
).toBeLessThanOrEqual(1);
227+
const tail = markdown.locator("p").last();
228+
await tail.scrollIntoViewIfNeeded();
229+
expect(await tail.textContent()).toBe(paragraphs.at(-1));
230+
if (captureUiProofEnabled) {
231+
await page.screenshot({
232+
path: path.join(
233+
suite.artifactDir,
234+
"user-bubble-clamp",
235+
`${viewport.name}-expanded-tail.png`,
236+
),
237+
});
238+
}
239+
await collapse.click();
240+
expect(await toggle.getAttribute("aria-expanded")).toBe("false");
241+
expect(await content.evaluate((element) => element.clientHeight)).toBeLessThanOrEqual(
242+
5 * lineHeight + 1,
243+
);
244+
const siblings = page.locator(".chat-group .chat-bubble");
245+
expect(await siblings.nth(1).textContent()).toContain("Short follow-up request.");
246+
expect(await siblings.nth(2).textContent()).toContain("The sample review is complete.");
247+
const boxes = await siblings.evaluateAll((elements) =>
248+
elements.map((element) => {
249+
const rect = element.getBoundingClientRect();
250+
return { top: rect.top, bottom: rect.bottom };
251+
}),
252+
);
253+
expect(boxes).toHaveLength(3);
254+
for (const [index, box] of boxes.entries()) {
255+
const previous = boxes[index - 1];
256+
if (previous) {
257+
expect(previous.bottom).toBeLessThanOrEqual(box.top + 1);
258+
}
259+
}
260+
} finally {
261+
await suite.closeBrowserContext(context);
262+
}
263+
},
264+
);
104265
});

‎ui/src/styles/chat/text.css‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@
3939
}
4040

4141
.chat-message-disclosure:not(.is-expanded) .chat-message-disclosure__content {
42-
display: -webkit-box;
42+
/* Legacy line clamp compresses hidden blocks that browser descendant reveal can
43+
expose as overlapping text. Clip the natural Markdown layout instead. */
44+
max-height: calc(var(--chat-text-size) * 1.5 * 5);
4345
overflow: hidden;
44-
-webkit-box-orient: vertical;
45-
-webkit-line-clamp: 5;
4646
}
4747

4848
.chat-message-disclosure__toggle {

0 commit comments

Comments
 (0)