Skip to content

Commit

Permalink
Delete Fragment
Browse files Browse the repository at this point in the history
  • Loading branch information
markuswustenberg committed Sep 19, 2024
1 parent b3c2b84 commit a8dc008
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 59 deletions.
23 changes: 0 additions & 23 deletions gomponents.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,29 +269,6 @@ func Group(children []Node) Node {
return group{children: children}
}

type fragment struct {
children []Node
}

// String satisfies [fmt.Stringer].
func (f fragment) String() string {
var b strings.Builder
_ = f.Render(&b)
return b.String()
}

// Render satisfies [Node].
func (f fragment) Render(w io.Writer) error {
return render(w, nil, f.children...)
}

// Fragment is multiple nodes concatenated. It's a lot like [Group], except it's variadic.
// It's useful in situations where you want to return an HTML fragment without a parent element.
// Note that if any of the direct children are [AttributeType], they will be ignored, to not produce invalid HTML.
func Fragment(children ...Node) Node {
return fragment{children}
}

// If condition is true, return the given [Node]. Otherwise, return nil.
// This helper function is good for inlining elements conditionally.
// If it's important that the given [Node] is only evaluated if condition is true
Expand Down
40 changes: 4 additions & 36 deletions gomponents_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,42 +261,10 @@ func TestGroup(t *testing.T) {
})
}

func TestFragment(t *testing.T) {
t.Run("groups multiple nodes into one", func(t *testing.T) {
n := g.Fragment(g.El("div"), g.El("span"))
assert.Equal(t, "<div></div><span></span>", n)
})

t.Run("ignores attributes at the first level", func(t *testing.T) {
n := g.Fragment(g.Attr("class", "hat"), g.El("div"), g.El("span"))
assert.Equal(t, "<div></div><span></span>", n)
})

t.Run("does not ignore attributes at the second level", func(t *testing.T) {
n := g.Fragment(g.El("div", g.Attr("class", "hat")), g.El("span"))
assert.Equal(t, `<div class="hat"></div><span></span>`, n)
})

t.Run("can render a fragment child node", func(t *testing.T) {
n := g.El("div", g.Fragment(g.El("div"), g.El("span")))
assert.Equal(t, "<div><div></div><span></span></div>", n)
})

t.Run("implements fmt.Stringer", func(t *testing.T) {
n := g.Fragment(g.El("div"), g.El("span"))
if n, ok := n.(fmt.Stringer); !ok || n.String() != "<div></div><span></span>" {
t.FailNow()
}
})
}

func ExampleFragment() {
f := g.Fragment(
g.El("div"),
g.El("span"),
)

_ = f.Render(os.Stdout)
func ExampleGroup() {
children := []g.Node{g.El("div"), g.El("span")}
e := g.Group(children)
_ = e.Render(os.Stdout)
// Output: <div></div><span></span>
}

Expand Down

0 comments on commit a8dc008

Please sign in to comment.