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

Create Raw function for Passing HTML #87

Merged
merged 3 commits into from
Nov 21, 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
19 changes: 18 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,24 @@ In this example, if `isAdmin` is `true`, the `Admin Panel` link is rendered. Oth
- **Script-supporting Elements**: `Script`, `Noscript`
- **Inline Semantic**: `A`, `Strong`, `Em`, `Code`, `I`

### Additional Utility: HTML Comments
### Raw HTML Insertion

The `Raw` function allows for the direct inclusion of raw HTML content within your document structure. This function can be used to insert HTML strings, which will be rendered as part of the final HTML output.

```go
rawHTML := `<div class="custom-html"><p>Custom HTML content</p></div>`
content := elem.Div(nil,
elem.H1(nil, elem.Text("Welcome to Elem-Go")),
elem.Raw(rawHTML), // Inserting the raw HTML
elem.P(nil, elem.Text("More content here...")),
)

htmlOutput := content.Render()
// Output: <div><h1>Welcome to Elem-Go</h1><div class="custom-html"><p>Custom HTML content</p></div><p>More content here...</p></div>
```
> **NOTE**: If you are passing HTML from an untrusted source, make sure to sanitize it to prevent potential security risks such as Cross-Site Scripting (XSS) attacks.

### HTML Comments

Apart from standard elements, `elem-go` also allows you to insert HTML comments using the `Comment` function:

Expand Down
10 changes: 10 additions & 0 deletions elem.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,16 @@ func (t TextNode) Render() string {
return string(t)
}

type RawNode string

func (r RawNode) RenderTo(builder *strings.Builder) {
builder.WriteString(string(r))
}

func (r RawNode) Render() string {
return string(r)
}

type CommentNode string

func (c CommentNode) RenderTo(builder *strings.Builder) {
Expand Down
7 changes: 7 additions & 0 deletions elements.go
Original file line number Diff line number Diff line change
Expand Up @@ -396,3 +396,10 @@ func Video(attrs attrs.Props, children ...Node) *Element {
func Source(attrs attrs.Props, children ...Node) *Element {
return NewElement("source", attrs, children...)
}

// ========== Other ==========

// Raw takes html content and returns a RawNode.
func Raw(html string) RawNode {
return RawNode(html)
}
11 changes: 11 additions & 0 deletions elements_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,7 @@ func TestTable(t *testing.T) {
}

// ========== Embedded Content ==========

func TestEmbedLink(t *testing.T) {
expected := `<iframe src="https://www.youtube.com/embed/446E-r0rXHI"></iframe>`
el := IFrame(attrs.Props{attrs.Src: "https://www.youtube.com/embed/446E-r0rXHI"})
Expand Down Expand Up @@ -505,3 +506,13 @@ func TestVideoWithSourceElementsAndFallbackText(t *testing.T) {
)
assert.Equal(t, expected, el.Render())
}

// ========== Other ==========

func TestRaw(t *testing.T) {
rawHTML := `<div class="test"><p>Test paragraph</p></div>`
el := Raw(rawHTML)
expected := rawHTML

assert.Equal(t, expected, el.Render())
}