Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.1

0.6.1 / src / web / html / html_table_section_element.lucb

#==============================================================================================
#
#   html_table_section_element - HTML::HTMLTableSectionElement: <thead>, <tbody> and <tfoot>
#
#   DESCRIPTION:
#       Port of LibWeb/HTML/HTMLTableSectionElement.cpp (lines 1-140) and
#       HTMLTableSectionElement.h: the constructor (which moves the mixin tables to
#       HTMLTableSectionElement's, as C++'s vptrs move), initialize, the rows collection,
#       insertRow/deleteRow, the presentational hints and the default role.
#
#==============================================================================================

## Ported from HTMLTableSectionElement.cpp:26 Web::HTML::HTMLTableSectionElement::HTMLTableSectionElement.
pub func html_table_section_element_construct(this: HtmlTableSectionElement*, document: DomDocument*, qualified_name: DomQualifiedName):
    html_element_construct(this.html_element(), document, qualified_name)
    # The vptrs of the mixin bases move to HTMLTableSectionElement's tables.
    let element = this.html_element()
    element.element.slottable_mixin.vtable = &html_table_section_element_slottable_mixin_vtable
    element.element.aria_mixin.vtable = &html_table_section_element_aria_mixin_vtable
    element.element.animatable.vtable = &html_table_section_element_animatable_vtable
    element.global_event_handlers.vtable = &html_table_section_element_global_event_handlers_vtable
    element.form_associated_element.vtable = &html_table_section_element_form_associated_element_vtable

## Ported from HTMLTableSectionElement.cpp:33 Web::HTML::HTMLTableSectionElement::initialize (override).
pub func html_table_section_element_initialize_impl(base: JsCell*, realm: Realm*):
    web_set_prototype_for_interface(base, realm, BindInterfaceName.html_table_section_element)
    html_element_initialize_impl(base, realm)

## Ported from HTMLTableSectionElement.cpp:39 Web::HTML::HTMLTableSectionElement::visit_edges (override).
pub func html_table_section_element_visit_edges_impl(base: gc.Cell*, visitor: gc.CellVisitor*):
    let this = (HtmlTableSectionElement*)base
    html_element_visit_edges_impl(base, visitor)
    gc.cell_visitor_visit_t(visitor, this.m_rows)

## The filter of the rows collection: tr elements.
func html_table_section_element_rows_filter(context: void*?, element: DomElement*) -> bool:
    return is_html_table_row_element(element)

## Ported from HTMLTableSectionElement.cpp:46 Web::HTML::HTMLTableSectionElement::rows.
# https://html.spec.whatwg.org/multipage/tables.html#dom-tbody-rows
pub func html_table_section_element_rows(this: HtmlTableSectionElement*) -> DomHtmlCollection*:
    # The rows attribute must return an HTMLCollection rooted at this element,
    # whose filter matches only tr elements that are children of this element.
    if this.m_rows == none:
        this.m_rows = dom_html_collection_create(this.dom_parent_node(), DomHtmlCollectionScope.children, ak.Function1[DomElement*, bool](call = html_table_section_element_rows_filter, context = none), ak.Function2[DomElement*, DomElement*, bool]())
    return this.m_rows else trap("the rows collection")

## Ported from HTMLTableSectionElement.cpp:59 Web::HTML::HTMLTableSectionElement::insert_row.
# https://html.spec.whatwg.org/multipage/tables.html#dom-tbody-insertrow
pub func html_table_section_element_insert_row(this: HtmlTableSectionElement*, index: i32) -> HtmlTableRowElement*!:
    let rows_collection = html_table_section_element_rows(this)
    let rows_collection_size = (i64)dom_html_collection_length(rows_collection)

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

    # 2. Let table row be the result of creating an element given this element's node document, "tr", and the HTML namespace.
    let table_row = (HtmlTableRowElement*)(try dom_create_element(dom_node_document(this.dom_node()), html_tag_names_tr, namespace_html))

    # 3. If index is −1 or equal to the number of items in the rows collection, then append table row to this element.
    if index == -1 or (i64)index == rows_collection_size:
        _ = try dom_node_append_child(this.dom_node(), table_row.dom_node())
    # 4. Otherwise, insert table row as a child of this element, immediately before the index-th tr element in the rows collection.
    else:
        let child = dom_html_collection_item(rows_collection, (usize)index)
        dom_node_insert_before(this.dom_node(), table_row.dom_node(), html_table_optional_element_node(child))

    # 5. Return table row.
    return table_row

## Ported from HTMLTableSectionElement.cpp:83 Web::HTML::HTMLTableSectionElement::delete_row.
# https://html.spec.whatwg.org/multipage/tables.html#dom-tbody-deleterow
pub func html_table_section_element_delete_row(this: HtmlTableSectionElement*, index: i32) -> !:
    let rows_collection = html_table_section_element_rows(this)
    let rows_collection_size = (i64)dom_html_collection_length(rows_collection)

    # 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 >= rows_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 rows"))))

    # 2. If index is −1, then remove the last element in the rows collection from this element, or do nothing if the rows collection is empty.
    if index == -1:
        if rows_collection_size > 0:
            let last = dom_html_collection_item(rows_collection, (usize)(rows_collection_size - 1)) else trap("an item in range")
            dom_node_remove(last.dom_node())
    # 3. Otherwise, remove the indexth element in the rows collection from this element.
    else:
        let row = dom_html_collection_item(rows_collection, (usize)index) else trap("an item in range")
        dom_node_remove(row.dom_node())

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

## Ported from HTMLTableSectionElement.cpp:116 Web::HTML::HTMLTableSectionElement::apply_presentational_hints (override).
pub func html_table_section_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))
        # https://html.spec.whatwg.org/multipage/rendering.html#tables-2:encoding-parsing-and-serializing-a-url
        elif ak.fly_string_eq_fly_string(&name, &html_attribute_names_background):
            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)))
        # https://html.spec.whatwg.org/multipage/rendering.html#tables-2:rules-for-parsing-a-legacy-colour-value
        elif ak.fly_string_eq_fly_string(&name, &html_attribute_names_bgcolor):
            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_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))

## Ported from HTMLTableSectionElement.h:30 Web::HTML::HTMLTableSectionElement::default_role (override).
pub func html_table_section_element_default_role_impl(base: AriaMixin*) -> AriaRole?:
    return AriaRole.rowgroup

## Ported from HTMLTableSectionElement.h:35 Web::HTML::HTMLTableSectionElement::is_html_table_section_element (override).
pub func html_table_section_element_is_html_table_section_element_impl(base: DomNode*) -> bool:
    return true