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

avoid HTML escaping in JSON files #3560

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
7 changes: 2 additions & 5 deletions internal/action/bindings.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package action

import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
Expand Down Expand Up @@ -303,8 +302,7 @@ func TryBindKey(k, v string, overwrite bool) (bool, error) {

BindKey(k, v, Binder["buffer"])

txt, _ := json.MarshalIndent(parsed, "", " ")
return true, ioutil.WriteFile(filename, append(txt, '\n'), 0644)
return true, config.WriteJson(filename, parsed)
}
return false, e
}
Expand Down Expand Up @@ -353,8 +351,7 @@ func UnbindKey(k string) error {
delete(config.Bindings["buffer"], k)
}

txt, _ := json.MarshalIndent(parsed, "", " ")
return ioutil.WriteFile(filename, append(txt, '\n'), 0644)
return config.WriteJson(filename, parsed)
}
return e
}
Expand Down
18 changes: 14 additions & 4 deletions internal/config/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,18 @@ func InitLocalSettings(settings map[string]interface{}, path string) {
}
}

func WriteJson(filename string, v interface{}) error {
file, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
return err
}
defer file.Close()
enc := json.NewEncoder(file)
enc.SetEscapeHTML(false)
enc.SetIndent("", " ")
return enc.Encode(v)
}

// WriteSettings writes the settings to the specified filename as JSON
func WriteSettings(filename string) error {
if settingsParseError {
Expand Down Expand Up @@ -346,8 +358,7 @@ func WriteSettings(filename string) error {
}
}

txt, _ := json.MarshalIndent(parsedSettings, "", " ")
err = ioutil.WriteFile(filename, append(txt, '\n'), 0644)
err = WriteJson(filename, parsedSettings)
}
return err
}
Expand All @@ -368,8 +379,7 @@ func OverwriteSettings(filename string) error {
}
}

txt, _ := json.MarshalIndent(settings, "", " ")
err = ioutil.WriteFile(filename, append(txt, '\n'), 0644)
err = WriteJson(filename, settings)
}
return err
}
Expand Down
Loading