Html
type HtmlHtml is the value of an HTML expression, <p>{name}</p>: an immutable list of nodes, each an element, a text node, or raw markup from Html.unsafe. It compares by its nodes with ==, and String.from renders it as markup, escaping text and attribute values.
An Html value is markup that a program builds with HTML expressions, compares, and renders. The Html namespace holds the two functions that work on it: one reads its nodes, and one turns a trusted string into markup. Html is also the type, for a component's return type or a record field.
Details
Htmlhas no constructor. An HTML expression makes it, such as<p>{name}</p>or the fragment<>...</>, andHtml.unsafemakes it from a string of trusted markup.- A value is kept in one normal form: fragments are spliced in,
voidleaves nothing, adjacent text is joined, and empty text is dropped. So two values built differently from the same elements and text are==, and anHtmlworks as a map key. String.fromrenders the markup, and so doprintlnand${...}in a string. Text and attribute values are escaped on the way out.len, indexing, andfordo not takeHtml. Read its nodes withHtml.nodes.Json.stringifypanics on it.
Basic examples
An HTML expression is a value of type Html. Interpolated text is escaped when it renders:
let name = "Tom & <Jerry>"
let greeting = <p class="greet">Hello, {name}</p>
println(greeting)<p class="greet">Hello, Tom & <Jerry></p>String.from gives the markup as a string:
String.from(<b>bold</b>) // "<b>bold</b>"
String.from(<br />) // "<br>"A function that returns Html is a component:
func badge(count: int): Html {
<span class="badge">{count}</span>
}
println(<h2>Inbox {badge(3)}</h2>)<h2>Inbox <span class="badge">3</span></h2>Scope
The empty fragment is the empty Html. It renders as nothing, and as <></> inside a container:
String.from(<></>) // ""
[<></>, <b>x</b>] // [<></>, <b>x</b>]An array of Html inserts its elements in order, nothing between them:
let items = ["milk", "eggs"] | Array.map({ <li>{x}</li> })
String.from(<ul>{items}</ul>) // "<ul><li>milk</li><li>eggs</li></ul>"Quotes and apostrophes in an attribute value are escaped:
String.from(<p title={"it's \"new\""}>x</p>) // "<p title=\"it's "new"\">x</p>"A string embeds the rendered markup, which is how a page gets its doctype:
let page = <html><body>Hi</body></html>
"<!doctype html>${page}" // "<!doctype html><html><body>Hi</body></html>"Html is a map key, and two values meaning the same find one entry:
let hits: Map[Html, int] = Map{}
hits[<b>x</b>] = 1
hits[<b>{"x"}</b>] // 1A record field can hold Html:
type Section = { title: string, body: Html }
let intro = Section{ title: "Intro", body: <p>Ants live in colonies.</p> }
println(<section><h2>{intro.title}</h2>{intro.body}</section>)<section><h2>Intro</h2><p>Ants live in colonies.</p></section>Functions and types by job
Making markup from a string
- Html.unsafe: a string of trusted markup as
Html, rendered unescaped
Reading the nodes
- Html.nodes: the top-level nodes, in order
- Html.Node: one node, an
Html.Element, anHtml.Text, or anHtml.Raw - Html.Attribute: one attribute of an element, a name and a value
Spreading attributes
- Html.Attributes: the map an attribute spread
{...attrs}takes
Applications
Test a component
Compare a component's output with the HTML expression it should give. The $ is literal text, and {whole} interpolates:
func price(cents: int): Html {
let whole = cents / 100
let rest = String.from(cents % 100).padStart(2, "0")
<span class="price">${whole}.{rest}</span>
}
assert(price(1250) == <span class="price">$12.50</span>)
assert(price(5) == <span class="price">$0.05</span>)Collect the text of a tree
Html.nodes reads one level; a recursive function reads the whole tree:
func nodeText(node: Html.Node): string {
if node is Html.Text { return node.text }
if node is Html.Element { return textOf(node.children) }
""
}
func textOf(h: Html): string {
Html.nodes(h) | Array.map(nodeText) | String.join("")
}
println(textOf(<p>Ants & <em>bees</em> work.</p>))Ants & bees work.Count the elements
func countElements(h: Html): int {
var count = 0
for node in Html.nodes(h) {
if node is Html.Element {
count += 1 + countElements(node.children)
}
}
count
}
println(countElements(<article><h1>Title</h1><p>One <em>two</em></p></article>))4Properties and relations
The normal form makes these pairs equal:
assert(<p>{"a"}{"b"}</p> == <p>ab</p>)
assert(<p>{<>x</>}</p> == <p>x</p>)
assert(<p>{""}</p> == <p></p>)
assert(<i>{if false { "hidden" }}</i> == <i></i>)Rendering text escapes it, and Html.nodes gives it back decoded:
let text = "1 < 2 & 3 > 2"
let h = <>{text}</>
assert(String.from(h) == "1 < 2 & 3 > 2")
assert(Html.nodes(h)[0] == Html.Text{ text: text })Possible issues
Html is not a collection
len, for, and indexing do not take Html:
let h = <><li>a</li><li>b</li></>
let n = len(h)
// error: len needs an array, a map, a set, a string, Bytes, or a channel, got Html
for x in h {
// error: for needs an array, a map, a set, a string, Bytes, or a sequence, got Html
println(x)
}
let first = h[0] // error: Html cannot be indexedRead the nodes as an array with Html.nodes:
let h = <><li>a</li><li>b</li></>
Html.nodes(h).length() // 2
Html.nodes(h)[0] // Html.Element{ name: "li", attributes: [], children: a }A string is not markup
A string never converts to Html. Interpolate it as text, or pass trusted markup through Html.unsafe:
let h: Html = "<b>x</b>" // error: cannot use string where Html is expectedA node record is not markup
Html.nodes returns copies as records, and no function turns a record back into Html. An Html.Element in content is a compile error:
let e = Html.Element{ name: "b", attributes: [], children: <>x</> }
let page = <div>{e}</div>
// error: markup takes a string, a number, void, Html, or an array of them, got Html.ElementRaw markup is not equal to the markup it spells
A raw node from Html.unsafe renders as the markup it holds, but it is not == to an HTML expression that builds the same markup:
let raw = Html.unsafe("<b>x</b>")
String.from(raw) == String.from(<b>x</b>) // true
raw == <b>x</b> // falseCompare rendered strings when a value holds raw markup.
Json.stringify panics
JSON has no form for Html. Render it with String.from and put the string in the JSON:
Json.stringify(<p>hi</p>)
// panic: Json.stringify: Html has no JSON form; write String.from of itJson.stringify(String.from(<p>hi</p>)) // "\"<p>hi</p>\""See also
- Html.unsafe: trusted markup from a string
- Html.nodes: the top-level nodes of a value
- String.from: render a value as a string
- HTML expressions: the syntax that builds
Html - HTML expressions guide: how the parts fit together
- Serving a page: serve markup from an HTTP handler
