Skip to content

Commit

Permalink
Add popover attributes (#236)
Browse files Browse the repository at this point in the history
This PR adds popover attributes as detailed here:
https://developer.mozilla.org/en-US/docs/Web/API/Popover_API/Using

---------

Co-authored-by: Markus Wüstenberg <[email protected]>
  • Loading branch information
knpwrs and markuswustenberg authored Oct 30, 2024
1 parent 2e44d49 commit 900ef87
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
12 changes: 12 additions & 0 deletions html/attributes.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,18 @@ func Placeholder(v string) g.Node {
return g.Attr("placeholder", v)
}

func Popover(value ...string) g.Node {
return g.Attr("popover", value...)
}

func PopoverTarget(v string) g.Node {
return g.Attr("popovertarget", v)
}

func PopoverTargetAction(v string) g.Node {
return g.Attr("popovertargetaction", v)
}

func Poster(v string) g.Node {
return g.Attr("poster", v)
}
Expand Down
33 changes: 33 additions & 0 deletions html/attributes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ func TestSimpleAttributes(t *testing.T) {
{Name: "name", Func: Name},
{Name: "pattern", Func: Pattern},
{Name: "placeholder", Func: Placeholder},
{Name: "popovertarget", Func: PopoverTarget},
{Name: "popovertargetaction", Func: PopoverTargetAction},
{Name: "poster", Func: Poster},
{Name: "preload", Func: Preload},
{Name: "rel", Func: Rel},
Expand Down Expand Up @@ -107,6 +109,37 @@ func TestSimpleAttributes(t *testing.T) {
}
}

func TestVariadicAttributes(t *testing.T) {
tests := []struct {
Name string
Func func(...string) g.Node
}{
{Name: "popover", Func: Popover},
}

for _, test := range tests {
t.Run(test.Name + "(no args)", func(t *testing.T) {
n := g.El("div", test.Func())
assert.Equal(t, fmt.Sprintf(`<div %v></div>`, test.Name), n)
})

t.Run(test.Name +"(one arg)", func(t *testing.T) {
n := g.El("div", test.Func("hat"))
assert.Equal(t, fmt.Sprintf(`<div %v="hat"></div>`, test.Name), n)
})

t.Run(test.Name + "(two args panics)", func(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Errorf("expected a panic")
}
}()
n := g.El("div", test.Func("hat", "party"))
assert.Equal(t, "unreachable", n)
})
}
}

func TestAria(t *testing.T) {
t.Run("returns an attribute which name is prefixed with aria-", func(t *testing.T) {
n := Aria("selected", "true")
Expand Down

0 comments on commit 900ef87

Please sign in to comment.