feat(html): add id and for helpers
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 20:04:27 +00:00
parent 1134683e1b
commit c6bca226a9
3 changed files with 34 additions and 0 deletions

View file

@ -56,6 +56,8 @@ Accessibility-oriented helpers are also provided for common attribute patterns:
- `AriaRequired(node, required)`
- `TabIndex(node, index)`
- `AutoFocus(node)`
- `ID(node, id)`
- `For(node, target)`
### Safety Guarantees

12
node.go
View file

@ -395,6 +395,18 @@ func AutoFocus(n Node) Node {
return Attr(n, "autofocus", "autofocus")
}
// node.go: ID sets the id attribute on an element node.
// Example: ID(El("section"), "main-content").
func ID(n Node, id string) Node {
return Attr(n, "id", id)
}
// node.go: For sets the for attribute on an element node.
// Example: For(El("label"), "email-input").
func For(n Node, target string) Node {
return Attr(n, "for", target)
}
func joinNonEmpty(parts ...string) string {
if len(parts) == 0 {
return ""

View file

@ -492,6 +492,26 @@ func TestClassHelper_IgnoresWhitespaceClasses(t *testing.T) {
}
}
func TestIDHelper(t *testing.T) {
ctx := NewContext()
node := ID(El("section", Raw("content")), "main-content")
got := node.Render(ctx)
want := `<section id="main-content">content</section>`
if got != want {
t.Errorf("ID() = %q, want %q", got, want)
}
}
func TestForHelper(t *testing.T) {
ctx := NewContext()
node := For(El("label", Raw("Email")), "email-input")
got := node.Render(ctx)
want := `<label for="email-input">Email</label>`
if got != want {
t.Errorf("For() = %q, want %q", got, want)
}
}
func TestAriaHiddenHelper(t *testing.T) {
ctx := NewContext()