From 9814efd3e3550339d7f7b50d8be1f7eb7c78c407 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20D=C3=BCrrenberger?= Date: Mon, 23 Nov 2026 13:52:31 +0100 Subject: [PATCH] Count the columns of a table by the colspans of its cells --- .../Core/Dom/CssLayoutEngineTable.cs | 4 ++- .../Tables/ColspanIntegrationTests.cs | 25 +++++++++++++++++++ .../HtmlRenderer.Test/Html/TablesTests.cs | 2 -- Spec/css-tables-3.yml | 3 +-- Spec/html.yml | 2 -- 5 files changed, 29 insertions(+), 7 deletions(-) create mode 100644 Source/Test/HtmlRenderer.IntegrationTest/Tables/ColspanIntegrationTests.cs diff --git a/Source/HtmlRenderer/Core/Dom/CssLayoutEngineTable.cs b/Source/HtmlRenderer/Core/Dom/CssLayoutEngineTable.cs index 382602c60..cc683aec7 100644 --- a/Source/HtmlRenderer/Core/Dom/CssLayoutEngineTable.cs +++ b/Source/HtmlRenderer/Core/Dom/CssLayoutEngineTable.cs @@ -12,6 +12,7 @@ using System; using System.Collections.Generic; +using System.Linq; using TheArtOfDev.HtmlRenderer.Adapters; using TheArtOfDev.HtmlRenderer.Adapters.Entities; using TheArtOfDev.HtmlRenderer.Core.Entities; @@ -293,8 +294,9 @@ private double CalculateCountAndWidth() } else { + // A cell takes as many columns as its colspan, HTML 4.9.12.1 foreach (CssBox b in _allRows) - _columnCount = Math.Max(_columnCount, b.Boxes.Count); + _columnCount = Math.Max(_columnCount, b.Boxes.Sum(cell => Math.Max(GetColSpan(cell), 1))); } //Initialize column widths array with NaNs diff --git a/Source/Test/HtmlRenderer.IntegrationTest/Tables/ColspanIntegrationTests.cs b/Source/Test/HtmlRenderer.IntegrationTest/Tables/ColspanIntegrationTests.cs new file mode 100644 index 000000000..c2bbafb33 --- /dev/null +++ b/Source/Test/HtmlRenderer.IntegrationTest/Tables/ColspanIntegrationTests.cs @@ -0,0 +1,25 @@ +using HtmlRenderer.IntegrationTest.TestSupport; + +namespace HtmlRenderer.IntegrationTest.Tables; + +[TestClass] +public sealed class ColspanIntegrationTests +{ + private const double Delta = 1.0; + + [TestMethod] + [Spec("html", "tables.html#forming-a-table")] + [Spec("css-tables-3", "#dimensioning-the-row-column-grid--step1")] + public void RowsWithDifferentCellCounts_ShareTheColumnsTheirColspansAddUpTo() + { + var (root, _) = LayoutHarness.Layout(LayoutHarness.Wrap( + "" + + "
Test8 570Test
1 4491 187
")); + var a = LayoutHarness.FindById(root, "a")!; + var b = LayoutHarness.FindById(root, "b")!; + var c = LayoutHarness.FindById(root, "c")!; + + Assert.IsGreaterThan(a.ActualBottom, c.Location.Y); + Assert.AreEqual(b.Location.X, c.Location.X, Delta); + } +} diff --git a/Source/Test/HtmlRenderer.Test/Html/TablesTests.cs b/Source/Test/HtmlRenderer.Test/Html/TablesTests.cs index ed1b37b55..ad3cce4e5 100644 --- a/Source/Test/HtmlRenderer.Test/Html/TablesTests.cs +++ b/Source/Test/HtmlRenderer.Test/Html/TablesTests.cs @@ -133,7 +133,6 @@ public void Colspan_AboveAThousand_IsClampedToAThousand() } [TestMethod] - [Ignore("Gap, see Spec/html.yml")] [Spec("html", "tables.html#forming-a-table")] public void Colspan_AddsColumns() { @@ -143,7 +142,6 @@ public void Colspan_AddsColumns() } [TestMethod] - [Ignore("Gap, see Spec/html.yml")] [Spec("html", "tables.html#forming-a-table")] public void CellSpanningPastTheOtherRows_IsLaidOut() { diff --git a/Spec/css-tables-3.yml b/Spec/css-tables-3.yml index 6af2255db..a577a1209 100644 --- a/Spec/css-tables-3.yml +++ b/Spec/css-tables-3.yml @@ -55,12 +55,11 @@ sections: status: partial gaps: - colspan and rowspan are read from any element, not only from td and th - - colspan doesn't add columns, see HTML 4.9.12.1 '#dimensioning-the-row-column-grid--step2': title: 3.3.2 Track merging status: unsupported gaps: - - Columns that only spanning cells cover aren't merged, a colspan past the column count breaks the table layout, see HTML 4.9.12.1 + - Columns that only spanning cells cover aren't merged '#missing-cells-fixup': title: 3.4 Missing cells fixup status: supported diff --git a/Spec/html.yml b/Spec/html.yml index 8fc571108..931ac775a 100644 --- a/Spec/html.yml +++ b/Spec/html.yml @@ -892,8 +892,6 @@ sections: title: 4.9.12.1 Forming a table status: partial gaps: - - The column count is the highest number of cells in a row, colspan doesn't add columns, so a cell past it gets no width - - A cell spanning past that column count breaks the layout of the whole table - Cells past the columns declared by col and colgroup aren't laid out - A colspan of 0 or below breaks the layout of the whole table instead of counting as 1 - colspan isn't clamped to 1000