568 lines
14 KiB
Go
568 lines
14 KiB
Go
package html
|
|
|
|
import (
|
|
"html"
|
|
"iter"
|
|
"maps"
|
|
"slices"
|
|
"strconv"
|
|
"strings"
|
|
|
|
i18n "dappco.re/go/core/i18n"
|
|
)
|
|
|
|
// node.go: Node is anything renderable.
|
|
// Example: El("p", Text("page.body")) returns a Node that can be passed to Render().
|
|
type Node interface {
|
|
Render(ctx *Context) string
|
|
}
|
|
|
|
// Compile-time interface checks.
|
|
var (
|
|
_ Node = (*rawNode)(nil)
|
|
_ Node = (*elNode)(nil)
|
|
_ Node = (*textNode)(nil)
|
|
_ Node = (*ifNode)(nil)
|
|
_ Node = (*unlessNode)(nil)
|
|
_ Node = (*entitledNode)(nil)
|
|
_ Node = (*switchNode)(nil)
|
|
_ Node = (*eachNode[any])(nil)
|
|
)
|
|
|
|
// renderNode renders a node while treating nil values as empty output.
|
|
func renderNode(n Node, ctx *Context) string {
|
|
if n == nil {
|
|
return ""
|
|
}
|
|
return n.Render(normaliseContext(ctx))
|
|
}
|
|
|
|
// renderNodeWithPath renders a node while preserving layout path prefixes for
|
|
// nested layouts that may be wrapped in conditional or switch nodes.
|
|
func renderNodeWithPath(n Node, ctx *Context, path string) string {
|
|
if n == nil {
|
|
return ""
|
|
}
|
|
ctx = normaliseContext(ctx)
|
|
|
|
switch t := n.(type) {
|
|
case *Layout:
|
|
if t == nil {
|
|
return ""
|
|
}
|
|
clone := *t
|
|
clone.path = path
|
|
return clone.Render(ctx)
|
|
case interface{ renderWithPath(*Context, string) string }:
|
|
return t.renderWithPath(ctx, path)
|
|
case *ifNode:
|
|
if t == nil || t.cond == nil || t.node == nil {
|
|
return ""
|
|
}
|
|
if t.cond(ctx) {
|
|
return renderNodeWithPath(t.node, ctx, path)
|
|
}
|
|
return ""
|
|
case *unlessNode:
|
|
if t == nil || t.cond == nil || t.node == nil {
|
|
return ""
|
|
}
|
|
if !t.cond(ctx) {
|
|
return renderNodeWithPath(t.node, ctx, path)
|
|
}
|
|
return ""
|
|
case *entitledNode:
|
|
if t == nil || t.node == nil {
|
|
return ""
|
|
}
|
|
if ctx == nil || ctx.Entitlements == nil || !ctx.Entitlements(t.feature) {
|
|
return ""
|
|
}
|
|
return renderNodeWithPath(t.node, ctx, path)
|
|
case *switchNode:
|
|
if t == nil || t.selector == nil || t.cases == nil {
|
|
return ""
|
|
}
|
|
key := t.selector(ctx)
|
|
if node, ok := t.cases[key]; ok {
|
|
return renderNodeWithPath(node, ctx, path)
|
|
}
|
|
return ""
|
|
default:
|
|
return n.Render(ctx)
|
|
}
|
|
}
|
|
|
|
// voidElements is the set of HTML elements that must not have a closing tag.
|
|
var voidElements = map[string]bool{
|
|
"area": true,
|
|
"base": true,
|
|
"br": true,
|
|
"col": true,
|
|
"embed": true,
|
|
"hr": true,
|
|
"img": true,
|
|
"input": true,
|
|
"link": true,
|
|
"meta": true,
|
|
"source": true,
|
|
"track": true,
|
|
"wbr": true,
|
|
}
|
|
|
|
// escapeAttr escapes a string for use in an HTML attribute value.
|
|
func escapeAttr(s string) string {
|
|
return html.EscapeString(s)
|
|
}
|
|
|
|
// --- rawNode ---
|
|
|
|
type rawNode struct {
|
|
content string
|
|
}
|
|
|
|
// node.go: Raw creates a node that renders without escaping.
|
|
// Example: Raw("<strong>trusted</strong>") preserves the HTML verbatim.
|
|
func Raw(content string) Node {
|
|
return &rawNode{content: content}
|
|
}
|
|
|
|
func (n *rawNode) Render(_ *Context) string {
|
|
if n == nil {
|
|
return ""
|
|
}
|
|
return n.content
|
|
}
|
|
|
|
// --- elNode ---
|
|
|
|
type elNode struct {
|
|
tag string
|
|
children []Node
|
|
attrs map[string]string
|
|
}
|
|
|
|
// node.go: El creates an HTML element node with children.
|
|
// Example: El("nav", Text("nav.label")) renders a semantic element with nested nodes.
|
|
func El(tag string, children ...Node) Node {
|
|
return &elNode{
|
|
tag: tag,
|
|
children: children,
|
|
attrs: make(map[string]string),
|
|
}
|
|
}
|
|
|
|
// node.go: Attr sets an attribute on an El node and returns the same node for chaining.
|
|
// Example: Attr(El("img"), "alt", "Logo") adds an escaped alt attribute.
|
|
//
|
|
// It recursively traverses wrappers like If, Unless, Entitled, Switch,
|
|
// and Each/EachSeq so the attribute lands on the rendered element.
|
|
func Attr(n Node, key, value string) Node {
|
|
switch t := n.(type) {
|
|
case *elNode:
|
|
t.attrs[key] = value
|
|
case *ifNode:
|
|
Attr(t.node, key, value)
|
|
case *unlessNode:
|
|
Attr(t.node, key, value)
|
|
case *entitledNode:
|
|
Attr(t.node, key, value)
|
|
case *switchNode:
|
|
for caseKey, caseNode := range t.cases {
|
|
t.cases[caseKey] = Attr(caseNode, key, value)
|
|
}
|
|
case interface{ setAttr(string, string) }:
|
|
t.setAttr(key, value)
|
|
}
|
|
return n
|
|
}
|
|
|
|
// node.go: AriaLabel sets the aria-label attribute on an element node.
|
|
// Example: AriaLabel(El("button"), "Open menu").
|
|
func AriaLabel(n Node, label string) Node {
|
|
return Attr(n, "aria-label", label)
|
|
}
|
|
|
|
// node.go: AriaDescribedBy sets the aria-describedby attribute on an element node.
|
|
// Example: AriaDescribedBy(El("input"), "help-text", "error-text").
|
|
// Multiple IDs are joined with spaces, matching the HTML attribute format.
|
|
func AriaDescribedBy(n Node, ids ...string) Node {
|
|
return Attr(n, "aria-describedby", strings.Join(ids, " "))
|
|
}
|
|
|
|
// node.go: AriaLabelledBy sets the aria-labelledby attribute on an element node.
|
|
// Example: AriaLabelledBy(El("section"), "section-title").
|
|
// Multiple IDs are joined with spaces, matching the HTML attribute format.
|
|
func AriaLabelledBy(n Node, ids ...string) Node {
|
|
return Attr(n, "aria-labelledby", strings.Join(ids, " "))
|
|
}
|
|
|
|
// node.go: AriaControls sets the aria-controls attribute on an element node.
|
|
// Example: AriaControls(El("button"), "menu-panel").
|
|
// Multiple IDs are joined with spaces, matching the HTML attribute format.
|
|
func AriaControls(n Node, ids ...string) Node {
|
|
return Attr(n, "aria-controls", strings.Join(ids, " "))
|
|
}
|
|
|
|
// node.go: AriaCurrent sets the aria-current attribute on an element node.
|
|
// Example: AriaCurrent(El("a"), "page").
|
|
// An empty value leaves the node unchanged so callers can opt out cleanly.
|
|
func AriaCurrent(n Node, current string) Node {
|
|
if current == "" {
|
|
return n
|
|
}
|
|
return Attr(n, "aria-current", current)
|
|
}
|
|
|
|
// node.go: AriaBusy sets the aria-busy attribute on an element node.
|
|
// Example: AriaBusy(El("section"), true).
|
|
func AriaBusy(n Node, busy bool) Node {
|
|
if busy {
|
|
return Attr(n, "aria-busy", "true")
|
|
}
|
|
return Attr(n, "aria-busy", "false")
|
|
}
|
|
|
|
// node.go: AriaLive sets the aria-live attribute on an element node.
|
|
// Example: AriaLive(El("div"), "polite").
|
|
// An empty value leaves the node unchanged so callers can opt out cleanly.
|
|
func AriaLive(n Node, live string) Node {
|
|
if live == "" {
|
|
return n
|
|
}
|
|
return Attr(n, "aria-live", live)
|
|
}
|
|
|
|
// node.go: Role sets the role attribute on an element node.
|
|
// Example: Role(El("aside"), "complementary").
|
|
func Role(n Node, role string) Node {
|
|
return Attr(n, "role", role)
|
|
}
|
|
|
|
// node.go: Lang sets the lang attribute on an element node.
|
|
// Example: Lang(El("html"), "en-GB").
|
|
func Lang(n Node, locale string) Node {
|
|
return Attr(n, "lang", locale)
|
|
}
|
|
|
|
// node.go: Dir sets the dir attribute on an element node.
|
|
// Example: Dir(El("p"), "rtl").
|
|
func Dir(n Node, direction string) Node {
|
|
return Attr(n, "dir", direction)
|
|
}
|
|
|
|
// node.go: Alt sets the alt attribute on an element node.
|
|
// Example: Alt(El("img"), "Product screenshot").
|
|
func Alt(n Node, text string) Node {
|
|
return Attr(n, "alt", text)
|
|
}
|
|
|
|
// node.go: AltText sets the alt attribute on an element node.
|
|
// Example: AltText(El("img"), "Product screenshot").
|
|
func AltText(n Node, text string) Node {
|
|
return Alt(n, text)
|
|
}
|
|
|
|
// node.go: Class sets the class attribute on an element node.
|
|
// Example: Class(El("div"), "card", "card--primary").
|
|
// Multiple class tokens are joined with spaces.
|
|
func Class(n Node, classes ...string) Node {
|
|
return Attr(n, "class", strings.Join(classes, " "))
|
|
}
|
|
|
|
// node.go: AriaHidden sets the aria-hidden attribute on an element node.
|
|
// Example: AriaHidden(El("svg"), true).
|
|
func AriaHidden(n Node, hidden bool) Node {
|
|
if hidden {
|
|
return Attr(n, "aria-hidden", "true")
|
|
}
|
|
return n
|
|
}
|
|
|
|
// node.go: AriaExpanded sets the aria-expanded attribute on an element node.
|
|
// Example: AriaExpanded(El("button"), true).
|
|
func AriaExpanded(n Node, expanded bool) Node {
|
|
if expanded {
|
|
return Attr(n, "aria-expanded", "true")
|
|
}
|
|
return Attr(n, "aria-expanded", "false")
|
|
}
|
|
|
|
// node.go: AriaDisabled sets the aria-disabled attribute on an element node.
|
|
// Example: AriaDisabled(El("button"), true).
|
|
func AriaDisabled(n Node, disabled bool) Node {
|
|
if disabled {
|
|
return Attr(n, "aria-disabled", "true")
|
|
}
|
|
return Attr(n, "aria-disabled", "false")
|
|
}
|
|
|
|
// node.go: AriaPressed sets the aria-pressed attribute on an element node.
|
|
// Example: AriaPressed(El("button"), true).
|
|
func AriaPressed(n Node, pressed bool) Node {
|
|
if pressed {
|
|
return Attr(n, "aria-pressed", "true")
|
|
}
|
|
return Attr(n, "aria-pressed", "false")
|
|
}
|
|
|
|
// node.go: AriaSelected sets the aria-selected attribute on an element node.
|
|
// Example: AriaSelected(El("option"), true).
|
|
func AriaSelected(n Node, selected bool) Node {
|
|
if selected {
|
|
return Attr(n, "aria-selected", "true")
|
|
}
|
|
return Attr(n, "aria-selected", "false")
|
|
}
|
|
|
|
// node.go: TabIndex sets the tabindex attribute on an element node.
|
|
// Example: TabIndex(El("button"), 0).
|
|
func TabIndex(n Node, index int) Node {
|
|
return Attr(n, "tabindex", strconv.Itoa(index))
|
|
}
|
|
|
|
// node.go: AutoFocus sets the autofocus attribute on an element node.
|
|
// Example: AutoFocus(El("input")).
|
|
func AutoFocus(n Node) Node {
|
|
return Attr(n, "autofocus", "autofocus")
|
|
}
|
|
|
|
func (n *elNode) Render(ctx *Context) string {
|
|
if n == nil {
|
|
return ""
|
|
}
|
|
|
|
return n.renderWithPath(normaliseContext(ctx), "")
|
|
}
|
|
|
|
func (n *elNode) renderWithPath(ctx *Context, path string) string {
|
|
if n == nil {
|
|
return ""
|
|
}
|
|
|
|
var b strings.Builder
|
|
|
|
b.WriteByte('<')
|
|
b.WriteString(escapeHTML(n.tag))
|
|
|
|
// Sort attribute keys for deterministic output.
|
|
keys := slices.Collect(maps.Keys(n.attrs))
|
|
slices.Sort(keys)
|
|
for _, key := range keys {
|
|
b.WriteByte(' ')
|
|
b.WriteString(escapeHTML(key))
|
|
b.WriteString(`="`)
|
|
b.WriteString(escapeAttr(n.attrs[key]))
|
|
b.WriteByte('"')
|
|
}
|
|
|
|
b.WriteByte('>')
|
|
|
|
if voidElements[n.tag] {
|
|
return b.String()
|
|
}
|
|
|
|
for i := range len(n.children) {
|
|
b.WriteString(renderNodeWithPath(n.children[i], ctx, path))
|
|
}
|
|
|
|
b.WriteString("</")
|
|
b.WriteString(escapeHTML(n.tag))
|
|
b.WriteByte('>')
|
|
|
|
return b.String()
|
|
}
|
|
|
|
// --- escapeHTML ---
|
|
|
|
// escapeHTML escapes a string for safe inclusion in HTML text content.
|
|
func escapeHTML(s string) string {
|
|
return html.EscapeString(s)
|
|
}
|
|
|
|
// --- textNode ---
|
|
|
|
type textNode struct {
|
|
key string
|
|
args []any
|
|
}
|
|
|
|
// node.go: Text creates a node that renders through the go-i18n grammar pipeline.
|
|
// Example: Text("page.title") renders translated text and escapes it for HTML.
|
|
// Output is HTML-escaped by default. Safe-by-default path.
|
|
func Text(key string, args ...any) Node {
|
|
return &textNode{key: key, args: args}
|
|
}
|
|
|
|
func (n *textNode) Render(ctx *Context) string {
|
|
if n == nil {
|
|
return ""
|
|
}
|
|
ctx = normaliseContext(ctx)
|
|
|
|
var text string
|
|
if ctx.service != nil {
|
|
text = ctx.service.T(n.key, n.args...)
|
|
} else {
|
|
text = i18n.T(n.key, n.args...)
|
|
}
|
|
return escapeHTML(text)
|
|
}
|
|
|
|
// --- ifNode ---
|
|
|
|
type ifNode struct {
|
|
cond func(*Context) bool
|
|
node Node
|
|
}
|
|
|
|
// node.go: If renders child only when condition is true.
|
|
// Example: If(func(*Context) bool { return true }, Raw("shown")).
|
|
func If(cond func(*Context) bool, node Node) Node {
|
|
return &ifNode{cond: cond, node: node}
|
|
}
|
|
|
|
func (n *ifNode) Render(ctx *Context) string {
|
|
if n == nil || n.cond == nil || n.node == nil {
|
|
return ""
|
|
}
|
|
ctx = normaliseContext(ctx)
|
|
|
|
if n.cond(ctx) {
|
|
return renderNodeWithPath(n.node, ctx, "")
|
|
}
|
|
return ""
|
|
}
|
|
|
|
// --- unlessNode ---
|
|
|
|
type unlessNode struct {
|
|
cond func(*Context) bool
|
|
node Node
|
|
}
|
|
|
|
// node.go: Unless renders child only when condition is false.
|
|
// Example: Unless(func(*Context) bool { return true }, Raw("hidden")).
|
|
func Unless(cond func(*Context) bool, node Node) Node {
|
|
return &unlessNode{cond: cond, node: node}
|
|
}
|
|
|
|
func (n *unlessNode) Render(ctx *Context) string {
|
|
if n == nil || n.cond == nil || n.node == nil {
|
|
return ""
|
|
}
|
|
ctx = normaliseContext(ctx)
|
|
|
|
if !n.cond(ctx) {
|
|
return renderNodeWithPath(n.node, ctx, "")
|
|
}
|
|
return ""
|
|
}
|
|
|
|
// --- entitledNode ---
|
|
|
|
type entitledNode struct {
|
|
feature string
|
|
node Node
|
|
}
|
|
|
|
// node.go: Entitled renders child only when entitlement is granted.
|
|
// Example: Entitled("premium", Raw("paid feature")).
|
|
// Content is absent, not hidden. If no entitlement function is set on the
|
|
// context, access is denied by default.
|
|
func Entitled(feature string, node Node) Node {
|
|
return &entitledNode{feature: feature, node: node}
|
|
}
|
|
|
|
func (n *entitledNode) Render(ctx *Context) string {
|
|
if n == nil || n.node == nil {
|
|
return ""
|
|
}
|
|
ctx = normaliseContext(ctx)
|
|
|
|
if ctx.Entitlements == nil || !ctx.Entitlements(n.feature) {
|
|
return ""
|
|
}
|
|
return renderNodeWithPath(n.node, ctx, "")
|
|
}
|
|
|
|
// --- switchNode ---
|
|
|
|
type switchNode struct {
|
|
selector func(*Context) string
|
|
cases map[string]Node
|
|
}
|
|
|
|
// node.go: Switch renders based on runtime selector value.
|
|
// Example: Switch(selector, map[string]Node{"desktop": Raw("wide")}).
|
|
func Switch(selector func(*Context) string, cases map[string]Node) Node {
|
|
return &switchNode{selector: selector, cases: cases}
|
|
}
|
|
|
|
func (n *switchNode) Render(ctx *Context) string {
|
|
if n == nil || n.selector == nil || n.cases == nil {
|
|
return ""
|
|
}
|
|
ctx = normaliseContext(ctx)
|
|
|
|
key := n.selector(ctx)
|
|
if node, ok := n.cases[key]; ok {
|
|
return renderNodeWithPath(node, ctx, "")
|
|
}
|
|
return ""
|
|
}
|
|
|
|
// --- eachNode ---
|
|
|
|
type eachNode[T any] struct {
|
|
items iter.Seq[T]
|
|
fn func(T) Node
|
|
}
|
|
|
|
// node.go: Each iterates items and renders each via fn.
|
|
// Example: Each(items, func(item Item) Node { return El("li", Text(item.Name)) }).
|
|
func Each[T any](items []T, fn func(T) Node) Node {
|
|
return EachSeq(slices.Values(items), fn)
|
|
}
|
|
|
|
// node.go: EachSeq iterates an iter.Seq and renders each via fn.
|
|
// Example: EachSeq(slices.Values(items), renderItem).
|
|
func EachSeq[T any](items iter.Seq[T], fn func(T) Node) Node {
|
|
return &eachNode[T]{items: items, fn: fn}
|
|
}
|
|
|
|
func (n *eachNode[T]) Render(ctx *Context) string {
|
|
if n == nil || n.items == nil || n.fn == nil {
|
|
return ""
|
|
}
|
|
ctx = normaliseContext(ctx)
|
|
|
|
var b strings.Builder
|
|
for item := range n.items {
|
|
b.WriteString(renderNodeWithPath(n.fn(item), ctx, ""))
|
|
}
|
|
return b.String()
|
|
}
|
|
|
|
func (n *eachNode[T]) renderWithPath(ctx *Context, path string) string {
|
|
if n == nil || n.items == nil || n.fn == nil {
|
|
return ""
|
|
}
|
|
|
|
var b strings.Builder
|
|
for item := range n.items {
|
|
b.WriteString(renderNodeWithPath(n.fn(item), ctx, path))
|
|
}
|
|
return b.String()
|
|
}
|
|
|
|
func (n *eachNode[T]) setAttr(key, value string) {
|
|
if n == nil || n.fn == nil {
|
|
return
|
|
}
|
|
|
|
prev := n.fn
|
|
n.fn = func(item T) Node {
|
|
return Attr(prev(item), key, value)
|
|
}
|
|
}
|