浏览代码

LibWeb: Fix division by zero in table columns width distribution

If total max columns width (grid_max) is zero then available width
should be divided equally between columns. Previously there was
division by zero: `column.max_width / grid_max`.
Aliaksandr Kalenik 2 年之前
父节点
当前提交
9fd51a59ff

+ 7 - 0
Tests/LibWeb/Layout/expected/table/zero-columns-gridmax.txt

@@ -0,0 +1,7 @@
+Viewport <#document> at (0,0) content-size 800x600 children: not-inline
+  BlockContainer <html> at (0,0) content-size 800x16 children: not-inline
+    BlockContainer <body> at (8,8) content-size 784x0 children: not-inline
+      TableWrapper <(anonymous)> at (8,8) content-size 200x0 children: not-inline
+        TableBox <div.table> at (8,8) content-size 200x0 children: not-inline
+          TableRowBox <div.row> at (8,8) content-size 200x0 children: not-inline
+            TableCellBox <div.cell> at (8,8) content-size 200x0 children: not-inline

+ 15 - 0
Tests/LibWeb/Layout/input/table/zero-columns-gridmax.html

@@ -0,0 +1,15 @@
+<style>
+.table {
+    display: table;
+    width: 200px;
+}
+
+.row {
+    display: table-row;
+}
+
+.cell {
+    display: table-cell;
+}
+</style>
+<div class="table"><div class="row"><div class="cell"></div></div></div>

+ 11 - 2
Userland/Libraries/LibWeb/Layout/TableFormattingContext.cpp

@@ -358,8 +358,17 @@ void TableFormattingContext::distribute_width_to_columns()
         }
 
         auto width_to_distribute = available_width - columns_total_used_width();
-        for (auto& column : m_columns) {
-            column.used_width += width_to_distribute * column.max_width / grid_max;
+        if (grid_max == 0) {
+            // If total max width of columns is zero then divide distributable width equally among them
+            auto column_width = width_to_distribute / m_columns.size();
+            for (auto& column : m_columns) {
+                column.used_width = column_width;
+            }
+        } else {
+            // Distribute width to columns proportionally to their max width
+            for (auto& column : m_columns) {
+                column.used_width += width_to_distribute * column.max_width / grid_max;
+            }
         }
     }
 }