Skip to content

Conversation

@shriramters
Copy link
Contributor

@shriramters shriramters commented Jul 20, 2025

Hello, Thanks for creating this beautiful parser, one of the only ones in modern C++!

I discovered a bug in the parser's state machine that causes it to generate a malformed DOM tree when parsing common, well-formatted HTML documents that contain whitespace (like newlines) between major tags such as <html>, <head>, and <body>.

The root cause is that when the parser is in a state like BeforeHead, it interprets a whitespace token as an implicit signal to create and immediately close the tag, leading to subsequent tags like the actual being inserted incorrectly into the .

This pull request fixes the issue by explicitly ignoring whitespace tokens in the relevant states (BeforeHtml, BeforeHead, InHead, and AfterHead), making the parser more resilient to standard document formatting.

How To Reproduce

Remove my changes from src/Parser.cpp and run the newly added tests

Commit Message

The HTML parser now correctly ignores leading whitespace before the and tags, as well as whitespace within and after the section. This improves parsing robustness for common HTML formatting variations.

  • Add a new test suite (WhitespaceTest.cpp) with specific unit tests to verify this behavior.
  • Update the CMake configuration to include the new test file in the build.

The HTML parser now correctly ignores leading whitespace before the <html> and <head> tags, as well as
whitespace within and after the <head> section. This improves parsing robustness for common HTML formatting
variations.

- Add a new test suite (WhitespaceTest.cpp) with specific unit tests to verify this behavior.
- Update the CMake configuration to include the new test file in the build.
@JustCabbage JustCabbage merged commit c794fb7 into JustCabbage:master Aug 10, 2025
8 of 10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants