From 4c109f9f1d4db4733d267ece171a533521029bdb Mon Sep 17 00:00:00 2001 From: Markus Wüstenberg Date: Thu, 24 Sep 2020 13:19:52 +0200 Subject: Introduce Placer interface (#18) When implemented, the `Place` method of the `Placer` interface tells `Render` in `El` where to put a Node. This is relevant for helpers that want to be rendered like attributes, inside the parent element. Fixes the bug where `attr.Classes` was rendered outside the element.--- attr/attributes.go | 4 ++++ attr/attributes_test.go | 6 ++++++ 2 files changed, 10 insertions(+) (limited to 'attr') diff --git a/attr/attributes.go b/attr/attributes.go index 56fcd74..415e6f1 100644 --- a/attr/attributes.go +++ b/attr/attributes.go @@ -35,6 +35,10 @@ func (c Classes) Render() string { return g.Attr("class", strings.Join(included, " ")).Render() } +func (c Classes) Place() g.Placement { + return g.Inside +} + // String satisfies fmt.Stringer. func (c Classes) String() string { return c.Render() diff --git a/attr/attributes_test.go b/attr/attributes_test.go index 9c7ece7..4da28e3 100644 --- a/attr/attributes_test.go +++ b/attr/attributes_test.go @@ -3,6 +3,7 @@ package attr_test import ( "testing" + g "github.com/maragudk/gomponents" "github.com/maragudk/gomponents/assert" "github.com/maragudk/gomponents/attr" ) @@ -29,6 +30,11 @@ func TestClasses(t *testing.T) { }) }) + t.Run("renders as attribute in an element", func(t *testing.T) { + e := g.El("div", attr.Classes{"hat": true}) + assert.Equal(t, `
`, e) + }) + t.Run("also works with fmt", func(t *testing.T) { a := attr.Classes{"hat": true} if a.String() != ` class="hat"` { -- cgit 1.4.1