An HTML5 lexer for safe template languages
A standard compliant, incremental/ streaming HTML5 lexer.
This is an HTML5 lexer designed to be used a basis for safe and HTML-context aware template languages, IDEs or syntax highlighters. It is different from the other available tokenizers in that it preserves all the information of the input string, e.g. formatting, quotation style and other idiosyncrasies. It does so by producing annotated chunks of the input string rather than the slightly more high level tokens that are described in the specification. However, it does do so in a manner that is compatible with the language defined in the HTML5 specification.
The main motivation for this project is a jarring absence of safe HTML template languages. By safe, I mean that the template placeholders are typed according to their context, and that the template engine ensures that the strings that come to fill the placeholders are automatically and correctly escaped to yield valid HTML.
The produced tokens are simply tuples (arrays)
[type, chunk] of a token type
and a chunk of the input string.
The lexer has a 'push parser' API.
Lexer constructor takes as its single argument a delegate object with
write (token) and
const Lexer =const delegate =consolenullconst lexer = delegatelexerlexer
[ 'data', '' ] [ 'startTagStart', '<' ] [ 'tagName', 'h1' ] [ 'tagEnd', '>' ] [ 'data', 'Hello, World' ] [ 'endTagStart', '</' ] [ 'tagName', 'h1' ] [ 'tagEnd', '>' ]
The lexer is incremental:
delegate.write will be called as soon as a token is available
and you can split the input across multiple writes:
const lexer = delegatelexerlexerlexerlexer
Warning The type names have changed to use a more consistent naming scheme in version 0.4.0.
The tokens emitted are simple tuples
[type, chunk], or
[type, chunk, info].
The type of a token is just a string, and it is one of:
bogusCharRef is emitted for sequences that start with an ampersand,
but that do not start a character reference, specifically, one of
tagSpace is emitted for 'space' between attributes in
Otherwise the names should be self explanatory.
The doctype states are not implemented.
The lexer interprets doctypes as 'bogus comments'.
The lexer interprets CDATA sections as 'bogus comments'.
(CDATA is only allowed in foreign content - svg and mathml.)
The lexer interprets script tags as rawtext elements. This has no dire consequences, other than that html begin and end comment tags that may surround it, are not marked as such.
- Important The token types have changed to use a more consistent naming scheme.
- Added a Makefile for building a browser version.
- Added a browser based test page.
MIT. Go for it.