PureHTML.Serializer (PureHTML v0.5.0)

View Source

Converts parsed HTML nodes back to HTML strings.

Implements the HTML fragment serialization algorithm.

Options

  • :scripting — whether scripting is enabled (default: true).

    to_html/2 is usually called on a tree parse/2 built. Parse's scripting flag decides whether noscript children are a text node or elements. With the default parse (scripting on), noscript holds one text node that must be emitted literally, so the serializer default is true. If the tree was parsed with scripting off, noscript holds elements and this flag does not affect them.

Summary

Functions

Serializes a list of parsed HTML nodes to an HTML string.

True for the HTML void elements, which serialize as a start tag only.

Functions

serialize(nodes, opts \\ [])

@spec serialize(
  [PureHTML.html_node()],
  keyword()
) :: String.t()

Serializes a list of parsed HTML nodes to an HTML string.

Examples

iex> PureHTML.Serializer.serialize([{"p", [], ["Hello"]}])
"<p>Hello</p>"

iex> PureHTML.Serializer.serialize([{"br", [], []}])
"<br>"

iex> PureHTML.Serializer.serialize([{"span", [{"title", "foo"}], []}])
"<span title=\"foo\"></span>"

void_element?(tag)

@spec void_element?(String.t()) :: boolean()

True for the HTML void elements, which serialize as a start tag only.