Skip to content

Commit 5223975

Browse files
committed
Avoid fatal error with invalid table data (with optional debug output)
1 parent 7409254 commit 5223975

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

src/layout-manager.js

+9-4
Original file line numberDiff line numberDiff line change
@@ -88,14 +88,19 @@ const { ColSpanCell, RowSpanCell } = Cell;
8888
}
8989

9090
function addRowSpanCells(table) {
91-
table.forEach(function (row, rowIndex) {
91+
table.forEach(function (row) {
9292
row.forEach(function (cell) {
9393
for (let i = 1; i < cell.rowSpan; i++) {
9494
let rowSpanCell = new RowSpanCell(cell);
95+
const line = cell.y + i;
9596
rowSpanCell.x = cell.x;
96-
rowSpanCell.y = cell.y + i;
97+
rowSpanCell.y = line;
9798
rowSpanCell.colSpan = cell.colSpan;
98-
insertCell(rowSpanCell, table[rowIndex + i]);
99+
if (!table[line]) {
100+
const { x, y } = rowSpanCell;
101+
warn(`${x}-${y}: Expected empty array for row ${line-1} (line ${line}).`);
102+
}
103+
insertCell(rowSpanCell, table[rowSpanCell.y] || []);
99104
}
100105
});
101106
});
@@ -146,7 +151,7 @@ const { ColSpanCell, RowSpanCell } = Cell;
146151
cell.x = opts.x;
147152
cell.y = opts.y;
148153
warn(`Missing cell at ${cell.y}-${cell.x}.`);
149-
insertCell(cell, table[y]);
154+
insertCell(cell, table[y] || []);
150155
}
151156
}
152157
}

test/issues/296-test.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ test('it should not loop infinitely with invalid table data', async () => {
1919
});
2020

2121
test('it should not error on invalid table data', () => {
22-
const table = new Table();
22+
const table = new Table({ debug: true });
2323
table.push(
2424
[
2525
{ content: 'A', colSpan: 2 },
@@ -28,4 +28,6 @@ test('it should not error on invalid table data', () => {
2828
[{ content: 'C', colSpan: 3 }]
2929
);
3030
expect(() => table.toString()).not.toThrow();
31+
// This expectation can be dropped if the expectation in the code changes
32+
expect(table.messages).toContain('2-2: Expected empty array for row 1 (line 2).');
3133
});

0 commit comments

Comments
 (0)