Skip to content

Commit a881e4f

Browse files
fix(web): copy row-spanning table selections as rows
A range across several cells or rows clones without its table, so the fragment serialized as run-together cell text. Rebuild the table shell around the fragment before serializing. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 725824e commit a881e4f

2 files changed

Lines changed: 68 additions & 2 deletions

File tree

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// @vitest-environment jsdom
2+
3+
import { afterEach, describe, expect, it } from "vite-plus/test";
4+
5+
import { chatMarkdownClipboardPayload } from "./markdown-clipboard";
6+
7+
function selectBetween(start: Node, end: Node): Selection {
8+
const range = document.createRange();
9+
range.setStart(start, 0);
10+
range.setEnd(end, end.childNodes.length);
11+
const selection = window.getSelection();
12+
if (!selection) throw new Error("jsdom has no selection");
13+
selection.removeAllRanges();
14+
selection.addRange(range);
15+
return selection;
16+
}
17+
18+
describe("chatMarkdownClipboardPayload", () => {
19+
afterEach(() => {
20+
document.body.innerHTML = "";
21+
});
22+
23+
it("copies a selection spanning table rows as rows, even though the range omits the table", () => {
24+
document.body.innerHTML = `
25+
<table>
26+
<thead><tr><th>Question</th><th>Owner</th></tr></thead>
27+
<tbody><tr><td>Loader URL?</td><td>Platform</td></tr></tbody>
28+
</table>`;
29+
const cells = document.querySelectorAll("th, td");
30+
const selection = selectBetween(cells[0]!, cells[3]!);
31+
32+
expect(chatMarkdownClipboardPayload(selection, "tsv")?.text).toBe(
33+
"Question\tOwner\nLoader URL?\tPlatform",
34+
);
35+
expect(chatMarkdownClipboardPayload(selection)?.text).toBe(
36+
"| Question | Owner |\n| --- | --- |\n| Loader URL? | Platform |",
37+
);
38+
});
39+
40+
it("keeps a selection inside one cell as plain text", () => {
41+
document.body.innerHTML = `<table><tbody><tr><td>Platform</td></tr></tbody></table>`;
42+
const cell = document.querySelector("td")!;
43+
44+
expect(chatMarkdownClipboardPayload(selectBetween(cell, cell), "tsv")?.text).toBe("Platform");
45+
});
46+
});

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

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,26 @@ function sanitizedHtmlFrom(container: Element): string {
452452
return `<meta charset="utf-8">${container.innerHTML}`;
453453
}
454454

455+
const TABLE_INTERIOR_TAGS = new Set(["TABLE", "THEAD", "TBODY", "TFOOT", "TR"]);
456+
457+
/**
458+
* A range spanning several cells or rows clones without its `table`, leaving
459+
* nothing to serialize as a table. Rebuilds the shell from the table down to
460+
* the range's common ancestor around the cloned fragment.
461+
*/
462+
function withTableShell(ancestor: Element | null, fragment: DocumentFragment): Node {
463+
if (!ancestor || !TABLE_INTERIOR_TAGS.has(ancestor.tagName)) return fragment;
464+
const table = ancestor.closest("table");
465+
let shell: Node = fragment;
466+
for (let element: Element | null = ancestor; element; element = element.parentElement) {
467+
const clone = element.cloneNode(false);
468+
clone.appendChild(shell);
469+
shell = clone;
470+
if (element === table) break;
471+
}
472+
return shell;
473+
}
474+
455475
export function chatMarkdownClipboardPayload(
456476
selection: Selection,
457477
tableFormat: TableCopyFormat = "markdown",
@@ -461,11 +481,11 @@ export function chatMarkdownClipboardPayload(
461481
for (let index = 0; index < selection.rangeCount; index += 1) {
462482
const range = selection.getRangeAt(index);
463483
if (range.collapsed) continue;
464-
const container = document.createElement("div");
465-
container.appendChild(range.cloneContents());
466484
const ancestor = range.commonAncestorContainer;
467485
const ancestorElement =
468486
ancestor.nodeType === Node.ELEMENT_NODE ? (ancestor as Element) : ancestor.parentElement;
487+
const container = document.createElement("div");
488+
container.appendChild(withTableShell(ancestorElement, range.cloneContents()));
469489
if (ancestorElement?.closest("pre")) {
470490
const text = range.toString();
471491
if (text) {

0 commit comments

Comments
 (0)