html
lib/html.vklib/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 { kind enum, tag str = ..., attrs map = ..., children block = ..., content str = ... }
Typed HTML DOM node (.el, .text, .raw, .frag, .comment, .doctype).
escape = fn [s str] str
Escapes HTML text content (&, <, >, ", ') to prevent XSS.
use [html]
if (html/escape '<script>alert("x")</script>') != '<script>alert("x")</script>' [
err 'escape'
] else [ .ok ]
el = fn [tag any, attrs map, children block] Node
Creates an HTML element node (accepts either .div enum or 'div' string tag).
raw = fn [trusted_html str] Node
Creates a trusted unescaped raw HTML/SVG node (never pass untrusted user input!).
frag = fn [children block] Node
Creates a document fragment grouping multiple children without a wrapper element.
div = fn [attrs map, children block] Node
Ergonomic HTML5 element helpers for typed pure-Veko components.
span = fn [attrs map, children block] Node
p = fn [attrs map, children block] Node
a = fn [attrs map, children block] Node
h1 = fn [attrs map, children block] Node
h2 = fn [attrs map, children block] Node
h3 = fn [attrs map, children block] Node
ul = fn [attrs map, children block] Node
ol = fn [attrs map, children block] Node
li = fn [attrs map, children block] Node
section = fn [attrs map, children block] Node
article = fn [attrs map, children block] Node
header = fn [attrs map, children block] Node
main-el = fn [attrs map, children block] Node
form = fn [attrs map, children block] Node
label = fn [attrs map, children block] Node
button = fn [attrs map, children block] Node
table = fn [attrs map, children block] Node
thead = fn [attrs map, children block] Node
tbody = fn [attrs map, children block] Node
tr = fn [attrs map, children block] Node
th = fn [attrs map, children block] Node
td = fn [attrs map, children block] Node
input = fn [attrs map] Node
img = fn [attrs map] Node
br = fn [] Node
hr = fn [] Node
meta-tag = fn [attrs map] Node
link-css = fn [href str] Node
style-el = fn [css str] Node
script = fn [attrs map, js str] Node
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.
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 <Veko></h1>')) [
err 'html document'
] else [ .ok ]
render = fn [node any] str
Renders any typed html/Node, Hiccup block ([.div { class = 'x' } ['hi']]), or nested component tree into an HTML string.
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 ]