Html.Node
type Html.Node = Html.Element | Html.Text | Html.RawHtml.Node is one node of an Html value, as Html.nodes returns it.
type Html.Element = { name: string, attributes: Array[Html.Attribute], children: Html }Html.Element is an element: its name as written, its attributes in order, and its children.
type Html.Text = { text: string }Html.Text is a text node, holding its text decoded.
type Html.Raw = { markup: string }Html.Raw is a node of trusted markup, made by Html.unsafe.
Details
- A value of type
Htmlis read withHtml.nodes, one level at a time; an element'schildrenis anHtml, read withHtml.nodesagain. - Names and attributes are as written, attributes in their final order, a boolean attribute that is on has the empty value, and text is decoded:
&reads as&. - The records are copies. Changing one does not change the
Htmlit came from, and building one does not make markup: no function turns anHtml.Elementrecord intoHtml. - A raw node from
Html.unsafeis opaque: its markup is a string, not elements. Html.Nodeis a closed union. A chain ofistests that covers all three members needs no finalelse, and a member added later breaks it on purpose.
Basic examples
Html.nodes returns the top-level nodes, each one member of Html.Node:
Html.nodes(<p>hi</p>) // [Html.Element{ name: "p", attributes: [], children: hi }]
Html.nodes(<>hi</>) // [Html.Text{ text: "hi" }]
Html.nodes(Html.unsafe("<hr>")) // [Html.Raw{ markup: "<hr>" }]Narrow a node with is to read the fields of its member:
let link = <a href="/docs">Docs</a>
let node = Html.nodes(link)[0]
if node is Html.Element {
println(node.name)
println(node.attributes)
println(node.children)
}a
[Html.Attribute{ name: "href", value: "/docs" }]
DocsHandle every member with a chain of is tests:
func describe(n: Html.Node): string {
if n is Html.Element { "element ${n.name}" }
else if n is Html.Text { "text ${n.text}" }
else if n is Html.Raw { "raw ${n.markup}" }
}
Html.nodes(<>a<br />{Html.unsafe("<hr>")}</>).map(describe) // ["text a", "element br", "raw <hr>"]Scope
Elements
An element's children are an Html, which Html.nodes reads one level further down:
let list = <ul><li>one</li><li>two</li></ul>
let ul = Html.nodes(list)[0]
if ul is Html.Element {
for item in Html.nodes(ul.children) {
println(item)
}
}Html.Element{ name: "li", attributes: [], children: one }
Html.Element{ name: "li", attributes: [], children: two }A line break between tags becomes one space of text, so a list written over several lines has text nodes between its items. Skip them with an is test, or filter({ x is Html.Element }), when you want only the elements:
let list = <ul>
<li>one</li>
<li>two</li>
</ul>
let ul = Html.nodes(list)[0]
if ul is Html.Element {
for item in Html.nodes(ul.children) {
println(item)
}
}Html.Element{ name: "li", attributes: [], children: one }
Html.Text{ text: " " }
Html.Element{ name: "li", attributes: [], children: two }Names keep their case as written, and a boolean attribute that is on reads as the empty value:
let done = true
let shapes = [<svg viewBox="0 0 8 8" />, <input type="checkbox" checked={done} disabled={!done} />]
for h in shapes {
let node = Html.nodes(h)[0]
if node is Html.Element {
println("${node.name}: ${node.attributes}")
}
}svg: [Html.Attribute{ name: "viewBox", value: "0 0 8 8" }]
input: [Html.Attribute{ name: "type", value: "checkbox" }, Html.Attribute{ name: "checked", value: "" }]Text
Text is decoded, and adjacent text joins into one node, whether written or interpolated:
Html.nodes(<>Tom & Jerry</>) // [Html.Text{ text: "Tom & Jerry" }]
Html.nodes(<>{"a"}{"b"}c</>) // [Html.Text{ text: "abc" }]
Html.nodes(<>{"<b>"}</>) // [Html.Text{ text: "<b>" }]
Html.nodes(<>{42}</>) // [Html.Text{ text: "42" }]Raw markup
A raw node holds its string as it is; the markup inside it is not parsed into elements:
Html.nodes(Html.unsafe("<b>x</b>")) // [Html.Raw{ markup: "<b>x</b>" }]An empty value
An empty Html has no nodes, and void or an empty text adds none:
Html.nodes(<></>) // []
Html.nodes(<>{""}{if false { "x" }}</>) // []Testing the member
A value of type Html.Node is exactly one member, which is tests:
let n: Html.Node = Html.Text{ text: "hi" }
n is Html.Text // true
n is Html.Element // falseApplications
Text content
Collect the text of a tree, walking into every element:
func textOf(h: Html): string {
Html.nodes(h)
| Array.map(func(n: Html.Node): string {
if n is Html.Element { textOf(n.children) }
else if n is Html.Text { n.text }
else { "" }
})
| String.join("")
}
println(textOf(<article><h1>Tom & Jerry</h1><p>A <em>short</em> film.</p></article>))Tom & JerryA short film.Links on a page
Find the href of every <a>, at any depth:
func links(h: Html): Array[string] {
Html.nodes(h) | Array.flatMap(func(n: Html.Node): Array[string] {
if !(n is Html.Element) { return [] }
let inner = links(n.children)
if n.name != "a" { return inner }
let href = n.attributes.find({ x.name == "href" })
if href == null { inner } else { [href.value, ...inner] }
})
}
let page = <nav>
<a href="/">Home</a>
<ul><li><a href="/docs">Docs</a></li><li><a>No link</a></li></ul>
</nav>
println(links(page))["/", "/docs"]Testing a component's structure
Check the shape of a component's output without comparing its whole markup:
func badge(count: int): Html {
<span class="badge" title={"${count} unread"}>{count}</span>
}
let root = Html.nodes(badge(3))[0]
assert(root is Html.Element)
assert(root.name == "span")
assert(root.attributes.some({ x.name == "class" && x.value == "badge" }))
assert(root.children == <>3</>)
println("ok")okProperties and relations
An element's children is the Html written inside it:
let body = <>Hello, <b>world</b></>
let node = Html.nodes(<p>{body}</p>)[0]
if node is Html.Element {
assert(node.children == body)
}Nodes compare by their fields, so a node equals the record that describes it:
let node = Html.nodes(<b>x</b>)[0]
assert(node == Html.Element{ name: "b", attributes: [], children: <>x</> })
assert(Html.nodes(<>x</>)[0] == Html.Text{ text: "x" })Possible issues
A field read on the union, before narrowing, is a compile error:
let n = Html.nodes(<p>hi</p>)[0]
println(n.name) // error: Html.Node has no field name on every member: narrow the value firstA record is not markup. An Html.Element record does not convert to Html, and String.from renders it as a record:
let e = Html.Element{ name: "b", attributes: [], children: <>x</> }
let h: Html = e // error: cannot use Html.Element where Html is expectedlet e = Html.Element{ name: "b", attributes: [], children: <>x</> }
String.from(e) // "Html.Element{ name: \"b\", attributes: [], children: x }"Changing a node changes the copy, not the Html it was read from:
let link = <a href="/old">x</a>
let node = Html.nodes(link)[0]
if node is Html.Element {
node.attributes[0].value = "/new"
println(node.attributes)
}
println(link)[Html.Attribute{ name: "href", value: "/new" }]
<a href="/old">x</a>Raw markup is not parsed, so a walk over the tree does not see inside it, and a raw node is not equal to the same markup written as an expression:
Html.nodes(Html.unsafe("<a href=\"/\">x</a>"))[0] is Html.Element // false
Html.unsafe("<b>x</b>") == <b>x</b> // falseA chain over the members that leaves one out is a compile error, since the chain then has no value for that member:
func describe(n: Html.Node): string {
if n is Html.Element { "element" }
else if n is Html.Text { "text" }
// error: an if without else has type void when the condition is false, which cannot be used where string is expected
}When a chain covers every member, it needs no else and cannot have one:
func describe(n: Html.Node): string {
if n is Html.Element { "element ${n.name}" }
else if n is Html.Text { "text ${n.text}" }
else if n is Html.Raw { "raw ${n.markup}" }
else { "unknown" } // error: this else cannot run: the tests before it cover every case
}See also
Html.nodes · Html.unsafe · Html.Attribute · Html.Attributes · Html · HTML expressions · is
