Skip to content

Commit

Permalink
added support for HTML comments #68
Browse files Browse the repository at this point in the history
  • Loading branch information
whisk committed Nov 8, 2023
1 parent c0db64d commit 1973f98
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
10 changes: 10 additions & 0 deletions elem.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,16 @@ func (t TextNode) Render() string {
return string(t)
}

type CommentNode string

func (c CommentNode) RenderTo(builder *strings.Builder) {
builder.WriteString("<!-- " + string(c) + " -->")
}

func (c CommentNode) Render() string {
return string("<!-- " + string(c) + " -->")
}

type Element struct {
Tag string
Attrs attrs.Props
Expand Down
4 changes: 4 additions & 0 deletions elements.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
16 changes: 15 additions & 1 deletion elements_test.go
Original file line number Diff line number Diff line change
@@ -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"
)
Expand Down Expand Up @@ -123,6 +124,19 @@ func TestStrong(t *testing.T) {
assert.Equal(t, expected, el.Render())
}

// ========== Comments ==========
func TestComment(t *testing.T) {
expected := `<!-- this is a comment -->`
actual := Comment("this is a comment").Render()
assert.Equal(t, expected, actual)
}

func TestCommentInElement(t *testing.T) {
expected := `<div>not a comment<!-- this is a comment --></div>`
actual := Div(nil, Text("not a comment"), Comment("this is a comment")).Render()
assert.Equal(t, expected, actual)
}

// ========== Lists ==========

func TestLi(t *testing.T) {
Expand Down

0 comments on commit 1973f98

Please sign in to comment.