swc-project/swc · error · Error

UnexpectedNullCharacter

UnexpectedNullCharacter

Error message

Unexpected null character

What it means

HTML parse error in the 'in body' insertion mode: a U+0000 NULL character token was found in the input. Per spec NULL characters in content are parse errors; the character is ignored (typically replaced by U+FFFD in text) and parsing continues.

Source

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

                    //
                    // Switch the insertion mode to "in body".
                    //
                    // Reprocess the current token.
                    _ => {
                        anything_else(self, token_and_info)?;
                    }
                }
            }
            // The "in body" insertion mode
            InsertionMode::InBody => {
                // When the user agent is to apply the rules for the "in body" insertion mode,
                // the user agent must handle the token as follows:
                match token {
                    // A character token that is U+0000 NULL
                    //
                    // Parse error. Ignore the token.
                    Token::Character { value, .. } if *value == '\x00' => self.errors.push(
                        Error::new(token_and_info.span, ErrorKind::UnexpectedNullCharacter),
                    ),
                    // 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
                    //
                    // Reconstruct the active formatting elements, if any.
                    //
                    // Insert the token's character.
                    Token::Character {
                        value: '\x09' | '\x0A' | '\x0C' | '\x0D' | '\x20',
                        ..
                    } => {
                        self.reconstruct_active_formatting_elements()?;
                        self.insert_character(token_and_info)?;
                    }
                    // Any other character token
                    //
                    // Reconstruct the active formatting elements, if any.

View on GitHub (pinned to 5176682b65)

Solutions

  1. Strip NUL bytes from the input before parsing
  2. Fix the producer of the document so it emits valid text without U+0000
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at crates/swc_html_parser/src/parser/mod.rs:2292 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/53bfc76bc0444e61. Report an issue: GitHub.