feat(html): add aria-description 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 18:51:28 +00:00
parent 6f65fc903c
commit 7b95c1fc74
3 changed files with 28 additions and 0 deletions

View file

@ -41,6 +41,7 @@ Accessibility-oriented helpers are also provided for common attribute patterns:
- `AriaCurrent(node, current)`
- `AriaBusy(node, busy)`
- `AriaLive(node, live)`
- `AriaDescription(node, description)`
- `Role(node, role)`
- `Lang(node, locale)`
- `Dir(node, direction)`

10
node.go
View file

@ -233,6 +233,16 @@ func AriaLive(n Node, live string) Node {
return Attr(n, "aria-live", live)
}
// node.go: AriaDescription sets the aria-description attribute on an element node.
// Example: AriaDescription(El("button"), "Opens the navigation menu").
// An empty value leaves the node unchanged so callers can opt out cleanly.
func AriaDescription(n Node, description string) Node {
if description == "" {
return n
}
return Attr(n, "aria-description", description)
}
// node.go: Role sets the role attribute on an element node.
// Example: Role(El("aside"), "complementary").
func Role(n Node, role string) Node {

View file

@ -337,6 +337,23 @@ func TestAriaLiveHelper(t *testing.T) {
}
}
func TestAriaDescriptionHelper(t *testing.T) {
ctx := NewContext()
described := AriaDescription(El("button", Raw("menu")), "Opens the navigation menu")
gotDescribed := described.Render(ctx)
wantDescribed := `<button aria-description="Opens the navigation menu">menu</button>`
if gotDescribed != wantDescribed {
t.Errorf("AriaDescription() = %q, want %q", gotDescribed, wantDescribed)
}
silent := AriaDescription(El("button", Raw("menu")), "")
gotSilent := silent.Render(ctx)
if strings.Contains(gotSilent, `aria-description=`) {
t.Errorf("AriaDescription(\"\") = %q, want no aria-description attribute", gotSilent)
}
}
func TestRoleHelper(t *testing.T) {
ctx := NewContext()
node := Role(El("button", Raw("menu")), "navigation")