fix(tables): fix autofit table column rendering and page break splitting #1987
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
Fixes autofit tables dropping columns when rows use rowspan/colspan patterns (e.g. the PCI compliance table) and improves page break splitting to match Word's behavior.
Root cause: The PCI table has a 4-column grid but some rows have only 2-3 physical cells due to rowspan/colspan. Two bugs caused the rightmost column to disappear:
cells.lengthinstead of the sum ofcolSpanvalues — rows with rowspan continuations have fewer physical cells, making the engine think the table had 3 columnscolwidthvalues (scaled up fromtcWduring import) were preferred over grid values — grid values sum exactly to page width, but colwidth values get distorted by scaling mathChanges
Column rendering fixes (core bug fix)
Grid priority over colwidth (
pm-adapter/converters/table.ts): Swap priority sow:tblGridvalues (which sum to exact page width) are used before falling back to PMcolwidthattributes. Cell colwidth values come fromtcW(cell width hints) and get scaled during import, introducing proportion errors that make columns narrower than they should be.Column count from colSpan sums (
measuring/dom/index.ts):maxCellCountnow sumscolSpanvalues per row instead of counting physical cells. Rows with rowspan continuations have fewer physical cells than grid columns — usingcells.lengthdropped the 4th column.Colspan tcW distribution (
legacy-handle-table-cell-node.js): When a colspan cell'stcWexceeds the grid span total, distribute proportionally across grid columns instead of using raw grid values. This prevents colspan cells from getting undersized colwidth arrays during DOCX import.Page break improvements
Rowspan-aware split point selection (
layout-table.ts): TrackmaxRowspanEnd(farthest row reached by any active rowspan) andlastCleanFitRow(last boundary with no rowspan crossing). When the standard break would split a rowspan group, prefer the clean break + force a page advance. This matches Word's behavior of keeping rowspan groups together on the same page.Ghost cell rendering (
renderTableFragment.ts): When a table continues from a previous fragment, render empty bordered cells ("ghost cells") for grid columns occupied by rowspan cells that started on the previous page. This maintains correct table structure and borders on continuation pages, matching Word's rendering.Header-skip for cantSplit rows (
layout-table.ts): When repeated headers would prevent acantSplitrow from fitting (but the row fits without headers), skip header repetition. Word does not split cantSplit rows just because repeated headers eat up space.fullPageHeight fix (
layout-table.ts): UsecontentBottom - topMarginfor over-tall row detection instead of justcontentBottom, which includes the top margin and overstates available space.Cell height refinement
measuring/dom/index.ts,renderTableCell.ts): In Word, the last paragraph'sspacing.afteris absorbed by the cell's bottom padding and doesn't add extra height. Skipping it saves ~4px per row, accumulating to shift page breaks closer to Word's behavior.Test plan
pnpm --filter @superdoc/pm-adapter test— column width priority tests updatedpnpm --filter @superdoc/measuring-dom test— spacing.after + colSpan column count testspnpm --filter @superdoc/painter-dom test— spacing.after rendering tests updatedtable-autofit-colspan.test.jsvalidates 4-column grid preserved when rows have <4 physical cellsCloses SD-1797