dymokomi/luce-browser-engine
Source of release 0.6.1
0.6.1 / src / web / html / parser / tests_html_parser_1.lucb
#==============================================================================================
#
# tests_html_parser_1 - tests of the stack of open elements and the list of active formatting
# elements (region r21)
#
# DESCRIPTION:
# Ladybird has no unit tests for these files (its parser is tested through the html5lib
# Text tests, which need scripting). These pin the C++ behaviour of every member: the five
# scope predicates with their stop lists and the MathML/SVG boundaries, popping with the
# popped-element hook, the topmost special node, the last element with a tag name, the
# element immediately above, replace / insert / remove, and the list's markers, Noah's Ark
# clause, own token copies, replace / insert_at / find_index and clearing to the last
# marker. Elements are made by hand in r14's test realm (r18's test_realm_element), as
# creating them through the element factory needs regions r17, r22 and r42.
#
#==============================================================================================
# mark: Helpers =================================================================================
## A test realm with a hand-built document; the VM is destroyed by the caller.
struct ParserTestWorld:
var vm: Vm*
var realm: Realm*
var document: DomDocument*
## Makes the test VM, realm and document.
func parser_test_world() -> ParserTestWorld:
web_initialize_strings()
let vm = web_test_create_vm()
let realm = web_test_create_realm(vm)
return ParserTestWorld(vm = vm, realm = realm, document = test_realm_document(realm))
## An element of `local_name` in the HTML namespace, made by hand.
func parser_test_html(world: ParserTestWorld*, local_name: str) -> DomElement*:
return test_realm_element(world.realm, world.document, local_name, namespace_html)
## An element of `local_name` in `namespace_`, made by hand.
func parser_test_foreign(world: ParserTestWorld*, local_name: str, namespace_: ak.FlyString) -> DomElement*:
return test_realm_element(world.realm, world.document, local_name, namespace_)
## Pushes HTML elements named `names` (top to bottom) onto `stack`.
func parser_test_push_html(world: ParserTestWorld*, stack: HtmlStackOfOpenElements*, names: const str[]):
for name in names:
html_stack_of_open_elements_push(stack, parser_test_html(world, name))
## The local names of the stack, top first, joined by spaces.
func parser_test_stack_names(stack: HtmlStackOfOpenElements*) -> ak.String:
var builder = ak.string_builder_new()
for element in ak.vector_span(&stack.m_elements):
if not ak.string_builder_is_empty(&builder):
ak.string_builder_append(&builder, " ")
html_parser_append_fly_string(&builder, dom_element_local_name(element))
return ak.string_builder_to_string_without_validation(&builder)
## Asserts the stack's names.
func parser_test_expect_stack(stack: HtmlStackOfOpenElements*, expected: str):
let names = parser_test_stack_names(stack)
if ak.string_eq_string_view(&names, ak.sv(expected)):
return
let view = ak.string_bytes_as_string_view(&names)
print(f"stack: expected [{expected}], got [{(str)ak.string_view_bytes(&view)}]")
assert(false, "stack mismatch")
## A start or end tag token named `name`, without attributes.
func parser_test_tag(type_: html_syntax.HtmlTokenType, name: str) -> html_syntax.HtmlToken:
var token: html_syntax.HtmlToken
html_syntax.html_token_construct(&token, type_)
html_syntax.html_token_set_tag_name(&token, ak.fly_string_from_str(name))
return token
# mark: StackOfOpenElements =====================================================================
test "StackOfOpenElements: has_in_scope stops at the base list, the button, table and list item lists":
var world = parser_test_world()
defer web_test_destroy_vm(world.vm)
let p = ak.fly_string_from_str("p")
let li = ak.fly_string_from_str("li")
var stack: HtmlStackOfOpenElements
parser_test_push_html(&world, &stack, ["html", "body", "div", "p", "span"])
assert(html_stack_of_open_elements_has_in_scope_fly_string(&stack, p))
assert(html_stack_of_open_elements_has_in_button_scope(&stack, p))
assert(html_stack_of_open_elements_has_in_table_scope(&stack, p))
assert(html_stack_of_open_elements_has_in_list_item_scope(&stack, p))
# A button stops button scope only; a table stops all four.
html_stack_of_open_elements_push(&stack, parser_test_html(&world, "button"))
assert(html_stack_of_open_elements_has_in_scope_fly_string(&stack, p))
assert(not html_stack_of_open_elements_has_in_button_scope(&stack, p))
assert(html_stack_of_open_elements_has_in_table_scope(&stack, p))
html_stack_of_open_elements_push(&stack, parser_test_html(&world, "td"))
assert(not html_stack_of_open_elements_has_in_scope_fly_string(&stack, p))
assert(html_stack_of_open_elements_has_in_table_scope(&stack, p))
html_stack_of_open_elements_push(&stack, parser_test_html(&world, "table"))
assert(not html_stack_of_open_elements_has_in_table_scope(&stack, p))
# ol and ul stop list item scope only.
var lists: HtmlStackOfOpenElements
parser_test_push_html(&world, &lists, ["html", "body", "li", "ul"])
assert(html_stack_of_open_elements_has_in_scope_fly_string(&lists, li))
assert(not html_stack_of_open_elements_has_in_list_item_scope(&lists, li))
test "StackOfOpenElements: SVG and MathML integration elements end the scope, except table scope":
var world = parser_test_world()
defer web_test_destroy_vm(world.vm)
let p = ak.fly_string_from_str("p")
var stack: HtmlStackOfOpenElements
parser_test_push_html(&world, &stack, ["html", "body", "p"])
html_stack_of_open_elements_push(&stack, parser_test_foreign(&world, "svg", namespace_svg))
assert(html_stack_of_open_elements_has_in_scope_fly_string(&stack, p))
html_stack_of_open_elements_push(&stack, parser_test_foreign(&world, "foreignObject", namespace_svg))
assert(not html_stack_of_open_elements_has_in_scope_fly_string(&stack, p))
assert(not html_stack_of_open_elements_has_in_button_scope(&stack, p))
assert(html_stack_of_open_elements_has_in_table_scope(&stack, p))
var math_stack: HtmlStackOfOpenElements
parser_test_push_html(&world, &math_stack, ["html", "body", "p"])
html_stack_of_open_elements_push(&math_stack, parser_test_foreign(&world, "mi", namespace_math_ml))
assert(not html_stack_of_open_elements_has_in_list_item_scope(&math_stack, p))
# An element with the tag name in another namespace is not the HTML element looked for:
# the walk continues to html, which stops it.
var foreign_p: HtmlStackOfOpenElements
parser_test_push_html(&world, &foreign_p, ["html", "body"])
html_stack_of_open_elements_push(&foreign_p, parser_test_foreign(&world, "p", namespace_svg))
assert(not html_stack_of_open_elements_has_in_scope_fly_string(&foreign_p, p))
test "StackOfOpenElements: has_in_scope of an element, contains and contains_template_element":
var world = parser_test_world()
defer web_test_destroy_vm(world.vm)
var stack: HtmlStackOfOpenElements
parser_test_push_html(&world, &stack, ["html", "body"])
let div = parser_test_html(&world, "div")
html_stack_of_open_elements_push(&stack, div)
assert(html_stack_of_open_elements_has_in_scope_dom_element(&stack, div))
assert(html_stack_of_open_elements_contains(&stack, div))
assert(not html_stack_of_open_elements_contains(&stack, parser_test_html(&world, "div")))
html_stack_of_open_elements_push(&stack, parser_test_html(&world, "table"))
assert(not html_stack_of_open_elements_has_in_scope_dom_element(&stack, div))
assert(not html_stack_of_open_elements_contains_template_element(&stack))
html_stack_of_open_elements_push(&stack, parser_test_foreign(&world, "template", namespace_svg))
assert(not html_stack_of_open_elements_contains_template_element(&stack))
html_stack_of_open_elements_push(&stack, parser_test_html(&world, "template"))
assert(html_stack_of_open_elements_contains_template_element(&stack))
## The popped-element hook's captures: the names popped, in order.
struct ParserTestPopped:
var builder: ak.StringBuilder
## The popped-element hook of the test: appends the popped element's name.
func parser_test_on_popped(context: void*?, element: DomElement*):
let popped = (ParserTestPopped*)(context else trap("no captures"))
html_parser_append_fly_string(&popped.builder, dom_element_local_name(element))
ak.string_builder_append(&popped.builder, ";")
test "StackOfOpenElements: popping runs the hook; pop_until skips same-named foreign elements":
var world = parser_test_world()
defer web_test_destroy_vm(world.vm)
var popped = ParserTestPopped(builder = ak.string_builder_new())
var stack: HtmlStackOfOpenElements
html_stack_of_open_elements_set_on_element_popped(&stack, ak.Function1[DomElement*, unit](call = parser_test_on_popped, context = &popped))
parser_test_push_html(&world, &stack, ["html", "body", "p", "b"])
html_stack_of_open_elements_push(&stack, parser_test_foreign(&world, "p", namespace_svg))
html_stack_of_open_elements_push(&stack, parser_test_html(&world, "i"))
let current = html_stack_of_open_elements_current_node(&stack)
assert(test_fly_is(dom_element_local_name(current), "i"))
assert(test_fly_is(dom_element_local_name(html_stack_of_open_elements_first(&stack)), "html"))
assert(html_stack_of_open_elements_last(&stack) == current)
html_stack_of_open_elements_pop_until_an_element_with_tag_name_has_been_popped(&stack, ak.fly_string_from_str("p"))
let popped_names = ak.string_builder_string_view(&popped.builder)
assert(ak.string_view_eq_string_view(&popped_names, ak.sv("i;p;b;p;")))
parser_test_expect_stack(&stack, "html body")
test "StackOfOpenElements: the topmost special node, the last element with a tag name and the element above":
var world = parser_test_world()
defer web_test_destroy_vm(world.vm)
var stack: HtmlStackOfOpenElements
parser_test_push_html(&world, &stack, ["html", "body"])
let b = parser_test_html(&world, "b")
html_stack_of_open_elements_push(&stack, b)
let div = parser_test_html(&world, "div")
html_stack_of_open_elements_push(&stack, div)
html_stack_of_open_elements_push(&stack, parser_test_html(&world, "i"))
html_stack_of_open_elements_push(&stack, parser_test_html(&world, "address"))
let span = parser_test_html(&world, "span")
html_stack_of_open_elements_push(&stack, span)
# The topmost (closest to the html element) special element below b is the div.
assert(html_stack_of_open_elements_topmost_special_node_below(&stack, b) == div)
assert(html_stack_of_open_elements_topmost_special_node_below(&stack, span) == none)
let last_div = html_stack_of_open_elements_last_element_with_tag_name(&stack, ak.fly_string_from_str("div"))
assert(last_div.element == div and last_div.index == 3)
let missing = html_stack_of_open_elements_last_element_with_tag_name(&stack, ak.fly_string_from_str("table"))
assert(missing.element == none and missing.index == -1)
assert(html_stack_of_open_elements_element_immediately_above(&stack, div) == b)
assert(html_stack_of_open_elements_element_immediately_above(&stack, html_stack_of_open_elements_first(&stack)) == none)
test "StackOfOpenElements: replace, insert_immediately_below and remove":
var world = parser_test_world()
defer web_test_destroy_vm(world.vm)
var stack: HtmlStackOfOpenElements
parser_test_push_html(&world, &stack, ["html", "body"])
let a = parser_test_html(&world, "a")
html_stack_of_open_elements_push(&stack, a)
let p = parser_test_html(&world, "p")
html_stack_of_open_elements_push(&stack, p)
html_stack_of_open_elements_replace(&stack, a, parser_test_html(&world, "em"))
parser_test_expect_stack(&stack, "html body em p")
html_stack_of_open_elements_insert_immediately_below(&stack, parser_test_html(&world, "i"), p)
parser_test_expect_stack(&stack, "html body em p i")
html_stack_of_open_elements_remove(&stack, p)
parser_test_expect_stack(&stack, "html body em i")
# Replacing or removing an element not on the stack changes nothing.
html_stack_of_open_elements_remove(&stack, a)
html_stack_of_open_elements_replace(&stack, a, p)
parser_test_expect_stack(&stack, "html body em i")
assert(not html_stack_of_open_elements_is_empty(&stack))
# mark: ListOfActiveFormattingElements ==========================================================
test "ListOfActiveFormattingElements: markers, lookups before the last marker and clearing":
var world = parser_test_world()
defer web_test_destroy_vm(world.vm)
var list: HtmlListOfActiveFormattingElements
assert(html_list_of_active_formatting_elements_is_empty(&list))
let b = parser_test_html(&world, "b")
var b_token = parser_test_tag(html_syntax.HtmlTokenType.start_tag, "b")
html_list_of_active_formatting_elements_add(&list, b, &b_token)
html_list_of_active_formatting_elements_add_marker(&list)
let i = parser_test_html(&world, "i")
var i_token = parser_test_tag(html_syntax.HtmlTokenType.start_tag, "i")
html_list_of_active_formatting_elements_add(&list, i, &i_token)
assert(html_list_of_active_formatting_elements_contains(&list, b))
assert(html_list_of_active_formatting_elements_find_index(&list, i) == 2)
assert(html_list_of_active_formatting_elements_find_index(&list, parser_test_html(&world, "i")) == none)
assert(html_list_of_active_formatting_elements_last_element_with_tag_name_before_marker(&list, ak.fly_string_from_str("i")) == i)
# b is before the marker: not found.
assert(html_list_of_active_formatting_elements_last_element_with_tag_name_before_marker(&list, ak.fly_string_from_str("b")) == none)
# Each entry owns a copy of its token.
let entry = ak.vector_at(&list.m_entries, 2)
let own_token = entry.token else trap("a token")
assert(own_token != &i_token)
assert(test_fly_is(html_syntax.html_token_tag_name(own_token), "i"))
assert(html_list_of_active_formatting_elements_entry_is_marker(ak.vector_at(&list.m_entries, 1)))
html_list_of_active_formatting_elements_clear_up_to_the_last_marker(&list)
assert(ak.vector_size(&list.m_entries) == 1)
assert(html_list_of_active_formatting_elements_last_element_with_tag_name_before_marker(&list, ak.fly_string_from_str("b")) == b)
html_list_of_active_formatting_elements_clear_up_to_the_last_marker(&list)
assert(html_list_of_active_formatting_elements_is_empty(&list))
test "ListOfActiveFormattingElements: Noah's Ark keeps three equal elements after the last marker":
var world = parser_test_world()
defer web_test_destroy_vm(world.vm)
var list: HtmlListOfActiveFormattingElements
var token = parser_test_tag(html_syntax.HtmlTokenType.start_tag, "b")
var elements: ak.Vector[DomElement*]
for _unused in 0..<4:
let b = parser_test_html(&world, "b")
ak.vector_append_t(&elements, b)
html_list_of_active_formatting_elements_add(&list, b, &token)
# The fourth equal element removed the earliest one.
assert(ak.vector_size(&list.m_entries) == 3)
assert(not html_list_of_active_formatting_elements_contains(&list, *ak.vector_at(&elements, 0)))
assert(html_list_of_active_formatting_elements_find_index(&list, *ak.vector_at(&elements, 1)) == 0)
# A b in another namespace, or an element with another name, is not equal.
html_list_of_active_formatting_elements_add(&list, parser_test_foreign(&world, "b", namespace_svg), &token)
html_list_of_active_formatting_elements_add(&list, parser_test_html(&world, "i"), &token)
assert(ak.vector_size(&list.m_entries) == 5)
let b5 = parser_test_html(&world, "b")
html_list_of_active_formatting_elements_add(&list, b5, &token)
assert(ak.vector_size(&list.m_entries) == 5)
assert(not html_list_of_active_formatting_elements_contains(&list, *ak.vector_at(&elements, 1)))
# After a marker, the elements before it do not count.
html_list_of_active_formatting_elements_add_marker(&list)
html_list_of_active_formatting_elements_add(&list, parser_test_html(&world, "b"), &token)
html_list_of_active_formatting_elements_add(&list, parser_test_html(&world, "b"), &token)
html_list_of_active_formatting_elements_add(&list, parser_test_html(&world, "b"), &token)
assert(ak.vector_size(&list.m_entries) == 9)
test "ListOfActiveFormattingElements: replace, insert_at, remove and create_own_token":
var world = parser_test_world()
defer web_test_destroy_vm(world.vm)
var list: HtmlListOfActiveFormattingElements
let a = parser_test_html(&world, "a")
var a_token = parser_test_tag(html_syntax.HtmlTokenType.start_tag, "a")
var attribute = html_syntax.html_token_attribute_new()
attribute.local_name = ak.fly_string_from_str("href")
attribute.value = ak.string_from_str("x")
html_syntax.html_token_add_attribute(&a_token, attribute)
html_list_of_active_formatting_elements_add(&list, a, &a_token)
let copy = html_list_of_active_formatting_elements_create_own_token(&a_token)
assert(html_syntax.html_token_is_start_tag(copy))
assert(test_fly_is(html_syntax.html_token_tag_name(copy), "a"))
assert(html_syntax.html_token_attribute_count(copy) == 1)
assert(test_string_is(html_syntax.html_token_attribute(copy, ak.fly_string_from_str("href")) else trap("href"), "x"))
let em = parser_test_html(&world, "em")
var em_token = parser_test_tag(html_syntax.HtmlTokenType.start_tag, "em")
html_list_of_active_formatting_elements_replace(&list, a, em, &em_token)
assert(html_list_of_active_formatting_elements_find_index(&list, em) == 0)
assert(not html_list_of_active_formatting_elements_contains(&list, a))
let entry_token = ak.vector_at(&list.m_entries, 0).token else trap("a token")
assert(test_fly_is(html_syntax.html_token_tag_name(entry_token), "em"))
let s = parser_test_html(&world, "s")
html_list_of_active_formatting_elements_insert_at(&list, 0, s, &em_token)
assert(html_list_of_active_formatting_elements_find_index(&list, s) == 0)
assert(html_list_of_active_formatting_elements_find_index(&list, em) == 1)
html_list_of_active_formatting_elements_remove(&list, s)
assert(html_list_of_active_formatting_elements_find_index(&list, em) == 0)
assert(ak.vector_size(html_list_of_active_formatting_elements_entries(&list)) == 1)