diff options
| author | Rob Pike <r@golang.org> | 2011-12-01 17:24:54 -0800 |
|---|---|---|
| committer | Rob Pike <r@golang.org> | 2011-12-01 17:24:54 -0800 |
| commit | e6b3371781d4f7b07c2c7c4e2f2ef4c4e7233225 (patch) | |
| tree | 6bd8ed831f3f50f11c37133827bdc282aa0610e7 /src/pkg/text/template/parse/parse.go | |
| parent | a5d300862b683e6a6d0e503c213d191155d1f63b (diff) | |
| download | go-e6b3371781d4f7b07c2c7c4e2f2ef4c4e7233225.tar.xz | |
template: move the empty check into parse, which needs it when constructing
tree sets.
R=golang-dev, gri
CC=golang-dev
https://golang.org/cl/5449062
Diffstat (limited to 'src/pkg/text/template/parse/parse.go')
| -rw-r--r-- | src/pkg/text/template/parse/parse.go | 32 |
1 files changed, 30 insertions, 2 deletions
diff --git a/src/pkg/text/template/parse/parse.go b/src/pkg/text/template/parse/parse.go index 346f613b04..4da756657d 100644 --- a/src/pkg/text/template/parse/parse.go +++ b/src/pkg/text/template/parse/parse.go @@ -7,6 +7,7 @@ package parse import ( + "bytes" "fmt" "runtime" "strconv" @@ -177,10 +178,37 @@ func (t *Tree) Parse(s, leftDelim, rightDelim string, treeSet map[string]*Tree, // add adds tree to the treeSet. func (t *Tree) add(treeSet map[string]*Tree) { - if _, present := treeSet[t.Name]; present { + tree := treeSet[t.Name] + if tree == nil || IsEmptyTree(tree.Root) { + treeSet[t.Name] = t + return + } + if !IsEmptyTree(t.Root) { t.errorf("template: multiple definition of template %q", t.Name) } - treeSet[t.Name] = t +} + +// IsEmptyTree reports whether this tree (node) is empty of everything but space. +func IsEmptyTree(n Node) bool { + switch n := n.(type) { + case *ActionNode: + case *IfNode: + case *ListNode: + for _, node := range n.Nodes { + if !IsEmptyTree(node) { + return false + } + } + return true + case *RangeNode: + case *TemplateNode: + case *TextNode: + return len(bytes.TrimSpace(n.Text)) == 0 + case *WithNode: + default: + panic("unknown node: " + n.String()) + } + return false } // parse is the top-level parser for a template, essentially the same |
