Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.0

0.6.0 / src / web / html / tests_html_forms_1.lucb

#==============================================================================================
#
#   tests_html_forms_1 - tests of the form elements, form owners, collections and constraint
#                        validation (region r23)
#
#   DESCRIPTION:
#       Ladybird has no unit tests for these files (forms are tested through Tests/LibWeb/Text,
#       which needs scripting), so these pin the C++ behaviour: form, fieldset, legend, label
#       and output made by the ElementFactory with their mixin tables and FormAssociatedElement
#       answers; garbage collection with HTML elements alive (FormAssociatedElement::visit_edges
#       and the weak form owner); resetting the form owner through insertion, the form
#       attribute and removal; enabled and the disabled fieldset; the constraint validation API
#       and ValidityState; formAction / action; ElementInternals' NotSupportedErrors.
#
#       The helpers of tests_html_element_1 (r22) build the elements: attributes go into the
#       NamedNodeMap and their change steps run through the attribute_changed virtual,
#       insertion and removal steps run through the inserted / removed_from virtuals, since the
#       DOM's own entry points reach style invalidation (region r42).
#
#==============================================================================================

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

## The FormAssociatedElement mixin of an HTML element.
func test_html_forms_fae(element: HtmlElement*) -> HtmlFormAssociatedElement*:
    return &element.form_associated_element

## `child` appended to `parent` and its insertion steps run (Node::inserted of an HTML element),
## with the document's DOM tree version bumped as Node::insert does.
func test_html_forms_insert(parent: HtmlElement*, child: HtmlElement*):
    tree_node_append_child(parent.dom_node(), child.dom_node())
    dom_document_bump_dom_tree_version(dom_node_document(parent.dom_node()))
    for node in dom_node_inclusive_subtree(child.dom_node()):
        if let element = as_if_html_element(node):
            html_element_inserted_impl(element.dom_node())

## `child` removed from `parent` and its removal steps run (Node::removed_from).
func test_html_forms_remove(parent: HtmlElement*, child: HtmlElement*):
    tree_node_remove_child(parent.dom_node(), child.dom_node())
    dom_document_bump_dom_tree_version(dom_node_document(parent.dom_node()))
    html_element_removed_from_impl(child.dom_node(), DomNodeIsSubtreeRoot.yes, parent.dom_node(), tree_node_root(parent.dom_node()))

## An element of the document's tree: `local_name` appended to the document and inserted.
func test_html_forms_root(document: DomDocument*, local_name: str) -> HtmlElement*:
    let root = test_html_create(document, local_name)
    tree_node_append_child(document.dom_node(), root.dom_node())
    html_element_inserted_impl(root.dom_node())
    return root

## Whether a String holds `text`.
func test_html_forms_is(string: ak.String, text: str) -> bool:
    return ak.string_eq_string_view(&string, ak.sv(text))

## Whether two Strings are equal.
func test_html_forms_same(a: ak.String, b: ak.String) -> bool:
    return ak.string_equals(&a, &b)

## Whether a Utf16String holds `text`.
func test_html_forms_utf16_is(string: ak.Utf16String, text: str) -> bool:
    return ak.utf16_string_base_eq_string_view(&string.utf16_string_base, ak.sv(text))

## A full collection of the heap of `vm` (the test document and pages are in the heap and visited
## in full: page/tests_page).
noinline func test_html_forms_collect(vm: Vm*):
    web_test_clobber_stack()
    gc.heap_collect_garbage(vm_heap(vm))

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

test "ElementFactory: form, fieldset, legend, label and output with their tables and answers":
    let (vm, realm, document) = test_html_setup()
    defer web_test_destroy_vm(vm)

    let form = as_html_form_element(test_html_create(document, "form"))
    let fieldset = as_html_field_set_element(test_html_create(document, "fieldset"))
    let legend = as_html_legend_element(test_html_create(document, "legend"))
    let label = as_html_label_element(test_html_create(document, "label"))
    let output = as_html_output_element(test_html_create(document, "output"))
    let div = test_html_create(document, "section")

    # The mixin tables moved to each class's own.
    assert(form.html_element().form_associated_element.vtable == &html_form_element_form_associated_element_vtable)
    assert(form.html_element().element.aria_mixin.vtable == &html_form_element_aria_mixin_vtable)
    assert(form.html_element().global_event_handlers.vtable == &html_form_element_global_event_handlers_vtable)
    assert(fieldset.html_element().form_associated_element.vtable == &html_field_set_element_form_associated_element_vtable)
    assert(fieldset.html_element().element.slottable_mixin.vtable == &html_field_set_element_slottable_mixin_vtable)
    assert(legend.html_element().form_associated_element.vtable == &html_legend_element_form_associated_element_vtable)
    assert(legend.html_element().element.animatable.vtable == &html_legend_element_animatable_vtable)
    assert(label.html_element().form_associated_element.vtable == &html_label_element_form_associated_element_vtable)
    assert(output.html_element().form_associated_element.vtable == &html_output_element_form_associated_element_vtable)
    assert(output.html_element().form_associated_element.class_id == output.cell().class_id)

    # FormAssociatedElement's answers: fieldset and output are listed form-associated elements,
    # output is resettable, both inherit autocapitalize; the others are not form-associated.
    assert(html_form_associated_element_is_form_associated_element(test_html_forms_fae(fieldset.html_element())))
    assert(html_form_associated_element_is_form_associated_element(test_html_forms_fae(output.html_element())))
    assert(not html_form_associated_element_is_form_associated_element(test_html_forms_fae(form.html_element())))
    assert(not html_form_associated_element_is_form_associated_element(test_html_forms_fae(legend.html_element())))
    assert(not html_form_associated_element_is_form_associated_element(test_html_forms_fae(label.html_element())))
    assert(not html_form_associated_element_is_form_associated_element(test_html_forms_fae(div)))
    assert(html_form_associated_element_is_listed(test_html_forms_fae(fieldset.html_element())))
    assert(html_form_associated_element_is_listed(test_html_forms_fae(output.html_element())))
    assert(not html_form_associated_element_is_listed(test_html_forms_fae(div)))
    assert(not html_form_associated_element_is_resettable(test_html_forms_fae(fieldset.html_element())))
    assert(html_form_associated_element_is_resettable(test_html_forms_fae(output.html_element())))
    assert(not html_form_associated_element_is_submittable(test_html_forms_fae(output.html_element())))
    assert(html_form_associated_element_is_autocapitalize_and_autocorrect_inheriting(test_html_forms_fae(fieldset.html_element())))
    assert(html_form_associated_element_is_autocapitalize_and_autocorrect_inheriting(test_html_forms_fae(output.html_element())))
    assert(not html_form_associated_element_is_autocapitalize_and_autocorrect_inheriting(test_html_forms_fae(div)))
    assert(not html_form_associated_element_is_button(test_html_forms_fae(output.html_element())))
    assert(not html_form_associated_element_is_submit_button(test_html_forms_fae(output.html_element())))
    assert(html_form_associated_element_is_mutable(test_html_forms_fae(output.html_element())))
    assert(html_element_is_labelable(output.html_element()))
    assert(not html_element_is_labelable(fieldset.html_element()))
    assert(html_form_associated_element_form_associated_element_to_html_element(test_html_forms_fae(output.html_element())) == output.html_element())

    # The other virtuals.
    assert(dom_node_is_html_form_element(form.dom_node()) and dom_node_is_html_fieldset_element(fieldset.dom_node()))
    assert(dom_event_target_has_activation_behavior(label.dom_event_target()))
    assert(test_html_forms_is(html_field_set_element_type(fieldset), "fieldset"))
    assert(test_html_forms_is(html_output_element_type(output), "output"))
    assert((html_field_set_element_default_role_impl(&fieldset.html_element().element.aria_mixin) else trap("no role")) == AriaRole.group)
    assert((html_form_element_default_role_impl(&form.html_element().element.aria_mixin) else trap("no role")) == AriaRole.form)
    assert((html_output_element_default_role_impl(&output.html_element().element.aria_mixin) else trap("no role")) == AriaRole.status)

    # The form's legacy platform object flags.
    let flags = form.html_element_base.element.parent_node.node.event_target.platform_object.m_legacy_platform_object_flags else trap("no flags")
    assert(flags.supports_indexed_properties == 1 and flags.supports_named_properties == 1)
    assert(flags.has_legacy_unenumerable_named_properties_interface_extended_attribute == 1 and flags.has_legacy_override_built_ins_interface_extended_attribute == 1)
    assert(flags.has_named_property_setter == 0)

    # FormAssociatedElement's members start as C++'s defaults.
    let fae = test_html_forms_fae(output.html_element())
    assert(html_form_associated_element_form(fae) == none)
    assert(not fae.m_parser_inserted and not fae.m_face_disabled_state)
    assert(ak.string_is_empty(&fae.m_face_validation_message) and ak.string_is_empty(&fae.m_custom_validity_error_message))
    match html_form_associated_element_face_submission_value(fae):
        .empty: assert(true)
        _: trap("the submission value starts empty")
    match html_form_associated_element_face_state(fae):
        .empty: assert(true)
        _: trap("the state starts empty")
    _ = realm

# mark: Garbage collection ======================================================================

test "GC with HTML elements alive: the tree survives and a collected form owner is cleared":
    web_test_clobber_stack()
    run_html_forms_collections()

noinline func run_html_forms_collections():
    web_initialize_strings()
    let (vm, realm) = web_test_create_vm_and_settings_realm()
    let document = test_html_heap_document(realm)
    vm_keep_alive(vm, document)
    make_html_forms_tree(document)
    test_html_forms_collect(vm)

    # The tree is the document's: every element of it lives, through every element's visit_edges
    # (HTMLElement's visits FormAssociatedElement's).
    let form = as_html_form_element(document.dom_node().tree_node.m_first_child else trap("no form"))
    assert(form.cell().vtable == (const gc.CellVTable*)&html_form_element_vtable)
    let fieldset = as_html_field_set_element(form.dom_node().tree_node.m_first_child else trap("no fieldset"))
    assert(fieldset.cell().vtable == (const gc.CellVTable*)&html_field_set_element_vtable)
    let legend = fieldset.dom_node().tree_node.m_first_child else trap("no legend")
    assert(legend.cell().vtable == (const gc.CellVTable*)&html_legend_element_vtable)
    let output = as_html_output_element(form.dom_node().tree_node.m_last_child else trap("no output"))
    assert(output.cell().vtable == (const gc.CellVTable*)&html_output_element_vtable)
    assert(output.html_element().form_associated_element.vtable == &html_output_element_form_associated_element_vtable)

    # The detached form was only the weak form owner of the output: collected, so the owner is null.
    assert(html_form_associated_element_form(&output.html_element().form_associated_element) == none)

    # A second collection over the same tree, with a ValidityState and an ElementInternals that
    # visit the output.
    let validity = html_form_associated_element_validity(&output.html_element().form_associated_element)
    let internals = html_element_internals_create(realm, output.html_element())
    test_html_forms_collect(vm)
    assert(html_validity_state_valid(validity))
    assert(internals.m_target_element == output.html_element())
    assert(output.cell().vtable == (const gc.CellVTable*)&html_output_element_vtable)
    web_test_destroy_vm(vm)

## The document's tree: a form holding a fieldset (with a legend) and an output; the output's
## form owner is then set to a detached form nothing else keeps.
noinline func make_html_forms_tree(document: DomDocument*):
    let form = as_html_form_element(test_html_forms_root(document, "form"))
    let fieldset = as_html_field_set_element(test_html_create(document, "fieldset"))
    test_html_forms_insert(fieldset.html_element(), test_html_create(document, "legend"))
    test_html_forms_insert(form.html_element(), fieldset.html_element())
    let output = as_html_output_element(test_html_create(document, "output"))
    test_html_forms_insert(form.html_element(), output.html_element())
    assert(html_form_associated_element_form(&output.html_element().form_associated_element) == form)
    assert(html_form_associated_element_form(&fieldset.html_element().form_associated_element) == form)

    let detached = as_html_form_element(test_html_create(document, "form"))
    html_form_associated_element_set_form(&output.html_element().form_associated_element, detached)
    assert(html_form_associated_element_form(&output.html_element().form_associated_element) == detached)
    assert(ak.vector_size(&form.m_associated_elements) == 1)

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

test "reset the form owner: the nearest ancestor form, the form attribute, removal and parser insertion":
    let (vm, realm, document) = test_html_setup()
    defer web_test_destroy_vm(vm)
    let root = test_html_forms_root(document, "section")
    let form1 = as_html_form_element(test_html_create(document, "form"))
    let form2 = as_html_form_element(test_html_create(document, "form"))
    test_html_set(form2.html_element(), html_attribute_names_id, "f2")
    test_html_forms_insert(root, form1.html_element())
    test_html_forms_insert(root, form2.html_element())

    # Inserted into form1: its nearest ancestor form owns it.
    let output = as_html_output_element(test_html_create(document, "output"))
    let fae = &output.html_element().form_associated_element
    test_html_forms_insert(form1.html_element(), output.html_element())
    assert(html_form_associated_element_form(fae) == form1)
    assert(ak.vector_size(&form1.m_associated_elements) == 1 and *ak.vector_at(&form1.m_associated_elements, 0) == output.html_element())

    # form="f2": the first element with that ID, a form, owns it (the document records the element).
    test_html_set(output.html_element(), html_attribute_names_form, "f2")
    assert(html_form_associated_element_form(fae) == form2)
    assert(ak.vector_is_empty(&form1.m_associated_elements) and ak.vector_size(&form2.m_associated_elements) == 1)

    # An ID that names no form: no owner.
    test_html_set(output.html_element(), html_attribute_names_form, "nothing")
    assert(html_form_associated_element_form(fae) == none)
    test_html_set(output.html_element(), html_attribute_names_form, "f2")
    assert(html_form_associated_element_form(fae) == form2)

    # Removing the attribute: back to the ancestor form.
    test_html_remove(output.html_element(), html_attribute_names_form)
    assert(html_form_associated_element_form(fae) == form1)

    # Removed from the tree: not in the owner's tree any more, so no owner.
    test_html_forms_remove(form1.html_element(), output.html_element())
    assert(html_form_associated_element_form(fae) == none)
    assert(ak.vector_is_empty(&form1.m_associated_elements))

    # A parser-inserted element keeps its owner on insertion (the parser associates it).
    html_form_associated_element_set_parser_inserted(fae)
    test_html_forms_insert(form1.html_element(), output.html_element())
    assert(html_form_associated_element_form(fae) == none)
    assert(not fae.m_parser_inserted or html_form_associated_element_form(fae) == none)
    html_form_associated_element_reset_form_owner(fae)
    assert(not fae.m_parser_inserted and html_form_associated_element_form(fae) == form1)

    # A fieldset is listed too; a label or a div are not form-associated, so insertion leaves them.
    let fieldset = test_html_create(document, "fieldset")
    test_html_forms_insert(form1.html_element(), fieldset)
    assert(html_form_associated_element_form(&fieldset.form_associated_element) == form1)
    let label = test_html_create(document, "label")
    test_html_forms_insert(form1.html_element(), label)
    assert(html_form_associated_element_form(&label.form_associated_element) == none)

    # The legend's form is its parent fieldset's form owner; a label's is its labeled control's.
    let legend = as_html_legend_element(test_html_create(document, "legend"))
    test_html_forms_insert(fieldset, legend.html_element())
    assert(html_legend_element_form(legend) == form1)
    assert(html_legend_element_form(as_html_legend_element(test_html_create(document, "legend"))) == none)
    _ = realm

# mark: Enabled and disabled fieldsets ==========================================================

test "enabled: a disabled fieldset ancestor disables everything outside its first legend":
    let (vm, realm, document) = test_html_setup()
    defer web_test_destroy_vm(vm)
    let outer = as_html_field_set_element(test_html_forms_root(document, "fieldset"))
    let legend = test_html_create(document, "legend")
    let second_legend = test_html_create(document, "legend")
    let inner = as_html_field_set_element(test_html_create(document, "fieldset"))
    test_html_forms_insert(outer.html_element(), legend)
    test_html_forms_insert(outer.html_element(), second_legend)
    test_html_forms_insert(outer.html_element(), inner.html_element())
    let in_legend = test_html_create(document, "output")
    let in_second_legend = test_html_create(document, "output")
    let in_inner = test_html_create(document, "output")
    test_html_forms_insert(legend, in_legend)
    test_html_forms_insert(second_legend, in_second_legend)
    test_html_forms_insert(inner.html_element(), in_inner)

    assert(html_form_associated_element_enabled(&in_inner.form_associated_element))
    assert(not html_field_set_element_is_disabled(outer) and not html_field_set_element_is_disabled(inner))

    test_html_set(outer.html_element(), html_attribute_names_disabled, "")
    assert(html_field_set_element_is_disabled(outer))
    assert(html_field_set_element_is_disabled(inner))
    assert(not html_form_associated_element_enabled(&in_inner.form_associated_element))
    assert(not html_form_associated_element_enabled(&in_second_legend.form_associated_element))
    assert(html_form_associated_element_enabled(&in_legend.form_associated_element))
    # An output's own disabled attribute does not disable it (only button, input, select, textarea and FACEs).
    test_html_set(in_legend, html_attribute_names_disabled, "")
    assert(html_form_associated_element_enabled(&in_legend.form_associated_element))

    test_html_remove(outer.html_element(), html_attribute_names_disabled)
    test_html_set(inner.html_element(), html_attribute_names_disabled, "")
    assert(not html_field_set_element_is_disabled(outer) and html_field_set_element_is_disabled(inner))
    assert(not html_form_associated_element_enabled(&in_inner.form_associated_element))
    assert(html_form_associated_element_enabled(&in_second_legend.form_associated_element))

    # The elements collection: the listed descendants, in tree order.
    let elements = html_field_set_element_elements(outer) else trap("no collection")
    assert(html_field_set_element_elements(outer) == elements)
    assert(dom_html_collection_length(elements) == 4)
    assert(dom_html_collection_item(elements, 0) == in_legend.dom_element())
    assert(dom_html_collection_item(elements, 2) == inner.dom_element())
    assert(dom_html_collection_item(elements, 3) == in_inner.dom_element())
    _ = realm

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

test "constraint validation: candidates, custom validity, validation messages and ValidityState":
    let (vm, realm, document) = test_html_setup()
    defer web_test_destroy_vm(vm)
    let output = test_html_create(document, "output")
    let fae = &output.form_associated_element

    # An output is not submittable, so never a candidate: it validates and has no message.
    assert(not html_form_associated_element_is_candidate_for_constraint_validation(fae))
    assert(not html_form_associated_element_will_validate(fae))
    assert(html_form_associated_element_check_validity(fae) and html_form_associated_element_report_validity(fae))
    assert(html_form_associated_element_satisfies_its_constraints(fae))
    assert(not html_form_associated_element_novalidate_state(fae))

    # setCustomValidity normalizes newlines, and a non-empty message is a custom error.
    var error_ = ak.string_from_str("bad\r\nvalue\r")
    html_form_associated_element_set_custom_validity(fae, &error_)
    assert(test_html_forms_is(error_, "bad\nvalue\n"))
    assert(test_html_forms_is(fae.m_custom_validity_error_message, "bad\nvalue\n"))
    assert(html_form_associated_element_suffering_from_a_custom_error(fae))
    assert(not html_form_associated_element_satisfies_its_constraints(fae))
    # Still not a candidate: the message stays empty.
    assert(test_html_forms_utf16_is(html_form_associated_element_validation_message(fae), ""))

    # ValidityState is live over the control.
    let validity = html_form_associated_element_validity(fae)
    assert(validity.m_control == fae)
    assert(html_validity_state_custom_error(validity) and not html_validity_state_valid(validity))
    var empty = ak.string_new()
    html_form_associated_element_set_custom_validity(fae, &empty)
    assert(not html_validity_state_custom_error(validity) and html_validity_state_valid(validity))

    # The FACE validity flags drive the suffering-from states.
    var flags = HtmlValidityStateFlags()
    assert(not html_validity_state_flags_has_one_or_more_true_values(&flags))
    flags.value_missing = true
    flags.step_mismatch = true
    assert(html_validity_state_flags_has_one_or_more_true_values(&flags))
    html_form_associated_element_set_face_validity_flags(fae, flags)
    assert(html_form_associated_element_face_validity_flags(fae).value_missing)
    assert(html_validity_state_value_missing(validity) and html_validity_state_step_mismatch(validity))
    assert(not html_validity_state_type_mismatch(validity) and not html_validity_state_pattern_mismatch(validity))
    assert(not html_validity_state_too_long(validity) and not html_validity_state_too_short(validity))
    assert(not html_validity_state_range_underflow(validity) and not html_validity_state_range_overflow(validity))
    assert(not html_validity_state_bad_input(validity) and not html_validity_state_valid(validity))
    html_form_associated_element_set_face_validation_message(fae, ak.string_from_str("missing"))
    assert(test_html_forms_is(html_form_associated_element_face_validation_message(fae), "missing"))
    html_form_associated_element_set_face_validation_anchor(fae, output)
    assert(gc.weak_ptr(&fae.m_face_validation_anchor) == output)
    html_form_associated_element_set_face_submission_value(fae, .string(ak.string_from_str("v")))
    match html_form_associated_element_face_submission_value(fae):
        .string(value): assert(test_html_forms_is(value, "v"))
        _: trap("expected a string")
    _ = realm

# mark: formAction, action and ElementInternals =================================================

test "formAction and action: the document's URL for a missing or empty attribute; the reflections":
    let (vm, realm, document) = test_html_setup()
    defer web_test_destroy_vm(vm)
    let output = test_html_create(document, "output")
    let form = as_html_form_element(test_html_create(document, "form"))
    let url = dom_document_url_string(document)
    assert(test_html_forms_same(url, html_form_associated_element_form_action(&output.form_associated_element)))
    assert(test_html_forms_same(url, html_form_element_action(form)))
    test_html_put(output, html_attribute_names_formaction, "")
    test_html_put(form.html_element(), html_attribute_names_action, "")
    assert(test_html_forms_same(url, html_form_associated_element_form_action(&output.form_associated_element)))
    assert(test_html_forms_same(url, html_form_element_action(form)))
    # (A value is completed against the document's base URL, whose fallback is region r28's.)
    _ = realm

test "ElementInternals: NotSupportedError unless the target is a form-associated custom element":
    let (vm, realm, document) = test_html_setup()
    defer web_test_destroy_vm(vm)
    let element = test_html_create(document, "x-foo")
    let internals = html_element_internals_create(realm, element)
    assert(internals.m_target_element == element)
    assert(html_element_internals_shadow_root(internals) == none)

    var failures = 0
    _ = html_element_internals_form(internals) catch:
        failures += 1
        recover none
    _ = html_element_internals_will_validate(internals) catch:
        failures += 1
        recover false
    _ = html_element_internals_validation_message(internals) catch:
        failures += 1
        recover ak.string_new()
    _ = html_element_internals_check_validity(internals) catch:
        failures += 1
        recover false
    _ = html_element_internals_report_validity(internals) catch:
        failures += 1
        recover false
    _ = html_element_internals_labels(internals) catch:
        failures += 1
        recover none
    html_element_internals_set_validity(internals, HtmlValidityStateFlags(), none, none) catch:
        failures += 1
    html_element_internals_set_form_value(internals, .string(ak.string_from_str("v")), none) catch:
        failures += 1
    assert(failures == 8)
    let name = test_html_take_dom_exception_name()
    assert(ak.fly_string_eq_string_view(&name, ak.sv("NotSupportedError")))