PureHTML. Tokenizer
(PureHTML v0.5.0)
View Source
HTML5 tokenizer that produces a stream of tokens.
The tokenizer is implemented as a state machine. Each call to next_token/1
advances the machine until it produces a token, then returns the token and
the updated state. The tree builder switches the state for raw text, RCDATA,
script data, and plaintext elements with set_state/2, and tells the
tokenizer whether the adjusted current node is foreign with
set_foreign_content/2 (for CDATA sections).
Usage
iex> PureHTML.Tokenizer.tokenize("<p>Hello</p>") |> Enum.to_list()
[{:start_tag, "p", [], false}, {:character, "Hello"}, {:end_tag, "p"}]Token Types
{:doctype, name, public_id, system_id, force_quirks?}{:start_tag, name, attrs, self_closing?}{:end_tag, name}{:comment, data}{:pi, target, data}{:character, data}
Summary
Functions
Creates a new tokenizer state from input.
Gets the next token from the tokenizer.
Updates the foreign content context flag.
Switches the tokenizer state. The tree builder does this for the generic raw text and RCDATA element parsing algorithms, script, and plaintext.
Tokenizes an HTML string, returning a Stream of tokens.
Tokenizes an HTML string and returns {tokens, error_count}, where the
count follows the parse errors of the tokenizer and the input stream.
Types
@type t() :: %PureHTML.Tokenizer{ adjusted_current_node_not_in_html_namespace: term(), attr_name: term(), attr_names: term(), attr_value: term(), buffer: term(), deferred_token: term(), duplicate_attr: term(), eof_emitted: term(), error_count: term(), input: term(), last_start_tag: term(), pending_chars: term(), return_state: term(), state: term(), token: term(), xml_violation_mode: term() }
Functions
Creates a new tokenizer state from input.
The input is decoded as UTF-8 with U+FFFD replacement, newlines are
normalized, and the input stream parse errors (controls, noncharacters)
are counted into error_count.
Options
:initial_state- Starting tokenizer state (default::data):last_start_tag- Last start tag name for appropriate end tag checks
Gets the next token from the tokenizer.
Returns {token, updated_tokenizer} or nil when done.
Updates the foreign content context flag.
When in_foreign_content is true, <![CDATA[ will be parsed as a CDATA section.
When false (default), it's treated as a bogus comment per HTML5 spec.
Switches the tokenizer state. The tree builder does this for the generic raw text and RCDATA element parsing algorithms, script, and plaintext.
@spec tokenize( String.t(), keyword() ) :: Enumerable.t()
Tokenizes an HTML string, returning a Stream of tokens.
The stream is lazy - tokens are produced on demand as the stream is consumed.
@spec tokenize_with_errors( String.t(), keyword() ) :: {[term()], non_neg_integer()}
Tokenizes an HTML string and returns {tokens, error_count}, where the
count follows the parse errors of the tokenizer and the input stream.