From c6bca226a9df05364d0b70ac84bbc437bf3ce486 Mon Sep 17 00:00:00 2001 From: Virgil Date: Fri, 3 Apr 2026 20:04:27 +0000 Subject: [PATCH] feat(html): add id and for helpers Co-Authored-By: Virgil --- docs/architecture.md | 2 ++ node.go | 12 ++++++++++++ node_test.go | 20 ++++++++++++++++++++ 3 files changed, 34 insertions(+) diff --git a/docs/architecture.md b/docs/architecture.md index da2617f..a176a39 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -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 diff --git a/node.go b/node.go index 6a46961..98862ea 100644 --- a/node.go +++ b/node.go @@ -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 "" diff --git a/node_test.go b/node_test.go index 812b405..efa28dd 100644 --- a/node_test.go +++ b/node_test.go @@ -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 := `
content
` + 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 := `` + if got != want { + t.Errorf("For() = %q, want %q", got, want) + } +} + func TestAriaHiddenHelper(t *testing.T) { ctx := NewContext()