]> Cypherpunks repositories - gostls13.git/commitdiff
Fix template package so that data items
authorRoger Peppe <rogpeppe@gmail.com>
Tue, 17 Aug 2010 13:13:07 +0000 (23:13 +1000)
committerRob Pike <r@golang.org>
Tue, 17 Aug 2010 13:13:07 +0000 (23:13 +1000)
preceded by white space parse correctly.

R=r
CC=golang-dev
https://golang.org/cl/2010041

src/pkg/template/template.go
src/pkg/template/template_test.go

index 19d94c8fe253d4bf188be1b5feecce867e9f1348..20a9e8e082031cf2cf4aeae5ab1ca41af8ca9bda 100644 (file)
@@ -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' {
index 3ce6a4bb400b9e611afe3e86d7c585a81e033fcf..31da9cc5f203aee6535f5d34f728244ebe41dcf3 100644 (file)
@@ -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)