Parsers for HTML syntax #
Compliant with the HTML living standard to the extent that it makes sense. Departures are documented on the appropriate parsers.
We eschew Lean's standard mechanism of syntactic categories in favor of hand-rolled parsers, formatters, and parenthesizers for two reasons:
Lean parsers generally consume trailing whitespace, whereas certain HTML parsers (e.g. end tag
>symbols) should not do so, so that this whitespace is instead included in text content following those parsers.Category parsing inspects the leading token and dispatches to the appropriate parser whereas we allow text contents to begin with reserved symbols such as
'.
In content, all whitespace is preserved by parsers, and then collapsed in
Content.view.To simplify away special handling of
$, most parsers here cannot be antiquoted. Parsers that can be antiquoted are documented as such.The docstrings of certain parsers are shown in IDEs on hover. These parsers may contain additional, non-docstring comments describing non-user-facing implementation details.
Helpers #
Character references #
Decodes one character reference starting at i,
which must point at at the character & in s.
Returns the decoded string, and the position just after ;.
Errors are reported at refAt,
which should produce the subsyntax between two offsets into s.
Instances For
Decodes HTML character references in ref, leaving other characters unchanged.
Instances For
Raw symbols #
Parses sym as an atom.
Unlike symbol, this parser does not consult the token table,
and sym is not registered as a token.
Trailing whitespace (including Lean-style comments) is consumed and stored in the atom
only if trailingWs is set; otherwise it is left to the next parser.
Instances For
Instances For
Instances For
Interpolations #
Parses an interpolation: a Lean term between openSym and }.
Trailing whitespace is consumed only if trailingWs is set.
Instances For
Instances For
Instances For
Instances For
Instances For
Text content #
Comments #
Text contents of an HTML comment, excluding start and end markers.
Instances For
Tag names #
An HTML tag name:
an ASCII letter followed by characters other than ASCII whitespace, U+0000 NULL, /, >.
This includes custom element names.
Instances For
Instances For
Attribute names #
An HTML attribute name.
Attribute names may not start with { (which would conflict with interpolation).
Instances For
Instances For
Attribute values #
- str (stx : TSyntax `str) : AttrValView
A string literal. Character references in the literal must be decoded before use (via
decodeCharacterReferences). - interp (stx : Interp false) : AttrValView
Instances For
Provides a view on the attribute value, decoding character references when the value is a string literal.
Instances For
Instances For
Attributes #
An HTML attribute.
We support double-quoted attribute values <tag name="val">,
empty attributes <tag name>,
interpolations of one value <tag name={ term }>,
and interpolations of a sequence of attributes <tag {... term }/>.
Character references are decoded in double-quoted values.
Instances For
Instances For
Instances For
Elements and content #
Content is not a syntax category because a category parser starts by reading a Lean token,
which fails on text that begins with whitespace or, for example, '.
Instead, contentItemFn dispatches to item parsers based on the next character.
A sequence of HTML text nodes, comments, elements, and interpolations.
Instances For
Instances For
Instances For
An HTML element:
<tag attr*/> or <tag attr*>content</tag>.
Tags must be closed: void element syntax such as <br>,
and implied end tags such as <ul><li>item</ul>,
are not supported.
Instances For
Instances For
Instances For
Returns the normalized text contents of this sequence of text nodes and comments:
Whitespace that contains a newline (U+000A or U+000D), and is not surrounded by text on both sides, is removed.
Other consecutive whitespace is collapsed into a single space (U+0020).
HTML character references are decoded into the Unicode characters they represent.
Throws if an invalid character reference is encountered in the text.
Instances For
A syntax spanning all nodes in this view. Useful for reporting elaboration errors.
Instances For
- element (stx : Element) : ContentItemView
- textComments (stx : TextCommentsView) : ContentItemView
- interp (isMany : Bool) (stx : Interp isMany) : ContentItemView
Instances For
Instances For
Instances For
Returns the sequence of items in an HTML content node.
Runs of text interspersed with comments are merged into a single item.
This is because their text must be processed together into a single output
(see TextCommentsView.getText).