Skip to content

Commit 4360e0c

Browse files
committed
R7: table content already benefits from R1-R6 fixes; one known limitation documented
Investigated before writing any new table-fragmentation code (same approach as R2/R5/R6): table cells route their own content through the same CssBox.PerformLayoutImp/CreateLineBoxes/ApplyLineBreaking machinery as any other box, so a cell's own paragraphs already correctly benefit from every R1-R6 fix for free. Confirmed via direct testing: a table cell whose own content spans several pages by itself preserves all content correctly, and subsequent rows correctly continue after it - no TableBreakToken/TableRowCursor machinery needed for this, matching the R2/R5/R6 pattern of this architecture rarely needing what it looks like it needs at first glance. The same investigation found one real, confirmed remaining gap: CssLayoutEngineTable.LayoutCells's repeated-<thead> check runs once per ROW (checking the layout cursor's page slot only at that row's own start) - so a row whose own cell content spans MULTIPLE pages by itself only gets a header repeat inserted for the first page it crosses onto, not further intermediate pages that same row continues to span. Not data loss or a crash, just a missing header repeat on some pages of a fairly exotic table shape (one cell vastly longer than its siblings, in a table with a repeating header). A real fix needs to know how many pages a row spans before deciding how much room to reserve for it, which requires relaying the row out a second time once its true span is known - tractable, but deliberately left as a documented, out-of-scope limitation given how rare the shape is versus the far more common case (many ordinary rows, table spans many pages), which already repeats correctly per the existing ThreadRepeatsOnEveryPageTheTableSpans test. New test confirms the working case (no data loss for a multi-page- spanning cell); the known-limitation comment lives directly beside the code it describes rather than a test asserting broken behavior as correct. Full regression suite (43 IntegrationTest + 16 PdfSharp tests) passes.
1 parent 03aa597 commit 4360e0c

2 files changed

Lines changed: 99 additions & 0 deletions

File tree

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -630,6 +630,19 @@ private void LayoutCells(RGraphics g)
630630
// Reserving the room here, before the first row of each continuation page is positioned,
631631
// is what keeps that row from being drawn underneath the repeated header instead of below
632632
// it - a fragment-tree-only repeat (no reservation) would just overlap real content.
633+
//
634+
// KNOWN LIMITATION (confirmed via direct testing, not yet fixed - fragmentation-engine-parity
635+
// plan's R8 stage): this check runs once per ROW (below, gated on `i`), reading `cury`'s slot
636+
// only at that row's own start. A row whose own cell content spans MULTIPLE pages by itself
637+
// (one cell vastly longer than its siblings) only gets a repeat inserted for the FIRST page
638+
// it crosses onto - the header does not repeat on further intermediate pages that same row's
639+
// content continues to span, only reappearing once a LATER row's own start advances the slot
640+
// again. Not data loss or a crash, just a missing header repeat on some pages of a fairly
641+
// exotic table shape. A real fix needs to know how many pages a row spans before deciding how
642+
// much room to reserve for it, which this single-pass-per-row model doesn't have without
643+
// relaying the row out a second time once its true span is known - tractable, but out of
644+
// scope for now given how rare the shape is (the far more common case - many ordinary rows,
645+
// table spans many pages - already repeats correctly, verified by ThreadRepeatsOnEveryPageTheTableSpans).
633646
var pageGridContainer = _tableBox.HtmlContainer;
634647
var repeatsHeader = pageGridContainer != null && pageGridContainer.HasRealPageGrid
635648
&& _headerBox != null && BreakValues.AvoidsBreak(_headerBox.BreakInside);
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
using System.Drawing;
2+
using System.Linq;
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 the fragmentation-engine-parity plan's R7 investigation finding: a table cell whose own
13+
/// content spans several pages by itself (the content routes through the same, already-fixed
14+
/// <c>CssBox.PerformLayoutImp</c>/<c>CreateLineBoxes</c>/<c>ApplyLineBreaking</c> machinery as any other
15+
/// box) is preserved intact and subsequent rows correctly continue after it - no TableBreakToken/
16+
/// TableRowCursor machinery needed for this case, matching the R2/R5/R6 finding that this port's
17+
/// architecture rarely needs what it looks like it needs at first glance.
18+
/// </summary>
19+
/// <remarks>
20+
/// Does NOT cover repeated-header behavior for this shape - a row whose own content spans multiple
21+
/// pages by itself only gets a header repeat inserted for the first page it crosses onto, not further
22+
/// intermediate pages that same row continues to span (see the KNOWN LIMITATION comment beside
23+
/// CssLayoutEngineTable.LayoutCells's repeat-check). Confirmed via direct testing, not fixed - the far
24+
/// more common shape (many ordinary rows, table spans many pages) already repeats correctly per
25+
/// StageD4RepeatedHeaderTest.ThreadRepeatsOnEveryPageTheTableSpans.
26+
/// </remarks>
27+
[TestClass]
28+
[DoNotParallelize]
29+
public sealed class StageR7TableMultiPageCellTest
30+
{
31+
private static HtmlContainerInt GetInternal(HtmlContainer wrapper)
32+
{
33+
var prop = typeof(HtmlContainer).GetProperty("HtmlContainerInt", BindingFlags.NonPublic | BindingFlags.Instance)!;
34+
return (HtmlContainerInt)prop.GetValue(wrapper)!;
35+
}
36+
37+
private static string AllText(BoxFragment f)
38+
{
39+
var words = new System.Collections.Generic.List<string>();
40+
void Collect(BoxFragment x)
41+
{
42+
foreach (var w in x.Words)
43+
if (!w.Word.IsLineBreak)
44+
words.Add(w.Word.Text);
45+
foreach (var c in x.Children)
46+
Collect(c);
47+
}
48+
Collect(f);
49+
return string.Join(" ", words);
50+
}
51+
52+
[TestMethod]
53+
public async Task RowAfterAMultiPageSpanningCell_IsNotLost()
54+
{
55+
using var wrapper = new HtmlContainer();
56+
var sentence = "one two three four five six seven eight nine ten ";
57+
var longCell = string.Concat(Enumerable.Repeat(sentence, 100));
58+
await wrapper.SetHtml(
59+
$"""
60+
<html><body>
61+
<table style="border-collapse:collapse;">
62+
<tr><td style="border:1px solid black;">{longCell}</td><td style="border:1px solid black;">short</td></tr>
63+
<tr><td style="border:1px solid black;">RowTwoCellOne</td><td style="border:1px solid black;">RowTwoCellTwo</td></tr>
64+
</table>
65+
</body></html>
66+
""");
67+
68+
var container = GetInternal(wrapper);
69+
container.PageSize = new TheArtOfDev.HtmlRenderer.Adapters.Entities.RSize(400, 700);
70+
container.MarginTop = 0;
71+
wrapper.MaxSize = new SizeF(400, 0);
72+
73+
using var bitmap = new Bitmap(400, 20000);
74+
using var g = Graphics.FromImage(bitmap);
75+
wrapper.PerformLayout(g);
76+
77+
var tree = container.FragmentTree;
78+
Assert.IsNotNull(tree);
79+
Assert.IsGreaterThan(3, tree.Fragmentainers.Count, "the long cell should genuinely span several pages for this to be meaningful");
80+
81+
var allText = string.Join(" ", tree.Fragmentainers.Select(f => AllText(f.Root)));
82+
StringAssert.Contains(allText, "short");
83+
StringAssert.Contains(allText, "RowTwoCellOne");
84+
StringAssert.Contains(allText, "RowTwoCellTwo");
85+
}
86+
}

0 commit comments

Comments
 (0)