Skip to content

Commit ac911aa

Browse files
committed
fix(web): preserve spreadsheet table values and browser coverage
1 parent 87f3987 commit ac911aa

3 files changed

Lines changed: 66 additions & 42 deletions

File tree

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import { describe, expect, it } from "vite-plus/test";
2+
3+
import { serializeTableElementToDelimited } from "./markdown-clipboard";
4+
5+
function renderTable(html: string): HTMLTableElement {
6+
const container = document.createElement("div");
7+
container.innerHTML = html;
8+
return container.querySelector("table")!;
9+
}
10+
11+
function exampleTable() {
12+
return renderTable(`
13+
<table>
14+
<thead><tr><th>Question</th><th>Owner, team</th></tr></thead>
15+
<tbody>
16+
<tr><td></td><td>Platform</td></tr>
17+
<tr><td>first<br>second\tthird</td><td></td></tr>
18+
<tr><td><img alt="Build badge" src="b.svg"></td><td>say "ok"</td></tr>
19+
</tbody>
20+
</table>`);
21+
}
22+
23+
describe("serializeTableElementToDelimited", () => {
24+
it("keeps every cell in its column as tab-separated rows", () => {
25+
expect(serializeTableElementToDelimited(exampleTable(), "tsv")).toBe(
26+
'Question\tOwner, team\n\tPlatform\nfirst second third\t\nBuild badge\tsay "ok"',
27+
);
28+
});
29+
30+
it.each(["tsv", "csv"] as const)("copies formula-leading text literally in %s", (format) => {
31+
const table = renderTable(
32+
"<table><tr><td>=1+1</td><td>+SUM(A1)</td><td>-cmd</td><td>&#64;cmd</td><td>-42</td><td>+1.5e2</td></tr></table>",
33+
);
34+
const separator = format === "tsv" ? "\t" : ",";
35+
expect(serializeTableElementToDelimited(table, format)).toBe(
36+
["'=1+1", "'+SUM(A1)", "'-cmd", "'@cmd", "-42", "+1.5e2"].join(separator),
37+
);
38+
});
39+
40+
it("escapes quote-leading TSV cells", () => {
41+
const table = renderTable('<table><tr><td>"hello"</td><td>"open</td></tr></table>');
42+
expect(serializeTableElementToDelimited(table, "tsv")).toBe('"""hello"""\t"""open"');
43+
});
44+
45+
it("keeps math as TeX", () => {
46+
const table = renderTable(
47+
'<table><tr><td><span data-markdown-math="inline" data-markdown-copy="x^2">rendered math</span></td></tr></table>',
48+
);
49+
expect(serializeTableElementToDelimited(table, "tsv")).toBe("x^2");
50+
});
51+
52+
it("quotes CSV cells that contain commas or quotes", () => {
53+
expect(serializeTableElementToDelimited(exampleTable(), "csv")).toBe(
54+
'Question,"Owner, team"\n,Platform\nfirst second third,\nBuild badge,"say ""ok"""',
55+
);
56+
});
57+
});

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

Lines changed: 0 additions & 35 deletions
This file was deleted.

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

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -356,8 +356,10 @@ function tableCellText(node: Node): string {
356356
}
357357

358358
function delimitedCell(value: string, format: "tsv" | "csv"): string {
359-
const normalized = value.replace(/\s+/g, " ").trim();
360-
if (format === "tsv") return normalized;
359+
const text = value.replace(/\s+/g, " ").trim();
360+
const isNumeric = /^[+-]?(?:\d+(?:\.\d*)?|\.\d+)(?:[eE][+-]?\d+)?$/.test(text);
361+
const normalized = /^[=+\-@]/.test(text) && !isNumeric ? `'${text}` : text;
362+
if (format === "tsv" && !normalized.startsWith('"')) return normalized;
361363
return /[",\n]/.test(normalized) ? `"${normalized.replaceAll('"', '""')}"` : normalized;
362364
}
363365

@@ -369,15 +371,15 @@ export function serializeTableElementToDelimited(table: Element, format: "tsv" |
369371
(cell) => cell.tagName === "TH" || cell.tagName === "TD",
370372
);
371373
if (cells.length === 0) continue;
372-
lines.push(cells.map((cell) => delimitedCell(tableCellText(cell), format)).join(format === "tsv" ? "\t" : ","));
374+
lines.push(
375+
cells
376+
.map((cell) => delimitedCell(tableCellText(cell), format))
377+
.join(format === "tsv" ? "\t" : ","),
378+
);
373379
}
374380
return lines.join("\n");
375381
}
376382

377-
export function serializeTableElementToCsv(table: Element): string {
378-
return serializeTableElementToDelimited(table, "csv");
379-
}
380-
381383
function mermaidSourceHtml(source: string) {
382384
const pre = document.createElement("pre");
383385
const code = document.createElement("code");

0 commit comments

Comments
 (0)