Skip to content

Commit 07f4a09

Browse files
committed
Fix rowspan cells silently skipped by the table's break-inside:avoid row-shift
CssLayoutEngineTable.LayoutCells's row-shift correction did `foreach (cell in row.Boxes) cell.OffsetTop(delta)` - but for a row that is the END of a rowspan, row.Boxes holds only the CssSpacingBox placeholder (Display:none, no children/words/rectangles), not the real spanning cell (ExtendedBox). OffsetTop on the placeholder was a silent no-op, leaving the spanning cell's real bottom edge stale while the rest of the row moved to the next page. Confirmed by temporarily reverting the fix: it reliably reproduced the spanning cell's bottom lagging behind its sibling's. Fix extends the spanning cell's ActualBottom (bottom edge only) rather than OffsetTop-ing its whole subtree: its top and content are already anchored to whichever earlier row it started in and shouldn't move, only its bottom edge needs to extend to cover the gap the row-shift just opened up.
1 parent f41c56d commit 07f4a09

2 files changed

Lines changed: 125 additions & 1 deletion

File tree

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -753,7 +753,23 @@ private void LayoutCells(RGraphics g)
753753
var delta = pageGridContainer.PageTopOf(topSlot + 1) - cury;
754754
foreach (CssBox cell in row.Boxes)
755755
{
756-
cell.OffsetTop(delta);
756+
// A rowspan-crossing cell's real content lives on CssSpacingBox.ExtendedBox,
757+
// not on the placeholder itself (Display:none, no children/words/rectangles -
758+
// OffsetTop on it was a silent no-op, leaving the spanning cell's actual
759+
// bottom edge stale while the rest of the row moved on). Unlike an ordinary
760+
// cell, the spanning cell's own top and content are already anchored to
761+
// whichever earlier row it started in (laid out there, unaffected by this
762+
// row's shift) - so rather than OffsetTop-ing the whole subtree (which would
763+
// incorrectly drag its top and content away from that row too), only its
764+
// bottom edge is extended to cover the gap this row's move just opened up.
765+
if (cell is CssSpacingBox spacer)
766+
{
767+
spacer.ExtendedBox.ActualBottom += delta;
768+
}
769+
else
770+
{
771+
cell.OffsetTop(delta);
772+
}
757773
}
758774
maxBottom += delta;
759775
}
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
using System.Collections.Generic;
2+
using System.Drawing;
3+
using System.Linq;
4+
using System.Reflection;
5+
using Microsoft.VisualStudio.TestTools.UnitTesting;
6+
using TheArtOfDev.HtmlRenderer.Core;
7+
using TheArtOfDev.HtmlRenderer.Core.Dom;
8+
using TheArtOfDev.HtmlRenderer.WinForms;
9+
10+
namespace TheArtOfDev.HtmlRenderer.IntegrationTest;
11+
12+
/// <summary>
13+
/// Verifies a real, previously-undocumented gap found while auditing this port's fragmentation engine
14+
/// against PeachPDF a second time: <c>CssLayoutEngineTable.LayoutCells</c>'s <c>break-inside:avoid</c>
15+
/// row-shift correction did <c>foreach (CssBox cell in row.Boxes) cell.OffsetTop(delta)</c> - but for a
16+
/// row that is the END of a rowspan, <c>row.Boxes</c> holds only the <c>CssSpacingBox</c> placeholder
17+
/// (Display:none, no children/words/rectangles), not the real spanning cell (<c>ExtendedBox</c>).
18+
/// <c>OffsetTop</c> on the placeholder was a silent no-op, leaving the spanning cell's real bottom edge
19+
/// stale relative to the rest of the row, which moved on to the next page. Confirmed by temporarily
20+
/// reverting the fix and re-running this exact test: it reliably reproduced the spanning cell's bottom
21+
/// edge lagging behind its sibling's at several filler counts.
22+
/// </summary>
23+
[TestClass]
24+
[DoNotParallelize]
25+
public sealed class RowspanCellShiftTest
26+
{
27+
private static HtmlContainerInt GetInternal(HtmlContainer wrapper)
28+
{
29+
var prop = typeof(HtmlContainer).GetProperty("HtmlContainerInt", BindingFlags.NonPublic | BindingFlags.Instance)!;
30+
return (HtmlContainerInt)prop.GetValue(wrapper)!;
31+
}
32+
33+
private static IEnumerable<CssBox> Walk(CssBox box)
34+
{
35+
yield return box;
36+
foreach (var b in box.Boxes)
37+
foreach (var d in Walk(b))
38+
yield return d;
39+
}
40+
41+
[TestMethod]
42+
public async Task RowspanCellSpanningAShiftedRow_BottomTracksTheShift_NotLeftStale()
43+
{
44+
// Sweep filler counts - the exact boundary where the row-shift fires depends on font-metric
45+
// arithmetic (see this session's established testing lesson: never hardcode a "just barely
46+
// straddles" calibration).
47+
var checkedAnyShift = false;
48+
49+
for (var fillerCount = 1; fillerCount < 60; fillerCount++)
50+
{
51+
using var wrapper = new HtmlContainer();
52+
var filler = string.Concat(Enumerable.Repeat("<p style='margin:0;'>filler line</p>", fillerCount));
53+
// Many extra rows before the rowspan pair push the table's own total height well past one
54+
// page, so RelocateIfNeeded's table-level relocation (which requires the whole table to fit
55+
// within one page) declines, leaving CssLayoutEngineTable's own row-level shift as the ONLY
56+
// mechanism that can act on the straddling row - otherwise a small table gets moved wholesale
57+
// and never exercises this bug at all.
58+
var extraRows = string.Concat(Enumerable.Range(0, 40).Select(i => $"<tr><td>Extra{i}A</td><td>Extra{i}B</td></tr>"));
59+
await wrapper.SetHtml(
60+
$"""
61+
<html><body>
62+
{filler}
63+
<table style="border-collapse:collapse; break-inside:avoid;">
64+
{extraRows}
65+
<tr><td rowspan="2">SpanCellContent</td><td>Row1Cell2</td></tr>
66+
<tr><td>Row2Cell2</td></tr>
67+
</table>
68+
</body></html>
69+
""");
70+
71+
var container = GetInternal(wrapper);
72+
container.PageSize = new TheArtOfDev.HtmlRenderer.Adapters.Entities.RSize(300, 300);
73+
container.MarginTop = 0;
74+
wrapper.MaxSize = new SizeF(300, 0);
75+
76+
using var bitmap = new Bitmap(300, 20000);
77+
using var g = Graphics.FromImage(bitmap);
78+
wrapper.PerformLayout(g);
79+
80+
var allBoxes = Walk(container.Root).ToList();
81+
82+
CssBox? FindEnclosingTd(string text)
83+
{
84+
var wordBox = allBoxes.FirstOrDefault(b => b.Words.Any(w => w.Text.Contains(text)));
85+
for (var b = wordBox; b != null; b = b.ParentBox)
86+
if (b.HtmlTag?.Name == "td")
87+
return b;
88+
return null;
89+
}
90+
91+
var spanCell = FindEnclosingTd("SpanCellContent");
92+
var row2Cell = FindEnclosingTd("Row2Cell2");
93+
if (spanCell == null || row2Cell == null)
94+
continue;
95+
96+
// Only meaningful once the shift has actually fired for this row (row2Cell flush at a fresh
97+
// page top) - otherwise there's nothing to have gotten stale in the first place.
98+
if (System.Math.Abs(row2Cell.Location.Y - container.PageTopOf(container.PageIndexOf(row2Cell.Location.Y))) > 0.5)
99+
continue;
100+
101+
checkedAnyShift = true;
102+
Assert.IsGreaterThanOrEqualTo(row2Cell.ActualBottom - 0.5, spanCell.ActualBottom,
103+
$"at fillerCount={fillerCount}, the rowspan cell's bottom ({spanCell.ActualBottom:F1}) fell short of its sibling's ({row2Cell.ActualBottom:F1}) after the row-shift");
104+
}
105+
106+
Assert.IsTrue(checkedAnyShift, "no filler count in range actually exercised the row-shift - test is not meaningful as written");
107+
}
108+
}

0 commit comments

Comments
 (0)