From: Roger Peppe Date: Tue, 17 Aug 2010 13:13:07 +0000 (+1000) Subject: Fix template package so that data items X-Git-Tag: weekly.2010-08-25~44 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=781c54d06cce088d1bba35736b365a042399ebc1;p=gostls13.git Fix template package so that data items preceded by white space parse correctly. R=r CC=golang-dev https://golang.org/cl/2010041 --- diff --git a/src/pkg/template/template.go b/src/pkg/template/template.go index 19d94c8fe2..20a9e8e082 100644 --- a/src/pkg/template/template.go +++ b/src/pkg/template/template.go @@ -220,8 +220,7 @@ func equal(s []byte, n int, t []byte) bool { // either side, up to and including the newline. func (t *Template) nextItem() []byte { special := false // is this a {.foo} directive, which means trim white space? - // Delete surrounding white space if this {.foo} is the only thing on the line. - trimSpace := t.p == 0 || t.buf[t.p-1] == '\n' + startOfLine := t.p == 0 || t.buf[t.p-1] == '\n' start := t.p var i int newline := func() { @@ -234,11 +233,7 @@ func (t *Template) nextItem() []byte { break } } - if !trimSpace && i > start { - // white space is valid text - t.p = i - return t.buf[start:i] - } + leadingWhite := i > start // What's left is nothing, newline, delimited string, or plain text Switch: switch { @@ -247,12 +242,16 @@ Switch: case t.buf[i] == '\n': newline() case equal(t.buf, i, t.ldelim): + // Delete surrounding white space if this {.foo} is the first thing on the line. i += len(t.ldelim) // position after delimiter - if i+1 < len(t.buf) && (t.buf[i] == '.' || t.buf[i] == '#') { - special = true - if trimSpace { - start = i - len(t.ldelim) - } + special = i+1 < len(t.buf) && (t.buf[i] == '.' || t.buf[i] == '#') + if special && startOfLine { + start = i - len(t.ldelim) + } else if leadingWhite { + // not trimming space: return leading white space if there is some. + i -= len(t.ldelim) + t.p = i + return t.buf[start:i] } for ; i < len(t.buf); i++ { if t.buf[i] == '\n' { @@ -277,7 +276,7 @@ Switch: } } item := t.buf[start:i] - if special && trimSpace { + if special && startOfLine { // consume trailing white space for ; i < len(t.buf) && white(t.buf[i]); i++ { if t.buf[i] == '\n' { diff --git a/src/pkg/template/template_test.go b/src/pkg/template/template_test.go index 3ce6a4bb40..31da9cc5f2 100644 --- a/src/pkg/template/template_test.go +++ b/src/pkg/template/template_test.go @@ -369,6 +369,14 @@ var tests = []*Test{ out: "stringresult\n" + "stringresult\n", }, + &Test{ + in: "{.repeated section stringmap}\n" + + "\t{@}\n" + + "{.end}", + + out: "\tstringresult\n" + + "\tstringresult\n", + }, // Interface values @@ -451,7 +459,7 @@ func testAll(t *testing.T, parseFunc func(*Test) (*Template, os.Error)) { buf.Reset() tmpl, err := parseFunc(test) if err != nil { - t.Error("unexpected parse error:", err) + t.Error("unexpected parse error: ", err) continue } err = tmpl.Execute(s, &buf)