Commit b2b3187f authored by Nigel Tao's avatar Nigel Tao

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

R=dsymonds
CC=golang-dev, mikesamuel
https://golang.org/cl/4972063
parent a5d0b7ee
......@@ -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",
......
......@@ -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.
......
......@@ -224,7 +224,7 @@ func TestJSRegexpEscaper(t *testing.T) {
x interface{}
esc string
}{
{"", ``},
{"", `(?:)`},
{"foo", `foo`},
{"\u0000", `\0`},
{"\t", `\t`},
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment