Ignore whitespace during HTML parsing #1
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.