Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.0

0.6.0 / src / web / layout / tests_layout_table.lucb

#==============================================================================================
#
#   tests_layout_table - table layout of parsed documents, pinned to the reference build
#
#   DESCRIPTION:
#       Runs the cases of tests_layout_table_cases through the port. The local oracle that
#       wrote the table (luce-browser-tools, oracles/luce-browser-engine/layout_geometry,
#       cases_table.txt) runs the reference build's LibWeb on the same documents. Modes:
#
#       layout              tests_layout_geometry's harness (layout, commit, and the three dumps
#                           of a Layout test). The first cases are Ladybird's own Layout tests
#                           whose layout first reached the TableWrapper; the others are written
#                           for this region: column widths in auto and fixed mode, spanning
#                           cells, table and row heights, separated and collapsed borders,
#                           captions, vertical alignment, row groups, collapsed rows, anonymous
#                           table parts, intrinsic and specified table widths, nested tables.
#       table               TableGrid::calculate_row_column_grid of every table box: its rows,
#                           cells and occupied slots (in the AK HashMap's iteration order).
#       border-specificity  TableFormattingContext::border_is_less_specific over pairs of border
#                           styles and widths.
#
#==============================================================================================

# mark: The "table" and "border-specificity" modes ==============================================

## The index of `node` in the inclusive pre-order of the layout tree under `root` (the oracle's node numbers).
func test_layout_table_node_index(root: LayoutNode*, node: LayoutNode*) -> usize:
    var index: usize = 0
    for candidate in layout_node_inclusive_subtree(root):
        if candidate == node:
            return index
        index += 1
    return index

## The "table" mode: TableGrid::calculate_row_column_grid of every box with a table-inside display, in pre-order.
func test_layout_table_dump_grids(out: ak.StringBuilder*, root: LayoutNode*):
    var builder = ak.string_builder_new()
    var node_index: usize = 0
    for node in layout_node_inclusive_subtree(root):
        let index = node_index
        node_index += 1
        let box = as_if_layout_box(node) else continue
        let display = layout_node_display(node)
        if not css_display_is_table_inside(&display):
            continue
        var cells = ak.Vector[LayoutTableGridCell]()
        var rows = ak.Vector[LayoutTableGridRow]()
        let grid = layout_table_grid_calculate_row_column_grid_vector(box, &cells, &rows)
        dump_appendff(&builder, "table #{}: {} columns, {} rows, {} cells\n", [ak.arg_u64((u64)index), ak.arg_u64((u64)layout_table_grid_column_count(&grid)),
                                                                               ak.arg_u64((u64)ak.vector_size(&rows)), ak.arg_u64((u64)ak.vector_size(&cells))])
        for row in &ak.vector_span(&rows):
            dump_appendff(&builder, "  row #{} collapsed {}\n", [ak.arg_u64((u64)test_layout_table_node_index(root, row.box.layout_node())), ak.arg_bool(row.is_collapsed)])
        for cell in &ak.vector_span(&cells):
            dump_appendff(&builder, "  cell #{} at {},{} span {}x{}\n", [ak.arg_u64((u64)test_layout_table_node_index(root, cell.box.layout_node())), ak.arg_u64((u64)cell.column_index),
                                                                         ak.arg_u64((u64)cell.row_index), ak.arg_u64((u64)cell.column_span), ak.arg_u64((u64)cell.row_span)])
        ak.string_builder_append(&builder, "  occupied:")
        let occupancy_grid = layout_table_grid_occupancy_grid(&grid)
        for it in ak.hash_map_begin_const(&occupancy_grid):
            dump_appendff(&builder, " {},{}", [ak.arg_u64((u64)it.key.x), ak.arg_u64((u64)it.key.y)])
        ak.string_builder_append(&builder, "\n")
    test_layout_tree_put_lines(out, ak.string_builder_string_view(&builder))

## The "border-specificity" mode: border_is_less_specific(a, b) for every pair of borders of the ten line styles and the
## widths 0, 1 and 3px (style-major), one line per a with a 0 or 1 per b.
func test_layout_table_dump_border_specificity(out: ak.StringBuilder*):
    let styles = [css_data.LineStyle.none_, css_data.LineStyle.hidden, css_data.LineStyle.dotted, css_data.LineStyle.dashed, css_data.LineStyle.solid,
                  css_data.LineStyle.double, css_data.LineStyle.groove, css_data.LineStyle.ridge, css_data.LineStyle.inset, css_data.LineStyle.outset]
    let widths = [0, 1, 3]
    var borders = ak.Vector[display_list.BorderData]()
    for style in styles:
        for width in widths:
            ak.vector_append_t(&borders, display_list.BorderData(line_style = style, width = gfx.CssPixels.from_int(width)))
    var builder = ak.string_builder_new()
    for a in ak.vector_span(&borders):
        for b in ak.vector_span(&borders):
            ak.string_builder_append(&builder, "1" if layout_table_formatting_context_border_is_less_specific(a, b) else "0")
        ak.string_builder_append(&builder, "\n")
    test_layout_tree_put_lines(out, ak.string_builder_string_view(&builder))

## Runs one case: "layout" (tests_layout_geometry's harness), "table" or "border-specificity".
func test_layout_table_run_case(heap: gc.Heap*, realm: Realm*, test_case: const TestLayoutGeometryCase*) -> ak.String:
    if test_case.mode == "layout":
        return test_layout_geometry_run_case(heap, realm, test_case)
    var out = ak.string_builder_new()
    if test_case.mode == "border-specificity":
        test_layout_table_dump_border_specificity(&out)
        return ak.string_builder_to_string_without_validation(&out)
    assert(test_case.mode == "table")
    var (document, navigable) = test_layout_tree_parse_and_style(heap, realm, test_case.html, test_case.css)
    defer gc.root_release(&navigable)
    test_layout_geometry_update_layout(document)
    gc.heap_collect_garbage(heap)
    let root = dom_document_unsafe_layout_node(document) else trap("no layout tree")
    test_layout_table_dump_grids(&out, root.layout_node())
    return ak.string_builder_to_string_without_validation(&out)

# mark: Tests ===================================================================================

test "table layout (column and row measures, widths, heights, collapsed borders, captions, grids) matches the reference build":
    web_initialize_strings()
    test_fonts_setup()
    let vm = web_test_create_vm()
    defer web_test_destroy_vm(vm)
    var (realm, realm_root) = test_layout_tree_window_realm(vm)
    defer gc.root_release(&realm_root)
    let heap = vm_heap(vm)
    var passed: usize = 0
    var failed: usize = 0
    let allocator: memory.Allocator = heap
    with allocator:
        test_page_client_color_scheme = CssPreferredColorScheme.light
        defer test_page_client_color_scheme = CssPreferredColorScheme.auto
        test_media_invalidation_install_user_agent_sheets(realm)
        defer test_style_computer_release_user_agent_sheets()
        for test_case in &test_layout_table_cases:
            let actual = test_layout_table_run_case(heap, realm, test_case)
            let actual_view = ak.string_bytes_as_string_view(&actual)
            if ak.string_view_equals(&actual_view, test_case.expected):
                passed += 1
            else:
                failed += 1
                print(f"table layout case mismatch ({test_case.mode}): {test_case.html} / {test_case.css}")
                print(f"expected:\n{test_case.expected}actual:\n{ak.string_view_text(actual_view)}")
    print(f"table layout: {passed} passed, {failed} failed")
    assert(failed == 0)
    assert(passed > 0)