added content escaping for text nodes#158
Merged
Merged
Conversation
chasefleming
requested changes
Jul 15, 2025
|
|
||
| // EscapeNodeContents escapes HTML5 special characters in a string to ensure safe rendering as a text node | ||
| func EscapeNodeContents(s string) string { | ||
| s = strings.ReplaceAll(s, "&", "&") |
There was a problem hiding this comment.
Pull Request Overview
This PR ensures that Text() nodes escape HTML5 special characters by introducing a new escaping utility and updating rendering methods, while preserving Raw() for unescaped content.
- Added
EscapeNodeContentsinutils.goto replace&,<, and>with their HTML entities. - Updated all
TextNoderendering methods to callEscapeNodeContents. - Added unit tests in
elements_test.goto verify the escaping behavior for various characters.
Reviewed Changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| utils.go | New EscapeNodeContents function and strings import |
| elements.go | Updated Text doc comment; render methods use escaping |
| elements_test.go | New tests for basic and HTML-escaped Text rendering |
Comments suppressed due to low confidence (1)
elements_test.go:692
- Add a test case to verify that '&' characters are properly escaped to '&' in Text rendering, for example:
func TestTextWithAmpersandEscaping(t *testing.T) {
expected := `<p>A & B</p>`
el := P(nil, Text("A & B"))
assert.Equal(t, expected, el.Render())
}}
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Text()now escapes HTML5 special characters for safe rendering.Raw()is still available if you need to render code without escaping HTML5 elements — for example, if you need non-standard tags.Technically, only
&and<need to be escaped, not>. I still escape>for consistency and the resulting code passes the validator.Some context
I accidentally ran into the problem that elements were not escaped in Text() and learned that there was no difference between Text and Raw nodes.