|
| 1 | +using HtmlRenderer.IntegrationTest.TestSupport; |
| 2 | + |
| 3 | +namespace HtmlRenderer.IntegrationTest.Positioning; |
| 4 | + |
| 5 | +/// <summary> |
| 6 | +/// Ported from PeachPDF.Tests' Acid2FeatureVerificationTests.cs (the position:relative/absolute/fixed offset |
| 7 | +/// section) and AbsolutePositioningIntegrationTests.cs's shrink-to-fit cases. CSS 2.1 §9.4.3: for each axis of |
| 8 | +/// a relatively/absolutely positioned box, the "near" offset (<c>left</c>/<c>top</c>) wins when set; if it's |
| 9 | +/// <c>auto</c> and the "far" offset (<c>right</c>/<c>bottom</c>) isn't, the far offset applies with its sign |
| 10 | +/// flipped. §9.4.3 also requires relative positioning to be purely visual - it must not affect the parent's |
| 11 | +/// content-driven height or any following sibling's layout. §10.3.7: an absolutely positioned box's offsets |
| 12 | +/// are measured from its nearest positioned ancestor's PADDING edge, and (like every other positioning scheme) |
| 13 | +/// its own margin still applies on top of that offset; with no explicit width, it shrinks to fit its content. |
| 14 | +/// <para> |
| 15 | +/// The PeachPDF source file's flexbox/grid blockification cases and detached-<thead>/<tfoot> |
| 16 | +/// containing-block cases are not ported: this fork has neither a flex/grid layout engine nor the notion of a |
| 17 | +/// detached header/footer proxy box those target. |
| 18 | +/// </para> |
| 19 | +/// </summary> |
| 20 | +[DoNotParallelize] |
| 21 | +[TestClass] |
| 22 | +public sealed class AbsolutePositioningIntegrationTests |
| 23 | +{ |
| 24 | + private const double Delta = 1.0; |
| 25 | + |
| 26 | + [TestMethod] |
| 27 | + public void PositionRelative_BottomOffset_MovesBoxOppositeDirection() |
| 28 | + { |
| 29 | + // top is auto, bottom is set - a positive "bottom" pulls the box UP, i.e. subtracts from Y. |
| 30 | + var html = LayoutHarness.Wrap("<div id='t' style='position:relative; bottom:10px; width:10px; height:10px;'></div>"); |
| 31 | + var (root, _) = LayoutHarness.Layout(html); |
| 32 | + var box = LayoutHarness.FindById(root, "t")!; |
| 33 | + |
| 34 | + // Static-flow position is Y=0 (LayoutHarness.Wrap sets body margin:0); "bottom:10px" must move it to Y=-10. |
| 35 | + Assert.AreEqual(-10, box.Location.Y, Delta); |
| 36 | + } |
| 37 | + |
| 38 | + [TestMethod] |
| 39 | + public void PositionRelative_Offset_DoesNotAffectParentHeightOrFollowingSibling() |
| 40 | + { |
| 41 | + // The offset box (and its descendants) move, but the parent's content-driven height and every |
| 42 | + // following sibling must lay out against the STATIC position. |
| 43 | + var html = LayoutHarness.Wrap( |
| 44 | + "<div id='parent'>" |
| 45 | + + "<div id='shifted' style='position:relative; bottom:-30px; height:40px;'></div>" |
| 46 | + + "</div>" |
| 47 | + + "<div id='after' style='height:10px;'></div>"); |
| 48 | + var (root, _) = LayoutHarness.Layout(html); |
| 49 | + var parent = LayoutHarness.FindById(root, "parent")!; |
| 50 | + var shifted = LayoutHarness.FindById(root, "shifted")!; |
| 51 | + var after = LayoutHarness.FindById(root, "after")!; |
| 52 | + |
| 53 | + // The offset itself is applied visually: the child sits 30px below the parent's top... |
| 54 | + Assert.AreEqual(30, shifted.Location.Y - parent.Location.Y, Delta); |
| 55 | + |
| 56 | + // ...but the parent is still exactly 40px tall (the child's static extent)... |
| 57 | + Assert.AreEqual(40, parent.ActualBottom - parent.Location.Y, Delta); |
| 58 | + |
| 59 | + // ...and the following sibling starts at the parent's un-inflated bottom. |
| 60 | + Assert.AreEqual(parent.ActualBottom, after.Location.Y, Delta); |
| 61 | + } |
| 62 | + |
| 63 | + [TestMethod] |
| 64 | + public void PositionRelative_OffsetOnBoxItself_DoesNotShiftFollowingSibling() |
| 65 | + { |
| 66 | + // "after" must lay out against "shifted"'s static bottom, not its visually offset bottom 25px lower. |
| 67 | + var html = LayoutHarness.Wrap( |
| 68 | + "<div id='shifted' style='position:relative; top:25px; height:40px;'></div>" |
| 69 | + + "<div id='after' style='height:10px;'></div>"); |
| 70 | + var (root, _) = LayoutHarness.Layout(html); |
| 71 | + var shifted = LayoutHarness.FindById(root, "shifted")!; |
| 72 | + var after = LayoutHarness.FindById(root, "after")!; |
| 73 | + |
| 74 | + Assert.AreEqual(25, shifted.RelativeOffsetY, Delta); |
| 75 | + Assert.AreEqual(shifted.ActualBottom - 25, after.Location.Y, Delta); |
| 76 | + } |
| 77 | + |
| 78 | + [TestMethod] |
| 79 | + public void PositionAbsolute_BottomOffset_PositionsRelativeToContainingBlockBottomEdge() |
| 80 | + { |
| 81 | + var html = LayoutHarness.Wrap( |
| 82 | + "<div id='cb' style='position:relative; width:100px; height:100px;'>" |
| 83 | + + "<div id='t' style='position:absolute; bottom:10px; width:10px; height:10px;'></div></div>"); |
| 84 | + var (root, _) = LayoutHarness.Layout(html); |
| 85 | + var cb = LayoutHarness.FindById(root, "cb")!; |
| 86 | + var box = LayoutHarness.FindById(root, "t")!; |
| 87 | + |
| 88 | + // Box's bottom edge must sit 10px above the containing block's own bottom (padding) edge. |
| 89 | + Assert.AreEqual(cb.ActualBottom - 10, box.ActualBottom, Delta); |
| 90 | + } |
| 91 | + |
| 92 | + [TestMethod] |
| 93 | + public void PositionAbsolute_WithMarginAndBorderedContainingBlock_AppliesBothCorrectly() |
| 94 | + { |
| 95 | + var html = LayoutHarness.Wrap( |
| 96 | + "<div id='cb' style='position:relative; border:16px solid black; width:100px; height:100px;'>" |
| 97 | + + "<div id='t' style='position:absolute; top:0; left:0; margin:36px 0 0 60px; width:10px; height:10px;'></div></div>"); |
| 98 | + var (root, _) = LayoutHarness.Layout(html); |
| 99 | + var cb = LayoutHarness.FindById(root, "cb")!; |
| 100 | + var box = LayoutHarness.FindById(root, "t")!; |
| 101 | + |
| 102 | + // Expected: containing block's PADDING edge (border-box + 16px border) + the box's own margin. |
| 103 | + Assert.AreEqual(cb.Location.X + 16 + 60, box.Location.X, Delta); |
| 104 | + Assert.AreEqual(cb.Location.Y + 16 + 36, box.Location.Y, Delta); |
| 105 | + } |
| 106 | + |
| 107 | + [TestMethod] |
| 108 | + public void PositionFixed_WithMargin_AppliesMarginOnTopOfOffset() |
| 109 | + { |
| 110 | + var html = LayoutHarness.Wrap( |
| 111 | + "<div id='t' style='position:fixed; top:10px; left:20px; margin:5px 0 0 8px; width:10px; height:10px;'></div>"); |
| 112 | + var (root, _) = LayoutHarness.Layout(html); |
| 113 | + var box = LayoutHarness.FindById(root, "t")!; |
| 114 | + |
| 115 | + Assert.AreEqual(20 + 8, box.Location.X, Delta); |
| 116 | + Assert.AreEqual(10 + 5, box.Location.Y, Delta); |
| 117 | + } |
| 118 | + |
| 119 | + [TestMethod] |
| 120 | + public void PositionAbsoluteAutoWidth_ShrinksToWidestChild_NotSumOfSiblingBorders() |
| 121 | + { |
| 122 | + // Three siblings under an absolutely-positioned, auto-width parent: #text (real content, ~short), |
| 123 | + // #border1 (80px combined border, no content), #border2 (60px combined border, no content) - the |
| 124 | + // correct shrink-to-fit width is #border1's own ~80px (the widest single line), not #border1 + |
| 125 | + // #border2's borders summed together (~140px). |
| 126 | + var html = LayoutHarness.Wrap( |
| 127 | + "<div id='container' style='position:relative; width:400px;'>" |
| 128 | + + "<div id='target' style='position:absolute;'>" |
| 129 | + + "<div id='text'>Hi</div>" |
| 130 | + + "<div id='border1' style='border-left:40px solid black; border-right:40px solid black;'></div>" |
| 131 | + + "<div id='border2' style='border-left:30px solid black; border-right:30px solid black;'></div>" |
| 132 | + + "</div></div>"); |
| 133 | + var (root, _) = LayoutHarness.Layout(html); |
| 134 | + var target = LayoutHarness.FindById(root, "target")!; |
| 135 | + |
| 136 | + var targetWidth = target.ActualRight - target.Location.X; |
| 137 | + |
| 138 | + // Allow a little headroom above 80 for #text's own (much smaller) content contribution. |
| 139 | + Assert.IsTrue(targetWidth is >= 79 and <= 100, |
| 140 | + $"expected shrink-to-fit width near 80px (the widest single sibling), was {targetWidth}"); |
| 141 | + } |
| 142 | + |
| 143 | + [TestMethod] |
| 144 | + public void PositionAbsoluteAutoWidth_MultipleExplicitWidthSiblings_TakesWidestNotSum() |
| 145 | + { |
| 146 | + var html = LayoutHarness.Wrap( |
| 147 | + "<div id='container' style='position:relative; width:400px;'>" |
| 148 | + + "<div id='target' style='position:absolute;'>" |
| 149 | + + "<div id='wide1' style='width:100px; height:10px;'></div>" |
| 150 | + + "<div id='wide2' style='width:90px; height:10px;'></div>" |
| 151 | + + "</div></div>"); |
| 152 | + var (root, _) = LayoutHarness.Layout(html); |
| 153 | + var target = LayoutHarness.FindById(root, "target")!; |
| 154 | + |
| 155 | + var targetWidth = target.ActualRight - target.Location.X; |
| 156 | + |
| 157 | + // The widest single sibling (100px) should win - the buggy summed-across-siblings result would be |
| 158 | + // at least 100+90=190px. |
| 159 | + Assert.IsTrue(targetWidth is >= 99 and <= 105, |
| 160 | + $"expected shrink-to-fit width near 100px (the widest single sibling), was {targetWidth}"); |
| 161 | + } |
| 162 | + |
| 163 | + [TestMethod] |
| 164 | + public void PositionAbsoluteAutoWidth_NonReplacedInlineChildsExplicitWidth_HasNoEffect() |
| 165 | + { |
| 166 | + // Per CSS2.1 10.3.3, `width` has no effect on a non-replaced inline-level box - its explicit width |
| 167 | + // must not be folded into an ancestor's shrink-to-fit computation. |
| 168 | + var html = LayoutHarness.Wrap( |
| 169 | + "<div id='container' style='position:relative; width:400px;'>" |
| 170 | + + "<div id='target' style='position:absolute;'>" |
| 171 | + + "<span id='inlineWide' style='display:inline; width:200px;'></span>" |
| 172 | + + "</div></div>"); |
| 173 | + var (root, _) = LayoutHarness.Layout(html); |
| 174 | + var target = LayoutHarness.FindById(root, "target")!; |
| 175 | + |
| 176 | + var targetWidth = target.ActualRight - target.Location.X; |
| 177 | + |
| 178 | + // The inline child's own "width:200px" must be ignored - target should shrink to ~0 (no real |
| 179 | + // content), not inflate to 200px. |
| 180 | + Assert.IsTrue(targetWidth is >= 0 and <= 20, |
| 181 | + $"expected shrink-to-fit width near 0px (inline width has no effect), was {targetWidth}"); |
| 182 | + } |
| 183 | +} |
0 commit comments