Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added support for HTML comments #68 #71

Merged
merged 1 commit into from
Nov 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions elem.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,20 @@ func (t TextNode) Render() string {
return string(t)
}

type CommentNode string

func (c CommentNode) RenderTo(builder *strings.Builder) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great implementation of the CommentNode! One small suggestion for optimization:

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this performance tip. Fixed.

builder.WriteString("<!-- ")
builder.WriteString(string(c))
builder.WriteString(" -->")
}

func (c CommentNode) Render() string {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then we can follow the same pattern as the Element.Render:

func (c CommentNode) Render() string {
	var builder strings.Builder
	c.RenderTo(&builder)
	return builder.String()
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

var builder strings.Builder
c.RenderTo(&builder)
return builder.String()
}

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