From ec0b7b7de4cba3934b3433e454f30328f2c39585 Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Thu, 12 Apr 2012 09:35:43 +1000 Subject: [PATCH] [release-branch.go1] html, exp/html: escape ' and " as ' and ", since IE8 and MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ««« backport a70135896879 html, exp/html: escape ' and " as ' and ", since IE8 and below do not support '. This makes package html consistent with package text/template's HTMLEscape function. Fixes #3489. R=rsc, mikesamuel, dsymonds CC=golang-dev https://golang.org/cl/5992071 »»» --- src/pkg/html/escape.go | 8 +++++--- src/pkg/net/http/server.go | 6 ++++-- src/pkg/text/template/funcs.go | 2 +- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/pkg/html/escape.go b/src/pkg/html/escape.go index fee771a5784e20..24cb7af85240ff 100644 --- a/src/pkg/html/escape.go +++ b/src/pkg/html/escape.go @@ -210,13 +210,15 @@ func escape(w writer, s string) error { case '&': esc = "&" case '\'': - esc = "'" + // "'" is shorter than "'" and apos was not in HTML until HTML5. + esc = "'" case '<': esc = "<" case '>': esc = ">" case '"': - esc = """ + // """ is shorter than """. + esc = """ default: panic("unrecognized escape character") } @@ -231,7 +233,7 @@ func escape(w writer, s string) error { } // EscapeString escapes special characters like "<" to become "<". It -// escapes only five such characters: amp, apos, lt, gt and quot. +// escapes only five such characters: <, >, &, ' and ". // UnescapeString(EscapeString(s)) == s always holds, but the converse isn't // always true. func EscapeString(s string) string { diff --git a/src/pkg/net/http/server.go b/src/pkg/net/http/server.go index 228ac401968182..924ffd348156e0 100644 --- a/src/pkg/net/http/server.go +++ b/src/pkg/net/http/server.go @@ -785,8 +785,10 @@ var htmlReplacer = strings.NewReplacer( "&", "&", "<", "<", ">", ">", - `"`, """, - "'", "'", + // """ is shorter than """. + `"`, """, + // "'" is shorter than "'" and apos was not in HTML until HTML5. + "'", "'", ) func htmlEscape(s string) string { diff --git a/src/pkg/text/template/funcs.go b/src/pkg/text/template/funcs.go index 525179cb499839..8fbf0ef50a765e 100644 --- a/src/pkg/text/template/funcs.go +++ b/src/pkg/text/template/funcs.go @@ -246,7 +246,7 @@ func not(arg interface{}) (truth bool) { var ( htmlQuot = []byte(""") // shorter than """ - htmlApos = []byte("'") // shorter than "'" + htmlApos = []byte("'") // shorter than "'" and apos was not in HTML until HTML5 htmlAmp = []byte("&") htmlLt = []byte("<") htmlGt = []byte(">")