Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.0

0.6.0 / src / web / html / tests_html_forms_2.lucb

#==============================================================================================
#
#   tests_html_forms_2 - tests of the autofill processing model, the text control selection,
#                        the form element and the entry list encodings (region r23)
#
#   DESCRIPTION:
#       The expected values of the autofill processing model and of normalizing line breaks
#       come from a local oracle that runs the donor's AutocompleteElement.cpp and
#       FormControlInfrastructure.cpp algorithms against the reference build's AK (never
#       committed). AutocompleteElement is a mixin of input, select and textarea, so its tests
#       give it a test table over a plain HTML element; the text control selection tests run on
#       a real textarea (html/tests_form_controls.lucb has the oracle cases of inputs and
#       textareas).
#
#==============================================================================================

# mark: A test AutocompleteElement ==============================================================

## An AutocompleteElement mixin beside the element it belongs to.
struct TestHtmlAutocomplete:
    var mixin: HtmlAutocompleteElement
    var element: HtmlElement*

## AutocompleteElement::autocomplete_element_to_html_element of the test mixin.
func test_html_autocomplete_to_html_element(this: HtmlAutocompleteElement*) -> HtmlElement*:
    return ((TestHtmlAutocomplete*)this).element

## The table of the test mixin.
let test_html_autocomplete_vtable: HtmlAutocompleteElementVTable = HtmlAutocompleteElementVTable(class_info = &html_element_class, autocomplete_element_to_html_element = test_html_autocomplete_to_html_element)

## The attribute details of an element whose autocomplete attribute is `value` (none: no attribute).
func test_html_autocomplete_parse(document: DomDocument*, value: str?, form_autocomplete_off: bool = false) -> HtmlAutocompleteElementAttributeDetails:
    let element = test_html_create(document, "section")
    if let attribute = value:
        _ = test_html_put(element, html_attribute_names_autocomplete, attribute)
    if form_autocomplete_off:
        let form = as_html_form_element(test_html_create(document, "form"))
        _ = test_html_put(form.html_element(), html_attribute_names_autocomplete, "off")
        html_form_associated_element_set_form(&element.form_associated_element, form)
    var autocomplete = TestHtmlAutocomplete(mixin = HtmlAutocompleteElement(vtable = &test_html_autocomplete_vtable, class_id = element.cell().class_id), element = element)
    return html_autocomplete_element_parse_autocomplete_attribute(&autocomplete.mixin)

## No strings (`none_` in the tests: a span of an array, which cannot be empty).
let test_html_no_strings: str[1] = [""]

## Whether a list of Strings is `expected`.
func test_html_strings_are(strings: const ak.Vector[ak.String]*, expected: const str[]) -> bool:
    if ak.vector_size(strings) != expected.length:
        return false
    for i in 0..<expected.length:
        if not ak.string_eq_string_view(ak.vector_at_const(strings, i), ak.sv(expected[i])):
            return false
    return true

## Whether the details are the oracle's: value, field name, credential type, hint set, scope.
func test_html_autocomplete_is(details: HtmlAutocompleteElementAttributeDetails, value: str, field_name: str, credential_type: str?, hints: const str[], scope: const str[]) -> bool:
    if not ak.string_eq_string_view(&details.value, ak.sv(value)):
        return false
    if not ak.string_eq_string_view(&details.field_name, ak.sv(field_name)):
        return false
    if let expected_credential = credential_type:
        let credential = details.credential_type else return false
        if not ak.string_eq_string_view(&credential, ak.sv(expected_credential)):
            return false
    elif details.credential_type != none:
        return false
    return test_html_strings_are(&details.hint_set, hints) and test_html_strings_are(&details.scope, scope)

# mark: The autofill processing model ===========================================================

test "AutocompleteElement: the autofill processing model (oracle values)":
    let (vm, realm, document) = test_html_setup()
    defer web_test_destroy_vm(vm)
    let none_ = test_html_no_strings[0..<0]

    # Default: no attribute, an empty or whitespace attribute, unknown or too many tokens.
    assert(test_html_autocomplete_is(test_html_autocomplete_parse(document, none), "", "on", none, none_, none_))
    assert(test_html_autocomplete_is(test_html_autocomplete_parse(document, none, form_autocomplete_off = true), "", "off", none, none_, none_))
    assert(test_html_autocomplete_is(test_html_autocomplete_parse(document, ""), "", "on", none, none_, none_))
    assert(test_html_autocomplete_is(test_html_autocomplete_parse(document, "   "), "", "on", none, none_, none_))
    assert(test_html_autocomplete_is(test_html_autocomplete_parse(document, "bogus", form_autocomplete_off = true), "", "off", none, none_, none_))
    assert(test_html_autocomplete_is(test_html_autocomplete_parse(document, "a b c name"), "", "on", none, none_, none_))

    # Off and Automatic.
    assert(test_html_autocomplete_is(test_html_autocomplete_parse(document, "off"), "off", "off", none, none_, none_))
    assert(test_html_autocomplete_is(test_html_autocomplete_parse(document, "ON"), "on", "on", none, none_, none_))

    # Normal and Contact fields with sections, modes and contacts.
    assert(test_html_autocomplete_is(test_html_autocomplete_parse(document, "Given-Name"), "given-name", "Given-Name", none, none_, none_))
    let shipping: str[1] = ["shipping"]
    let section_shipping: str[2] = ["section-foo", "shipping"]
    assert(test_html_autocomplete_is(test_html_autocomplete_parse(document, "section-Foo shipping name"), "section-foo shipping name", "name", none, shipping, section_shipping))
    let work_billing: str[2] = ["work", "billing"]
    let section_billing_work: str[3] = ["section-x", "billing", "work"]
    assert(test_html_autocomplete_is(test_html_autocomplete_parse(document, "Section-x billing Work Tel"), "section-x billing work tel", "Tel", none, work_billing, section_billing_work))
    let home: str[1] = ["home"]
    assert(test_html_autocomplete_is(test_html_autocomplete_parse(document, "home email"), "home email", "email", none, home, home))
    let work_shipping: str[2] = ["work", "shipping"]
    let shipping_work: str[2] = ["shipping", "work"]
    assert(test_html_autocomplete_is(test_html_autocomplete_parse(document, "shipping work email"), "shipping work email", "email", none, work_shipping, shipping_work))
    assert(test_html_autocomplete_is(test_html_autocomplete_parse(document, "work billing email"), "", "on", none, none_, none_))
    assert(test_html_autocomplete_is(test_html_autocomplete_parse(document, "fax name"), "", "on", none, none_, none_))
    assert(test_html_autocomplete_is(test_html_autocomplete_parse(document, "billing shipping name"), "", "on", none, none_, none_))
    assert(test_html_autocomplete_is(test_html_autocomplete_parse(document, "section-1 section-2 name"), "", "on", none, none_, none_))

    # Credentials.
    assert(test_html_autocomplete_is(test_html_autocomplete_parse(document, "email webauthn"), "email webauthn", "webauthn", "webauthn", none_, none_))
    assert(test_html_autocomplete_is(test_html_autocomplete_parse(document, "WEBAUTHN"), "webauthn", "WEBAUTHN", "webauthn", none_, none_))
    let section_home: str[2] = ["section-a", "home"]
    assert(test_html_autocomplete_is(test_html_autocomplete_parse(document, "section-a home tel webauthn"), "section-a home tel webauthn", "webauthn", "webauthn", home, section_home))
    assert(test_html_autocomplete_is(test_html_autocomplete_parse(document, "name webauthn x"), "", "on", none, none_, none_))
    assert(test_html_autocomplete_is(test_html_autocomplete_parse(document, "off webauthn"), "", "on", none, none_, none_))

    # The mantle of an element that is not an input, the tokens and the IDL attribute.
    let element = test_html_create(document, "section")
    _ = test_html_put(element, html_attribute_names_autocomplete, " shipping\tname ")
    var autocomplete = TestHtmlAutocomplete(mixin = HtmlAutocompleteElement(vtable = &test_html_autocomplete_vtable, class_id = element.cell().class_id), element = element)
    assert(html_autocomplete_element_get_autofill_mantle(&autocomplete.mixin) == HtmlAutocompleteElementAutofillMantle.expectation)
    let tokens = html_autocomplete_element_autocomplete_tokens(&autocomplete.mixin)
    let expected_tokens: str[2] = ["shipping", "name"]
    assert(test_html_strings_are(&tokens, expected_tokens))
    assert(test_html_forms_is(html_autocomplete_element_autocomplete(&autocomplete.mixin), "shipping name"))

    # Determining a field's category.
    let tel = html_determine_a_field_category(ak.sv("TEL-National"))
    assert(tel.category == HtmlCategory.contact and tel.maximum_tokens == 4)
    let unknown = html_determine_a_field_category(ak.sv("telephone"))
    assert(unknown.category == none and unknown.maximum_tokens == none)
    _ = realm

# mark: The text control selection ==============================================================

## A textarea whose raw value is `value`.
func test_html_forms_textarea(document: DomDocument*, value: str) -> HtmlTextAreaElement*:
    let textarea = as_html_text_area_element(test_html_create(document, "textarea"))
    textarea.m_raw_value = ak.utf16_string_from_str(value)
    return textarea

## The textarea's API value is `expected`.
func test_html_forms_textarea_value_is(textarea: HtmlTextAreaElement*, expected: str) -> bool:
    let value = html_text_area_element_api_value(textarea)
    return ak.utf16_string_base_eq_string_view(&value.utf16_string_base, ak.sv(expected))

test "FormAssociatedTextControlElement: selection offsets, direction, stringifier and setRangeText":
    let (vm, realm, document) = test_html_setup()
    defer web_test_destroy_vm(vm)
    let textarea = test_html_forms_textarea(document, "hello world")
    let mixin = &textarea.form_associated_text_control_element

    # A backwards selection reads in order; the bindings of a textarea.
    mixin.m_selection_start = 11
    mixin.m_selection_end = 6
    assert(html_form_associated_text_control_element_selection_start(mixin) == 6 and html_form_associated_text_control_element_selection_end(mixin) == 11)
    assert((html_form_associated_text_control_element_selection_start_binding(mixin) else trap("null")) == 6)
    assert((html_form_associated_text_control_element_selection_end_binding(mixin) else trap("null")) == 11)
    let selected = html_form_associated_text_control_element_selected_text_for_stringifier_impl(mixin) else trap("no selected text")
    assert(ak.utf16_string_base_eq_string_view(&selected.utf16_string_base, ak.sv("world")))

    # The relevant value shrinks. A selection (start before end) is clamped at both ends; a
    # backwards one is a cursor position, of which only the start is clamped.
    textarea.m_raw_value = ak.utf16_string_from_str("hello")
    textarea.m_api_value = none
    html_form_associated_text_control_element_relevant_value_was_changed(mixin)
    assert(mixin.m_selection_start == 5 and mixin.m_selection_end == 6)
    mixin.m_selection_start = 2
    mixin.m_selection_end = 11
    html_form_associated_text_control_element_relevant_value_was_changed(mixin)
    assert(mixin.m_selection_start == 2 and mixin.m_selection_end == 5)
    mixin.m_selection_start = 6
    mixin.m_selection_end = 11
    html_form_associated_text_control_element_relevant_value_was_changed(mixin)
    assert(mixin.m_selection_start == 5 and mixin.m_selection_end == 5)
    assert(html_form_associated_text_control_element_selected_text_for_stringifier_impl(mixin) == none)
    assert((html_form_associated_text_control_element_selection_start_binding(mixin) else trap("null")) == 5)

    # The selection direction.
    let none_name = html_form_associated_text_control_element_selection_direction(mixin) else trap("null")
    assert(ak.string_eq_string_view(&none_name, ak.sv("none")))
    html_form_associated_text_control_element_set_selection_direction(mixin, ak.string_from_str("backward"))
    assert(html_form_associated_text_control_element_selection_direction_state(mixin) == HtmlSelectionDirection.backward)
    let backward_name = html_form_associated_text_control_element_selection_direction(mixin) else trap("null")
    assert(ak.string_eq_string_view(&backward_name, ak.sv("backward")))
    html_form_associated_text_control_element_set_selection_direction(mixin, ak.string_from_str("Forward"))
    assert(html_form_associated_text_control_element_selection_direction_state(mixin) == HtmlSelectionDirection.none_)
    html_form_associated_text_control_element_set_selection_direction(mixin, none)
    assert(html_form_associated_text_control_element_selection_direction_state(mixin) == HtmlSelectionDirection.none_)

    # setRangeText that leaves the selection where it is: the value changes, the dirty flag is set.
    mixin.m_selection_start = 0
    mixin.m_selection_end = 0
    html_form_associated_text_control_element_set_range_text(mixin, ak.utf16_string_from_str(">> "), 0, 0) else trap("set_range_text failed")
    assert(test_html_forms_textarea_value_is(textarea, ">> hello"))
    assert(textarea.m_dirty_value)
    assert(mixin.m_selection_start == 0 and mixin.m_selection_end == 0)
    # Replacing a range before the selection (start, end beyond the length are clamped).
    mixin.m_selection_start = 0
    html_form_associated_text_control_element_set_range_text(mixin, ak.utf16_string_from_str("!"), 99, 100) else trap("set_range_text failed")
    assert(test_html_forms_textarea_value_is(textarea, ">> hello!"))
    assert(mixin.m_selection_start == 0 and mixin.m_selection_end == 0)

    # start > end is an IndexSizeError.
    var failed = false
    html_form_associated_text_control_element_set_range_text(mixin, ak.utf16_string_new(), 3, 2) catch:
        failed = true
    assert(failed)
    let name = test_html_take_dom_exception_name()
    assert(ak.fly_string_eq_string_view(&name, ak.sv("IndexSizeError")))

    # The scheduled selectionchange flag and the strings of a direction.
    assert(not html_form_associated_text_control_element_has_scheduled_selectionchange_event(mixin))
    html_form_associated_text_control_element_set_scheduled_selectionchange_event(mixin, true)
    assert(html_form_associated_text_control_element_has_scheduled_selectionchange_event(mixin))
    assert(html_string_to_selection_direction(ak.string_from_str("forward")) == HtmlSelectionDirection.forward)
    assert(html_string_to_selection_direction(ak.string_from_str("sideways")) == HtmlSelectionDirection.none_)
    assert(html_form_associated_text_control_element_as_cell_impl(&mixin.input_events_target_base) == textarea.js_cell())
    _ = realm

# mark: Normalizing line breaks and the encodings ===============================================

## A string of `text` with `\r` and `\n` as written.
func test_html_forms_normalized_is(input: str, expected: str) -> bool:
    let normalized = html_normalize_line_breaks(ak.sv(input)) else trap("normalize_line_breaks failed")
    return ak.string_eq_string_view(&normalized, ak.sv(expected))

test "normalize line breaks (oracle values: a CR LF pair is dropped, as the donor's lexer does)":
    assert(test_html_forms_normalized_is("a\rb", "a\r\nb"))
    assert(test_html_forms_normalized_is("a\nb", "a\r\nb"))
    # donor quirk: consume_until stops at the CR and ignore(2) skips the CR LF pair.
    assert(test_html_forms_normalized_is("a\r\nb", "ab"))
    assert(test_html_forms_normalized_is("\r\r\n\n", "\r\n\r\n"))
    assert(test_html_forms_normalized_is("x\n\ry", "x\r\n\r\ny"))
    assert(test_html_forms_normalized_is("plain", "plain"))
    assert(test_html_forms_normalized_is("", ""))
    assert(test_html_forms_normalized_is("\r", "\r\n"))
    assert(test_html_forms_normalized_is("a\n", "a\r\n"))

## A string entry `name`=`value`.
func test_html_forms_entry(name: str, value: str) -> XhrFormDataEntry:
    return XhrFormDataEntry(name = ak.string_from_str(name), value = .string(ak.string_from_str(value)))

test "entry lists: name-value pairs, text/plain and multipart/form-data of string entries":
    var entries: ak.Vector[XhrFormDataEntry]
    ak.vector_append_t(&entries, test_html_forms_entry("a\nb", "1\r2"))
    ak.vector_append_t(&entries, test_html_forms_entry("q\"", "x y"))

    let pairs = html_convert_to_list_of_name_value_pairs(entries) else trap("convert failed")
    assert(ak.vector_size(&pairs) == 2)
    assert(ak.string_eq_string_view(&ak.vector_at_const(&pairs, 0).name, ak.sv("a\r\nb")))
    assert(ak.string_eq_string_view(&ak.vector_at_const(&pairs, 0).value, ak.sv("1\r\n2")))
    let plain = html_plain_text_encode(pairs) else trap("plain text failed")
    assert(ak.string_eq_string_view(&plain, ak.sv("a\r\nb=1\r\n2\r\nq\"=x y\r\n")))

    let serialized = html_serialize_to_multipart_form_data(entries) else trap("multipart failed")
    let boundary = ak.string_bytes_as_string_view(&serialized.boundary)
    assert(ak.string_view_starts_with_string_view(&boundary, ak.sv("---------------------------")))
    var expected = ak.string_builder_new()
    ak.string_builder_append(&expected, "--")
    ak.string_builder_append_string_view(&expected, boundary)
    ak.string_builder_append(&expected, "\r\nContent-Disposition: form-data; name=\"a%0D%0Ab\"\r\n\r\n1\r\n2\r\n--")
    ak.string_builder_append_string_view(&expected, boundary)
    ak.string_builder_append(&expected, "\r\nContent-Disposition: form-data; name=\"q%22\"\r\n\r\nx y\r\n--")
    ak.string_builder_append_string_view(&expected, boundary)
    ak.string_builder_append(&expected, "--\r\n")
    let expected_string = ak.string_builder_to_string(&expected) else trap("to_string failed")
    var data = serialized.serialized_data
    let expected_view = ak.string_bytes_as_string_view(&expected_string)
    assert(ak.string_view_eq_string_view(&expected_view, ak.string_view_from_bytes(ak.byte_buffer_bytes(&data))))

# mark: The form element ========================================================================

test "HTMLFormElement: method, enctype, action, elements, submittable elements and validation":
    let (vm, realm, document) = test_html_setup()
    defer web_test_destroy_vm(vm)
    let root = test_html_forms_root(document, "section")
    let form = as_html_form_element(test_html_create(document, "form"))
    test_html_forms_insert(root, form.html_element())
    let fieldset = test_html_create(document, "fieldset")
    let output = test_html_create(document, "output")
    let label = test_html_create(document, "label")
    test_html_forms_insert(form.html_element(), fieldset)
    test_html_forms_insert(fieldset, output)
    test_html_forms_insert(form.html_element(), label)

    # The method and enctype states (ASCII case-insensitive, with the GET / urlencoded defaults).
    assert(html_form_element_method_state_from_form_element(form, output) == HtmlFormElementMethodAttributeState.get)
    _ = test_html_put(form.html_element(), html_attribute_names_method, "PoSt")
    assert(html_form_element_method_state_from_form_element(form, output) == HtmlFormElementMethodAttributeState.post)
    _ = test_html_put(form.html_element(), html_attribute_names_method, "dialog")
    assert(html_form_element_method_state_from_form_element(form, form.html_element()) == HtmlFormElementMethodAttributeState.dialog)
    _ = test_html_put(form.html_element(), html_attribute_names_method, "put")
    assert(html_form_element_method_state_from_form_element(form, output) == HtmlFormElementMethodAttributeState.get)
    assert(html_form_element_encoding_type_state_from_form_element(form, output) == HtmlFormElementEncodingTypeAttributeState.form_url_encoded)
    _ = test_html_put(form.html_element(), html_attribute_names_enctype, "Multipart/Form-Data")
    assert(html_form_element_encoding_type_state_from_form_element(form, output) == HtmlFormElementEncodingTypeAttributeState.form_data)
    _ = test_html_put(form.html_element(), html_attribute_names_enctype, "text/plain")
    assert(html_form_element_encoding_type_state_from_form_element(form, output) == HtmlFormElementEncodingTypeAttributeState.plain_text)
    _ = test_html_put(form.html_element(), html_attribute_names_enctype, "text/html")
    assert(html_form_element_encoding_type_state_from_form_element(form, output) == HtmlFormElementEncodingTypeAttributeState.form_url_encoded)

    # The action: the form's attribute (an output is not a submit button, so its formaction is ignored).
    assert(test_html_forms_is(html_form_element_action_from_form_element(form, output), ""))
    _ = test_html_put(output, html_attribute_names_formaction, "ignored")
    _ = test_html_put(form.html_element(), html_attribute_names_action, "go")
    assert(test_html_forms_is(html_form_element_action_from_form_element(form, output), "go"))

    # elements: the listed controls the form owns, in tree order; length.
    let elements = html_form_element_elements(form)
    assert(html_form_element_elements(form) == elements)
    assert(html_form_element_length(form) == 2)
    assert(dom_html_collection_item(&elements.html_collection, 0) == fieldset.dom_element())
    assert(dom_html_collection_item(&elements.html_collection, 1) == output.dom_element())
    let item = html_form_element_item_value_impl(form.bind_platform_object(), 1) else trap("no item")
    assert(js_value_as_object(item) == (JsObject*)output)
    assert(html_form_element_item_value_impl(form.bind_platform_object(), 2) == none)

    # Nothing here is submittable: no constraints, no default button, no blocking fields.
    var submittable = html_form_element_get_submittable_elements(form)
    assert(ak.vector_is_empty(&submittable))
    assert(html_form_element_statically_validate_constraints(form).result)
    assert(html_form_element_check_validity(form) else trap("check_validity threw"))
    assert(html_form_element_report_validity(form) else trap("report_validity threw"))
    assert(html_form_element_default_button(form) == none)
    assert(html_form_element_number_of_fields_blocking_implicit_submission(form) == 0)

    # The constructing entry list flag: a nested construction returns null.
    html_form_element_set_constructing_entry_list(form, true)
    let entry_list = html_construct_entry_list(realm, form) else trap("construct_entry_list threw")
    assert(entry_list == none)
    html_form_element_set_constructing_entry_list(form, false)

    # relList is one DOMTokenList.
    assert(html_form_element_rel_list(form) == html_form_element_rel_list(form))

    # Picking an encoding: accept-charset's first known label, else UTF-8.
    let default_encoding = html_form_element_pick_an_encoding(form) else trap("pick failed")
    assert(ak.string_eq_string_view(&default_encoding, ak.sv("UTF-8")))
    _ = test_html_put(form.html_element(), html_attribute_names_accept_charset, " bogus\tlatin2 utf-8")
    let picked = html_form_element_pick_an_encoding(form) else trap("pick failed")
    assert(ak.string_eq_string_view(&picked, ak.sv("ISO-8859-2")))
    _ = test_html_put(form.html_element(), html_attribute_names_accept_charset, "bogus")
    let fallback = html_form_element_pick_an_encoding(form) else trap("pick failed")
    assert(ak.string_eq_string_view(&fallback, ak.sv("UTF-8")))
    _ = label

test "HTMLFormElement: supported property names, the past names map and namedItem of the controls":
    let (vm, realm, document) = test_html_setup()
    defer web_test_destroy_vm(vm)
    let root = test_html_forms_root(document, "section")
    let form = as_html_form_element(test_html_create(document, "form"))
    test_html_forms_insert(root, form.html_element())
    let fieldset = test_html_create(document, "fieldset")
    let output = test_html_create(document, "output")
    test_html_set(output, html_attribute_names_id, "o1")
    test_html_set(output, html_attribute_names_name, "n1")
    test_html_set(fieldset, html_attribute_names_name, "fs")
    test_html_forms_insert(form.html_element(), fieldset)
    test_html_forms_insert(form.html_element(), output)

    # Tree order, id before name.
    var names = html_form_element_supported_property_names_impl(form.bind_platform_object())
    assert(ak.vector_size(&names) == 3)
    assert(ak.fly_string_eq_string_view(ak.vector_at(&names, 0), ak.sv("fs")))
    assert(ak.fly_string_eq_string_view(ak.vector_at(&names, 1), ak.sv("o1")))
    assert(ak.fly_string_eq_string_view(ak.vector_at(&names, 2), ak.sv("n1")))
    assert(html_form_element_is_supported_property_name_impl(form.bind_platform_object(), ak.fly_string_from_str("n1")))
    assert(not html_form_element_is_supported_property_name_impl(form.bind_platform_object(), ak.fly_string_from_str("zz")))

    # A past name keeps a name supported; changing the element's form owner drops its entries.
    _ = ak.hash_map_set(&form.m_past_names_map, ak.fly_string_from_str("old"), HtmlFormElementPastNameEntry(node = output.dom_node(), insertion_time = ak.monotonic_time_now()))
    assert(html_form_element_is_supported_property_name_impl(form.bind_platform_object(), ak.fly_string_from_str("old")))
    var with_past = html_form_element_supported_property_names_impl(form.bind_platform_object())
    assert(ak.vector_size(&with_past) == 4 and ak.fly_string_eq_string_view(ak.vector_at(&with_past, 3), ak.sv("old")))
    html_form_associated_element_set_form(&output.form_associated_element, none)
    assert(not html_form_element_is_supported_property_name_impl(form.bind_platform_object(), ak.fly_string_from_str("old")))
    html_form_associated_element_reset_form_owner(&output.form_associated_element)

    # HTMLFormControlsCollection::namedItem: one match, none, the empty name.
    let elements = html_form_element_elements(form)
    match html_form_controls_collection_named_item_or_radio_node_list(elements, ak.fly_string_from_str("n1")):
        .dom_element(element): assert(element == output.dom_element())
        _: trap("expected the output")
    match html_form_controls_collection_named_item_or_radio_node_list(elements, ak.fly_string_from_str("o1")):
        .dom_element(element): assert(element == output.dom_element())
        _: trap("expected the output")
    match html_form_controls_collection_named_item_or_radio_node_list(elements, ak.fly_string_from_str("nothing")):
        .empty: assert(true)
        _: trap("expected nothing")
    match html_form_controls_collection_named_item_or_radio_node_list(elements, ak.fly_string_new()):
        .empty: assert(true)
        _: trap("expected nothing")
    assert(js_value_is_undefined(html_form_controls_collection_named_item_value_impl(elements.bind_platform_object(), ak.fly_string_from_str("nothing"))))
    _ = realm

# mark: Labels and output =======================================================================

test "HTMLLabelElement: the labeled control by for and by descendant, and the label's form":
    let (vm, realm, document) = test_html_setup()
    defer web_test_destroy_vm(vm)
    let root = test_html_forms_root(document, "section")
    let form = as_html_form_element(test_html_create(document, "form"))
    test_html_forms_insert(root, form.html_element())
    let output = test_html_create(document, "output")
    test_html_set(output, html_attribute_names_id, "result")
    test_html_forms_insert(form.html_element(), output)

    let label = as_html_label_element(test_html_create(document, "label"))
    test_html_forms_insert(root, label.html_element())
    assert(html_label_element_control(label) == none and html_label_element_form(label) == none)

    # A labelable descendant.
    let inner = test_html_create(document, "output")
    test_html_forms_insert(label.html_element(), inner)
    assert(html_label_element_control(label) == inner)
    assert(html_label_element_form(label) == none)

    # for= names the control in the tree: its form owner is the label's form.
    _ = test_html_put(label.html_element(), html_attribute_names_for, "result")
    assert(test_html_optional_is(html_label_element_for(label), "result"))
    assert(html_label_element_control(label) == output)
    assert(html_label_element_form(label) == form)
    # An ID that is not a labelable element's: no control (the descendant is not used).
    _ = test_html_put(label.html_element(), html_attribute_names_for, "nothing")
    assert(html_label_element_control(label) == none)
    _ = realm

test "HTMLOutputElement: htmlFor, value, defaultValue and the reset algorithm":
    let (vm, realm, document) = test_html_setup()
    defer web_test_destroy_vm(vm)
    let output = as_html_output_element(test_html_create(document, "output"))
    tree_node_append_child(output.dom_node(), dom_document_create_text_node(document, ak.utf16_string_from_str("42")).dom_node())

    assert(html_output_element_html_for(output) == html_output_element_html_for(output))
    assert(test_html_forms_utf16_is(html_output_element_value(output), "42"))
    assert(test_html_forms_utf16_is(html_output_element_default_value(output), "42"))
    let form_value = html_form_associated_element_form_value(&output.html_element().form_associated_element)
    assert(ak.utf16_string_base_eq_string_view(&form_value.utf16_string_base, ak.sv("42")))

    # With a default value override, setting defaultValue changes only the override.
    output.m_default_value_override = ak.utf16_string_from_str("7")
    html_output_element_set_default_value(output, ak.utf16_string_from_str("8"))
    assert(test_html_forms_utf16_is(html_output_element_default_value(output), "8"))
    assert(test_html_forms_utf16_is(html_output_element_value(output), "42"))
    _ = realm