1
0

Made a func cleanupNewlines() to replace repeats across handlers.

This commit is contained in:
Alexander Andreev 2023-05-28 04:52:09 +04:00
parent 5b62a77fb8
commit 99f76234a0
Signed by: Arav
GPG Key ID: D22A817D95815393
3 changed files with 11 additions and 15 deletions

View File

@ -37,9 +37,7 @@ func (h *GuestbookApiHandlers) New(w http.ResponseWriter, r *http.Request) {
r.Form.Set("name", "Anonymous") r.Form.Set("name", "Anonymous")
} }
message := strings.ReplaceAll(r.FormValue("message"), "\r\n", "\n") message := cleanupNewlines(r.FormValue("message"))
message = strings.ReplaceAll(message, "\n\r", "\n")
message = strings.ReplaceAll(message, "\r", "\n")
entry, err = justguestbook.NewEntry(r.FormValue("name"), message, entry, err = justguestbook.NewEntry(r.FormValue("name"), message,
r.FormValue("website"), r.FormValue("hide_website") != "") r.FormValue("website"), r.FormValue("hide_website") != "")
@ -68,9 +66,7 @@ func (h *GuestbookApiHandlers) Edit(w http.ResponseWriter, r *http.Request) {
r.Form.Set("name", "Anonymous") r.Form.Set("name", "Anonymous")
} }
message := strings.ReplaceAll(r.FormValue("message"), "\r\n", "\n") message := cleanupNewlines(r.FormValue("message"))
message = strings.ReplaceAll(message, "\n\r", "\n")
message = strings.ReplaceAll(message, "\r", "\n")
entry, err = justguestbook.NewEntry(r.FormValue("name"), message, entry, err = justguestbook.NewEntry(r.FormValue("name"), message,
r.FormValue("website"), r.FormValue("hide_website") != "") r.FormValue("website"), r.FormValue("hide_website") != "")
@ -106,9 +102,7 @@ func (h *GuestbookApiHandlers) Reply(w http.ResponseWriter, r *http.Request) {
id, _ := strconv.ParseInt(httpr.Param(r, "id"), 10, 64) id, _ := strconv.ParseInt(httpr.Param(r, "id"), 10, 64)
message := strings.ReplaceAll(r.FormValue("message"), "\r\n", "\n") message := cleanupNewlines(r.FormValue("message"))
message = strings.ReplaceAll(message, "\n\r", "\n")
message = strings.ReplaceAll(message, "\r", "\n")
reply, err = justguestbook.NewReply(id, message) reply, err = justguestbook.NewReply(id, message)
if err != nil { if err != nil {
@ -132,9 +126,7 @@ func (h *GuestbookApiHandlers) EditReply(w http.ResponseWriter, r *http.Request)
id, _ := strconv.ParseInt(httpr.Param(r, "id"), 10, 64) id, _ := strconv.ParseInt(httpr.Param(r, "id"), 10, 64)
message := strings.ReplaceAll(r.FormValue("message"), "\r\n", "\n") message := cleanupNewlines(r.FormValue("message"))
message = strings.ReplaceAll(message, "\n\r", "\n")
message = strings.ReplaceAll(message, "\r", "\n")
reply, err = justguestbook.NewReply(id, message) reply, err = justguestbook.NewReply(id, message)
if err != nil { if err != nil {

View File

@ -66,9 +66,7 @@ func (h *MindflowApiHandlers) EditPost(w http.ResponseWriter, r *http.Request) {
if strings.Contains(r.Header.Get("Content-Type"), "application/x-www-form-urlencoded") { if strings.Contains(r.Header.Get("Content-Type"), "application/x-www-form-urlencoded") {
r.ParseForm() r.ParseForm()
body := strings.ReplaceAll(r.FormValue("body"), "\r\n", "\n") body := cleanupNewlines(r.FormValue("body"))
body = strings.ReplaceAll(body, "\n\r", "\n")
body = strings.ReplaceAll(body, "\r", "\n")
if r.FormValue("category") != "" { if r.FormValue("category") != "" {
category, err = mindflow.NewCategory(r.FormValue("new-category")) category, err = mindflow.NewCategory(r.FormValue("new-category"))

View File

@ -175,3 +175,9 @@ func SitemapXml(w http.ResponseWriter, r *http.Request) {
func Error(w http.ResponseWriter, code int, reason, message string) { func Error(w http.ResponseWriter, code int, reason, message string) {
web.ErrorXXX("/"+http.StatusText(code), reason, message, code, w) web.ErrorXXX("/"+http.StatusText(code), reason, message, code, w)
} }
func cleanupNewlines(text string) (out string) {
out = strings.ReplaceAll(text, "\r\n", "\n")
out = strings.ReplaceAll(out, "\n\r", "\n")
return strings.ReplaceAll(out, "\r", "\n")
}