feat(html): add class 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:21:16 +00:00
parent a925142e4e
commit a5e02f6472
3 changed files with 18 additions and 0 deletions

View file

@ -42,6 +42,7 @@ Accessibility-oriented helpers are also provided for common attribute patterns:
- `Lang(node, locale)`
- `Dir(node, direction)`
- `Alt(node, text)`
- `Class(node, classes...)`
- `AriaHidden(node, hidden)`
- `AriaExpanded(node, expanded)`
- `TabIndex(node, index)`

View file

@ -228,6 +228,13 @@ func Alt(n Node, text string) Node {
return Attr(n, "alt", text)
}
// node.go: Class sets the class attribute on an element node.
// Example: Class(El("div"), "card", "card--primary").
// Multiple class tokens are joined with spaces.
func Class(n Node, classes ...string) Node {
return Attr(n, "class", strings.Join(classes, " "))
}
// node.go: AriaHidden sets the aria-hidden attribute on an element node.
// Example: AriaHidden(El("svg"), true).
func AriaHidden(n Node, hidden bool) Node {

View file

@ -324,6 +324,16 @@ func TestAltHelper(t *testing.T) {
}
}
func TestClassHelper(t *testing.T) {
ctx := NewContext()
node := Class(El("div", Raw("content")), "card", "card--primary")
got := node.Render(ctx)
want := `<div class="card card--primary">content</div>`
if got != want {
t.Errorf("Class() = %q, want %q", got, want)
}
}
func TestAriaHiddenHelper(t *testing.T) {
ctx := NewContext()