My application is written in Go. A page of the application receives user data from an HTML text area which is saved to Google App Engine datastore as string data. Then, on another page of the application, it requires to display the data in the format that the user typed into the HTML text area or at least preserve the line breaks as the user hit enter key while typing the data into the HTML text field.
I tried to use <pre>
tag to display the preserved line breaks that user typed into the text field which worked fine. However, I found that there is a double tab
\t\t
inserted at the beginning of the string when using the <pre>
tag which I could remove the double tab by using Javascript.
My idea to keep the line breaks that users type into the text field is to use javascript replace function to replace all \r\n
, \n
and \r
to <br>
, however, it didn't work out. It seems like Go doesn't output the line breaks into the html source code. I am using "html/template" for safety reason but I didn't expect it to remove the \r\n
characters from its output entirely. And is there any suggestion on the strange double tab \t\t
at the beginning to the string?
My application is written in Go. A page of the application receives user data from an HTML text area which is saved to Google App Engine datastore as string data. Then, on another page of the application, it requires to display the data in the format that the user typed into the HTML text area or at least preserve the line breaks as the user hit enter key while typing the data into the HTML text field.
I tried to use <pre>
tag to display the preserved line breaks that user typed into the text field which worked fine. However, I found that there is a double tab
\t\t
inserted at the beginning of the string when using the <pre>
tag which I could remove the double tab by using Javascript.
My idea to keep the line breaks that users type into the text field is to use javascript replace function to replace all \r\n
, \n
and \r
to <br>
, however, it didn't work out. It seems like Go doesn't output the line breaks into the html source code. I am using "html/template" for safety reason but I didn't expect it to remove the \r\n
characters from its output entirely. And is there any suggestion on the strange double tab \t\t
at the beginning to the string?
-
I replaced
\n
as well, I didn't work out either. I am using Linux. – ともこ Commented Mar 20, 2016 at 13:41
2 Answers
Reset to default 5Instead of changing the newlines to <br>
in javascript, do it in Go: http://play.golang/p/jJXMZxtYcv
package main
import (
"html/template"
"log"
"os"
"strings"
)
func main() {
const tpl = `
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>{{.Title}}</title>
</head>
<body>
<div>{{nl2br .Text}}</div>
</body>
</html>`
check := func(err error) {
if err != nil {
log.Fatal(err)
}
}
funcs := template.FuncMap{
"nl2br": func(text string) template.HTML {
return template.HTML(strings.Replace(template.HTMLEscapeString(text), "\n", "<br>", -1))
},
}
t, err := template.New("webpage").Funcs(funcs).Parse(tpl)
check(err)
data := struct {
Title string
Text string
}{
Title: "My page",
Text: "\t\tLine 1\r\nLine2\r\n",
}
err = t.Execute(os.Stdout, data)
check(err)
}
You can simply solve you issue with CSS formatting, namely the white-space
property:
The white-space property specifies how white-space inside an element is handled.
CSS Syntax:
white-space: normal|nowrap|pre|pre-line|pre-wrap|initial|inherit;
The pre
value acts as if it would be wrapped in an HTML <pre>
tag, which would also render the double tabs.
Using the pre-line
value works for you:
pre-line
Sequences of whitespace will collapse into a single whitespace. Text will wrap when necessary, and on line breaks
So lines are preserved, but multiple whitespaces are collapsed into a single one. Advantage is that you don't need to transform your original text, the browser will apply the proper formatting. Code remains safe against HTML injection attacks.
Complete example:
func main() {
t := template.Must(template.New("").Parse(src))
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
t.Execute(w, text)
})
panic(http.ListenAndServe("localhost:8080", nil))
}
const src = `<html><body>Entered text:
<div style="white-space:pre-line">{{.}}</div>
</body></html>`
const text = "\t\t1st line\n2nd line\r\n3rdline"
Your initial double tabs \t\t
Note that your initial double tabs \t\t
are not generated by browsers or form submits (nor the Datastore if you store it there) but are most likely the result of you using a template where you have an indentation of 2 tabs. Indentation of templates is also written to output, so you should properly close your <textarea>
without newlines in the template. Since you didn't post your template and code, I can't be more help on finding out why you have initial double tabs, but you should find the root cause and not treat the symptoms.