From 6e534fbf21d291c799022773c2d598d4f902f286 Mon Sep 17 00:00:00 2001 From: Chase Fleming <1666730+chasefleming@users.noreply.github.com> Date: Tue, 21 Nov 2023 07:14:40 -0800 Subject: [PATCH 1/3] Create Raw function for passing html --- elem.go | 10 ++++++++++ elements.go | 7 +++++++ elements_test.go | 11 +++++++++++ 3 files changed, 28 insertions(+) diff --git a/elem.go b/elem.go index 2931bbc..edd8e14 100644 --- a/elem.go +++ b/elem.go @@ -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) { diff --git a/elements.go b/elements.go index be0e2e1..b4db73a 100644 --- a/elements.go +++ b/elements.go @@ -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) +} diff --git a/elements_test.go b/elements_test.go index 1cd26cb..1ea166c 100644 --- a/elements_test.go +++ b/elements_test.go @@ -474,6 +474,7 @@ func TestTable(t *testing.T) { } // ========== Embedded Content ========== + func TestEmbedLink(t *testing.T) { expected := `` el := IFrame(attrs.Props{attrs.Src: "https://www.youtube.com/embed/446E-r0rXHI"}) @@ -505,3 +506,13 @@ func TestVideoWithSourceElementsAndFallbackText(t *testing.T) { ) assert.Equal(t, expected, el.Render()) } + +// ========== Other ========== + +func TestRaw(t *testing.T) { + rawHTML := `

Test paragraph

` + el := Raw(rawHTML) + expected := rawHTML + + assert.Equal(t, expected, el.Render()) +} From 4ee51fcd4c99641419a1fddb7528b7b1e1ba7868 Mon Sep 17 00:00:00 2001 From: Chase Fleming <1666730+chasefleming@users.noreply.github.com> Date: Tue, 21 Nov 2023 08:36:18 -0800 Subject: [PATCH 2/3] Add `Raw` to README --- README.md | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 4ecc0d2..e5d107d 100644 --- a/README.md +++ b/README.md @@ -148,7 +148,23 @@ 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 := `

Custom HTML content

` +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:

Welcome to Elem-Go

Custom HTML content

More content here...

+``` + +### HTML Comments Apart from standard elements, `elem-go` also allows you to insert HTML comments using the `Comment` function: From c16877c4041af01e1760b79bf85a45a55eeee411 Mon Sep 17 00:00:00 2001 From: Chase Fleming <1666730+chasefleming@users.noreply.github.com> Date: Tue, 21 Nov 2023 08:47:56 -0800 Subject: [PATCH 3/3] Add note --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index e5d107d..0f53b3d 100644 --- a/README.md +++ b/README.md @@ -163,6 +163,7 @@ content := elem.Div(nil, htmlOutput := content.Render() // Output:

Welcome to Elem-Go

Custom HTML content

More content here...

``` +> **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