|
| 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