From 97a4e21bf81503e8c6908629b50f7d1ee29fa18e Mon Sep 17 00:00:00 2001 From: Roland Date: Mon, 12 Oct 2026 11:21:43 +0800 Subject: [PATCH] fix: include single-column spans in automatic column widths --- src/calculateMaximumColumnWidths.ts | 13 +++--- test/calculateMaximumColumnWidths.ts | 47 +++++++++++++++++++++ test/spanningCells.ts | 63 ++++++++++++++++++++++++++++ 3 files changed, 116 insertions(+), 7 deletions(-) diff --git a/src/calculateMaximumColumnWidths.ts b/src/calculateMaximumColumnWidths.ts index 3c453ba..a51b293 100644 --- a/src/calculateMaximumColumnWidths.ts +++ b/src/calculateMaximumColumnWidths.ts @@ -22,16 +22,15 @@ export const calculateMaximumCellWidth = (cell: Cell): number => { export const calculateMaximumColumnWidths = (rows: Row[], spanningCellConfigs: SpanningCellConfig[] = []): number[] => { const columnWidths = new Array(rows[0].length).fill(0); const rangeCoordinates = spanningCellConfigs.map(calculateRangeCoordinate); - const isSpanningCell = (rowIndex: number, columnIndex: number): boolean => { - return rangeCoordinates.some((rangeCoordinate) => { - return isCellInRange({col: columnIndex, - row: rowIndex}, rangeCoordinate); - }); - }; rows.forEach((row, rowIndex) => { row.forEach((cell, cellIndex) => { - if (isSpanningCell(rowIndex, cellIndex)) { + const range = rangeCoordinates.find((rangeCoordinate) => { + return isCellInRange({col: cellIndex, + row: rowIndex}, rangeCoordinate); + }); + + if (range && (range.topLeft.col !== range.bottomRight.col || range.topLeft.row !== rowIndex)) { return; } columnWidths[cellIndex] = Math.max(columnWidths[cellIndex], calculateMaximumCellWidth(cell)); diff --git a/test/calculateMaximumColumnWidths.ts b/test/calculateMaximumColumnWidths.ts index fce263b..22149f4 100644 --- a/test/calculateMaximumColumnWidths.ts +++ b/test/calculateMaximumColumnWidths.ts @@ -4,6 +4,9 @@ import { import { calculateMaximumColumnWidths, } from '../src/calculateMaximumColumnWidths'; +import { + stringToRed, +} from './utils'; describe('calculateMaximumColumnWidths', () => { context('all cells have different width', () => { @@ -28,4 +31,48 @@ describe('calculateMaximumColumnWidths', () => { expect(cellWidths[0]).to.equal(4); }); }); + context('spanning cells', () => { + it('includes cells spanning a single column', () => { + const cellWidths = calculateMaximumColumnWidths([ + ['a', 'Heading'], + ['b', 'c'], + ], [{col: 1, + colSpan: 1, + row: 0}]); + + expect(cellWidths).to.deep.equal([1, 7]); + }); + it('includes the first cell of a row span and ignores covered cells', () => { + const cellWidths = calculateMaximumColumnWidths([ + ['Heading', 'y'], + ['This content is hidden', 'z'], + ], [{col: 0, + row: 0, + rowSpan: 2}]); + + expect(cellWidths).to.deep.equal([7, 1]); + }); + it('ignores all cells in a span covering multiple columns', () => { + const cellWidths = calculateMaximumColumnWidths([ + ['Heading spanning two columns', 'Hidden content'], + ['More hidden content', 'Also hidden'], + ['a', 'bb'], + ], [{col: 0, + colSpan: 2, + row: 0, + rowSpan: 2}]); + + expect(cellWidths).to.deep.equal([1, 2]); + }); + it('measures the display width of multiline single-column spans', () => { + const cellWidths = calculateMaximumColumnWidths([ + [stringToRed('a\n中文')], + ['x'], + ], [{col: 0, + colSpan: 1, + row: 0}]); + + expect(cellWidths).to.deep.equal([4]); + }); + }); }); diff --git a/test/spanningCells.ts b/test/spanningCells.ts index ba69d3f..1d84ee8 100644 --- a/test/spanningCells.ts +++ b/test/spanningCells.ts @@ -12,6 +12,69 @@ import { } from './utils'; describe('spanningCells', () => { + it('uses single-column spanning cell content to size the column', () => { + const result = table([ + ['Heading', 'Value'], + ['a', '1'], + ], { + spanningCells: [{alignment: 'center', + col: 0, + colSpan: 1, + row: 0}], + }); + + expectTable(result, ` +╔═════════╤═══════╗ +║ Heading │ Value ║ +╟─────────┼───────╢ +║ a │ 1 ║ +╚═════════╧═══════╝ +`); + }); + + it('sizes a column from a cell spanning multiple rows', () => { + const result = table([ + ['a', 'x'], + ['Group', 'y'], + ['This content is hidden', 'z'], + ], { + spanningCells: [{col: 0, + row: 1, + rowSpan: 2}], + }); + + expectTable(result, ` +╔═══════╤═══╗ +║ a │ x ║ +╟───────┼───╢ +║ Group │ y ║ +║ ├───╢ +║ │ z ║ +╚═══════╧═══╝ +`); + }); + + it('respects explicit column widths for single-column spans', () => { + const result = table([ + ['abcdef'], + ['x'], + ], { + columns: [{width: 3}], + spanningCells: [{col: 0, + colSpan: 1, + row: 0}], + }); + + expectTable(result, ` +╔═════╗ +║ abc ║ +║ def ║ +╟─────╢ +║ x ║ +╚═════╝ +`); + }); + const baseUserCellConfig: CellUserConfig = { alignment: 'left', verticalAlignment: 'top',