Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.0

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

#==============================================================================================
#
#   html_parser_5 - HTML::HTMLParser: the "in body" insertion mode, part 1
#
#   DESCRIPTION:
#       Port of LibWeb/HTML/Parser/HTMLParser.cpp (lines 2193-2609): handle_in_body from the
#       character tokens to the "button" start tag. handle_in_body is one C++ function of 1,090
#       lines; the port keeps its entries in order and continues in html_parser_6 with
#       html_parser_handle_in_body_continued, which this part calls when no entry here matched.
#
#==============================================================================================

## The tag names an end-of-file token or a body/html end tag may leave open without a parse error
## (HTMLParser.cpp:2347, 2370, 2393).
func html_parser_in_body_may_remain_open(element: DomElement*) -> bool:
    return html_parser_local_name_is_one_of(element, [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_tag_names_body, html_tag_names_html])

## `token.for_each_attribute([&](auto& attribute) { if (!element.has_attribute(attribute.local_name)) element.append_attribute(attribute.local_name, attribute.value); })`.
func html_parser_add_missing_attributes(token: html_syntax.HtmlToken*, element: DomElement*):
    let attributes = html_syntax.html_token_tag_attributes(token) else return
    for attribute in &ak.vector_span(attributes):
        if not dom_element_has_attribute(element, attribute.local_name):
            dom_element_append_attribute_fly_string(element, attribute.local_name, attribute.value)

## Ported from HTMLParser.cpp:2194 Web::HTML::HTMLParser::handle_in_body (the entries up to the
## "button" start tag; the rest is html_parser_handle_in_body_continued).
# https://html.spec.whatwg.org/multipage/parsing.html#parsing-main-inbody
pub func html_parser_handle_in_body(this: HtmlParser*, token: html_syntax.HtmlToken*):
    if html_syntax.html_token_is_character(token):
        # -> A character token that is U+0000 NULL
        if html_syntax.html_token_code_point(token) == 0:
            # Parse error. Ignore the token.
            html_log_parse_error()
            return

        # -> 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_parser_whitespace(token):
            # Reconstruct the active formatting elements, if any.
            html_parser_reconstruct_the_active_formatting_elements(this)

            # Insert the token's character.
            html_parser_insert_character(this, html_syntax.html_token_code_point(token))
            return

        # -> Any other character token

        # Reconstruct the active formatting elements, if any.
        html_parser_reconstruct_the_active_formatting_elements(this)

        # Insert the token's character.
        html_parser_insert_character(this, html_syntax.html_token_code_point(token))

        # Set the frameset-ok flag to "not ok".
        this.m_frameset_ok = false
        return

    # -> A comment token
    if html_syntax.html_token_is_comment(token):
        html_parser_insert_comment(this, token)
        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):
        # Parse error.
        html_log_parse_error()

        # If there is a template element on the stack of open elements, then ignore the token.
        if html_stack_of_open_elements_contains_template_element(&this.m_stack_of_open_elements):
            return

        # Otherwise, for each attribute on the token, check to see if the attribute is already present on the top element of the stack of open elements.
        # If it is not, add the attribute and its corresponding value to that element.
        let top_element = html_stack_of_open_elements_first(&this.m_stack_of_open_elements)
        html_parser_add_missing_attributes(token, top_element)
        return

    # -> A start tag whose tag name is one of: "base", "basefont", "bgsound", "link", "meta", "noframes", "script", "style", "template", "title"
    # -> An end tag whose tag name is "template"
    if (html_syntax.html_token_is_start_tag(token) and html_parser_tag_name_is_one_of(token, [html_tag_names_base, html_tag_names_basefont, html_tag_names_bgsound, html_tag_names_link, html_tag_names_meta, html_tag_names_noframes, html_tag_names_script, html_tag_names_style, html_tag_names_template, html_tag_names_title])) or (html_syntax.html_token_is_end_tag(token) and html_parser_tag_name_is(token, html_tag_names_template)):
        # Process the token using the rules for the "in head" insertion mode.
        html_parser_process_using_the_rules_for(this, HtmlParserInsertionMode.in_head, token)
        return

    let elements = &this.m_stack_of_open_elements.m_elements

    # -> A start tag whose tag name is "body"
    if html_syntax.html_token_is_start_tag(token) and html_parser_tag_name_is(token, html_tag_names_body):
        # Parse error.
        html_log_parse_error()

        # If the stack of open elements has only one node on it,
        # or if the second element on the stack of open elements is not a body element,
        # or if there is a template element on the stack of open elements, then ignore the token.
        # (fragment case or there is a template element on the stack)
        if ak.vector_size(elements) == 1 or not html_parser_local_name_is(*ak.vector_at(elements, 1), html_tag_names_body) or html_stack_of_open_elements_contains_template_element(&this.m_stack_of_open_elements):
            return

        # Otherwise, set the frameset-ok flag to "not ok"; then, for each attribute on the token, check to see if the attribute is already
        # present on the body element (the second element) on the stack of open elements, and if it is not, add the attribute and its
        # corresponding value to that element.
        this.m_frameset_ok = false
        let body_element = *ak.vector_at(elements, 1)
        html_parser_add_missing_attributes(token, body_element)
        return

    # -> A start tag whose tag name is "frameset"
    if html_syntax.html_token_is_start_tag(token) and html_parser_tag_name_is(token, html_tag_names_frameset):
        # Parse error.
        html_log_parse_error()

        # If the stack of open elements has only one node on it, or if the second element on the stack of open elements is not a body element, then ignore the token.
        # (fragment case or there is a template element on the stack)
        if ak.vector_size(elements) == 1 or not html_parser_local_name_is(*ak.vector_at(elements, 1), html_tag_names_body):
            assert(this.m_parsing_fragment or html_stack_of_open_elements_contains_template_element(&this.m_stack_of_open_elements))
            return

        # If the frameset-ok flag is set to "not ok", ignore the token.
        if not this.m_frameset_ok:
            return

        # Otherwise, run the following steps:
        # 1. Remove the second element on the stack of open elements from its parent node, if it has one.
        dom_node_remove((*ak.vector_at(elements, 1)).dom_node(), true)

        # 2. Pop all the nodes from the bottom of the stack of open elements, from the current node up to, but not including, the root html element.
        while ak.vector_size(elements) > 1:
            if is_html_html_element(*ak.vector_last(elements)):
                break
            _ = html_stack_of_open_elements_pop(&this.m_stack_of_open_elements)

        # 3. Insert an HTML element for the token.
        _ = html_parser_insert_html_element(this, token)

        # 4. Switch the insertion mode to "in frameset".
        this.m_insertion_mode = HtmlParserInsertionMode.in_frameset

        return

    # -> An end-of-file token
    if html_syntax.html_token_is_end_of_file(token):
        # If the stack of template insertion modes is not empty, then process the token using the rules for the "in template" insertion mode.
        if not ak.vector_is_empty(&this.m_stack_of_template_insertion_modes):
            html_parser_process_using_the_rules_for(this, HtmlParserInsertionMode.in_template, token)
            return

        # Otherwise, follow these steps:
        # 1. If there is a node in the stack of open elements that is not either a dd element, a dt element, an li element, an optgroup element,
        #    an option element, a p element, an rb element, an rp element, an rt element, an rtc element, a tbody element, a td element, a tfoot
        #    element, a th element, a thead element, a tr element, the body element, or the html element, then this is a parse error.
        for node in ak.vector_span(elements):
            if not html_parser_in_body_may_remain_open(node):
                html_log_parse_error()
                break

        # 2. Stop parsing.
        html_parser_stop_parsing(this)
        return

    # -> An end tag whose tag name is "body"
    if html_syntax.html_token_is_end_tag(token) and html_parser_tag_name_is(token, html_tag_names_body):
        # If the stack of open elements does not have a body element in scope, this is a parse error; ignore the token.
        if not html_stack_of_open_elements_has_in_scope_fly_string(&this.m_stack_of_open_elements, html_tag_names_body):
            html_log_parse_error()
            return

        # Otherwise, if there is a node in the stack of open elements that is not either a dd element, a dt element, an li element, an optgroup element,
        # an option element, a p element, an rb element, an rp element, an rt element, an rtc element, a tbody element, a td element, a tfoot element, a
        # th element, a thead element, a tr element, the body element, or the html element, then this is a parse error.
        for node in ak.vector_span(elements):
            if not html_parser_in_body_may_remain_open(node):
                html_log_parse_error()
                break

        # Switch the insertion mode to "after body".
        this.m_insertion_mode = HtmlParserInsertionMode.after_body
        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 stack of open elements does not have a body element in scope, this is a parse error; ignore the token.
        if not html_stack_of_open_elements_has_in_scope_fly_string(&this.m_stack_of_open_elements, html_tag_names_body):
            html_log_parse_error()
            return

        # Otherwise, if there is a node in the stack of open elements that is not either a dd element, a dt element, an li element, an optgroup element, an
        # option element, a p element, an rb element, an rp element, an rt element, an rtc element, a tbody element, a td element, a tfoot element, a th element,
        # a thead element, a tr element, the body element, or the html element, then this is a parse error.
        for node in ak.vector_span(elements):
            if not html_parser_in_body_may_remain_open(node):
                html_log_parse_error()
                break

        # Switch the insertion mode to "after body".
        this.m_insertion_mode = HtmlParserInsertionMode.after_body

        # Reprocess the token.
        html_parser_process_using_the_rules_for(this, this.m_insertion_mode, token)
        return

    # -> A start tag whose tag name is one of: "address", "article", "aside", "blockquote", "center", "details", "dialog", "dir", "div", "dl", "fieldset", "figcaption", "figure", "footer", "header", "hgroup", "main", "menu", "nav", "ol", "p", "search", "section", "summary", "ul"
    if html_syntax.html_token_is_start_tag(token) and html_parser_tag_name_is_one_of(token, [html_tag_names_address, html_tag_names_article, html_tag_names_aside, html_tag_names_blockquote, html_tag_names_center, html_tag_names_details, html_tag_names_dialog, html_tag_names_dir, html_tag_names_div, html_tag_names_dl, html_tag_names_fieldset, html_tag_names_figcaption, html_tag_names_figure, html_tag_names_footer, html_tag_names_header, html_tag_names_hgroup, html_tag_names_main, html_tag_names_menu, html_tag_names_nav, html_tag_names_ol, html_tag_names_p, html_tag_names_search, html_tag_names_section, html_tag_names_summary, html_tag_names_ul]):
        # If the stack of open elements has a p element in button scope, then close a p element.
        if html_stack_of_open_elements_has_in_button_scope(&this.m_stack_of_open_elements, html_tag_names_p):
            html_parser_close_a_p_element(this)

        # Insert an HTML element for the token.
        _ = html_parser_insert_html_element(this, token)
        return

    # -> A start tag whose tag name is one of: "h1", "h2", "h3", "h4", "h5", "h6"
    if html_syntax.html_token_is_start_tag(token) and html_parser_tag_name_is_one_of(token, [html_tag_names_h1, html_tag_names_h2, html_tag_names_h3, html_tag_names_h4, html_tag_names_h5, html_tag_names_h6]):
        # If the stack of open elements has a p element in button scope, then close a p element.
        if html_stack_of_open_elements_has_in_button_scope(&this.m_stack_of_open_elements, html_tag_names_p):
            html_parser_close_a_p_element(this)

        # If the current node is an HTML element whose tag name is one of "h1", "h2", "h3", "h4", "h5", or "h6", then this is a parse error; pop the current node off the stack of open elements.
        if html_parser_local_name_is_one_of(html_parser_current_element(this), [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_log_parse_error()
            _ = html_stack_of_open_elements_pop(&this.m_stack_of_open_elements)

        # Insert an HTML element for the token.
        _ = html_parser_insert_html_element(this, token)
        return

    # -> A start tag whose tag name is one of: "pre", "listing"
    if html_syntax.html_token_is_start_tag(token) and html_parser_tag_name_is_one_of(token, [html_tag_names_pre, html_tag_names_listing]):
        # If the stack of open elements has a p element in button scope, then close a p element.
        if html_stack_of_open_elements_has_in_button_scope(&this.m_stack_of_open_elements, html_tag_names_p):
            html_parser_close_a_p_element(this)

        # Insert an HTML element for the token.
        _ = html_parser_insert_html_element(this, token)

        # If the next token is a U+000A LINE FEED (LF) character token,
        # then ignore that token and move on to the next one.
        # (Newlines at the start of pre blocks are ignored as an authoring convenience.)
        this.m_next_line_feed_can_be_ignored = true

        # Set the frameset-ok flag to "not ok".
        this.m_frameset_ok = false

        return

    # -> A start tag whose tag name is "form"
    if html_syntax.html_token_is_start_tag(token) and html_parser_tag_name_is(token, html_tag_names_form):
        # If the form element pointer is not null, and there is no template element on the stack of open elements, then this is a parse error; ignore the token.
        if this.m_form_element != none and not html_stack_of_open_elements_contains_template_element(&this.m_stack_of_open_elements):
            html_log_parse_error()
            return

        # Otherwise:
        # 1. If the stack of open elements has a p element in button scope, then close a p element.
        if html_stack_of_open_elements_has_in_button_scope(&this.m_stack_of_open_elements, html_tag_names_p):
            html_parser_close_a_p_element(this)

        # 2. Insert an HTML element for the token, and, if there is no template element on the stack of open elements,
        #    set the form element pointer to point to the element created.
        let element = html_parser_insert_html_element(this, token)
        if not html_stack_of_open_elements_contains_template_element(&this.m_stack_of_open_elements):
            this.m_form_element = as_html_form_element(element)
        return

    # -> A start tag whose tag name is "li"
    if html_syntax.html_token_is_start_tag(token) and html_parser_tag_name_is(token, html_tag_names_li):
        # 1. Set the frameset-ok flag to "not ok".
        this.m_frameset_ok = false

        # 2. Initialize node to be the current node (the bottommost node of the stack).
        # 3. Loop: If node is an li element, then run these substeps:
        var i = (isize)ak.vector_size(elements) - 1
        while i >= 0:
            let node = *ak.vector_at(elements, (usize)i)
            if html_parser_local_name_is(node, html_tag_names_li):
                # 1. Generate implied end tags, except for li elements.
                html_parser_generate_implied_end_tags(this, html_tag_names_li)

                # 2. If the current node is not an li element, then this is a parse error.
                if not html_parser_local_name_is(html_parser_current_element(this), html_tag_names_li):
                    html_log_parse_error()

                # 3. Pop elements from the stack of open elements until an li element has been popped from the stack.
                html_stack_of_open_elements_pop_until_an_element_with_tag_name_has_been_popped(&this.m_stack_of_open_elements, html_tag_names_li)

                # 4. Jump to the step labeled done below.
                break

            # 4. If node is in the special category, but is not an address, div, or p element, then jump to the step labeled done below.
            if html_parser_is_special_tag(dom_element_local_name(node), dom_element_namespace_uri(node)) and not html_parser_local_name_is_one_of(node, [html_tag_names_address, html_tag_names_div, html_tag_names_p]):
                break

            # 5. Otherwise, set node to the previous entry in the stack of open elements and return to the step labeled loop.
            i -= 1

        # 6. Done: If the stack of open elements has a p element in button scope, then close a p element.
        if html_stack_of_open_elements_has_in_button_scope(&this.m_stack_of_open_elements, html_tag_names_p):
            html_parser_close_a_p_element(this)

        # 7. Finally, insert an HTML element for the token.
        _ = html_parser_insert_html_element(this, token)
        return

    # -> A start tag whose tag name is one of: "dd", "dt"
    if html_syntax.html_token_is_start_tag(token) and html_parser_tag_name_is_one_of(token, [html_tag_names_dd, html_tag_names_dt]):
        # 1. Set the frameset-ok flag to "not ok".
        this.m_frameset_ok = false

        # 2. Initialize node to be the current node (the bottommost node of the stack).
        # 3. Loop: If node is a dd element, then run these substeps:
        # 4. If node is a dt element, then run these substeps:
        var i = (isize)ak.vector_size(elements) - 1
        while i >= 0:
            # 1. Generate implied end tags, except for dd elements.
            let node = *ak.vector_at(elements, (usize)i)
            if html_parser_local_name_is(node, html_tag_names_dd):
                html_parser_generate_implied_end_tags(this, html_tag_names_dd)
                # 2. If the current node is not a dd element, then this is a parse error.
                if not html_parser_local_name_is(html_parser_current_element(this), html_tag_names_dd):
                    html_log_parse_error()

                # 3. Pop elements from the stack of open elements until a dd element has been popped from the stack.
                html_stack_of_open_elements_pop_until_an_element_with_tag_name_has_been_popped(&this.m_stack_of_open_elements, html_tag_names_dd)

                # 4. Jump to the step labeled done below.
                break

            # 1. Generate implied end tags, except for dt elements.
            if html_parser_local_name_is(node, html_tag_names_dt):
                # 2. If the current node is not a dt element, then this is a parse error.
                html_parser_generate_implied_end_tags(this, html_tag_names_dt)
                if not html_parser_local_name_is(html_parser_current_element(this), html_tag_names_dt):
                    html_log_parse_error()
                # 3. Pop elements from the stack of open elements until a dt element has been popped from the stack.
                html_stack_of_open_elements_pop_until_an_element_with_tag_name_has_been_popped(&this.m_stack_of_open_elements, html_tag_names_dt)

                # 4. Jump to the step labeled done below.
                break

            # 5. If node is in the special category, but is not an address, div, or p element, then jump to the step labeled done below.
            if html_parser_is_special_tag(dom_element_local_name(node), dom_element_namespace_uri(node)) and not html_parser_local_name_is_one_of(node, [html_tag_names_address, html_tag_names_div, html_tag_names_p]):
                break

            # 6. Otherwise, set node to the previous entry in the stack of open elements and return to the step labeled loop.
            i -= 1

        # 7: Done: If the stack of open elements has a p element in button scope, then close a p element.
        if html_stack_of_open_elements_has_in_button_scope(&this.m_stack_of_open_elements, html_tag_names_p):
            html_parser_close_a_p_element(this)

        # 8: Finally, insert an HTML element for the token.
        _ = html_parser_insert_html_element(this, token)
        return

    # -> A start tag whose tag name is "plaintext"
    if html_syntax.html_token_is_start_tag(token) and html_parser_tag_name_is(token, html_tag_names_plaintext):
        # If the stack of open elements has a p element in button scope, then close a p element.
        if html_stack_of_open_elements_has_in_button_scope(&this.m_stack_of_open_elements, html_tag_names_p):
            html_parser_close_a_p_element(this)

        # Insert an HTML element for the token.
        _ = html_parser_insert_html_element(this, token)

        # Switch the tokenizer to the PLAINTEXT state.
        html_syntax.html_tokenizer_switch_to_html_parser(&this.m_tokenizer, html_syntax.HtmlTokenizerState.plaintext)
        return

    # -> A start tag whose tag name is "button"
    if html_syntax.html_token_is_start_tag(token) and html_parser_tag_name_is(token, html_tag_names_button):
        # 1. If the stack of open elements has a button element in scope, then run these substeps:
        if html_stack_of_open_elements_has_in_button_scope(&this.m_stack_of_open_elements, html_tag_names_button):
            # 1. Parse error.
            html_log_parse_error()

            # 2. Generate implied end tags.
            html_parser_generate_implied_end_tags(this)

            # 3. Pop elements from the stack of open elements until a button element has been popped from the stack.
            html_stack_of_open_elements_pop_until_an_element_with_tag_name_has_been_popped(&this.m_stack_of_open_elements, html_tag_names_button)

        # 2. Reconstruct the active formatting elements, if any.
        html_parser_reconstruct_the_active_formatting_elements(this)

        # 3. Insert an HTML element for the token.
        _ = html_parser_insert_html_element(this, token)

        # 4. Set the frameset-ok flag to "not ok".
        this.m_frameset_ok = false
        return

    html_parser_handle_in_body_continued(this, token)