Skip to content

Commit c3c0432

Browse files
authored
Merge pull request #87 from chasefleming/chasefleming/86
Create `Raw` function for Passing HTML
2 parents c657e09 + c16877c commit c3c0432

File tree

4 files changed

+46
-1
lines changed

4 files changed

+46
-1
lines changed

README.md

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,24 @@ In this example, if `isAdmin` is `true`, the `Admin Panel` link is rendered. Oth
148148
- **Script-supporting Elements**: `Script`, `Noscript`
149149
- **Inline Semantic**: `A`, `Strong`, `Em`, `Code`, `I`
150150

151-
### Additional Utility: HTML Comments
151+
### Raw HTML Insertion
152+
153+
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.
154+
155+
```go
156+
rawHTML := `<div class="custom-html"><p>Custom HTML content</p></div>`
157+
content := elem.Div(nil,
158+
elem.H1(nil, elem.Text("Welcome to Elem-Go")),
159+
elem.Raw(rawHTML), // Inserting the raw HTML
160+
elem.P(nil, elem.Text("More content here...")),
161+
)
162+
163+
htmlOutput := content.Render()
164+
// Output: <div><h1>Welcome to Elem-Go</h1><div class="custom-html"><p>Custom HTML content</p></div><p>More content here...</p></div>
165+
```
166+
> **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.
167+
168+
### HTML Comments
152169

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

elem.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,16 @@ func (t TextNode) Render() string {
6868
return string(t)
6969
}
7070

71+
type RawNode string
72+
73+
func (r RawNode) RenderTo(builder *strings.Builder) {
74+
builder.WriteString(string(r))
75+
}
76+
77+
func (r RawNode) Render() string {
78+
return string(r)
79+
}
80+
7181
type CommentNode string
7282

7383
func (c CommentNode) RenderTo(builder *strings.Builder) {

elements.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,3 +396,10 @@ func Video(attrs attrs.Props, children ...Node) *Element {
396396
func Source(attrs attrs.Props, children ...Node) *Element {
397397
return NewElement("source", attrs, children...)
398398
}
399+
400+
// ========== Other ==========
401+
402+
// Raw takes html content and returns a RawNode.
403+
func Raw(html string) RawNode {
404+
return RawNode(html)
405+
}

elements_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,7 @@ func TestTable(t *testing.T) {
474474
}
475475

476476
// ========== Embedded Content ==========
477+
477478
func TestEmbedLink(t *testing.T) {
478479
expected := `<iframe src="https://www.youtube.com/embed/446E-r0rXHI"></iframe>`
479480
el := IFrame(attrs.Props{attrs.Src: "https://www.youtube.com/embed/446E-r0rXHI"})
@@ -505,3 +506,13 @@ func TestVideoWithSourceElementsAndFallbackText(t *testing.T) {
505506
)
506507
assert.Equal(t, expected, el.Render())
507508
}
509+
510+
// ========== Other ==========
511+
512+
func TestRaw(t *testing.T) {
513+
rawHTML := `<div class="test"><p>Test paragraph</p></div>`
514+
el := Raw(rawHTML)
515+
expected := rawHTML
516+
517+
assert.Equal(t, expected, el.Render())
518+
}

0 commit comments

Comments
 (0)