Skip to content

Use HtmlKit tokenizer for HTML5 parsing - #243

Open
jhaygood86 wants to merge 7 commits into
ArthurHub:masterfrom
jhaygood86:feature/html5-support
Open

Use HtmlKit tokenizer for HTML5 parsing#243
jhaygood86 wants to merge 7 commits into
ArthurHub:masterfrom
jhaygood86:feature/html5-support

Conversation

@jhaygood86

@jhaygood86 jhaygood86 commented Jul 14, 2026

Copy link
Copy Markdown

This is the first of many pull requests to bring in the various HTML and CSS fixes from PeachPDF (which is a stripped down fork) to upstream. The goal is as each change gets merged in, I'll start work on the next change. The eventual goal will be parity where possible for features. There are many cases where that won't be possible, especially on the PDF side, but where it is possible, I'll make sure it works.

Background:
PeachPDF replaced HTML 5 parsing with using the tokenizer in MimeKit. We also use various other functionalities from MimeKit, so we bring in the full library. HTML-Renderer just needs the tokenization, so I used HtmlKit. If any of the other changes that requiring MimeKit gets ported in, then it would make sense to use MimeKit. The only difference is a namespace import and package import. They are the same codebase from the same maintainer.

The reason for this is that the hand-rolled regex based parser is not compliant with HTML 5 parsing rules at all. There are some related changes in the DOM layer that were brought in as well to make it spec compliant.

Changes:

  • Replaces the hand-rolled HTML scanner with HtmlKit tokenization and updates DOM parsing to better follow HTML5 behavior, including omitted end-tag handling (p/td/tr), safer comment/script handling, and noscript re-parsing.
  • Adds anonymous table box correction logic and related DOM/CSS utilities
  • Migrates text storage from SubString to string (removing SubString.cs)
  • Includes a new demo sample showcasing malformed-HTML rendering improvements.

@eXpl0it3r

Copy link
Copy Markdown
Collaborator

Just FYI, I've added some regression testing, so we can detect what this change "breaks" or rather what has already been broken in the existing samples.

As such you can expect a bunch of failures that have likely nothing to do with your contribution. 😉

@jhaygood86

Copy link
Copy Markdown
Author

After these first 2 PRs get merged, I plan on bringing over the relevant parts of the regression suite PeachPDF has. It's up to around 7,000 tests and verifies all sorts of HTML, CSS, and SVG spec conformance things. Not all of the tests are useful, and there's some missing tests for GDI and WPF specific codepaths, but its a good initial corpus.

@eXpl0it3r

Copy link
Copy Markdown
Collaborator

Oh that sounds awesome! I just quickly hacked something together, so we have at least some baseline for the HtmlKit and ExCSS PRs beyond the manual testing.

@eXpl0it3r
eXpl0it3r force-pushed the feature/html5-support branch from 0bfe73e to 0a6d568 Compare July 28, 2026 20:30
@eXpl0it3r

eXpl0it3r commented Jul 28, 2026

Copy link
Copy Markdown
Collaborator

I noticed that non-breaking spaces weren't handled anymore with the removal of Substring (and   was treated as space) and added a commit to fix this. Feel free to overwrite it, if you have a better fix from PeachPDF.

@jhaygood86

Copy link
Copy Markdown
Author

@eXpl0it3r I just found a similar issue in PeachPDF -- let me see about porting it over. See: jhaygood86/PeachPDF#431. The fix was to "do nothing" since HtmlKit already decoded everything correctly.

@jhaygood86

Copy link
Copy Markdown
Author

@eXpl0it3r Looks like your change is more or less equivalent for now, so I'll just leave it.

@eXpl0it3r

Copy link
Copy Markdown
Collaborator

Fixed another issue where consecutive line break tags (e.g. <br/><br/><br/>) wouldn't yield empty lines, even though they should.

A lot of the other "regressions" are just from invalid HTML (e.g. <div> inside a <p> tag, etc.).

@jhaygood86

Copy link
Copy Markdown
Author

That's one that is fixed in PeachPDF already in the "fix up" phase. When I do the next stage, I'll make sure that all of those fixes come in. There's quite a bit of "take this parsed and cascaded document and make it actually be structured correctly" work that HTML-Renderer kind of only does

jhaygood86 and others added 7 commits July 29, 2026 16:50
Replaces the hand-rolled HTML scanner with HtmlKit tokenization and updates DOM parsing to better follow HTML5 behavior, including omitted end-tag handling (`p`/`td`/`tr`), safer comment/script handling, and `noscript` re-parsing. Adds anonymous table box correction logic and related DOM/CSS utilities, migrates text storage from `SubString` to `string` (removing `SubString.cs`), and includes a new demo sample showcasing malformed-HTML rendering improvements.
Improve the HTML5 parsing demo snippet by expanding the inline `if` into a multi-line block and refining the demo string text. The escaped source example and live script output were updated together to stay consistent and easier to read.
Delete the `InternalsVisibleTo` entry for `HtmlParserVerify` from `HtmlRenderer.csproj`, narrowing assembly internals access and keeping project metadata aligned with current test/verification setup.
@eXpl0it3r
eXpl0it3r force-pushed the feature/html5-support branch from c88f600 to 7a70b46 Compare July 29, 2026 14:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants