Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions packages/layout-engine/painters/dom/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3029,7 +3029,7 @@ describe('DomPainter', () => {
expect(appliedWordSpacing).toBeCloseTo(expectedWordSpacing, 5);
});

it('reuses fragment DOM nodes when layout geometry changes', () => {
it('rebuilds fragment DOM nodes when layout geometry changes to keep line epochs in sync', () => {
const painter = createTestPainter({ blocks: [block], measures: [measure] });
painter.paint(layout, mount);

Expand All @@ -3051,9 +3051,12 @@ describe('DomPainter', () => {

painter.paint(movedLayout, mount);
const fragmentAfter = mount.querySelector('.superdoc-fragment') as HTMLElement;
const lineAfter = fragmentAfter.querySelector('.superdoc-line') as HTMLElement;

expect(fragmentAfter).toBe(fragmentBefore);
expect(fragmentAfter).not.toBe(fragmentBefore);
Copy link
Copy Markdown
Contributor

@chittolinag chittolinag Apr 17, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@artem-harbour I think it makes more sense to use toEqual here. I know that the old code was already using toBe, but toBe does a strict comparison using ===. This means that the objects could be semantically the same, but toBe would still result in false.

Jest's doc:

Image

expect(fragmentAfter.style.left).toBe('60px');
expect(fragmentAfter.dataset.layoutEpoch).toBeTruthy();
expect(lineAfter.dataset.layoutEpoch).toBe(fragmentAfter.dataset.layoutEpoch);
});

it('rebuilds fragment DOM when block content changes via setData', () => {
Expand Down Expand Up @@ -5016,10 +5019,13 @@ describe('DomPainter', () => {
painter.paint(updatedLayout, mount);

const updatedWrapper = mount.querySelector('.superdoc-fragment-list-item') as HTMLElement;
expect(updatedWrapper).toBe(initialWrapper);
const updatedLine = updatedWrapper.querySelector('.superdoc-line') as HTMLElement;
expect(updatedWrapper).not.toBe(initialWrapper);
expect(updatedWrapper.style.left).toBe('90px');
expect(updatedWrapper.style.top).toBe('55px');
expect(updatedWrapper.style.width).toBe('310px');
expect(updatedWrapper.dataset.layoutEpoch).toBeTruthy();
expect(updatedLine.dataset.layoutEpoch).toBe(updatedWrapper.dataset.layoutEpoch);
});

it('applies resolved zIndex only to anchored media fragments', () => {
Expand Down
12 changes: 12 additions & 0 deletions packages/layout-engine/painters/dom/src/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2714,6 +2714,7 @@ export class DomPainter {

if (current) {
existing.delete(key);
const geometryChanged = hasFragmentGeometryChanged(current.fragment, fragment);
const sdtBoundaryMismatch = shouldRebuildForSdtBoundary(current.element, sdtBoundary);
// Detect mismatch in any between-border property
const betweenBorderMismatch =
Expand All @@ -2730,6 +2731,7 @@ export class DomPainter {
current.element.dataset.pmStart != null &&
this.currentMapping.map(Number(current.element.dataset.pmStart)) !== newPmStart;
const needsRebuild =
geometryChanged ||
this.changedBlocks.has(fragment.blockId) ||
current.signature !== fragmentSignature(fragment, this.blockLookup) ||
sdtBoundaryMismatch ||
Expand Down Expand Up @@ -7288,6 +7290,16 @@ const fragmentSignature = (fragment: Fragment, lookup: BlockLookup): string => {
return base;
};

const hasFragmentGeometryChanged = (previous: Fragment, next: Fragment): boolean =>
previous.x !== next.x ||
previous.y !== next.y ||
previous.width !== next.width ||
('height' in previous &&
'height' in next &&
typeof previous.height === 'number' &&
typeof next.height === 'number' &&
previous.height !== next.height);

const getSdtMetadataId = (metadata: SdtMetadata | null | undefined): string => {
if (!metadata) return '';
if ('id' in metadata && metadata.id != null) {
Expand Down
Loading