feat(html): add id and for helpers
Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
parent
1134683e1b
commit
c6bca226a9
3 changed files with 34 additions and 0 deletions
|
|
@ -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
12
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 ""
|
||||
|
|
|
|||
20
node_test.go
20
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 := `<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()
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue