Sablon.HTML.Parser (sablon v0.4.3)

Copy Markdown View Source

A lenient HTML fragment parser.

It produces the same mutable DOM the WordML side uses, so the AST builder can navigate and rewrite HTML exactly the way it does markup from the template.

Parsing follows the parts of the HTML rules that matter for document content: tag and attribute names are case insensitive, attribute values may be unquoted or absent, void elements never nest, and tags with optional end tags (p, li, tr, td, ...) are closed implicitly. Whitespace is preserved everywhere - a space between two inline tags is content, and dropping it would silently run two words together in the rendered document.

Summary

Functions

Decodes HTML character references.

The name given to the fragment's root element.

Parses html and returns the fragment's root element.

Parses html into a tree of {:element, name, attrs, children}, {:text, binary} and {:comment, binary} tuples. Exposed for testing the parser on its own.

Functions

decode_entities(binary)

@spec decode_entities(binary()) :: binary()

Decodes HTML character references.

Numeric references are always decoded; named ones are decoded when they are in the supported table and otherwise left as written, which keeps unknown markup intact rather than mangling it.

fragment_name()

@spec fragment_name() :: binary()

The name given to the fragment's root element.

parse_fragment(html)

@spec parse_fragment(binary()) :: Sablon.XML.Node.t()

Parses html and returns the fragment's root element.

The root is named #document-fragment, matching the tag the configuration registers for top level content.

simple_form(html)

@spec simple_form(binary()) :: [tuple()]

Parses html into a tree of {:element, name, attrs, children}, {:text, binary} and {:comment, binary} tuples. Exposed for testing the parser on its own.