|
| 1 | +using HtmlRenderer.IntegrationTest.TestSupport; |
| 2 | +using TheArtOfDev.HtmlRenderer.Core.Dom; |
| 3 | + |
| 4 | +namespace HtmlRenderer.IntegrationTest.Layout; |
| 5 | + |
| 6 | +/// <summary> |
| 7 | +/// Ported from PeachPDF.Tests/Integration/FloatLayoutRegressionTests.cs. CSS 2.1 §9.5: a floated box is taken |
| 8 | +/// out of normal flow and shifted to the left/right edge of its containing block; subsequent inline content |
| 9 | +/// flows around it, and other floats stack against it rather than overlapping. §9.5.2 defines <c>clear</c> as |
| 10 | +/// only clearing floats of the matching (or "both") side. |
| 11 | +/// <para> |
| 12 | +/// The four perf-regression-guard cases from the source file (<c>FloatScanCalls</c>/<c>FloatScanBoxVisits</c> |
| 13 | +/// counters on <c>HtmlContainerInt</c>, guarding an O(document size) vs O(1) float-scan short-circuit) are not |
| 14 | +/// ported: this fork's <c>HtmlContainerInt</c> has the same <c>HasFloatedBoxes</c> short-circuit and float-scan |
| 15 | +/// helpers (confirmed in <c>DomUtils.GetFirstIntersectingFloatBox</c> et al.), but no <c>FloatScanCalls</c>/ |
| 16 | +/// <c>FloatScanBoxVisits</c> instrumentation to observe it through - that's an internal perf counter, not CSS |
| 17 | +/// 2.1 behavior, so it's out of scope for this pass rather than something to add. |
| 18 | +/// </para> |
| 19 | +/// </summary> |
| 20 | +[DoNotParallelize] |
| 21 | +[TestClass] |
| 22 | +public sealed class FloatLayoutIntegrationTests |
| 23 | +{ |
| 24 | + private const double Delta = 1.0; |
| 25 | + |
| 26 | + [TestMethod] |
| 27 | + public void Float_PushesFollowingSiblingTextToTheRight() |
| 28 | + { |
| 29 | + var html = LayoutHarness.Wrap( |
| 30 | + "<div style='width:300px;'>" |
| 31 | + + "<div style='float:left; width:100px; height:50px;'></div>" |
| 32 | + + "<p id='text' style='margin:0;'>Hello world</p></div>"); |
| 33 | + |
| 34 | + var (root, _) = LayoutHarness.Layout(html); |
| 35 | + var text = LayoutHarness.FindById(root, "text")!; |
| 36 | + var firstWord = FindFirstWord(text); |
| 37 | + |
| 38 | + Assert.IsNotNull(firstWord); |
| 39 | + Assert.IsTrue(firstWord!.Left >= 90, |
| 40 | + $"first word should be pushed right past the 100px float, was at {firstWord.Left}"); |
| 41 | + } |
| 42 | + |
| 43 | + [TestMethod] |
| 44 | + public void WithoutFloat_SiblingTextStartsAtContainerEdge() |
| 45 | + { |
| 46 | + var html = LayoutHarness.Wrap( |
| 47 | + "<div style='width:300px;'>" |
| 48 | + + "<div style='width:100px; height:50px;'></div>" |
| 49 | + + "<p id='text' style='margin:0;'>Hello world</p></div>"); |
| 50 | + |
| 51 | + var (root, _) = LayoutHarness.Layout(html); |
| 52 | + var text = LayoutHarness.FindById(root, "text")!; |
| 53 | + var firstWord = FindFirstWord(text); |
| 54 | + |
| 55 | + Assert.IsNotNull(firstWord); |
| 56 | + Assert.IsTrue(firstWord!.Left < 10, |
| 57 | + $"first word should start at the container's left edge without a float, was at {firstWord.Left}"); |
| 58 | + } |
| 59 | + |
| 60 | + [TestMethod] |
| 61 | + public void Float_NarrowsAvailableWidth_SoTextWrapsToMoreLines() |
| 62 | + { |
| 63 | + const string longText = |
| 64 | + "This is a fairly long sentence that should wrap across multiple lines once the available width is narrowed by a floated sibling element."; |
| 65 | + |
| 66 | + var withFloatHtml = LayoutHarness.Wrap( |
| 67 | + $"<div style='width:250px;'><div style='float:left; width:150px; height:40px;'></div>" |
| 68 | + + $"<p id='text' style='margin:0;'>{longText}</p></div>"); |
| 69 | + var withoutFloatHtml = LayoutHarness.Wrap( |
| 70 | + $"<div style='width:250px;'><p id='text' style='margin:0;'>{longText}</p></div>"); |
| 71 | + |
| 72 | + var (withFloatRoot, _) = LayoutHarness.Layout(withFloatHtml); |
| 73 | + var (withoutFloatRoot, _) = LayoutHarness.Layout(withoutFloatHtml); |
| 74 | + |
| 75 | + var withFloatText = LayoutHarness.FindById(withFloatRoot, "text")!; |
| 76 | + var withoutFloatText = LayoutHarness.FindById(withoutFloatRoot, "text")!; |
| 77 | + |
| 78 | + Assert.IsTrue(withFloatText.ActualBottom - withFloatText.Location.Y |
| 79 | + > withoutFloatText.ActualBottom - withoutFloatText.Location.Y, |
| 80 | + "narrowing the line width with a float should force extra line wraps and a taller box " |
| 81 | + + $"(with float height: {withFloatText.ActualBottom - withFloatText.Location.Y}, " |
| 82 | + + $"without: {withoutFloatText.ActualBottom - withoutFloatText.Location.Y})"); |
| 83 | + } |
| 84 | + |
| 85 | + [TestMethod] |
| 86 | + public void FloatLeft_WrapsBelowAFullWidthFloatRightSibling() |
| 87 | + { |
| 88 | + // A float:left box that would overlap a previously-placed, full-width float:right sibling must wrap |
| 89 | + // below it rather than overlapping. |
| 90 | + var html = LayoutHarness.Wrap( |
| 91 | + "<div style='width:200px;'>" |
| 92 | + + "<div id='r' style='float:right; width:200px; height:50px;'></div>" |
| 93 | + + "<div id='l' style='float:left; width:100px; height:30px;'></div></div>"); |
| 94 | + |
| 95 | + var (root, _) = LayoutHarness.Layout(html); |
| 96 | + var r = LayoutHarness.FindById(root, "r")!; |
| 97 | + var l = LayoutHarness.FindById(root, "l")!; |
| 98 | + |
| 99 | + Assert.IsTrue(l.Location.Y >= r.ActualBottom, |
| 100 | + $"float:left box should wrap below the full-width float:right sibling it can't fit beside " |
| 101 | + + $"(l.Y={l.Location.Y}, r.ActualBottom={r.ActualBottom})"); |
| 102 | + } |
| 103 | + |
| 104 | + [TestMethod] |
| 105 | + public void FloatRight_InNarrowerNestedBlock_AvoidsAWiderAncestorFloatRightSibling() |
| 106 | + { |
| 107 | + // A float:right box placed inside a narrower, non-floated nested block still avoids an ancestor |
| 108 | + // float:right sibling that sits past the nested block's own right edge. |
| 109 | + var html = LayoutHarness.Wrap( |
| 110 | + "<div style='width:500px;'>" |
| 111 | + + "<div id='outerR' style='float:right; width:50px; height:80px;'></div>" |
| 112 | + + "<div style='width:200px;'><div id='r' style='float:right; width:100px; height:30px;'></div></div></div>"); |
| 113 | + |
| 114 | + var (root, _) = LayoutHarness.Layout(html); |
| 115 | + var outerR = LayoutHarness.FindById(root, "outerR")!; |
| 116 | + var r = LayoutHarness.FindById(root, "r")!; |
| 117 | + |
| 118 | + Assert.AreEqual(outerR.Location.X - outerR.ActualMarginLeft, r.ActualRight, Delta); |
| 119 | + } |
| 120 | + |
| 121 | + [TestMethod] |
| 122 | + public void FloatRight_InNarrowerNestedBlock_WithMarginLeft_StillAvoidsAWiderAncestorFloatRightSibling() |
| 123 | + { |
| 124 | + var html = LayoutHarness.Wrap( |
| 125 | + "<div style='width:500px;'>" |
| 126 | + + "<div id='outerR' style='float:right; width:280px; height:80px;'></div>" |
| 127 | + + "<div style='width:200px;'>" |
| 128 | + + "<div id='r' style='float:right; width:100px; height:30px; margin-left:40px;'></div></div></div>"); |
| 129 | + |
| 130 | + var (root, _) = LayoutHarness.Layout(html); |
| 131 | + var outerR = LayoutHarness.FindById(root, "outerR")!; |
| 132 | + var r = LayoutHarness.FindById(root, "r")!; |
| 133 | + |
| 134 | + Assert.AreEqual(outerR.Location.X - outerR.ActualMarginLeft, r.ActualRight, Delta); |
| 135 | + } |
| 136 | + |
| 137 | + [TestMethod] |
| 138 | + public void FloatRight_NarrowsLineWrapWidth_SoTextWrapsBeforeReachingIt() |
| 139 | + { |
| 140 | + var html = LayoutHarness.Wrap( |
| 141 | + "<div style='width:300px;'>" |
| 142 | + + "<div id='f' style='float:right; width:100px; height:50px;'></div>" |
| 143 | + + "<p id='text' style='margin:0;'>this line of text should wrap before it reaches the floated box on the right</p></div>"); |
| 144 | + |
| 145 | + var (root, _) = LayoutHarness.Layout(html); |
| 146 | + var floatBox = LayoutHarness.FindById(root, "f")!; |
| 147 | + var text = LayoutHarness.FindById(root, "text")!; |
| 148 | + var floatLeftEdge = floatBox.Location.X - floatBox.ActualMarginLeft; |
| 149 | + |
| 150 | + var wordsOverlappingFloat = WordsOverlappingVerticalSpan(text, floatBox.Location.Y, floatBox.ActualBottom); |
| 151 | + |
| 152 | + Assert.AreNotEqual(0, wordsOverlappingFloat.Count); |
| 153 | + |
| 154 | + foreach (var word in wordsOverlappingFloat) |
| 155 | + { |
| 156 | + Assert.IsTrue(word.Left + word.Width <= floatLeftEdge + 1, |
| 157 | + $"word '{word.Text}' at right={word.Left + word.Width} overlaps the float:right box, " |
| 158 | + + $"whose left edge (including margin) is at {floatLeftEdge}"); |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + [TestMethod] |
| 163 | + public void FloatRight_WithMarginLeft_StillReachesContainingBlockRightEdge() |
| 164 | + { |
| 165 | + var html = LayoutHarness.Wrap( |
| 166 | + "<dl style='width:200px; margin:0; padding:0; border:0;'>" |
| 167 | + + "<dd id='dd' style='float:right; width:80px; height:20px; margin:0 0 0 10px;'></dd></dl>"); |
| 168 | + |
| 169 | + var (root, _) = LayoutHarness.Layout(html); |
| 170 | + var dl = LayoutHarness.FindById(root, "dd")!.ParentBox; |
| 171 | + var dd = LayoutHarness.FindById(root, "dd")!; |
| 172 | + |
| 173 | + Assert.AreEqual(dl.ClientRight, dd.ActualRight, Delta); |
| 174 | + } |
| 175 | + |
| 176 | + [TestMethod] |
| 177 | + public void FloatLeft_StillNarrowsLineWrapWidth_AfterTheRightFloatFix() |
| 178 | + { |
| 179 | + const string longText = |
| 180 | + "this line of text should wrap below and around the floated box on the left before it reaches the container edge"; |
| 181 | + |
| 182 | + var withFloatHtml = LayoutHarness.Wrap( |
| 183 | + $"<div style='width:300px;'><div id='f' style='float:left; width:100px; height:50px;'></div>" |
| 184 | + + $"<p id='text' style='margin:0;'>{longText}</p></div>"); |
| 185 | + var withoutFloatHtml = LayoutHarness.Wrap( |
| 186 | + $"<div style='width:300px;'><p id='text' style='margin:0;'>{longText}</p></div>"); |
| 187 | + |
| 188 | + var (withFloatRoot, _) = LayoutHarness.Layout(withFloatHtml); |
| 189 | + var (withoutFloatRoot, _) = LayoutHarness.Layout(withoutFloatHtml); |
| 190 | + |
| 191 | + var floatBox = LayoutHarness.FindById(withFloatRoot, "f")!; |
| 192 | + var withFloatText = LayoutHarness.FindById(withFloatRoot, "text")!; |
| 193 | + var withoutFloatText = LayoutHarness.FindById(withoutFloatRoot, "text")!; |
| 194 | + var floatRightEdge = floatBox.ActualRight + floatBox.ActualMarginRight; |
| 195 | + |
| 196 | + var wordsOverlappingFloat = |
| 197 | + WordsOverlappingVerticalSpan(withFloatText, floatBox.Location.Y, floatBox.ActualBottom); |
| 198 | + |
| 199 | + Assert.AreNotEqual(0, wordsOverlappingFloat.Count); |
| 200 | + |
| 201 | + foreach (var word in wordsOverlappingFloat) |
| 202 | + { |
| 203 | + Assert.IsTrue(word.Left >= floatRightEdge - 1, |
| 204 | + $"word '{word.Text}' at left={word.Left} starts before the float:left box's right edge " |
| 205 | + + $"(including margin) at {floatRightEdge}"); |
| 206 | + } |
| 207 | + |
| 208 | + Assert.IsTrue(withFloatText.ActualBottom - withFloatText.Location.Y |
| 209 | + > withoutFloatText.ActualBottom - withoutFloatText.Location.Y, |
| 210 | + "narrowing the line width with a float:left should force extra line wraps and a taller box"); |
| 211 | + } |
| 212 | + |
| 213 | + [TestMethod] |
| 214 | + public void ClearLeft_IgnoresAPrecedingFloatRightSibling() |
| 215 | + { |
| 216 | + // clear:left only clears past float:left siblings - a float:right sibling must not push it down. |
| 217 | + var html = LayoutHarness.Wrap( |
| 218 | + "<div style='width:200px;'>" |
| 219 | + + "<div id='r' style='float:right; width:50px; height:80px;'></div>" |
| 220 | + + "<div id='cleared' style='clear:left; margin:0;'>text</div></div>"); |
| 221 | + |
| 222 | + var (root, _) = LayoutHarness.Layout(html); |
| 223 | + var cleared = LayoutHarness.FindById(root, "cleared")!; |
| 224 | + |
| 225 | + Assert.IsTrue(cleared.Location.Y < 80, |
| 226 | + $"clear:left must not clear past a float:right sibling, was pushed to Y={cleared.Location.Y}"); |
| 227 | + } |
| 228 | + |
| 229 | + [TestMethod] |
| 230 | + public void ClearRight_IgnoresAPrecedingFloatLeftSibling() |
| 231 | + { |
| 232 | + // Symmetric case: clear:right ignoring a float:left sibling. |
| 233 | + var html = LayoutHarness.Wrap( |
| 234 | + "<div style='width:200px;'>" |
| 235 | + + "<div id='l' style='float:left; width:50px; height:80px;'></div>" |
| 236 | + + "<div id='cleared' style='clear:right; margin:0;'>text</div></div>"); |
| 237 | + |
| 238 | + var (root, _) = LayoutHarness.Layout(html); |
| 239 | + var cleared = LayoutHarness.FindById(root, "cleared")!; |
| 240 | + |
| 241 | + Assert.IsTrue(cleared.Location.Y < 80, |
| 242 | + $"clear:right must not clear past a float:left sibling, was pushed to Y={cleared.Location.Y}"); |
| 243 | + } |
| 244 | + |
| 245 | + // ── Helpers ──────────────────────────────────────────────────────────── |
| 246 | + |
| 247 | + private static CssRect? FindFirstWord(CssBox box) |
| 248 | + { |
| 249 | + if (box.Words.Count > 0) return box.Words[0]; |
| 250 | + foreach (var child in box.Boxes) |
| 251 | + { |
| 252 | + var found = FindFirstWord(child); |
| 253 | + if (found is not null) return found; |
| 254 | + } |
| 255 | + return null; |
| 256 | + } |
| 257 | + |
| 258 | + private static List<CssRect> WordsOverlappingVerticalSpan(CssBox box, double top, double bottom) |
| 259 | + { |
| 260 | + List<CssRect> words = []; |
| 261 | + CollectWordsOverlappingVerticalSpan(box, top, bottom, words); |
| 262 | + return words; |
| 263 | + } |
| 264 | + |
| 265 | + private static void CollectWordsOverlappingVerticalSpan(CssBox box, double top, double bottom, List<CssRect> words) |
| 266 | + { |
| 267 | + foreach (var word in box.Words) |
| 268 | + { |
| 269 | + if (word.Top < bottom && word.Top + word.Height > top) |
| 270 | + { |
| 271 | + words.Add(word); |
| 272 | + } |
| 273 | + } |
| 274 | + |
| 275 | + foreach (var child in box.Boxes) |
| 276 | + { |
| 277 | + CollectWordsOverlappingVerticalSpan(child, top, bottom, words); |
| 278 | + } |
| 279 | + } |
| 280 | +} |
0 commit comments