swc-project/swc · error · Error

HeadingWhenHeadingOpen

HeadingWhenHeadingOpen

Error message

Heading cannot be a child of another heading

What it means

HTML parse error in 'in body': a start tag h1-h6 was seen while the current node is also a heading element, i.e. a heading nested directly inside another heading, which is invalid. The parser still closes the current heading and inserts the new one.

Solutions

  1. Close the outer heading before starting another (e.g. <h1>a<h2>b -> <h1>a</h1><h2>b)
  2. Use a non-heading container element for the nested structure
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at crates/swc_html_parser/src/parser/mod.rs:2799 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of swc-project/swc@5176682b65 (2026-08-17). Data as JSON: /api/errors/1ffef64b9cd47796. Report an issue: GitHub.

Appendix: source

Thrown at crates/swc_html_parser/src/parser/mod.rs:2799

                    //
                    // Insert an HTML element for the token.
                    Token::StartTag {
                        tag_name,
                        is_self_closing,
                        ..
                    } if matches!(&**tag_name, "h1" | "h2" | "h3" | "h4" | "h5" | "h6") => {
                        if self.open_elements_stack.has_in_button_scope("p") {
                            self.close_p_element(token_and_info, false);
                        }

                        match self.open_elements_stack.items.last() {
                            Some(node)
                                if is_html_element!(
                                    node,
                                    "h1" | "h2" | "h3" | "h4" | "h5" | "h6"
                                ) =>
                            {
                                self.errors.push(Error::new(
                                    token_and_info.span,
                                    ErrorKind::HeadingWhenHeadingOpen,
                                ));

                                self.open_elements_stack.pop();
                            }
                            _ => {}
                        }

                        self.insert_html_element(token_and_info)?;
                        maybe_allow_self_closing!(is_self_closing, tag_name);
                    }
                    // A start tag whose tag name is one of: "pre", "listing"
                    //
                    // If the stack of open elements has a p element in button scope, then close a p
                    // element.
                    //
                    // Insert an HTML element for the token.

View on GitHub (pinned to 5176682b65)