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 {
// 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' {
......
......@@ -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)
......
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