Skip to content

Commit a9df413

Browse files
fix(ui): keep the GitHub mark on its label's line when chat wraps
The mark is an in-flow atomic inline, which carries a soft wrap opportunity after it, so a chat line could end on the octocat and start the next one with the reference it labels. Painting the mark out of flow and reserving its space with the anchor's own padding removes that opportunity: nothing inside the anchor precedes the label any more. The anchor stays inline, so a long bare URL still fills the line it starts on instead of moving down whole -- the sibling file-link chip's atomic treatment could not be reused here.
1 parent e04dfd2 commit a9df413

2 files changed

Lines changed: 194 additions & 7 deletions

File tree

Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
// Control UI tests cover how GitHub links present in chat when a line wraps.
2+
import fs from "node:fs";
3+
import os from "node:os";
4+
import path from "node:path";
5+
import { chromium, type Browser } from "playwright";
6+
import { afterAll, beforeAll, describe, expect, it } from "vitest";
7+
import { readStyleSheet } from "../../../test/helpers/ui-style-fixtures.js";
8+
import {
9+
canRunPlaywrightChromium,
10+
resolvePlaywrightChromiumExecutablePath,
11+
} from "../test-helpers/control-ui-e2e.ts";
12+
13+
const chromiumExecutablePath = resolvePlaywrightChromiumExecutablePath(chromium.executablePath());
14+
const describeGitHubLinkPresentation = canRunPlaywrightChromium(chromiumExecutablePath)
15+
? describe
16+
: describe.skip;
17+
18+
function readChatCss(): string {
19+
return ["ui/src/styles/base.css", "ui/src/styles/chat/text.css"]
20+
.map((file) => readStyleSheet(file))
21+
.join("\n");
22+
}
23+
24+
// The three shapes the parser can produce, all carrying the same mark:
25+
// a bare item URL whose label it rewrites to owner/repo#number, a bare URL to
26+
// any other GitHub path, and an authored label. Only the first two carry
27+
// markdown-bare-url, so the sweep covers both wrap regimes.
28+
const LINK_FORMS = [
29+
{
30+
className: "markdown-bare-url markdown-github-link",
31+
id: "human-ref",
32+
label: "openclaw/openclaw#123309",
33+
lead: "then follow-up tracked in ",
34+
},
35+
{
36+
className: "markdown-bare-url markdown-github-link",
37+
id: "bare-url",
38+
label: "https://github.com/openclaw/openclaw/blob/main/ui/src/styles/chat/text.css#L254",
39+
lead: "then the owning rule lives at ",
40+
},
41+
{
42+
className: "markdown-github-link",
43+
id: "authored",
44+
label: "the sibling chip rule",
45+
lead: "then see ",
46+
},
47+
] as const;
48+
49+
function fixtureDocument(themeMode: "dark" | "light"): string {
50+
const themeAttributes =
51+
themeMode === "light" ? `data-theme="light" data-theme-mode="light"` : `data-theme="dark"`;
52+
const columns = LINK_FORMS.map(
53+
({ className, id, label, lead }) => `
54+
<div class="chat-text" id="column-${id}">Reproduce the failing run and read the notes
55+
first, ${lead}<a id="${id}" class="${className}" href="https://github.com/openclaw/openclaw"
56+
>${label}</a> before landing the fix.</div>`,
57+
).join("");
58+
return `<!doctype html><html ${themeAttributes}><head><style>${readChatCss()}</style></head>
59+
<body>${columns}</body></html>`;
60+
}
61+
62+
type WrapSample = {
63+
readonly columnWidth: number;
64+
readonly fragments: number;
65+
readonly labelStartsMarkLine: boolean;
66+
readonly markLineTop: number;
67+
};
68+
69+
// Sweeping the column instead of picking one width: the mark has to stay with
70+
// its label at every position the reference can land in, and a single tuned
71+
// width stops exercising the wrap boundary as soon as prose or font metrics move.
72+
async function probeWrap(
73+
themeMode: "dark" | "light",
74+
): Promise<Record<string, readonly WrapSample[]>> {
75+
const fixtureFile = path.join(fixtureDirectory, `${themeMode}.html`);
76+
fs.writeFileSync(fixtureFile, fixtureDocument(themeMode), "utf8");
77+
const page = await browser.newPage();
78+
try {
79+
await page.goto(`file://${fixtureFile}`);
80+
return await page.evaluate(
81+
(ids: readonly string[]) => {
82+
const resolve = (selector: string) => {
83+
const element = document.querySelector(selector);
84+
if (!(element instanceof HTMLElement)) {
85+
throw new Error(`Missing GitHub link fixture element for ${selector}`);
86+
}
87+
return element;
88+
};
89+
const samples: Record<string, WrapSample[]> = {};
90+
for (const id of ids) {
91+
const column = resolve(`#column-${id}`);
92+
const link = resolve(`#${id}`);
93+
const collected: WrapSample[] = [];
94+
for (let columnWidth = 200; columnWidth <= 900; columnWidth += 4) {
95+
column.style.width = `${columnWidth}px`;
96+
// The mark is painted at the start of the link box, so the link box
97+
// and the first label character sharing a line is exactly the
98+
// invariant: the mark is never left behind on the previous line.
99+
const linkStart = link.getClientRects()[0];
100+
const labelRange = document.createRange();
101+
const labelText = link.firstChild;
102+
if (!labelText || !linkStart) {
103+
throw new Error(`Missing label geometry for ${id}`);
104+
}
105+
labelRange.setStart(labelText, 0);
106+
labelRange.setEnd(labelText, 1);
107+
const labelStart = labelRange.getBoundingClientRect();
108+
collected.push({
109+
columnWidth,
110+
fragments: link.getClientRects().length,
111+
labelStartsMarkLine: Math.abs(labelStart.top - linkStart.top) < 2,
112+
markLineTop: Math.round(linkStart.top),
113+
});
114+
}
115+
samples[id] = collected;
116+
}
117+
return samples;
118+
},
119+
LINK_FORMS.map((form) => form.id),
120+
);
121+
} finally {
122+
await page.close();
123+
}
124+
}
125+
126+
let browser: Browser;
127+
let fixtureDirectory: string;
128+
129+
beforeAll(async () => {
130+
if (!canRunPlaywrightChromium(chromiumExecutablePath)) {
131+
return;
132+
}
133+
// Resolve the temp root: macOS hands back a /var symlink and the file:// URL
134+
// must be the canonical path.
135+
fixtureDirectory = fs.realpathSync(
136+
fs.mkdtempSync(path.join(os.tmpdir(), "chat-github-link-presentation-")),
137+
);
138+
browser = await chromium.launch({ executablePath: chromiumExecutablePath, headless: true });
139+
});
140+
141+
afterAll(async () => {
142+
await browser?.close();
143+
if (fixtureDirectory) {
144+
fs.rmSync(fixtureDirectory, { force: true, recursive: true });
145+
}
146+
});
147+
148+
describeGitHubLinkPresentation("chat GitHub link presentation", () => {
149+
it.each(["light", "dark"] as const)(
150+
"keeps the GitHub mark on its label's line at every column width in %s",
151+
async (themeMode) => {
152+
const samples = await probeWrap(themeMode);
153+
for (const { id } of LINK_FORMS) {
154+
const collected = samples[id] ?? [];
155+
// Vacuity guard: the reference must actually move between lines across
156+
// the sweep, or the assertion below passes on prose that never wraps.
157+
expect(new Set(collected.map((sample) => sample.markLineTop)).size).toBeGreaterThan(1);
158+
const stranded = collected.filter((sample) => !sample.labelStartsMarkLine);
159+
expect({ id, stranded }).toEqual({ id, stranded: [] });
160+
}
161+
},
162+
);
163+
164+
it("keeps GitHub links breaking across lines instead of moving whole", async () => {
165+
const samples = await probeWrap("dark");
166+
// The file-link chip answers the same invariant by making the whole anchor
167+
// atomic. That is the wrong answer here: an atomic anchor never fragments,
168+
// so a long URL would move to the next line rather than break inside it and
169+
// leave the line it should have filled ragged. Both forms must still split.
170+
for (const id of ["bare-url", "authored"]) {
171+
const collected = samples[id] ?? [];
172+
expect({ id, splits: collected.some((sample) => sample.fragments > 1) }).toEqual({
173+
id,
174+
splits: true,
175+
});
176+
}
177+
});
178+
});

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

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -255,11 +255,13 @@
255255
--github-mark: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath fill='%23000' d='M12 .5C5.65.5.5 5.65.5 12c0 5.08 3.29 9.39 7.86 10.91.58.11.79-.25.79-.56 0-.27-.01-1.17-.02-2.12-3.2.7-3.87-1.36-3.87-1.36-.52-1.33-1.28-1.68-1.28-1.68-1.04-.71.08-.7.08-.7 1.15.08 1.76 1.19 1.76 1.19 1.03 1.76 2.7 1.25 3.36.96.1-.75.4-1.25.72-1.54-2.55-.29-5.24-1.28-5.24-5.69 0-1.26.45-2.28 1.19-3.09-.12-.29-.52-1.46.11-3.05 0 0 .97-.31 3.18 1.18a11.1 11.1 0 0 1 5.78 0c2.21-1.49 3.18-1.18 3.18-1.18.63 1.59.23 2.76.11 3.05.74.81 1.19 1.83 1.19 3.09 0 4.42-2.7 5.39-5.26 5.68.41.35.78 1.05.78 2.12 0 1.53-.01 2.76-.01 3.14 0 .3.21.67.8.55A11.5 11.5 0 0 0 23.5 12C23.5 5.65 18.35.5 12 .5Z'/%3E%3C/svg%3E");
256256

257257
content: "";
258-
display: inline-block;
258+
position: absolute;
259+
inset-inline-start: 0;
260+
/* vertical-align cannot reach an out-of-flow box, so the baseline shift it
261+
used to carry becomes an offset from the first line box's top edge. */
262+
top: 0.1em;
259263
width: 1em;
260264
height: 1em;
261-
margin-inline-end: 0.3em;
262-
vertical-align: -0.14em;
263265
background: currentColor;
264266
mask: var(--github-mark) center / contain no-repeat;
265267
-webkit-mask: var(--github-mark) center / contain no-repeat;
@@ -269,6 +271,13 @@
269271
signal and clutters dense URL text. Hover restores it as the affordance,
270272
like the file-link convention below. */
271273
.chat-text :where(a.markdown-github-link) {
274+
/* The mark is painted out of flow with its space reserved here, so nothing
275+
inside the anchor precedes the label: an in-flow atomic mark carries a soft
276+
wrap opportunity after it, so a line could end on the mark alone. The anchor
277+
stays inline so the bare-URL rule below can still fill the line a long URL
278+
starts on; the file-link chip's `display: inline-block` could not. */
279+
position: relative;
280+
padding-inline-start: 1.3em; /* the mark's 1em box plus the 0.3em gap it had */
272281
text-decoration: none;
273282
}
274283

@@ -277,10 +286,10 @@
277286
}
278287

279288
/* The inherited `overflow-wrap: anywhere` breaks a word only once every other
280-
option is gone, so a long URL is pushed whole onto the next line — stranding
281-
the mark alone at the end of the previous one. Unconditional break points
282-
make the URL fill the line it starts on; authored labels are excluded so
283-
prose still wraps on word boundaries. */
289+
option is gone, so a long URL is pushed whole onto the next line, leaving the
290+
line it should have filled ragged. Unconditional break points make the URL
291+
fill the line it starts on; authored labels are excluded so prose still wraps
292+
on word boundaries. */
284293
.chat-text :where(a.markdown-bare-url) {
285294
line-break: anywhere;
286295
}

0 commit comments

Comments
 (0)