Skip to content

Commit ed4904c

Browse files
committed
fix(web): preserve empty rows when copying tables as html
1 parent 75d4b52 commit ed4904c

2 files changed

Lines changed: 7 additions & 3 deletions

File tree

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,7 @@ describe("copying a table as HTML", () => {
412412
cell("TD", "1", { rowspan: "0" }),
413413
cell("TD", "2", { rowspan: "-1" }),
414414
),
415+
row(),
415416
),
416417
new FakeElement("TFOOT").append(
417418
row(cell("TD", "Total"), cell("TD", "3", { colspan: "2garbage" })),
@@ -428,6 +429,7 @@ describe("copying a table as HTML", () => {
428429
" <tbody>",
429430
// rowspan="0" runs to the end of its section; a negative span is invalid.
430431
' <tr><th align="left">EU</th><td rowspan="0">1</td><td>2</td></tr>',
432+
" <tr></tr>",
431433
" </tbody>",
432434
" <tfoot>",
433435
// HTML reads a span's leading digits, as the DOM does.

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -542,14 +542,16 @@ function htmlTableCell(cell: Element, rtl: boolean): string {
542542
}
543543

544544
function htmlTableSection(tag: string, rows: ReadonlyArray<Element>, rtl: boolean): string[] {
545-
const lines = rows.flatMap((row) => {
545+
let hasCells = false;
546+
const lines = rows.map((row) => {
546547
const cells = [...row.children].filter(
547548
(cell) => cell.tagName === "TH" || cell.tagName === "TD",
548549
);
549550
const html = cells.map((cell) => htmlTableCell(cell, rtl)).join("");
550-
return cells.length > 0 ? [` <tr>${html}</tr>`] : [];
551+
hasCells ||= cells.length > 0;
552+
return ` <tr>${html}</tr>`;
551553
});
552-
return lines.length > 0 ? [` <${tag}>`, ...lines, ` </${tag}>`] : [];
554+
return hasCells ? [` <${tag}>`, ...lines, ` </${tag}>`] : [];
553555
}
554556

555557
/**

0 commit comments

Comments
 (0)