Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.1

0.6.1 / src / web / html / html_selected_content_element.lucb

#==============================================================================================
#
#   html_selected_content_element - HTML::HTMLSelectedContentElement: <selectedcontent>
#
#   DESCRIPTION:
#       Port of LibWeb/HTML/HTMLSelectedContentElement.cpp (lines 1-112) and the inline
#       functions of HTMLSelectedContentElement.h: construction (which moves the mixin tables
#       to HTMLSelectedContentElement's, as C++'s vptrs move), clearing, the disabled state,
#       and the post-connection and removing steps that keep the nearest select's
#       selectedcontent up to date.
#
#       Copyright (c) 2025, Feng Yu <f3n67u@outlook.com>
#       SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================

## Ported from HTMLSelectedContentElement.cpp:16 Web::HTML::HTMLSelectedContentElement::HTMLSelectedContentElement.
pub func html_selected_content_element_construct(this: HtmlSelectedContentElement*, document: DomDocument*, qualified_name: DomQualifiedName):
    html_element_construct(this.html_element(), document, qualified_name)
    # The vptrs of the mixin bases move to HTMLSelectedContentElement's tables.
    let element = this.html_element()
    element.element.slottable_mixin.vtable = &html_selected_content_element_slottable_mixin_vtable
    element.element.aria_mixin.vtable = &html_selected_content_element_aria_mixin_vtable
    element.element.animatable.vtable = &html_selected_content_element_animatable_vtable
    element.global_event_handlers.vtable = &html_selected_content_element_global_event_handlers_vtable
    element.form_associated_element.vtable = &html_selected_content_element_form_associated_element_vtable

## Ported from HTMLSelectedContentElement.cpp:23 Web::HTML::HTMLSelectedContentElement::initialize (override).
pub func html_selected_content_element_initialize_impl(base: JsCell*, realm: Realm*):
    web_set_prototype_for_interface(base, realm, BindInterfaceName.html_selected_content_element)
    html_element_initialize_impl(base, realm)

## Ported from HTMLSelectedContentElement.cpp:30 Web::HTML::HTMLSelectedContentElement::clear_selectedcontent.
# https://html.spec.whatwg.org/multipage/form-elements.html#clear-a-selectedcontent
pub func html_selected_content_element_clear_selectedcontent(this: HtmlSelectedContentElement*):
    # To clear a selectedcontent given a selectedcontent element selectedcontent:

    # 1. Replace all with null within selectedcontent.
    dom_node_replace_all(this.dom_node(), none)

## Ported from HTMLSelectedContentElement.cpp:39 Web::HTML::HTMLSelectedContentElement::post_connection (override).
# https://html.spec.whatwg.org/multipage/form-elements.html#the-selectedcontent-element:html-element-post-connection-steps
pub func html_selected_content_element_post_connection_impl(base: DomNode*):
    let this = (HtmlSelectedContentElement*)base
    # The selectedcontent HTML element post-connection steps, given selectedcontent, are:

    # 1. Let nearestSelectAncestor be null.
    var nearest_select_ancestor: HtmlSelectElement*? = none

    # 2. Let ancestor be selectedcontent's parent.
    # NB: This step is not necessary; the variable is never referenced.
    #     See https://github.com/whatwg/html/issues/11963.

    # 3. Set selectedcontent's disabled state to false.
    html_selected_content_element_set_disabled(this, false)

    # 4. For each ancestor of selectedcontent's ancestors, in reverse tree order:
    var maybe_ancestor = base.tree_node.m_parent
    while let ancestor = maybe_ancestor:
        #  1. If ancestor is a select element:
        if let select_element = as_if_html_select_element(ancestor):
            # 1. If nearestSelectAncestor is null, then set nearestSelectAncestor to select.
            if nearest_select_ancestor == none:
                nearest_select_ancestor = select_element
            # 2. Otherwise, set selectedcontent's disabled state to true.
            else:
                html_selected_content_element_set_disabled(this, true)

        # 2. If ancestor is an option element or a selectedcontent element,
        #    then set selectedcontent's disabled state to true.
        if is_html_option_element(ancestor) or is_html_selected_content_element(ancestor):
            html_selected_content_element_set_disabled(this, true)

        maybe_ancestor = ancestor.tree_node.m_parent

    # 5. If nearestSelectAncestor is null or nearestSelectAncestor has the multiple attribute, then return.
    let select = nearest_select_ancestor else return
    if dom_element_has_attribute(select.dom_element(), html_attribute_names_multiple):
        return

    # 6. Run update a select's selectedcontent given nearestSelectAncestor.
    html_select_element_update_selectedcontent(select) else trap("MUST: update_selectedcontent")

    # 7. Run clear a select's non-primary selectedcontent elements given nearestSelectAncestor.
    html_select_element_clear_non_primary_selectedcontent(select)

## Ported from HTMLSelectedContentElement.cpp:85 Web::HTML::HTMLSelectedContentElement::removed_from (override).
# https://html.spec.whatwg.org/multipage/form-elements.html#the-selectedcontent-element:html-element-removing-steps
pub func html_selected_content_element_removed_from_impl(base: DomNode*, is_subtree_root: DomNodeIsSubtreeRoot, old_ancestor: DomNode*?, old_root: DomNode*):
    let this = (HtmlSelectedContentElement*)base
    # The selectedcontent HTML element removing steps, given removedNode, isSubtreeRoot, and oldAncestor are:
    html_element_removed_from_impl(base, is_subtree_root, old_ancestor, old_root)

    # 1. If removedNode's disabled is true, then return.
    if this.m_disabled:
        return

    # 2. For each ancestor of removedNode's ancestors, in reverse tree order:
    var maybe_ancestor = base.tree_node.m_parent
    while let ancestor = maybe_ancestor:
        # 1. If ancestor is a select element, then return.
        if is_html_select_element(ancestor):
            return
        maybe_ancestor = ancestor.tree_node.m_parent

    # 3. For each ancestor of oldAncestor's inclusive ancestors, in reverse tree order:
    var maybe_old_ancestor = old_ancestor
    while let ancestor = maybe_old_ancestor:
        # 1. If ancestor is a select element, then run update a select's selectedcontent given ancestor and return.
        if let select_element = as_if_html_select_element(ancestor):
            html_select_element_update_selectedcontent(select_element) else trap("MUST: update_selectedcontent")
            return
        maybe_old_ancestor = ancestor.tree_node.m_parent

## Ported from HTMLSelectedContentElement.h:23 Web::HTML::HTMLSelectedContentElement::default_role (override).
# https://www.w3.org/TR/html-aria/#el-selectedcontent
pub func html_selected_content_element_default_role_impl(base: AriaMixin*) -> AriaRole?:
    _ = base
    return AriaRole.generic

## Ported from HTMLSelectedContentElement.h:28 Web::HTML::HTMLSelectedContentElement::disabled.
pub func html_selected_content_element_disabled(this: HtmlSelectedContentElement*) -> bool:
    return this.m_disabled

## Ported from HTMLSelectedContentElement.h:29 Web::HTML::HTMLSelectedContentElement::set_disabled.
pub func html_selected_content_element_set_disabled(this: HtmlSelectedContentElement*, disabled: bool):
    this.m_disabled = disabled