Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.0

0.6.0 / src / web / html / html_table_row_element.lucb

#==============================================================================================
#
#   html_table_row_element - HTML::HTMLTableRowElement: <tr>
#
#   DESCRIPTION:
#       Port of LibWeb/HTML/HTMLTableRowElement.cpp (lines 1-200) and HTMLTableRowElement.h:
#       the constructor (which moves the mixin tables to HTMLTableRowElement's, as C++'s vptrs
#       move), initialize, the presentational hints, the cells collection, rowIndex and
#       sectionRowIndex, insertCell/deleteCell and the default role.
#
#==============================================================================================

## Ported from HTMLTableRowElement.cpp:29 Web::HTML::HTMLTableRowElement::HTMLTableRowElement.
pub func html_table_row_element_construct(this: HtmlTableRowElement*, document: DomDocument*, qualified_name: DomQualifiedName):
    html_element_construct(this.html_element(), document, qualified_name)
    # The vptrs of the mixin bases move to HTMLTableRowElement's tables.
    let element = this.html_element()
    element.element.slottable_mixin.vtable = &html_table_row_element_slottable_mixin_vtable
    element.element.aria_mixin.vtable = &html_table_row_element_aria_mixin_vtable
    element.element.animatable.vtable = &html_table_row_element_animatable_vtable
    element.global_event_handlers.vtable = &html_table_row_element_global_event_handlers_vtable
    element.form_associated_element.vtable = &html_table_row_element_form_associated_element_vtable

## Ported from HTMLTableRowElement.cpp:36 Web::HTML::HTMLTableRowElement::initialize (override).
pub func html_table_row_element_initialize_impl(base: JsCell*, realm: Realm*):
    web_set_prototype_for_interface(base, realm, BindInterfaceName.html_table_row_element)
    html_element_initialize_impl(base, realm)

## Ported from HTMLTableRowElement.cpp:42 Web::HTML::HTMLTableRowElement::is_presentational_hint (override).
pub func html_table_row_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[5] = [html_attribute_names_align, html_attribute_names_bgcolor, html_attribute_names_background, html_attribute_names_height, html_attribute_names_valign]
    for candidate in names:
        if ak.fly_string_eq_fly_string(&name, &candidate):
            return true
    return false

## Ported from HTMLTableRowElement.cpp:55 Web::HTML::HTMLTableRowElement::apply_presentational_hints (override).
pub func html_table_row_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_align):
            if let parsed_value = html_parse_table_child_element_align_value(value):
                ak.vector_append_t(properties, CssStyleProperty(property_id = css_data.PropertyId.text_align, value = parsed_value))
        elif 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()))
        elif 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)))
        elif 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))
        elif ak.fly_string_eq_fly_string(&name, &html_attribute_names_valign):
            if let parsed_value = parse_css_value(html_document_parsing_params(document), value, css_data.PropertyId.vertical_align):
                ak.vector_append_t(properties, CssStyleProperty(property_id = css_data.PropertyId.vertical_align, value = parsed_value))

## Ported from HTMLTableRowElement.cpp:81 Web::HTML::HTMLTableRowElement::visit_edges (override).
pub func html_table_row_element_visit_edges_impl(base: gc.Cell*, visitor: gc.CellVisitor*):
    let this = (HtmlTableRowElement*)base
    html_element_visit_edges_impl(base, visitor)
    gc.cell_visitor_visit_t(visitor, this.m_cells)

## The filter of the cells collection: td and th elements.
func html_table_row_element_cells_filter(context: void*?, element: DomElement*) -> bool:
    return is_html_table_cell_element(element)

## Ported from HTMLTableRowElement.cpp:88 Web::HTML::HTMLTableRowElement::cells.
# https://html.spec.whatwg.org/multipage/tables.html#dom-tr-cells
pub func html_table_row_element_cells(this: HtmlTableRowElement*) -> DomHtmlCollection*:
    # The cells attribute must return an HTMLCollection rooted at this tr element,
    # whose filter matches only td and th elements that are children of the tr element.
    if this.m_cells == none:
        this.m_cells = dom_html_collection_create(this.dom_parent_node(), DomHtmlCollectionScope.children, ak.Function1[DomElement*, bool](call = html_table_row_element_cells_filter, context = none), ak.Function2[DomElement*, DomElement*, bool]())
    return this.m_cells else trap("the cells collection")

## The index of `this` in a rows collection, or -1 (rowIndex and sectionRowIndex).
func html_table_row_element_index_in(this: HtmlTableRowElement*, rows_collection: DomHtmlCollection*) -> i32:
    var rows = dom_html_collection_collect_matching_elements(rows_collection)
    for (i, row) in ak.vector_span(&rows).indexed():
        if row == this.dom_element():
            return (i32)i
    return -1

## Ported from HTMLTableRowElement.cpp:101 Web::HTML::HTMLTableRowElement::row_index.
# https://html.spec.whatwg.org/multipage/tables.html#dom-tr-rowindex
pub func html_table_row_element_row_index(this: HtmlTableRowElement*) -> i32:
    # The rowIndex attribute must, if this element has a parent table element,
    # or a parent tbody, thead, or tfoot element and a grandparent table element,
    # return the index of this tr element in that table element's rows collection.
    # If there is no such table element, then the attribute must return −1.
    var rows_collection: DomHtmlCollection*? = none
    if let parent = tree_node_parent(this.dom_node()):
        if let table = as_if_html_table_element(parent):
            rows_collection = html_table_element_rows(table)
        elif is_html_table_section_element(parent):
            if let grandparent = tree_node_parent(parent):
                if let table = as_if_html_table_element(grandparent):
                    rows_collection = html_table_element_rows(table)
    let collection = rows_collection else return -1
    return html_table_row_element_index_in(this, collection)

## Ported from HTMLTableRowElement.cpp:126 Web::HTML::HTMLTableRowElement::section_row_index.
pub func html_table_row_element_section_row_index(this: HtmlTableRowElement*) -> i32:
    # The sectionRowIndex attribute must, if this element has a parent table, tbody, thead, or tfoot element,
    # return the index of the tr element in the parent element's rows collection
    # (for tables, that's HTMLTableElement's rows collection; for table sections, that's HTMLTableSectionElement's rows collection).
    # If there is no such parent element, then the attribute must return −1.
    var rows_collection: DomHtmlCollection*? = none
    if let parent = tree_node_parent(this.dom_node()):
        if let table = as_if_html_table_element(parent):
            rows_collection = html_table_element_rows(table)
        elif let section = as_if_html_table_section_element(parent):
            rows_collection = html_table_section_element_rows(section)
    let collection = rows_collection else return -1
    return html_table_row_element_index_in(this, collection)

## Ported from HTMLTableRowElement.cpp:152 Web::HTML::HTMLTableRowElement::insert_cell.
# https://html.spec.whatwg.org/multipage/tables.html#dom-tr-insertcell
pub func html_table_row_element_insert_cell(this: HtmlTableRowElement*, index: i32) -> HtmlTableCellElement*!:
    let cells_collection = html_table_row_element_cells(this)
    let cells_collection_size = (i32)dom_html_collection_length(cells_collection)

    # 1. If index is less than −1 or greater than the number of elements in the cells collection, then throw an "IndexSizeError" DOMException.
    if index < -1 or index > cells_collection_size:
        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 cells"))))

    # 2. Let table cell be the result of creating an element given this tr element's node document, "td", and the HTML namespace.
    let table_cell = (HtmlTableCellElement*)(try dom_create_element(dom_node_document(this.dom_node()), html_tag_names_td, namespace_html))

    # 3. If index is equal to −1 or equal to the number of items in cells collection, then append table cell to this tr element.
    if index == -1 or index == cells_collection_size:
        _ = try dom_node_append_child(this.dom_node(), table_cell.dom_node())

    # 4. Otherwise, insert table cell as a child of this tr element, immediately before the indexth td or th element in the cells collection.
    else:
        let child = dom_html_collection_item(cells_collection, (usize)index)
        dom_node_insert_before(this.dom_node(), table_cell.dom_node(), html_table_optional_element_node(child))

    # 5. Return table cell.
    return table_cell

## An optional element as an optional node (`GC::Ptr<Element>` passed where a `Node*` is taken).
pub func html_table_optional_element_node(element: DomElement*?) -> DomNode*?:
    let value = element else return none
    return value.dom_node()

## Ported from HTMLTableRowElement.cpp:177 Web::HTML::HTMLTableRowElement::delete_cell.
# https://html.spec.whatwg.org/multipage/tables.html#dom-tr-deletecell
pub func html_table_row_element_delete_cell(this: HtmlTableRowElement*, index: i32) -> !:
    let cells_collection = html_table_row_element_cells(this)
    let cells_collection_size = (i32)dom_html_collection_length(cells_collection)

    # 1. If index is less than −1 or greater than or equal to the number of elements in the cells collection, then throw an "IndexSizeError" DOMException.
    if index < -1 or index >= cells_collection_size:
        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 cells"))))

    # 2. If index is −1, then remove the last element in the cells collection from its parent, or do nothing if the cells collection is empty.
    if index == -1:
        if cells_collection_size > 0:
            let last = dom_html_collection_item(cells_collection, (usize)(cells_collection_size - 1)) else trap("an item in range")
            dom_node_remove(last.dom_node())

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

## Ported from HTMLTableRowElement.h:29 Web::HTML::HTMLTableRowElement::default_role (override).
pub func html_table_row_element_default_role_impl(base: AriaMixin*) -> AriaRole?:
    return AriaRole.row

## Ported from HTMLTableRowElement.h:34 Web::HTML::HTMLTableRowElement::is_html_table_row_element (override).
pub func html_table_row_element_is_html_table_row_element_impl(base: DomNode*) -> bool:
    return true