Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.1

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

#==============================================================================================
#
#   html_parser_9 - HTML::HTMLParser: the table, caption, column group and template insertion
#                   modes
#
#   DESCRIPTION:
#       Port of LibWeb/HTML/Parser/HTMLParser.cpp (lines 3933-4537): the "in table body",
#       "in table" (with foster parenting for anything else), "in caption", "in column group"
#       and "in template" insertion modes.
#
#==============================================================================================

## Ported from HTMLParser.cpp:3934 Web::HTML::HTMLParser::handle_in_table_body.
# https://html.spec.whatwg.org/multipage/parsing.html#parsing-main-intbody
pub func html_parser_handle_in_table_body(this: HtmlParser*, token: html_syntax.HtmlToken*):
    # -> A start tag whose tag name is "tr"
    if html_syntax.html_token_is_start_tag(token) and html_parser_tag_name_is(token, html_tag_names_tr):
        # Clear the stack back to a table body context. (See below.)
        html_parser_clear_the_stack_back_to_a_table_body_context(this)

        # Insert an HTML element for the token, then switch the insertion mode to "in row".
        _ = html_parser_insert_html_element(this, token)
        this.m_insertion_mode = HtmlParserInsertionMode.in_row
        return

    # -> A start tag whose tag name is one of: "th", "td"
    if html_syntax.html_token_is_start_tag(token) and html_parser_tag_name_is_one_of(token, [html_tag_names_th, html_tag_names_td]):
        # Parse error.
        html_log_parse_error()

        # Clear the stack back to a table body context. (See below.)
        html_parser_clear_the_stack_back_to_a_table_body_context(this)

        # Insert an HTML element for a "tr" start tag token with no attributes, then switch the insertion mode to "in row".
        _ = html_parser_insert_html_element_for_tag_name(this, html_tag_names_tr)
        this.m_insertion_mode = HtmlParserInsertionMode.in_row

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

    # -> An end tag whose tag name is one of: "tbody", "tfoot", "thead"
    if html_syntax.html_token_is_end_tag(token) and html_parser_tag_name_is_one_of(token, [html_tag_names_tbody, html_tag_names_tfoot, html_tag_names_thead]):
        # If the stack of open elements does not have an element in table scope that is an HTML element with the same
        # tag name as the token, this is a parse error; ignore the token.
        if not html_stack_of_open_elements_has_in_table_scope(&this.m_stack_of_open_elements, html_syntax.html_token_tag_name(token)):
            html_log_parse_error()
            return

        # Otherwise:
        # 1. Clear the stack back to a table body context. (See below.)
        html_parser_clear_the_stack_back_to_a_table_body_context(this)

        # 2. Pop the current node from the stack of open elements. Switch the insertion mode to "in table".
        _ = html_stack_of_open_elements_pop(&this.m_stack_of_open_elements)
        this.m_insertion_mode = HtmlParserInsertionMode.in_table
        return

    # -> A start tag whose tag name is one of: "caption", "col", "colgroup", "tbody", "tfoot", "thead"
    # -> An end tag whose tag name is "table"
    if (html_syntax.html_token_is_start_tag(token) and html_parser_tag_name_is_one_of(token, [html_tag_names_caption, html_tag_names_col, html_tag_names_colgroup, html_tag_names_tbody, html_tag_names_tfoot, html_tag_names_thead])) or (html_syntax.html_token_is_end_tag(token) and html_parser_tag_name_is(token, html_tag_names_table)):

        # If the stack of open elements does not have a tbody, thead, or tfoot element in table scope, this is a parse
        # error; ignore the token.
        if not html_stack_of_open_elements_has_in_table_scope(&this.m_stack_of_open_elements, html_tag_names_tbody) and not html_stack_of_open_elements_has_in_table_scope(&this.m_stack_of_open_elements, html_tag_names_thead) and not html_stack_of_open_elements_has_in_table_scope(&this.m_stack_of_open_elements, html_tag_names_tfoot):
            html_log_parse_error()
            return

        # Otherwise:
        # 1. Clear the stack back to a table body context. (See below.)
        html_parser_clear_the_stack_back_to_a_table_body_context(this)

        # 2. Pop the current node from the stack of open elements. Switch the insertion mode to "in table".
        _ = html_stack_of_open_elements_pop(&this.m_stack_of_open_elements)
        this.m_insertion_mode = HtmlParserInsertionMode.in_table

        # 3. Reprocess the token.
        html_parser_process_using_the_rules_for(this, HtmlParserInsertionMode.in_table, token)
        return

    # -> An end tag whose tag name is one of: "body", "caption", "col", "colgroup", "html", "td", "th", "tr"
    if html_syntax.html_token_is_end_tag(token) and html_parser_tag_name_is_one_of(token, [html_tag_names_body, html_tag_names_caption, html_tag_names_col, html_tag_names_colgroup, html_tag_names_html, html_tag_names_td, html_tag_names_th, html_tag_names_tr]):
        # Parse error. Ignore the token.
        html_log_parse_error()
        return

    # -> Anything else
    #    Process the token using the rules for the "in table" insertion mode.
    html_parser_process_using_the_rules_for(this, HtmlParserInsertionMode.in_table, token)

## Ported from HTMLParser.cpp:4023 Web::HTML::HTMLParser::handle_in_table.
# https://html.spec.whatwg.org/multipage/parsing.html#parsing-main-intable
pub func html_parser_handle_in_table(this: HtmlParser*, token: html_syntax.HtmlToken*):
    # -> A character token, if the current node is table, tbody, template, tfoot, thead, or tr element
    if html_syntax.html_token_is_character(token) and html_parser_local_name_is_one_of(html_parser_current_element(this), [html_tag_names_table, html_tag_names_tbody, html_tag_names_tfoot, html_tag_names_thead, html_tag_names_tr]):
        # Let the pending table character tokens be an empty list of tokens.
        ak.vector_clear(&this.m_pending_table_character_tokens)

        # Set the original insertion mode to the current insertion mode.
        this.m_original_insertion_mode = this.m_insertion_mode

        # Switch the insertion mode to "in table text" and reprocess the token.
        this.m_insertion_mode = HtmlParserInsertionMode.in_table_text
        html_parser_process_using_the_rules_for(this, HtmlParserInsertionMode.in_table_text, token)
        return

    # -> A comment token
    if html_syntax.html_token_is_comment(token):
        # Insert a comment.
        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 "caption"
    if html_syntax.html_token_is_start_tag(token) and html_parser_tag_name_is(token, html_tag_names_caption):
        # Clear the stack back to a table context.
        html_parser_clear_the_stack_back_to_a_table_context(this)

        # Insert a marker at the end of the list of active formatting elements.
        html_list_of_active_formatting_elements_add_marker(&this.m_list_of_active_formatting_elements)

        # Insert an HTML element for the token, then switch the insertion mode to "in caption".
        _ = html_parser_insert_html_element(this, token)
        this.m_insertion_mode = HtmlParserInsertionMode.in_caption
        return

    # -> A start tag whose tag name is "colgroup"
    if html_syntax.html_token_is_start_tag(token) and html_parser_tag_name_is(token, html_tag_names_colgroup):
        # Clear the stack back to a table context.
        html_parser_clear_the_stack_back_to_a_table_context(this)

        # Insert an HTML element for the token, then switch the insertion mode to "in column group".
        _ = html_parser_insert_html_element(this, token)
        this.m_insertion_mode = HtmlParserInsertionMode.in_column_group
        return

    # -> A start tag whose tag name is "col"
    if html_syntax.html_token_is_start_tag(token) and html_parser_tag_name_is(token, html_tag_names_col):
        # Clear the stack back to a table context.
        html_parser_clear_the_stack_back_to_a_table_context(this)

        # Insert an HTML element for a "colgroup" start tag token with no attributes, then switch the insertion mode to "in column group".
        _ = html_parser_insert_html_element_for_tag_name(this, html_tag_names_colgroup)
        this.m_insertion_mode = HtmlParserInsertionMode.in_column_group

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

    # -> A start tag whose tag name is one of: "tbody", "tfoot", "thead"
    if html_syntax.html_token_is_start_tag(token) and html_parser_tag_name_is_one_of(token, [html_tag_names_tbody, html_tag_names_tfoot, html_tag_names_thead]):
        # Clear the stack back to a table context.
        html_parser_clear_the_stack_back_to_a_table_context(this)

        # Insert an HTML element for the token, then switch the insertion mode to "in table body".
        _ = html_parser_insert_html_element(this, token)
        this.m_insertion_mode = HtmlParserInsertionMode.in_table_body
        return

    # -> A start tag whose tag name is one of: "td", "th", "tr"
    if html_syntax.html_token_is_start_tag(token) and html_parser_tag_name_is_one_of(token, [html_tag_names_td, html_tag_names_th, html_tag_names_tr]):
        # Clear the stack back to a table context.
        html_parser_clear_the_stack_back_to_a_table_context(this)

        # Insert an HTML element for a "tbody" start tag token with no attributes, then switch the insertion mode to "in table body".
        _ = html_parser_insert_html_element_for_tag_name(this, html_tag_names_tbody)
        this.m_insertion_mode = HtmlParserInsertionMode.in_table_body

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

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

        # If the stack of open elements does not have a table element in table scope, ignore the token.
        if not html_stack_of_open_elements_has_in_table_scope(&this.m_stack_of_open_elements, html_tag_names_table):
            return

        # Otherwise:
        # 1. Pop elements from this stack until a table 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_table)

        # 2. Reset the insertion mode appropriately.
        html_parser_reset_the_insertion_mode_appropriately(this)

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

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

        # Otherwise:
        # 1. Pop elements from this stack until a table 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_table)

        # 2. Reset the insertion mode appropriately.
        html_parser_reset_the_insertion_mode_appropriately(this)
        return

    # -> An end tag whose tag name is one of: "body", "caption", "col", "colgroup", "html", "tbody", "td", "tfoot", "th", "thead", "tr"
    if html_syntax.html_token_is_end_tag(token) and html_parser_tag_name_is_one_of(token, [html_tag_names_body, html_tag_names_caption, html_tag_names_col, html_tag_names_colgroup, html_tag_names_html, html_tag_names_tbody, html_tag_names_td, html_tag_names_tfoot, html_tag_names_th, html_tag_names_thead, html_tag_names_tr]):
        # Parse error. Ignore the token.
        html_log_parse_error()
        return

    # -> A start tag whose tag name is one of: "style", "script", "template"
    # -> 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_style, html_tag_names_script, html_tag_names_template])) 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

    # -> A start tag whose tag name is "input"
    var anything_else = false
    if html_syntax.html_token_is_start_tag(token) and html_parser_tag_name_is(token, html_tag_names_input):
        # If the token does not have an attribute with the name "type",
        # or if it does, but that attribute's value is not an ASCII case-insensitive match for the string "hidden",
        # then: act as described in the "anything else" entry below.
        let type_attribute = html_syntax.html_token_attribute(token, html_attribute_names_type)
        if not html_parser_optional_string_equals_ignoring_ascii_case(type_attribute, "hidden"):
            anything_else = true  # goto AnythingElse
        else:
            # Otherwise:
            # 1. Parse error.
            html_log_parse_error()

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

            # 3. Pop that input element off the stack of open elements.
            _ = html_stack_of_open_elements_pop(&this.m_stack_of_open_elements)

            # 4. Acknowledge the token's self-closing flag, if it is set.
            html_syntax.html_token_acknowledge_self_closing_flag_if_set(token)
            return

    if not anything_else:
        # -> 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):
            # Parse error.
            html_log_parse_error()

            # If there is a template element on the stack of open elements,
            # or if the form element pointer is not null, ignore the token.
            if this.m_form_element != none or html_stack_of_open_elements_contains_template_element(&this.m_stack_of_open_elements):
                return

            # Otherwise:
            # 1. Insert an HTML element for the token, and set the form element pointer to point to the element created.
            this.m_form_element = as_html_form_element(html_parser_insert_html_element(this, token))

            # 2. Pop that form element off the stack of open elements.
            _ = html_stack_of_open_elements_pop(&this.m_stack_of_open_elements)
            return

        # -> An end-of-file token
        if html_syntax.html_token_is_end_of_file(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

    # -> Anything else
    # AnythingElse:
    # Parse error.
    html_log_parse_error()

    # Enable foster parenting, process the token using the rules for the "in body" insertion mode, and then disable foster parenting.
    this.m_foster_parenting = true
    html_parser_process_using_the_rules_for(this, HtmlParserInsertionMode.in_body, token)
    this.m_foster_parenting = false

## Ported from HTMLParser.cpp:4236 Web::HTML::HTMLParser::handle_in_caption.
# https://html.spec.whatwg.org/multipage/parsing.html#parsing-main-incaption
pub func html_parser_handle_in_caption(this: HtmlParser*, token: html_syntax.HtmlToken*):
    # -> An end tag whose tag name is "caption"
    if html_syntax.html_token_is_end_tag(token) and html_parser_tag_name_is(token, html_tag_names_caption):
        # If the stack of open elements does not have a caption element in table scope, this is a parse error; ignore
        # the token. (fragment case)
        if not html_stack_of_open_elements_has_in_table_scope(&this.m_stack_of_open_elements, html_tag_names_caption):
            assert(this.m_parsing_fragment)
            html_log_parse_error()
            return

        # Otherwise:
        # 1. Generate implied end tags.
        html_parser_generate_implied_end_tags(this)

        # 2. Now, if the current node is not a caption element, then this is a parse error.
        if not html_parser_local_name_is(html_parser_current_element(this), html_tag_names_caption):
            html_log_parse_error()

        # 3. Pop elements from this stack until a caption 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_caption)

        # 4. Clear the list of active formatting elements up to the last marker.
        html_list_of_active_formatting_elements_clear_up_to_the_last_marker(&this.m_list_of_active_formatting_elements)

        # 5. Switch the insertion mode to "in table".
        this.m_insertion_mode = HtmlParserInsertionMode.in_table
        return

    # -> A start tag whose tag name is one of: "caption", "col", "colgroup", "tbody", "td", "tfoot", "th", "thead", "tr"
    # -> An end tag whose tag name is "table"
    if (html_syntax.html_token_is_start_tag(token) and html_parser_tag_name_is_one_of(token, [html_tag_names_caption, html_tag_names_col, html_tag_names_colgroup, html_tag_names_tbody, html_tag_names_td, html_tag_names_tfoot, html_tag_names_th, html_tag_names_thead, html_tag_names_tr])) or (html_syntax.html_token_is_end_tag(token) and html_parser_tag_name_is(token, html_tag_names_table)):

        # If the stack of open elements does not have a caption element in table scope, this is a parse error; ignore
        # the token. (fragment case)
        if not html_stack_of_open_elements_has_in_table_scope(&this.m_stack_of_open_elements, html_tag_names_caption):
            assert(this.m_parsing_fragment)
            html_log_parse_error()
            return

        # Otherwise:
        # 1. Generate implied end tags.
        html_parser_generate_implied_end_tags(this)

        # 2. Now, if the current node is not a caption element, then this is a parse error.
        if not html_parser_local_name_is(html_parser_current_element(this), html_tag_names_caption):
            html_log_parse_error()

        # 3. Pop elements from this stack until a caption 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_caption)

        # 4. Clear the list of active formatting elements up to the last marker.
        html_list_of_active_formatting_elements_clear_up_to_the_last_marker(&this.m_list_of_active_formatting_elements)

        # 5. Switch the insertion mode to "in table".
        this.m_insertion_mode = HtmlParserInsertionMode.in_table

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

    # -> An end tag whose tag name is one of: "body", "col", "colgroup", "html", "tbody", "td", "tfoot", "th", "thead", "tr"
    if html_syntax.html_token_is_end_tag(token) and html_parser_tag_name_is_one_of(token, [html_tag_names_body, html_tag_names_col, html_tag_names_colgroup, html_tag_names_html, html_tag_names_tbody, html_tag_names_td, html_tag_names_tfoot, html_tag_names_th, html_tag_names_thead, html_tag_names_tr]):
        # Parse error. Ignore the token.
        html_log_parse_error()
        return

    # -> Anything else
    #    Process the token using the rules for the "in body" insertion mode.
    html_parser_process_using_the_rules_for(this, HtmlParserInsertionMode.in_body, token)

## Ported from HTMLParser.cpp:4315 Web::HTML::HTMLParser::handle_in_column_group.
# https://html.spec.whatwg.org/multipage/parsing.html#parsing-main-incolgroup
pub func html_parser_handle_in_column_group(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):
        # Insert the character.
        html_parser_insert_character(this, html_syntax.html_token_code_point(token))
        return

    # -> A comment token
    if html_syntax.html_token_is_comment(token):
        # Insert a comment.
        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):
        # 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 start tag whose tag name is "col"
    if html_syntax.html_token_is_start_tag(token) and html_parser_tag_name_is(token, html_tag_names_col):
        # Insert an HTML element for the token. Immediately pop the current node off the stack of open elements.
        _ = html_parser_insert_html_element(this, token)
        _ = html_stack_of_open_elements_pop(&this.m_stack_of_open_elements)

        # Acknowledge the token's self-closing flag, if it is set.
        html_syntax.html_token_acknowledge_self_closing_flag_if_set(token)
        return

    # -> An end tag whose tag name is "colgroup"
    if html_syntax.html_token_is_end_tag(token) and html_parser_tag_name_is(token, html_tag_names_colgroup):
        # If the current node is not a colgroup element, then this is a parse error; ignore the token.
        if not html_parser_local_name_is(html_parser_current_element(this), html_tag_names_colgroup):
            html_log_parse_error()
            return

        # Otherwise, pop the current node from the stack of open elements. Switch the insertion mode to "in table".
        _ = html_stack_of_open_elements_pop(&this.m_stack_of_open_elements)
        this.m_insertion_mode = HtmlParserInsertionMode.in_table
        return

    # -> An end tag whose tag name is "col"
    if html_syntax.html_token_is_end_tag(token) and html_parser_tag_name_is(token, html_tag_names_col):
        # Parse error. Ignore the token.
        html_log_parse_error()
        return

    # -> A start tag whose tag name is "template"
    # -> An end tag whose tag name is "template"
    if (html_syntax.html_token_is_start_tag(token) 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

    # -> An end-of-file token
    if html_syntax.html_token_is_end_of_file(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

    # -> Anything else
    # If the current node is not a colgroup element, then this is a parse error; ignore the token.
    if not html_parser_local_name_is(html_parser_current_element(this), html_tag_names_colgroup):
        html_log_parse_error()
        return

    # Otherwise, pop the current node from the stack of open elements.
    _ = html_stack_of_open_elements_pop(&this.m_stack_of_open_elements)

    # Switch the insertion mode to "in table".
    this.m_insertion_mode = HtmlParserInsertionMode.in_table

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

## Ported from HTMLParser.cpp:4413 Web::HTML::HTMLParser::handle_in_template.
# https://html.spec.whatwg.org/multipage/parsing.html#parsing-main-intemplate
pub func html_parser_handle_in_template(this: HtmlParser*, token: html_syntax.HtmlToken*):
    # -> A character token
    # -> A comment token
    # -> A DOCTYPE token
    if html_syntax.html_token_is_character(token) or html_syntax.html_token_is_comment(token) or html_syntax.html_token_is_doctype(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 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

    # -> A start tag whose tag name is one of: "caption", "colgroup", "tbody", "tfoot", "thead"
    if html_syntax.html_token_is_start_tag(token) and html_parser_tag_name_is_one_of(token, [html_tag_names_caption, html_tag_names_colgroup, html_tag_names_tbody, html_tag_names_tfoot, html_tag_names_thead]):
        # Pop the current template insertion mode off the stack of template insertion modes.
        _ = ak.vector_take_last(&this.m_stack_of_template_insertion_modes)

        # Push "in table" onto the stack of template insertion modes so that it is the new current template insertion mode.
        ak.vector_append_t(&this.m_stack_of_template_insertion_modes, HtmlParserInsertionMode.in_table)

        # Switch the insertion mode to "in table", and reprocess the token.
        this.m_insertion_mode = HtmlParserInsertionMode.in_table
        html_parser_process_using_the_rules_for(this, this.m_insertion_mode, token)
        return

    # -> A start tag whose tag name is "col"
    if html_syntax.html_token_is_start_tag(token) and html_parser_tag_name_is(token, html_tag_names_col):
        # Pop the current template insertion mode off the stack of template insertion modes.
        _ = ak.vector_take_last(&this.m_stack_of_template_insertion_modes)

        # Push "in column group" onto the stack of template insertion modes so that it is the new current template insertion mode.
        ak.vector_append_t(&this.m_stack_of_template_insertion_modes, HtmlParserInsertionMode.in_column_group)

        # Switch the insertion mode to "in column group", and reprocess the token.
        this.m_insertion_mode = HtmlParserInsertionMode.in_column_group
        html_parser_process_using_the_rules_for(this, this.m_insertion_mode, token)
        return

    # -> A start tag whose tag name is "tr"
    if html_syntax.html_token_is_start_tag(token) and html_parser_tag_name_is(token, html_tag_names_tr):
        # Pop the current template insertion mode off the stack of template insertion modes.
        _ = ak.vector_take_last(&this.m_stack_of_template_insertion_modes)

        # Push "in table body" onto the stack of template insertion modes so that it is the new current template insertion mode.
        ak.vector_append_t(&this.m_stack_of_template_insertion_modes, HtmlParserInsertionMode.in_table_body)

        # Switch the insertion mode to "in table body", and reprocess the token.
        this.m_insertion_mode = HtmlParserInsertionMode.in_table_body
        html_parser_process_using_the_rules_for(this, this.m_insertion_mode, token)
        return

    # -> A start tag whose tag name is one of: "td", "th"
    if html_syntax.html_token_is_start_tag(token) and html_parser_tag_name_is_one_of(token, [html_tag_names_td, html_tag_names_th]):
        # Pop the current template insertion mode off the stack of template insertion modes.
        _ = ak.vector_take_last(&this.m_stack_of_template_insertion_modes)

        # Push "in row" onto the stack of template insertion modes so that it is the new current template insertion mode.
        ak.vector_append_t(&this.m_stack_of_template_insertion_modes, HtmlParserInsertionMode.in_row)

        # Switch the insertion mode to "in row", and reprocess the token.
        this.m_insertion_mode = HtmlParserInsertionMode.in_row
        html_parser_process_using_the_rules_for(this, this.m_insertion_mode, token)
        return

    # -> Any other start tag
    if html_syntax.html_token_is_start_tag(token):
        # Pop the current template insertion mode off the stack of template insertion modes.
        _ = ak.vector_take_last(&this.m_stack_of_template_insertion_modes)

        # Push "in body" onto the stack of template insertion modes so that it is the new current template insertion mode.
        ak.vector_append_t(&this.m_stack_of_template_insertion_modes, HtmlParserInsertionMode.in_body)

        # Switch the insertion mode to "in body", and reprocess the token.
        this.m_insertion_mode = HtmlParserInsertionMode.in_body
        html_parser_process_using_the_rules_for(this, this.m_insertion_mode, token)
        return

    # -> Any other end tag
    if html_syntax.html_token_is_end_tag(token):
        # Parse error. Ignore the token.
        html_log_parse_error()
        return

    # -> An end-of-file token
    if html_syntax.html_token_is_end_of_file(token):
        # If there is no template element on the stack of open elements, then stop parsing. (fragment case)
        if not html_stack_of_open_elements_contains_template_element(&this.m_stack_of_open_elements):
            assert(this.m_parsing_fragment)
            html_parser_stop_parsing(this)
            return

        # Otherwise, this is a parse error.
        html_log_parse_error()

        # Pop elements from the stack of open elements until a template 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_template)

        # Clear the list of active formatting elements up to the last marker.
        html_list_of_active_formatting_elements_clear_up_to_the_last_marker(&this.m_list_of_active_formatting_elements)

        # Pop the current template insertion mode off the stack of template insertion modes.
        _ = ak.vector_take_last(&this.m_stack_of_template_insertion_modes)

        # Reset the insertion mode appropriately.
        html_parser_reset_the_insertion_mode_appropriately(this)

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