Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.0

0.6.0 / src / web / html / form_associated_element_1.lucb

#==============================================================================================
#
#   form_associated_element_1 - HTML::FormAssociatedElement: construction, categories
#                               and the form owner
#
#   DESCRIPTION:
#       Port of LibWeb/HTML/FormAssociatedElement.cpp (lines 1-303) and FormAssociatedElement.h:
#       the members' initializers, ValidityStateFlags, the listed / submittable / resettable
#       categories, the reset algorithm, the form owner (set_form, reset the form owner and the
#       insertion, removal, move and attribute steps that reset it), validity, setCustomValidity,
#       enabled, and the disabled state of form-associated custom elements.
#
#       FormAssociatedElement is a stateful mixin embedded in every HTMLElement (DESIGN.md
#       §2.5): its virtuals dispatch through the table of the class that owns it, and the owning
#       element is reached through the virtual form_associated_element_to_html_element.
#
#==============================================================================================

# mark: Helpers =================================================================================

## `form_associated_element_to_html_element()`: the HTML element this mixin lives in.
func html_form_associated_element_html_element(this: HtmlFormAssociatedElement*) -> HtmlElement*:
    return html_form_associated_element_form_associated_element_to_html_element(this)

## `Optional<FlyString> == Optional<String>` (AK::Optional's operator==): both empty, or both
## present with equal strings.
func html_optional_fly_string_eq_optional_string(a: ak.FlyString?, b: ak.String?) -> bool:
    if let a_value = a:
        let b_value = b else return false
        return ak.string_eq_fly_string(&b_value, &a_value)
    return b == none

## `name == HTML::AttributeNames::...` on FlyStrings.
func html_form_name_is(name: ak.FlyString, attribute_name: ak.FlyString) -> bool:
    return ak.fly_string_eq_fly_string(&name, &attribute_name)

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

## FormAssociatedElement.h:167 FormAssociatedElement::FormAssociatedElement() = default: the
## members' initializers (the cell is zeroed, and so are its empty Strings; the two
## FACESubmissionValue variants start as Empty). HTMLElement's constructor runs it.
pub func html_form_associated_element_construct(this: HtmlFormAssociatedElement*):
    this.m_face_submission_value = .empty
    this.m_face_state = .empty

# mark: ValidityStateFlags and selection directions =============================================

## Ported from FormAssociatedElement.h:34 Web::HTML::ValidityStateFlags::has_one_or_more_true_values.
pub func html_validity_state_flags_has_one_or_more_true_values(this: const HtmlValidityStateFlags*) -> bool:
    return this.value_missing or this.type_mismatch or this.pattern_mismatch or this.too_long or this.too_short or this.range_underflow or this.range_overflow or this.step_mismatch or this.bad_input or this.custom_error

## Ported from FormAssociatedElement.cpp:38 Web::HTML::string_to_selection_direction.
func html_string_to_selection_direction(value: ak.String?) -> HtmlSelectionDirection:
    let present = value else return HtmlSelectionDirection.none_
    if ak.string_eq_string_view(&present, ak.sv("forward")):
        return HtmlSelectionDirection.forward
    if ak.string_eq_string_view(&present, ak.sv("backward")):
        return HtmlSelectionDirection.backward
    return HtmlSelectionDirection.none_

# mark: Categories ==============================================================================

## Ported from FormAssociatedElement.cpp:50 Web::HTML::FormAssociatedElement::is_form_associated_element (virtual).
# https://html.spec.whatwg.org/multipage/forms.html#form-associated-element
pub func html_form_associated_element_is_form_associated_element_impl(this: HtmlFormAssociatedElement*) -> bool:
    return html_element_is_form_associated_custom_element(html_form_associated_element_html_element(this))

## Ported from FormAssociatedElement.cpp:56 Web::HTML::FormAssociatedElement::is_listed (virtual).
# https://html.spec.whatwg.org/multipage/forms.html#category-listed
pub func html_form_associated_element_is_listed_impl(this: HtmlFormAssociatedElement*) -> bool:
    return html_element_is_form_associated_custom_element(html_form_associated_element_html_element(this))

## Ported from FormAssociatedElement.cpp:62 Web::HTML::FormAssociatedElement::is_submittable (virtual).
# https://html.spec.whatwg.org/multipage/forms.html#category-submit
pub func html_form_associated_element_is_submittable_impl(this: HtmlFormAssociatedElement*) -> bool:
    return html_element_is_form_associated_custom_element(html_form_associated_element_html_element(this))

## Ported from FormAssociatedElement.cpp:68 Web::HTML::FormAssociatedElement::is_resettable (virtual).
# https://html.spec.whatwg.org/multipage/forms.html#category-reset
pub func html_form_associated_element_is_resettable_impl(this: HtmlFormAssociatedElement*) -> bool:
    return html_element_is_form_associated_custom_element(html_form_associated_element_html_element(this))

## Ported from FormAssociatedElement.cpp:74 Web::HTML::FormAssociatedElement::reset_algorithm (virtual).
# https://html.spec.whatwg.org/multipage/custom-elements.html#custom-elements-core-concepts%3Aconcept-form-reset-control
pub func html_form_associated_element_reset_algorithm_impl(this: HtmlFormAssociatedElement*):
    # The reset algorithm for form-associated custom elements is to enqueue a custom element callback reaction with
    # the element, callback name "formResetCallback", and « ».
    let html_element = html_form_associated_element_html_element(this)
    if not html_element_is_form_associated_custom_element(html_element):
        return

    var empty_arguments: ak.Vector[JsValue]
    dom_element_enqueue_a_custom_element_callback_reaction(html_element.dom_element(), html_custom_element_reaction_names_form_reset_callback, empty_arguments)

# mark: The form owner ==========================================================================

## Ported from FormAssociatedElement.h:54 Web::HTML::FormAssociatedElement::form.
pub func html_form_associated_element_form(this: HtmlFormAssociatedElement*) -> HtmlFormElement*?:
    return gc.weak_ptr(&this.m_form)

## Ported from FormAssociatedElement.h:55 Web::HTML::FormAssociatedElement::form.
pub func html_form_associated_element_form_const(this: HtmlFormAssociatedElement*) -> HtmlFormElement*?:
    return gc.weak_ptr(&this.m_form)

## Ported from FormAssociatedElement.cpp:86 Web::HTML::FormAssociatedElement::set_form.
pub func html_form_associated_element_set_form(this: HtmlFormAssociatedElement*, form: HtmlFormElement*?):
    if let old_form = gc.weak_ptr(&this.m_form):
        html_form_element_remove_associated_element(old_form, html_form_associated_element_html_element(this))
    gc.weak_assign(&this.m_form, form)
    if let new_form = gc.weak_ptr(&this.m_form):
        html_form_element_add_associated_element(new_form, html_form_associated_element_html_element(this))

## Ported from FormAssociatedElement.cpp:96 Web::HTML::FormAssociatedElement::validity.
# https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#dom-cva-validity
pub func html_form_associated_element_validity(this: HtmlFormAssociatedElement*) -> HtmlValidityState*:
    let realm = bind_platform_object_realm(html_form_associated_element_html_element(this).bind_platform_object())
    return realm_create_html_validity_state(realm, realm, this)

## Ported from FormAssociatedElement.cpp:103 Web::HTML::FormAssociatedElement::set_custom_validity.
# https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#dom-cva-setcustomvalidity
pub func html_form_associated_element_set_custom_validity(this: HtmlFormAssociatedElement*, error_: ak.String*):
    # The setCustomValidity(error) method steps are:

    # 1. Set error to the result of normalizing newlines given error.
    *error_ = web_infra.normalize_newlines_string(*error_)

    # 2. Set the custom validity error message to error.
    this.m_custom_validity_error_message = *error_

## Ported from FormAssociatedElement.cpp:114 Web::HTML::FormAssociatedElement::enabled.
pub func html_form_associated_element_enabled(this: HtmlFormAssociatedElement*) -> bool:
    # https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#concept-fe-disabled
    let html_element = html_form_associated_element_html_element(this)

    # A form control is disabled if any of the following are true:
    # - The element is a button, input, select, textarea, or form-associated custom element, and the disabled attribute is specified on this element (regardless of its value); or
    if (is_html_button_element(html_element) or is_html_input_element(html_element) or is_html_select_element(html_element) or is_html_text_area_element(html_element) or html_element_is_form_associated_custom_element(html_form_associated_element_html_element(this))) and dom_element_has_attribute(html_element.dom_element(), html_attribute_names_disabled):
        return false

    # - The element is a descendant of a fieldset element whose disabled attribute is specified, and is not a descendant of that fieldset element's first legend element child, if any.
    var fieldset_ancestor = tree_node_first_ancestor_of_type(html_element.dom_node(), &html_field_set_element_class)
    while let fieldset = fieldset_ancestor:
        if dom_element_has_attribute((DomElement*)fieldset, html_attribute_names_disabled):
            if let first_legend_element_child = tree_node_first_child_of_type(fieldset, &html_legend_element_class):
                if not tree_node_is_descendant_of(html_element.dom_node(), first_legend_element_child):
                    return false
            else:
                return false
        fieldset_ancestor = tree_node_first_ancestor_of_type(fieldset, &html_field_set_element_class)

    return true

## Ported from FormAssociatedElement.cpp:136 Web::HTML::FormAssociatedElement::set_parser_inserted.
pub func html_form_associated_element_set_parser_inserted(this: HtmlFormAssociatedElement*):
    this.m_parser_inserted = true

## Ported from FormAssociatedElement.cpp:142 Web::HTML::FormAssociatedElement::form_node_was_inserted.
# https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#association-of-controls-and-forms:nodes-are-inserted
pub func html_form_associated_element_form_node_was_inserted(this: HtmlFormAssociatedElement*):
    # 1. If the form-associated element's parser inserted flag is set, then return.
    if this.m_parser_inserted:
        return

    # 2. Reset the form owner of the form-associated element.
    html_form_associated_element_reset_form_owner(this)

## Ported from FormAssociatedElement.cpp:153 Web::HTML::FormAssociatedElement::form_node_was_removed.
# https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#association-of-controls-and-forms:nodes-are-removed
pub func html_form_associated_element_form_node_was_removed(this: HtmlFormAssociatedElement*):
    # 1. If the form-associated element has a form owner and the form-associated element and its form owner are no longer in the same tree, then reset the form owner of the form-associated element.
    if let form = gc.weak_ptr(&this.m_form):
        if tree_node_root(html_form_associated_element_html_element(this).dom_node()) != tree_node_root(form.dom_node()):
            html_form_associated_element_reset_form_owner(this)

## Ported from FormAssociatedElement.cpp:161 Web::HTML::FormAssociatedElement::form_node_was_moved.
# https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#association-of-controls-and-forms:attr-fae-form-2
pub func html_form_associated_element_form_node_was_moved(this: HtmlFormAssociatedElement*):
    # When a listed form-associated element's form attribute is set, changed, or removed, then the user agent must reset the form owner of that element.
    if let form = gc.weak_ptr(&this.m_form):
        if tree_node_root(html_form_associated_element_html_element(this).dom_node()) != tree_node_root(form.dom_node()):
            html_form_associated_element_reset_form_owner(this)

## Ported from FormAssociatedElement.cpp:169 Web::HTML::FormAssociatedElement::form_node_attribute_changed.
# https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#association-of-controls-and-forms:category-listed-3
pub func html_form_associated_element_form_node_attribute_changed(this: HtmlFormAssociatedElement*, name: ak.FlyString, value: ak.String?):
    # When a listed form-associated element's form attribute is set, changed, or removed, then the user agent must
    # reset the form owner of that element.
    if html_form_name_is(name, html_attribute_names_form):
        let html_element = html_form_associated_element_html_element(this)

        if value != none:
            dom_document_add_form_associated_element_with_form_attribute(html_element_document(html_element), this)
        else:
            dom_document_remove_form_associated_element_with_form_attribute(html_element_document(html_element), this)

        html_form_associated_element_reset_form_owner(this)

## Ported from FormAssociatedElement.cpp:186 Web::HTML::FormAssociatedElement::element_id_changed.
# https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#association-of-controls-and-forms:category-listed-4
pub func html_form_associated_element_element_id_changed(this: HtmlFormAssociatedElement*):
    # When a listed form-associated element has a form attribute and the ID of any of the elements in the tree changes,
    # then the user agent must reset the form owner of that form-associated element.
    assert(dom_element_has_attribute(html_form_associated_element_html_element(this).dom_element(), html_attribute_names_form))
    html_form_associated_element_reset_form_owner(this)

## Ported from FormAssociatedElement.cpp:195 Web::HTML::FormAssociatedElement::element_with_id_was_added_or_removed.
# https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#association-of-controls-and-forms:category-listed-5
pub func html_form_associated_element_element_with_id_was_added_or_removed(this: HtmlFormAssociatedElement*):
    # When a listed form-associated element has a form attribute and an element with an ID is inserted into or removed
    # from the Document, then the user agent must reset the form owner of that form-associated element.
    assert(dom_element_has_attribute(html_form_associated_element_html_element(this).dom_element(), html_attribute_names_form))
    html_form_associated_element_reset_form_owner(this)

## Ported from FormAssociatedElement.cpp:204 Web::HTML::FormAssociatedElement::reset_form_owner.
# https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#reset-the-form-owner
pub func html_form_associated_element_reset_form_owner(this: HtmlFormAssociatedElement*):
    let html_element = html_form_associated_element_html_element(this)

    # 1. Unset element's parser inserted flag.
    this.m_parser_inserted = false

    # 2. If all of the following conditions are true
    #    - element's form owner is not null
    #    - element is not listed or its form content attribute is not present
    #    - element's form owner is its nearest form element ancestor after the change to the ancestor chain
    #    then do nothing, and return.
    if let form = gc.weak_ptr(&this.m_form):
        if (not html_form_associated_element_is_listed(this) or not dom_element_has_attribute(html_element.dom_element(), html_attribute_names_form)) and tree_node_first_ancestor_of_type(html_element.dom_node(), &html_form_element_class) == form.dom_node():
            return

    # AD-HOC: The spec states:
    #         - When the user agent resets the form owner of a form-associated custom element and doing so changes the
    #           form owner, its formAssociatedCallback is called, given the new form owner (or null if no owner) as an
    #           argument.
    #         However, this is not specified as algorithmic steps, so we have to do this ourselves.
    #         Spec issue: https://github.com/whatwg/html/issues/12169
    let old_form = gc.weak_ptr(&this.m_form)

    # 3. Set element's form owner to null.
    html_form_associated_element_set_form(this, none)

    # 4. If element is listed, has a form content attribute, and is connected, then:
    if html_form_associated_element_is_listed(this) and dom_element_has_attribute(html_element.dom_element(), html_attribute_names_form) and dom_node_is_connected(html_element.dom_node()):
        # 1. If the first element in element's tree, in tree order, to have an ID that is identical to element's form content attribute's value, is a form element, then associate the element with that form element.
        let form_value = dom_element_attribute(html_element.dom_element(), html_attribute_names_form)
        for node in dom_node_inclusive_subtree(tree_node_root(html_element.dom_node())):
            let element = as_if_html_element(node) else continue
            if html_optional_fly_string_eq_optional_string(dom_element_id(element.dom_element()), form_value):
                if is_html_form_element(element):
                    html_form_associated_element_set_form(this, as_html_form_element(element))
                break

    # 5. Otherwise, if element has an ancestor form element, then associate element with the nearest such ancestor form element.
    else:
        let form_ancestor = (HtmlFormElement*?)tree_node_first_ancestor_of_type(html_element.dom_node(), &html_form_element_class)
        if let form = form_ancestor:
            html_form_associated_element_set_form(this, form)

    # See the AD-HOC comment above.
    if gc.weak_ptr(&this.m_form) != old_form and html_element_is_form_associated_custom_element(html_element):
        var arguments: ak.Vector[JsValue]
        ak.vector_append_t(&arguments, js_value_from_object((JsObject*?)gc.weak_ptr(&this.m_form)))
        dom_element_enqueue_a_custom_element_callback_reaction(html_element.dom_element(), html_custom_element_reaction_names_form_associated_callback, arguments)

## Ported from FormAssociatedElement.cpp:263 Web::HTML::FormAssociatedElement::form_associated_element_was_inserted (virtual).
pub func html_form_associated_element_form_associated_element_was_inserted_impl(this: HtmlFormAssociatedElement*):
    html_form_associated_element_update_face_disabled_state(this)

## Ported from FormAssociatedElement.cpp:268 Web::HTML::FormAssociatedElement::form_associated_element_was_removed (virtual).
pub func html_form_associated_element_form_associated_element_was_removed_impl(this: HtmlFormAssociatedElement*, arg0: DomNode*?):
    html_form_associated_element_update_face_disabled_state(this)

## Ported from FormAssociatedElement.cpp:273 Web::HTML::FormAssociatedElement::form_associated_element_was_moved (virtual).
pub func html_form_associated_element_form_associated_element_was_moved_impl(this: HtmlFormAssociatedElement*, arg0: DomNode*?):
    html_form_associated_element_update_face_disabled_state(this)

## Ported from FormAssociatedElement.cpp:278 Web::HTML::FormAssociatedElement::form_associated_element_attribute_changed (virtual).
pub func html_form_associated_element_form_associated_element_attribute_changed_impl(this: HtmlFormAssociatedElement*, name: ak.FlyString, arg1: ak.String?, arg2: ak.String?, arg3: ak.FlyString?):
    if html_form_name_is(name, html_attribute_names_disabled):
        html_form_associated_element_update_face_disabled_state(this)

## Ported from FormAssociatedElement.cpp:288 Web::HTML::FormAssociatedElement::update_face_disabled_state.
# AD-HOC: The specification states that when the disabled state of a form-associated custom element is changed,
#         its formDisabledCallback should be enqueued. However, there are no explicit algorithmic steps for this
#         outside of "upgrade an element". We track the disabled state and enqueue the callback when it changes.
#         See: https://github.com/whatwg/html/issues/12169
pub func html_form_associated_element_update_face_disabled_state(this: HtmlFormAssociatedElement*):
    let html_element = html_form_associated_element_html_element(this)
    if not html_element_is_form_associated_custom_element(html_element):
        return

    let is_disabled = not html_form_associated_element_enabled(this)
    if is_disabled == this.m_face_disabled_state:
        return

    this.m_face_disabled_state = is_disabled

    var arguments: ak.Vector[JsValue]
    ak.vector_append_t(&arguments, js_value_from_bool(is_disabled))
    dom_element_enqueue_a_custom_element_callback_reaction(html_element.dom_element(), html_custom_element_reaction_names_form_disabled_callback, arguments)

## Ported from FormAssociatedElement.cpp:306 Web::HTML::FormAssociatedElement::clear_algorithm (virtual).
# https://w3c.github.io/webdriver/#dfn-clear-algorithm
pub func html_form_associated_element_clear_algorithm_impl(this: HtmlFormAssociatedElement*):
    # When the clear algorithm is invoked for an element that does not define its own clear algorithm, its reset
    # algorithm must be invoked instead.
    html_form_associated_element_reset_algorithm(this)