Commit df4de948 authored by Rob Pike's avatar Rob Pike

text/template/parse: print TextNodes using %s not %q

This means that printing a Node will produce output that can be used as valid input.
It won't be exactly the same - some spacing may be different - but it will mean the same.

Fixes #4593.

R=golang-dev, dsymonds
CC=golang-dev
https://golang.org/cl/12006047
parent 221af5c1
...@@ -33,10 +33,10 @@ var multiParseTests = []multiParseTest{ ...@@ -33,10 +33,10 @@ var multiParseTests = []multiParseTest{
nil}, nil},
{"one", `{{define "foo"}} FOO {{end}}`, noError, {"one", `{{define "foo"}} FOO {{end}}`, noError,
[]string{"foo"}, []string{"foo"},
[]string{`" FOO "`}}, []string{" FOO "}},
{"two", `{{define "foo"}} FOO {{end}}{{define "bar"}} BAR {{end}}`, noError, {"two", `{{define "foo"}} FOO {{end}}{{define "bar"}} BAR {{end}}`, noError,
[]string{"foo", "bar"}, []string{"foo", "bar"},
[]string{`" FOO "`, `" BAR "`}}, []string{" FOO ", " BAR "}},
// errors // errors
{"missing end", `{{define "foo"}} FOO `, hasError, {"missing end", `{{define "foo"}} FOO `, hasError,
nil, nil,
......
...@@ -13,6 +13,8 @@ import ( ...@@ -13,6 +13,8 @@ import (
"strings" "strings"
) )
var textFormat = "%s" // Changed to "%q" in tests for better error messages.
// A Node is an element in the parse tree. The interface is trivial. // A Node is an element in the parse tree. The interface is trivial.
// The interface contains an unexported method so that only // The interface contains an unexported method so that only
// types local to this package can satisfy it. // types local to this package can satisfy it.
...@@ -125,7 +127,7 @@ func newText(pos Pos, text string) *TextNode { ...@@ -125,7 +127,7 @@ func newText(pos Pos, text string) *TextNode {
} }
func (t *TextNode) String() string { func (t *TextNode) String() string {
return fmt.Sprintf("%q", t.Text) return fmt.Sprintf(textFormat, t.Text)
} }
func (t *TextNode) Copy() Node { func (t *TextNode) Copy() Node {
......
...@@ -256,6 +256,8 @@ var builtins = map[string]interface{}{ ...@@ -256,6 +256,8 @@ var builtins = map[string]interface{}{
} }
func testParse(doCopy bool, t *testing.T) { func testParse(doCopy bool, t *testing.T) {
textFormat = "%q"
defer func() { textFormat = "%s" }()
for _, test := range parseTests { for _, test := range parseTests {
tmpl, err := New(test.name).Parse(test.input, "", "", make(map[string]*Tree), builtins) tmpl, err := New(test.name).Parse(test.input, "", "", make(map[string]*Tree), builtins)
switch { switch {
......
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