Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.1

0.6.1 / src / web / layout / table_formatting_context_4.lucb

#==============================================================================================
#
#   table_formatting_context_4 - Layout::TableFormattingContext: border conflict resolution
#                                and the grid's cells by coordinate
#
#   DESCRIPTION:
#       Port of LibWeb/Layout/TableFormattingContext.cpp (lines 1276-1702) and the
#       BorderConflictFinder of TableFormattingContext.h: the border conflict resolution of the
#       collapsing border model (https://www.w3.org/TR/CSS22/tables.html#border-conflict-resolution),
#       which gives each cell the winning border of every side among the cells, rows, row
#       groups, columns, column groups and the table meeting there; the content height of a
#       cell's rows; and the cells by coordinate and the columns' originating cells.
#
#       Copyright (c) 2020, Andreas Kling <andreas@ladybird.org>
#       SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================

# mark: Border conflict resolution ==============================================================

## The `static HashMap<CSS::LineStyle, unsigned> const line_style_score` of TableFormattingContext::border_is_less_specific:
## the order in which border styles are preferred ('double', 'solid', 'dashed', 'dotted', 'ridge', 'outset', 'groove' and
## the lowest: 'inset'). `*line_style_score.get(style)` VERIFYs that the style is in the map.
func layout_table_formatting_context_line_style_score(line_style: css_data.LineStyle) -> u32:
    match line_style:
        .inset: return 0
        .groove: return 1
        .outset: return 2
        .ridge: return 3
        .dotted: return 4
        .dashed: return 5
        .solid: return 6
        .double: return 7
        _: trap("VERIFY: line_style_score has the line style")

## Ported from Libraries/LibWeb/Layout/TableFormattingContext.cpp:1276 Web::Layout::TableFormattingContext::border_is_less_specific.
pub func layout_table_formatting_context_border_is_less_specific(a: display_list.BorderData, b: display_list.BorderData) -> bool:
    # Implements criteria for steps 1, 2 and 3 of border conflict resolution algorithm, as described in
    # https://www.w3.org/TR/CSS22/tables.html#border-conflict-resolution.

    # 1. Borders with the 'border-style' of 'hidden' take precedence over all other conflicting borders. Any border with this
    #    value suppresses all borders at this location.
    if a.line_style == css_data.LineStyle.hidden:
        return false

    if b.line_style == css_data.LineStyle.hidden:
        return true

    # 2. Borders with a style of 'none' have the lowest priority. Only if the border properties of all the elements meeting
    #    at this edge are 'none' will the border be omitted (but note that 'none' is the default value for the border style.)
    if a.line_style == css_data.LineStyle.none_:
        return true
    if b.line_style == css_data.LineStyle.none_:
        return false
    # 3. If none of the styles are 'hidden' and at least one of them is not 'none', then narrow borders are discarded in favor
    #    of wider ones. If several have the same 'border-width' then styles are preferred in this order: 'double', 'solid',
    #    'dashed', 'dotted', 'ridge', 'outset', 'groove', and the lowest: 'inset'.
    if a.width.gt(b.width):
        return false
    elif a.width.lt(b.width):
        return true
    if layout_table_formatting_context_line_style_score(a.line_style) > layout_table_formatting_context_line_style_score(b.line_style):
        return false
    elif layout_table_formatting_context_line_style_score(a.line_style) < layout_table_formatting_context_line_style_score(b.line_style):
        return true
    return false

## Ported from Libraries/LibWeb/Layout/TableFormattingContext.cpp:1325 Web::Layout::TableFormattingContext::border_data_conflicting_edge.
func layout_table_formatting_context_border_data_conflicting_edge(conflicting_edge: const LayoutTableFormattingContextConflictingEdge*) -> display_list.BorderData:
    let element = conflicting_edge.element else trap("a conflicting edge without an element")
    let style = &layout_node_computed_values(element).computed_values
    match conflicting_edge.side:
        .top:
            return css_computed_values_border_top(style)
        .bottom:
            return css_computed_values_border_bottom(style)
        .left:
            return css_computed_values_border_left(style)
        .right:
            return css_computed_values_border_right(style)
        _:
            trap("VERIFY_NOT_REACHED")

## Ported from Libraries/LibWeb/Layout/TableFormattingContext.cpp:1347 Web::Layout::TableFormattingContext::border_data_with_element_kind_from_conflicting_edge.
func layout_table_formatting_context_border_data_with_element_kind_from_conflicting_edge(conflicting_edge: const LayoutTableFormattingContextConflictingEdge*) -> PaintPaintableBoxBorderDataWithElementKind:
    let border_data = layout_table_formatting_context_border_data_conflicting_edge(conflicting_edge)
    return PaintPaintableBoxBorderDataWithElementKind(border_data = border_data, element_kind = conflicting_edge.element_kind)

## Ported from Libraries/LibWeb/Layout/TableFormattingContext.cpp:1353 Web::Layout::TableFormattingContext::winning_conflicting_edge.
func layout_table_formatting_context_winning_conflicting_edge(a: const LayoutTableFormattingContextConflictingEdge*, b: const LayoutTableFormattingContextConflictingEdge*) -> LayoutTableFormattingContextConflictingEdge:
    let a_border_data = layout_table_formatting_context_border_data_conflicting_edge(a)
    let b_border_data = layout_table_formatting_context_border_data_conflicting_edge(b)
    # First check if step 4 of border conflict resolution applies, as described in https://www.w3.org/TR/CSS22/tables.html#border-conflict-resolution.
    if a_border_data.line_style == b_border_data.line_style and a_border_data.width == b_border_data.width:
        # 4. If border styles differ only in color, then a style set on a cell wins over one on a row, which wins over a
        #    row group, column, column group and, lastly, table. When two elements of the same type conflict, then the one
        #    further to the left (if the table's 'direction' is 'ltr'; right, if it is 'rtl') and further to the top wins.
        if (i32)a.element_kind < (i32)b.element_kind:
            return *a
        elif (i32)a.element_kind > (i32)b.element_kind:
            return *b
        # Here the element kind is the same, thus the coordinates are either both set or not set.
        assert((a.column != none) == (b.column != none))
        assert((a.row != none) == (b.row != none))
        if let a_column = a.column:
            let b_column = b.column else trap("VERIFY: both columns are set")
            if a_column < b_column:
                return *a
            elif a_column > b_column:
                return *b
        if let a_row = a.row:
            let b_row = b.row else trap("VERIFY: both rows are set")
            if a_row < b_row:
                return *a
            elif a_row > b_row:
                return *b
        return *a
    # Apply steps 1, 2 and 3 of the border conflict resolution algorithm.
    return *b if layout_table_formatting_context_border_is_less_specific(a_border_data, b_border_data) else *a

## The winning edge of one side of a cell in TableFormattingContext::border_conflict_resolution: the cell's own edge
## against each conflicting edge the finder collects.
func layout_table_formatting_context_winning_edge_of_cell_side(finder: const LayoutTableFormattingContextBorderConflictFinder*, cell: const LayoutTableGridCell*, side: LayoutTableFormattingContextConflictingSide) -> LayoutTableFormattingContextConflictingEdge:
    var winning_edge = LayoutTableFormattingContextConflictingEdge(
        element = cell.box.layout_node(),
        element_kind = PaintPaintableBoxConflictingElementKind.cell,
        side = side,
        row = cell.row_index,
        column = cell.column_index,
    )
    let conflicting_edges = layout_table_formatting_context_border_conflict_finder_conflicting_edges(finder, cell, side)
    for conflicting_edge in &ak.vector_span_const(&conflicting_edges):
        winning_edge = layout_table_formatting_context_winning_conflicting_edge(&winning_edge, conflicting_edge)
    return winning_edge

## Ported from Libraries/LibWeb/Layout/TableFormattingContext.cpp:1390 Web::Layout::TableFormattingContext::border_conflict_resolution.
func layout_table_formatting_context_border_conflict_resolution(this: LayoutTableFormattingContext*):
    # Implements border conflict resolution, as described in https://www.w3.org/TR/CSS22/tables.html#border-conflict-resolution.
    var finder: LayoutTableFormattingContextBorderConflictFinder = ---
    layout_table_formatting_context_border_conflict_finder_construct(&finder, this)
    let border_collapse = css_computed_values_border_collapse(layout_box_computed(layout_table_formatting_context_table_box(this)))
    for cell in &ak.vector_span(&this.m_cells):
        let cell_state = layout_state_get_mutable(this.formatting_context.m_state, cell.box.layout_node_with_style())
        layout_state_used_values_set_table_cell_coordinates(cell_state, PaintPaintableBoxTableCellCoordinates(
            row_index = cell.row_index,
            column_index = cell.column_index,
            row_span = cell.row_span,
            column_span = cell.column_span,
        ))
        if border_collapse == css_data.BorderCollapse.separate:
            continue
        var override_borders_data = PaintPaintableBoxBordersDataWithElementKind()
        # Each side starts from the cell's own edge (`ConflictingEdge winning_edge_left { .element = cell.box, ... }`) and
        # keeps the winner against each of the finder's conflicting edges.
        let winning_edge_left = layout_table_formatting_context_winning_edge_of_cell_side(&finder, cell, LayoutTableFormattingContextConflictingSide.left)
        override_borders_data.left = layout_table_formatting_context_border_data_with_element_kind_from_conflicting_edge(&winning_edge_left)
        cell_state.border_left = override_borders_data.left.border_data.width
        let winning_edge_right = layout_table_formatting_context_winning_edge_of_cell_side(&finder, cell, LayoutTableFormattingContextConflictingSide.right)
        override_borders_data.right = layout_table_formatting_context_border_data_with_element_kind_from_conflicting_edge(&winning_edge_right)
        cell_state.border_right = override_borders_data.right.border_data.width
        let winning_edge_top = layout_table_formatting_context_winning_edge_of_cell_side(&finder, cell, LayoutTableFormattingContextConflictingSide.top)
        override_borders_data.top = layout_table_formatting_context_border_data_with_element_kind_from_conflicting_edge(&winning_edge_top)
        cell_state.border_top = override_borders_data.top.border_data.width
        let winning_edge_bottom = layout_table_formatting_context_winning_edge_of_cell_side(&finder, cell, LayoutTableFormattingContextConflictingSide.bottom)
        override_borders_data.bottom = layout_table_formatting_context_border_data_with_element_kind_from_conflicting_edge(&winning_edge_bottom)
        cell_state.border_bottom = override_borders_data.bottom.border_data.width
        layout_state_used_values_set_override_borders_data(cell_state, override_borders_data)

## Ported from Libraries/LibWeb/Layout/TableFormattingContext.cpp:1458 Web::Layout::TableFormattingContext::compute_row_content_height.
func layout_table_formatting_context_compute_row_content_height(this: LayoutTableFormattingContext*, cell: const LayoutTableGridCell*) -> gfx.CssPixels:
    let state = this.formatting_context.m_state
    let row_state = layout_state_get(state, ak.vector_at(&this.m_rows, cell.row_index).box.layout_node_with_style())
    if cell.row_span == 1:
        return layout_state_used_values_content_height(row_state)
    # The height of a cell is the sum of all spanned rows, as described in
    # https://www.w3.org/TR/css-tables-3/#bounding-box-assignment

    # When the row span is greater than 1, the borders of inner rows within the span have to be
    # included in the content height of the spanning cell. First top and final bottom borders are
    # excluded to be consistent with the handling of row span 1 case above, which uses the content
    # height (no top and bottom borders) of the row.
    var span_height = layout_zero_px()
    for i in 0..<cell.row_span:
        let spanned_row_state = layout_state_get(state, ak.vector_at(&this.m_rows, cell.row_index + i).box.layout_node_with_style())
        if i == 0:
            span_height = span_height.add(layout_state_used_values_content_height(spanned_row_state).add(layout_state_used_values_border_box_bottom(spanned_row_state)))
        elif i == cell.row_span - 1:
            span_height = span_height.add(layout_state_used_values_border_box_top(spanned_row_state).add(layout_state_used_values_content_height(spanned_row_state)))
        else:
            span_height = span_height.add(layout_state_used_values_border_box_height(spanned_row_state))

    # Compute cell height as specified by https://www.w3.org/TR/css-tables-3/#bounding-box-assignment:
    # width/height is the sum of:
    # - the widths/heights of all spanned visible columns/rows
    # - the horizontal/vertical border-spacing times the amount of spanned visible columns/rows minus one
    # FIXME: Account for visibility.
    span_height = span_height.add(layout_table_formatting_context_mul_count(layout_table_formatting_context_border_spacing_vertical(this), cell.row_span - 1))
    return span_height

# mark: BorderConflictFinder ====================================================================

## Ported from Libraries/LibWeb/Layout/TableFormattingContext.cpp:1492 Web::Layout::TableFormattingContext::BorderConflictFinder::BorderConflictFinder.
func layout_table_formatting_context_border_conflict_finder_construct(this: LayoutTableFormattingContextBorderConflictFinder*, context: LayoutTableFormattingContext*):
    this.m_col_elements_by_index = ak.Vector[LayoutNode*?]()
    this.m_row_group_elements_by_index = ak.Vector[LayoutTableFormattingContextBorderConflictFinderRowGroupInfo?]()
    this.m_context = context
    layout_table_formatting_context_border_conflict_finder_collect_conflicting_col_elements(this)
    layout_table_formatting_context_border_conflict_finder_collect_conflicting_row_group_elements(this)

## Ported from Libraries/LibWeb/Layout/TableFormattingContext.h:174 Web::Layout::TableFormattingContext::BorderConflictFinder::get_col_element.
func layout_table_formatting_context_border_conflict_finder_get_col_element(this: const LayoutTableFormattingContextBorderConflictFinder*, index: usize) -> LayoutNode*?:
    if index >= ak.vector_size(&this.m_col_elements_by_index):
        return none
    return *ak.vector_at_const(&this.m_col_elements_by_index, index)

## Ported from Libraries/LibWeb/Layout/TableFormattingContext.cpp:1499 Web::Layout::TableFormattingContext::BorderConflictFinder::collect_conflicting_col_elements.
func layout_table_formatting_context_border_conflict_finder_collect_conflicting_col_elements(this: LayoutTableFormattingContextBorderConflictFinder*):
    ak.vector_resize(&this.m_col_elements_by_index, ak.vector_size(&this.m_context.m_columns))
    for child in layout_node_children(layout_table_formatting_context_table_box(this.m_context).layout_node()):
        let child_display = layout_node_display(child)
        if not css_display_is_table_column_group(&child_display):
            continue
        var column_index: usize = 0
        for child_of_column_group in layout_node_children(child):
            let child_of_column_group_display = layout_node_display(child_of_column_group)
            assert(css_display_is_table_column(&child_of_column_group_display))
            let span = layout_table_formatting_context_column_span_attribute(child_of_column_group)
            ak.vector_resize(&this.m_col_elements_by_index, column_index + span)
            for i in column_index..<column_index + span:
                *ak.vector_at(&this.m_col_elements_by_index, i) = child_of_column_group
            column_index += span

## Ported from Libraries/LibWeb/Layout/TableFormattingContext.cpp:1520 Web::Layout::TableFormattingContext::BorderConflictFinder::collect_conflicting_row_group_elements.
func layout_table_formatting_context_border_conflict_finder_collect_conflicting_row_group_elements(this: LayoutTableFormattingContextBorderConflictFinder*):
    ak.vector_resize(&this.m_row_group_elements_by_index, ak.vector_size(&this.m_context.m_rows))
    var current_row_index: usize = 0
    for row_group_box in layout_table_grid_child_boxes_matching(layout_table_formatting_context_table_box(this.m_context), layout_table_grid_is_table_row_group):
        let start_row_index = current_row_index
        var row_count: usize = 0
        for row_box in layout_table_grid_child_boxes_matching(row_group_box, layout_table_grid_is_table_row):
            _ = row_box
            row_count += 1
        for row_box in layout_table_grid_child_boxes_matching(row_group_box, layout_table_grid_is_table_row):
            _ = row_box
            *ak.vector_at(&this.m_row_group_elements_by_index, current_row_index) = LayoutTableFormattingContextBorderConflictFinderRowGroupInfo(
                row_group = row_group_box.layout_node(), start_index = start_row_index, row_count = row_count)
            current_row_index += 1

## Ported from Libraries/LibWeb/Layout/TableFormattingContext.cpp:1540 Web::Layout::TableFormattingContext::BorderConflictFinder::collect_cell_conflicting_edges.
func layout_table_formatting_context_border_conflict_finder_collect_cell_conflicting_edges(this: const LayoutTableFormattingContextBorderConflictFinder*, result: ak.Vector[LayoutTableFormattingContextConflictingEdge]*, cell: const LayoutTableGridCell*, edge: LayoutTableFormattingContextConflictingSide):
    let cells_by_coordinate = &this.m_context.m_cells_by_coordinate
    # Right edge of the cell to the left.
    if cell.column_index >= cell.column_span and edge == LayoutTableFormattingContextConflictingSide.left:
        let left_cell_column_index = cell.column_index - cell.column_span
        let maybe_cell_to_left = *ak.vector_at(ak.vector_at(cells_by_coordinate, cell.row_index), left_cell_column_index)
        if let cell_to_left = maybe_cell_to_left:
            ak.vector_append_t(result, LayoutTableFormattingContextConflictingEdge(element = cell_to_left.box.layout_node(), element_kind = PaintPaintableBoxConflictingElementKind.cell, side = LayoutTableFormattingContextConflictingSide.right, row = cell.row_index, column = left_cell_column_index))
    # Left edge of the cell to the right.
    if cell.column_index + cell.column_span < ak.vector_size(ak.vector_at(cells_by_coordinate, cell.row_index)) and edge == LayoutTableFormattingContextConflictingSide.right:
        let right_cell_column_index = cell.column_index + cell.column_span
        let maybe_cell_to_right = *ak.vector_at(ak.vector_at(cells_by_coordinate, cell.row_index), right_cell_column_index)
        if let cell_to_right = maybe_cell_to_right:
            ak.vector_append_t(result, LayoutTableFormattingContextConflictingEdge(element = cell_to_right.box.layout_node(), element_kind = PaintPaintableBoxConflictingElementKind.cell, side = LayoutTableFormattingContextConflictingSide.left, row = cell.row_index, column = right_cell_column_index))
    # Bottom edge of the cell above.
    if cell.row_index >= cell.row_span and edge == LayoutTableFormattingContextConflictingSide.top:
        let above_cell_row_index = cell.row_index - cell.row_span
        let maybe_cell_above = *ak.vector_at(ak.vector_at(cells_by_coordinate, above_cell_row_index), cell.column_index)
        if let cell_above = maybe_cell_above:
            ak.vector_append_t(result, LayoutTableFormattingContextConflictingEdge(element = cell_above.box.layout_node(), element_kind = PaintPaintableBoxConflictingElementKind.cell, side = LayoutTableFormattingContextConflictingSide.bottom, row = above_cell_row_index, column = cell.column_index))
    # Top edge of the cell below.
    if cell.row_index + cell.row_span < ak.vector_size(cells_by_coordinate) and edge == LayoutTableFormattingContextConflictingSide.bottom:
        let below_cell_row_index = cell.row_index + cell.row_span
        let maybe_cell_below = *ak.vector_at(ak.vector_at(cells_by_coordinate, below_cell_row_index), cell.column_index)
        if let cell_below = maybe_cell_below:
            ak.vector_append_t(result, LayoutTableFormattingContextConflictingEdge(element = cell_below.box.layout_node(), element_kind = PaintPaintableBoxConflictingElementKind.cell, side = LayoutTableFormattingContextConflictingSide.top, row = below_cell_row_index, column = cell.column_index))

## Ported from Libraries/LibWeb/Layout/TableFormattingContext.cpp:1576 Web::Layout::TableFormattingContext::BorderConflictFinder::collect_row_conflicting_edges.
func layout_table_formatting_context_border_conflict_finder_collect_row_conflicting_edges(this: const LayoutTableFormattingContextBorderConflictFinder*, result: ak.Vector[LayoutTableFormattingContextConflictingEdge]*, cell: const LayoutTableGridCell*, edge: LayoutTableFormattingContextConflictingSide):
    let rows = &this.m_context.m_rows
    # Top edge of the row.
    if edge == LayoutTableFormattingContextConflictingSide.top:
        ak.vector_append_t(result, LayoutTableFormattingContextConflictingEdge(element = ak.vector_at(rows, cell.row_index).box.layout_node(), element_kind = PaintPaintableBoxConflictingElementKind.row, side = LayoutTableFormattingContextConflictingSide.top, row = cell.row_index, column = none))
    # Bottom edge of the row.
    if edge == LayoutTableFormattingContextConflictingSide.bottom:
        ak.vector_append_t(result, LayoutTableFormattingContextConflictingEdge(element = ak.vector_at(rows, cell.row_index).box.layout_node(), element_kind = PaintPaintableBoxConflictingElementKind.row, side = LayoutTableFormattingContextConflictingSide.bottom, row = cell.row_index, column = none))
    # Bottom edge of the row above.
    if cell.row_index >= cell.row_span and edge == LayoutTableFormattingContextConflictingSide.top:
        let above_row_index = cell.row_index - cell.row_span
        ak.vector_append_t(result, LayoutTableFormattingContextConflictingEdge(element = ak.vector_at(rows, above_row_index).box.layout_node(), element_kind = PaintPaintableBoxConflictingElementKind.row, side = LayoutTableFormattingContextConflictingSide.bottom, row = above_row_index, column = none))
    # Top edge of the row below.
    if cell.row_index + cell.row_span < ak.vector_size(rows) and edge == LayoutTableFormattingContextConflictingSide.bottom:
        let below_row_index = cell.row_index + cell.row_span
        ak.vector_append_t(result, LayoutTableFormattingContextConflictingEdge(element = ak.vector_at(rows, below_row_index).box.layout_node(), element_kind = PaintPaintableBoxConflictingElementKind.row, side = LayoutTableFormattingContextConflictingSide.top, row = below_row_index, column = none))

## Ported from Libraries/LibWeb/Layout/TableFormattingContext.cpp:1598 Web::Layout::TableFormattingContext::BorderConflictFinder::collect_row_group_conflicting_edges.
func layout_table_formatting_context_border_conflict_finder_collect_row_group_conflicting_edges(this: const LayoutTableFormattingContextBorderConflictFinder*, result: ak.Vector[LayoutTableFormattingContextConflictingEdge]*, cell: const LayoutTableGridCell*, edge: LayoutTableFormattingContextConflictingSide):
    let row_group_elements_by_index = &this.m_row_group_elements_by_index
    let maybe_row_group = *ak.vector_at_const(row_group_elements_by_index, cell.row_index)
    # Top edge of the row group.
    if let row_group = maybe_row_group:
        if cell.row_index == row_group.start_index and edge == LayoutTableFormattingContextConflictingSide.top:
            ak.vector_append_t(result, LayoutTableFormattingContextConflictingEdge(element = row_group.row_group, element_kind = PaintPaintableBoxConflictingElementKind.row_group, side = LayoutTableFormattingContextConflictingSide.top, row = row_group.start_index, column = none))
    # Bottom edge of the row group above.
    if cell.row_index >= cell.row_span:
        let maybe_row_group_above = *ak.vector_at_const(row_group_elements_by_index, cell.row_index - cell.row_span)
        if let row_group_above = maybe_row_group_above:
            if cell.row_index == row_group_above.start_index + row_group_above.row_count and edge == LayoutTableFormattingContextConflictingSide.top:
                ak.vector_append_t(result, LayoutTableFormattingContextConflictingEdge(element = row_group_above.row_group, element_kind = PaintPaintableBoxConflictingElementKind.row_group, side = LayoutTableFormattingContextConflictingSide.bottom, row = row_group_above.start_index, column = none))
    # Bottom edge of the row group.
    if let row_group = maybe_row_group:
        if cell.row_index == row_group.start_index + row_group.row_count - 1 and edge == LayoutTableFormattingContextConflictingSide.bottom:
            ak.vector_append_t(result, LayoutTableFormattingContextConflictingEdge(element = row_group.row_group, element_kind = PaintPaintableBoxConflictingElementKind.row_group, side = LayoutTableFormattingContextConflictingSide.bottom, row = row_group.start_index, column = none))
    # Top edge of the row group below.
    if cell.row_index + cell.row_span < ak.vector_size(row_group_elements_by_index):
        let maybe_row_group_below = *ak.vector_at_const(row_group_elements_by_index, cell.row_index + cell.row_span)
        if let row_group_below = maybe_row_group_below:
            if cell.row_index + cell.row_span == row_group_below.start_index and edge == LayoutTableFormattingContextConflictingSide.bottom:
                ak.vector_append_t(result, LayoutTableFormattingContextConflictingEdge(element = row_group_below.row_group, element_kind = PaintPaintableBoxConflictingElementKind.row_group, side = LayoutTableFormattingContextConflictingSide.top, row = row_group_below.start_index, column = none))

## Ported from Libraries/LibWeb/Layout/TableFormattingContext.cpp:1625 Web::Layout::TableFormattingContext::BorderConflictFinder::collect_column_group_conflicting_edges.
func layout_table_formatting_context_border_conflict_finder_collect_column_group_conflicting_edges(this: const LayoutTableFormattingContextBorderConflictFinder*, result: ak.Vector[LayoutTableFormattingContextConflictingEdge]*, cell: const LayoutTableGridCell*, edge: LayoutTableFormattingContextConflictingSide):
    # Left edge of the column group.
    if let col_element = layout_table_formatting_context_border_conflict_finder_get_col_element(this, cell.column_index):
        if edge == LayoutTableFormattingContextConflictingSide.left:
            ak.vector_append_t(result, LayoutTableFormattingContextConflictingEdge(element = col_element, element_kind = PaintPaintableBoxConflictingElementKind.column_group, side = LayoutTableFormattingContextConflictingSide.left, row = none, column = cell.column_index))
    # Right edge of the column group to the left.
    # (`cell.column_index - cell.column_span` wraps as size_t arithmetic does when the cell starts the row.)
    if let col_element = layout_table_formatting_context_border_conflict_finder_get_col_element(this, cell.column_index -% cell.column_span):
        if cell.column_index >= cell.column_span and edge == LayoutTableFormattingContextConflictingSide.left:
            ak.vector_append_t(result, LayoutTableFormattingContextConflictingEdge(element = col_element, element_kind = PaintPaintableBoxConflictingElementKind.column_group, side = LayoutTableFormattingContextConflictingSide.right, row = none, column = cell.column_index - cell.column_span))
    # Right edge of the column group.
    if let col_element = layout_table_formatting_context_border_conflict_finder_get_col_element(this, cell.column_index):
        if edge == LayoutTableFormattingContextConflictingSide.right:
            ak.vector_append_t(result, LayoutTableFormattingContextConflictingEdge(element = col_element, element_kind = PaintPaintableBoxConflictingElementKind.column_group, side = LayoutTableFormattingContextConflictingSide.right, row = none, column = cell.column_index))
    # Left edge of the column group to the right.
    if let col_element = layout_table_formatting_context_border_conflict_finder_get_col_element(this, cell.column_index -% cell.column_span):
        if edge == LayoutTableFormattingContextConflictingSide.right:
            ak.vector_append_t(result, LayoutTableFormattingContextConflictingEdge(element = col_element, element_kind = PaintPaintableBoxConflictingElementKind.column_group, side = LayoutTableFormattingContextConflictingSide.left, row = none, column = cell.column_index + cell.column_span))

## Ported from Libraries/LibWeb/Layout/TableFormattingContext.cpp:1645 Web::Layout::TableFormattingContext::BorderConflictFinder::collect_table_box_conflicting_edges.
func layout_table_formatting_context_border_conflict_finder_collect_table_box_conflicting_edges(this: const LayoutTableFormattingContextBorderConflictFinder*, result: ak.Vector[LayoutTableFormattingContextConflictingEdge]*, cell: const LayoutTableGridCell*, edge: LayoutTableFormattingContextConflictingSide):
    let context = this.m_context
    let table_box = layout_table_formatting_context_table_box(context).layout_node()
    let row_box = ak.vector_at(&context.m_rows, cell.row_index).box.layout_node()
    let maybe_row_group = *ak.vector_at_const(&this.m_row_group_elements_by_index, cell.row_index)
    # Top edge from column group or table. Left and right edges of the column group are handled in collect_column_group_conflicting_edges.
    if cell.row_index == 0 and edge == LayoutTableFormattingContextConflictingSide.top:
        if let col_element = layout_table_formatting_context_border_conflict_finder_get_col_element(this, cell.column_index):
            ak.vector_append_t(result, LayoutTableFormattingContextConflictingEdge(element = col_element, element_kind = PaintPaintableBoxConflictingElementKind.column_group, side = LayoutTableFormattingContextConflictingSide.top, row = none, column = cell.column_index))
        ak.vector_append_t(result, LayoutTableFormattingContextConflictingEdge(element = table_box, element_kind = PaintPaintableBoxConflictingElementKind.table, side = LayoutTableFormattingContextConflictingSide.top, row = none, column = none))
    # Bottom edge from column group or table. Left and right edges of the column group are handled in collect_column_group_conflicting_edges.
    if cell.row_index + cell.row_span == ak.vector_size(&context.m_rows) and edge == LayoutTableFormattingContextConflictingSide.bottom:
        if let col_element = layout_table_formatting_context_border_conflict_finder_get_col_element(this, cell.column_index):
            ak.vector_append_t(result, LayoutTableFormattingContextConflictingEdge(element = col_element, element_kind = PaintPaintableBoxConflictingElementKind.column_group, side = LayoutTableFormattingContextConflictingSide.bottom, row = none, column = cell.column_index))
        ak.vector_append_t(result, LayoutTableFormattingContextConflictingEdge(element = table_box, element_kind = PaintPaintableBoxConflictingElementKind.table, side = LayoutTableFormattingContextConflictingSide.bottom, row = none, column = none))
    # Left edge from row group or table. Top and bottom edges of the row group are handled in collect_row_group_conflicting_edges.
    if cell.column_index == 0 and edge == LayoutTableFormattingContextConflictingSide.left:
        ak.vector_append_t(result, LayoutTableFormattingContextConflictingEdge(element = row_box, element_kind = PaintPaintableBoxConflictingElementKind.row, side = LayoutTableFormattingContextConflictingSide.left, row = cell.row_index, column = none))
        if let row_group = maybe_row_group:
            ak.vector_append_t(result, LayoutTableFormattingContextConflictingEdge(element = row_group.row_group, element_kind = PaintPaintableBoxConflictingElementKind.row_group, side = LayoutTableFormattingContextConflictingSide.left, row = cell.row_index, column = none))
        ak.vector_append_t(result, LayoutTableFormattingContextConflictingEdge(element = table_box, element_kind = PaintPaintableBoxConflictingElementKind.table, side = LayoutTableFormattingContextConflictingSide.left, row = none, column = none))
    # Right edge from row group or table. Top and bottom edges of the row group are handled in collect_row_group_conflicting_edges.
    if cell.column_index + cell.column_span == ak.vector_size(&context.m_columns) and edge == LayoutTableFormattingContextConflictingSide.right:
        ak.vector_append_t(result, LayoutTableFormattingContextConflictingEdge(element = row_box, element_kind = PaintPaintableBoxConflictingElementKind.row, side = LayoutTableFormattingContextConflictingSide.right, row = cell.row_index, column = none))
        if let row_group = maybe_row_group:
            ak.vector_append_t(result, LayoutTableFormattingContextConflictingEdge(element = row_group.row_group, element_kind = PaintPaintableBoxConflictingElementKind.row_group, side = LayoutTableFormattingContextConflictingSide.right, row = cell.row_index, column = none))
        ak.vector_append_t(result, LayoutTableFormattingContextConflictingEdge(element = table_box, element_kind = PaintPaintableBoxConflictingElementKind.table, side = LayoutTableFormattingContextConflictingSide.right, row = none, column = none))

## Ported from Libraries/LibWeb/Layout/TableFormattingContext.cpp:1679 Web::Layout::TableFormattingContext::BorderConflictFinder::conflicting_edges.
func layout_table_formatting_context_border_conflict_finder_conflicting_edges(this: const LayoutTableFormattingContextBorderConflictFinder*, cell: const LayoutTableGridCell*, edge: LayoutTableFormattingContextConflictingSide) -> ak.Vector[LayoutTableFormattingContextConflictingEdge]:
    var result = ak.Vector[LayoutTableFormattingContextConflictingEdge]()
    layout_table_formatting_context_border_conflict_finder_collect_cell_conflicting_edges(this, &result, cell, edge)
    layout_table_formatting_context_border_conflict_finder_collect_row_conflicting_edges(this, &result, cell, edge)
    layout_table_formatting_context_border_conflict_finder_collect_row_group_conflicting_edges(this, &result, cell, edge)
    layout_table_formatting_context_border_conflict_finder_collect_column_group_conflicting_edges(this, &result, cell, edge)
    layout_table_formatting_context_border_conflict_finder_collect_table_box_conflicting_edges(this, &result, cell, edge)
    return result

# mark: The grid ================================================================================

## Ported from Libraries/LibWeb/Layout/TableFormattingContext.cpp:1691 Web::Layout::TableFormattingContext::finish_grid_initialization.
func layout_table_formatting_context_finish_grid_initialization(this: LayoutTableFormattingContext*, table_grid: const LayoutTableGrid*):
    ak.vector_resize(&this.m_columns, layout_table_grid_column_count(table_grid))
    ak.vector_resize(&this.m_cells_by_coordinate, ak.vector_size(&this.m_rows))
    for position_to_cell_row in &ak.vector_span(&this.m_cells_by_coordinate):
        ak.vector_resize(position_to_cell_row, layout_table_grid_column_count(table_grid))
    for cell in &ak.vector_span(&this.m_cells):
        *ak.vector_at(ak.vector_at(&this.m_cells_by_coordinate, cell.row_index), cell.column_index) = cell
        ak.vector_at(&this.m_columns, cell.column_index).has_originating_cells = true