[LFC][TFC] Use <col> to adjust the preferred column width.
https://bugs.webkit.org/show_bug.cgi?id=202997
<rdar://problem/56300345>

Reviewed by Antti Koivisto.

The <col> elment can set the preferred width on the table column. Let's take these values into account while computing the preferred width for columns.

* layout/tableformatting/TableFormattingContext.cpp:
(WebCore::Layout::TableFormattingContext::computePreferredWidthForColumns):
* layout/tableformatting/TableFormattingContext.h:
* layout/tableformatting/TableFormattingContextGeometry.cpp:
(WebCore::Layout::TableFormattingContext::Geometry::computedColumnWidth const):
* layout/tableformatting/TableGrid.h:
(WebCore::Layout::TableGrid::Column::columnBox const):


git-svn-id: http://svn.webkit.org/repository/webkit/trunk@251151 268f45cc-cd09-0410-ab3c-d52691b4dbfc
diff --git a/Source/WebCore/ChangeLog b/Source/WebCore/ChangeLog
index 35c3595..d20b82c 100644
--- a/Source/WebCore/ChangeLog
+++ b/Source/WebCore/ChangeLog
@@ -1,5 +1,23 @@
 2019-10-15  Zalan Bujtas  <zalan@apple.com>
 
+        [LFC][TFC] Use <col> to adjust the preferred column width.
+        https://bugs.webkit.org/show_bug.cgi?id=202997
+        <rdar://problem/56300345>
+
+        Reviewed by Antti Koivisto.
+
+        The <col> elment can set the preferred width on the table column. Let's take these values into account while computing the preferred width for columns.
+
+        * layout/tableformatting/TableFormattingContext.cpp:
+        (WebCore::Layout::TableFormattingContext::computePreferredWidthForColumns):
+        * layout/tableformatting/TableFormattingContext.h:
+        * layout/tableformatting/TableFormattingContextGeometry.cpp:
+        (WebCore::Layout::TableFormattingContext::Geometry::computedColumnWidth const):
+        * layout/tableformatting/TableGrid.h:
+        (WebCore::Layout::TableGrid::Column::columnBox const):
+
+2019-10-15  Zalan Bujtas  <zalan@apple.com>
+
         [LFC][TFC] Add support for colgroup/col
         https://bugs.webkit.org/show_bug.cgi?id=202991
         <rdar://problem/56294715>
diff --git a/Source/WebCore/layout/tableformatting/TableFormattingContext.cpp b/Source/WebCore/layout/tableformatting/TableFormattingContext.cpp
index dafdb97..bf1466f0 100644
--- a/Source/WebCore/layout/tableformatting/TableFormattingContext.cpp
+++ b/Source/WebCore/layout/tableformatting/TableFormattingContext.cpp
@@ -249,9 +249,15 @@
             columnIntrinsicWidths.minimum = std::max(slot->widthConstraints.minimum, columnIntrinsicWidths.minimum);
             columnIntrinsicWidths.maximum = std::max(slot->widthConstraints.maximum, columnIntrinsicWidths.maximum);
         }
+        // Now that we have the content driven min/max widths, check if <col> sets a preferred width on this column.
+        if (auto* columnBox = columns[columnIndex].columnBox()) {
+            if (auto columnPreferredWidth = geometry().computedColumnWidth(*columnBox)) {
+                // Let's stay at least as wide as the preferred width.
+                columnIntrinsicWidths.minimum = std::max(columnIntrinsicWidths.minimum, *columnPreferredWidth);
+            }
+        }
         columns[columnIndex].setWidthConstraints(columnIntrinsicWidths);
     }
-    // FIXME: Take column group elements into account.
 }
 
 LayoutUnit TableFormattingContext::computedTableWidth()
diff --git a/Source/WebCore/layout/tableformatting/TableFormattingContext.h b/Source/WebCore/layout/tableformatting/TableFormattingContext.h
index 4a6e603..747d207 100644
--- a/Source/WebCore/layout/tableformatting/TableFormattingContext.h
+++ b/Source/WebCore/layout/tableformatting/TableFormattingContext.h
@@ -47,6 +47,7 @@
     class Geometry : public FormattingContext::Geometry {
     public:
         ContentHeightAndMargin tableCellHeightAndMargin(const Box&) const;
+        Optional<LayoutUnit> computedColumnWidth(const Box& columnBox) const;
 
     private:
         friend class TableFormattingContext;
diff --git a/Source/WebCore/layout/tableformatting/TableFormattingContextGeometry.cpp b/Source/WebCore/layout/tableformatting/TableFormattingContextGeometry.cpp
index 421195e..18a2169 100644
--- a/Source/WebCore/layout/tableformatting/TableFormattingContextGeometry.cpp
+++ b/Source/WebCore/layout/tableformatting/TableFormattingContextGeometry.cpp
@@ -45,6 +45,15 @@
     return ContentHeightAndMargin { *height, { } };
 }
 
+Optional<LayoutUnit> TableFormattingContext::Geometry::computedColumnWidth(const Box& columnBox) const
+{
+    // Check both style and <col>'s width attribute.
+    // FIXME: Figure out what to do with calculated values, like <col style="width: 10%">.
+    if (auto computedWidthValue = computedContentWidth(columnBox, { }))
+        return computedWidthValue;
+    return columnBox.columnWidth();
+}
+
 }
 }
 
diff --git a/Source/WebCore/layout/tableformatting/TableGrid.h b/Source/WebCore/layout/tableformatting/TableGrid.h
index 70694a4..66ebad2 100644
--- a/Source/WebCore/layout/tableformatting/TableGrid.h
+++ b/Source/WebCore/layout/tableformatting/TableGrid.h
@@ -80,6 +80,8 @@
         void setLogicalWidth(LayoutUnit);
         LayoutUnit logicalWidth() const;
 
+        const Box* columnBox() const { return m_columnBox.get(); }
+
     private:
         friend class ColumnsContext;
         Column(const Box* columnBox);