Przeglądaj źródła

LibWeb: Resolve table border conflicts with cells

Build a mapping from coordinates to cells and use it to resolve
border conflicts between adjacent cells.
Andi Gallo 2 lat temu
rodzic
commit
e30662a8a0

+ 64 - 0
Tests/LibWeb/Layout/expected/table/border-conflict-resolution-with-cell.txt

@@ -0,0 +1,64 @@
+Viewport <#document> at (0,0) content-size 800x600 children: not-inline
+  BlockContainer <html> at (0,0) content-size 800x600 [BFC] children: not-inline
+    BlockContainer <body> at (8,8) content-size 784x86.9375 children: not-inline
+      TableWrapper <(anonymous)> at (8,8) content-size 172.671875x86.9375 [BFC] children: not-inline
+        Box <table> at (8,8) content-size 172.671875x86.9375 table-box [TFC] children: not-inline
+          BlockContainer <(anonymous)> (not painted) children: inline
+            TextNode <#text>
+          Box <tbody> at (8,8) content-size 172.671875x86.9375 table-row-group children: not-inline
+            BlockContainer <(anonymous)> (not painted) children: inline
+              TextNode <#text>
+            Box <tr> at (8,8) content-size 172.671875x43.46875 table-row children: not-inline
+              BlockContainer <(anonymous)> (not painted) children: inline
+                TextNode <#text>
+              BlockContainer <td> at (29,21) content-size 14.265625x17.46875 table-cell [BFC] children: inline
+                line 0 width: 14.265625, height: 17.46875, bottom: 17.46875, baseline: 13.53125
+                  frag 0 from TextNode start: 0, length: 1, rect: [29,21 14.265625x17.46875]
+                    "A"
+                TextNode <#text>
+              BlockContainer <(anonymous)> (not painted) children: inline
+                TextNode <#text>
+              BlockContainer <td.td-thick-border> at (89.265625,21) content-size 9.859375x17.46875 table-cell [BFC] children: inline
+                line 0 width: 9.34375, height: 17.46875, bottom: 17.46875, baseline: 13.53125
+                  frag 0 from TextNode start: 0, length: 1, rect: [89.265625,21 9.34375x17.46875]
+                    "B"
+                TextNode <#text>
+              BlockContainer <(anonymous)> (not painted) children: inline
+                TextNode <#text>
+              BlockContainer <td> at (145.125,20) content-size 14.546875x17.46875 table-cell [BFC] children: inline
+                line 0 width: 10.3125, height: 17.46875, bottom: 17.46875, baseline: 13.53125
+                  frag 0 from TextNode start: 0, length: 1, rect: [145.125,20 10.3125x17.46875]
+                    "C"
+                TextNode <#text>
+              BlockContainer <(anonymous)> (not painted) children: inline
+                TextNode <#text>
+            BlockContainer <(anonymous)> (not painted) children: inline
+              TextNode <#text>
+            Box <tr> at (8,51.46875) content-size 172.671875x43.46875 table-row children: not-inline
+              BlockContainer <(anonymous)> (not painted) children: inline
+                TextNode <#text>
+              BlockContainer <td> at (29,64.46875) content-size 16.265625x17.46875 table-cell [BFC] children: inline
+                line 0 width: 11.140625, height: 17.46875, bottom: 17.46875, baseline: 13.53125
+                  frag 0 from TextNode start: 0, length: 1, rect: [29,64.46875 11.140625x17.46875]
+                    "D"
+                TextNode <#text>
+              BlockContainer <(anonymous)> (not painted) children: inline
+                TextNode <#text>
+              BlockContainer <td> at (87.265625,65.46875) content-size 11.859375x17.46875 table-cell [BFC] children: inline
+                line 0 width: 11.859375, height: 17.46875, bottom: 17.46875, baseline: 13.53125
+                  frag 0 from TextNode start: 0, length: 1, rect: [87.265625,65.46875 11.859375x17.46875]
+                    "E"
+                TextNode <#text>
+              BlockContainer <(anonymous)> (not painted) children: inline
+                TextNode <#text>
+              BlockContainer <td.td-thick-border> at (145.125,64.46875) content-size 12.546875x17.46875 table-cell [BFC] children: inline
+                line 0 width: 12.546875, height: 17.46875, bottom: 17.46875, baseline: 13.53125
+                  frag 0 from TextNode start: 0, length: 1, rect: [145.125,64.46875 12.546875x17.46875]
+                    "F"
+                TextNode <#text>
+              BlockContainer <(anonymous)> (not painted) children: inline
+                TextNode <#text>
+            BlockContainer <(anonymous)> (not painted) children: inline
+              TextNode <#text>
+          BlockContainer <(anonymous)> (not painted) children: inline
+            TextNode <#text>

+ 29 - 0
Tests/LibWeb/Layout/input/table/border-conflict-resolution-with-cell.html

@@ -0,0 +1,29 @@
+<style>
+  table {
+    border-collapse: collapse;
+  }
+
+  td {
+    border: 1px solid black;
+    padding: 10px 20px;
+  }
+
+  .td-thick-border {
+    border: 5px solid black;
+  }
+</style>
+
+<table>
+  <tbody>
+    <tr>
+      <td>A</td>
+      <td class="td-thick-border">B</td>
+      <td>C</td>
+    </tr>
+    <tr>
+      <td>D</td>
+      <td>E</td>
+      <td class="td-thick-border">F</td>
+    </tr>
+  </tbody>
+</table>

+ 36 - 1
Userland/Libraries/LibWeb/Layout/TableFormattingContext.cpp

@@ -97,6 +97,7 @@ void TableFormattingContext::calculate_row_column_grid(Box const& box)
 
     size_t x_width = 0, y_height = 0;
     size_t x_current = 0, y_current = 0;
+    size_t max_cell_x = 0, max_cell_y = 0;
 
     // Implements https://html.spec.whatwg.org/multipage/tables.html#algorithm-for-processing-rows
     auto process_row = [&](auto& row) {
@@ -131,6 +132,8 @@ void TableFormattingContext::calculate_row_column_grid(Box const& box)
                     for (size_t x = x_current; x < x_current + colspan; x++)
                         grid.set(GridPosition { x, y }, true);
                 m_cells.append(Cell { *box, x_current, y_current, colspan, rowspan });
+                max_cell_x = max(x_current, max_cell_x);
+                max_cell_y = max(y_current, max_cell_y);
 
                 x_current += colspan;
             }
@@ -159,6 +162,14 @@ void TableFormattingContext::calculate_row_column_grid(Box const& box)
         cell.row_span = min(cell.row_span, m_rows.size() - cell.row_index);
         cell.column_span = min(cell.column_span, m_columns.size() - cell.column_index);
     }
+
+    m_cells_by_coordinate.resize(max_cell_y + 1);
+    for (auto& position_to_cell_row : m_cells_by_coordinate) {
+        position_to_cell_row.resize(max_cell_x + 1);
+    }
+    for (auto const& cell : m_cells) {
+        m_cells_by_coordinate[cell.row_index][cell.column_index] = cell;
+    }
 }
 
 void TableFormattingContext::compute_cell_measures(AvailableSpace const& available_space)
@@ -1040,8 +1051,32 @@ Vector<TableFormattingContext::ConflictingEdge> TableFormattingContext::BorderCo
     Cell const& cell, TableFormattingContext::ConflictingSide edge) const
 {
     // FIXME: Conflicting elements can be cells, rows, row groups, columns, column groups, and the table itself,
-    //        but we only consider 'col' elements in a 'colgroup' and the table itself for now.
+    //        but we only consider cells, 'col' elements in a 'colgroup' and the table itself for now.
     Vector<ConflictingEdge> result = {};
+    if (cell.column_index >= cell.column_span && edge == ConflictingSide::Left) {
+        auto maybe_cell_to_left = m_context->m_cells_by_coordinate[cell.row_index][cell.column_index - cell.column_span];
+        if (maybe_cell_to_left.has_value()) {
+            result.append({ maybe_cell_to_left->box, ConflictingSide::Right });
+        }
+    }
+    if (cell.column_index + cell.column_span < m_context->m_cells_by_coordinate[cell.row_index].size() && edge == ConflictingSide::Right) {
+        auto maybe_cell_to_right = m_context->m_cells_by_coordinate[cell.row_index][cell.column_index + cell.column_span];
+        if (maybe_cell_to_right.has_value()) {
+            result.append({ maybe_cell_to_right->box, ConflictingSide::Left });
+        }
+    }
+    if (cell.row_index >= cell.row_span && edge == ConflictingSide::Top) {
+        auto maybe_cell_above = m_context->m_cells_by_coordinate[cell.row_index - cell.row_span][cell.column_index];
+        if (maybe_cell_above.has_value()) {
+            result.append({ maybe_cell_above->box, ConflictingSide::Bottom });
+        }
+    }
+    if (cell.row_index + cell.row_span < m_context->m_cells_by_coordinate.size() && edge == ConflictingSide::Bottom) {
+        auto maybe_cell_below = m_context->m_cells_by_coordinate[cell.row_index + cell.row_span][cell.column_index];
+        if (maybe_cell_below.has_value()) {
+            result.append({ maybe_cell_below->box, ConflictingSide::Top });
+        }
+    }
     if (m_col_elements_by_index[cell.column_index] && edge == ConflictingSide::Left) {
         result.append({ m_col_elements_by_index[cell.column_index], ConflictingSide::Left });
     }

+ 2 - 1
Userland/Libraries/LibWeb/Layout/TableFormattingContext.h

@@ -134,7 +134,7 @@ private:
         ConflictingSide side;
     };
 
-    const CSS::BorderData& border_data_conflicting_edge(ConflictingEdge const& conflicting_edge);
+    static const CSS::BorderData& border_data_conflicting_edge(ConflictingEdge const& conflicting_edge);
 
     class BorderConflictFinder {
     public:
@@ -149,6 +149,7 @@ private:
     };
 
     Vector<Cell> m_cells;
+    Vector<Vector<Optional<Cell const&>>> m_cells_by_coordinate;
     Vector<Column> m_columns;
     Vector<Row> m_rows;
 };