]> Cypherpunks repositories - gostls13.git/commitdiff
text/template: detect unmatched else at parsing time
authorDidier Spezia <didier.06@gmail.com>
Fri, 1 May 2015 18:20:31 +0000 (18:20 +0000)
committerRob Pike <r@golang.org>
Fri, 1 May 2015 21:27:27 +0000 (21:27 +0000)
An unmatched {{else}} should trigger a parsing error.

The top level parser is able to issue an error in case
of unmatched {{end}}. It does it a posteriori (i.e. after having
parsed the action).

Extend this behavior to also check for unmatched {{else}}

Fixes #10611

Change-Id: I1d4f433cc64e11bea5f4d61419ccc707ac01bb1d
Reviewed-on: https://go-review.googlesource.com/9620
Reviewed-by: Rob Pike <r@golang.org>
src/text/template/parse/parse.go
src/text/template/parse/parse_test.go

index af33880c15aae4a24c599c339e0fa1d5760a9e5b..d0efcbf60960119b6ac2b0725cb9d6ff545197f2 100644 (file)
@@ -288,11 +288,12 @@ func (t *Tree) parse(treeSet map[string]*Tree) (next Node) {
                        }
                        t.backup2(delim)
                }
-               n := t.textOrAction()
-               if n.Type() == nodeEnd {
+               switch n := t.textOrAction(); n.Type() {
+               case nodeEnd, nodeElse:
                        t.errorf("unexpected %s", n)
+               default:
+                       t.Root.append(n)
                }
-               t.Root.append(n)
        }
        return nil
 }
index 4a504fa7c839ccbd33d84274cda671fd74b8423e..faac06fe5a62fd2f7ac262a6116df68d97068e92 100644 (file)
@@ -230,6 +230,9 @@ var parseTests = []parseTest{
        // Errors.
        {"unclosed action", "hello{{range", hasError, ""},
        {"unmatched end", "{{end}}", hasError, ""},
+       {"unmatched else", "{{else}}", hasError, ""},
+       {"unmatched else after if", "{{if .X}}hello{{end}}{{else}}", hasError, ""},
+       {"multiple else", "{{if .X}}1{{else}}2{{else}}3{{end}}", hasError, ""},
        {"missing end", "hello{{range .x}}", hasError, ""},
        {"missing end after else", "hello{{range .x}}{{else}}", hasError, ""},
        {"undefined function", "hello{{undefined}}", hasError, ""},