Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.1

0.6.1 / src / web / html / html_table_element.lucb

#==============================================================================================
#
#   html_table_element - HTML::HTMLTableElement: <table>
#
#   DESCRIPTION:
#       Port of LibWeb/HTML/HTMLTableElement.cpp (lines 1-509) and HTMLTableElement.h: the
#       constructor (which moves the mixin tables to HTMLTableElement's, as C++'s vptrs move),
#       initialize, the presentational hints, the cellpadding attribute (which restyles the
#       cells), the caption/tHead/tFoot accessors and their create/delete operations, the
#       tBodies and rows collections, insertRow/deleteRow and the border.
#
#==============================================================================================

# mark: Construction ============================================================================

## Ported from HTMLTableElement.cpp:32 Web::HTML::HTMLTableElement::HTMLTableElement.
pub func html_table_element_construct(this: HtmlTableElement*, document: DomDocument*, qualified_name: DomQualifiedName):
    html_element_construct(this.html_element(), document, qualified_name)
    # The vptrs of the mixin bases move to HTMLTableElement's tables.
    let element = this.html_element()
    element.element.slottable_mixin.vtable = &html_table_element_slottable_mixin_vtable
    element.element.aria_mixin.vtable = &html_table_element_aria_mixin_vtable
    element.element.animatable.vtable = &html_table_element_animatable_vtable
    element.global_event_handlers.vtable = &html_table_element_global_event_handlers_vtable
    element.form_associated_element.vtable = &html_table_element_form_associated_element_vtable

## Ported from HTMLTableElement.cpp:39 Web::HTML::HTMLTableElement::initialize (override).
pub func html_table_element_initialize_impl(base: JsCell*, realm: Realm*):
    web_set_prototype_for_interface(base, realm, BindInterfaceName.html_table_element)
    html_element_initialize_impl(base, realm)

## Ported from HTMLTableElement.cpp:45 Web::HTML::HTMLTableElement::visit_edges (override).
pub func html_table_element_visit_edges_impl(base: gc.Cell*, visitor: gc.CellVisitor*):
    let this = (HtmlTableElement*)base
    html_element_visit_edges_impl(base, visitor)
    gc.cell_visitor_visit_t(visitor, this.m_rows)
    gc.cell_visitor_visit_t(visitor, this.m_t_bodies)

# mark: Presentational hints ====================================================================

## Ported from HTMLTableElement.cpp:52 Web::HTML::parse_border.
func html_parse_border(value: ak.StringView) -> u32:
    return ak.string_view_to_number_u32(&value) else 0

## Ported from HTMLTableElement.cpp:57 Web::HTML::HTMLTableElement::is_presentational_hint (override).
pub func html_table_element_is_presentational_hint_impl(base: DomElement*, name: ak.FlyString) -> bool:
    if dom_element_is_presentational_hint_impl(base, name):
        return true

    let names: ak.FlyString[9] = [html_attribute_names_align, html_attribute_names_background, html_attribute_names_bgcolor, html_attribute_names_border, html_attribute_names_bordercolor, html_attribute_names_cellpadding, html_attribute_names_cellspacing, html_attribute_names_height, html_attribute_names_width]
    for candidate in names:
        if ak.fly_string_eq_fly_string(&name, &candidate):
            return true
    return false

## Ported from HTMLTableElement.cpp:74 Web::HTML::HTMLTableElement::apply_presentational_hints (override).
pub func html_table_element_apply_presentational_hints_impl(base: DomElement*, properties: ak.Vector[CssStyleProperty]*):
    dom_element_apply_presentational_hints_impl(base, properties)
    let document = dom_node_document(base.dom_node())
    for attribute in dom_element_attribute_span(base):
        let name = dom_attr_name(attribute)
        let value_string = dom_attr_value(attribute)
        let value = ak.string_bytes_as_string_view(&value_string)
        if ak.fly_string_eq_fly_string(&name, &html_attribute_names_width):
            if let parsed_value = html_parse_nonzero_dimension_value(value):
                ak.vector_append_t(properties, CssStyleProperty(property_id = css_data.PropertyId.width, value = parsed_value))
            continue
        if ak.fly_string_eq_fly_string(&name, &html_attribute_names_height):
            if let parsed_value = html_parse_dimension_value(value):
                ak.vector_append_t(properties, CssStyleProperty(property_id = css_data.PropertyId.height, value = parsed_value))
            continue
        if ak.fly_string_eq_fly_string(&name, &html_attribute_names_align):
            if ak.string_equals_ignoring_ascii_case_string_view(&value_string, ak.sv("center")):
                ak.vector_append_t(properties, CssStyleProperty(property_id = css_data.PropertyId.margin_left, value = css_keyword_style_value_create(css_data.Keyword.auto).css_style_value()))
                ak.vector_append_t(properties, CssStyleProperty(property_id = css_data.PropertyId.margin_right, value = css_keyword_style_value_create(css_data.Keyword.auto).css_style_value()))
            elif let parsed_value = parse_css_value(html_document_parsing_params(document), value, css_data.PropertyId.float):
                ak.vector_append_t(properties, CssStyleProperty(property_id = css_data.PropertyId.float, value = parsed_value))
            continue
        if ak.fly_string_eq_fly_string(&name, &html_attribute_names_background):
            # https://html.spec.whatwg.org/multipage/rendering.html#tables-2:encoding-parsing-and-serializing-a-url
            if let parsed_value = dom_document_encoding_parse_url(document, value):
                ak.vector_append_t(properties, CssStyleProperty(property_id = css_data.PropertyId.background_image, value = html_table_background_image_value(parsed_value)))
            continue
        if ak.fly_string_eq_fly_string(&name, &html_attribute_names_bgcolor):
            # https://html.spec.whatwg.org/multipage/rendering.html#tables-2:rules-for-parsing-a-legacy-colour-value
            if let color = html_parse_legacy_color_value(value):
                ak.vector_append_t(properties, CssStyleProperty(property_id = css_data.PropertyId.background_color, value = css_color_style_value_create_from_color(color, CssColorSyntax.legacy).css_style_value()))
            continue
        if ak.fly_string_eq_fly_string(&name, &html_attribute_names_cellspacing):
            if let parsed_value = html_parse_dimension_value(value):
                ak.vector_append_t(properties, CssStyleProperty(property_id = css_data.PropertyId.border_spacing, value = parsed_value))
            continue
        if ak.fly_string_eq_fly_string(&name, &html_attribute_names_border):
            let border = html_parse_border(value)
            if border == 0:
                continue
            html_table_element_apply_border_style(properties, border, css_data.PropertyId.border_left_style, css_data.PropertyId.border_left_width, css_data.PropertyId.border_left_color)
            html_table_element_apply_border_style(properties, border, css_data.PropertyId.border_top_style, css_data.PropertyId.border_top_width, css_data.PropertyId.border_top_color)
            html_table_element_apply_border_style(properties, border, css_data.PropertyId.border_right_style, css_data.PropertyId.border_right_width, css_data.PropertyId.border_right_color)
            html_table_element_apply_border_style(properties, border, css_data.PropertyId.border_bottom_style, css_data.PropertyId.border_bottom_width, css_data.PropertyId.border_bottom_color)
        if ak.fly_string_eq_fly_string(&name, &html_attribute_names_bordercolor):
            # https://html.spec.whatwg.org/multipage/rendering.html#tables-2:attr-table-bordercolor
            # When a table element has a bordercolor attribute, its value is expected to be parsed using the rules for parsing a legacy color value,
            # and if that does not return failure, the user agent is expected to treat the attribute as a presentational hint setting the element's
            # 'border-top-color', 'border-right-color', 'border-bottom-color', and 'border-left-color' properties to the resulting color.
            if let parsed_color = html_parse_legacy_color_value(value):
                let color_value = css_color_style_value_create_from_color(parsed_color, CssColorSyntax.legacy).css_style_value()
                ak.vector_append_t(properties, CssStyleProperty(property_id = css_data.PropertyId.border_top_color, value = color_value))
                ak.vector_append_t(properties, CssStyleProperty(property_id = css_data.PropertyId.border_right_color, value = color_value))
                ak.vector_append_t(properties, CssStyleProperty(property_id = css_data.PropertyId.border_bottom_color, value = color_value))
                ak.vector_append_t(properties, CssStyleProperty(property_id = css_data.PropertyId.border_left_color, value = color_value))

## apply_presentational_hints' apply_border_style lambda: an outset border `border` px wide in
## rgb(128, 128, 128).
func html_table_element_apply_border_style(properties: ak.Vector[CssStyleProperty]*, border: u32, style_property: css_data.PropertyId, width_property: css_data.PropertyId, color_property: css_data.PropertyId):
    let legacy_line_style = css_keyword_style_value_create(css_data.Keyword.outset)
    ak.vector_append_t(properties, CssStyleProperty(property_id = style_property, value = legacy_line_style.css_style_value()))
    ak.vector_append_t(properties, CssStyleProperty(property_id = width_property, value = css_length_style_value_create(css_length_make_px_f64(f64(border))).css_style_value()))
    ak.vector_append_t(properties, CssStyleProperty(property_id = color_property, value = css_color_style_value_create_from_color(gfx.color_new_u8_u8_u8(128, 128, 128), CssColorSyntax.legacy).css_style_value()))

## Ported from HTMLTableElement.cpp:146 Web::HTML::HTMLTableElement::attribute_changed (override).
pub func html_table_element_attribute_changed_impl(base: DomElement*, name: ak.FlyString, old_value: ak.String?, value: ak.String?, namespace_: ak.FlyString?):
    let this = (HtmlTableElement*)base
    html_element_attribute_changed_impl(base, name, old_value, value, namespace_)

    if ak.fly_string_eq_fly_string(&name, &html_attribute_names_cellpadding):
        let old_cellpadding = this.m_cellpadding
        if let new_value = value:
            let parsed = html_parse_integer(ak.string_bytes_as_string_view(&new_value)) else 0
            this.m_cellpadding = (u32)ak.max(0, parsed)
        else:
            this.m_cellpadding = 1

        # NOTE: cellpadding is magical, it applies to the cells inside this table, not the table itself.
        #       When it changes, we need new style for the cells.
        if old_cellpadding != this.m_cellpadding:
            for node in dom_node_subtree(this.dom_node()):
                if is_html_table_cell_element(node):
                    dom_node_set_needs_style_update(node, true)
        return

# mark: Caption, tHead, tFoot ===================================================================

## Ported from HTMLTableElement.cpp:170 Web::HTML::HTMLTableElement::caption.
# https://html.spec.whatwg.org/multipage/tables.html#dom-table-caption
pub func html_table_element_caption(this: HtmlTableElement*) -> HtmlTableCaptionElement*?:
    # The caption IDL attribute must return, on getting, the first caption element child of the table element,
    # if any, or null otherwise.
    return (HtmlTableCaptionElement*?)tree_node_first_child_of_type(this.dom_node(), &html_table_caption_element_class)

## Ported from HTMLTableElement.cpp:178 Web::HTML::HTMLTableElement::set_caption.
# https://html.spec.whatwg.org/multipage/tables.html#dom-table-caption
pub func html_table_element_set_caption(this: HtmlTableElement*, caption: HtmlTableCaptionElement*?) -> !:
    # On setting, the first caption element child of the table element, if any, must be removed,
    # and the new value, if not null, must be inserted as the first node of the table element.
    html_table_element_delete_caption(this)

    if let new_caption = caption:
        _ = try dom_node_pre_insert(this.dom_node(), new_caption.dom_node(), tree_node_first_child(this.dom_node()))

## Ported from HTMLTableElement.cpp:191 Web::HTML::HTMLTableElement::create_caption.
# https://html.spec.whatwg.org/multipage/tables.html#dom-table-createcaption
pub func html_table_element_create_caption(this: HtmlTableElement*) -> HtmlTableCaptionElement*:
    if let maybe_caption = html_table_element_caption(this):
        return maybe_caption

    let caption = dom_create_element(dom_node_document(this.dom_node()), html_tag_names_caption, namespace_html) else trap("FIXME: should propagate errors")
    _ = dom_node_pre_insert(this.dom_node(), caption.dom_node(), tree_node_first_child(this.dom_node())) else trap("MUST: pre_insert")
    return (HtmlTableCaptionElement*)caption

## Ported from HTMLTableElement.cpp:204 Web::HTML::HTMLTableElement::delete_caption.
# https://html.spec.whatwg.org/multipage/tables.html#dom-table-deletecaption
pub func html_table_element_delete_caption(this: HtmlTableElement*):
    if let maybe_caption = html_table_element_caption(this):
        dom_node_remove(maybe_caption.dom_node(), false)

## The first child of `this` that is a table section element named `local_name` (t_head, t_foot).
func html_table_element_first_section_child_named(this: HtmlTableElement*, local_name: ak.FlyString) -> HtmlTableSectionElement*?:
    for child in dom_node_children(this.dom_node()):
        if let table_section_element = as_if_html_table_section_element(child):
            let name = dom_element_local_name(table_section_element.dom_element())
            if ak.fly_string_eq_fly_string(&name, &local_name):
                return table_section_element
    return none

## Ported from HTMLTableElement.cpp:213 Web::HTML::HTMLTableElement::t_head.
# https://html.spec.whatwg.org/multipage/tables.html#dom-table-thead
pub func html_table_element_t_head(this: HtmlTableElement*) -> HtmlTableSectionElement*?:
    # The tHead IDL attribute must return, on getting, the first thead element child of the table element,
    # if any, or null otherwise.
    return html_table_element_first_section_child_named(this, html_tag_names_thead)

## The child before which a new thead goes (set_t_head, create_t_head): the first HTML element
## child that is neither a caption nor a colgroup.
func html_table_element_thead_insertion_child(this: HtmlTableElement*) -> DomNode*?:
    # We insert the new thead after any <caption> or <colgroup> elements
    for child in dom_node_children(this.dom_node()):
        if not is_html_element(child):
            continue
        if is_html_table_caption_element(child):
            continue
        if let table_col_element = as_if_html_table_col_element(child):
            let name = dom_element_local_name(table_col_element.dom_element())
            if ak.fly_string_eq_fly_string(&name, &html_tag_names_colgroup):
                continue

        # We have found an element which is not a <caption> or <colgroup>, we'll insert before this
        return child
    return none

## Ported from HTMLTableElement.cpp:228 Web::HTML::HTMLTableElement::set_t_head.
# https://html.spec.whatwg.org/multipage/tables.html#dom-table-thead
pub func html_table_element_set_t_head(this: HtmlTableElement*, thead: HtmlTableSectionElement*?) -> !:
    # If the new value is neither null nor a thead element, then a "HierarchyRequestError" DOMException must be thrown instead.
    if let new_thead = thead:
        let name = dom_element_local_name(new_thead.dom_element())
        if not ak.fly_string_eq_fly_string(&name, &html_tag_names_thead):
            try idl_throw(.idl_dom_exception(idl_hierarchy_request_error_create(html_element_realm(this.html_element()), ak.utf16_string_from_str("Element is not thead"))))

    # On setting, if the new value is null or a thead element, the first thead element child of the table element,
    # if any, must be removed,
    html_table_element_delete_t_head(this)

    let new_thead = thead else return

    # and the new value, if not null, must be inserted immediately before the first element in the table element
    # that is neither a caption element nor a colgroup element, if any,
    # or at the end of the table if there are no such elements.
    let child_to_insert_before = html_table_element_thead_insertion_child(this)

    _ = try dom_node_pre_insert(this.dom_node(), new_thead.dom_node(), child_to_insert_before)

## Ported from HTMLTableElement.cpp:268 Web::HTML::HTMLTableElement::create_t_head.
# https://html.spec.whatwg.org/multipage/tables.html#dom-table-createthead
pub func html_table_element_create_t_head(this: HtmlTableElement*) -> HtmlTableSectionElement*:
    if let maybe_thead = html_table_element_t_head(this):
        return maybe_thead

    let thead = dom_create_element(dom_node_document(this.dom_node()), html_tag_names_thead, namespace_html) else trap("FIXME: should propagate errors")

    let child_to_insert_before = html_table_element_thead_insertion_child(this)

    _ = dom_node_pre_insert(this.dom_node(), thead.dom_node(), child_to_insert_before) else trap("MUST: pre_insert")

    return (HtmlTableSectionElement*)thead

## Ported from HTMLTableElement.cpp:299 Web::HTML::HTMLTableElement::delete_t_head.
# https://html.spec.whatwg.org/multipage/tables.html#dom-table-deletethead
pub func html_table_element_delete_t_head(this: HtmlTableElement*):
    if let maybe_thead = html_table_element_t_head(this):
        dom_node_remove(maybe_thead.dom_node(), false)

## Ported from HTMLTableElement.cpp:308 Web::HTML::HTMLTableElement::t_foot.
# https://html.spec.whatwg.org/multipage/tables.html#dom-table-tfoot
pub func html_table_element_t_foot(this: HtmlTableElement*) -> HtmlTableSectionElement*?:
    # The tFoot IDL attribute must return, on getting, the first tfoot element child of the table element,
    # if any, or null otherwise.
    return html_table_element_first_section_child_named(this, html_tag_names_tfoot)

## Ported from HTMLTableElement.cpp:323 Web::HTML::HTMLTableElement::set_t_foot.
# https://html.spec.whatwg.org/multipage/tables.html#dom-table-tfoot
pub func html_table_element_set_t_foot(this: HtmlTableElement*, tfoot: HtmlTableSectionElement*?) -> !:
    # If the new value is neither null nor a tfoot element, then a "HierarchyRequestError" DOMException must be thrown instead.
    if let new_tfoot = tfoot:
        let name = dom_element_local_name(new_tfoot.dom_element())
        if not ak.fly_string_eq_fly_string(&name, &html_tag_names_tfoot):
            try idl_throw(.idl_dom_exception(idl_hierarchy_request_error_create(html_element_realm(this.html_element()), ak.utf16_string_from_str("Element is not tfoot"))))

    # On setting, if the new value is null or a tfoot element, the first tfoot element child of the table element,
    # if any, must be removed,
    html_table_element_delete_t_foot(this)

    # and the new value, if not null, must be inserted at the end of the table.
    if let new_tfoot = tfoot:
        _ = try dom_node_append_child(this.dom_node(), new_tfoot.dom_node())

## Ported from HTMLTableElement.cpp:342 Web::HTML::HTMLTableElement::create_t_foot.
# https://html.spec.whatwg.org/multipage/tables.html#dom-table-createtfoot
pub func html_table_element_create_t_foot(this: HtmlTableElement*) -> HtmlTableSectionElement*:
    if let maybe_tfoot = html_table_element_t_foot(this):
        return maybe_tfoot

    let tfoot = dom_create_element(dom_node_document(this.dom_node()), html_tag_names_tfoot, namespace_html) else trap("FIXME: should propagate errors")
    _ = dom_node_append_child(this.dom_node(), tfoot.dom_node()) else trap("MUST: append_child")
    return (HtmlTableSectionElement*)tfoot

## Ported from HTMLTableElement.cpp:354 Web::HTML::HTMLTableElement::delete_t_foot.
# https://html.spec.whatwg.org/multipage/tables.html#dom-table-deletetfoot
pub func html_table_element_delete_t_foot(this: HtmlTableElement*):
    if let maybe_tfoot = html_table_element_t_foot(this):
        dom_node_remove(maybe_tfoot.dom_node(), false)

# mark: tBodies and rows ========================================================================

## The filter of the tBodies collection: tbody elements.
func html_table_element_t_bodies_filter(context: void*?, element: DomElement*) -> bool:
    let name = dom_element_local_name(element)
    return ak.fly_string_eq_fly_string(&name, &html_tag_names_tbody)

## Ported from HTMLTableElement.cpp:363 Web::HTML::HTMLTableElement::t_bodies.
# https://html.spec.whatwg.org/multipage/tables.html#dom-table-tbodies
pub func html_table_element_t_bodies(this: HtmlTableElement*) -> DomHtmlCollection*:
    # The tBodies attribute must return an HTMLCollection rooted at the table node,
    # whose filter matches only tbody elements that are children of the table element.
    if this.m_t_bodies == none:
        this.m_t_bodies = dom_html_collection_create(this.dom_parent_node(), DomHtmlCollectionScope.children, ak.Function1[DomElement*, bool](call = html_table_element_t_bodies_filter, context = none), ak.Function2[DomElement*, DomElement*, bool]())
    return this.m_t_bodies else trap("the tBodies collection")

## Ported from HTMLTableElement.cpp:376 Web::HTML::HTMLTableElement::create_t_body.
# https://html.spec.whatwg.org/multipage/tables.html#dom-table-createtbody
pub func html_table_element_create_t_body(this: HtmlTableElement*) -> HtmlTableSectionElement*:
    let tbody = dom_create_element(dom_node_document(this.dom_node()), html_tag_names_tbody, namespace_html) else trap("FIXME: should propagate errors")

    # We insert the new tbody after the last <tbody> element
    var child_to_insert_before: DomNode*? = none
    var walk = tree_node_last_child(this.dom_node())
    while let child = walk:
        walk = tree_node_previous_sibling(child)
        if not is_html_element(child):
            continue
        if let table_section_element = as_if_html_table_section_element(child):
            let name = dom_element_local_name(table_section_element.dom_element())
            if ak.fly_string_eq_fly_string(&name, &html_tag_names_tbody):
                # We have found an element which is a <tbody> we'll insert after this
                child_to_insert_before = tree_node_next_sibling(child)
                break

    _ = dom_node_pre_insert(this.dom_node(), tbody.dom_node(), child_to_insert_before) else trap("MUST: pre_insert")

    return (HtmlTableSectionElement*)tbody

## The filter of the rows collection (its context is the table): tr elements that are children
## of the table, or of a thead, tbody or tfoot child of the table.
func html_table_element_rows_filter(context: void*?, element: DomElement*) -> bool:
    let table_node = (DomElement*)(context else trap("the rows filter's table"))
    # Only match TR elements which are:
    # * children of the table element
    # * children of the thead, tbody, or tfoot elements that are themselves children of the table element
    if not is_html_table_row_element(element):
        return false
    let parent_element = dom_node_parent_element(element.dom_node())
    if parent_element == table_node:
        return true

    if let parent = parent_element:
        let parent_name = dom_element_local_name(parent)
        if ak.fly_string_eq_fly_string(&parent_name, &html_tag_names_thead) or ak.fly_string_eq_fly_string(&parent_name, &html_tag_names_tbody) or ak.fly_string_eq_fly_string(&parent_name, &html_tag_names_tfoot):
            if tree_node_parent(parent.dom_node()) == table_node.dom_node():
                return true

    return false

## The rows collection's sort_priority lambda: thead rows first, then table and tbody rows, then
## tfoot rows.
func html_table_element_rows_sort_priority(element: DomElement*) -> i32:
    let parent = dom_node_parent_element(element.dom_node()) else trap("a row of the rows collection has a parent element")
    let parent_tag = dom_element_local_name(parent)
    if ak.fly_string_eq_fly_string(&parent_tag, &html_tag_names_thead):
        return 1
    if ak.fly_string_eq_fly_string(&parent_tag, &html_tag_names_table) or ak.fly_string_eq_fly_string(&parent_tag, &html_tag_names_tbody):
        return 2
    if ak.fly_string_eq_fly_string(&parent_tag, &html_tag_names_tfoot):
        return 3
    trap("VERIFY_NOT_REACHED")

## The sort of the rows collection: by sort priority.
func html_table_element_rows_sort(context: void*?, a: DomElement*, b: DomElement*) -> bool:
    let prio_a = html_table_element_rows_sort_priority(a)
    let prio_b = html_table_element_rows_sort_priority(b)

    if prio_a != prio_b:
        return prio_a < prio_b

    return false

## Ported from HTMLTableElement.cpp:400 Web::HTML::HTMLTableElement::rows.
# https://html.spec.whatwg.org/multipage/tables.html#dom-table-rows
pub func html_table_element_rows(this: HtmlTableElement*) -> DomHtmlCollection*:
    let table_node = this
    # The elements in the collection must be ordered such that those elements whose parent is a thead are included
    # first, in tree order, followed by those elements whose parent is either a table or tbody element, again in tree
    # order, followed finally by those elements whose parent is a tfoot element, still in tree order.
    if this.m_rows == none:
        this.m_rows = dom_html_collection_create(this.dom_parent_node(), DomHtmlCollectionScope.descendants, ak.Function1[DomElement*, bool](call = html_table_element_rows_filter, context = table_node), ak.Function2[DomElement*, DomElement*, bool](call = html_table_element_rows_sort, context = none))
    return this.m_rows else trap("the rows collection")

## Ported from HTMLTableElement.cpp:448 Web::HTML::HTMLTableElement::insert_row.
# https://html.spec.whatwg.org/multipage/tables.html#dom-table-insertrow
pub func html_table_element_insert_row(this: HtmlTableElement*, index: i32) -> HtmlTableRowElement*!:
    let rows = html_table_element_rows(this)
    let rows_length = dom_html_collection_length(rows)

    if index < -1 or (i64)index > (i64)rows_length:
        try idl_throw(.idl_dom_exception(idl_index_size_error_create(html_element_realm(this.html_element()), ak.utf16_string_from_str("Index is negative or greater than the number of rows"))))
    let document = dom_node_document(this.dom_node())
    let tr = (HtmlTableRowElement*)(try dom_create_element(document, html_tag_names_tr, namespace_html))
    if rows_length == 0 and not tree_node_has_child_of_type(this.dom_node(), &html_table_row_element_class):
        let tbody = try dom_create_element(document, html_tag_names_tbody, namespace_html)
        _ = try dom_node_append_child(tbody.dom_node(), tr.dom_node())
        _ = try dom_node_append_child(this.dom_node(), tbody.dom_node())
    elif rows_length == 0:
        let tbody = tree_node_last_child_of_type(this.dom_node(), &html_table_row_element_class) else trap("a tr child")
        _ = try dom_node_append_child(tbody, tr.dom_node())
    elif index == -1 or (i64)index == (i64)rows_length:
        let last_row = dom_html_collection_item(rows, rows_length - 1) else trap("an item in range")
        let parent_of_last_tr = dom_node_parent_element(last_row.dom_node()) else trap("a row has a parent")
        _ = try dom_node_append_child(parent_of_last_tr.dom_node(), tr.dom_node())
    else:
        let row = dom_html_collection_item(rows, (usize)index) else trap("an item in range")
        let parent = dom_node_parent_element(row.dom_node()) else trap("a row has a parent")
        dom_node_insert_before(parent.dom_node(), tr.dom_node(), row.dom_node())
    return tr

## Ported from HTMLTableElement.cpp:474 Web::HTML::HTMLTableElement::delete_row.
# https://html.spec.whatwg.org/multipage/tables.html#dom-table-deleterow
pub func html_table_element_delete_row(this: HtmlTableElement*, index: i32) -> !:
    let rows = html_table_element_rows(this)
    let rows_length = dom_html_collection_length(rows)

    # 1. If index is less than −1 or greater than or equal to the number of elements in the rows collection, then throw an "IndexSizeError" DOMException.
    if index < -1 or (i64)index >= (i64)rows_length:
        try idl_throw(.idl_dom_exception(idl_index_size_error_create(html_element_realm(this.html_element()), ak.utf16_string_from_str("Index is negative or greater than or equal to the number of rows"))))

    # 2. If index is −1, then remove the last element in the rows collection from its parent, or do nothing if the rows collection is empty.
    if index == -1:
        if rows_length == 0:
            return

        let row_to_remove = dom_html_collection_item(rows, rows_length - 1) else trap("an item in range")
        dom_node_remove(row_to_remove.dom_node(), false)
        return

    # 3. Otherwise, remove the indexth element in the rows collection from its parent.
    let row_to_remove = dom_html_collection_item(rows, (usize)index) else trap("an item in range")
    dom_node_remove(row_to_remove.dom_node(), false)

# mark: Border and cellpadding ==================================================================

## Ported from HTMLTableElement.cpp:499 Web::HTML::HTMLTableElement::border.
pub func html_table_element_border(this: HtmlTableElement*) -> u32:
    let value = dom_element_get_attribute_value(this.dom_element(), html_attribute_names_border)
    return html_parse_border(ak.string_bytes_as_string_view(&value))

## Ported from HTMLTableElement.cpp:504 Web::HTML::HTMLTableElement::cellpadding.
pub func html_table_element_cellpadding(this: HtmlTableElement*) -> u32?:
    return this.m_cellpadding

## Ported from HTMLTableElement.h:48 Web::HTML::HTMLTableElement::default_role (override).
pub func html_table_element_default_role_impl(base: AriaMixin*) -> AriaRole?:
    return AriaRole.table

## Ported from HTMLTableElement.h:56 Web::HTML::HTMLTableElement::is_html_table_element (override).
pub func html_table_element_is_html_table_element_impl(base: DomNode*) -> bool:
    return true