mirror of https://github.com/golang/go.git
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:
parent
a5d0b7ee3e
commit
b2b3187f5e
|
|
@ -194,8 +194,13 @@ func TestEscape(t *testing.T) {
|
|||
},
|
||||
{
|
||||
"jsRe",
|
||||
"<button onclick='alert("{{.H}}")'>",
|
||||
`<button onclick='alert("\x3cHello\x3e")'>`,
|
||||
`<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`},
|
||||
|
|
|
|||
Loading…
Reference in New Issue