diff --git a/elem.go b/elem.go index e4fb4d8..1d3c70c 100644 --- a/elem.go +++ b/elem.go @@ -62,6 +62,20 @@ func (t TextNode) Render() string { return string(t) } +type CommentNode string + +func (c CommentNode) RenderTo(builder *strings.Builder) { + builder.WriteString("") +} + +func (c CommentNode) Render() string { + var builder strings.Builder + c.RenderTo(&builder) + return builder.String() +} + type Element struct { Tag string Attrs attrs.Props diff --git a/elements.go b/elements.go index d8c00a6..57b37b0 100644 --- a/elements.go +++ b/elements.go @@ -88,6 +88,10 @@ func Text(content string) TextNode { return TextNode(content) } +func Comment(comment string) CommentNode { + return CommentNode(comment) +} + // ========== Lists ========== func Li(attrs attrs.Props, children ...Node) *Element { diff --git a/elements_test.go b/elements_test.go index b68db81..275f9a8 100644 --- a/elements_test.go +++ b/elements_test.go @@ -1,9 +1,10 @@ package elem import ( - "github.com/chasefleming/elem-go/styles" "testing" + "github.com/chasefleming/elem-go/styles" + "github.com/chasefleming/elem-go/attrs" "github.com/stretchr/testify/assert" ) @@ -123,6 +124,19 @@ func TestStrong(t *testing.T) { assert.Equal(t, expected, el.Render()) } +// ========== Comments ========== +func TestComment(t *testing.T) { + expected := `` + actual := Comment("this is a comment").Render() + assert.Equal(t, expected, actual) +} + +func TestCommentInElement(t *testing.T) { + expected := `
not a comment
` + actual := Div(nil, Text("not a comment"), Comment("this is a comment")).Render() + assert.Equal(t, expected, actual) +} + // ========== Lists ========== func TestLi(t *testing.T) {