Skip to content

Commit 03aa597

Browse files
committed
Fix R1 regression: forced breaks nested inside table cells corrupted layout
Found while investigating the plan's R7 stage (table resumption), before writing any new table code: CssLayoutEngineTable's row loop calls cell.PerformLayout directly and does not participate in the PendingBreakToken bubbling protocol an ordinary block-child loop does - a table row is not itself laid out via that loop, so nothing ever reads a cell's own PendingBreakToken and turns it into a real pass boundary. Before R1, a forced break inside a table cell just computed an adjusted top inline, in the same single continuous pass everything else used - harmless. After R1, a forced break anywhere (including inside a table cell) requests deferral to a later pass and returns from PerformLayoutImp without calling CreateLineBoxes - but MeasureWordsSize already ran unconditionally before that point, so the deferred content's words had real sizes but stale/default (0,0) positions. Confirmed by direct fragment-tree inspection: the content wasn't lost, it silently rendered overlapping whatever else was in the cell, with no new page ever created for it. Fix: CssBox.CanDeferToLaterPass() walks a box's own ancestor chain for a table-cell boundary; a forced break found there falls back to immediate same-pass placement (the pre-R1 behavior) instead of deferring, since deferring here could never actually be resumed. Not full parity (this content doesn't get its own fresh fragmentainer pass the way top-level content does), but correct rather than silently corrupted - matching this port's established pattern of local correction where true resumption isn't wired up yet. R3 (avoid/monolithic relocation) and R4 (keep-with-next) are unaffected - both relayout within the same pass rather than deferring across passes, so they never depended on the block-child-loop bubbling chain reaching past a table cell boundary in the first place. New regression test constructs the exact scenario and verifies both markers are present and correctly ordered by ABSOLUTE document-Y (reconstructed from each fragmentainer's own band top, since raw fragment-local Y values aren't comparable across different pages). Full regression suite (42 IntegrationTest + 16 PdfSharp tests) passes.
1 parent 380a207 commit 03aa597

2 files changed

Lines changed: 129 additions & 6 deletions

File tree

‎Source/HtmlRenderer/Core/Dom/CssBox.cs‎

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -594,6 +594,28 @@ internal void ResumeAt(BreakToken token, double? resumeTopOverride = null)
594594
_resumeTopOverride = resumeTopOverride;
595595
}
596596

597+
/// <summary>
598+
/// Whether a forced break here could actually be deferred to (and resumed in) a later pass -
599+
/// false anywhere inside a table cell's subtree. <see cref="CssLayoutEngineTable"/>'s row loop
600+
/// calls <c>cell.PerformLayout</c> directly, the same way it always has, and does not participate
601+
/// in the <see cref="PendingBreakToken"/> bubbling protocol an ordinary block-child loop does (see
602+
/// that property's doc comment) - a table row is not itself laid out via that loop, so nothing
603+
/// would ever read a cell's own <see cref="PendingBreakToken"/> and turn it into a real pass
604+
/// boundary. Deferring anyway would leave the deferred content measured but never positioned
605+
/// (its <see cref="PerformLayoutImp"/> call returns before reaching <c>CreateLineBoxes</c>/the
606+
/// block-child loop, yet nothing ever resumes it) - found as a real regression while
607+
/// investigating table fragmentation, once R1's forced-break deferral existed to trigger it.
608+
/// </summary>
609+
private bool CanDeferToLaterPass()
610+
{
611+
for (var box = this; box != null; box = box.ParentBox)
612+
{
613+
if (box.Display == CssConstants.TableCell)
614+
return false;
615+
}
616+
return true;
617+
}
618+
597619
/// <summary>
598620
/// Set this box in
599621
/// </summary>
@@ -873,12 +895,23 @@ protected virtual void PerformLayoutImp(RGraphics g)
873895
}
874896
else if (BlockFragmentation.TryGetForcedBreakTarget(this, prevSibling, baseTopWithoutMargin, out var breakSlot, out var breakTop))
875897
{
876-
// A forced break-before/after applies and this is a genuinely fresh entry (no
877-
// resume state of any kind) - defer this box (and everything after it in its
878-
// parent's child loop) to a later pass entirely, rather than positioning it now.
879-
RequestedBreakBeforeSlot = breakSlot;
880-
RequestedBreakBeforeTop = breakTop;
881-
return;
898+
if (CanDeferToLaterPass())
899+
{
900+
// A forced break-before/after applies and this is a genuinely fresh entry
901+
// (no resume state of any kind) - defer this box (and everything after it
902+
// in its parent's child loop) to a later pass entirely, rather than
903+
// positioning it now.
904+
RequestedBreakBeforeSlot = breakSlot;
905+
RequestedBreakBeforeTop = breakTop;
906+
return;
907+
}
908+
909+
// Deferring would never actually be resumed here (see CanDeferToLaterPass) -
910+
// place immediately at the target instead, matching how forced breaks worked
911+
// before real pass-based deferral existed. Not ideal (this content doesn't
912+
// get a fresh fragmentainer pass the way top-level content does), but correct
913+
// rather than silently measured-but-never-positioned.
914+
top = breakTop;
882915
}
883916
else
884917
{
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
using System.Collections.Generic;
2+
using System.Drawing;
3+
using System.Reflection;
4+
using Microsoft.VisualStudio.TestTools.UnitTesting;
5+
using TheArtOfDev.HtmlRenderer.Core;
6+
using TheArtOfDev.HtmlRenderer.Core.Fragments;
7+
using TheArtOfDev.HtmlRenderer.WinForms;
8+
9+
namespace TheArtOfDev.HtmlRenderer.IntegrationTest;
10+
11+
/// <summary>
12+
/// Verifies a real regression found while investigating the fragmentation-engine-parity plan's R7 stage
13+
/// (table resumption), introduced by R1's forced-break deferral: <c>CssLayoutEngineTable</c>'s row loop
14+
/// calls <c>cell.PerformLayout</c> directly and does not participate in the <c>PendingBreakToken</c>
15+
/// bubbling protocol an ordinary block-child loop does. A forced break nested inside a table cell (e.g. a
16+
/// <c>&lt;div style="break-before:page"&gt;</c> inside a <c>&lt;td&gt;</c>) would request deferral to a
17+
/// later pass exactly like any other box - but nothing ever reads that request or resumes it, since a
18+
/// table row is not itself laid out via the block-child loop. The deferred content's own layout returned
19+
/// before ever calling <c>CreateLineBoxes</c>, yet its words had already been measured (unconditional,
20+
/// at the top of every <c>PerformLayoutImp</c> call) - so it ended up rendered at a stale/default (0,0)
21+
/// position, silently overlapping whatever else was there, rather than being lost outright or correctly
22+
/// paginated. Confirmed by direct fragment-tree inspection before the fix: the word appeared, but at the
23+
/// wrong position, with no new page created for it.
24+
/// </summary>
25+
[TestClass]
26+
[DoNotParallelize]
27+
public sealed class StageR7TableCellForcedBreakTest
28+
{
29+
private static HtmlContainerInt GetInternal(HtmlContainer wrapper)
30+
{
31+
var prop = typeof(HtmlContainer).GetProperty("HtmlContainerInt", BindingFlags.NonPublic | BindingFlags.Instance)!;
32+
return (HtmlContainerInt)prop.GetValue(wrapper)!;
33+
}
34+
35+
/// <summary>
36+
/// Reconstructs each word's ABSOLUTE document-Y (fragment rects are page-band-local, so comparing
37+
/// raw <c>Rect.Top</c> values across different fragmentainers is meaningless - a word at local Y=0
38+
/// on page 2 is not "above" a word at local Y=10 on page 1).
39+
/// </summary>
40+
private static void CollectWordsWithAbsoluteY(BoxFragment f, double bandTop, List<(string Text, double AbsoluteTop)> into)
41+
{
42+
foreach (var w in f.Words)
43+
if (!w.Word.IsLineBreak)
44+
into.Add((w.Word.Text, w.Rect.Top + bandTop));
45+
foreach (var c in f.Children)
46+
CollectWordsWithAbsoluteY(c, bandTop, into);
47+
}
48+
49+
[TestMethod]
50+
public async Task ForcedBreakInsideTableCell_DoesNotOverlapOrLoseContent()
51+
{
52+
using var wrapper = new HtmlContainer();
53+
await wrapper.SetHtml(
54+
"""
55+
<html><body>
56+
<table><tr><td>
57+
<div style="margin:0;">BeforeMarker</div>
58+
<div style="margin:0; break-before: page;">AfterMarker</div>
59+
</td></tr></table>
60+
</body></html>
61+
""");
62+
63+
var container = GetInternal(wrapper);
64+
container.PageSize = new TheArtOfDev.HtmlRenderer.Adapters.Entities.RSize(500, 700);
65+
container.MarginTop = 0;
66+
wrapper.MaxSize = new SizeF(500, 0);
67+
68+
using var bitmap = new Bitmap(500, 5000);
69+
using var g = Graphics.FromImage(bitmap);
70+
wrapper.PerformLayout(g);
71+
72+
var tree = container.FragmentTree;
73+
Assert.IsNotNull(tree);
74+
75+
var words = new List<(string Text, double AbsoluteTop)>();
76+
foreach (var f in tree.Fragmentainers)
77+
CollectWordsWithAbsoluteY(f.Root, f.LocalOriginY, words);
78+
79+
var before = words.Find(w => w.Text == "BeforeMarker");
80+
var after = words.Find(w => w.Text == "AfterMarker");
81+
82+
Assert.IsNotNull(before.Text, "BeforeMarker must still be present");
83+
Assert.IsNotNull(after.Text, "AfterMarker must still be present - not silently dropped");
84+
85+
// The real regression: AfterMarker rendered at the SAME position as BeforeMarker (or at a
86+
// stale/default position near zero) rather than being placed below it in normal document flow.
87+
Assert.IsGreaterThan(before.AbsoluteTop, after.AbsoluteTop,
88+
$"AfterMarker (absoluteTop={after.AbsoluteTop}) must render below BeforeMarker (absoluteTop={before.AbsoluteTop}), not overlapping it");
89+
}
90+
}

0 commit comments

Comments
 (0)