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[()].
TagName
Section titled “TagName”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).
AttrName
Section titled “AttrName”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.
EventName
Section titled “EventName”type EventName = String where matches(it, "[a-z]+")A DOM event’s name without on: click, submit, input.
KeyName
Section titled “KeyName”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.
AttrValue
Section titled “AttrValue”type AttrValue = String | Int | BoolWhat name={value} holds in markup: text, a number, or a flag that is
there when true.
Functions
Section titled “Functions”dispatch
Section titled “dispatch”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]element
Section titled “element”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.
children
Section titled “children”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]document
Section titled “document”fn document[M]( title_text: String, head_items: List[Html[M]], body_items: List[Html[M]],) -> StringA 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]render
Section titled “render”fn render[M](h: Html[M]) -> Stringh as HTML text.
escape_text
Section titled “escape_text”fn escape_text(s: String) -> Strings with &, < and > escaped, for text between tags.
escape_attr
Section titled “escape_attr”fn escape_attr(s: String) -> Strings escaped for an attribute value between double quotes.
is_void
Section titled “is_void”fn is_void(tag: String) -> BoolElements that have no children and no closing tag.
style_sheet
Section titled “style_sheet”fn style_sheet[M](css: String) -> Html[M]A <style> element holding a style sheet.
style_link
Section titled “style_link”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]strong
Section titled “strong”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]header
Section titled “header”fn header[M](attrs: List[Attr[M]], children: List[Html[M]]) -> Html[M]footer
Section titled “footer”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]section
Section titled “section”fn section[M](attrs: List[Attr[M]], children: List[Html[M]]) -> Html[M]article
Section titled “article”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]button
Section titled “button”fn button[M](attrs: List[Attr[M]], children: List[Html[M]]) -> Html[M]textarea
Section titled “textarea”fn textarea[M](attrs: List[Attr[M]], children: List[Html[M]]) -> Html[M]select
Section titled “select”fn select[M](attrs: List[Attr[M]], children: List[Html[M]]) -> Html[M]option
Section titled “option”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]input_type
Section titled “input_type”fn input_type[M](kind: String) -> Attr[M]The type attribute of an input or a button. type is a keyword.
placeholder
Section titled “placeholder”fn placeholder[M](text: String) -> Attr[M]label_for
Section titled “label_for”fn label_for[M](input_id: String) -> Attr[M]The for attribute of a label. for is a keyword.
disabled
Section titled “disabled”fn disabled[M]() -> Attr[M]fn flag[M](name: AttrName) -> Attr[M]An attribute without a value, such as required.
checked
Section titled “checked”fn checked[M]() -> Attr[M]required
Section titled “required”fn required[M]() -> Attr[M]on_click
Section titled “on_click”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.
on_submit
Section titled “on_submit”fn on_submit[M](msg: M) -> Attr[M]Sends msg when the form is submitted.
on_input
Section titled “on_input”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 }).
on_change
Section titled “on_change”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".
on_submit_form
Section titled “on_submit_form”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.
key_bindings
Section titled “key_bindings”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.
on_key
Section titled “on_key”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.
focused
Section titled “focused”fn focused[M]() -> Attr[M]The element takes the keyboard focus when it appears with this attribute, or the attribute appears on it.
revealed
Section titled “revealed”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.
attribute
Section titled “attribute”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).
raw_html
Section titled “raw_html”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.
holds_url
Section titled “holds_url”fn holds_url(name: String) -> BoolWhether the attribute name holds a URL a browser follows or loads.
safe_url
Section titled “safe_url”fn safe_url(url: String) -> String where allowed_scheme(scheme(it)) ensures it == "#" || it == urlurl, 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.
allowed_scheme
Section titled “allowed_scheme”fn allowed_scheme(s: String) -> BoolThe schemes safe_url keeps.
scheme
Section titled “scheme”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).