]> Cypherpunks repositories - gostls13.git/commitdiff
text/template/parse: allow space after continue or break
authorRob Pike <r@golang.org>
Mon, 14 Mar 2022 23:21:08 +0000 (10:21 +1100)
committerRob Pike <r@golang.org>
Tue, 15 Mar 2022 00:19:35 +0000 (00:19 +0000)
Trivial fix: We must skip space after either of these keywords
before we expect a closing delimiter.

Also delete the stutter-generating extra 'in' in the error message.
(See what I did there?)

Fixes #51670

Change-Id: If5415632c36eaac6699bdc0aa6ce18be956c9b53
Reviewed-on: https://go-review.googlesource.com/c/go/+/392615
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Daniel Martí <mvdan@mvdan.cc>
Trust: Daniel Martí <mvdan@mvdan.cc>
TryBot-Result: Gopher Robot <gobot@golang.org>

src/text/template/parse/parse.go
src/text/template/parse/parse_test.go

index b0cbe9dfc8b0c7097097fb5e414cec75977f58ac..ce548b08865d893308249ccade5a3da2496312e5 100644 (file)
@@ -415,8 +415,8 @@ func (t *Tree) action() (n Node) {
 //     {{break}}
 // Break keyword is past.
 func (t *Tree) breakControl(pos Pos, line int) Node {
-       if token := t.next(); token.typ != itemRightDelim {
-               t.unexpected(token, "in {{break}}")
+       if token := t.nextNonSpace(); token.typ != itemRightDelim {
+               t.unexpected(token, "{{break}}")
        }
        if t.rangeDepth == 0 {
                t.errorf("{{break}} outside {{range}}")
@@ -428,8 +428,8 @@ func (t *Tree) breakControl(pos Pos, line int) Node {
 //     {{continue}}
 // Continue keyword is past.
 func (t *Tree) continueControl(pos Pos, line int) Node {
-       if token := t.next(); token.typ != itemRightDelim {
-               t.unexpected(token, "in {{continue}}")
+       if token := t.nextNonSpace(); token.typ != itemRightDelim {
+               t.unexpected(token, "{{continue}}")
        }
        if t.rangeDepth == 0 {
                t.errorf("{{continue}} outside {{range}}")
index 0c4778c7b3b7f865cf6880231118e5868c1f2207..fdb25d78f5510dbe91568514a965b53a0bd13cc4 100644 (file)
@@ -260,6 +260,10 @@ var parseTests = []parseTest{
        {"newline in pipeline", "{{\n\"x\"\n|\nprintf\n}}", noError, `{{"x" | printf}}`},
        {"newline in comment", "{{/*\nhello\n*/}}", noError, ""},
        {"newline in comment", "{{-\n/*\nhello\n*/\n-}}", noError, ""},
+       {"spaces around continue", "{{range .SI}}{{.}}{{ continue }}{{end}}", noError,
+               `{{range .SI}}{{.}}{{continue}}{{end}}`},
+       {"spaces around break", "{{range .SI}}{{.}}{{ break }}{{end}}", noError,
+               `{{range .SI}}{{.}}{{break}}{{end}}`},
 
        // Errors.
        {"unclosed action", "hello{{range", hasError, ""},