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 -> Node

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

element : Str, List(Attribute), List(Node) -> Node

Construct a normal element with a trusted tag name.

void_element : Str, List(Attribute) -> Node

Construct a void element with a trusted tag name.

render : Node -> Str

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

render_document : Node -> 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].

html : List(Attribute), List(Node) -> Node

Construct an html element.

head : List(Attribute), List(Node) -> Node

Construct a head element.

body : List(Attribute), List(Node) -> Node

Construct a body element.

title : List(Attribute), List(Node) -> Node

Construct a title element.

header : List(Attribute), List(Node) -> Node

Construct a header element.

nav : List(Attribute), List(Node) -> Node

Construct a nav element.

main : List(Attribute), List(Node) -> Node

Construct a main element.

div : List(Attribute), List(Node) -> Node

Construct a div element.

span : List(Attribute), List(Node) -> Node

Construct a span element.

p : List(Attribute), List(Node) -> Node

Construct a p element.

a : List(Attribute), List(Node) -> Node

Construct an a element.

ul : List(Attribute), List(Node) -> Node

Construct a ul element.

li : List(Attribute), List(Node) -> Node

Construct an li element.

h1 : List(Attribute), List(Node) -> Node

Construct an h1 element.

h2 : List(Attribute), List(Node) -> Node

Construct an h2 element.

h3 : List(Attribute), List(Node) -> Node

Construct an h3 element.

h4 : List(Attribute), List(Node) -> Node

Construct an h4 element.

h5 : List(Attribute), List(Node) -> Node

Construct an h5 element.

h6 : List(Attribute), List(Node) -> Node

Construct an h6 element.

form : List(Attribute), List(Node) -> Node

Construct a form element.

button : List(Attribute), List(Node) -> Node

Construct a button element.

label : List(Attribute), List(Node) -> Node

Construct a label element.

table : List(Attribute), List(Node) -> Node

Construct a table element.

thead : List(Attribute), List(Node) -> Node

Construct a thead element.

tbody : List(Attribute), List(Node) -> Node

Construct a tbody element.

tr : List(Attribute), List(Node) -> Node

Construct a tr element.

th : List(Attribute), List(Node) -> Node

Construct a th element.

td : List(Attribute), List(Node) -> Node

Construct a td element.

svg : List(Attribute), List(Node) -> Node

Construct an svg element.

select : List(Attribute), List(Node) -> Node

Construct a select element.

option : List(Attribute), List(Node) -> Node

Construct an option element.

input : List(Attribute) -> Node

Construct an input void element.

meta : List(Attribute) -> Node

Construct a meta void element.

link : List(Attribute) -> Node

Construct a link void element.

br : List(Attribute) -> Node

Construct a br void element.

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

:= [
    Text(Str),
    Raw(Str),
    Element(Str, List(Attribute), List(Node)),
    VoidElement(Str, List(Attribute)),
]

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