From 99f76234a08499561eceebaee3ddcb3642eabdeb Mon Sep 17 00:00:00 2001 From: "Alexander \"Arav\" Andreev" Date: Sun, 28 May 2023 04:52:09 +0400 Subject: [PATCH] Made a func cleanupNewlines() to replace repeats across handlers. --- internal/http/api_guestbook_handlers.go | 16 ++++------------ internal/http/api_mindflow_handlers.go | 4 +--- internal/http/web_handlers.go | 6 ++++++ 3 files changed, 11 insertions(+), 15 deletions(-) diff --git a/internal/http/api_guestbook_handlers.go b/internal/http/api_guestbook_handlers.go index 103d107..68fb6de 100644 --- a/internal/http/api_guestbook_handlers.go +++ b/internal/http/api_guestbook_handlers.go @@ -37,9 +37,7 @@ func (h *GuestbookApiHandlers) New(w http.ResponseWriter, r *http.Request) { r.Form.Set("name", "Anonymous") } - message := strings.ReplaceAll(r.FormValue("message"), "\r\n", "\n") - message = strings.ReplaceAll(message, "\n\r", "\n") - message = strings.ReplaceAll(message, "\r", "\n") + message := cleanupNewlines(r.FormValue("message")) entry, err = justguestbook.NewEntry(r.FormValue("name"), message, 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") } - message := strings.ReplaceAll(r.FormValue("message"), "\r\n", "\n") - message = strings.ReplaceAll(message, "\n\r", "\n") - message = strings.ReplaceAll(message, "\r", "\n") + message := cleanupNewlines(r.FormValue("message")) entry, err = justguestbook.NewEntry(r.FormValue("name"), message, 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) - message := strings.ReplaceAll(r.FormValue("message"), "\r\n", "\n") - message = strings.ReplaceAll(message, "\n\r", "\n") - message = strings.ReplaceAll(message, "\r", "\n") + message := cleanupNewlines(r.FormValue("message")) reply, err = justguestbook.NewReply(id, message) 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) - message := strings.ReplaceAll(r.FormValue("message"), "\r\n", "\n") - message = strings.ReplaceAll(message, "\n\r", "\n") - message = strings.ReplaceAll(message, "\r", "\n") + message := cleanupNewlines(r.FormValue("message")) reply, err = justguestbook.NewReply(id, message) if err != nil { diff --git a/internal/http/api_mindflow_handlers.go b/internal/http/api_mindflow_handlers.go index 84e1ae2..5c0d1bf 100644 --- a/internal/http/api_mindflow_handlers.go +++ b/internal/http/api_mindflow_handlers.go @@ -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") { r.ParseForm() - body := strings.ReplaceAll(r.FormValue("body"), "\r\n", "\n") - body = strings.ReplaceAll(body, "\n\r", "\n") - body = strings.ReplaceAll(body, "\r", "\n") + body := cleanupNewlines(r.FormValue("body")) if r.FormValue("category") != "" { category, err = mindflow.NewCategory(r.FormValue("new-category")) diff --git a/internal/http/web_handlers.go b/internal/http/web_handlers.go index 639ba5e..c736444 100644 --- a/internal/http/web_handlers.go +++ b/internal/http/web_handlers.go @@ -175,3 +175,9 @@ func SitemapXml(w http.ResponseWriter, r *http.Request) { func Error(w http.ResponseWriter, code int, reason, message string) { 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") +}