From 6993e2db3848bbc3e8db0b50de3b38192bfbf924 Mon Sep 17 00:00:00 2001 From: yousefed Date: Sat, 21 Nov 2026 19:59:59 +0100 Subject: [PATCH 1/3] fix(core): a merged table cell has no empty column width `tableContentToNodes` made the `colwidth` of a cell with a span as an array with one entry for each column of the span, with `undefined` for a column without a width: `[undefined, undefined]` or `[200, undefined]`. The cell node writes this array into the HTML as `colwidth=","` or `colwidth="200,"`, and the HTML parser reads an empty entry as `NaN`. Thus a copy and paste of a table with such a cell put NaN column widths into the document. A cell with a span now has: - `colwidth: null` when no column in the span has a width, as a cell without a span. - `0` for a column without a width when another column in the span has one, as prosemirror-tables writes it. The snapshot of the suggestion tests for a merge and a split of cells recorded the array `[undefined, undefined]`. It now has `null`, and no change of `colwidth`. --- .../blockToNode.browser.test.ts | 44 +++++++++++++++++++ .../nodeConversions/blockToNode.node.test.ts | 39 ++++++++++++++++ .../src/api/nodeConversions/blockToNode.ts | 11 +++-- .../__snapshots__/tables.test.tsx.snap | 9 ++-- 4 files changed, 95 insertions(+), 8 deletions(-) diff --git a/packages/core/src/api/nodeConversions/blockToNode.browser.test.ts b/packages/core/src/api/nodeConversions/blockToNode.browser.test.ts index b8896600d6..39babc8794 100644 --- a/packages/core/src/api/nodeConversions/blockToNode.browser.test.ts +++ b/packages/core/src/api/nodeConversions/blockToNode.browser.test.ts @@ -21,3 +21,47 @@ describe("blockToNode: plain-block backwards compatibility (write path)", () => ]); }); }); + +describe("blockToNode: tables", () => { + // The widths of a merged cell must not have an empty entry for a column + // without a width. The HTML export writes such an entry as `colwidth=","` + // or `colwidth="200,"`, and the HTML parser reads that as `NaN`. + describe.each([ + // No column in the span has a width: no widths, as a cell without a span. + { columnWidths: undefined, parsed: [undefined, undefined] }, + // One column has a width: `0` for the other, as prosemirror-tables has it. + { columnWidths: [200, undefined], parsed: [200, 0] }, + ])( + "a merged cell with the column widths $columnWidths", + ({ columnWidths, parsed }) => { + const table = { + type: "table", + content: { + type: "tableContent", + columnWidths, + rows: [ + { + cells: [ + { type: "tableCell", props: { colspan: 2 }, content: "Merged" }, + ], + }, + { cells: ["a", "b"] }, + ], + }, + } as any; + + it("has no NaN column widths after an HTML round trip", async ({ + setupEditor, + }) => { + const { editor } = await setupEditor({ mountType: false }); + + const html = editor.blocksToFullHTML([table]); + const block = editor + .tryParseHTMLToBlocks(html) + .find((block) => block.type === "table")!; + + expect(block.content.columnWidths).toEqual(parsed); + }); + }, + ); +}); diff --git a/packages/core/src/api/nodeConversions/blockToNode.node.test.ts b/packages/core/src/api/nodeConversions/blockToNode.node.test.ts index 9fbf0a9c23..aa0978afc8 100644 --- a/packages/core/src/api/nodeConversions/blockToNode.node.test.ts +++ b/packages/core/src/api/nodeConversions/blockToNode.node.test.ts @@ -125,4 +125,43 @@ describe("blockToNode: tables", () => { expect(table.child(1).child(0).attrs.colwidth).toEqual([200]); expect(table.child(1).child(1).attrs.colwidth).toEqual([300]); }); + + // The widths of a merged cell must not have an empty entry for a column + // without a width. The HTML export writes such an entry as `colwidth=","` + // or `colwidth="200,"`, and the HTML parser reads that as `NaN`. + describe.each([ + // No column in the span has a width: no widths, as a cell without a span. + { columnWidths: undefined, colwidth: null }, + // One column has a width: `0` for the other, as prosemirror-tables has it. + { columnWidths: [200, undefined], colwidth: [200, 0] }, + ])( + "a merged cell with the column widths $columnWidths", + ({ columnWidths, colwidth }) => { + const table = { + type: "table", + content: { + type: "tableContent", + columnWidths, + rows: [ + { + cells: [ + { type: "tableCell", props: { colspan: 2 }, content: "Merged" }, + ], + }, + { cells: ["a", "b"] }, + ], + }, + } as any; + + it("has no empty entry in its colwidth", async ({ setupEditor }) => { + const { editor } = await setupEditor(); + + const node = blockToNode(table, editor.pmSchema); + + expect(node.firstChild!.child(0).child(0).attrs.colwidth).toEqual( + colwidth, + ); + }); + }, + ); }); diff --git a/packages/core/src/api/nodeConversions/blockToNode.ts b/packages/core/src/api/nodeConversions/blockToNode.ts index de7789a2a3..9c4cab4c83 100644 --- a/packages/core/src/api/nodeConversions/blockToNode.ts +++ b/packages/core/src/api/nodeConversions/blockToNode.ts @@ -273,10 +273,15 @@ export function tableContentToNodes< if (colspan > 1) { // If the cell has a > 1 colspan, we need to get the column width for each cell in the span - colwidth = new Array(colspan).fill(false).map((_, i) => { - // Starting from the absolute column index, get the column width for each cell in the span - return columnWidths[absoluteColumnIndex + i] ?? undefined; + const spanWidths = new Array(colspan).fill(false).map((_, i) => { + // Starting from the absolute column index, get the column width for each cell in the span. + // A column without a width is `0`, as prosemirror-tables writes it. + return columnWidths[absoluteColumnIndex + i] ?? 0; }); + // If no column in the span has a width, the cell has no widths + // (`null`), as a cell without a span. An empty entry would go into + // the HTML as `colwidth=","`, which the HTML parser reads as `NaN`. + colwidth = spanWidths.some(Boolean) ? spanWidths : null; } } else { content = inlineContentToNodes( diff --git a/tests/src/end-to-end/y-prosemirror/__snapshots__/tables.test.tsx.snap b/tests/src/end-to-end/y-prosemirror/__snapshots__/tables.test.tsx.snap index 45168035e6..6bba0abf1a 100644 --- a/tests/src/end-to-end/y-prosemirror/__snapshots__/tables.test.tsx.snap +++ b/tests/src/end-to-end/y-prosemirror/__snapshots__/tables.test.tsx.snap @@ -669,7 +669,7 @@ exports[`suggestion mode: merge two cells 2`] = ` Date: Sat, 21 Nov 2026 20:29:28 +0100 Subject: [PATCH 2/3] test: the version diff of a cell merge has no colwidth change The version diff of a merge and of a split of table cells recorded a change of `colwidth` next to the change of `colspan`: the merged cell had the array `[undefined, undefined]` where the other cells have `null`. The cell now has `null`, so only `colspan` changes. --- .../y-prosemirror/__snapshots__/versioning.test.tsx.snap | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/src/end-to-end/y-prosemirror/__snapshots__/versioning.test.tsx.snap b/tests/src/end-to-end/y-prosemirror/__snapshots__/versioning.test.tsx.snap index 76a0ee414b..0f79771a09 100644 --- a/tests/src/end-to-end/y-prosemirror/__snapshots__/versioning.test.tsx.snap +++ b/tests/src/end-to-end/y-prosemirror/__snapshots__/versioning.test.tsx.snap @@ -785,7 +785,7 @@ exports[`versioning diff: Merge blocks vs edit block B 1`] = ` exports[`versioning diff: Merge cells 1`] = ` [ - "attrs colspan:A colwidth:A", + "attrs colspan:A", "insert "+B1" A", "delete A", "insert ", @@ -931,7 +931,7 @@ exports[`versioning diff: Split a block vs type at end 1`] = ` exports[`versioning diff: Split a merged cell 1`] = ` [ - "attrs colspan:A colwidth:A", + "attrs colspan:A", "delete "+B1" A", "insert A", "insert A", From 1bd7535cabb7d0a0c36d5663a57465c043dfcaf7 Mon Sep 17 00:00:00 2001 From: yousefed Date: Sun, 22 Nov 2026 16:26:32 +0100 Subject: [PATCH 3/3] fix(core): a merged table cell without widths has a col for each column The HTML export wrote one for a first-row cell that has no column widths, also when the cell spans two or more columns. Since the fix for the empty column width, such a cell has no widths (null), thus the had too few elements and the width of a later column went to the wrong column. The now gets one without a width for each column in the span of such a cell. --- .../blockToNode.browser.test.ts | 37 +++++++++++++++++++ packages/core/src/blocks/Table/block.ts | 5 ++- 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/packages/core/src/api/nodeConversions/blockToNode.browser.test.ts b/packages/core/src/api/nodeConversions/blockToNode.browser.test.ts index 39babc8794..62252b87cb 100644 --- a/packages/core/src/api/nodeConversions/blockToNode.browser.test.ts +++ b/packages/core/src/api/nodeConversions/blockToNode.browser.test.ts @@ -64,4 +64,41 @@ describe("blockToNode: tables", () => { }); }, ); + + // A merged cell without widths must have one `` for each column in its + // span. With only one ``, the width of the third column goes to the + // second column. + it("exports a for each column of a merged cell without widths", async ({ + setupEditor, + }) => { + const { editor } = await setupEditor({ mountType: false }); + + const html = editor.blocksToFullHTML([ + { + type: "table", + content: { + type: "tableContent", + columnWidths: [undefined, undefined, 200], + rows: [ + { + cells: [ + { type: "tableCell", props: { colspan: 2 }, content: "Merged" }, + "c", + ], + }, + { cells: ["a", "b", "c"] }, + ], + }, + } as any, + ]); + + const cols = new DOMParser() + .parseFromString(html, "text/html") + .querySelectorAll("col"); + expect(Array.from(cols, (col) => col.style.width)).toEqual([ + "", + "", + "200px", + ]); + }); }); diff --git a/packages/core/src/blocks/Table/block.ts b/packages/core/src/blocks/Table/block.ts index a1328df485..f24c466a69 100644 --- a/packages/core/src/blocks/Table/block.ts +++ b/packages/core/src/blocks/Table/block.ts @@ -237,7 +237,10 @@ const TiptapTableNode = Node.create({ colGroup.appendChild(col); } } else { - colGroup.appendChild(document.createElement("col")); + // A cell without widths has one `` for each column in its span. + for (let i = 0; i < tableCell.attrs["colspan"]; i++) { + colGroup.appendChild(document.createElement("col")); + } } }