HTML Tokenizer

HTML Tokenizer

The tokenizer is the first stage of HTML parsing: it converts markup text into a stream of standards-defined tokens. It is public API, so tools that care about tokens rather than trees — linters, sanitizer pre-passes, syntax highlighters — can consume the stream directly.


Tokenizing Markup

Tokenizer.tokenize() processes complete input; Tokenizer.tokenize_fragment() applies fragment tokenization rules. Both emit the standard token vocabulary.

Token Classes

Each token is a typed object: StartTagToken and EndTagToken for tags (with attributes), CharacterToken for text, CommentToken, DoctypeToken, and EofToken marking end of input.

Tokenizer States

TokenizerState models the standards state machine — the same states the specification names — and Tokenizer.set_state() lets fragment and context-sensitive callers start in the right state.

Character References

Named and numeric character references resolve through resolve_named_char_ref(), resolve_numeric_char_ref(), and find_named_char_ref_match() — the full named-reference table, not a shortcut subset.


Tips and Best Practices

  • Use tokenize_fragment() with the correct initial state when tokenizing content that would appear inside a specific element
  • Treat EofToken as the definitive end signal rather than exhausting the iterator by exception
  • For sanitizers, inspect StartTagToken attributes at the token level before any tree is built

Common Issues

IssueCauseFix
Script/style content tokenizes as markupWrong initial state for contextSet the appropriate TokenizerState via set_state()
Entities appear unresolvedConsuming raw text without reference resolutionUse the char-ref resolver functions
Fragment output differs from full parseFragment rules intentionally differUse tokenize() for full documents

FAQ

Can I use the tokenizer without building a tree?

Yes — the token stream is a public, standalone output.

Are tokenizer states the standard ones?

TokenizerState mirrors the specification’s state machine and is settable via set_state().

How are named character references handled?

Through resolve_named_char_ref() and find_named_char_ref_match() against the full named-reference table.


API Reference Summary

Class/MethodDescription
Tokenizer.tokenizeTokenize complete input
Tokenizer.tokenize_fragmentTokenize with fragment rules
Tokenizer.set_stateSet the initial tokenizer state
StartTagTokenStart-tag token with attributes
TokenizerStateStandards state machine states
resolve_named_char_refResolve a named character reference

See Also