Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix to erroneous column truncation when using colSpan #290

Merged
merged 2 commits into from
Apr 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 14 additions & 5 deletions src/layout-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,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 @@ -215,12 +216,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 @@ -235,7 +244,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
Original file line number Diff line number Diff line change
@@ -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'));
});
});