dymokomi/luce-browser-engine
Source of release 0.6.1
0.6.1 / src / web / html / tests_html_element_1.lucb
#==============================================================================================
#
# tests_html_element_1 - tests of HTMLElement construction and its attribute reflections
# (region r22)
#
# DESCRIPTION:
# Ladybird has no unit tests for these files (HTMLElement is tested through
# Tests/LibWeb/Text, which needs scripting), so these pin the C++ behaviour: elements made
# by the ElementFactory's tag dispatch (HTMLElement for the phrasing and sectioning names
# and valid custom element names, HTMLUnknownElement otherwise) with their class, mixin
# tables and virtuals; the interned event names and boolean attributes; then, over
# attributes, translate, dir, title, contenteditable, hidden, inert, popover,
# accessKeyLabel, draggable, spellcheck, writingSuggestions, autocapitalize, autocorrect,
# the computed heading level and the default ARIA roles.
#
# Elements live in r29's settings realm with a heap document. A seam of another region is
# bypassed: attributes are put in the element's NamedNodeMap and the attribute change
# steps are run through the attribute_changed virtual (Element::attribute_changed's
# caller goes on to r42's invalidate_style_after_attribute_change). Trees are built with
# TreeNode::append_child (r42's invalidate_node_style).
#
#==============================================================================================
# mark: Helpers =================================================================================
## The qualified name `local_name` in the HTML namespace.
func test_html_qualified_name(local_name: str) -> DomQualifiedName:
var qualified_name: DomQualifiedName = ---
dom_qualified_name_construct(&qualified_name, ak.fly_string_from_str(local_name), none, namespace_html)
return qualified_name
## An HTML element made by the ElementFactory's tag dispatch (create_html_element).
func test_html_create(document: DomDocument*, local_name: str) -> HtmlElement*:
let element = as_html_element(dom_create_html_element(document, test_html_qualified_name(local_name)))
return element
## A plain HTMLElement named `local_name` (realm.create<HTMLElement>), whatever the name.
func test_html_plain(document: DomDocument*, local_name: str) -> HtmlElement*:
let element = realm_create_html_element(bind_platform_object_realm(document.bind_platform_object()), document, test_html_qualified_name(local_name))
return element
## A document in a new VM with an agent and a settings realm, with LibWeb's names interned.
func test_html_setup() -> (Vm*, Realm*, DomDocument*):
web_initialize_strings()
let (vm, realm) = web_test_create_vm_and_settings_realm()
return (vm, realm, test_html_heap_document(realm))
## The attribute `name` of `element`, if it has one.
func test_html_find_attribute(element: DomElement*, name: ak.FlyString) -> DomAttr*?:
let attributes = element.m_attributes else return none
for attr in ak.vector_span(&attributes.m_attributes):
let local_name = dom_attr_local_name(attr)
if ak.fly_string_eq_fly_string(&local_name, &name):
return attr
return none
## Puts the attribute `name` with `value` in an HTML element's attribute list (no change steps:
## Element's steps for some attributes, dir among them, invalidate style, region r42).
func test_html_put(element: HtmlElement*, name: ak.FlyString, value: str) -> ak.String?:
let dom_element = element.dom_element()
if dom_element.m_attributes == none:
dom_element.m_attributes = dom_named_node_map_create(dom_element)
let new_value = ak.string_from_str(value)
if let attr = test_html_find_attribute(dom_element, name):
let old_value = attr.m_value
attr.m_value = new_value
return old_value
let attributes = dom_element.m_attributes else trap("no attribute map")
ak.vector_append_t(&attributes.m_attributes, dom_attr_create_fly_string(dom_node_document(element.dom_node()), name, new_value, dom_element))
return none
## Sets the attribute `name` of an HTML element to `value` and runs the attribute change steps
## through the attribute_changed virtual.
func test_html_set(element: HtmlElement*, name: ak.FlyString, value: str):
let old_value = test_html_put(element, name, value)
dom_element_attribute_changed(element.dom_element(), name, old_value, ak.string_from_str(value), none)
## Removes the attribute `name` of an HTML element and runs the attribute change steps.
func test_html_remove(element: HtmlElement*, name: ak.FlyString):
let dom_element = element.dom_element()
let attr = test_html_find_attribute(dom_element, name) else return
let attributes = dom_element.m_attributes else trap("no attribute map")
for index in 0..<ak.vector_size(&attributes.m_attributes):
if *ak.vector_at(&attributes.m_attributes, index) == attr:
ak.vector_remove(&attributes.m_attributes, index)
break
dom_element_attribute_changed(dom_element, name, attr.m_value, none, none)
## `child` appended to `parent` by TreeNode::append_child.
func test_html_append(parent: HtmlElement*, child: HtmlElement*):
tree_node_append_child(parent.dom_node(), child.dom_node())
## Whether an optional String holds `text`.
func test_html_optional_is(value: ak.String?, text: str) -> bool:
let present = value else return false
return ak.string_eq_string_view(&present, ak.sv(text))
## Whether a StringView holds `text`.
func test_html_view_is(view: ak.StringView, text: str) -> bool:
return ak.string_view_equals(&view, text)
# mark: Construction ============================================================================
test "ElementFactory: HTMLElement for sectioning, phrasing and custom names, HTMLUnknownElement otherwise":
let (vm, realm, document) = test_html_setup()
defer web_test_destroy_vm(vm)
let names: str[5] = ["article", "b", "wbr", "noscript", "x-foo"]
for name in names:
let element = test_html_create(document, name)
assert(element.cell().class_id == html_element_class.first_id, name)
assert(element.cell().vtable == (const gc.CellVTable*)&html_element_vtable, name)
assert(element.element.aria_mixin.vtable == &html_element_aria_mixin_vtable)
assert(element.element.slottable_mixin.vtable == &html_element_slottable_mixin_vtable)
assert(element.element.animatable.vtable == &html_element_animatable_vtable)
assert(element.global_event_handlers.vtable == &html_element_global_event_handlers_vtable)
assert(element.global_event_handlers.class_id == element.cell().class_id)
assert(element.form_associated_element.class_id == element.cell().class_id)
assert(dom_node_optional_fly_string_is(dom_element_namespace_uri(element.dom_element()), namespace_html))
assert(dom_node_document(element.dom_node()) == document)
assert(bind_platform_object_realm(element.bind_platform_object()) == realm)
let unknown = as_html_element(dom_create_html_element(document, test_html_qualified_name("foo")))
assert(is_html_unknown_element(unknown) and is_html_element(unknown))
assert(unknown.cell().vtable == (const gc.CellVTable*)&html_unknown_element_vtable)
assert(unknown.element.aria_mixin.vtable == &html_unknown_element_aria_mixin_vtable)
assert(unknown.element.slottable_mixin.vtable == &html_unknown_element_slottable_mixin_vtable)
assert(unknown.element.animatable.vtable == &html_unknown_element_animatable_vtable)
assert(unknown.global_event_handlers.vtable == &html_unknown_element_global_event_handlers_vtable)
assert(unknown.form_associated_element.vtable == &html_unknown_element_form_associated_element_vtable)
# The initial state of HTMLElement's members (HTMLElement.h), of its HTMLOrSVGElement and
# FormAssociatedElement bases.
let article = test_html_create(document, "article")
assert(article.m_content_editable_state == HtmlContentEditableState.inherit)
assert(html_element_popover_visibility_state(article) == HtmlElementPopoverVisibilityState.hidden)
assert(not html_element_is_inert(article) and not article.m_click_in_progress and not article.m_popover_showing_or_hiding)
assert(article.m_popover_toggle_task_tracker == none and html_element_opened_in_popover_mode(article) == none)
assert(article.html_or_svg_element.m_dataset == none and ak.string_is_empty(&article.html_or_svg_element.m_cryptographic_nonce))
match article.form_associated_element.m_face_submission_value:
.empty: assert(true)
_: trap("FACE submission value is Empty")
test "HTMLElement's virtuals: node name, is_html_element, the mixins' element and the bindings name":
let (vm, realm, document) = test_html_setup()
defer web_test_destroy_vm(vm)
let article = test_html_create(document, "article")
let name = dom_node_node_name(article.dom_node())
assert(ak.fly_string_eq_string_view(&name, ak.sv("ARTICLE")))
assert(dom_node_is_html_element(article.dom_node()))
assert(html_global_event_handlers_global_event_handlers_to_event_target(&article.global_event_handlers, html_event_names_click) == article.dom_event_target())
assert(html_form_associated_element_form_associated_element_to_html_element(&article.form_associated_element) == article)
assert(not html_element_is_labelable(article))
assert(html_element_labels(article) == none)
assert(not html_element_uses_button_layout(article))
var command = ak.string_from_str("toggle-popover")
assert(not html_element_is_valid_command(article, &command))
assert(not html_element_is_form_associated_custom_element(article))
assert(not html_element_is_focusable_impl(article.dom_event_target()))
test "HTML::EventNames, Gamepad::EventNames and AttributeNames::is_boolean_attribute":
web_initialize_strings()
assert(ak.fly_string_eq_string_view(&html_event_names_dom_content_loaded, ak.sv("DOMContentLoaded")))
assert(ak.fly_string_eq_string_view(&html_event_names_webkit_animation_end, ak.sv("webkitAnimationEnd")))
assert(ak.fly_string_eq_string_view(&html_event_names_beforetoggle, ak.sv("beforetoggle")))
assert(ak.fly_string_eq_string_view(&html_event_names_load, ak.sv("load")))
assert(ak.fly_string_eq_string_view(&gamepad_event_names_gamepadconnected, ak.sv("gamepadconnected")))
let booleans: str[6] = ["checked", "CHECKED", "Hidden", "default", "switch", "willvalidate"]
for name in booleans:
assert(html_is_boolean_attribute(ak.fly_string_from_str(name)), name)
let others: str[4] = ["class", "value", "checkedx", ""]
for name in others:
assert(not html_is_boolean_attribute(ak.fly_string_from_str(name)), name)
# mark: Reflected attributes ====================================================================
test "translate follows the translation mode; dir is limited to ltr, rtl and auto; title":
let (vm, realm, document) = test_html_setup()
defer web_test_destroy_vm(vm)
let parent = test_html_create(document, "section")
let child = test_html_create(document, "b")
test_html_append(parent, child)
assert(html_element_translate(child))
test_html_set(parent, html_attribute_names_translate, "no")
assert(not html_element_translate(parent) and not html_element_translate(child))
test_html_set(child, html_attribute_names_translate, "YES")
assert(html_element_translate(child))
assert(test_html_view_is(html_element_dir(child), ""))
_ = test_html_put(child, html_attribute_names_dir, "RTL")
assert(test_html_view_is(html_element_dir(child), "rtl"))
_ = test_html_put(child, html_attribute_names_dir, "Auto")
assert(test_html_view_is(html_element_dir(child), "auto"))
_ = test_html_put(child, html_attribute_names_dir, "sideways")
assert(test_html_view_is(html_element_dir(child), ""))
assert(html_element_title(child) == none)
test_html_set(child, html_attribute_names_title, "A title")
assert(test_html_optional_is(html_element_title(child), "A title"))
test "contenteditable: the attribute's states, contentEditable, editing hosts and the SyntaxError":
let (vm, realm, document) = test_html_setup()
defer web_test_destroy_vm(vm)
let host = test_html_create(document, "article")
let inner = test_html_create(document, "b")
test_html_append(host, inner)
assert(test_html_view_is(html_element_content_editable(host), "inherit"))
test_html_set(host, html_attribute_names_contenteditable, "")
assert(html_element_content_editable_state(host) == HtmlContentEditableState.true_)
assert(test_html_view_is(html_element_content_editable(host), "true"))
assert(dom_node_is_editing_host(host.dom_node()) and html_element_is_content_editable(host))
assert(html_element_is_focusable_impl(host.dom_event_target()))
assert(html_element_is_content_editable(inner) and not dom_node_is_editing_host(inner.dom_node()))
test_html_set(host, html_attribute_names_contenteditable, "PLAINTEXT-ONLY")
assert(test_html_view_is(html_element_content_editable(host), "plaintext-only"))
test_html_set(host, html_attribute_names_contenteditable, "False")
assert(test_html_view_is(html_element_content_editable(host), "false"))
assert(not html_element_is_content_editable(host) and not html_element_is_content_editable(inner))
test_html_set(host, html_attribute_names_contenteditable, "bogus")
assert(test_html_view_is(html_element_content_editable(host), "inherit"))
test_html_set(host, html_attribute_names_contenteditable, "TRUE")
assert(test_html_view_is(html_element_content_editable(host), "true"))
test_html_remove(host, html_attribute_names_contenteditable)
assert(html_element_content_editable_state(host) == HtmlContentEditableState.inherit)
assert(not html_element_is_content_editable(inner))
# did_receive_focus and did_lose_focus do nothing outside the true and plaintext-only states.
html_element_did_receive_focus_impl(host.dom_element())
html_element_did_lose_focus_impl(host.dom_element())
var failed = false
html_element_set_content_editable(host, ak.sv("maybe")) catch:
failed = true
recover ()
assert(failed)
match idl_take_pending_exception():
.idl_dom_exception(exception):
let exception_name = idl_dom_exception_name(exception)
assert(ak.fly_string_eq_string_view(&exception_name, ak.sv("SyntaxError")))
_: trap("expected a DOMException")
test "hidden: until-found, true and false; setting the falsy values removes nothing that is not there":
let (vm, realm, document) = test_html_setup()
defer web_test_destroy_vm(vm)
let element = test_html_create(document, "section")
match html_element_hidden(element):
.bool_(flag): assert(not flag)
_: trap("hidden is false")
test_html_set(element, html_attribute_names_hidden, "")
match html_element_hidden(element):
.bool_(flag): assert(flag)
_: trap("hidden is true")
test_html_set(element, html_attribute_names_hidden, "UNTIL-found")
match html_element_hidden(element):
.string(string): assert(ak.string_eq_string_view(&string, ak.sv("until-found")))
_: trap("hidden is until-found")
let plain = test_html_create(document, "i")
html_element_set_hidden(plain, .bool_(false))
html_element_set_hidden(plain, .f64_(0.0))
html_element_set_hidden(plain, .f64_(math.nan))
html_element_set_hidden(plain, .string(ak.string_new()))
html_element_set_hidden(plain, .empty)
assert(not dom_element_has_attribute(plain.dom_element(), html_attribute_names_hidden))
test "inert: the attribute makes the subtree inert, except subtrees with their own inert attribute":
let (vm, realm, document) = test_html_setup()
defer web_test_destroy_vm(vm)
let root = test_html_create(document, "section")
let child = test_html_create(document, "b")
let grandchild = test_html_create(document, "i")
let escaping = test_html_create(document, "u")
let escaping_child = test_html_create(document, "em")
test_html_append(root, child)
test_html_append(child, grandchild)
test_html_append(root, escaping)
test_html_append(escaping, escaping_child)
test_html_set(escaping, html_attribute_names_inert, "")
assert(html_element_is_inert(escaping) and html_element_is_inert(escaping_child))
html_element_set_subtree_inertness(escaping, false)
test_html_set(root, html_attribute_names_inert, "")
assert(html_element_is_inert(root) and html_element_is_inert(child) and html_element_is_inert(grandchild))
assert(not html_element_is_inert(escaping) and not html_element_is_inert(escaping_child))
assert(dom_node_is_inert(grandchild.dom_node()))
test_html_remove(root, html_attribute_names_inert)
assert(not html_element_is_inert(root) and not html_element_is_inert(grandchild))
# Inserting under an inert parent makes the inserted subtree inert unless it has the attribute.
html_element_set_inert(root, true)
let inserted = test_html_create(document, "s")
test_html_append(root, inserted)
html_element_inserted_impl(inserted.dom_node())
assert(html_element_is_inert(inserted))
test "popover: the attribute's states, as the IDL attribute reflects them":
let (vm, realm, document) = test_html_setup()
defer web_test_destroy_vm(vm)
let element = test_html_create(document, "section")
assert(html_element_popover(element) == none)
let values: str[6] = ["", "AUTO", "hint", "HiNt", "manual", "bogus"]
let states: str[6] = ["auto", "auto", "hint", "hint", "manual", "manual"]
for index in 0..<values.length:
test_html_set(element, html_attribute_names_popover, values[index])
assert(test_html_optional_is(html_element_popover(element), states[index]), values[index])
assert(html_element_popover_value_to_state(none) == none)
test "accessKeyLabel is the accesskey attribute when it is one code point":
let (vm, realm, document) = test_html_setup()
defer web_test_destroy_vm(vm)
let element = test_html_create(document, "b")
var label = html_element_access_key_label(element)
assert(ak.string_is_empty(&label))
let values: str[4] = ["a", "\u{e9}", "ab", ""]
let labels: str[4] = ["a", "\u{e9}", "", ""]
for index in 0..<values.length:
test_html_set(element, html_attribute_names_accesskey, values[index])
label = html_element_access_key_label(element)
assert(ak.string_eq_string_view(&label, ak.sv(labels[index])), values[index])
test "draggable, spellcheck and writingSuggestions, with their defaults and inheritance":
let (vm, realm, document) = test_html_setup()
defer web_test_destroy_vm(vm)
let parent = test_html_create(document, "section")
let child = test_html_create(document, "b")
test_html_append(parent, child)
assert(not html_element_draggable(child))
test_html_set(child, html_attribute_names_draggable, "TRUE")
assert(html_element_draggable(child))
test_html_set(child, html_attribute_names_draggable, "false")
assert(not html_element_draggable(child))
assert(not html_element_spellcheck(child))
test_html_set(parent, html_attribute_names_spellcheck, "")
assert(html_element_spellcheck(parent) and html_element_spellcheck(child))
test_html_set(child, html_attribute_names_spellcheck, "False")
assert(not html_element_spellcheck(child))
test_html_set(child, html_attribute_names_spellcheck, "bogus")
assert(html_element_spellcheck(child))
test_html_set(parent, html_attribute_names_spellcheck, "false")
assert(not html_element_spellcheck(child))
var suggestions = html_element_writing_suggestions(child)
assert(ak.string_eq_string_view(&suggestions, ak.sv("true")))
test_html_set(parent, html_attribute_names_writingsuggestions, "FALSE")
suggestions = html_element_writing_suggestions(child)
assert(ak.string_eq_string_view(&suggestions, ak.sv("false")))
test_html_set(child, html_attribute_names_writingsuggestions, "bogus")
suggestions = html_element_writing_suggestions(child)
assert(ak.string_eq_string_view(&suggestions, ak.sv("true")))
test "autocapitalize and autocorrect: the attribute's states and the defaults":
let (vm, realm, document) = test_html_setup()
defer web_test_destroy_vm(vm)
let element = test_html_create(document, "section")
assert(html_element_own_autocapitalization_hint(element) == HtmlElementAutocapitalizationHint.default)
var value = html_element_autocapitalize(element)
assert(ak.string_is_empty(&value))
let keywords: str[8] = ["off", "NONE", "on", "sentences", "Words", "characters", "bogus", ""]
let results: str[8] = ["none", "none", "sentences", "sentences", "words", "characters", "sentences", ""]
for index in 0..<keywords.length:
test_html_set(element, html_attribute_names_autocapitalize, keywords[index])
value = html_element_autocapitalize(element)
assert(ak.string_eq_string_view(&value, ak.sv(results[index])), keywords[index])
assert(html_element_autocorrect(element))
test_html_set(element, html_attribute_names_autocorrect, "OFF")
assert(html_element_used_autocorrection_state(element) == HtmlElementAutocorrectionState.off and not html_element_autocorrect(element))
test_html_set(element, html_attribute_names_autocorrect, "")
assert(html_element_autocorrect(element))
test "computed heading level: h1-h6, headingoffset on inclusive ancestors, headingreset, capped at 9":
let (vm, realm, document) = test_html_setup()
defer web_test_destroy_vm(vm)
let outer = test_html_create(document, "section")
let middle = test_html_create(document, "article")
let heading = test_html_plain(document, "h2")
test_html_append(outer, middle)
test_html_append(middle, heading)
assert(html_element_computed_heading_level(heading) == 2 and html_element_computed_heading_offset(heading) == 0)
test_html_set(middle, html_attribute_names_headingoffset, "3")
test_html_set(outer, html_attribute_names_headingoffset, "1")
assert(html_element_computed_heading_offset(heading) == 4 and html_element_computed_heading_level(heading) == 6)
test_html_set(middle, html_attribute_names_headingreset, "")
assert(html_element_computed_heading_offset(heading) == 3)
test_html_set(heading, html_attribute_names_headingoffset, " 20x")
assert(html_element_computed_heading_offset(heading) == 23 and html_element_computed_heading_level(heading) == 9)
test_html_set(heading, html_attribute_names_headingoffset, "-1")
assert(html_element_computed_heading_offset(heading) == 3)
let names: str[6] = ["h1", "h2", "h3", "h4", "h5", "h6"]
for index in 0..<names.length:
assert(html_element_computed_heading_level(test_html_plain(document, names[index])) == (u32)(index + 1))
test "the default ARIA roles of HTML elements":
let (vm, realm, document) = test_html_setup()
defer web_test_destroy_vm(vm)
let names: str[18] = ["address", "article", "aside", "b", "code", "dd", "dir", "dt", "em", "figure", "hgroup", "main", "mark", "nav", "s", "search", "strong", "summary"]
let roles: AriaRole[18] = [AriaRole.group, AriaRole.article, AriaRole.complementary, AriaRole.generic, AriaRole.code, AriaRole.definition, AriaRole.list, AriaRole.term, AriaRole.emphasis, AriaRole.figure, AriaRole.group, AriaRole.main, AriaRole.mark, AriaRole.navigation, AriaRole.deletion, AriaRole.search, AriaRole.strong, AriaRole.button]
for index in 0..<names.length:
let element = test_html_plain(document, names[index])
assert(aria_mixin_default_role(&element.element.aria_mixin) == roles[index], names[index])
assert(aria_mixin_default_role(&test_html_plain(document, "span").element.aria_mixin) == none)
let footer = test_html_plain(document, "footer")
let header = test_html_plain(document, "header")
assert(aria_mixin_default_role(&footer.element.aria_mixin) == AriaRole.contentinfo)
assert(aria_mixin_default_role(&header.element.aria_mixin) == AriaRole.banner)
let article = test_html_plain(document, "article")
test_html_append(article, footer)
let nav = test_html_plain(document, "nav")
test_html_append(nav, header)
assert(aria_mixin_default_role(&footer.element.aria_mixin) == AriaRole.sectionfooter)
assert(aria_mixin_default_role(&header.element.aria_mixin) == AriaRole.sectionheader)