Skip to content

Commit

Permalink
Fix to erroneous column truncation when using colSpan (#289) (#290)
Browse files Browse the repository at this point in the history
  • Loading branch information
speedytwenty committed Apr 5, 2022
1 parent 8b2fbab commit 40e86bc
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 5 deletions.
19 changes: 14 additions & 5 deletions src/layout-manager.js
Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -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);
}
Expand Down
35 changes: 35 additions & 0 deletions 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'));
});
});

0 comments on commit 40e86bc

Please sign in to comment.