Skip to content

std.web

HTML pages built as values, written as markup or with one function per element, rendered by render or document. Markup (<p class="x">{name}</p>) is read by the parser as calls to this module (element, attr, children). Text and values are escaped when rendered, unless given to raw_html. A page with events has a message type M. A page without events is an Html[()].

type TagName = String
where matches(it, "[a-z][a-z0-9]*(-[a-z0-9]+)*") && it != "script"

Lowercase letters, digits and dashes: div, my-card. Not script: pages can’t hold scripts (raw_html can).

type AttrName = String
where matches(it, "[a-zA-Z_:][-a-zA-Z0-9_:.]*")
&& !matches(it, "[oO][nN].*")
&& !matches(it, "[sS][rR][cC][dD][oO][cC]")

An attribute’s name. Names starting with on are inline scripts, which pages can’t hold. Use on(event, msg) instead. Nor can they hold srcdoc, a whole page inside an iframe.

type EventName = String
where matches(it, "[a-z]+")

A DOM event’s name without on: click, submit, input.

type KeyName = String
where matches(it, "(ctrl\+)?(alt\+)?(meta\+)?.+")

A key for on_key: what the key types (a, /, ?) or its name (Escape, Enter, ArrowLeft). Modifiers come first, in this order: ctrl+, alt+, meta+. meta+p is ⌘P on a Mac. Shift is not named. It is in what the key types (?, A).

type Child[M] = Html[M] | String | Int | List[Html[M]]

What goes between an element’s tags in markup (<p>{x}</p>): an element, text, a number, or a list of elements.

type Html[M] = { node: Node[M] }

HTML whose events carry messages of type M. Only this module’s functions make one, so names are checked once, where it is made (element, attr). Text is escaped when rendered, so it can’t inject markup.

type Attr[M] = { kind: AttrKind[M] }

An attribute of an element. Made by attr, flag and on.

type AttrValue = String | Int | Bool

What name={value} holds in markup: text, a number, or a flag that is there when true.

fn dispatch[M](
h: Html[M],
id: Int,
value: String,
fields: Dict[String, String],
) -> Option[M]

The message of handler number id of the page h, or None if there is no such handler. A handler’s message is its own (on, every, on_key), or made from a field’s value or a form’s fields. The runtime calls it with the page the user sees when a data-handle-… event comes.

fn a[M](attrs: List[Attr[M]], children: List[Html[M]]) -> Html[M]
fn element[M](
tag: TagName,
attrs: List[Attr[M]],
children: List[Html[M]],
) -> Html[M]
requires len(children) == 0
|| !matches(
tag,
"area|base|br|col|embed|hr|img|input|link|meta|source|track|wbr",
)

Any element. Void elements (br, img, input, …) can’t have children. The condition is is_void written out, so the prover sees it.

fn children[M](items: List[Child[M]]) -> List[Html[M]]

The children of an element written as markup, in order: text and numbers become escaped text, a list’s elements are put in its place. Cost: O(n) in the children.

fn text[M](s: String) -> Html[M]

Text, escaped when rendered.

fn id[M](value: String) -> Attr[M]
fn attr[M](name: AttrName, value: String) -> Attr[M]

Any attribute: name="value". The value of an attribute holding a URL (href, src, action, …) goes through safe_url.

fn name[M](value: String) -> Attr[M]
fn value[M](v: String) -> Attr[M]
fn document[M](
title_text: String,
head_items: List[Html[M]],
body_items: List[Html[M]],
) -> String

A whole page: the doctype, a <head> with the title and head, and a <body> holding body.

fn title[M](attrs: List[Attr[M]], children: List[Html[M]]) -> Html[M]
fn render[M](h: Html[M]) -> String

h as HTML text.

fn escape_text(s: String) -> String

s with &, < and > escaped, for text between tags.

fn escape_attr(s: String) -> String

s escaped for an attribute value between double quotes.

fn is_void(tag: String) -> Bool

Elements that have no children and no closing tag.

fn style_sheet[M](css: String) -> Html[M]

A <style> element holding a style sheet.

fn style_link[M](url: String) -> Html[M]

A <link rel="stylesheet"> to the style sheet at url.

fn div[M](attrs: List[Attr[M]], children: List[Html[M]]) -> Html[M]
fn span[M](attrs: List[Attr[M]], children: List[Html[M]]) -> Html[M]
fn p[M](attrs: List[Attr[M]], children: List[Html[M]]) -> Html[M]
fn h1[M](attrs: List[Attr[M]], children: List[Html[M]]) -> Html[M]
fn h2[M](attrs: List[Attr[M]], children: List[Html[M]]) -> Html[M]
fn h3[M](attrs: List[Attr[M]], children: List[Html[M]]) -> Html[M]
fn strong[M](attrs: List[Attr[M]], children: List[Html[M]]) -> Html[M]
fn em[M](attrs: List[Attr[M]], children: List[Html[M]]) -> Html[M]
fn code[M](attrs: List[Attr[M]], children: List[Html[M]]) -> Html[M]
fn pre[M](attrs: List[Attr[M]], children: List[Html[M]]) -> Html[M]
fn ul[M](attrs: List[Attr[M]], children: List[Html[M]]) -> Html[M]
fn ol[M](attrs: List[Attr[M]], children: List[Html[M]]) -> Html[M]
fn li[M](attrs: List[Attr[M]], children: List[Html[M]]) -> Html[M]
fn table[M](attrs: List[Attr[M]], children: List[Html[M]]) -> Html[M]
fn thead[M](attrs: List[Attr[M]], children: List[Html[M]]) -> Html[M]
fn tbody[M](attrs: List[Attr[M]], children: List[Html[M]]) -> Html[M]
fn tr[M](attrs: List[Attr[M]], children: List[Html[M]]) -> Html[M]
fn th[M](attrs: List[Attr[M]], children: List[Html[M]]) -> Html[M]
fn td[M](attrs: List[Attr[M]], children: List[Html[M]]) -> Html[M]
fn header[M](attrs: List[Attr[M]], children: List[Html[M]]) -> Html[M]
fn footer[M](attrs: List[Attr[M]], children: List[Html[M]]) -> Html[M]
fn nav[M](attrs: List[Attr[M]], children: List[Html[M]]) -> Html[M]
fn section[M](attrs: List[Attr[M]], children: List[Html[M]]) -> Html[M]
fn article[M](attrs: List[Attr[M]], children: List[Html[M]]) -> Html[M]
fn form[M](attrs: List[Attr[M]], children: List[Html[M]]) -> Html[M]
fn label[M](attrs: List[Attr[M]], children: List[Html[M]]) -> Html[M]
fn button[M](attrs: List[Attr[M]], children: List[Html[M]]) -> Html[M]
fn textarea[M](attrs: List[Attr[M]], children: List[Html[M]]) -> Html[M]
fn select[M](attrs: List[Attr[M]], children: List[Html[M]]) -> Html[M]
fn option[M](attrs: List[Attr[M]], children: List[Html[M]]) -> Html[M]
fn input[M](attrs: List[Attr[M]]) -> Html[M]

An <input>. A void element, so attributes only.

fn img[M](attrs: List[Attr[M]]) -> Html[M]

An <img>. A void element, so attributes only.

fn br[M]() -> Html[M]

A line break.

fn hr[M]() -> Html[M]

A horizontal rule.

fn class[M](names: String) -> Attr[M]
fn href[M](url: String) -> Attr[M]
fn src[M](url: String) -> Attr[M]
fn alt[M](text: String) -> Attr[M]
fn style[M](css: String) -> Attr[M]
fn input_type[M](kind: String) -> Attr[M]

The type attribute of an input or a button. type is a keyword.

fn placeholder[M](text: String) -> Attr[M]
fn label_for[M](input_id: String) -> Attr[M]

The for attribute of a label. for is a keyword.

fn disabled[M]() -> Attr[M]
fn flag[M](name: AttrName) -> Attr[M]

An attribute without a value, such as required.

fn checked[M]() -> Attr[M]
fn required[M]() -> Attr[M]
fn on_click[M](msg: M) -> Attr[M]

Sends msg when the element is clicked.

fn on[M](event: EventName, msg: M) -> Attr[M]

Sends msg to the program when event happens on the element.

fn on_submit[M](msg: M) -> Attr[M]

Sends msg when the form is submitted.

fn on_input[M](make: (String) -> M) -> Attr[M]

Sends make(value) as the user types in a field (input, textarea): on_input(s -> SetTitle { title: s }).

fn on_change[M](make: (String) -> M) -> Attr[M]

Sends make(value) when the field’s value changes (select, checkbox). A checkbox’s value is "true" or "false".

fn on_submit_form[M](make: (Dict[String, String]) -> M) -> Attr[M]

Sends make(fields) when the form is submitted, with its fields by name: on_submit_form(fields -> Add { fields }). An unchecked checkbox is not among them.

fn every[M](ms: Int where it >= 100, msg: M) -> Attr[M]

Sends msg every ms milliseconds while the element is on the page: div([every(1500, Refresh)], [...]). A message isn’t sent while the previous one is still being handled, so a slow update doesn’t pile them up.

fn key_bindings[M](bindings: List[(KeyName, M)]) -> Html[M]

Several keys and their messages (see on_key), as an element that shows nothing. Put it anywhere in the view.

fn on_key[M](key: KeyName, msg: M) -> Attr[M]

Sends msg when key is pressed anywhere on the page while the element is on it. The browser doesn’t also act on the key. While the user types in a field, only keys with ctrl+ or meta+, and Escape, are sent. One key per element. For several, use key_bindings.

fn focused[M]() -> Attr[M]

The element takes the keyboard focus when it appears with this attribute, or the attribute appears on it.

fn revealed[M]() -> Attr[M]

The page scrolls as little as needed to show the element when it appears with this attribute, or the attribute appears on it.

fn attribute[M](name: AttrName, value: AttrValue) -> Attr[M]

An attribute written name={value} in markup: class={c}, colspan={2}, disabled={busy} (a flag, left out when false).

fn raw_html[M](html: String) -> Html[M]

HTML written into the page as it is, not escaped. Only for markup from a source you trust: text from users must go through text.

fn holds_url(name: String) -> Bool

Whether the attribute name holds a URL a browser follows or loads.

fn safe_url(url: String) -> String where allowed_scheme(scheme(it))
ensures it == "#" || it == url

url, or # when its scheme isn’t one a page may link to: http, https, mailto, tel, or none (a relative URL). So javascript: and data: URLs can’t run code.

fn allowed_scheme(s: String) -> Bool

The schemes safe_url keeps.

fn scheme(url: String) -> String
ensures !starts_with(url, "#") || it == ""

The scheme of url, lowercase, as a browser reads it: what comes before the first : when no /, ? or # comes first, without the spaces, tabs, line breaks and control characters a browser skips. "" when it has none (a relative URL).