Skip to content

Commit eb5612d

Browse files
fix(web): prevent long links from creating per-character elements (#26843)
Co-authored-by: ALMs <[email protected]> Co-authored-by: maria-rcks <[email protected]>
1 parent e13c0a9 commit eb5612d

2 files changed

Lines changed: 64 additions & 12 deletions

File tree

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import { expect, it, vi } from "vite-plus/test";
2+
3+
import { render } from "../../../test/render";
4+
import "../../index.css";
5+
6+
import { MarkdownLinkContext } from "./ChatMarkdownMedia";
7+
import { MarkdownExternalLinkContent } from "./MarkdownExternalLink";
8+
9+
it.each(["plain", "formatted"] as const)(
10+
"keeps %s long links bounded in the DOM without losing text or wrapping",
11+
async (kind) => {
12+
const link = (url: string) => (
13+
<div className="chat-markdown w-64">
14+
<table>
15+
<tbody>
16+
<tr>
17+
<td>
18+
<a href={url}>
19+
<MarkdownExternalLinkContent
20+
host="github.com"
21+
plainText={kind === "plain" ? url : null}
22+
>
23+
<MarkdownLinkContext value>
24+
{url}
25+
{kind === "formatted" ? <strong> details</strong> : null}
26+
</MarkdownLinkContext>
27+
</MarkdownExternalLinkContent>
28+
</a>
29+
</td>
30+
</tr>
31+
</tbody>
32+
</table>
33+
</div>
34+
);
35+
const url = (length: number) => `https://github.com/search?q=${"a".repeat(length)}`;
36+
await using screen = await render(link(url(128)));
37+
const initialElementCount = screen.container.querySelectorAll("a *").length;
38+
39+
const longUrl = url(16_000);
40+
await screen.rerender(link(longUrl));
41+
await vi.waitFor(() => {
42+
const anchor = screen.container.querySelector("a")!;
43+
const table = screen.container.querySelector("table")!;
44+
expect(anchor.getAttribute("href")).toBe(longUrl);
45+
expect(anchor.textContent).toBe(longUrl + (kind === "formatted" ? " details" : ""));
46+
expect(anchor.querySelectorAll("*")).toHaveLength(initialElementCount);
47+
if (kind === "formatted")
48+
expect(anchor.querySelector("strong")?.textContent).toBe(" details");
49+
expect(table.scrollWidth).toBeLessThanOrEqual(
50+
screen.container.firstElementChild!.clientWidth,
51+
);
52+
});
53+
},
54+
);

‎apps/web/src/components/chat/MarkdownExternalLink.tsx‎

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { faviconUrlForOrigin } from "@t3tools/shared/favicon";
22
import { GlobeIcon } from "lucide-react";
3-
import { Children, Fragment, memo, useState, type ReactNode } from "react";
3+
import { Children, memo, useState, type ReactNode } from "react";
44

55
import { sourceControlClients } from "@t3tools/client-runtime/source-control-clients";
66

@@ -54,18 +54,16 @@ function leadingExternalLinkTextLength(text: string): number {
5454
return Math.min(text.length, 1);
5555
}
5656

57-
// Long words such as URLs can break after any character so they fit narrow
58-
// table columns. Short ones, like a PR number, stay whole.
57+
// Keep short labels such as PR numbers intact in narrow table columns.
5958
function breakableExternalLinkText(text: string): ReactNode[] {
6059
return Array.from(text.matchAll(/\s+|\S+/g), ({ 0: word, index: start }) =>
61-
word.length <= 16
62-
? word
63-
: Array.from(word, (character, offset) => (
64-
<Fragment key={`${start + offset}:${character}`}>
65-
{character}
66-
<wbr />
67-
</Fragment>
68-
)),
60+
word.length <= 16 ? (
61+
word
62+
) : (
63+
<span key={start} className="[overflow-wrap:anywhere]">
64+
{word}
65+
</span>
66+
),
6967
);
7068
}
7169

@@ -110,7 +108,7 @@ export function MarkdownExternalLinkContent({
110108

111109
return (
112110
<>
113-
<span className="whitespace-nowrap">
111+
<span className="[overflow-wrap:anywhere]">
114112
<MarkdownLinkFavicon host={host} />
115113
{firstChild}
116114
</span>

0 commit comments

Comments
 (0)