diff options
| author | Rob Pike <r@golang.org> | 2015-09-08 14:58:12 -0700 |
|---|---|---|
| committer | Rob Pike <r@golang.org> | 2015-09-09 05:28:11 +0000 |
| commit | e6ee26a03b79d0e8b658463bdb29349ca68e1460 (patch) | |
| tree | 3c8ba47d66ba2ba5d9d00675a1fb67de8b4ebd08 /src/text/template/parse/parse_test.go | |
| parent | 49fb8cc10c2d61ebdf3829f42bba9bec7b0a7ff7 (diff) | |
| download | go-e6ee26a03b79d0e8b658463bdb29349ca68e1460.tar.xz | |
text/template: provide a way to trim leading and trailing space between actions
Borrowing a suggestion from the issue listed below, we modify the lexer to
trim spaces at the beginning (end) of a block of text if the action immediately
before (after) is marked with a minus sign. To avoid parsing/lexing ambiguity,
we require an ASCII space between the minus sign and the rest of the action.
Thus:
{{23 -}}
<
{{- 45}}
produces the output
23<45
All the work is done in the lexer. The modification is invisible to the parser
or any outside package (except I guess for noticing some gaps in the input
if one tracks error positions). Thus it slips in without worry in text/template
and html/template both.
Fixes long-requested issue #9969.
Change-Id: I3774be650bfa6370cb993d0899aa669c211de7b2
Reviewed-on: https://go-review.googlesource.com/14391
Reviewed-by: Andrew Gerrand <adg@golang.org>
Diffstat (limited to 'src/text/template/parse/parse_test.go')
| -rw-r--r-- | src/text/template/parse/parse_test.go | 7 |
1 files changed, 7 insertions, 0 deletions
diff --git a/src/text/template/parse/parse_test.go b/src/text/template/parse/parse_test.go index 200d50c194..28b5f7cb90 100644 --- a/src/text/template/parse/parse_test.go +++ b/src/text/template/parse/parse_test.go @@ -228,6 +228,13 @@ var parseTests = []parseTest{ `{{with .X}}"hello"{{end}}`}, {"with with else", "{{with .X}}hello{{else}}goodbye{{end}}", noError, `{{with .X}}"hello"{{else}}"goodbye"{{end}}`}, + // Trimming spaces. + {"trim left", "x \r\n\t{{- 3}}", noError, `"x"{{3}}`}, + {"trim right", "{{3 -}}\n\n\ty", noError, `{{3}}"y"`}, + {"trim left and right", "x \r\n\t{{- 3 -}}\n\n\ty", noError, `"x"{{3}}"y"`}, + {"comment trim left", "x \r\n\t{{- /* hi */}}", noError, `"x"`}, + {"comment trim right", "{{/* hi */ -}}\n\n\ty", noError, `"y"`}, + {"comment trim left and right", "x \r\n\t{{- /* */ -}}\n\n\ty", noError, `"x""y"`}, // Errors. {"unclosed action", "hello{{range", hasError, ""}, {"unmatched end", "{{end}}", hasError, ""}, |
