|
| 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.Fragments; |
| 8 | +using TheArtOfDev.HtmlRenderer.WinForms; |
| 9 | + |
| 10 | +namespace TheArtOfDev.HtmlRenderer.IntegrationTest; |
| 11 | + |
| 12 | +/// <summary> |
| 13 | +/// Verifies the fragmentation-engine-parity plan's R9 investigation finding: PeachPDF's "keep-with-next |
| 14 | +/// run-pull rewind across an already-frozen fragmentainer" does not have a counterpart problem in this |
| 15 | +/// port's architecture, so no new rewind machinery is needed - the existing same-pass |
| 16 | +/// <see cref="TheArtOfDev.HtmlRenderer.Core.Fragmentation.BlockFragmentation.EnforceKeepWithNext"/> (R4) |
| 17 | +/// already covers it. |
| 18 | +/// </summary> |
| 19 | +/// <remarks> |
| 20 | +/// PeachPDF needs a real cross-pass rewind because ordinary overflow-driven pagination is itself a real |
| 21 | +/// pass boundary there - a keep-with-next violation discovered while laying out page N+1 may need to |
| 22 | +/// reach back into page N's content, which was already committed via that pass's own <c>EmitPass</c>. |
| 23 | +/// In this port, only a FORCED break (<c>break-before/after: page</c>) ever creates a real pass boundary |
| 24 | +/// in <c>HtmlContainerInt.DriveLayoutPasses</c> - ordinary overflow and <c>break-inside:avoid</c> are both |
| 25 | +/// same-pass local corrections (R2/R3), and <c>FragmentEmitter</c> runs once, only after every pass has |
| 26 | +/// settled, so nothing is ever truly "frozen" mid-layout the way PeachPDF's per-pass emit makes it. |
| 27 | +/// A keep-with-next run is therefore always laid out - and checked by <c>EnforceKeepWithNext</c> - within |
| 28 | +/// the SAME pass as the sibling it's chained to, even immediately after resuming from an unrelated forced |
| 29 | +/// break earlier in the document, as this test confirms directly against the fragment tree. And a run |
| 30 | +/// could never need to be pulled across a forced break itself either way: the forced break is the |
| 31 | +/// intentional separator keep-with-next exists to avoid accidentally recreating, not an obstacle to undo. |
| 32 | +/// </remarks> |
| 33 | +[TestClass] |
| 34 | +[DoNotParallelize] |
| 35 | +public sealed class StageR9KeepWithNextAcrossForcedBreakTest |
| 36 | +{ |
| 37 | + private static HtmlContainerInt GetInternal(HtmlContainer wrapper) |
| 38 | + { |
| 39 | + var prop = typeof(HtmlContainer).GetProperty("HtmlContainerInt", BindingFlags.NonPublic | BindingFlags.Instance)!; |
| 40 | + return (HtmlContainerInt)prop.GetValue(wrapper)!; |
| 41 | + } |
| 42 | + |
| 43 | + private static string AllText(BoxFragment f) |
| 44 | + { |
| 45 | + var words = new List<string>(); |
| 46 | + void Collect(BoxFragment x) |
| 47 | + { |
| 48 | + foreach (var w in x.Words) |
| 49 | + if (!w.Word.IsLineBreak) |
| 50 | + words.Add(w.Word.Text); |
| 51 | + foreach (var c in x.Children) |
| 52 | + Collect(c); |
| 53 | + } |
| 54 | + Collect(f); |
| 55 | + return string.Join(" ", words); |
| 56 | + } |
| 57 | + |
| 58 | + [TestMethod] |
| 59 | + public async Task KeepWithNextPairRightAfterAForcedBreak_StaysTogether_OnTheResumedPage() |
| 60 | + { |
| 61 | + using var wrapper = new HtmlContainer(); |
| 62 | + var filler = string.Concat(Enumerable.Repeat("<p style='margin:0;'>filler line of text</p>", 39)); |
| 63 | + await wrapper.SetHtml( |
| 64 | + $""" |
| 65 | + <html><body> |
| 66 | + <div style="margin:0; break-before: page;">ForcedBreakMarker</div> |
| 67 | + {filler} |
| 68 | + <h4 style="margin:0; break-after: avoid;">Section heading</h4> |
| 69 | + <p style="margin:0;">Paragraph right after the heading.</p> |
| 70 | + </body></html> |
| 71 | + """); |
| 72 | + |
| 73 | + var container = GetInternal(wrapper); |
| 74 | + container.PageSize = new TheArtOfDev.HtmlRenderer.Adapters.Entities.RSize(595, 800); |
| 75 | + container.MarginTop = 20; |
| 76 | + wrapper.MaxSize = new SizeF(595, 0); |
| 77 | + |
| 78 | + using var bitmap = new Bitmap(595, 20000); |
| 79 | + using var g = Graphics.FromImage(bitmap); |
| 80 | + wrapper.PerformLayout(g); |
| 81 | + |
| 82 | + var tree = container.FragmentTree; |
| 83 | + Assert.IsNotNull(tree); |
| 84 | + Assert.IsGreaterThanOrEqualTo(2, tree.Fragmentainers.Count, "the forced break must actually introduce a real pass boundary for this test to be meaningful"); |
| 85 | + |
| 86 | + var pageOfHeading = -1; |
| 87 | + var pageOfParagraph = -1; |
| 88 | + for (var i = 0; i < tree.Fragmentainers.Count; i++) |
| 89 | + { |
| 90 | + var text = AllText(tree.Fragmentainers[i].Root); |
| 91 | + if (text.Contains("Section heading")) pageOfHeading = i; |
| 92 | + if (text.Contains("Paragraph right after the heading.")) pageOfParagraph = i; |
| 93 | + } |
| 94 | + |
| 95 | + Assert.AreNotEqual(-1, pageOfHeading, "heading must not be lost"); |
| 96 | + Assert.AreNotEqual(-1, pageOfParagraph, "paragraph must not be lost"); |
| 97 | + Assert.AreEqual(pageOfHeading, pageOfParagraph, "break-after:avoid must keep the heading with its paragraph even immediately after resuming from an unrelated forced break"); |
| 98 | + } |
| 99 | +} |
0 commit comments