Skip to content

Commit 4ada1e5

Browse files
committed
fix(web): preserve image placeholder text in table copies
1 parent ac911aa commit 4ada1e5

3 files changed

Lines changed: 38 additions & 1 deletion

File tree

‎apps/web/src/components/chat/ChatMarkdownMedia.browser.test.tsx‎

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ import { EnvironmentId, ThreadId } from "@t3tools/contracts";
33
import { describe, expect, it, vi } from "vite-plus/test";
44

55
import { render } from "../../../test/render";
6-
import { ChatMarkdownAssetImage } from "./ChatMarkdownMedia";
6+
import { serializeTableElementToDelimited } from "../../markdown-clipboard";
7+
import { ChatMarkdownAssetImage, ChatMarkdownImage } from "./ChatMarkdownMedia";
78

89
// Every evaluation of the URL query is one mint: the first read, then one per refresh.
910
const assetQuery = vi.hoisted(() => ({ mints: 0 }));
@@ -84,3 +85,34 @@ describe("ChatMarkdownAssetImage", () => {
8485
expect(assetQuery.mints).toBe(2);
8586
});
8687
});
88+
89+
it.each([
90+
{ standalone: false, sourceFailed: false },
91+
{ standalone: false, sourceFailed: true },
92+
{ standalone: true, sourceFailed: false },
93+
{ standalone: true, sourceFailed: true },
94+
])("copies image alt text while unavailable: %j", async ({ standalone, sourceFailed }) => {
95+
assetQuery.mints = 0;
96+
await using screen = await render(
97+
<table>
98+
<tbody>
99+
<tr>
100+
<td>
101+
<ChatMarkdownImage
102+
src={null}
103+
alt="Build badge"
104+
copyMarkdown="![Build badge](badge.png)"
105+
standalone={standalone}
106+
sourceFailed={sourceFailed}
107+
actionsSource={{ kind: "image", name: "Build badge", src: null }}
108+
/>
109+
</td>
110+
<td>next</td>
111+
</tr>
112+
</tbody>
113+
</table>,
114+
);
115+
const table = screen.container.querySelector("table")!;
116+
expect(serializeTableElementToDelimited(table, "tsv")).toBe("Build badge\tnext");
117+
expect(serializeTableElementToDelimited(table, "csv")).toBe("Build badge,next");
118+
});

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ export function ChatMarkdownImageFallback(props: {
125125
const content = (
126126
<span
127127
data-markdown-copy={props.copyMarkdown}
128+
data-markdown-alt={props.alt}
128129
className={cn(
129130
CHAT_MARKDOWN_MEDIA_LAYOUT_CLASS_NAME,
130131
"rounded-md border border-border/40 bg-muted/40 px-2 py-1 text-xs text-muted-foreground",
@@ -230,6 +231,7 @@ export function ChatMarkdownImage(props: {
230231
<span
231232
id={props.imageProps?.id}
232233
data-markdown-copy={props.copyMarkdown}
234+
data-markdown-alt={props.alt}
233235
role="status"
234236
aria-label="Loading image"
235237
className={CHAT_MARKDOWN_MEDIA_LAYOUT_CLASS_NAME}
@@ -241,6 +243,7 @@ export function ChatMarkdownImage(props: {
241243
<span
242244
id={props.imageProps?.id}
243245
data-markdown-copy={props.copyMarkdown}
246+
data-markdown-alt={props.alt}
244247
className={cn(
245248
CHAT_MARKDOWN_MEDIA_LAYOUT_CLASS_NAME,
246249
CHAT_MARKDOWN_IMAGE_FRAME_CLASS_NAME,

‎apps/web/src/markdown-clipboard.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,8 @@ function tableCellText(node: Node): string {
351351
return element.getAttribute("data-markdown-copy")?.trim() ?? "";
352352
}
353353
if (element.tagName === "BR") return " ";
354+
const placeholderAlt = element.getAttribute("data-markdown-alt");
355+
if (placeholderAlt !== null) return placeholderAlt;
354356
if (element.tagName === "IMG") return element.getAttribute("alt") ?? "";
355357
return [...node.childNodes].map(tableCellText).join("");
356358
}

0 commit comments

Comments
 (0)