Skip to content

Commit da4bb3c

Browse files
committed
fix(web): keep the HTML flavor when the browser lacks ClipboardItem
1 parent 7fa312a commit da4bb3c

2 files changed

Lines changed: 13 additions & 4 deletions

File tree

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ import { MarkdownCodeBlock } from "./MarkdownCodeBlock";
77
import { MarkdownTable } from "./MarkdownTable";
88

99
// Insecure origins (a LAN or tailnet address over plain http) have no async clipboard.
10-
function withoutAsyncClipboard() {
10+
function withoutAsyncClipboard(clipboard?: Partial<Clipboard>) {
1111
const original = Object.getOwnPropertyDescriptor(Navigator.prototype, "clipboard");
12-
Object.defineProperty(navigator, "clipboard", { configurable: true, value: undefined });
12+
Object.defineProperty(navigator, "clipboard", { configurable: true, value: clipboard });
1313
const copied = new DataTransfer();
1414
const execCommand = vi.spyOn(document, "execCommand").mockImplementation(() => {
1515
document.activeElement?.dispatchEvent(
@@ -52,7 +52,11 @@ describe("markdown copy without the async clipboard", () => {
5252
expect(clipboard.execCommand).toHaveBeenCalledWith("copy");
5353
});
5454

55-
it("copies a table through the execCommand fallback", async () => {
55+
it.each([
56+
["no async clipboard", undefined],
57+
// Firefox before 127 has writeText but no ClipboardItem.
58+
["writeText only", { writeText: () => Promise.resolve() }],
59+
])("copies a table through the execCommand fallback with %s", async (_, asyncClipboard) => {
5660
await using screen = await render(
5761
<MarkdownTable>
5862
<thead>
@@ -69,7 +73,7 @@ describe("markdown copy without the async clipboard", () => {
6973
// The table renders no links and reads no route.
7074
{ router: false },
7175
);
72-
using clipboard = withoutAsyncClipboard();
76+
using clipboard = withoutAsyncClipboard(asyncClipboard);
7377

7478
await userEvent.click(screen.getByRole("button", { name: "Copy table" }));
7579
await userEvent.click(page.getByRole("menuitem", { name: "Copy as rich text" }));

‎apps/web/src/hooks/useCopyToClipboard.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,11 @@ export async function writeTextToClipboard(
130130
});
131131
}
132132

133+
// Without ClipboardItem (Firefox before 127), only the copy event can carry extra flavors.
134+
if (extraFlavors && (typeof ClipboardItem === "undefined" || !navigator.clipboard.write)) {
135+
if (writeTextWithExecCommand(value, extraFlavors)) return true;
136+
}
137+
133138
try {
134139
if (extraFlavors && typeof ClipboardItem !== "undefined" && navigator.clipboard.write) {
135140
try {

0 commit comments

Comments
 (0)