From 377dddd1dc9861b7ad43f3048eac112d576d41c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20D=C3=BCrrenberger?= Date: Mon, 23 Nov 2026 13:49:11 +0100 Subject: [PATCH] Keep tables in place in a container above or left of the origin --- .../Core/Dom/CssLayoutEngineTable.cs | 8 +++--- .../Tables/TablePlacementIntegrationTests.cs | 26 +++++++++++++++++++ 2 files changed, 31 insertions(+), 3 deletions(-) create mode 100644 Source/Test/HtmlRenderer.IntegrationTest/Tables/TablePlacementIntegrationTests.cs diff --git a/Source/HtmlRenderer/Core/Dom/CssLayoutEngineTable.cs b/Source/HtmlRenderer/Core/Dom/CssLayoutEngineTable.cs index 382602c60..01f3c08da 100644 --- a/Source/HtmlRenderer/Core/Dom/CssLayoutEngineTable.cs +++ b/Source/HtmlRenderer/Core/Dom/CssLayoutEngineTable.cs @@ -601,11 +601,13 @@ private void EnforceMinimumSize() /// private void LayoutCells(RGraphics g) { - double startx = Math.Max(_tableBox.ClientLeft + GetHorizontalSpacing(), 0); - double starty = Math.Max(_tableBox.ClientTop + GetVerticalSpacing(), 0); + // The -1 spacing of collapsed borders mustn't move the cells out of the container + var origin = _tableBox.HtmlContainer?.Location ?? RPoint.Empty; + double startx = Math.Max(_tableBox.ClientLeft + GetHorizontalSpacing(), origin.X); + double starty = Math.Max(_tableBox.ClientTop + GetVerticalSpacing(), origin.Y); double cury = starty; double maxRight = startx; - double maxBottom = 0f; + double maxBottom = starty; int currentrow = 0; // HTML's align attribute on the table ends up in text-align diff --git a/Source/Test/HtmlRenderer.IntegrationTest/Tables/TablePlacementIntegrationTests.cs b/Source/Test/HtmlRenderer.IntegrationTest/Tables/TablePlacementIntegrationTests.cs new file mode 100644 index 000000000..bc8248a4b --- /dev/null +++ b/Source/Test/HtmlRenderer.IntegrationTest/Tables/TablePlacementIntegrationTests.cs @@ -0,0 +1,26 @@ +using HtmlRenderer.IntegrationTest.TestSupport; +using TheArtOfDev.HtmlRenderer.Adapters.Entities; + +namespace HtmlRenderer.IntegrationTest.Tables; + +[TestClass] +public sealed class TablePlacementIntegrationTests +{ + private const double Delta = 1.0; + + [TestMethod] + public void Table_InAContainerAboveAndLeftOfTheOrigin_StaysInPlace() + { + var (root, _) = LayoutHarness.Layout( + LayoutHarness.Wrap("
x

after

"), + prepare: r => r.HtmlContainer!.Location = new RPoint(-30, -50)); + var table = LayoutHarness.FindById(root, "t")!; + var cell = LayoutHarness.FindById(root, "c")!; + + Assert.AreEqual(-30, table.Location.X, Delta); + Assert.AreEqual(-50, table.Location.Y, Delta); + Assert.AreEqual(table.Location.X, cell.Location.X, Delta); + Assert.AreEqual(table.Location.Y, cell.Location.Y, Delta); + Assert.AreEqual(table.ActualBottom, LayoutHarness.FindById(root, "p")!.Location.Y, Delta); + } +}