Skip to content

Commit 86e6c6a

Browse files
committed
fix: improve collapsing of vertical table borders
Revert earlier change which disabled rendering of top/left borders, because this would cause the height of a thick left border to be too short for a cell with added top/bottom borders (e.g. see "Thick border all around" in SD-1131-table-borders.docx). Instead, render all four borders, but allow them to overlap, to simulate collapsed borders.
1 parent a3afd75 commit 86e6c6a

2 files changed

Lines changed: 14 additions & 14 deletions

File tree

packages/layout-engine/painters/dom/src/table/renderTableCell.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -307,23 +307,24 @@ export const renderTableCell = (deps: TableCellRenderDependencies): TableCellRen
307307
const paddingTop =
308308
(cell?.attrs?.padding?.top ?? defaultTableCell.attrs.padding.top) + rowBorderTop - getBorderWidth(borders?.top);
309309
const paddingBottom = cell?.attrs?.padding?.bottom ?? defaultTableCell.attrs.padding.bottom;
310-
const paddingLeft = cell?.attrs?.padding?.left ?? defaultTableCell.attrs.padding.left;
310+
const paddingLeft =
311+
(cell?.attrs?.padding?.left ?? defaultTableCell.attrs.padding.left) - getBorderWidth(borders?.left) / 2;
311312
const paddingRight = cell?.attrs?.padding?.right ?? defaultTableCell.attrs.padding.right;
312313

313314
const cellEl = doc.createElement('div');
314315
cellEl.style.position = 'absolute';
315-
cellEl.style.left = `${x}px`;
316+
cellEl.style.left = `${x - getBorderWidth(borders?.left) / 2}px`;
316317
cellEl.style.top = `${y}px`;
317-
cellEl.style.width = `${cellMeasure.width}px`;
318+
cellEl.style.width = `${cellMeasure.width + getBorderWidth(borders?.left) / 2 + getBorderWidth(borders?.right) / 2}px`;
318319
cellEl.style.height = `${rowHeight + getBorderWidth(borders?.bottom)}px`;
319320
cellEl.style.boxSizing = 'border-box';
320321
// Cell clips all overflow - no scrollbars, content just gets clipped at boundaries
321322
cellEl.style.overflow = 'hidden';
322323
// Apply padding directly to cell so content is positioned correctly
323-
cellEl.style.paddingLeft = `${paddingLeft}px`;
324-
cellEl.style.paddingTop = `${paddingTop}px`;
325-
cellEl.style.paddingRight = `${paddingRight}px`;
326-
cellEl.style.paddingBottom = `${paddingBottom}px`;
324+
cellEl.style.paddingLeft = `${Math.max(0, paddingLeft)}px`;
325+
cellEl.style.paddingTop = `${Math.max(0, paddingTop)}px`;
326+
cellEl.style.paddingRight = `${Math.max(0, paddingRight)}px`;
327+
cellEl.style.paddingBottom = `${Math.max(paddingBottom, 0)}px`;
327328

328329
if (borders) {
329330
applyCellBorders(cellEl, borders);

packages/layout-engine/painters/dom/src/table/renderTableRow.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type {
2+
CellBorders,
23
DrawingBlock,
34
Line,
45
ParagraphBlock,
@@ -239,7 +240,7 @@ export const renderTableRow = (deps: TableRowRenderDependencies): void => {
239240
// - If continuesFromPrev=true: draw TOP border (table's top border) to close the top
240241
// - If continuesOnNext=true: draw BOTTOM border (table's bottom border) to close the bottom
241242
// This means both fragments at a split have their edge borders drawn.
242-
let resolvedBorders;
243+
let resolvedBorders: CellBorders | undefined;
243244
if (hasBordersAttribute && !hasExplicitBorders) {
244245
// Cell explicitly has borders={} meaning "no borders"
245246
// TODO: Is this actually accurate? Shouldn't borders={} mean "don't override any borders"?
@@ -258,15 +259,13 @@ export const renderTableRow = (deps: TableRowRenderDependencies): void => {
258259

259260
resolvedBorders = {
260261
// For top: use cell's if defined, otherwise use table's top border for first row OR continuation
261-
top: treatAsFirstRow ? (cellBordersAttr?.top ?? borderValueToSpec(tableBorders?.top)) : undefined,
262+
top: borderValueToSpec(cellBordersAttr?.top ?? (treatAsFirstRow ? tableBorders?.top : undefined)),
262263
// For bottom: use cell's if defined, otherwise use table's bottom border for last row OR before continuation
263-
bottom: cellBordersAttr?.bottom ?? borderValueToSpec(treatAsLastRow ? tableBorders?.bottom : undefined),
264+
bottom: borderValueToSpec(cellBordersAttr?.bottom ?? (treatAsLastRow ? tableBorders?.bottom : undefined)),
264265
// For left: use cell's if defined, otherwise use table's left for first col
265-
left: isFirstCol
266-
? (cellBordersAttr?.left ?? borderValueToSpec(isFirstCol ? tableBorders?.left : tableBorders?.insideV))
267-
: undefined,
266+
left: borderValueToSpec(cellBordersAttr?.left ?? (isFirstCol ? tableBorders?.left : undefined)),
268267
// For right: use cell's if defined, otherwise use table's right for last col only
269-
right: cellBordersAttr?.right ?? borderValueToSpec(isLastCol ? tableBorders?.right : undefined),
268+
right: borderValueToSpec(cellBordersAttr?.right ?? (isLastCol ? tableBorders?.right : undefined)),
270269
};
271270
}
272271

0 commit comments

Comments
 (0)