Skip to content

Commit 8903205

Browse files
committed
R3: break-inside:avoid/monolithic relocation via real relayout (R2 folded in)
R2 ("overflow-driven block breaks") turned out to be a no-op given this architecture: RelocateIfNeeded already does nothing for ordinary (non-avoid, non-monolithic) content - it's left to split naturally via the same recursive child-loop/InlineFragmentation math that already produces correct positions, with no "does this fit" decision applicable to a plain block container. There was no core case for R2 to convert. Folding straight into R3, the first stage with a real behavior/code change. RelocateIfNeeded now relays the child out fresh at its target position (CssBox.ResumeAt + a second PerformLayout call, within the same pass) instead of OffsetTop-shifting its already-finished geometry. This is strictly more correct, not just architecturally purer: a relaid-out child's own descendants that have their own break-inside:avoid or a nested forced break get to make that decision relative to the real page boundaries at the NEW position, where a flat OffsetTop shift would have carried whatever decision they made at the old one unchanged - possibly wrong once the shift lands them against a different boundary. Keep-with- next (the preceding-run shift) stays the older OffsetTop correction for now; R4 converts that together with margin truncation. Fixed a real ordering bug surfaced while touching this code: the child loop called RelocateIfNeeded before checking whether the child's own child loop had stopped mid-way (a nested forced break) - a child in that state never reaches its own epilogue, so ActualBottom/Location only reflect a partial pass, and RelocateIfNeeded's straddle test would have read meaningless geometry. Reordered so a pending nested break is checked and bubbled first; also re-checked after the relocation relayout itself, since that relayout can surface its own nested break. Extracted the repeated bubble-and-stop logic into CssBox.BubbleChildPendingToken. New test: a scroll-container (overflow:hidden) taller than one page confirms it's left straddling the boundary in place, not moved (nowhere to move it to would help) or looped. Full regression suite (38 IntegrationTest + 16 PdfSharp tests) passes unchanged.
1 parent b5b1408 commit 8903205

3 files changed

Lines changed: 113 additions & 22 deletions

File tree

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

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -930,17 +930,20 @@ protected virtual void PerformLayoutImp(RGraphics g)
930930
return;
931931
}
932932

933-
BlockFragmentation.RelocateIfNeeded(childBox);
933+
// Checked BEFORE RelocateIfNeeded, not after: a child whose own child loop
934+
// stopped mid-way (a nested forced break) never reached its epilogue, so its
935+
// ActualBottom/Location only reflect a partial pass - RelocateIfNeeded's
936+
// straddle test would read meaningless geometry if run on it.
937+
if (BubbleChildPendingToken(childBox, i))
938+
return;
934939

935-
if (childBox.PendingBreakToken != null)
936-
{
937-
// Child placed itself but stopped somewhere inside its own content/child
938-
// loop - wrap its token in a link naming this box and stop laying out any
939-
// further siblings this pass.
940-
PendingBreakToken = new BlockBreakToken(
941-
this, childBox.PendingBreakToken.ResumeSlotIndex, i, childBox.PendingBreakToken, false, null);
940+
BlockFragmentation.RelocateIfNeeded(g, childBox);
941+
942+
// RelocateIfNeeded's own relayout (see its doc comment) can itself surface a
943+
// break nested inside the relocated child's subtree - e.g. a forced break
944+
// inside a break-inside:avoid container - so check again.
945+
if (BubbleChildPendingToken(childBox, i))
942946
return;
943-
}
944947
}
945948
ActualRight = CalculateActualRight();
946949

@@ -980,6 +983,23 @@ protected virtual void PerformLayoutImp(RGraphics g)
980983
}
981984
}
982985

986+
/// <summary>
987+
/// If <paramref name="childBox"/> stopped somewhere inside its own content/child loop this pass,
988+
/// wraps its token in a link naming this box (at <paramref name="childIndex"/>) and sets it as
989+
/// this box's own <see cref="PendingBreakToken"/>, for the caller to stop laying out any further
990+
/// siblings and return. See <see cref="PendingBreakToken"/>'s doc comment for how this bubbling
991+
/// reaches <see cref="HtmlContainerInt"/>'s pass loop.
992+
/// </summary>
993+
private bool BubbleChildPendingToken(CssBox childBox, int childIndex)
994+
{
995+
if (childBox.PendingBreakToken == null)
996+
return false;
997+
998+
PendingBreakToken = new BlockBreakToken(
999+
this, childBox.PendingBreakToken.ResumeSlotIndex, childIndex, childBox.PendingBreakToken, false, null);
1000+
return true;
1001+
}
1002+
9831003
/// <summary>
9841004
/// Assigns words its width and height
9851005
/// </summary>

‎Source/HtmlRenderer/Core/Fragmentation/BlockFragmentation.cs‎

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
11
using System;
22
using System.Collections.Generic;
3+
using TheArtOfDev.HtmlRenderer.Adapters;
34
using TheArtOfDev.HtmlRenderer.Core.Dom;
45
using TheArtOfDev.HtmlRenderer.Core.Utils;
56

67
namespace TheArtOfDev.HtmlRenderer.Core.Fragmentation
78
{
89
/// <summary>
910
/// Block-level page-break decisions. Being replaced, stage by stage, with real resumable-pass-loop
10-
/// equivalents matching PeachPDF's architecture (see the fragmentation-engine-parity plan) - forced
11-
/// breaks (<see cref="TryGetForcedBreakTarget"/>) already go through <c>CssBox</c>'s real pass loop
12-
/// as of that plan's R1. Margin truncation (<see cref="ResolveBlockTop"/>) and relocation
13-
/// (<see cref="RelocateIfNeeded"/>) remain local, single-pass corrections for now - they only need a
14-
/// box's own natural position, or its already-finished height, and never re-enter content that
15-
/// hasn't been measured yet - until later plan stages replace them too.
11+
/// equivalents matching PeachPDF's architecture (see the fragmentation-engine-parity plan): forced
12+
/// breaks (<see cref="TryGetForcedBreakTarget"/>, plan R1) go through <c>CssBox</c>'s real pass loop
13+
/// across fragmentainers; <c>break-inside:avoid</c>/monolithic relocation (<see cref="RelocateIfNeeded"/>,
14+
/// plan R3) relays the child out fresh at its target position within the SAME pass, rather than
15+
/// shifting already-finished geometry - real relayout, but not yet a cross-pass token, since nothing
16+
/// downstream has been touched yet when it fires. Margin truncation (<see cref="ResolveBlockTop"/>)
17+
/// and keep-with-next (still inside <see cref="RelocateIfNeeded"/>) remain the older flat
18+
/// <c>OffsetTop</c> correction for now, until plan R4 converts them together.
1619
/// </summary>
1720
internal static class BlockFragmentation
1821
{
@@ -85,13 +88,24 @@ internal static bool TryGetForcedBreakTarget(CssBox box, CssBox prevSibling, dou
8588

8689
/// <summary>
8790
/// Called by a block container's child loop right after <paramref name="child"/> (and its whole
88-
/// subtree) has finished laying out. If the child straddles a page boundary and either asks not
89-
/// to be broken (<c>break-inside: avoid</c>) or may not be broken at all (a replaced element, a
90-
/// scroll container), and it fits within a single page's height, the child - and any preceding
91-
/// siblings chained to it by <c>break-after</c>/<c>break-before: avoid</c> (keep-with-next,
92-
/// css-break-3 §3.1) - are shifted down to the next page's content top.
91+
/// subtree) has finished laying out this pass. If the child straddles a page boundary and either
92+
/// asks not to be broken (<c>break-inside: avoid</c>) or may not be broken at all (a replaced
93+
/// element, a scroll container), and it fits within a single page's height, the child is relaid
94+
/// out fresh at the next page's content top - and any preceding siblings chained to it by
95+
/// <c>break-after</c>/<c>break-before: avoid</c> (keep-with-next, css-break-3 §3.1) are shifted
96+
/// there too, via the older <c>OffsetTop</c> correction, since they already finished this pass and
97+
/// keep-with-next itself isn't converted yet.
9398
/// </summary>
94-
internal static void RelocateIfNeeded(CssBox child)
99+
/// <remarks>
100+
/// The child is genuinely relaid out (<c>ResumeAt</c> + <c>PerformLayout</c>), not
101+
/// <c>OffsetTop</c>-shifted the way it used to be and the way its preceding keep-with-next run
102+
/// still is: nothing after this child in its parent's loop has been touched yet this pass, so
103+
/// re-entering its own layout at the new top is cheap, and it is also more correct than a flat
104+
/// shift - any of the child's OWN descendants that themselves have <c>break-inside:avoid</c> or a
105+
/// nested forced break get to make their own decision relative to the real page boundaries at the
106+
/// new position, rather than blindly carrying whatever decision they made at the old one.
107+
/// </remarks>
108+
internal static void RelocateIfNeeded(RGraphics g, CssBox child)
95109
{
96110
var container = child.HtmlContainer;
97111
if (container == null || !container.HasRealPageGrid || child.IsOutOfFlow)
@@ -123,7 +137,8 @@ internal static void RelocateIfNeeded(CssBox child)
123137
member.OffsetTop(delta);
124138
}
125139

126-
child.OffsetTop(delta);
140+
child.ResumeAt(null, target);
141+
child.PerformLayout(g);
127142
}
128143

129144
/// <summary>
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
using System.Drawing;
2+
using System.Reflection;
3+
using Microsoft.VisualStudio.TestTools.UnitTesting;
4+
using TheArtOfDev.HtmlRenderer.Core;
5+
using TheArtOfDev.HtmlRenderer.WinForms;
6+
7+
namespace TheArtOfDev.HtmlRenderer.IntegrationTest;
8+
9+
/// <summary>
10+
/// Verifies the R3 stage of the fragmentation-engine-parity plan: <c>break-inside:avoid</c>/monolithic
11+
/// relocation now relays the child out fresh at its target position (<c>CssBox.ResumeAt</c> + a second
12+
/// <c>PerformLayout</c> call within the same pass) instead of shifting already-finished geometry with
13+
/// <c>OffsetTop</c>.
14+
/// </summary>
15+
[TestClass]
16+
[DoNotParallelize]
17+
public sealed class StageR3RelocationTest
18+
{
19+
private static HtmlContainerInt GetInternal(HtmlContainer wrapper)
20+
{
21+
var prop = typeof(HtmlContainer).GetProperty("HtmlContainerInt", BindingFlags.NonPublic | BindingFlags.Instance)!;
22+
return (HtmlContainerInt)prop.GetValue(wrapper)!;
23+
}
24+
25+
[TestMethod]
26+
public async Task MonolithicContentTallerThanOnePage_IsLeftInPlace_NotMoved()
27+
{
28+
using var wrapper = new HtmlContainer();
29+
// A scroll container (overflow:hidden, MonolithicContent.IsScrollContainer) taller than the
30+
// 700px page - RelocateIfNeeded's "fits on no single page" guard must leave it straddling the
31+
// boundary in place rather than moving it (nowhere to move it TO would help) or looping.
32+
await wrapper.SetHtml(
33+
"""
34+
<html><body>
35+
<div style="margin:0; height:250px;">filler</div>
36+
<div style="margin:0; height:900px; overflow:hidden;">monolithic content taller than one page</div>
37+
</body></html>
38+
""");
39+
40+
var container = GetInternal(wrapper);
41+
container.PageSize = new TheArtOfDev.HtmlRenderer.Adapters.Entities.RSize(500, 700);
42+
container.MarginTop = 0;
43+
wrapper.MaxSize = new SizeF(500, 0);
44+
45+
using var bitmap = new Bitmap(500, 5000);
46+
using var g = Graphics.FromImage(bitmap);
47+
wrapper.PerformLayout(g);
48+
49+
var tree = container.FragmentTree;
50+
Assert.IsNotNull(tree);
51+
// Straddles the one boundary it naturally crosses (250 + 900 = 1150, past the 700px mark) and
52+
// stops there - not moved to a later page (which would still not fit it whole) and not spun
53+
// into extra pages by a mistaken relocation attempt.
54+
Assert.AreEqual(2, tree.Fragmentainers.Count);
55+
}
56+
}

0 commit comments

Comments
 (0)