-
Notifications
You must be signed in to change notification settings - Fork 186
Expand file tree
/
Copy pathrenderTableFragment.ts
More file actions
638 lines (585 loc) · 25 KB
/
Copy pathrenderTableFragment.ts
File metadata and controls
638 lines (585 loc) · 25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
import type {
DrawingBlock,
Fragment,
Line,
ParagraphBlock,
SdtMetadata,
TableBlock,
TableFragment,
TableMeasure,
} from '@superdoc/contracts';
import { getTableVisualDirection } from '@superdoc/contracts';
import type { ResolvePhysicalFamily } from '@superdoc/font-system';
import { CLASS_NAMES, fragmentStyles } from '../styles.js';
import { DOM_CLASS_NAMES } from '../constants.js';
import type { FragmentRenderContext } from '../renderer.js';
import { renderTableRow } from './renderTableRow.js';
import {
applySdtContainerChrome,
getSdtContainerKey,
getSdtContainerMetadata,
hasExplicitSdtContainerKey,
type SdtAncestorOptions,
type SdtBoundaryOptions,
} from '../sdt/container.js';
import { applyBorder, borderValueToSpec, hasExplicitCellBorders } from './border-utils.js';
import { getTableCellGridBounds } from './grid-geometry.js';
type ApplyStylesFn = (el: HTMLElement, styles: Partial<CSSStyleDeclaration>) => void;
/**
* Dependencies required for rendering a table fragment.
*
* Encapsulates all external dependencies needed to render a table, including
* document access, rendering context, pre-resolved table data, and helper functions.
*/
export type TableRenderDependencies = {
/** Document object for creating DOM elements */
doc: Document;
/** Table fragment to render (contains dimensions and row range) */
fragment: TableFragment;
/** Rendering context (section info, etc.) */
context: FragmentRenderContext;
/** Pre-extracted TableBlock (replaces blockLookup.get()) */
block: TableBlock;
/** Pre-extracted TableMeasure (replaces blockLookup.get()) */
measure: TableMeasure;
/** Pre-computed cell spacing in pixels */
cellSpacingPx: number;
/** Pre-computed effective column widths (fragment.columnWidths ?? measure.columnWidths) */
effectiveColumnWidths: number[];
/** Optional SDT boundary overrides for container styling */
sdtBoundary?: SdtBoundaryOptions;
/** Ancestor SDT key used to suppress duplicate container chrome in nested tables */
ancestorContainerKey?: string | null;
/** Ancestor SDT metadata used to suppress duplicate id-less container chrome in nested tables */
ancestorContainerSdt?: SdtMetadata | null;
/** Ancestor SDT keys used to suppress duplicate container chrome in nested tables */
ancestorContainerKeys?: SdtAncestorOptions['ancestorContainerKeys'];
/** Ancestor SDT metadata chain used to suppress duplicate id-less container chrome in nested tables */
ancestorContainerSdts?: SdtAncestorOptions['ancestorContainerSdts'];
/** Receives notification when this table fragment or descendants render SDT container chrome */
onSdtContainerChrome?: () => void;
/** Built-in SDT chrome rendering mode. */
chrome?: 'default' | 'none';
/** Function to render a line of paragraph content */
renderLine: (
block: ParagraphBlock,
line: Line,
context: FragmentRenderContext,
lineIndex: number,
isLastLine: boolean,
) => HTMLElement;
/** Optional callback invoked after a table line's final styles/markers are applied. */
captureLineSnapshot?: (
lineEl: HTMLElement,
context: FragmentRenderContext,
options?: { inTableParagraph?: boolean; wrapperEl?: HTMLElement },
) => void;
/** Function to render drawing content (images, shapes, shape groups) */
renderDrawingContent?: (block: DrawingBlock) => HTMLElement;
/** Function to apply fragment positioning and dimensions */
applyFragmentFrame: (el: HTMLElement, fragment: Fragment) => void;
/** Function to apply SDT metadata as data attributes */
applySdtDataset: (el: HTMLElement | null, metadata?: SdtMetadata | null) => void;
/** Function to apply container SDT metadata as data attributes */
applyContainerSdtDataset?: (el: HTMLElement | null, metadata?: SdtMetadata | null) => void;
/** Function to apply CSS styles to an element */
applyStyles: ApplyStylesFn;
/**
* Per-document logical->physical font resolver for in-cell list markers and drop caps. Threaded
* from the renderer's per-document resolver so they paint the same physical family they were
* measured in. Undefined falls back to the global resolver.
*/
resolvePhysical?: ResolvePhysicalFamily;
};
/**
* Renders a table fragment as a DOM element.
*
* Creates a container div with absolutely-positioned rows and cells. Handles:
* - Table border overlays for outer borders
* - Border collapse settings
* - Cell spacing
* - Row-by-row rendering with proper positioning
* - Metadata embedding for interactive table resizing
*
* **Error Handling:**
* If the document is unavailable, returns an error placeholder instead of
* throwing. Table block/measure validation is performed by the caller before
* invoking this helper.
*
* **SDT Container Styling:**
* If the table block has SDT metadata (`block.attrs?.sdt`), applies appropriate
* container styling via `applySdtContainerChrome()`:
* - Document sections: Gray border with hover tooltip
* - Structured content blocks: Blue border with label
* Uses type-safe helper functions to avoid unsafe type assertions.
*
* **Metadata Embedding:**
* Embeds column boundary metadata in the `data-table-boundaries` attribute
* using a compact JSON format:
* ```json
* {
* "columns": [
* {"i": 0, "x": 0, "w": 100, "min": 25, "r": 1},
* {"i": 1, "x": 100, "w": 150, "min": 30, "r": 1}
* ],
* "rows": [
* {"i": 0, "y": 0, "h": 30, "min": 10, "r": 1},
* {"i": 1, "y": 34, "h": 25, "min": 10, "r": 1}
* ]
* }
* ```
* Where for columns: i=index, x=position, w=width, min=minWidth, r=resizable(0/1)
* Where for rows: i=index, y=position, h=height, min=minHeight, r=resizable(0/1)
*
* **Edge Cases:**
* - Missing metadata: Element created without data-table-boundaries attribute
* - Empty columnBoundaries: Creates empty columns array in JSON
* - Missing block ID: Element created without data-sd-block-id attribute
*
* @param deps - All dependencies required for rendering
* @returns HTMLElement containing the rendered table fragment, or error placeholder
*
* @example
* ```typescript
* const tableElement = renderTableFragment({
* doc: document,
* fragment: tableFragment,
* context: renderContext,
* block: tableBlock,
* measure: tableMeasure,
* cellSpacingPx: 0,
* effectiveColumnWidths: tableMeasure.columnWidths,
* renderLine,
* applyFragmentFrame,
* applySdtDataset,
* applyStyles
* });
* container.appendChild(tableElement);
* ```
*/
export const renderTableFragment = (deps: TableRenderDependencies): HTMLElement => {
const {
doc,
fragment,
block,
measure,
cellSpacingPx,
effectiveColumnWidths,
chrome,
context,
sdtBoundary,
ancestorContainerKey,
ancestorContainerSdt,
ancestorContainerKeys,
ancestorContainerSdts,
onSdtContainerChrome,
renderLine,
captureLineSnapshot,
renderDrawingContent,
applyFragmentFrame,
applySdtDataset,
applyContainerSdtDataset,
applyStyles,
resolvePhysical,
} = deps;
// Check document first before using it in error handlers
if (!doc) {
console.error('DomPainter: document is not available');
// Use global document as fallback for error placeholder when available
if (typeof document !== 'undefined') {
const placeholder = document.createElement('div');
placeholder.classList.add(CLASS_NAMES.fragment, 'superdoc-error-placeholder');
placeholder.textContent = '[Document not available]';
placeholder.style.border = '1px dashed red';
placeholder.style.padding = '8px';
return placeholder;
}
throw new Error('Document is required for table rendering');
}
const tableBorders = block.attrs?.borders;
const tableIndentValue = (block.attrs?.tableIndent as { width?: unknown } | null | undefined)?.width;
const tableIndent = typeof tableIndentValue === 'number' && Number.isFinite(tableIndentValue) ? tableIndentValue : 0;
// RTL table: w:bidiVisual (ECMA-376 §17.4.1) — cells displayed right-to-left,
// table-level properties (borders, margins, indent) are mirrored.
const isRtl = getTableVisualDirection(block.attrs) === 'rtl';
// Note: We don't use createTableBorderOverlay because we implement single-owner
// border model where cells handle all borders (including outer table borders)
// to prevent double borders when rendering with absolutely-positioned divs.
const container = doc.createElement('div');
container.classList.add(CLASS_NAMES.fragment);
applyStyles(container, fragmentStyles);
applyFragmentFrame(container, fragment);
if (fragment.pmStart != null) {
container.dataset.pmStart = String(fragment.pmStart);
}
if (fragment.pmEnd != null) {
container.dataset.pmEnd = String(fragment.pmEnd);
}
container.style.height = `${fragment.height}px`;
applySdtDataset(container, block.attrs?.sdt);
applyContainerSdtDataset?.(container, block.attrs?.containerSdt);
// Outer table border widths: reserve space so border is inside fragment size; content is offset
const tableBorderWidths = measure.tableBorderWidths;
if (tableBorderWidths) {
container.style.boxSizing = 'border-box';
}
const contentLeft = tableBorderWidths?.left ?? 0;
const contentTop = tableBorderWidths?.top ?? 0;
// Apply SDT container styling (document sections, structured content blocks)
if (
applySdtContainerChrome(
doc,
container,
block.attrs?.sdt,
block.attrs?.containerSdt,
sdtBoundary,
{
ancestorContainerKey,
ancestorContainerSdt,
ancestorContainerKeys,
ancestorContainerSdts,
},
chrome,
)
) {
onSdtContainerChrome?.();
}
const tableContainerSdt = getSdtContainerMetadata(block.attrs?.sdt, block.attrs?.containerSdt);
const tableContainerKey = getSdtContainerKey(block.attrs?.sdt, block.attrs?.containerSdt);
const nextAncestorContainerKeys = [
...(ancestorContainerKeys ?? []),
ancestorContainerKey,
hasExplicitSdtContainerKey(block.attrs?.sdt, block.attrs?.containerSdt) ? tableContainerKey : null,
].filter((key): key is string => Boolean(key));
const nextAncestorContainerSdts = [...(ancestorContainerSdts ?? []), ancestorContainerSdt, tableContainerSdt].filter(
(sdt): sdt is SdtMetadata => Boolean(sdt),
);
const nextAncestorContainerKey = nextAncestorContainerKeys[nextAncestorContainerKeys.length - 1] ?? null;
const nextAncestorContainerSdt = nextAncestorContainerSdts[nextAncestorContainerSdts.length - 1] ?? null;
// Add table-specific class for resize overlay targeting and click mapping
container.classList.add(DOM_CLASS_NAMES.TABLE_FRAGMENT);
// Cell spacing pre-computed by the resolver; no cross-stage import needed.
// Add metadata for interactive table resizing
if (fragment.metadata?.columnBoundaries) {
// Build row-aware boundary segments scoped to THIS fragment's rows.
// When a table splits across pages, each fragment only renders a subset of rows
// (repeated headers + body rows from fromRow to toRow). Segments must match
// exactly the rendered rows so resize handles don't overflow the fragment.
const columnCount = effectiveColumnWidths.length;
// boundarySegments[colIndex] = array of {fromRow, toRow, y, height} segments where this boundary exists
const boundarySegments: Array<Array<{ fromRow: number; toRow: number; y: number; height: number }>> = [];
for (let i = 0; i < columnCount; i++) {
boundarySegments.push([]);
}
// Build the list of rows actually rendered in this fragment, matching the
// rendering order: repeated headers first, then body rows.
// NOTE: This header-then-body iteration must stay in sync with the rendering
// loop below (~line 315) which uses the same order to render row elements.
const renderedRows: Array<{ rowIndex: number; height: number }> = [];
// Repeated header rows (only on continuation fragments)
if (fragment.repeatHeaderCount && fragment.repeatHeaderCount > 0) {
for (let r = 0; r < fragment.repeatHeaderCount; r++) {
const rowMeasure = measure.rows[r];
if (!rowMeasure) break;
renderedRows.push({ rowIndex: r, height: rowMeasure.height });
}
}
// Body rows (fromRow to toRow), with partial row height for mid-row splits
for (let r = fragment.fromRow; r < fragment.toRow; r++) {
const rowMeasure = measure.rows[r];
if (!rowMeasure) break;
const isPartialRow = fragment.partialRow && fragment.partialRow.rowIndex === r;
const actualHeight = isPartialRow ? fragment.partialRow!.partialHeight : rowMeasure.height;
renderedRows.push({ rowIndex: r, height: actualHeight });
}
// For each rendered row, determine which grid columns have cell boundaries
// A boundary exists at column X if there's a cell that ENDS at column X (gridColumnStart + colSpan = X)
// rowY includes outer spacing (before first row, between rows, after last) so segment positions match rendered cells
let rowY = cellSpacingPx;
for (let i = 0; i < renderedRows.length; i++) {
const { rowIndex, height } = renderedRows[i];
const rowMeasure = measure.rows[rowIndex];
if (!rowMeasure) continue;
// Track which column boundaries exist in this row
const boundariesInRow = new Set<number>();
for (const cellMeasure of rowMeasure.cells) {
const startCol = cellMeasure.gridColumnStart ?? 0;
const colSpan = cellMeasure.colSpan ?? 1;
const endCol = startCol + colSpan;
// A cell creates boundaries at its start and end columns
// Start boundary (left edge of cell)
if (startCol > 0) {
boundariesInRow.add(startCol);
}
// End boundary (right edge of cell)
if (endCol < columnCount) {
boundariesInRow.add(endCol);
}
}
// For each boundary that exists in this row, extend or create a segment
for (const boundaryCol of boundariesInRow) {
const segments = boundarySegments[boundaryCol];
const lastSegment = segments[segments.length - 1];
// If the last segment ends at the previous rendered row, extend it
if (lastSegment && i > 0 && lastSegment.toRow === i) {
lastSegment.toRow = i + 1;
lastSegment.height += height;
} else {
// Start a new segment
segments.push({
fromRow: i,
toRow: i + 1,
y: rowY,
height,
});
}
}
rowY += height + cellSpacingPx;
}
const metadata: Record<string, unknown> = {
columns: fragment.metadata.columnBoundaries.map((boundary) => ({
i: boundary.index,
x: boundary.x + contentLeft,
w: boundary.width,
min: boundary.minWidth,
r: boundary.resizable ? 1 : 0,
})),
rtl: isRtl,
// Add segments for each column boundary (segments where resize handle should appear)
segments: boundarySegments.map((segs, colIndex) =>
segs.map((seg) => ({
c: colIndex, // column index
y: seg.y + contentTop, // y position (relative to table container)
h: seg.height, // height of segment
})),
),
};
// Add row boundary metadata for interactive row resizing
// Where: i=index, y=position, h=height, min=minHeight, r=resizable(0/1)
if (fragment.metadata.rowBoundaries && fragment.metadata.rowBoundaries.length > 0) {
metadata.rows = fragment.metadata.rowBoundaries.map((rb) => ({
i: rb.index,
y: rb.y + contentTop,
h: rb.height,
min: rb.minHeight,
r: rb.resizable ? 1 : 0,
}));
}
container.setAttribute('data-table-boundaries', JSON.stringify(metadata));
}
// Add block ID for PM transaction targeting
if (block.id) {
container.setAttribute('data-sd-block-id', block.id);
}
const borderCollapse = block.attrs?.borderCollapse ?? (block.attrs?.cellSpacing != null ? 'separate' : 'collapse');
if (borderCollapse === 'separate' && tableBorders) {
applyBorder(container, 'Top', borderValueToSpec(tableBorders.top));
applyBorder(container, 'Right', borderValueToSpec(isRtl ? tableBorders.left : tableBorders.right));
applyBorder(container, 'Bottom', borderValueToSpec(tableBorders.bottom));
applyBorder(container, 'Left', borderValueToSpec(isRtl ? tableBorders.right : tableBorders.left));
}
// Pre-calculate all row heights for rowspan calculations
// IMPORTANT: If this fragment has a partial row, we need to use the partial height
// for that row, not the full measured height. This ensures rowspan cells that
// extend into the partial row are sized correctly for this fragment.
const allRowHeights: number[] = measure.rows.map((r, idx: number) => {
if (fragment.partialRow && fragment.partialRow.rowIndex === idx) {
// Use partial height for the split row
return fragment.partialRow.partialHeight;
}
return r?.height ?? 0;
});
// First row starts after space before table content (space between table border and first row)
let y = cellSpacingPx;
// If this is a continuation fragment with repeated headers, render headers first.
// NOTE: This header-then-body iteration must stay in sync with the metadata
// segment builder above (~line 199) which uses the same order.
if (fragment.repeatHeaderCount && fragment.repeatHeaderCount > 0) {
for (let r = 0; r < fragment.repeatHeaderCount; r += 1) {
const rowMeasure = measure.rows[r];
if (!rowMeasure) break;
renderTableRow({
doc,
container,
rowIndex: r,
y,
rowMeasure,
row: block.rows[r],
totalRows: block.rows.length,
tableBorders,
columnWidths: effectiveColumnWidths,
allRowHeights,
tableIndent,
isRtl,
context,
renderLine,
captureLineSnapshot,
renderDrawingContent,
applySdtDataset,
ancestorContainerKey: nextAncestorContainerKey,
ancestorContainerSdt: nextAncestorContainerSdt,
ancestorContainerKeys: nextAncestorContainerKeys,
ancestorContainerSdts: nextAncestorContainerSdts,
onSdtContainerChrome,
chrome,
// Headers are always rendered as-is (no border suppression)
continuesFromPrev: false,
continuesOnNext: false,
cellSpacingPx,
resolvePhysical,
});
// Add row height + spacing after every row (including last) for outer spacing after last row
y += rowMeasure.height + cellSpacingPx;
}
}
// Render rowspan continuation cells ("ghost cells")
// When a table continues from a previous fragment, some grid columns in the
// first body rows may be occupied by rowspan cells that started on a previous page.
// Create empty cells to maintain table structure and borders (matching Word behavior).
if (fragment.continuesFromPrev && fragment.fromRow > 0) {
const repeatCount = fragment.repeatHeaderCount ?? 0;
for (let r = repeatCount; r < fragment.fromRow; r++) {
const srcRowMeasure = measure.rows[r];
if (!srcRowMeasure) continue;
for (let ci = 0; ci < srcRowMeasure.cells.length; ci++) {
const srcCellMeasure = srcRowMeasure.cells[ci];
const rowSpan = srcCellMeasure.rowSpan ?? 1;
if (rowSpan <= 1) continue;
const spanEndRow = r + rowSpan;
if (spanEndRow <= fragment.fromRow) continue;
// This cell's rowspan extends into this fragment's body rows
const gridCol = srcCellMeasure.gridColumnStart ?? 0;
const colSpan = srcCellMeasure.colSpan ?? 1;
// Calculate x position (sum of columns before gridCol)
let ghostX = 0;
for (let i = 0; i < gridCol && i < effectiveColumnWidths.length; i++) {
ghostX += effectiveColumnWidths[i];
}
// Calculate width (sum of spanned columns)
let ghostWidth = 0;
for (let i = gridCol; i < gridCol + colSpan && i < effectiveColumnWidths.length; i++) {
ghostWidth += effectiveColumnWidths[i];
}
// RTL: mirror ghost cell x position.
// ghostX must include spacing to match the totalWidth formula.
if (isRtl && ghostWidth > 0) {
let totalWidth = cellSpacingPx;
for (let i = 0; i < effectiveColumnWidths.length; i++) {
totalWidth += effectiveColumnWidths[i] + cellSpacingPx;
}
// Recompute ghostX with spacing (matching calculateXPosition in renderTableRow)
let ghostXWithSpacing = cellSpacingPx;
for (let i = 0; i < gridCol && i < effectiveColumnWidths.length; i++) {
ghostXWithSpacing += effectiveColumnWidths[i] + cellSpacingPx;
}
ghostX = totalWidth - ghostXWithSpacing - ghostWidth;
}
// Calculate height: from fromRow to min(spanEndRow, toRow)
const effectiveEnd = Math.min(spanEndRow, fragment.toRow);
let ghostHeight = 0;
for (let ri = fragment.fromRow; ri < effectiveEnd; ri++) {
ghostHeight += allRowHeights[ri] ?? 0;
}
if (ghostWidth <= 0 || ghostHeight <= 0) continue;
// Create ghost cell
const ghostDiv = doc.createElement('div');
ghostDiv.style.position = 'absolute';
ghostDiv.style.left = `${ghostX}px`;
ghostDiv.style.top = `${y}px`;
ghostDiv.style.width = `${ghostWidth}px`;
ghostDiv.style.height = `${ghostHeight}px`;
ghostDiv.style.boxSizing = 'border-box';
ghostDiv.style.overflow = 'hidden';
// Resolve borders for the ghost cell
const srcCell = block.rows[r]?.cells?.[ci];
const cellBordersAttr = srcCell?.attrs?.borders;
const explicit = hasExplicitCellBorders(cellBordersAttr);
const cellBounds = getTableCellGridBounds({
rowIndex: r,
rowSpan,
gridColumnStart: gridCol,
colSpan,
totalRows: block.rows.length,
totalCols: effectiveColumnWidths.length,
});
const cellEndsWithinFragment = effectiveEnd <= fragment.toRow && spanEndRow <= fragment.toRow;
if (tableBorders) {
// Resolve borders using logical positions
const topB = (explicit ? cellBordersAttr.top : undefined) ?? borderValueToSpec(tableBorders.top);
let leftB =
(explicit ? cellBordersAttr.left : undefined) ??
borderValueToSpec(cellBounds.touchesLeftEdge ? tableBorders.left : tableBorders.insideV);
let rightB =
(explicit ? cellBordersAttr.right : undefined) ??
borderValueToSpec(cellBounds.touchesRightEdge ? tableBorders.right : tableBorders.insideV);
const bottomB = cellEndsWithinFragment
? ((explicit ? cellBordersAttr.bottom : undefined) ??
borderValueToSpec(cellBounds.touchesBottomEdge ? tableBorders.bottom : tableBorders.insideH))
: undefined;
// RTL: swap resolved left↔right so CSS matches visual edges
if (isRtl) {
const tmp = leftB;
leftB = rightB;
rightB = tmp;
}
applyBorder(ghostDiv, 'Top', topB);
applyBorder(ghostDiv, 'Left', leftB);
applyBorder(ghostDiv, 'Right', rightB);
if (bottomB) applyBorder(ghostDiv, 'Bottom', bottomB);
}
// Apply cell background if present
if (srcCell?.attrs?.background) {
ghostDiv.style.backgroundColor = srcCell.attrs.background;
}
container.appendChild(ghostDiv);
}
}
}
// Render body rows (fromRow to toRow)
for (let r = fragment.fromRow; r < fragment.toRow; r += 1) {
const rowMeasure = measure.rows[r];
if (!rowMeasure) break;
const isFirstRenderedBodyRow = r === fragment.fromRow;
const isLastRenderedBodyRow = r === fragment.toRow - 1;
// Check if this row has partial row data (mid-row split)
const isPartialRow = fragment.partialRow && fragment.partialRow.rowIndex === r;
const partialRowData = isPartialRow ? fragment.partialRow : undefined;
const actualRowHeight = partialRowData ? partialRowData.partialHeight : rowMeasure.height;
renderTableRow({
doc,
container,
rowIndex: r,
y,
rowMeasure,
row: block.rows[r],
totalRows: block.rows.length,
tableBorders,
columnWidths: effectiveColumnWidths,
allRowHeights,
tableIndent,
isRtl,
context,
renderLine,
captureLineSnapshot,
renderDrawingContent,
applySdtDataset,
ancestorContainerKey: nextAncestorContainerKey,
ancestorContainerSdt: nextAncestorContainerSdt,
ancestorContainerKeys: nextAncestorContainerKeys,
ancestorContainerSdts: nextAncestorContainerSdts,
onSdtContainerChrome,
chrome,
// Draw top border if table continues from previous fragment (MS Word behavior)
continuesFromPrev: isFirstRenderedBodyRow && fragment.continuesFromPrev === true,
// Draw bottom border if table continues on next fragment (MS Word behavior)
continuesOnNext: isLastRenderedBodyRow && fragment.continuesOnNext === true,
// Pass partial row data for mid-row splits
partialRow: partialRowData,
cellSpacingPx,
resolvePhysical,
});
// Add row height + spacing after every row (including last) for outer spacing after last row
y += actualRowHeight + cellSpacingPx;
}
return container;
};