Skip to content

Commit

Permalink
[release-branch.go1] html, exp/html: escape ' and " as ' and "…
Browse files Browse the repository at this point in the history
…, since IE8 and

««« 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
»»»
  • Loading branch information
nigeltao committed Apr 11, 2012
1 parent a200931 commit ec0b7b7
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 6 deletions.
8 changes: 5 additions & 3 deletions src/pkg/html/escape.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "&lt;"
case '>':
esc = "&gt;"
case '"':
esc = "&quot;"
// "&#34;" is shorter than "&quot;".
esc = "&#34;"
default:
panic("unrecognized escape character")
}
Expand All @@ -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 {
Expand Down
6 changes: 4 additions & 2 deletions src/pkg/net/http/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion src/pkg/text/template/funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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;")
Expand Down

0 comments on commit ec0b7b7

Please sign in to comment.