Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.0

0.6.0 / src / web / html / html_select_element_2.lucb

#==============================================================================================
#
#   html_select_element_2 - HTML::HTMLSelectElement: picker, shadow tree, selectedness
#
#   DESCRIPTION:
#       Port of LibWeb/HTML/HTMLSelectElement.cpp (lines 491-947) and the inline functions of
#       HTMLSelectElement.h: sending select update notifications, the open state, the
#       activation behavior and showing the picker (the select items given to the page's
#       dropdown), choosing an item, the insertion and attribute change steps, the chevron of
#       the user-agent shadow tree (its SVG) and the select button's text, the selectedness
#       setting algorithm, the placeholder label option, the enabled and non-primary
#       selectedcontent elements, and constraint validation.
#
#==============================================================================================

# mark: Update notifications and the picker =====================================================

## The captures of send_select_update_notifications' element task: `[this]`.
struct HtmlSelectElementSendSelectUpdateNotificationsTask:
    var this: HtmlSelectElement*

## send_select_update_notifications' element task.
func html_select_element_send_select_update_notifications_task(context: void*?):
    let captures = (HtmlSelectElementSendSelectUpdateNotificationsTask*)(context else trap("no captures"))
    let this = captures.this
    let realm = html_element_realm(this.html_element())
    # 1. Set the select element's user validity to true.
    this.m_user_validity = true

    # 2. Run update a select's selectedcontent given element.
    html_select_element_update_selectedcontent(this) else trap("MUST: update_selectedcontent")

    # 3. Run clone selected option into select button given element.
    html_select_element_clone_selected_option_into_select_button(this)

    # 4. Fire an event named input at element, with the bubbles and composed attributes initialized to true.
    let input_event = dom_event_create(realm, html_event_names_input)
    dom_event_set_bubbles(input_event, true)
    dom_event_set_composed(input_event, true)
    _ = dom_event_target_dispatch_event(this.dom_event_target(), input_event)

    # 5. Fire an event named change at element, with the bubbles attribute initialized to true.
    let change_event = dom_event_create(realm, html_event_names_change)
    dom_event_set_bubbles(change_event, true)
    _ = dom_event_target_dispatch_event(this.dom_event_target(), change_event)

## Ported from HTMLSelectElement.cpp:492 Web::HTML::HTMLSelectElement::send_select_update_notifications.
# https://html.spec.whatwg.org/multipage/form-elements.html#send-select-update-notifications
func html_select_element_send_select_update_notifications(this: HtmlSelectElement*):
    # To send select update notifications for a select element element, queue an element task on
    # the user interaction task source given element to run these steps:
    let captures = new HtmlSelectElementSendSelectUpdateNotificationsTask(this = this) else trap("out of memory")
    _ = dom_element_queue_an_element_task(this.dom_element(), HtmlTaskSource.user_interaction, ak.Function0[unit](call = html_select_element_send_select_update_notifications_task, context = captures))

## Ported from HTMLSelectElement.cpp:519 Web::HTML::HTMLSelectElement::set_is_open.
pub func html_select_element_set_is_open(this: HtmlSelectElement*, open: bool):
    if open == this.m_is_open:
        return

    this.m_is_open = open
    css_invalidation_invalidate_style_after_select_open_state_change(this.dom_element())

## Ported from HTMLSelectElement.cpp:528 Web::HTML::HTMLSelectElement::has_activation_behavior (override).
pub func html_select_element_has_activation_behavior_impl(base: DomEventTarget*) -> bool:
    _ = base
    return true

## A SelectItemOption of an option element: `SelectItemOption { id_counter++, ... }`.
func html_select_element_select_item_option(id: u32, option_element: HtmlOptionElement*) -> HtmlSelectItemOption:
    let label = html_option_element_label(option_element)
    let value = html_option_element_value(option_element)
    return HtmlSelectItemOption(id = id, selected = html_option_element_selected(option_element), disabled = html_option_element_disabled(option_element), option_element = option_element, label = web_infra.strip_and_collapse_whitespace_string_view(ak.string_bytes_as_string_view(&label)) else trap("MUST: strip_and_collapse_whitespace"), value = ak.utf16_string_base_to_utf8_but_should_be_ported_to_utf16(&value.utf16_string_base))

## Ported from HTMLSelectElement.cpp:534 Web::HTML::HTMLSelectElement::show_the_picker_if_applicable.
# https://html.spec.whatwg.org/multipage/input.html#show-the-picker,-if-applicable
func html_select_element_show_the_picker_if_applicable(this: HtmlSelectElement*):
    # FIXME: Deduplicate with HTMLInputElement
    # To show the picker, if applicable for a select element element:

    # 1. If element's relevant global object does not have transient activation, then return.
    let relevant_global = as_html_window((gc.Cell*)html_relevant_global_object((JsObject*)this))
    if not html_window_has_transient_activation(relevant_global):
        return

    # 2. If element is not mutable, then return.
    if not html_select_element_is_mutable_impl(html_select_element_form_associated(this)):
        return

    # 3. Consume user activation given element's relevant global object.
    html_window_consume_user_activation(relevant_global)

    # 4. If element does not support a picker, then return.
    # NB: Select elements always support a picker.

    # 5. If element is an input element and element's type attribute is in the File Upload state, then run these steps
    #    in parallel:
    # NB: Not applicable to select elements.

    # 6. Otherwise, the user agent should show the relevant user interface for selecting a value for element, in the
    #    way it normally would when the user interacts with the control.
    #    When showing such a user interface, it must respect the requirements stated in the relevant parts of the
    #    specification for how element behaves given its type attribute state. (For example, various sections describe
    #    restrictions on the resulting value string.)
    #    This step can have side effects, such as closing other pickers that were previously shown by this algorithm.
    #    (If this closes a file selection picker, then per the above that will lead to firing either input and change
    #    events, or a cancel event.)

    # Populate select items
    ak.vector_clear(&this.m_select_items)
    var id_counter: u32 = 1
    var children = dom_node_children_as_vector(this.dom_node())
    for maybe_child in ak.vector_span(&children):
        let child = maybe_child else continue
        if let opt_group_element = as_if_html_opt_group_element(child):
            if not dom_element_has_attribute(opt_group_element.dom_element(), html_attribute_names_hidden):
                var option_group_items = ak.Vector[HtmlSelectItemOption]()
                var group_children = dom_node_children_as_vector(opt_group_element.dom_node())
                for maybe_group_child in ak.vector_span(&group_children):
                    let group_child = maybe_group_child else continue
                    if let option_element = as_if_html_option_element(group_child):
                        if not dom_element_has_attribute(option_element.dom_element(), html_attribute_names_hidden):
                            ak.vector_append_t(&option_group_items, html_select_element_select_item_option(id_counter, option_element))
                            id_counter += 1
                let group_label = dom_element_get_attribute(opt_group_element.dom_element(), html_attribute_names_label) else ak.string_new()
                ak.vector_append_t(&this.m_select_items, .html_select_item_option_group(HtmlSelectItemOptionGroup(label = group_label, items = option_group_items)))

        if let option_element = as_if_html_option_element(child):
            if not dom_element_has_attribute(option_element.dom_element(), html_attribute_names_hidden):
                ak.vector_append_t(&this.m_select_items, .html_select_item_option(html_select_element_select_item_option(id_counter, option_element)))
                id_counter += 1

        if let hr_element = as_if_html_htmlhr_element(child):
            if not dom_element_has_attribute(hr_element.dom_element(), html_attribute_names_hidden):
                ak.vector_append_t(&this.m_select_items, .html_select_item_separator(HtmlSelectItemSeparator()))

    # Request select dropdown
    let weak_element = gc.make_weak(this)
    let rect = dom_element_get_bounding_client_rect(this.dom_element())
    let document = dom_node_document(this.dom_node())
    let navigable = dom_document_navigable(document) else trap("VERIFY: GC::Ptr<Navigable>::operator-> on null")
    let position = html_navigable_to_top_level_position(navigable, gfx.CssPixelPoint(m_x = gfx.css_pixel_rect_x(&rect), m_y = gfx.css_pixel_rect_bottom(&rect)))
    page_did_request_select_dropdown(dom_document_page(document), weak_element, position, gfx.css_pixel_rect_width(&rect), this.m_select_items)
    html_select_element_set_is_open(this, true)

## Ported from HTMLSelectElement.cpp:604 Web::HTML::HTMLSelectElement::show_picker.
# https://html.spec.whatwg.org/multipage/input.html#dom-select-showpicker
pub func html_select_element_show_picker(this: HtmlSelectElement*) -> !:
    let realm = html_element_realm(this.html_element())
    # FIXME: Deduplicate with HTMLInputElement
    # The showPicker() method steps are:

    # 1. If this is not mutable, then throw an "InvalidStateError" DOMException.
    if not html_select_element_is_mutable_impl(html_select_element_form_associated(this)):
        try idl_throw(.idl_dom_exception(idl_invalid_state_error_create(realm, ak.utf16_string_from_str("Element is not mutable"))))

    # 2. If this's relevant settings object's origin is not same origin with this's relevant settings object's top-level origin,
    #    and this is a select element, then throw a "SecurityError" DOMException.
    let settings = html_relevant_settings_object_dom_node(this.dom_node())
    let origin = html_environment_settings_object_origin(settings)
    let top_level_origin = settings.environment.top_level_origin else trap("VERIFY: Optional::value() on an empty optional")
    if not web_url.origin_is_same_origin(&origin, top_level_origin):
        try idl_throw(.idl_dom_exception(idl_security_error_create(realm, ak.utf16_string_from_str("Cross origin pickers are not allowed"))))

    # 3. If this's relevant global object does not have transient activation, then throw a "NotAllowedError" DOMException.
    let global_object = html_relevant_global_object((JsObject*)this)
    if not html_window_has_transient_activation(as_html_window((gc.Cell*)global_object)):
        try idl_throw(.idl_dom_exception(idl_not_allowed_error_create(realm, ak.utf16_string_from_str("Too long since user activation to show picker"))))

    # FIXME: 4. If this is a select element, and this is not being rendered, then throw a "NotSupportedError" DOMException.

    # 5. Show the picker, if applicable, for this.
    html_select_element_show_the_picker_if_applicable(this)

## Ported from HTMLSelectElement.cpp:632 Web::HTML::HTMLSelectElement::activation_behavior (override).
pub func html_select_element_activation_behavior_impl(base: DomEventTarget*, event: DomEvent*):
    if dom_event_is_trusted(event):
        html_select_element_show_the_picker_if_applicable((HtmlSelectElement*)base)

## Ported from HTMLSelectElement.cpp:638 Web::HTML::HTMLSelectElement::did_select_item.
pub func html_select_element_did_select_item(this: HtmlSelectElement*, id: u32?):
    html_select_element_set_is_open(this, false)

    let selected_id = id else return

    html_select_element_update_cached_list_of_options(this)
    for option_element in ak.vector_span(&this.m_cached_list_of_options):
        html_option_element_set_selected(option_element, false)

    for item in &ak.vector_span(&this.m_select_items):
        match *item:
            .html_select_item_option(item_option):
                if item_option.id == selected_id:
                    html_option_element_set_selected(item_option.option_element else trap("VERIFY: an option element"), true)
            .html_select_item_option_group(item_option_group):
                var group_items = item_option_group.items
                for group_item in &ak.vector_span(&group_items):
                    if group_item.id == selected_id:
                        html_option_element_set_selected(group_item.option_element else trap("VERIFY: an option element"), true)
            .html_select_item_separator(separator):
                _ = separator

    html_select_element_clone_selected_option_into_select_button(this)
    html_select_element_send_select_update_notifications(this)

# mark: Insertion, attributes and the shadow tree ===============================================

## Ported from HTMLSelectElement.cpp:668 Web::HTML::HTMLSelectElement::form_associated_element_was_inserted (override).
pub func html_select_element_form_associated_element_was_inserted_impl(base: HtmlFormAssociatedElement*):
    html_select_element_create_shadow_tree_if_needed(html_select_element_from_form_associated_element(base))

## Ported from HTMLSelectElement.cpp:673 Web::HTML::HTMLSelectElement::form_associated_element_attribute_changed (override).
pub func html_select_element_form_associated_element_attribute_changed_impl(base: HtmlFormAssociatedElement*, name: ak.FlyString, old_value: ak.String?, value: ak.String?, namespace_: ak.FlyString?):
    _ = old_value
    _ = namespace_
    let this = html_select_element_from_form_associated_element(base)
    if ak.fly_string_eq_fly_string(&name, &html_attribute_names_multiple):
        # If the multiple attribute is absent then update the selectedness of the option elements.
        if value == none:
            html_select_element_update_selectedness(this)

## Ported from HTMLSelectElement.cpp:683 Web::HTML::HTMLSelectElement::computed_properties_changed (override).
pub func html_select_element_computed_properties_changed_impl(base: DomElement*):
    let this = (HtmlSelectElement*)base
    # Hide chevron icon when appearance is none
    if let chevron_icon_element = this.m_chevron_icon_element:
        let appearance = css_computed_properties_appearance(dom_element_computed_properties(this.dom_element()) else trap("VERIFY: GC::Ptr<ComputedProperties>::operator-> on null"))
        if appearance == css_data.Appearance.none_:
            css_style_properties_set_property_property_id(dom_element_style_for_bindings(chevron_icon_element), css_data.PropertyId.display, ak.sv("none"), ak.sv("")) else trap("MUST: set_property")
        else:
            css_style_properties_set_property_property_id(dom_element_style_for_bindings(chevron_icon_element), css_data.PropertyId.display, ak.sv("block"), ak.sv("")) else trap("MUST: set_property")

## Ported from HTMLSelectElement.cpp:696 Web::HTML::HTMLSelectElement::create_shadow_tree_if_needed.
func html_select_element_create_shadow_tree_if_needed(this: HtmlSelectElement*):
    if dom_element_shadow_root(this.dom_element()) != none:
        return

    let document = dom_node_document(this.dom_node())
    let shadow_root = realm_create_dom_shadow_root(html_element_realm(this.html_element()), document, this.dom_element(), BindShadowRootMode.closed)
    dom_shadow_root_set_user_agent_internal(shadow_root, true)
    dom_element_set_shadow_root(this.dom_element(), shadow_root)

    let border = dom_create_element(document, html_tag_names_div, namespace_html) else trap("FIXME: should propagate errors")
    dom_element_set_attribute_value(border, html_attribute_names_style, ak.string_from_str("\n        display: flex;\n        align-items: center;\n        height: 100%;\n    "))
    html_append_child(shadow_root.dom_node(), border.dom_node())

    let inner_text_element = dom_create_element(document, html_tag_names_div, namespace_html) else trap("FIXME: should propagate errors")
    this.m_inner_text_element = inner_text_element
    dom_element_set_attribute_value(inner_text_element, html_attribute_names_style, ak.string_from_str("\n        flex: 1;\n    "))
    html_append_child(border.dom_node(), inner_text_element.dom_node())

    let chevron_icon_element = dom_create_element(document, html_tag_names_div, namespace_html) else trap("FIXME: should propagate errors")
    this.m_chevron_icon_element = chevron_icon_element
    dom_element_set_attribute_value(chevron_icon_element, html_attribute_names_style, ak.string_from_str("\n        width: 16px;\n        height: 16px;\n        margin-left: 4px;\n    "))

    let chevron_svg_element = dom_create_element(document, svg_tag_names_svg, namespace_svg) else trap("FIXME: should propagate errors")
    dom_element_set_attribute_value(chevron_svg_element, svg_attribute_names_xmlns, ak.fly_string_to_string_const(&namespace_svg))
    dom_element_set_attribute_value(chevron_svg_element, svg_attribute_names_view_box, ak.string_from_str("0 0 24 24"))
    html_append_child(chevron_icon_element.dom_node(), chevron_svg_element.dom_node())

    let chevron_path_element = dom_create_element(document, svg_tag_names_path, namespace_svg) else trap("FIXME: should propagate errors")
    dom_element_set_attribute_value(chevron_path_element, svg_attribute_names_fill, ak.string_from_str("currentcolor"))
    dom_element_set_attribute_value(chevron_path_element, svg_attribute_names_d, ak.string_from_str("M7.41,8.58L12,13.17L16.59,8.58L18,10L12,16L6,10L7.41,8.58Z"))
    html_append_child(chevron_svg_element.dom_node(), chevron_path_element.dom_node())

    html_append_child(border.dom_node(), chevron_icon_element.dom_node())

    html_select_element_clone_selected_option_into_select_button(this)

## Ported from HTMLSelectElement.cpp:742 Web::HTML::HTMLSelectElement::clone_selected_option_into_select_button.
# https://html.spec.whatwg.org/multipage/form-elements.html#clone-selected-option-into-select-button
pub func html_select_element_clone_selected_option_into_select_button(this: HtmlSelectElement*):
    # To clone selected option into select button, given a select element select:

    let inner_text_element = this.m_inner_text_element else return

    html_select_element_update_cached_list_of_options(this)

    # 1. Let option be the first element of select's option list whose selectedness is set to true,
    #    if such an element exists; otherwise null.
    var option: HtmlOptionElement*? = none
    for candidate in ak.vector_span(&this.m_cached_list_of_options):
        if html_option_element_selected(candidate):
            option = candidate
            break

    # 2. Let text be the empty string.
    var text = ak.utf16_string_new()

    # 3. If option is not null, then set text to option's label.
    if let selected_option = option:
        let label = html_option_element_label(selected_option)
        text = ak.utf16_string_from_utf8_string(&label)

    # 4. Set select's select fallback button text to text.
    dom_node_string_replace_all(inner_text_element.dom_node(), text)

# mark: The selectedness setting algorithm ======================================================

## Ported from HTMLSelectElement.cpp:768 Web::HTML::HTMLSelectElement::update_selectedness.
# https://html.spec.whatwg.org/multipage/form-elements.html#selectedness-setting-algorithm
# https://whatpr.org/html/11890/form-elements.html#selectedness-setting-algorithm
pub func html_select_element_update_selectedness(this: HtmlSelectElement*):
    # The selectedness setting algorithm, given a select element element, is to run the following steps:
    html_select_element_update_cached_list_of_options(this)

    # 1. Let updateSelectedcontent be false.
    var should_update_selectedcontent = false

    # 2. If element 's multiple attribute is absent, and element's display size is 1,
    #    and no option elements in the element's list of options have their selectedness set to true, then
    if not dom_element_has_attribute(this.dom_element(), html_attribute_names_multiple) and html_select_element_display_size(this) == 1 and this.m_cached_number_of_selected_options == 0:
        # 1. Set the selectedness of the first option element in the list of options in tree order
        #    that is not disabled, if any, to true.
        for option_element in ak.vector_span(&this.m_cached_list_of_options):
            if not html_option_element_disabled(option_element):
                html_option_element_set_selected_internal(option_element, true)
                break

        # 2. Set updateSelectedcontent to true.
        should_update_selectedcontent = true
    # Otherwise, if element's multiple attribute is absent,
    # and two or more option elements in element's list of options have their selectedness set to true, then:
    elif not dom_element_has_attribute(this.dom_element(), html_attribute_names_multiple) and this.m_cached_number_of_selected_options >= 2:
        # 1. Set the selectedness of all but the last option element with its selectedness set to true
        #    in the list of options in tree order to false.
        var last_selected_option: HtmlOptionElement*? = none
        var last_selected_option_update_index: u64 = 0

        for option_element in ak.vector_span(&this.m_cached_list_of_options):
            if not html_option_element_selected(option_element):
                continue
            if last_selected_option == none or html_option_element_selectedness_update_index(option_element) > last_selected_option_update_index:
                last_selected_option = option_element
                last_selected_option_update_index = html_option_element_selectedness_update_index(option_element)

        for option_element in ak.vector_span(&this.m_cached_list_of_options):
            if option_element != last_selected_option:
                html_option_element_set_selected_internal(option_element, false)

        # 2. Set updateSelectedcontent to true.
        should_update_selectedcontent = true

    # 4. If updateSelectedcontent is true, then run update a select's selectedcontent given element.
    if should_update_selectedcontent:
        html_select_element_update_selectedcontent(this) else trap("MUST: update_selectedcontent")

    # AD-HOC: The selectedness setting algorithm does not itself refresh the select's fallback button text, but the
    #         set of selected options may have changed. Run the spec's "clone selected option into select button"
    #         algorithm so the button stays in sync.
    html_select_element_clone_selected_option_into_select_button(this)

## Ported from HTMLSelectElement.cpp:828 Web::HTML::HTMLSelectElement::is_focusable (override).
pub func html_select_element_is_focusable_impl(base: DomEventTarget*) -> bool:
    return html_form_associated_element_enabled(html_select_element_form_associated((HtmlSelectElement*)base))

## Ported from HTMLSelectElement.cpp:834 Web::HTML::HTMLSelectElement::placeholder_label_option.
# https://html.spec.whatwg.org/multipage/form-elements.html#placeholder-label-option
pub func html_select_element_placeholder_label_option(this: HtmlSelectElement*) -> HtmlOptionElement*?:
    # If a select element has a required attribute specified, does not have a multiple attribute specified, and has a display size of 1;
    if dom_element_has_attribute(this.dom_element(), html_attribute_names_required) and not dom_element_has_attribute(this.dom_element(), html_attribute_names_multiple) and html_select_element_display_size(this) == 1:
        # and if the value of the first option element in the
        # select element's list of options (if any) is the empty string, and that option element's parent node is the select element (and not an optgroup element), then that option is the
        # select element's placeholder label option.
        var list_of_options = html_select_element_list_of_options(this)
        let first_option_element = ak.vector_span(&list_of_options)[0] else trap("VERIFY: GC::Root<HTMLOptionElement> is null")
        let first_value = html_option_element_value(first_option_element)
        if html_utf16_is_empty(&first_value) and first_option_element.dom_node().tree_node.m_parent == this.dom_node():
            return first_option_element
    return none

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

## Ported from HTMLSelectElement.cpp:849 Web::HTML::HTMLSelectElement::enabled_selectedcontent.
# https://html.spec.whatwg.org/multipage/form-elements.html#select-enabled-selectedcontent
pub func html_select_element_enabled_selectedcontent(this: HtmlSelectElement*) -> HtmlSelectedContentElement*?:
    # To get a select's enabled selectedcontent given a select element select:

    # 1. If select has the multiple attribute, then return null.
    if dom_element_has_attribute(this.dom_element(), html_attribute_names_multiple):
        return none

    # 2. Let selectedcontent be the first selectedcontent element descendant of select in tree order if any such
    #    element exists; otherwise return null.
    var selectedcontent: HtmlSelectedContentElement*? = none
    for node in dom_node_subtree(this.dom_node()):
        if let element = as_if_html_selected_content_element(node):
            selectedcontent = element
            break
    let found = selectedcontent else return none

    # 3. If selectedcontent is disabled, then return null.
    if html_selected_content_element_disabled(found):
        return none

    # 4. Return selectedcontent.
    return found

## Ported from HTMLSelectElement.cpp:876 Web::HTML::HTMLSelectElement::clear_non_primary_selectedcontent.
# https://html.spec.whatwg.org/multipage/form-elements.html#clear-a-select%27s-non-primary-selectedcontent-elements
pub func html_select_element_clear_non_primary_selectedcontent(this: HtmlSelectElement*):
    # To clear a select's non-primary selectedcontent elements, given a select element select:

    # 1. Let passedFirstSelectedcontent be false.
    var passed_first_selectedcontent = false

    # 2. For each descendant of select's descendants in tree order that is a selectedcontent element:
    for node in dom_node_subtree(this.dom_node()):
        let element = as_if_html_selected_content_element(node) else continue
        # 1. If passedFirstSelectedcontent is false, then set passedFirstSelectedcontent to true.
        if not passed_first_selectedcontent:
            passed_first_selectedcontent = true
        # 2. Otherwise, run clear a selectedcontent given descendant.
        else:
            html_selected_content_element_clear_selectedcontent(element)

## Ported from HTMLSelectElement.cpp:897 Web::HTML::HTMLSelectElement::update_selectedcontent.
# https://html.spec.whatwg.org/multipage/form-elements.html#update-a-select%27s-selectedcontent
pub func html_select_element_update_selectedcontent(this: HtmlSelectElement*) -> !:
    # To update a select's selectedcontent given a select element select:

    # 1. Let selectedcontent be the result of get a select's enabled selectedcontent given select.
    # 2. If selectedcontent is null, then return.
    let selectedcontent = html_select_element_enabled_selectedcontent(this) else return

    # 3. Let option be the first option in select's list of options whose selectedness is true,
    #    if any such option exists, otherwise null.
    html_select_element_update_cached_list_of_options(this)
    var option: HtmlOptionElement*? = none
    for candidate in ak.vector_span(&this.m_cached_list_of_options):
        if html_option_element_selected(candidate):
            option = candidate
            break

    # 4. If option is null, then run clear a selectedcontent given selectedcontent.
    if option == none:
        html_selected_content_element_clear_selectedcontent(selectedcontent)
        return

    # 5. Otherwise, run clone an option into a selectedcontent given option and selectedcontent.
    try html_option_element_clone_into_selectedcontent(option else trap("unreachable"), selectedcontent)

# mark: Constraint validation ===================================================================

## Ported from HTMLSelectElement.cpp:931 Web::HTML::HTMLSelectElement::suffering_from_being_missing (override).
# https://html.spec.whatwg.org/multipage/form-elements.html#the-select-element%3Asuffering-from-being-missing
pub func html_select_element_suffering_from_being_missing_impl(base: HtmlFormAssociatedElement*) -> bool:
    let this = html_select_element_from_form_associated_element(base)
    # If the element has its required attribute specified, and either none of the option elements in the select element's list of options have their selectedness
    # set to true, or the only option element in the select element's list of options with its selectedness set to true is the placeholder label option, then the element is suffering from being
    # missing.
    let selected_options = html_select_element_selected_options(this)
    return dom_element_has_attribute(this.dom_element(), html_attribute_names_required) and (dom_html_collection_length(selected_options) == 0 or (dom_html_collection_length(selected_options) == 1 and dom_html_collection_item(selected_options, 0) == (DomElement*?)html_select_element_placeholder_label_option(this)))

## Ported from HTMLSelectElement.cpp:941 Web::HTML::HTMLSelectElement::is_mutable (override).
# https://html.spec.whatwg.org/multipage/form-elements.html#the-select-element:concept-fe-mutable
pub func html_select_element_is_mutable_impl(base: HtmlFormAssociatedElement*) -> bool:
    # A select element that is not disabled is mutable.
    return html_form_associated_element_enabled(base)

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

## Ported from HTMLSelectElement.h:26 Web::HTML::HTMLSelectElement::autocomplete_element_to_html_element (override).
pub func html_select_element_autocomplete_element_to_html_element_impl(base: HtmlAutocompleteElement*) -> HtmlElement*:
    return (HtmlElement*)((u8*)base - memory.offset_of(HtmlSelectElement, autocomplete_element))

## Ported from HTMLSelectElement.h:31 Web::HTML::HTMLSelectElement::is_html_select_element (override).
pub func html_select_element_is_html_select_element_impl(base: DomNode*) -> bool:
    _ = base
    return true

## Ported from HTMLSelectElement.h:51 Web::HTML::HTMLSelectElement::selected_options (const).
pub func html_select_element_selected_options_const(this: HtmlSelectElement*) -> DomHtmlCollection*:
    return html_select_element_selected_options(this)

## Ported from HTMLSelectElement.h:57 Web::HTML::HTMLSelectElement::form_value (override).
pub func html_select_element_form_value_impl(base: HtmlFormAssociatedElement*) -> ak.Utf16String:
    return html_select_element_value(html_select_element_from_form_associated_element(base))

## Ported from HTMLSelectElement.h:60 Web::HTML::HTMLSelectElement::is_open.
pub func html_select_element_is_open(this: HtmlSelectElement*) -> bool:
    return this.m_is_open

## Ported from HTMLSelectElement.h:74 Web::HTML::HTMLSelectElement::is_form_associated_element (override).
pub func html_select_element_is_form_associated_element_impl(base: HtmlFormAssociatedElement*) -> bool:
    _ = base
    return true

## Ported from HTMLSelectElement.h:78 Web::HTML::HTMLSelectElement::is_listed (override).
# https://html.spec.whatwg.org/multipage/forms.html#category-listed
pub func html_select_element_is_listed_impl(base: HtmlFormAssociatedElement*) -> bool:
    _ = base
    return true

## Ported from HTMLSelectElement.h:81 Web::HTML::HTMLSelectElement::is_submittable (override).
# https://html.spec.whatwg.org/multipage/forms.html#category-submit
pub func html_select_element_is_submittable_impl(base: HtmlFormAssociatedElement*) -> bool:
    _ = base
    return true

## Ported from HTMLSelectElement.h:84 Web::HTML::HTMLSelectElement::is_resettable (override).
# https://html.spec.whatwg.org/multipage/forms.html#category-reset
pub func html_select_element_is_resettable_impl(base: HtmlFormAssociatedElement*) -> bool:
    _ = base
    return true

## Ported from HTMLSelectElement.h:87 Web::HTML::HTMLSelectElement::is_autocapitalize_and_autocorrect_inheriting (override).
# https://html.spec.whatwg.org/multipage/forms.html#category-autocapitalize
pub func html_select_element_is_autocapitalize_and_autocorrect_inheriting_impl(base: HtmlFormAssociatedElement*) -> bool:
    _ = base
    return true

## Ported from HTMLSelectElement.h:91 Web::HTML::HTMLSelectElement::is_labelable (override).
# https://html.spec.whatwg.org/multipage/forms.html#category-label
pub func html_select_element_is_labelable_impl(base: HtmlElement*) -> bool:
    _ = base
    return true

## Ported from HTMLSelectElement.h:113 Web::HTML::HTMLSelectElement::user_validity.
pub func html_select_element_user_validity(this: HtmlSelectElement*) -> bool:
    return this.m_user_validity

## Ported from HTMLSelectElement.h:114 Web::HTML::HTMLSelectElement::set_user_validity.
pub func html_select_element_set_user_validity(this: HtmlSelectElement*, flag: bool):
    this.m_user_validity = flag