feat(html): add autofocus helper
Some checks are pending
Security Scan / security (push) Waiting to run
Test / test (push) Waiting to run

Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
Virgil 2026-04-03 17:09:10 +00:00
parent 1d71ac4676
commit f7843ae180
3 changed files with 15 additions and 0 deletions

View file

@ -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

View file

@ -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 ""

View file

@ -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")