Skip to content

Commit 40e86bc

Browse files
authored
Fix to erroneous column truncation when using colSpan (#289) (#290)
1 parent 8b2fbab commit 40e86bc

File tree

2 files changed

+49
-5
lines changed

2 files changed

+49
-5
lines changed

src/layout-manager.js

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ function makeComputeWidths(colSpan, desiredWidth, x, forcedMin) {
194194
return function (vals, table) {
195195
let result = [];
196196
let spanners = [];
197+
let auto = {};
197198
table.forEach(function (row) {
198199
row.forEach(function (cell) {
199200
if ((cell[colSpan] || 1) > 1) {
@@ -217,12 +218,20 @@ function makeComputeWidths(colSpan, desiredWidth, x, forcedMin) {
217218
let col = cell[x];
218219
let existingWidth = result[col];
219220
let editableCols = typeof vals[col] === 'number' ? 0 : 1;
220-
for (let i = 1; i < span; i++) {
221-
existingWidth += 1 + result[col + i];
222-
if (typeof vals[col + i] !== 'number') {
223-
editableCols++;
221+
if (typeof existingWidth === 'number') {
222+
for (let i = 1; i < span; i++) {
223+
existingWidth += 1 + result[col + i];
224+
if (typeof vals[col + i] !== 'number') {
225+
editableCols++;
226+
}
227+
}
228+
} else {
229+
existingWidth = desiredWidth === 'desiredWidth' ? cell.desiredWidth - 1 : 1;
230+
if (!auto[col] || auto[col] < existingWidth) {
231+
auto[col] = existingWidth;
224232
}
225233
}
234+
226235
if (cell[desiredWidth] > existingWidth) {
227236
let i = 0;
228237
while (editableCols > 0 && cell[desiredWidth] > existingWidth) {
@@ -237,7 +246,7 @@ function makeComputeWidths(colSpan, desiredWidth, x, forcedMin) {
237246
}
238247
}
239248

240-
Object.assign(vals, result);
249+
Object.assign(vals, result, auto);
241250
for (let j = 0; j < vals.length; j++) {
242251
vals[j] = Math.max(forcedMin, vals[j] || 0);
243252
}

test/issues/289-test.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
const Table = require('../..');
2+
3+
describe('erroneous colSpan does not get truncated', () => {
4+
test('before row with column width', () => {
5+
const table = new Table({ style: { border: [], head: [] } });
6+
table.push([{ colSpan: 2, content: 'I should not be truncated' }]);
7+
let expected = [
8+
'┌────────────────────────────┐',
9+
'│ I should not be truncated │',
10+
'└────────────────────────────┘',
11+
];
12+
expect(table.toString()).toEqual(expected.join('\n'));
13+
});
14+
test('after row with column width', () => {
15+
const table = new Table({ style: { head: [], border: [] } });
16+
table.push(
17+
[{ content: '0-0 (1x3)', colSpan: 3, rowSpan: 1 }],
18+
[
19+
{ content: '1-0 (2x2)', colSpan: 2, rowSpan: 2 },
20+
{ content: '1-2 (2x1)', colSpan: 1, rowSpan: 2 },
21+
],
22+
[]
23+
);
24+
let expected = [
25+
'┌────────────────────────┐',
26+
'│ 0-0 (1x3) │',
27+
'├────────────┬───────────┤',
28+
'│ 1-0 (2x2) │ 1-2 (2x1) │',
29+
'│ │ │',
30+
'│ │ │',
31+
'└────────────┴───────────┘',
32+
];
33+
expect(table.toString()).toEqual(expected.join('\n'));
34+
});
35+
});

0 commit comments

Comments
 (0)