Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.1

0.6.1 / src / web / html / parser / stack_of_open_elements.lucb

#==============================================================================================
#
#   stack_of_open_elements - HTML::StackOfOpenElements: the tree builder's stack of open elements
#
#   DESCRIPTION:
#       Port of LibWeb/HTML/Parser/StackOfOpenElements.cpp (lines 1-202) and the inline members
#       of StackOfOpenElements.h (lines 26-51). The stack grows downwards: the first element is
#       the html element, the last one the current node. `s_base_list` holds FlyStrings longer
#       than a short string, so it is built at startup by html_stack_of_open_elements_initialize
#       _strings (web_initialize_strings runs it) in memory.heap, where module globals live.
#
#==============================================================================================

## Whether html_stack_of_open_elements_initialize_strings has run.
var html_stack_of_open_elements_strings_initialized: bool

## Ported from StackOfOpenElements.cpp:16 the initializer of Web::HTML::s_base_list.
pub func html_stack_of_open_elements_initialize_strings():
    if html_stack_of_open_elements_strings_initialized:
        return
    with memory.heap:
        let names: str[10] = ["applet", "caption", "html", "table", "td", "th", "marquee", "object", "select", "template"]
        for name in names:
            ak.vector_append_t(&html_s_base_list, ak.fly_string_from_str(name))
    html_stack_of_open_elements_strings_initialized = true

# mark: StackOfOpenElements.cpp =================================================================

## Ported from StackOfOpenElements.cpp:31 Web::HTML::StackOfOpenElements::visit_edges.
pub func html_stack_of_open_elements_visit_edges(this: HtmlStackOfOpenElements*, visitor: gc.CellVisitor*):
    gc.cell_visitor_visit_vector(visitor, &this.m_elements)

## Ported from StackOfOpenElements.cpp:36 Web::HTML::StackOfOpenElements::pop.
pub func html_stack_of_open_elements_pop(this: HtmlStackOfOpenElements*) -> DomElement*:
    let element = ak.vector_take_last(&this.m_elements)

    if let call = this.m_on_element_popped.call:
        call(this.m_on_element_popped.context, element)

    return element

## Ported from StackOfOpenElements.cpp:47 Web::HTML::StackOfOpenElements::has_in_scope_impl.
# https://html.spec.whatwg.org/multipage/parsing.html#has-an-element-in-the-specific-scope
func html_stack_of_open_elements_has_in_scope_impl_fly_string(this: const HtmlStackOfOpenElements*, tag_name: ak.FlyString, list: const ak.Vector[ak.FlyString]*, check_math_and_svg: HtmlStackOfOpenElementsCheckMathAndSvg) -> bool:
    var i = ak.vector_size(&this.m_elements)
    while i > 0:
        i -= 1
        let element = *ak.vector_at_const(&this.m_elements, i)
        if html_parser_element_namespace_is(element, namespace_html) and html_parser_local_name_is(element, tag_name):
            return true
        if html_parser_element_namespace_is(element, namespace_html) and html_parser_fly_string_list_contains(list, dom_element_local_name(element)):
            return false
        if check_math_and_svg == HtmlStackOfOpenElementsCheckMathAndSvg.yes and html_parser_element_namespace_is(element, namespace_svg) and html_parser_local_name_is_one_of(element, [svg_tag_names_foreign_object, svg_tag_names_desc, svg_tag_names_title]):
            return false
        if check_math_and_svg == HtmlStackOfOpenElementsCheckMathAndSvg.yes and html_parser_element_namespace_is(element, namespace_math_ml) and html_parser_local_name_is_one_of(element, [math_ml_tag_names_mi, math_ml_tag_names_mo, math_ml_tag_names_mn, math_ml_tag_names_ms, math_ml_tag_names_mtext, math_ml_tag_names_annotation_xml]):
            return false
    trap("VERIFY_NOT_REACHED")

## Ported from StackOfOpenElements.cpp:63 Web::HTML::StackOfOpenElements::has_in_scope.
# https://html.spec.whatwg.org/multipage/parsing.html#has-an-element-in-scope
pub func html_stack_of_open_elements_has_in_scope_fly_string(this: const HtmlStackOfOpenElements*, tag_name: ak.FlyString) -> bool:
    return html_stack_of_open_elements_has_in_scope_impl_fly_string(this, tag_name, &html_s_base_list, HtmlStackOfOpenElementsCheckMathAndSvg.yes)

## Ported from StackOfOpenElements.cpp:69 Web::HTML::StackOfOpenElements::has_in_scope_impl.
# https://html.spec.whatwg.org/multipage/parsing.html#has-an-element-in-the-specific-scope
func html_stack_of_open_elements_has_in_scope_impl_dom_element(this: const HtmlStackOfOpenElements*, target_node: DomElement*, list: const ak.Vector[ak.FlyString]*) -> bool:
    var i = ak.vector_size(&this.m_elements)
    while i > 0:
        i -= 1
        let element = *ak.vector_at_const(&this.m_elements, i)
        if element == target_node:
            return true
        if html_parser_element_namespace_is(element, namespace_html) and html_parser_fly_string_list_contains(list, dom_element_local_name(element)):
            return false
        if html_parser_element_namespace_is(element, namespace_svg) and html_parser_local_name_is_one_of(element, [svg_tag_names_foreign_object, svg_tag_names_desc, svg_tag_names_title]):
            return false
        if html_parser_element_namespace_is(element, namespace_math_ml) and html_parser_local_name_is_one_of(element, [math_ml_tag_names_mi, math_ml_tag_names_mo, math_ml_tag_names_mn, math_ml_tag_names_ms, math_ml_tag_names_mtext, math_ml_tag_names_annotation_xml]):
            return false
    trap("VERIFY_NOT_REACHED")

## Ported from StackOfOpenElements.cpp:85 Web::HTML::StackOfOpenElements::has_in_scope.
# https://html.spec.whatwg.org/multipage/parsing.html#has-an-element-in-scope
pub func html_stack_of_open_elements_has_in_scope_dom_element(this: const HtmlStackOfOpenElements*, target_node: DomElement*) -> bool:
    return html_stack_of_open_elements_has_in_scope_impl_dom_element(this, target_node, &html_s_base_list)

## Ported from StackOfOpenElements.cpp:91 Web::HTML::StackOfOpenElements::has_in_button_scope.
# https://html.spec.whatwg.org/multipage/parsing.html#has-an-element-in-button-scope
pub func html_stack_of_open_elements_has_in_button_scope(this: const HtmlStackOfOpenElements*, tag_name: ak.FlyString) -> bool:
    var list = ak.vector_clone(&html_s_base_list)
    ak.vector_append_t(&list, ak.fly_string_from_str("button"))
    return html_stack_of_open_elements_has_in_scope_impl_fly_string(this, tag_name, &list, HtmlStackOfOpenElementsCheckMathAndSvg.yes)

## Ported from StackOfOpenElements.cpp:99 Web::HTML::StackOfOpenElements::has_in_table_scope.
# https://html.spec.whatwg.org/multipage/parsing.html#has-an-element-in-table-scope
pub func html_stack_of_open_elements_has_in_table_scope(this: const HtmlStackOfOpenElements*, tag_name: ak.FlyString) -> bool:
    var list: ak.Vector[ak.FlyString]
    ak.vector_append_t(&list, ak.fly_string_from_str("html"))
    ak.vector_append_t(&list, ak.fly_string_from_str("table"))
    ak.vector_append_t(&list, ak.fly_string_from_str("template"))
    return html_stack_of_open_elements_has_in_scope_impl_fly_string(this, tag_name, &list, HtmlStackOfOpenElementsCheckMathAndSvg.no)

## Ported from StackOfOpenElements.cpp:105 Web::HTML::StackOfOpenElements::has_in_list_item_scope.
# https://html.spec.whatwg.org/multipage/parsing.html#has-an-element-in-list-item-scope
pub func html_stack_of_open_elements_has_in_list_item_scope(this: const HtmlStackOfOpenElements*, tag_name: ak.FlyString) -> bool:
    var list = ak.vector_clone(&html_s_base_list)
    ak.vector_append_t(&list, ak.fly_string_from_str("ol"))
    ak.vector_append_t(&list, ak.fly_string_from_str("ul"))
    return html_stack_of_open_elements_has_in_scope_impl_fly_string(this, tag_name, &list, HtmlStackOfOpenElementsCheckMathAndSvg.yes)

## Ported from StackOfOpenElements.cpp:113 Web::HTML::StackOfOpenElements::contains.
pub func html_stack_of_open_elements_contains(this: const HtmlStackOfOpenElements*, element: DomElement*) -> bool:
    for element_on_stack in ak.vector_span_const(&this.m_elements):
        if element == element_on_stack:
            return true
    return false

## Ported from StackOfOpenElements.cpp:122 Web::HTML::StackOfOpenElements::contains_template_element.
pub func html_stack_of_open_elements_contains_template_element(this: const HtmlStackOfOpenElements*) -> bool:
    for element in ak.vector_span_const(&this.m_elements):
        if not html_parser_element_namespace_is(element, namespace_html):
            continue
        if html_parser_local_name_is(element, html_tag_names_template):
            return true
    return false

## Ported from StackOfOpenElements.cpp:133 Web::HTML::StackOfOpenElements::pop_until_an_element_with_tag_name_has_been_popped.
pub func html_stack_of_open_elements_pop_until_an_element_with_tag_name_has_been_popped(this: HtmlStackOfOpenElements*, tag_name: ak.FlyString):
    while not html_parser_element_namespace_is(*ak.vector_last(&this.m_elements), namespace_html) or not html_parser_local_name_is(*ak.vector_last(&this.m_elements), tag_name):
        _ = html_stack_of_open_elements_pop(this)
    _ = html_stack_of_open_elements_pop(this)

## Ported from StackOfOpenElements.cpp:140 Web::HTML::StackOfOpenElements::topmost_special_node_below.
pub func html_stack_of_open_elements_topmost_special_node_below(this: HtmlStackOfOpenElements*, formatting_element: DomElement*) -> DomElement*?:
    var found_element: DomElement*? = none
    var i = ak.vector_size(&this.m_elements)
    while i > 0:
        i -= 1
        let element = *ak.vector_at(&this.m_elements, i)
        if element == formatting_element:
            break
        if html_parser_is_special_tag(dom_element_local_name(element), dom_element_namespace_uri(element)):
            found_element = element
    return found_element

## Ported from StackOfOpenElements.cpp:152 Web::HTML::StackOfOpenElements::last_element_with_tag_name.
pub func html_stack_of_open_elements_last_element_with_tag_name(this: HtmlStackOfOpenElements*, tag_name: ak.FlyString) -> HtmlStackOfOpenElementsLastElementResult:
    var i = (isize)ak.vector_size(&this.m_elements) - 1
    while i >= 0:
        let element = *ak.vector_at(&this.m_elements, (usize)i)
        if html_parser_local_name_is(element, tag_name):
            return HtmlStackOfOpenElementsLastElementResult(element = element, index = i)
        i -= 1
    return HtmlStackOfOpenElementsLastElementResult(element = none, index = -1)

## Ported from StackOfOpenElements.cpp:162 Web::HTML::StackOfOpenElements::element_immediately_above.
pub func html_stack_of_open_elements_element_immediately_above(this: HtmlStackOfOpenElements*, target: DomElement*) -> DomElement*?:
    var found_target = false
    var i = ak.vector_size(&this.m_elements)
    while i > 0:
        i -= 1
        let element = *ak.vector_at(&this.m_elements, i)
        if element == target:
            found_target = true
        elif found_target:
            return element
    return none

## Ported from StackOfOpenElements.cpp:174 Web::HTML::StackOfOpenElements::remove
## (`remove_first_matching([&element](auto& other) { return other.ptr() == &element; })`).
pub func html_stack_of_open_elements_remove(this: HtmlStackOfOpenElements*, element: DomElement*):
    html_parser_remove_first_element(&this.m_elements, element)

## Ported from StackOfOpenElements.cpp:181 Web::HTML::StackOfOpenElements::replace.
pub func html_stack_of_open_elements_replace(this: HtmlStackOfOpenElements*, to_remove: DomElement*, to_add: DomElement*):
    for i in 0..<ak.vector_size(&this.m_elements):
        if *ak.vector_at(&this.m_elements, i) == to_remove:
            ak.vector_remove(&this.m_elements, i)
            ak.vector_insert(&this.m_elements, i, to_add)
            break

## Ported from StackOfOpenElements.cpp:192 Web::HTML::StackOfOpenElements::insert_immediately_below.
pub func html_stack_of_open_elements_insert_immediately_below(this: HtmlStackOfOpenElements*, element_to_add: DomElement*, target: DomElement*):
    for i in 0..<ak.vector_size(&this.m_elements):
        if *ak.vector_at(&this.m_elements, i) == target:
            ak.vector_insert(&this.m_elements, i + 1, element_to_add)
            break

# mark: StackOfOpenElements.h ===================================================================

## Ported from StackOfOpenElements.h:26 Web::HTML::StackOfOpenElements::first.
pub func html_stack_of_open_elements_first(this: HtmlStackOfOpenElements*) -> DomElement*:
    return *ak.vector_first(&this.m_elements)

## Ported from StackOfOpenElements.h:27 Web::HTML::StackOfOpenElements::last.
pub func html_stack_of_open_elements_last(this: HtmlStackOfOpenElements*) -> DomElement*:
    return *ak.vector_last(&this.m_elements)

## Ported from StackOfOpenElements.h:29 Web::HTML::StackOfOpenElements::is_empty.
pub func html_stack_of_open_elements_is_empty(this: const HtmlStackOfOpenElements*) -> bool:
    return ak.vector_is_empty(&this.m_elements)

## Ported from StackOfOpenElements.h:30 Web::HTML::StackOfOpenElements::push.
pub func html_stack_of_open_elements_push(this: HtmlStackOfOpenElements*, element: DomElement*):
    ak.vector_append_t(&this.m_elements, element)

## Ported from StackOfOpenElements.h:32 Web::HTML::StackOfOpenElements::set_on_element_popped.
pub func html_stack_of_open_elements_set_on_element_popped(this: HtmlStackOfOpenElements*, on_element_popped: ak.Function1[DomElement*, unit]):
    this.m_on_element_popped = on_element_popped

## Ported from StackOfOpenElements.h:37 Web::HTML::StackOfOpenElements::current_node.
pub func html_stack_of_open_elements_current_node_const(this: const HtmlStackOfOpenElements*) -> DomElement*:
    return *ak.vector_at_const(&this.m_elements, ak.vector_size(&this.m_elements) - 1)

## Ported from StackOfOpenElements.h:38 Web::HTML::StackOfOpenElements::current_node.
pub func html_stack_of_open_elements_current_node(this: HtmlStackOfOpenElements*) -> DomElement*:
    return *ak.vector_last(&this.m_elements)

## Ported from StackOfOpenElements.h:50 Web::HTML::StackOfOpenElements::elements (const).
pub func html_stack_of_open_elements_elements_const(this: const HtmlStackOfOpenElements*) -> const ak.Vector[DomElement*]*:
    return &this.m_elements

## Ported from StackOfOpenElements.h:51 Web::HTML::StackOfOpenElements::elements.
pub func html_stack_of_open_elements_elements(this: HtmlStackOfOpenElements*) -> ak.Vector[DomElement*]*:
    return &this.m_elements