From a2f34806a792b38050dfb08a61cdee7235ad9d9f Mon Sep 17 00:00:00 2001 From: Nick the Sick Date: Wed, 10 Dec 2025 17:00:35 +0100 Subject: [PATCH] fix: an invalidly specified table should not crash the editor --- .../blockManipulation/tables/tables.test.ts | 140 ++++++++++++++++++ .../api/blockManipulation/tables/tables.ts | 2 +- 2 files changed, 141 insertions(+), 1 deletion(-) diff --git a/packages/core/src/api/blockManipulation/tables/tables.test.ts b/packages/core/src/api/blockManipulation/tables/tables.test.ts index 166076ef0f..b386ea211b 100644 --- a/packages/core/src/api/blockManipulation/tables/tables.test.ts +++ b/packages/core/src/api/blockManipulation/tables/tables.test.ts @@ -546,6 +546,140 @@ const tableWithColspansAndRowspans = { any >; +const invalidTableShape = { + type: "table", + id: "table-0", + props: { + textColor: "default", + }, + content: { + type: "tableContent", + columnWidths: [100, 100], + rows: [ + { + cells: [ + { + type: "tableCell", + content: [ + { + type: "text", + text: "Table Cell", + styles: {}, + props: { + backgroundColor: "default", + textColor: "default", + textAlignment: "left", + }, + }, + ], + props: { + backgroundColor: "default", + textColor: "default", + textAlignment: "left", + }, + }, + ], + }, + { + cells: [ + { + type: "tableCell", + content: [ + { + type: "text", + text: "Table Cell", + styles: {}, + props: { + backgroundColor: "default", + textColor: "default", + textAlignment: "left", + }, + }, + ], + props: { + backgroundColor: "default", + textColor: "default", + textAlignment: "left", + }, + }, + ], + }, + { + cells: [ + { + type: "tableCell", + content: [ + { + type: "text", + text: "Table Cell", + styles: {}, + props: { + backgroundColor: "default", + textColor: "default", + textAlignment: "left", + }, + }, + ], + props: { + backgroundColor: "default", + textColor: "default", + textAlignment: "left", + }, + }, + { + type: "tableCell", + content: [ + { + type: "text", + text: "Table Cell", + styles: {}, + props: { + backgroundColor: "default", + textColor: "default", + textAlignment: "left", + }, + }, + ], + props: { + colspan: 2, + backgroundColor: "default", + textColor: "default", + textAlignment: "left", + }, + }, + { + type: "tableCell", + content: [ + { + type: "text", + text: "Table x", + styles: {}, + props: { + backgroundColor: "default", + textColor: "default", + textAlignment: "left", + }, + }, + ], + props: { + backgroundColor: "default", + textColor: "default", + textAlignment: "left", + }, + }, + ], + }, + ], + }, + children: [], +} satisfies Block< + { + table: DefaultBlockSchema["table"]; + }, + any, + any +>; + /** * Normal table * | 1-1 | 1-2 | 1-3 | 1-4 | @@ -882,6 +1016,12 @@ describe("Test getAbsoluteTableCellIndices", () => { cell: tableWithComplexRowspansAndColspans.content.rows[2].cells[2], }); }); + + it("should not crash at an invalid table shape", () => { + expect(() => + getAbsoluteTableCells({ row: 2, col: 2 }, invalidTableShape), + ).not.toThrow(); + }); }); describe("Test getRelativeTableCellIndices", () => { diff --git a/packages/core/src/api/blockManipulation/tables/tables.ts b/packages/core/src/api/blockManipulation/tables/tables.ts index f770a34afd..6e19d6f45e 100644 --- a/packages/core/src/api/blockManipulation/tables/tables.ts +++ b/packages/core/src/api/blockManipulation/tables/tables.ts @@ -305,9 +305,9 @@ export function getAbsoluteTableCells( } { for (let r = 0; r < occupancyGrid.length; r++) { for (let c = 0; c < occupancyGrid[r].length; c++) { - // console.log(r, c, occupancyGrid); const cell = occupancyGrid[r][c]; if ( + cell && cell.row === relativeCellIndices.row && cell.col === relativeCellIndices.col ) {