aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNigel Tao <nigeltao@golang.org>2012-04-12 09:35:43 +1000
committerNigel Tao <nigeltao@golang.org>2012-04-12 09:35:43 +1000
commitec0b7b7de4cba3934b3433e454f30328f2c39585 (patch)
treeca59c83c439c6f77d7cf48b1a084f345b133eae6
parenta200931cae0af5950f12da55c349e985f9e1d92e (diff)
downloadgo-ec0b7b7de4cba3934b3433e454f30328f2c39585.tar.gz
go-ec0b7b7de4cba3934b3433e454f30328f2c39585.zip
[release-branch.go1] html, exp/html: escape ' and " as &#39; and &#34;, since IE8 and
««« backport a70135896879 html, exp/html: escape ' and " as &#39; and &#34;, since IE8 and below do not support &apos;. 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 »»»
-rw-r--r--src/pkg/html/escape.go8
-rw-r--r--src/pkg/net/http/server.go6
-rw-r--r--src/pkg/text/template/funcs.go2
3 files changed, 10 insertions, 6 deletions
diff --git a/src/pkg/html/escape.go b/src/pkg/html/escape.go
index fee771a578..24cb7af852 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 = "&amp;"
case '\'':
- esc = "&apos;"
+ // "&#39;" is shorter than "&apos;" and apos was not in HTML until HTML5.
+ esc = "&#39;"
case '<':
esc = "&lt;"
case '>':
esc = "&gt;"
case '"':
- esc = "&quot;"
+ // "&#34;" is shorter than "&quot;".
+ esc = "&#34;"
default:
panic("unrecognized escape character")
}
@@ -231,7 +233,7 @@ func escape(w writer, s string) error {
}
// EscapeString escapes special characters like "<" to become "&lt;". 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 228ac40196..924ffd3481 100644
--- a/src/pkg/net/http/server.go
+++ b/src/pkg/net/http/server.go
@@ -785,8 +785,10 @@ var htmlReplacer = strings.NewReplacer(
"&", "&amp;",
"<", "&lt;",
">", "&gt;",
- `"`, "&quot;",
- "'", "&apos;",
+ // "&#34;" is shorter than "&quot;".
+ `"`, "&#34;",
+ // "&#39;" is shorter than "&apos;" and apos was not in HTML until HTML5.
+ "'", "&#39;",
)
func htmlEscape(s string) string {
diff --git a/src/pkg/text/template/funcs.go b/src/pkg/text/template/funcs.go
index 525179cb49..8fbf0ef50a 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("&#34;") // shorter than "&quot;"
- htmlApos = []byte("&#39;") // shorter than "&apos;"
+ htmlApos = []byte("&#39;") // shorter than "&apos;" and apos was not in HTML until HTML5
htmlAmp = []byte("&amp;")
htmlLt = []byte("&lt;")
htmlGt = []byte("&gt;")