From f7843ae1806984ed8ac2aaca5e3a7ecf10e404c9 Mon Sep 17 00:00:00 2001 From: Virgil Date: Fri, 3 Apr 2026 17:09:10 +0000 Subject: [PATCH] feat(html): add autofocus helper Co-Authored-By: Virgil --- docs/architecture.md | 1 + node.go | 5 +++++ node_test.go | 9 +++++++++ 3 files changed, 15 insertions(+) diff --git a/docs/architecture.md b/docs/architecture.md index 05472c7..5e92c37 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -39,6 +39,7 @@ Accessibility-oriented helpers are also provided for common attribute patterns: - `Alt(node, text)` - `AriaHidden(node, hidden)` - `TabIndex(node, index)` +- `AutoFocus(node)` ### Safety Guarantees diff --git a/node.go b/node.go index b90bcaf..e193f42 100644 --- a/node.go +++ b/node.go @@ -192,6 +192,11 @@ func TabIndex(n Node, index int) Node { return Attr(n, "tabindex", strconv.Itoa(index)) } +// AutoFocus sets the autofocus attribute on an element node. +func AutoFocus(n Node) Node { + return Attr(n, "autofocus", "autofocus") +} + func (n *elNode) Render(ctx *Context) string { if n == nil { return "" diff --git a/node_test.go b/node_test.go index 4b73b3e..f82cb9c 100644 --- a/node_test.go +++ b/node_test.go @@ -286,6 +286,15 @@ func TestTabIndexHelper(t *testing.T) { } } +func TestAutoFocusHelper(t *testing.T) { + ctx := NewContext() + node := AutoFocus(El("input")) + got := node.Render(ctx) + if !strings.Contains(got, `autofocus="autofocus"`) { + t.Errorf("AutoFocus() = %q, want autofocus=\"autofocus\"", got) + } +} + func TestElNode_MultipleAttrs(t *testing.T) { ctx := NewContext() node := Attr(Attr(El("a", Raw("link")), "href", "/home"), "class", "nav")