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

html template package renders escaped version of html (unexpected) #26392

Closed
rybarix opened this issue Jul 15, 2018 · 5 comments
Closed

html template package renders escaped version of html (unexpected) #26392

rybarix opened this issue Jul 15, 2018 · 5 comments

Comments

@rybarix
Copy link

rybarix commented Jul 15, 2018

Please answer these questions before submitting your issue. Thanks!

What version of Go are you using (go version)?

go version go1.10.3 linux/amd64

Does this issue reproduce with the latest release?

yes

What operating system and processor architecture are you using (go env)?

GOARCH="amd64"

What did you do?

// issue.go
package main
import (
	"html/template"
	"log"
	"net/http"
)

var templates = template.Must(template.ParseFiles("view_product.html"))
func RenderTemplate(w http.ResponseWriter, tmpl string, data interface{}) {
	err := templates.Execute(w, data)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
	}
}

func controller(w http.ResponseWriter, r *http.Request) {
	viewer.RenderTemplate(w, "view_product", nil)
}
func main() {
	http.HandleFunc("/", controller)
	log.Fatal(http.ListenAndServe(":8080", nil))
}
// view_product.html
<ul>
    <li>Hello</li>
</ul>

What did you expect to see?

interpreted html by browser:

  • Hello

What did you see instead?

escaped version:

<ul>
    <li>Hello</li>
</ul>

*But if we add <h1>Hello</h1> or div to 'view_product.html', browser renders correctly, (tested on chrome, firefox)

view_product.html -- working version which for some reason renders correctly

<h1>Hello</h1>
<ul>
    <li>Hello</li>
</ul>

There is unexpected behavior, which is not consistent.

@mvdan
Copy link
Member

mvdan commented Jul 15, 2018

Have you checked what output the html/template.Template renders to? Templates are simply templates - they do not know how a web browser will render them.

@rybarix
Copy link
Author

rybarix commented Jul 15, 2018

but why adding h1 element resolve render issue.
PS Im new to golang.

@earthboundkid
Copy link
Contributor

earthboundkid commented Jul 16, 2018

The issue is that the web browser doesn’t know that the page is HTML. You need to set the Content-Type header. Add w.Header().Set("Content-Type", "text/html"). See https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Type for more.

@crvv
Copy link
Contributor

crvv commented Jul 16, 2018

If you don't set Content-Type header. Go HTTP server will set it for you.
https://golang.org/pkg/net/http/#ResponseWriter

If the Header does not contain a Content-Type line, Write adds a Content-Type set to the result of passing the initial 512 bytes of written data to DetectContentType.

And DetectContentType thinks

<h1>Hello</h1>
<ul>
    <li>Hello</li>
</ul>

is text/html.

But

<ul>
    <li>Hello</li>
</ul>

is text/plain

I don't think this is a bug. HTML should start with <!DOCTYPE html>

@rybarix
Copy link
Author

rybarix commented Jul 16, 2018

@crvv @carlmjohnson
thank you for explanation, that's enough to close this issue

@rybarix rybarix closed this as completed Jul 16, 2018
@golang golang locked and limited conversation to collaborators Jul 16, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants