dymokomi/luce-browser-engine
Source of release 0.6.0
0.6.0 / src / web / html / tests_html_elements.lucb
#==============================================================================================
#
# tests_html_elements - region r27's elements end to end, against the reference build
#
# DESCRIPTION:
# Runs the cases of tests_html_elements_cases (a local oracle's output: the reference
# Ladybird build on the same inputs) through the port and compares:
#
# doc the HTML parser end to end, bytes in, DOM tree out (the html5lib
# tree-construction dump of tests_html_parser_support, with a template's
# contents and a DOCTYPE's identifiers as html5lib prints them)
# frag the HTML fragment parsing algorithm in a context element (enabled by
# region r26: the fragment's temporary document is initialized with a
# ListOfAvailableImages)
# fontsize HTMLFontElement::parse_legacy_font_size
# hints is_presentational_hint and apply_presentational_hints of a parsed element
# role the default ARIA role (as its enum value)
# canvas HTMLCanvasElement's width, height and bitmap size
# marquee HTMLMarqueeElement's scrollAmount and scrollDelay
# script HTMLScriptElement's preparation state after parsing (scripting disabled)
# query querySelectorAll's elements: :heading(n) over the cached heading levels
# (region r38's selector engine)
#
# Documents are web_test_create_document's (page/tests_page): the real Document
# constructor in a settings realm, of type HTML, parsed with scripting disabled.
#
#==============================================================================================
# mark: The html5lib dump ========================================================================
## Dumps one node and its subtree in the html5lib format: tests_html_parser_support's dump, with
## the identifiers of a DOCTYPE and the contents of a template.
func test_html_elements_dump_node(builder: ak.StringBuilder*, node: DomNode*, level: usize):
if let doctype = as_if_dom_document_type(node):
parser_test_dump_indent(builder, level)
ak.string_builder_append(builder, "<!DOCTYPE ")
html_parser_append_string(builder, dom_document_type_name(doctype))
let public_id = dom_document_type_public_id(doctype)
let system_id = dom_document_type_system_id(doctype)
if not ak.string_is_empty(&public_id) or not ak.string_is_empty(&system_id):
ak.string_builder_append(builder, " \"")
html_parser_append_string(builder, public_id)
ak.string_builder_append(builder, "\" \"")
html_parser_append_string(builder, system_id)
ak.string_builder_append(builder, "\"")
ak.string_builder_append(builder, ">\n")
return
if let element = as_if_dom_element(node):
parser_test_dump_indent(builder, level)
ak.string_builder_append(builder, "<")
parser_test_dump_namespace_prefix(builder, dom_element_namespace_uri(element))
html_parser_append_fly_string(builder, dom_element_local_name(element))
ak.string_builder_append(builder, ">\n")
parser_test_dump_attributes(builder, element, level + 1)
if let template = as_if_html_template_element(element):
parser_test_dump_indent(builder, level + 1)
ak.string_builder_append(builder, "content\n")
for child in dom_node_children(html_template_element_content(template).dom_node()):
test_html_elements_dump_node(builder, child, level + 2)
for child in dom_node_children(node):
test_html_elements_dump_node(builder, child, level + 1)
return
parser_test_dump_node(builder, node, level)
## An HTML document of `realm` parsed from `html` with scripting disabled.
func test_html_elements_parse(realm: Realm*, html: str) -> DomDocument*:
let document = web_test_create_document(realm, DocumentDomDocumentType.html)
let parser = html_parser_create(document, ak.sv(html), HtmlParserScriptingMode.disabled, ak.sv("UTF-8"))
html_parser_run_url(parser, dom_document_url(document))
return document
## The first element of `document`, in tree order, whose local name is `tag`.
func test_html_elements_first(document: DomDocument*, tag: str) -> DomElement*:
let name = ak.fly_string_from_str(tag)
for node in dom_node_subtree(document.dom_node()):
if let element = as_if_dom_element(node):
let local_name = dom_element_local_name(element)
if ak.fly_string_eq_fly_string(&local_name, &name):
return element
trap("no element of that name")
## Appends a number and a newline.
func test_html_elements_put_number(builder: ak.StringBuilder*, value: i64):
let text = ak.string_number_i64(value)
ak.string_builder_append_string_view(builder, ak.string_bytes_as_string_view(&text))
# mark: Running a case ===========================================================================
## The port's output for one case, in the oracle's format.
func test_html_elements_run(realm: Realm*, test_case: const TestHtmlElementsCase*) -> ak.String:
var out = ak.string_builder_new()
let mode = test_case.mode
if mode == "doc":
let document = test_html_elements_parse(realm, test_case.arguments[0])
for child in dom_node_children(document.dom_node()):
test_html_elements_dump_node(&out, child, 0)
elif mode == "frag":
let document = test_html_elements_parse(realm, "<!DOCTYPE html><body>")
let context = dom_create_element(document, ak.fly_string_from_str(test_case.arguments[0]), namespace_html) else trap("create_element")
let body = dom_document_body(document) else trap("no body")
_ = dom_node_append_child(body.dom_node(), context.dom_node()) else trap("append_child")
let nodes = html_parser_parse_html_fragment(context, ak.sv(test_case.arguments[1])) else trap("parse_html_fragment")
for node in &ak.vector_span_const(&nodes):
test_html_elements_dump_node(&out, *node else trap("null node"), 0)
elif mode == "fontsize":
if let keyword = html_font_element_parse_legacy_font_size(ak.sv(test_case.arguments[0])):
ak.string_builder_append_string_view(&out, css_data.string_from_keyword(keyword))
ak.string_builder_append(&out, "\n")
else:
ak.string_builder_append(&out, "none\n")
elif mode == "hints":
let document = test_html_elements_parse(realm, test_case.arguments[1])
let element = test_html_elements_first(document, test_case.arguments[0])
if let attributes = element.m_attributes:
for i in 0..<dom_named_node_map_length(attributes):
let attribute = dom_named_node_map_item(attributes, (u32)i) else trap("NamedNodeMap::item in range")
let name = dom_attr_name(attribute)
ak.string_builder_append(&out, "hint ")
html_parser_append_fly_string(&out, name)
ak.string_builder_append(&out, " 1\n" if dom_element_is_presentational_hint(element, name) else " 0\n")
var properties: ak.Vector[CssStyleProperty]
dom_element_apply_presentational_hints(element, &properties)
for property in &ak.vector_span_const(&properties):
html_parser_append_fly_string(&out, css_data.string_from_property_id(property.property_id))
ak.string_builder_append(&out, ": ")
html_parser_append_string(&out, css_style_value_to_string(property.value, css_syntax.SerializationMode.normal))
ak.string_builder_append(&out, "\n")
elif mode == "role":
let document = test_html_elements_parse(realm, test_case.arguments[1])
let element = test_html_elements_first(document, test_case.arguments[0])
if let role = aria_mixin_default_role(&element.aria_mixin):
test_html_elements_put_number(&out, (i64)(i32)role)
ak.string_builder_append(&out, "\n")
else:
ak.string_builder_append(&out, "none\n")
elif mode == "canvas":
let document = test_html_elements_parse(realm, test_case.arguments[0])
let canvas = as_html_canvas_element(test_html_elements_first(document, "canvas"))
let size = html_canvas_element_bitmap_size_for_canvas(canvas)
test_html_elements_put_number(&out, (i64)html_canvas_element_width(canvas))
ak.string_builder_append(&out, " ")
test_html_elements_put_number(&out, (i64)html_canvas_element_height(canvas))
ak.string_builder_append(&out, " ")
test_html_elements_put_number(&out, (i64)size.m_width)
ak.string_builder_append(&out, "x")
test_html_elements_put_number(&out, (i64)size.m_height)
ak.string_builder_append(&out, "\n")
elif mode == "marquee":
let document = test_html_elements_parse(realm, test_case.arguments[0])
let marquee = as_html_marquee_element(test_html_elements_first(document, "marquee"))
test_html_elements_put_number(&out, (i64)html_marquee_element_scroll_amount(marquee))
ak.string_builder_append(&out, " ")
test_html_elements_put_number(&out, (i64)html_marquee_element_scroll_delay(marquee))
ak.string_builder_append(&out, "\n")
elif mode == "script":
let document = test_html_elements_parse(realm, test_case.arguments[0])
for node in dom_node_subtree(document.dom_node()):
let script = as_if_html_script_element(node) else continue
ak.string_builder_append(&out, "already_started=")
test_html_elements_put_number(&out, 1 if script.m_already_started else 0)
ak.string_builder_append(&out, " type=")
test_html_elements_put_number(&out, (i64)(i32)script.m_script_type)
ak.string_builder_append(&out, " parser_inserted=")
test_html_elements_put_number(&out, 1 if html_script_element_is_parser_inserted(script) else 0)
ak.string_builder_append(&out, " force_async=")
test_html_elements_put_number(&out, 1 if script.m_force_async else 0)
ak.string_builder_append(&out, " async=")
test_html_elements_put_number(&out, 1 if html_script_element_async(script) else 0)
ak.string_builder_append(&out, "\n")
elif mode == "query":
let document = test_html_elements_parse(realm, test_case.arguments[1])
let list = dom_parent_node_query_selector_all(document.dom_parent_node(), ak.sv(test_case.arguments[0])) else trap("querySelectorAll")
for i in 0..<dom_node_list_length(list):
let element = as_dom_element(dom_node_list_item(list, i) else trap("item in range"))
html_parser_append_string(&out, dom_element_get_attribute_value(element, html_attribute_names_id))
ak.string_builder_append(&out, "\n")
else:
trap("unknown mode")
return ak.string_builder_to_string_without_validation(&out)
## Runs every case of `mode`, printing each mismatch, and fails if any.
func test_html_elements_run_mode(mode: str):
web_initialize_strings()
let (vm, realm) = web_test_create_vm_and_settings_realm()
defer web_test_destroy_vm(vm)
var failures = 0
var count = 0
# The documents and their trees live in the VM's heap (the test's own allocator is a small
# fixed buffer).
let allocator: memory.Allocator = vm_heap(vm)
with allocator:
for test_case in &test_html_elements_cases:
if test_case.mode != mode:
continue
count += 1
let actual = test_html_elements_run(realm, test_case)
if ak.string_eq_string_view(&actual, ak.sv(test_case.expected)):
continue
failures += 1
let view = ak.string_bytes_as_string_view(&actual)
print(f"{mode} {test_case.arguments[0]} {test_case.arguments[1]}\nexpected:\n{test_case.expected}actual:\n{(str)ak.string_view_bytes(&view)}")
assert(count > 0, "no cases of the mode")
assert(failures == 0, "cases differ from the reference build")
# mark: Tests ====================================================================================
test "scenario: whole documents through the HTML parser, as the reference build's DOM trees":
test_html_elements_run_mode("doc")
test "scenario: HTML fragments parsed in a context element, as the reference build's trees":
test_html_elements_run_mode("frag")
test "HTMLFontElement::parse_legacy_font_size matches the reference build":
test_html_elements_run_mode("fontsize")
test "presentational hints of div, p, headings, pre, br, hr, font, body, marquee, embed, frameset, tables, lists and img":
test_html_elements_run_mode("hints")
test "default ARIA roles of the region's elements":
test_html_elements_run_mode("role")
test "HTMLMarqueeElement's scrollAmount and scrollDelay reflect with their defaults":
test_html_elements_run_mode("marquee")
test "HTMLScriptElement: prepare the script element with scripting disabled stops at step 19":
test_html_elements_run_mode("script")
test "scenario: :heading(n) selects by HTMLHeadingElement's heading level, as the reference build":
test_html_elements_run_mode("query")