Skip to content

Commit

Permalink
Merge pull request #87 from chasefleming/chasefleming/86
Browse files Browse the repository at this point in the history
Create `Raw` function for Passing HTML
  • Loading branch information
chasefleming authored Nov 21, 2023
2 parents c657e09 + c16877c commit c3c0432
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 1 deletion.
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())
}

0 comments on commit c3c0432

Please sign in to comment.