Commit 781c54d0 authored by Roger Peppe's avatar Roger Peppe Committed by Rob Pike

Fix template package so that data items

preceded by white space parse correctly.

R=r
CC=golang-dev
https://golang.org/cl/2010041
parent e5aad819
...@@ -220,8 +220,7 @@ func equal(s []byte, n int, t []byte) bool { ...@@ -220,8 +220,7 @@ func equal(s []byte, n int, t []byte) bool {
// either side, up to and including the newline. // either side, up to and including the newline.
func (t *Template) nextItem() []byte { func (t *Template) nextItem() []byte {
special := false // is this a {.foo} directive, which means trim white space? 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. startOfLine := t.p == 0 || t.buf[t.p-1] == '\n'
trimSpace := t.p == 0 || t.buf[t.p-1] == '\n'
start := t.p start := t.p
var i int var i int
newline := func() { newline := func() {
...@@ -234,11 +233,7 @@ func (t *Template) nextItem() []byte { ...@@ -234,11 +233,7 @@ func (t *Template) nextItem() []byte {
break break
} }
} }
if !trimSpace && i > start { leadingWhite := i > start
// white space is valid text
t.p = i
return t.buf[start:i]
}
// What's left is nothing, newline, delimited string, or plain text // What's left is nothing, newline, delimited string, or plain text
Switch: Switch:
switch { switch {
...@@ -247,12 +242,16 @@ Switch: ...@@ -247,12 +242,16 @@ Switch:
case t.buf[i] == '\n': case t.buf[i] == '\n':
newline() newline()
case equal(t.buf, i, t.ldelim): 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 i += len(t.ldelim) // position after delimiter
if i+1 < len(t.buf) && (t.buf[i] == '.' || t.buf[i] == '#') { special = i+1 < len(t.buf) && (t.buf[i] == '.' || t.buf[i] == '#')
special = true if special && startOfLine {
if trimSpace { start = i - len(t.ldelim)
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++ { for ; i < len(t.buf); i++ {
if t.buf[i] == '\n' { if t.buf[i] == '\n' {
...@@ -277,7 +276,7 @@ Switch: ...@@ -277,7 +276,7 @@ Switch:
} }
} }
item := t.buf[start:i] item := t.buf[start:i]
if special && trimSpace { if special && startOfLine {
// consume trailing white space // consume trailing white space
for ; i < len(t.buf) && white(t.buf[i]); i++ { for ; i < len(t.buf) && white(t.buf[i]); i++ {
if t.buf[i] == '\n' { if t.buf[i] == '\n' {
......
...@@ -369,6 +369,14 @@ var tests = []*Test{ ...@@ -369,6 +369,14 @@ var tests = []*Test{
out: "stringresult\n" + out: "stringresult\n" +
"stringresult\n", "stringresult\n",
}, },
&Test{
in: "{.repeated section stringmap}\n" +
"\t{@}\n" +
"{.end}",
out: "\tstringresult\n" +
"\tstringresult\n",
},
// Interface values // Interface values
...@@ -451,7 +459,7 @@ func testAll(t *testing.T, parseFunc func(*Test) (*Template, os.Error)) { ...@@ -451,7 +459,7 @@ func testAll(t *testing.T, parseFunc func(*Test) (*Template, os.Error)) {
buf.Reset() buf.Reset()
tmpl, err := parseFunc(test) tmpl, err := parseFunc(test)
if err != nil { if err != nil {
t.Error("unexpected parse error:", err) t.Error("unexpected parse error: ", err)
continue continue
} }
err = tmpl.Execute(s, &buf) err = tmpl.Execute(s, &buf)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment