Html

:= []

Build and render small server-generated HTML documents.

Text and attribute values are escaped by default. Tag and attribute names supplied to the generic constructors are trusted syntax and must be constants, not user input.

page = Html.html([], [
    Html.body([], [
        Html.h1([], [Html.text("Hello <Roc>")]),
        Html.a([Attribute.href("/account")], [Html.text("Account")]),
    ]),
])

response = Response.from_status(200)
    .with_headers([{ name: "Content-Type", value: "text/html; charset=utf-8" }])
    .with_body(Str.to_utf8(Html.render(page)))
text : Str -> HtmlNode

Construct escaped text. Use this for all untrusted or dynamic text.

render : HtmlNode -> Str

Render a complete HTML document beginning with <!DOCTYPE html>.

render_document : HtmlNode -> Document

Render one root node as a complete HTML document.

fragment_from_rendered_str : Str -> Fragment

Treat an already-rendered string as a patchable fragment.

This is the explicit compatibility boundary for another renderer or trusted literal markup. Dynamic or untrusted text belongs in [text].

Fragment

:= [Fragment(Str)]

Rendered sibling HTML nodes for APIs that require an HTML fragment.

This nominal boundary distinguishes rendered markup from ordinary text. It does not claim that the fragment has particular IDs or matches a DOM selector.

to_str : Fragment -> Str

Return the rendered fragment text.

Document

:= [Document(Str)]

A complete rendered HTML document including its document type.

to_str : Document -> Str

Return the rendered document text.

Node : HtmlNode

An HTML node. Prefer this module's constructors so text and attribute values are escaped consistently.