exp/template/html: fix JS regexp escape of an empty string.

R=dsymonds
CC=golang-dev, mikesamuel
https://golang.org/cl/4972063
This commit is contained in:
Nigel Tao 2011-09-12 11:57:34 +10:00
parent a5d0b7ee3e
commit b2b3187f5e
3 changed files with 14 additions and 4 deletions

View File

@ -194,8 +194,13 @@ func TestEscape(t *testing.T) {
},
{
"jsRe",
"<button onclick='alert(&quot;{{.H}}&quot;)'>",
`<button onclick='alert(&quot;\x3cHello\x3e&quot;)'>`,
`<button onclick='alert(/{{"foo+bar"}}/.test(""))'>`,
`<button onclick='alert(/foo\x2bbar/.test(""))'>`,
},
{
"jsReBlank",
`<script>alert(/{{""}}/.test(""));</script>`,
`<script>alert(/(?:)/.test(""));</script>`,
},
{
"styleBidiKeywordPassed",

View File

@ -174,7 +174,12 @@ func jsStrEscaper(args ...interface{}) string {
// expression literal. /foo{{.X}}bar/ matches the string "foo" followed by
// the literal text of {{.X}} followed by the string "bar".
func jsRegexpEscaper(args ...interface{}) string {
return replace(stringify(args...), jsRegexpReplacementTable)
s := replace(stringify(args...), jsRegexpReplacementTable)
if s == "" {
// /{{.X}}/ should not produce a line comment when .X == "".
return "(?:)"
}
return s
}
// stringify is an optimized form of fmt.Sprint.

View File

@ -224,7 +224,7 @@ func TestJSRegexpEscaper(t *testing.T) {
x interface{}
esc string
}{
{"", ``},
{"", `(?:)`},
{"foo", `foo`},
{"\u0000", `\0`},
{"\t", `\t`},