Commit caa46213 authored by Rob Pike's avatar Rob Pike

text/template: catch unmatched right delimiter

It was simply a missing error case: when scanning plain text
outside of an action, a right delimiter should be an error.

R=golang-dev, dsymonds
CC=golang-dev
https://golang.org/cl/13468045
parent fe62a1f1
...@@ -217,6 +217,10 @@ func lexText(l *lexer) stateFn { ...@@ -217,6 +217,10 @@ func lexText(l *lexer) stateFn {
} }
return lexLeftDelim return lexLeftDelim
} }
// Check for right after left in case they're the same.
if strings.HasPrefix(l.input[l.pos:], l.rightDelim) {
return l.errorf("unmatched right delimiter")
}
if l.next() == eof { if l.next() == eof {
break break
} }
......
...@@ -340,6 +340,9 @@ var lexTests = []lexTest{ ...@@ -340,6 +340,9 @@ var lexTests = []lexTest{
{itemText, 0, "hello-"}, {itemText, 0, "hello-"},
{itemError, 0, `comment ends before closing delimiter`}, {itemError, 0, `comment ends before closing delimiter`},
}}, }},
{"unmatched right delimiter", "hello-{.}}-world", []item{
{itemError, 0, `unmatched right delimiter`},
}},
} }
// collect gathers the emitted items into a slice. // collect gathers the emitted items into a slice.
......
...@@ -312,7 +312,7 @@ var isEmptyTests = []isEmptyTest{ ...@@ -312,7 +312,7 @@ var isEmptyTests = []isEmptyTest{
{"spaces only", " \t\n \t\n", true}, {"spaces only", " \t\n \t\n", true},
{"definition", `{{define "x"}}something{{end}}`, true}, {"definition", `{{define "x"}}something{{end}}`, true},
{"definitions and space", "{{define `x`}}something{{end}}\n\n{{define `y`}}something{{end}}\n\n", true}, {"definitions and space", "{{define `x`}}something{{end}}\n\n{{define `y`}}something{{end}}\n\n", true},
{"definitions and text", "{{define `x`}}something{{end}}\nx\n{{define `y`}}something{{end}}\ny\n}}", false}, {"definitions and text", "{{define `x`}}something{{end}}\nx\n{{define `y`}}something{{end}}\ny\n", false},
{"definition and action", "{{define `x`}}something{{end}}{{if 3}}foo{{end}}", false}, {"definition and action", "{{define `x`}}something{{end}}{{if 3}}foo{{end}}", false},
} }
......
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