diff --git a/src/layout-manager.js b/src/layout-manager.js index 2dc4960..3937452 100644 --- a/src/layout-manager.js +++ b/src/layout-manager.js @@ -194,6 +194,7 @@ function makeComputeWidths(colSpan, desiredWidth, x, forcedMin) { return function (vals, table) { let result = []; let spanners = []; + let auto = {}; table.forEach(function (row) { row.forEach(function (cell) { if ((cell[colSpan] || 1) > 1) { @@ -217,12 +218,20 @@ function makeComputeWidths(colSpan, desiredWidth, x, forcedMin) { let col = cell[x]; let existingWidth = result[col]; let editableCols = typeof vals[col] === 'number' ? 0 : 1; - for (let i = 1; i < span; i++) { - existingWidth += 1 + result[col + i]; - if (typeof vals[col + i] !== 'number') { - editableCols++; + if (typeof existingWidth === 'number') { + for (let i = 1; i < span; i++) { + existingWidth += 1 + result[col + i]; + if (typeof vals[col + i] !== 'number') { + editableCols++; + } + } + } else { + existingWidth = desiredWidth === 'desiredWidth' ? cell.desiredWidth - 1 : 1; + if (!auto[col] || auto[col] < existingWidth) { + auto[col] = existingWidth; } } + if (cell[desiredWidth] > existingWidth) { let i = 0; while (editableCols > 0 && cell[desiredWidth] > existingWidth) { @@ -237,7 +246,7 @@ function makeComputeWidths(colSpan, desiredWidth, x, forcedMin) { } } - Object.assign(vals, result); + Object.assign(vals, result, auto); for (let j = 0; j < vals.length; j++) { vals[j] = Math.max(forcedMin, vals[j] || 0); } diff --git a/test/issues/289-test.js b/test/issues/289-test.js new file mode 100644 index 0000000..2734684 --- /dev/null +++ b/test/issues/289-test.js @@ -0,0 +1,35 @@ +const Table = require('../..'); + +describe('erroneous colSpan does not get truncated', () => { + test('before row with column width', () => { + const table = new Table({ style: { border: [], head: [] } }); + table.push([{ colSpan: 2, content: 'I should not be truncated' }]); + let expected = [ + '┌────────────────────────────┐', + '│ I should not be truncated │', + '└────────────────────────────┘', + ]; + expect(table.toString()).toEqual(expected.join('\n')); + }); + test('after row with column width', () => { + const table = new Table({ style: { head: [], border: [] } }); + table.push( + [{ content: '0-0 (1x3)', colSpan: 3, rowSpan: 1 }], + [ + { content: '1-0 (2x2)', colSpan: 2, rowSpan: 2 }, + { content: '1-2 (2x1)', colSpan: 1, rowSpan: 2 }, + ], + [] + ); + let expected = [ + '┌────────────────────────┐', + '│ 0-0 (1x3) │', + '├────────────┬───────────┤', + '│ 1-0 (2x2) │ 1-2 (2x1) │', + '│ │ │', + '│ │ │', + '└────────────┴───────────┘', + ]; + expect(table.toString()).toEqual(expected.join('\n')); + }); +});