Skip to content

Commit

Permalink
text/template/parse: fix confusion about markers near right delims
Browse files Browse the repository at this point in the history
Fixes #52527.
Fixes #55336.

Change-Id: I8f5c521c693e74451a558788909e7e4ad1cc797a
Reviewed-on: https://go-review.googlesource.com/c/go/+/433036
Run-TryBot: Russ Cox <[email protected]>
TryBot-Result: Gopher Robot <[email protected]>
Reviewed-by: Rob Pike <[email protected]>
Auto-Submit: Russ Cox <[email protected]>
Reviewed-by: Cherry Mui <[email protected]>
  • Loading branch information
rsc authored and gopherbot committed Sep 23, 2022
1 parent 2551324 commit 65deb9c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/text/template/parse/lex.go
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ func lexComment(l *lexer) stateFn {

// lexRightDelim scans the right delimiter, which is known to be present, possibly with a trim marker.
func lexRightDelim(l *lexer) stateFn {
trimSpace := hasRightTrimMarker(l.input[l.pos:])
_, trimSpace := l.atRightDelim()
if trimSpace {
l.pos += trimMarkerLen
l.ignore()
Expand Down
17 changes: 17 additions & 0 deletions src/text/template/parse/lex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,23 @@ func TestDelimsAlphaNumeric(t *testing.T) {
}
}

func TestDelimsAndMarkers(t *testing.T) {
test := lexTest{"delims that look like markers", "{{- .x -}} {{- - .x - -}}", []item{
mkItem(itemLeftDelim, "{{- "),
mkItem(itemField, ".x"),
mkItem(itemRightDelim, " -}}"),
mkItem(itemLeftDelim, "{{- "),
mkItem(itemField, ".x"),
mkItem(itemRightDelim, " -}}"),
tEOF,
}}
items := collect(&test, "{{- ", " -}}")

if !equal(items, test.items, false) {
t.Errorf("%s: got\n\t%v\nexpected\n\t%v", test.name, items, test.items)
}
}

var lexPosTests = []lexTest{
{"empty", "", []item{{itemEOF, 0, "", 1}}},
{"punctuation", "{{,@%#}}", []item{
Expand Down

0 comments on commit 65deb9c

Please sign in to comment.