veko doc
Self-Contained API Reference • Gradual Types • Capabilities • Wasm Sandbox

html

lib/html.vk

lib/html.vk Pure-Veko homoiconic HTML5 component DSL & typed DOM builder. Supports both Hiccup-style Veko blocks ([.div { class = 'card' } [ [.h1 ['Hi']] ]]) and typed pure-Veko component builders (html/el, html/div, html/document), with automatic XSS text/attribute escaping, URL scheme sanitization, HTML5 void tags, and seamless integration with lib/web (web/view, web/page). Zero capabilities required.

Node struct
signature
Node = struct { kind enum, tag str = ..., attrs map = ..., children block = ..., content str = ... }

Typed HTML DOM node (.el, .text, .raw, .frag, .comment, .doctype).

signature
escape = fn [s str] str

Escapes HTML text content (&, <, >, ", ') to prevent XSS.

Executable Example
veko
use [html]
if (html/escape '<script>alert("x")</script>') != '&lt;script&gt;alert(&quot;x&quot;)&lt;/script&gt;' [
  err 'escape'
] else [ .ok ]
el fn
signature
el = fn [tag any, attrs map, children block] Node

Creates an HTML element node (accepts either .div enum or 'div' string tag).

text fn
signature
text = fn [s str] Node

Creates an auto-escaped text node.

raw fn
signature
raw = fn [trusted_html str] Node

Creates a trusted unescaped raw HTML/SVG node (never pass untrusted user input!).

frag fn
signature
frag = fn [children block] Node

Creates a document fragment grouping multiple children without a wrapper element.

signature
comment = fn [s str] Node

Creates an HTML comment node (<!-- ... -->).

signature
doctype = fn [] Node

Creates an HTML5 <!DOCTYPE html> preamble node.

div fn
signature
div = fn [attrs map, children block] Node

Ergonomic HTML5 element helpers for typed pure-Veko components.

span fn
signature
span = fn [attrs map, children block] Node
p fn
signature
p = fn [attrs map, children block] Node
a fn
signature
a = fn [attrs map, children block] Node
h1 fn
signature
h1 = fn [attrs map, children block] Node
h2 fn
signature
h2 = fn [attrs map, children block] Node
h3 fn
signature
h3 = fn [attrs map, children block] Node
ul fn
signature
ul = fn [attrs map, children block] Node
ol fn
signature
ol = fn [attrs map, children block] Node
li fn
signature
li = fn [attrs map, children block] Node
signature
section = fn [attrs map, children block] Node
signature
article = fn [attrs map, children block] Node
signature
header = fn [attrs map, children block] Node
nav fn
signature
nav = fn [attrs map, children block] Node
signature
main-el = fn [attrs map, children block] Node
form fn
signature
form = fn [attrs map, children block] Node
signature
label = fn [attrs map, children block] Node
signature
button = fn [attrs map, children block] Node
signature
table = fn [attrs map, children block] Node
signature
thead = fn [attrs map, children block] Node
signature
tbody = fn [attrs map, children block] Node
tr fn
signature
tr = fn [attrs map, children block] Node
th fn
signature
th = fn [attrs map, children block] Node
td fn
signature
td = fn [attrs map, children block] Node
signature
input = fn [attrs map] Node
img fn
signature
img = fn [attrs map] Node
br fn
signature
br = fn [] Node
hr fn
signature
hr = fn [] Node
signature
meta-tag = fn [attrs map] Node
signature
style-el = fn [css str] Node
signature
script = fn [attrs map, js str] Node
signature
document = fn [title str, head_extra block, body_children block] Node

Builds a complete HTML5 <!DOCTYPE html> document with <meta charset="utf-8">, viewport, <title>, optional head_extra nodes, and body_children.

Executable Example
veko
use [html]
doc = html/document 'Veko App' [] [ [.h1 ['Hello <Veko>']] ]
out = html/render doc
if (not (str/contains? out '<!DOCTYPE html>')) or (not (str/contains? out '<h1>Hello &lt;Veko&gt;</h1>')) [
  err 'html document'
] else [ .ok ]
signature
render = fn [node any] str

Renders any typed html/Node, Hiccup block ([.div { class = 'x' } ['hi']]), or nested component tree into an HTML string.

Executable Example
veko
use [html]
card = fn [title str, items block] block [
  [.section { class = 'card' } [
    [.h2 [title]]
    [.ul (map items fn [it] [ [.li [it]] ])]
    [.a { href = 'javascript:alert(1)' } ['Safe']]
  ]]
]
s = html/render (card 'Tasks' ['Build' 'Ship'])
if (not (str/contains? s '<section class="card"><h2>Tasks</h2><ul><li>Build</li><li>Ship</li></ul><a href="#">Safe</a></section>')) [
  err 'hiccup render'
] else [ .ok ]