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

fix: Do not HTML escape in fallback JSON mode #17

Merged
merged 2 commits into from
Oct 15, 2024
Merged
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
15 changes: 13 additions & 2 deletions mustache.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"encoding/json"
"fmt"
"io"
"io/ioutil"

Check failure on line 10 in mustache.go

View workflow job for this annotation

GitHub Actions / golangci-lint

SA1019: "io/ioutil" has been deprecated since Go 1.19: As of Go 1.16, the same functionality is now provided by package [io] or package [os], and those implementations should be preferred in new code. See the specific function documentation for details. (staticcheck)
"reflect"
"strings"
)
Expand Down Expand Up @@ -312,8 +312,19 @@
case float32, float64:
output = fmt.Sprintf("%g", v)
default:
obj, _ := json.Marshal(v)
output = string(obj)
// The default json encoder will HTML escape &, <, and >.
// Since we explicitly handle escape by user directive, let's make
// sure that doesn't happen in the case we just got asked to
// marshal a full object (like via `{{{.}}}`).
var b bytes.Buffer
enc := json.NewEncoder(&b)
enc.SetEscapeHTML(false)

_ = enc.Encode(v)
output = b.String()

// Sadly, the built-in encoder will add a newline so we need to remove that.
output = strings.TrimRight(output, "\n")
}
}

Expand Down
Loading