Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.1

0.6.1 / src / web / html / html_option_element.lucb

#==============================================================================================
#
#   html_option_element - HTML::HTMLOptionElement: <option>
#
#   DESCRIPTION:
#       Port of LibWeb/HTML/HTMLOptionElement.cpp (lines 1-371) and the inline functions of
#       HTMLOptionElement.h: construction (which moves the mixin tables to HTMLOptionElement's,
#       as C++'s vptrs move), selectedness and dirtiness, the value, text and label, the index
#       in the list of options, disabled options, the form, the cached nearest ancestor select
#       element, cloning into a selectedcontent element, and the insertion, removing, moving
#       and children changed steps.
#
#       Copyright (c) 2020, the SerenityOS developers.
#       Copyright (c) 2022-2024, Andreas Kling <andreas@ladybird.org>
#       SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================

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

## Ported from HTMLOptionElement.cpp:34 Web::HTML::HTMLOptionElement::HTMLOptionElement.
pub func html_option_element_construct(this: HtmlOptionElement*, document: DomDocument*, qualified_name: DomQualifiedName):
    html_element_construct(this.html_element(), document, qualified_name)
    # The vptrs of the mixin bases move to HTMLOptionElement's tables.
    let element = this.html_element()
    element.element.slottable_mixin.vtable = &html_option_element_slottable_mixin_vtable
    element.element.aria_mixin.vtable = &html_option_element_aria_mixin_vtable
    element.element.animatable.vtable = &html_option_element_animatable_vtable
    element.global_event_handlers.vtable = &html_option_element_global_event_handlers_vtable
    element.form_associated_element.vtable = &html_option_element_form_associated_element_vtable

## Ported from HTMLOptionElement.cpp:41 Web::HTML::HTMLOptionElement::initialize (override).
pub func html_option_element_initialize_impl(base: JsCell*, realm: Realm*):
    web_set_prototype_for_interface(base, realm, BindInterfaceName.html_option_element)
    html_element_initialize_impl(base, realm)

## Ported from HTMLOptionElement.cpp:47 Web::HTML::HTMLOptionElement::visit_edges (override).
pub func html_option_element_visit_edges_impl(base: gc.Cell*, visitor: gc.CellVisitor*):
    let this = (HtmlOptionElement*)base
    html_element_visit_edges_impl(base, visitor)
    gc.cell_visitor_visit_t(visitor, this.m_cached_nearest_select_element)

## `first_ancestor_of_type<HTMLSelectElement>()` of an option element.
func html_option_element_first_ancestor_select(this: HtmlOptionElement*) -> HtmlSelectElement*?:
    let ancestor = tree_node_first_ancestor_of_type(this.dom_node(), &html_select_element_class) else return none
    return as_html_select_element(ancestor)

## Ported from HTMLOptionElement.cpp:54 Web::HTML::HTMLOptionElement::update_selection_label.
# FIXME: This needs to be called any time a descendant's text is modified.
func html_option_element_update_selection_label(this: HtmlOptionElement*):
    if html_option_element_selected(this):
        if let select_element = html_option_element_first_ancestor_select(this):
            html_select_element_clone_selected_option_into_select_button(select_element)

## Ported from HTMLOptionElement.cpp:63 Web::HTML::HTMLOptionElement::attribute_changed (override).
pub func html_option_element_attribute_changed_impl(base: DomElement*, name: ak.FlyString, old_value: ak.String?, value: ak.String?, namespace_: ak.FlyString?):
    let this = (HtmlOptionElement*)base
    html_element_attribute_changed_impl(base, name, old_value, value, namespace_)

    if ak.fly_string_eq_fly_string(&name, &html_attribute_names_selected):
        if value == none:
            # Whenever an option element's selected attribute is removed, if its dirtiness is false, its selectedness must be set to false.
            if not this.m_dirty:
                html_option_element_set_selected_internal(this, false)
        else:
            # Except where otherwise specified, when the element is created, its selectedness must be set to true
            # if the element has a selected attribute. Whenever an option element's selected attribute is added,
            # if its dirtiness is false, its selectedness must be set to true.
            if not this.m_dirty:
                html_option_element_set_selected_internal(this, true)
    elif ak.fly_string_eq_fly_string(&name, &html_attribute_names_label):
        html_option_element_update_selection_label(this)

# mark: Selectedness and the value ==============================================================

## Ported from HTMLOptionElement.cpp:85 Web::HTML::HTMLOptionElement::set_selected.
# https://html.spec.whatwg.org/multipage/form-elements.html#dom-option-selected
pub func html_option_element_set_selected(this: HtmlOptionElement*, selected: bool):
    # On setting, it must set the element's selectedness to the new value, set its dirtiness to true, and then cause the element to ask for a reset.
    html_option_element_set_selected_internal(this, selected)
    this.m_dirty = true
    html_option_element_ask_for_a_reset(this)

## Ported from HTMLOptionElement.cpp:93 Web::HTML::HTMLOptionElement::set_selected_internal.
pub func html_option_element_set_selected_internal(this: HtmlOptionElement*, selected: bool):
    if this.m_selected != selected:
        css_invalidation_invalidate_style_after_option_selected_state_change(this.dom_element())

    this.m_selected = selected
    if selected:
        this.m_selectedness_update_index = html_m_next_selectedness_update_index
        html_m_next_selectedness_update_index += 1

    # this is here to invalidate the cache on the HTMLCollection in HTMLSelectElement::selected_options
    dom_document_bump_dom_tree_version(dom_node_document(this.dom_node()))

## Ported from HTMLOptionElement.cpp:107 Web::HTML::HTMLOptionElement::value.
# https://html.spec.whatwg.org/multipage/form-elements.html#dom-option-value
pub func html_option_element_value(this: HtmlOptionElement*) -> ak.Utf16String:
    # The value of an option element is the value of the value content attribute, if there is one.
    # ...or, if there is not, the value of the element's text IDL attribute.
    if let value = dom_element_attribute(this.dom_element(), html_attribute_names_value):
        return ak.utf16_string_from_utf8_string(&value)
    return html_option_element_text(this)

## Ported from HTMLOptionElement.cpp:117 Web::HTML::HTMLOptionElement::set_value.
# https://html.spec.whatwg.org/multipage/form-elements.html#dom-option-value
pub func html_option_element_set_value(this: HtmlOptionElement*, value: ak.Utf16String):
    dom_element_set_attribute_value(this.dom_element(), html_attribute_names_value, ak.utf16_string_base_to_utf8_but_should_be_ported_to_utf16(&value.utf16_string_base))

## Ported from HTMLOptionElement.cpp:122 Web::HTML::concatenate_descendants_text_content.
func html_concatenate_descendants_text_content(node: DomNode*, builder: ak.StringBuilder*):
    if is_html_script_element(node) or is_svg_script_element(node):
        return
    if let text = as_if_dom_text(node):
        let data = dom_character_data_data(text.dom_character_data())
        ak.string_builder_append_utf16_view(builder, ak.utf16_string_base_utf16_view(&data.utf16_string_base))
    for child in dom_node_children(node):
        html_concatenate_descendants_text_content(child, builder)

## Ported from HTMLOptionElement.cpp:135 Web::HTML::HTMLOptionElement::label.
# https://html.spec.whatwg.org/multipage/form-elements.html#dom-option-label
pub func html_option_element_label(this: HtmlOptionElement*) -> ak.String:
    # The label IDL attribute, on getting, if there is a label content attribute,
    # must return that attribute's value; otherwise, it must return the element's label.
    if let label = dom_element_attribute(this.dom_element(), html_attribute_names_label):
        return label
    let text = html_option_element_text(this)
    return ak.utf16_string_base_to_utf8_but_should_be_ported_to_utf16(&text.utf16_string_base)

## Ported from HTMLOptionElement.cpp:145 Web::HTML::HTMLOptionElement::set_label.
# https://html.spec.whatwg.org/multipage/form-elements.html#dom-option-label
pub func html_option_element_set_label(this: HtmlOptionElement*, label: ak.String):
    dom_element_set_attribute_value(this.dom_element(), html_attribute_names_label, label)
    # Note: this causes attribute_changed() to be called, which will update the <select>'s label

## Ported from HTMLOptionElement.cpp:152 Web::HTML::HTMLOptionElement::text.
# https://html.spec.whatwg.org/multipage/form-elements.html#dom-option-text
pub func html_option_element_text(this: HtmlOptionElement*) -> ak.Utf16String:
    var builder = ak.string_builder_new_mode(ak.StringBuilderMode.utf16)

    # Concatenation of data of all the Text node descendants of the option element, in tree order,
    # excluding any that are descendants of descendants of the option element that are themselves
    # script or SVG script elements.
    for child in dom_node_children(this.dom_node()):
        html_concatenate_descendants_text_content(child, &builder)

    # Return the result of stripping and collapsing ASCII whitespace from the above concatenation.
    return web_infra.strip_and_collapse_whitespace_utf16_string(ak.string_builder_to_utf16_string(&builder))

## Ported from HTMLOptionElement.cpp:169 Web::HTML::HTMLOptionElement::set_text.
# https://html.spec.whatwg.org/multipage/form-elements.html#dom-option-text
pub func html_option_element_set_text(this: HtmlOptionElement*, text: ak.Utf16String):
    dom_node_string_replace_all(this.dom_node(), text)
    # Note: this causes children_changed() to be called, which will update the <select>'s label

## Ported from HTMLOptionElement.cpp:176 Web::HTML::HTMLOptionElement::index.
# https://html.spec.whatwg.org/multipage/form-elements.html#concept-option-index
pub func html_option_element_index(this: HtmlOptionElement*) -> i32:
    # An option element's index is the number of option elements that are in the same list of options but that come before it in tree order.
    if let select_element = html_option_element_first_ancestor_select(this):
        var index: i32 = 0
        var list_of_options = html_select_element_list_of_options(select_element)
        for option_element in ak.vector_span(&list_of_options):
            if option_element == (HtmlOptionElement*?)this:
                return index
            index += 1

    # If the option element is not in a list of options, then the option element's index is zero.
    return 0

## Ported from HTMLOptionElement.cpp:193 Web::HTML::HTMLOptionElement::ask_for_a_reset.
# https://html.spec.whatwg.org/multipage/form-elements.html#ask-for-a-reset
func html_option_element_ask_for_a_reset(this: HtmlOptionElement*):
    # If an option element in the list of options asks for a reset, then run that select element's selectedness setting algorithm.
    if let select = html_option_element_first_ancestor_select(this):
        html_select_element_update_selectedness(select)

## Ported from HTMLOptionElement.cpp:202 Web::HTML::HTMLOptionElement::disabled.
# https://html.spec.whatwg.org/multipage/form-elements.html#concept-option-disabled
pub func html_option_element_disabled(this: HtmlOptionElement*) -> bool:
    # An option element is disabled if its disabled attribute is present or if it is a child of an optgroup element whose disabled attribute is present.
    if dom_element_has_attribute(this.dom_element(), html_attribute_names_disabled):
        return true
    let parent = this.dom_node().tree_node.m_parent else return false
    return is_html_opt_group_element(parent) and dom_element_has_attribute(as_dom_element(parent), html_attribute_names_disabled)

## Ported from HTMLOptionElement.cpp:210 Web::HTML::HTMLOptionElement::form.
# https://html.spec.whatwg.org/multipage/form-elements.html#dom-option-form
pub func html_option_element_form(this: HtmlOptionElement*) -> HtmlFormElement*?:
    # The form getter steps are:

    # 1. Let select be this's option element nearest ancestor select.
    # 2. If select is null, then return null.
    let select = html_option_element_nearest_select_element_const(this) else return none

    # 3. Return select's form owner.
    return html_form_associated_element_form(&select.html_element_base.form_associated_element)

# mark: The nearest ancestor select =============================================================

## Ported from HTMLOptionElement.cpp:226 Web::HTML::HTMLOptionElement::update_nearest_select_element.
# https://html.spec.whatwg.org/multipage/form-elements.html#update-an-option's-nearest-ancestor-select
func html_option_element_update_nearest_select_element(this: HtmlOptionElement*):
    # 1. Let oldSelect be option's cached nearest ancestor select element.
    let old_select = this.m_cached_nearest_select_element

    # 2. Let newSelect be option's option element nearest ancestor select.
    let new_select = html_option_element_compute_nearest_select_element(this)

    # 3. If oldSelect is not newSelect:
    if old_select != new_select:
        # 1. If oldSelect is not null, then run the selectedness setting algorithm given oldSelect.
        if let select = old_select:
            html_select_element_update_selectedness(select)

        # 2. If newSelect is not null, then run the selectedness setting algorithm given newSelect.
        if let select = new_select:
            html_select_element_update_selectedness(select)

    # 4. Set option's cached nearest ancestor select element to newSelect.
    this.m_cached_nearest_select_element = new_select

## Ported from HTMLOptionElement.cpp:250 Web::HTML::HTMLOptionElement::compute_nearest_select_element.
# https://html.spec.whatwg.org/multipage/form-elements.html#option-element-nearest-ancestor-select
func html_option_element_compute_nearest_select_element(this: HtmlOptionElement*) -> HtmlSelectElement*?:
    # 1. Let ancestorOptgroup be null.
    var ancestor_optgroup: HtmlOptGroupElement*? = none

    # 2. For each ancestor of option's ancestors, in reverse tree order:
    var maybe_ancestor = this.dom_node().tree_node.m_parent
    while let ancestor = maybe_ancestor:
        # 1. If ancestor is a datalist, hr, or option element, then return null.
        if is_html_data_list_element(ancestor) or is_html_htmlhr_element(ancestor) or is_html_option_element(ancestor):
            return none

        # 2. If ancestor is an optgroup element:
        if let optgroup_element = as_if_html_opt_group_element(ancestor):
            # 1. If ancestorOptgroup is not null, then return null.
            if ancestor_optgroup != none:
                return none

            # 2. Set ancestorOptgroup to ancestor.
            ancestor_optgroup = optgroup_element

        # 3. If ancestor is a select, then return ancestor.
        if let select_element = as_if_html_select_element(ancestor):
            return select_element

        maybe_ancestor = ancestor.tree_node.m_parent

    # 3. Return null.
    return none

## Ported from HTMLOptionElement.cpp:282 Web::HTML::HTMLOptionElement::default_role (override).
pub func html_option_element_default_role_impl(base: AriaMixin*) -> AriaRole?:
    _ = base
    # https://www.w3.org/TR/html-aria/#el-option
    # TODO: Only an option element that is in a list of options or that represents a suggestion in a datalist should return option
    return AriaRole.option

# mark: selectedcontent =========================================================================

## Ported from HTMLOptionElement.cpp:290 Web::HTML::HTMLOptionElement::maybe_clone_into_selectedcontent.
# https://html.spec.whatwg.org/multipage/form-elements.html#the-option-element:clone-an-option-into-a-selectedcontent
pub func html_option_element_maybe_clone_into_selectedcontent(this: HtmlOptionElement*) -> !:
    # To maybe clone an option into selectedcontent, given an option option:

    # 1. Let select be option's option element nearest ancestor select.
    let select = this.m_cached_nearest_select_element

    # 2. If all of the following conditions are true:
    #      - select is not null;
    #      - option's selectedness is true; and
    #      - select's enabled selectedcontent is not null,
    #    then run clone an option into a selectedcontent given option and select's enabled selectedcontent.
    if select != none and html_option_element_selected(this):
        if let selectedcontent = html_select_element_enabled_selectedcontent(select else trap("unreachable")):
            try html_option_element_clone_into_selectedcontent(this, selectedcontent)

## Ported from HTMLOptionElement.cpp:311 Web::HTML::HTMLOptionElement::clone_into_selectedcontent.
# https://html.spec.whatwg.org/multipage/form-elements.html#clone-an-option-into-a-selectedcontent
pub func html_option_element_clone_into_selectedcontent(this: HtmlOptionElement*, selectedcontent: HtmlSelectedContentElement*) -> !:
    let document = dom_node_document(this.dom_node())
    # To clone an option into a selectedcontent, given an option element option and a selectedcontent element selectedcontent:

    # 1. Let documentFragment be a new DocumentFragment whose node document is option's node document.
    let fragment = realm_create_dom_document_fragment(html_element_realm(this.html_element()), document)

    # 2. For each child of option's children:
    var maybe_child = this.dom_node().tree_node.m_first_child
    while let child = maybe_child:
        # 1. Let childClone be the result of running clone given child with subtree set to true.
        let child_clone = try dom_node_clone_node(child, document, true)

        # 2. Append childClone to documentFragment.
        _ = try dom_node_append_child(fragment.dom_node(), child_clone)
        maybe_child = child.tree_node.m_next_sibling

    # 3. Replace all with documentFragment within selectedcontent.
    dom_node_replace_all(selectedcontent.dom_node(), fragment.dom_node())

# mark: Tree changes ============================================================================

## Ported from HTMLOptionElement.cpp:333 Web::HTML::HTMLOptionElement::inserted (override).
pub func html_option_element_inserted_impl(base: DomNode*):
    let this = (HtmlOptionElement*)base
    html_element_inserted_impl(base)

    html_option_element_set_selected_internal(this, html_option_element_selected(this))

    # The option HTML element insertion steps, given insertedOption,
    # are to run update an option's nearest ancestor select given insertedOption.
    html_option_element_update_nearest_select_element(this)

## Ported from HTMLOptionElement.cpp:345 Web::HTML::HTMLOptionElement::removed_from (override).
# https://html.spec.whatwg.org/multipage/form-elements.html#the-option-element:html-element-removing-steps
pub func html_option_element_removed_from_impl(base: DomNode*, is_subtree_root: DomNodeIsSubtreeRoot, old_ancestor: DomNode*?, old_root: DomNode*):
    html_element_removed_from_impl(base, is_subtree_root, old_ancestor, old_root)

    # The option HTML element removing steps, given removedNode, isSubtreeRoot, and oldAncestor are to run update an
    # option's nearest ancestor select given removedNode.
    html_option_element_update_nearest_select_element((HtmlOptionElement*)base)

## Ported from HTMLOptionElement.cpp:355 Web::HTML::HTMLOptionElement::moved_from (override).
# https://html.spec.whatwg.org/multipage/form-elements.html#the-option-element:html-element-moving-steps
pub func html_option_element_moved_from_impl(base: DomNode*, is_subtree_root: DomNodeIsSubtreeRoot, old_ancestor: DomNode*?):
    html_element_moved_from_impl(base, is_subtree_root, old_ancestor)

    # The option HTML element moving steps, given movedNode, isSubtreeRoot, and oldAncestor are to run update an
    # option's nearest ancestor select given movedNode.
    html_option_element_update_nearest_select_element((HtmlOptionElement*)base)

## Ported from HTMLOptionElement.cpp:364 Web::HTML::HTMLOptionElement::children_changed (override).
pub func html_option_element_children_changed_impl(base: DomNode*, metadata: DomNodeChildrenChangedMetadata):
    dom_element_children_changed_impl(base, metadata)

    html_option_element_update_selection_label((HtmlOptionElement*)base)

# mark: Inline functions of HTMLOptionElement.h =================================================

## Ported from HTMLOptionElement.h:22 Web::HTML::HTMLOptionElement::selected.
pub func html_option_element_selected(this: HtmlOptionElement*) -> bool:
    return this.m_selected

## Ported from HTMLOptionElement.h:25 Web::HTML::HTMLOptionElement::selectedness_update_index.
pub func html_option_element_selectedness_update_index(this: HtmlOptionElement*) -> u64:
    return this.m_selectedness_update_index

## Ported from HTMLOptionElement.h:28 Web::HTML::HTMLOptionElement::form_value (override).
pub func html_option_element_form_value_impl(base: HtmlFormAssociatedElement*) -> ak.Utf16String:
    return html_option_element_value((HtmlOptionElement*)((u8*)base - memory.offset_of(HtmlElement, form_associated_element)))

## Ported from HTMLOptionElement.h:45 Web::HTML::HTMLOptionElement::nearest_select_element.
pub func html_option_element_nearest_select_element(this: HtmlOptionElement*) -> HtmlSelectElement*?:
    return this.m_cached_nearest_select_element

## Ported from HTMLOptionElement.h:46 Web::HTML::HTMLOptionElement::nearest_select_element (const).
pub func html_option_element_nearest_select_element_const(this: HtmlOptionElement*) -> HtmlSelectElement*?:
    return this.m_cached_nearest_select_element

## Ported from HTMLOptionElement.h:59 Web::HTML::HTMLOptionElement::is_html_option_element (override).
pub func html_option_element_is_html_option_element_impl(base: DomNode*) -> bool:
    _ = base
    return true