Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.0

0.6.0 / src / web / html / parser / html_parser_4.lucb

#==============================================================================================
#
#   html_parser_4 - HTML::HTMLParser: implied end tags, "after body", reconstructing the active
#                   formatting elements, the adoption agency algorithm and the special category
#
#   DESCRIPTION:
#       Port of LibWeb/HTML/Parser/HTMLParser.cpp (lines 1729-2191). The labels of
#       reconstruct_the_active_formatting_elements (Rewind, Advance, Create) are two loops: the
#       rewind loop walks back through the list, the advance/create loop recreates the entries
#       after it, starting at Create when the rewind reached the first entry.
#
#==============================================================================================

# mark: Implied end tags ========================================================================

## Ported from HTMLParser.cpp:1730 Web::HTML::HTMLParser::generate_implied_end_tags.
# https://html.spec.whatwg.org/multipage/parsing.html#generate-implied-end-tags
pub func html_parser_generate_implied_end_tags(this: HtmlParser*, exception: ak.FlyString = ak.FlyString()):
    while not html_parser_local_name_is(html_parser_current_element(this), exception) and html_parser_local_name_is_one_of(html_parser_current_element(this), [html_tag_names_dd, html_tag_names_dt, html_tag_names_li, html_tag_names_optgroup, html_tag_names_option, html_tag_names_p, html_tag_names_rb, html_tag_names_rp, html_tag_names_rt, html_tag_names_rtc]):
        _ = html_stack_of_open_elements_pop(&this.m_stack_of_open_elements)

## Ported from HTMLParser.cpp:1737 Web::HTML::HTMLParser::generate_all_implied_end_tags_thoroughly.
# https://html.spec.whatwg.org/multipage/parsing.html#generate-implied-end-tags
pub func html_parser_generate_all_implied_end_tags_thoroughly(this: HtmlParser*):
    while html_parser_local_name_is_one_of(html_parser_current_element(this), [html_tag_names_caption, html_tag_names_colgroup, html_tag_names_dd, html_tag_names_dt, html_tag_names_li, html_tag_names_optgroup, html_tag_names_option, html_tag_names_p, html_tag_names_rb, html_tag_names_rp, html_tag_names_rt, html_tag_names_rtc, html_tag_names_tbody, html_tag_names_td, html_tag_names_tfoot, html_tag_names_th, html_tag_names_thead, html_tag_names_tr]):
        _ = html_stack_of_open_elements_pop(&this.m_stack_of_open_elements)

## Ported from HTMLParser.cpp:1743 Web::HTML::HTMLParser::close_a_p_element.
pub func html_parser_close_a_p_element(this: HtmlParser*):
    html_parser_generate_implied_end_tags(this, html_tag_names_p)
    if not html_parser_local_name_is(html_parser_current_element(this), html_tag_names_p):
        html_log_parse_error()
    html_stack_of_open_elements_pop_until_an_element_with_tag_name_has_been_popped(&this.m_stack_of_open_elements, html_tag_names_p)

# mark: The after body insertion modes ==========================================================

## Ported from HTMLParser.cpp:1753 Web::HTML::HTMLParser::handle_after_body.
# https://html.spec.whatwg.org/multipage/parsing.html#parsing-main-afterbody
pub func html_parser_handle_after_body(this: HtmlParser*, token: html_syntax.HtmlToken*):
    # -> A character token that is one of U+0009 CHARACTER TABULATION, U+000A LINE FEED (LF), U+000C FORM FEED (FF),
    #    U+000D CARRIAGE RETURN (CR), or U+0020 SPACE
    if html_syntax.html_token_is_character(token) and html_syntax.html_token_is_parser_whitespace(token):
        # Process the token using the rules for the "in body" insertion mode.
        html_parser_process_using_the_rules_for(this, HtmlParserInsertionMode.in_body, token)
        return

    # -> A comment token
    if html_syntax.html_token_is_comment(token):
        # Insert a comment as the last child of the first element in the stack of open elements (the html element).
        let insertion_location = html_stack_of_open_elements_first(&this.m_stack_of_open_elements)
        _ = dom_node_append_child(insertion_location.dom_node(), html_parser_create_comment(this, token).dom_node()) else trap("MUST: append_child")
        return

    # -> A DOCTYPE token
    if html_syntax.html_token_is_doctype(token):
        # Parse error. Ignore the token.
        html_log_parse_error()
        return

    # -> A start tag whose tag name is "html"
    if html_syntax.html_token_is_start_tag(token) and html_parser_tag_name_is(token, html_tag_names_html):
        # Process the token using the rules for the "in body" insertion mode.
        html_parser_process_using_the_rules_for(this, HtmlParserInsertionMode.in_body, token)
        return

    # -> An end tag whose tag name is "html"
    if html_syntax.html_token_is_end_tag(token) and html_parser_tag_name_is(token, html_tag_names_html):
        # If the parser was created as part of the HTML fragment parsing algorithm, this is a parse error; ignore the
        # token. (fragment case)
        if this.m_parsing_fragment:
            html_log_parse_error()
            return

        # Otherwise, switch the insertion mode to "after after body".
        this.m_insertion_mode = HtmlParserInsertionMode.after_after_body
        return

    # -> An end-of-file token
    if html_syntax.html_token_is_end_of_file(token):
        # Stop parsing.
        html_parser_stop_parsing(this)
        return

    # -> Anything else
    #    Parse error. Switch the insertion mode to "in body" and reprocess the token.
    html_log_parse_error()
    this.m_insertion_mode = HtmlParserInsertionMode.in_body
    html_parser_process_using_the_rules_for(this, HtmlParserInsertionMode.in_body, token)

## Ported from HTMLParser.cpp:1814 Web::HTML::HTMLParser::handle_after_after_body.
# https://html.spec.whatwg.org/multipage/parsing.html#the-after-after-body-insertion-mode
pub func html_parser_handle_after_after_body(this: HtmlParser*, token: html_syntax.HtmlToken*):
    # -> A comment token
    if html_syntax.html_token_is_comment(token):
        # Insert a comment as the last child of the Document object.
        let comment = html_parser_create_comment(this, token)
        _ = dom_node_append_child(html_parser_document(this).dom_node(), comment.dom_node()) else trap("MUST: append_child")
        return

    # -> A DOCTYPE token
    # -> A character token that is one of U+0009 CHARACTER TABULATION, U+000A LINE FEED (LF), U+000C FORM FEED (FF),
    #    U+000D CARRIAGE RETURN (CR), or U+0020 SPACE
    # -> A start tag whose tag name is "html"
    if html_syntax.html_token_is_doctype(token) or html_syntax.html_token_is_parser_whitespace(token) or (html_syntax.html_token_is_start_tag(token) and html_parser_tag_name_is(token, html_tag_names_html)):
        # Process the token using the rules for the "in body" insertion mode.
        html_parser_process_using_the_rules_for(this, HtmlParserInsertionMode.in_body, token)
        return

    # -> An end-of-file token
    if html_syntax.html_token_is_end_of_file(token):
        # Stop parsing.
        html_parser_stop_parsing(this)
        return

    # -> Anything else
    #    Parse error. Switch the insertion mode to "in body" and reprocess the token.
    html_log_parse_error()
    this.m_insertion_mode = HtmlParserInsertionMode.in_body
    html_parser_process_using_the_rules_for(this, this.m_insertion_mode, token)

# mark: The list of active formatting elements ==================================================

## Ported from HTMLParser.cpp:1849 Web::HTML::HTMLParser::reconstruct_the_active_formatting_elements.
# https://html.spec.whatwg.org/multipage/parsing.html#reconstruct-the-active-formatting-elements
pub func html_parser_reconstruct_the_active_formatting_elements(this: HtmlParser*):
    let entries = &this.m_list_of_active_formatting_elements.m_entries

    # 1. If there are no entries in the list of active formatting elements, then there is nothing to reconstruct; stop this algorithm.
    if html_list_of_active_formatting_elements_is_empty(&this.m_list_of_active_formatting_elements):
        return

    # 2. If the last (most recently added) entry in the list of active formatting elements is a marker, or if it is an element that is in the stack of open elements,
    #    then there is nothing to reconstruct; stop this algorithm.
    if html_list_of_active_formatting_elements_entry_is_marker(ak.vector_last(entries)):
        return

    if html_stack_of_open_elements_contains(&this.m_stack_of_open_elements, ak.vector_last(entries).element else trap("checked")):
        return

    # 3. Let entry be the last (most recently added) element in the list of active formatting elements.
    var index = ak.vector_size(entries) - 1

    # NOTE: Entry will never be null, but must be a pointer instead of a reference to allow rebinding.
    var entry = ak.vector_at(entries, index)

    # Rewind:
    var go_to_create = false
    while true:
        # 4. Rewind: If there are no entries before entry in the list of active formatting elements, then jump to the step labeled create.
        if index == 0:
            go_to_create = true
            break

        # 5. Let entry be the entry one earlier than entry in the list of active formatting elements.
        index -= 1
        entry = ak.vector_at(entries, index)

        # 6. If entry is neither a marker nor an element that is also in the stack of open elements, go to the step labeled rewind.
        if not html_list_of_active_formatting_elements_entry_is_marker(entry) and not html_stack_of_open_elements_contains(&this.m_stack_of_open_elements, entry.element else trap("checked")):
            continue
        break

    while true:
        if not go_to_create:
            # Advance:
            # 7. Advance: Let entry be the element one later than entry in the list of active formatting elements.
            index += 1
            entry = ak.vector_at(entries, index)
        go_to_create = false

        # Create:
        # 8. Create: Insert an HTML element for the token for which the element entry was created, to obtain new element.
        assert(not html_list_of_active_formatting_elements_entry_is_marker(entry))

        let entry_token = entry.token else trap("VERIFY: an element entry has its token")
        let new_element = html_parser_insert_html_element(this, entry_token)

        # 9. Replace the entry for entry in the list with an entry for new element.
        html_list_of_active_formatting_elements_replace(&this.m_list_of_active_formatting_elements, entry.element else trap("checked"), new_element, entry_token)

        # 10. If the entry for new element in the list of active formatting elements is not the last entry in the list, return to the step labeled advance.
        if index != ak.vector_size(entries) - 1:
            continue
        break

## Ported from HTMLParser.cpp:1902 Web::HTML::HTMLParser::run_the_adoption_agency_algorithm.
# https://html.spec.whatwg.org/multipage/parsing.html#adoption-agency-algorithm
pub func html_parser_run_the_adoption_agency_algorithm(this: HtmlParser*, token: html_syntax.HtmlToken*) -> HtmlParserAdoptionAgencyAlgorithmOutcome:
    # 1. Let subject be token's tag name.
    let subject = html_syntax.html_token_tag_name(token)

    # 2. If the current node is an HTML element whose tag name is subject,
    #    and the current node is not in the list of active formatting elements,
    #    then pop the current node off the stack of open elements, and return.
    if html_parser_local_name_is(html_parser_current_element(this), subject) and not html_list_of_active_formatting_elements_contains(&this.m_list_of_active_formatting_elements, html_parser_current_element(this)):
        _ = html_stack_of_open_elements_pop(&this.m_stack_of_open_elements)
        return HtmlParserAdoptionAgencyAlgorithmOutcome.do_nothing

    # 3. Let outerLoopCounter be 0.
    var outer_loop_counter: usize = 0

    # 4. While true:
    while true:
        # 1. If outerLoopCounter is greater than or equal to 8, then return.
        if outer_loop_counter >= 8:
            return HtmlParserAdoptionAgencyAlgorithmOutcome.do_nothing

        # 2. Increment outerLoopCounter by 1.
        outer_loop_counter += 1

        # 3. Let formattingElement be the last element in the list of active formatting elements that:
        #    - is between the end of the list and the last marker in the list, if any, or the start of the list otherwise, and
        #    - has the tag name subject.
        #    If there is no such element, then return and instead act as described in the "any other end tag" entry above.
        let formatting_element = html_list_of_active_formatting_elements_last_element_with_tag_name_before_marker(&this.m_list_of_active_formatting_elements, subject) else return HtmlParserAdoptionAgencyAlgorithmOutcome.run_any_other_end_tag_steps

        # 4. If formattingElement is not in the stack of open elements,
        if not html_stack_of_open_elements_contains(&this.m_stack_of_open_elements, formatting_element):
            # then this is a parse error;
            html_log_parse_error()
            # remove the element from the list,
            html_list_of_active_formatting_elements_remove(&this.m_list_of_active_formatting_elements, formatting_element)
            # and return.
            return HtmlParserAdoptionAgencyAlgorithmOutcome.do_nothing

        # 5. If formattingElement is in the stack of open elements, but the element is not in scope,
        if not html_stack_of_open_elements_has_in_scope_dom_element(&this.m_stack_of_open_elements, formatting_element):
            # then this is a parse error;
            html_log_parse_error()
            # return.
            return HtmlParserAdoptionAgencyAlgorithmOutcome.do_nothing

        # 6. If formattingElement is not the current node,
        if formatting_element != html_parser_current_element(this):
            # this is a parse error. (But do not return.)
            html_log_parse_error()

        # 7. Let furthestBlock be the topmost node in the stack of open elements that is lower in the stack than formattingElement,
        #    and is an element in the special category. There might not be one.
        # 8. If there is no furthestBlock,
        let maybe_furthest_block = html_stack_of_open_elements_topmost_special_node_below(&this.m_stack_of_open_elements, formatting_element)
        if maybe_furthest_block == none:
            # then the UA must first pop all the nodes from the bottom of the stack of open elements,
            # from the current node up to and including formattingElement,
            while html_parser_current_element(this) != formatting_element:
                _ = html_stack_of_open_elements_pop(&this.m_stack_of_open_elements)
            _ = html_stack_of_open_elements_pop(&this.m_stack_of_open_elements)

            # then remove formattingElement from the list of active formatting elements,
            html_list_of_active_formatting_elements_remove(&this.m_list_of_active_formatting_elements, formatting_element)
            # and finally return.
            return HtmlParserAdoptionAgencyAlgorithmOutcome.do_nothing
        let furthest_block = maybe_furthest_block else trap("checked")

        # 9. Let commonAncestor be the element immediately above formattingElement in the stack of open elements.
        let common_ancestor = html_stack_of_open_elements_element_immediately_above(&this.m_stack_of_open_elements, formatting_element)

        # 10. Let a bookmark note the position of formattingElement in the list of active formatting elements
        #     relative to the elements on either side of it in the list.
        var bookmark = html_list_of_active_formatting_elements_find_index(&this.m_list_of_active_formatting_elements, formatting_element) else trap("VERIFY: find_index().value()")

        # 11. Let node and lastNode be furthestBlock.
        var node: DomElement* = furthest_block
        var last_node: DomElement* = furthest_block

        # NOTE: Keep track of this for later
        var node_above_node = html_stack_of_open_elements_element_immediately_above(&this.m_stack_of_open_elements, node)

        # 12. Let innerLoopCounter be 0.
        var inner_loop_counter: usize = 0

        # 13. While true:
        while true:
            # 1. Increment innerLoopCounter by 1.
            inner_loop_counter += 1

            # 2. Let node be the element immediately above node in the stack of open elements,
            #    or if node is no longer in the stack of open elements (e.g. because it got removed by this algorithm),
            #    the element that was immediately above node in the stack of open elements before node was removed.
            node = node_above_node else trap("VERIFY: node")

            # NOTE: Keep track of this for later
            node_above_node = html_stack_of_open_elements_element_immediately_above(&this.m_stack_of_open_elements, node)

            # 3. If node is formattingElement, then break.
            if node == formatting_element:
                break

            # 4. If innerLoopCounter is greater than 3 and node is in the list of active formatting elements,
            var node_index = html_list_of_active_formatting_elements_find_index(&this.m_list_of_active_formatting_elements, node)
            if inner_loop_counter > 3 and node_index != none:
                if (node_index else trap("checked")) < bookmark:
                    bookmark -= 1
                # then remove node from the list of active formatting elements.
                html_list_of_active_formatting_elements_remove(&this.m_list_of_active_formatting_elements, node)
                node_index = none

            # 5. If node is not in the list of active formatting elements,
            if node_index == none:
                # then remove node from the stack of open elements and continue.
                html_stack_of_open_elements_remove(&this.m_stack_of_open_elements, node)
                continue
            let index = node_index else trap("checked")

            # 6. Create an element for the token for which the element node was created,
            #    in the HTML namespace, with commonAncestor as the intended parent;
            let entry_token = ak.vector_at(&this.m_list_of_active_formatting_elements.m_entries, index).token else trap("VERIFY: an element entry has its token")
            let element = html_parser_create_element_for(this, entry_token, namespace_html, (common_ancestor else trap("VERIFY: common ancestor")).dom_node())
            #    replace the entry for node in the list of active formatting elements with an entry for the new element,
            html_list_of_active_formatting_elements_replace(&this.m_list_of_active_formatting_elements, node, element, entry_token)
            #    replace the entry for node in the stack of open elements with an entry for the new element,
            html_stack_of_open_elements_replace(&this.m_stack_of_open_elements, node, element)
            #    and let node be the new element.
            node = element

            # 7. If lastNode is furthestBlock,
            if last_node == furthest_block:
                # then move the aforementioned bookmark to be immediately after the new node in the list of active formatting elements.
                bookmark = (html_list_of_active_formatting_elements_find_index(&this.m_list_of_active_formatting_elements, node) else trap("VERIFY: find_index().value()")) + 1

            # 8. Append lastNode to node.
            _ = dom_node_append_child(node.dom_node(), last_node.dom_node()) else trap("MUST: append_child")

            # 9. Set lastNode to node.
            last_node = node

        # 14. Insert whatever lastNode ended up being in the previous step at the appropriate place for inserting a node,
        #     but using commonAncestor as the override target.
        let adjusted_insertion_location = html_parser_find_appropriate_place_for_inserting_node(this, common_ancestor)
        dom_node_insert_before(adjusted_insertion_location.parent else trap("VERIFY: adjusted insertion location"), last_node.dom_node(), adjusted_insertion_location.insert_before_sibling, false)

        # 15. Create an element for the token for which formattingElement was created,
        #     in the HTML namespace, with furthestBlock as the intended parent.
        let formatting_element_index = html_list_of_active_formatting_elements_find_index(&this.m_list_of_active_formatting_elements, formatting_element)
        let entry = ak.vector_at(&this.m_list_of_active_formatting_elements.m_entries, formatting_element_index else trap("VERIFY: find_index().value()"))
        let token_data = html_list_of_active_formatting_elements_create_own_token(entry.token else trap("VERIFY: an element entry has its token"))
        let element = html_parser_create_element_for(this, token_data, namespace_html, furthest_block.dom_node())

        # 16. Take all of the child nodes of furthestBlock and append them to the element created in the last step.
        let children = dom_node_children_as_vector(furthest_block.dom_node())
        for child in ak.vector_span_const(&children):
            let removed = dom_node_remove_child(furthest_block.dom_node(), child else trap("VERIFY: child")) else trap("release_value")
            _ = dom_node_append_child(element.dom_node(), removed) else trap("MUST: append_child")

        # 17. Append that new element to furthestBlock.
        _ = dom_node_append_child(furthest_block.dom_node(), element.dom_node()) else trap("MUST: append_child")

        # 18. Remove formattingElement from the list of active formatting elements,
        #     and insert the new element into the list of active formatting elements at the position of the aforementioned bookmark.
        if formatting_element_index != none and (formatting_element_index else trap("checked")) < bookmark:
            bookmark -= 1
        html_list_of_active_formatting_elements_remove(&this.m_list_of_active_formatting_elements, formatting_element)
        html_list_of_active_formatting_elements_insert_at(&this.m_list_of_active_formatting_elements, bookmark, element, token_data)

        # 19. Remove formattingElement from the stack of open elements, and insert the new element
        #     into the stack of open elements immediately below the position of furthestBlock in that stack.
        html_stack_of_open_elements_remove(&this.m_stack_of_open_elements, formatting_element)
        html_stack_of_open_elements_insert_immediately_below(&this.m_stack_of_open_elements, element, furthest_block)

# mark: The special category ====================================================================

## Ported from HTMLParser.cpp:2088 Web::HTML::HTMLParser::is_special_tag.
# https://html.spec.whatwg.org/multipage/parsing.html#special
pub func html_parser_is_special_tag(tag_name: ak.FlyString, namespace_: ak.FlyString?) -> bool:
    if dom_node_optional_fly_string_is(namespace_, namespace_html):
        return dom_fly_string_is_one_of_fly_strings(tag_name, [
            html_tag_names_address,
            html_tag_names_applet,
            html_tag_names_area,
            html_tag_names_article,
            html_tag_names_aside,
            html_tag_names_base,
            html_tag_names_basefont,
            html_tag_names_bgsound,
            html_tag_names_blockquote,
            html_tag_names_body,
            html_tag_names_br,
            html_tag_names_button,
            html_tag_names_caption,
            html_tag_names_center,
            html_tag_names_col,
            html_tag_names_colgroup,
            html_tag_names_dd,
            html_tag_names_details,
            html_tag_names_dir,
            html_tag_names_div,
            html_tag_names_dl,
            html_tag_names_dt,
            html_tag_names_embed,
            html_tag_names_fieldset,
            html_tag_names_figcaption,
            html_tag_names_figure,
            html_tag_names_footer,
            html_tag_names_form,
            html_tag_names_frame,
            html_tag_names_frameset,
            html_tag_names_h1,
            html_tag_names_h2,
            html_tag_names_h3,
            html_tag_names_h4,
            html_tag_names_h5,
            html_tag_names_h6,
            html_tag_names_head,
            html_tag_names_header,
            html_tag_names_hgroup,
            html_tag_names_hr,
            html_tag_names_html,
            html_tag_names_iframe,
            html_tag_names_img,
            html_tag_names_input,
            html_tag_names_keygen,
            html_tag_names_li,
            html_tag_names_link,
            html_tag_names_listing,
            html_tag_names_main,
            html_tag_names_marquee,
            html_tag_names_menu,
            html_tag_names_meta,
            html_tag_names_nav,
            html_tag_names_noembed,
            html_tag_names_noframes,
            html_tag_names_noscript,
            html_tag_names_object,
            html_tag_names_ol,
            html_tag_names_p,
            html_tag_names_param,
            html_tag_names_plaintext,
            html_tag_names_pre,
            html_tag_names_script,
            html_tag_names_search,
            html_tag_names_section,
            html_tag_names_select,
            html_tag_names_source,
            html_tag_names_style,
            html_tag_names_summary,
            html_tag_names_table,
            html_tag_names_tbody,
            html_tag_names_td,
            html_tag_names_template,
            html_tag_names_textarea,
            html_tag_names_tfoot,
            html_tag_names_th,
            html_tag_names_thead,
            html_tag_names_title,
            html_tag_names_tr,
            html_tag_names_track,
            html_tag_names_ul,
            html_tag_names_wbr,
            html_tag_names_xmp,
        ])
    elif dom_node_optional_fly_string_is(namespace_, namespace_svg):
        return dom_fly_string_is_one_of_fly_strings(tag_name, [svg_tag_names_desc, svg_tag_names_foreign_object, svg_tag_names_title])
    elif dom_node_optional_fly_string_is(namespace_, namespace_math_ml):
        return dom_fly_string_is_one_of_fly_strings(tag_name, [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