API reference
HTML in Zig, the way the WHATWG HTML standard parses it.
parse turns UTF-8 bytes into a Document, and parseFragment parses
markup in the context of an element, as innerHTML does. Parser does
the same in chunks, so a document can stream through it. Tokenizer is
the stage before tree construction, for callers who only want tokens.
None of them do I/O.
module
Parser
src
The HTML parser: tree construction, §13.2.6 of the standard.
Functions
A parser for a whole document.
pub fn initFragment(gpa: Allocator, context: Context, options: Options) Allocator.Error!Parser
src
A parser for a fragment in context, as the HTML fragment parsing
algorithm defines it. The document's root is a fragment.
Parse more of the input. The bytes are copied, so the caller may reuse them.
End the input and return the document, which the caller then owns and
frees with Document.deinit. Call deinit on the parser as well.
Parse a whole document.
pub fn parseFragment(
gpa: Allocator,
bytes: []const u8,
context: Context,
options: Options,
) Allocator.Error!Document
src
Parse a fragment in context. The document's root is the fragment.
Types
Parse a whole document.
pub fn parseFragment(
gpa: Allocator,
bytes: []const u8,
context: Context,
options: Options,
) Allocator.Error!Document
src
Parse a fragment in context. The document's root is the fragment.
module
Document
src
A parsed document or fragment.
Functions
A document with only its root: a document node, or a fragment.
The element at index, or null for any other kind of node.
A new node with no parent. Its strings must already belong to the
document; dupe copies one in.
Copy bytes into the document's arena.
pub fn insertBefore(doc: *Document, parent: Node.Index, child: Node.Index, before: Node.Index) void
src
Insert child, which has no parent, into parent before before, or at
the end when before is .none.
Take child out of its parent, if it has one.
pub fn isInclusiveAncestor(doc: *const Document, ancestor: Node.Index, descendant: Node.Index) bool
src
Whether ancestor is descendant or one of its ancestors.
Types
struct
Node
src
Fields
parent: Index
first_child: Index
last_child: Index
prev_sibling: Index
next_sibling: Index
data: Data
Functions
A node holding data, with no parent, children, or siblings.
Types
union
Data
src
pub const Data = union(enum)
Fields
document document
fragment fragment
A DocumentFragment: the result of fragment parsing, and the
contents of a template element.
doctype: Doctype
element: Element
text: ArrayList(u8)
Adjacent text is one node, so this grows as the parser appends.
comment: []const u8
processing_instruction: ProcessingInstruction
struct
Element
src
Fields
namespace: Namespace
name: []const u8
The local name, with the case the standard gives foreign elements:
foreignObject, not foreignobject.
tag: Tag
name as one of the tags the parser treats specially, or .other.
attributes: []const Attribute
template_contents: Node.Index
The fragment holding a template element's contents, or .none.
Functions
pub fn init(namespace: Namespace, name: []const u8, tag: Tag, attributes: []const Attribute) Element
src
An element with no template contents: every element but an HTML
template.
enum
Tag
src
The element names tree construction handles by name, in any namespace.
The field names are the element names, so @tagName gives the name back.
Fields
a a
address address
@"annotation-xml" @"annotation-xml"
applet applet
area area
article article
aside aside
b b
base base
basefont basefont
bgsound bgsound
big big
blockquote blockquote
body body
br br
button button
caption caption
center center
code code
col col
colgroup colgroup
dd dd
desc desc
details details
dialog dialog
dir dir
div div
dl dl
dt dt
em em
embed embed
fieldset fieldset
figcaption figcaption
figure figure
font font
foreignObject foreignObject
form form
frame frame
frameset frameset
h1 h1
h2 h2
h3 h3
h4 h4
h5 h5
h6 h6
head head
header header
hgroup hgroup
hr hr
html html
i i
iframe iframe
image image
img img
input input
keygen keygen
li li
link link
listing listing
main main
malignmark malignmark
marquee marquee
math math
meta meta
mglyph mglyph
mi mi
mn mn
mo mo
ms ms
mtext mtext
nobr nobr
noembed noembed
noframes noframes
noscript noscript
object object
ol ol
optgroup optgroup
option option
p p
param param
plaintext plaintext
pre pre
rb rb
rp rp
rt rt
rtc rtc
ruby ruby
s s
script script
search search
section section
select select
small small
source source
span span
strike strike
strong strong
style style
sub sub
summary summary
sup sup
svg svg
table table
tbody tbody
td td
template template
textarea textarea
tfoot tfoot
th th
thead thead
title title
tr tr
track track
tt tt
u u
ul ul
@"var" @"var"
wbr wbr
xmp xmp
other other
Functions
struct
Node
src
pub const Node = Document.Node
Fields
parent: Index
first_child: Index
last_child: Index
prev_sibling: Index
next_sibling: Index
data: Data
Functions
A node holding data, with no parent, children, or siblings.
Types
union
Data
src
pub const Data = union(enum)
Fields
document document
fragment fragment
A DocumentFragment: the result of fragment parsing, and the
contents of a template element.
doctype: Doctype
element: Element
text: ArrayList(u8)
Adjacent text is one node, so this grows as the parser appends.
comment: []const u8
processing_instruction: ProcessingInstruction
module
Tokenizer
src
The HTML tokenizer, §13.2.5 of the standard.
Functions
Append bytes to the input. They are copied, so the caller may reuse them.
Mark the end of the input. next then runs to the eof token.
Switch to one of the text states. Tree construction does this after a
start tag such as <title> or <script>.
pub fn setLastStartTag(self: *Tokenizer, name: []const u8) Allocator.Error!void
src
Set the tag name an end tag must have to close an RCDATA, RAWTEXT, or script data run, as if a start tag with this name had just been emitted.
The next token, or null when the tokenizer needs more input. After eof
every call returns eof again.
Types
struct
Tag
src
Fields
name: []const u8
Lowercased for ASCII letters, as the standard requires.
attributes: []const Attribute
In document order. A repeated name is dropped, as the standard requires.
self_closing: bool
struct
ParseError
src
A parse error, with the line and column, both counted from one, of the character the tokenizer was looking at when it found the error. Columns count UTF-16 code units, as browsers and html5lib-tests do, so a code point above U+FFFF takes two.
Fields
enum
ErrorCode
src
The parse error codes of §13.2.2 that the tokenizer can report. The names
are the standard's, with _ for -.
Fields
abrupt_closing_of_empty_comment abrupt_closing_of_empty_comment
abrupt_doctype_public_identifier abrupt_doctype_public_identifier
abrupt_doctype_system_identifier abrupt_doctype_system_identifier
absence_of_digits_in_numeric_character_reference absence_of_digits_in_numeric_character_reference
cdata_in_html_content cdata_in_html_content
character_reference_outside_unicode_range character_reference_outside_unicode_range
control_character_in_input_stream control_character_in_input_stream
control_character_reference control_character_reference
disallowed_processing_instruction_target disallowed_processing_instruction_target
duplicate_attribute duplicate_attribute
end_tag_with_attributes end_tag_with_attributes
end_tag_with_trailing_solidus end_tag_with_trailing_solidus
eof_before_tag_name eof_before_tag_name
eof_in_cdata eof_in_cdata
eof_in_comment eof_in_comment
eof_in_doctype eof_in_doctype
eof_in_processing_instruction eof_in_processing_instruction
eof_in_script_html_comment_like_text eof_in_script_html_comment_like_text
eof_in_tag eof_in_tag
incorrectly_closed_comment incorrectly_closed_comment
incorrectly_opened_comment incorrectly_opened_comment
invalid_character_sequence_after_doctype_name invalid_character_sequence_after_doctype_name
invalid_first_character_of_processing_instruction_target invalid_first_character_of_processing_instruction_target
invalid_first_character_of_tag_name invalid_first_character_of_tag_name
invalid_processing_instruction_target invalid_processing_instruction_target
missing_attribute_value missing_attribute_value
missing_doctype_name missing_doctype_name
missing_doctype_public_identifier missing_doctype_public_identifier
missing_doctype_system_identifier missing_doctype_system_identifier
missing_end_tag_name missing_end_tag_name
missing_quote_before_doctype_public_identifier missing_quote_before_doctype_public_identifier
missing_quote_before_doctype_system_identifier missing_quote_before_doctype_system_identifier
missing_semicolon_after_character_reference missing_semicolon_after_character_reference
missing_whitespace_after_doctype_public_keyword missing_whitespace_after_doctype_public_keyword
missing_whitespace_after_doctype_system_keyword missing_whitespace_after_doctype_system_keyword
missing_whitespace_before_doctype_name missing_whitespace_before_doctype_name
missing_whitespace_between_attributes missing_whitespace_between_attributes
missing_whitespace_between_doctype_public_and_system_identifiers missing_whitespace_between_doctype_public_and_system_identifiers
nested_comment nested_comment
noncharacter_character_reference noncharacter_character_reference
noncharacter_in_input_stream noncharacter_in_input_stream
null_character_reference null_character_reference
surrogate_character_reference surrogate_character_reference
unexpected_character_after_doctype_system_identifier unexpected_character_after_doctype_system_identifier
unexpected_character_in_attribute_name unexpected_character_in_attribute_name
unexpected_character_in_unquoted_attribute_value unexpected_character_in_unquoted_attribute_value
unexpected_equals_sign_before_attribute_name unexpected_equals_sign_before_attribute_name
unexpected_null_character unexpected_null_character
unexpected_solidus_in_tag unexpected_solidus_in_tag
unknown_named_character_reference unknown_named_character_reference
enum
TextState
src
The states tree construction switches the tokenizer into, for the
contents of elements such as title, style, script, and plaintext.
Fields
data data
rcdata rcdata
rawtext rawtext
script_data script_data
plaintext plaintext
cdata_section cdata_section
struct
ParseError
src
pub const ParseError = Tokenizer.ParseError
A parse error, with the line and column, both counted from one, of the character the tokenizer was looking at when it found the error. Columns count UTF-16 code units, as browsers and html5lib-tests do, so a code point above U+FFFF takes two.
Fields
enum
ErrorCode
src
pub const ErrorCode = Tokenizer.ErrorCode
The parse error codes of §13.2.2 that the tokenizer can report. The names
are the standard's, with _ for -.
Fields
abrupt_closing_of_empty_comment abrupt_closing_of_empty_comment
abrupt_doctype_public_identifier abrupt_doctype_public_identifier
abrupt_doctype_system_identifier abrupt_doctype_system_identifier
absence_of_digits_in_numeric_character_reference absence_of_digits_in_numeric_character_reference
cdata_in_html_content cdata_in_html_content
character_reference_outside_unicode_range character_reference_outside_unicode_range
control_character_in_input_stream control_character_in_input_stream
control_character_reference control_character_reference
disallowed_processing_instruction_target disallowed_processing_instruction_target
duplicate_attribute duplicate_attribute
end_tag_with_attributes end_tag_with_attributes
end_tag_with_trailing_solidus end_tag_with_trailing_solidus
eof_before_tag_name eof_before_tag_name
eof_in_cdata eof_in_cdata
eof_in_comment eof_in_comment
eof_in_doctype eof_in_doctype
eof_in_processing_instruction eof_in_processing_instruction
eof_in_script_html_comment_like_text eof_in_script_html_comment_like_text
eof_in_tag eof_in_tag
incorrectly_closed_comment incorrectly_closed_comment
incorrectly_opened_comment incorrectly_opened_comment
invalid_character_sequence_after_doctype_name invalid_character_sequence_after_doctype_name
invalid_first_character_of_processing_instruction_target invalid_first_character_of_processing_instruction_target
invalid_first_character_of_tag_name invalid_first_character_of_tag_name
invalid_processing_instruction_target invalid_processing_instruction_target
missing_attribute_value missing_attribute_value
missing_doctype_name missing_doctype_name
missing_doctype_public_identifier missing_doctype_public_identifier
missing_doctype_system_identifier missing_doctype_system_identifier
missing_end_tag_name missing_end_tag_name
missing_quote_before_doctype_public_identifier missing_quote_before_doctype_public_identifier
missing_quote_before_doctype_system_identifier missing_quote_before_doctype_system_identifier
missing_semicolon_after_character_reference missing_semicolon_after_character_reference
missing_whitespace_after_doctype_public_keyword missing_whitespace_after_doctype_public_keyword
missing_whitespace_after_doctype_system_keyword missing_whitespace_after_doctype_system_keyword
missing_whitespace_before_doctype_name missing_whitespace_before_doctype_name
missing_whitespace_between_attributes missing_whitespace_between_attributes
missing_whitespace_between_doctype_public_and_system_identifiers missing_whitespace_between_doctype_public_and_system_identifiers
nested_comment nested_comment
noncharacter_character_reference noncharacter_character_reference
noncharacter_in_input_stream noncharacter_in_input_stream
null_character_reference null_character_reference
surrogate_character_reference surrogate_character_reference
unexpected_character_after_doctype_system_identifier unexpected_character_after_doctype_system_identifier
unexpected_character_in_attribute_name unexpected_character_in_attribute_name
unexpected_character_in_unquoted_attribute_value unexpected_character_in_unquoted_attribute_value
unexpected_equals_sign_before_attribute_name unexpected_equals_sign_before_attribute_name
unexpected_null_character unexpected_null_character
unexpected_solidus_in_tag unexpected_solidus_in_tag
unknown_named_character_reference unknown_named_character_reference