]> Cypherpunks.ru repositories - gostls13.git/commitdiff
text/template/parse: fix confusion about markers near right delims
authorRuss Cox <rsc@golang.org>
Fri, 23 Sep 2022 00:56:38 +0000 (20:56 -0400)
committerGopher Robot <gobot@golang.org>
Fri, 23 Sep 2022 15:03:43 +0000 (15:03 +0000)
Fixes #52527.
Fixes #55336.

Change-Id: I8f5c521c693e74451a558788909e7e4ad1cc797a
Reviewed-on: https://go-review.googlesource.com/c/go/+/433036
Run-TryBot: Russ Cox <rsc@golang.org>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Rob Pike <r@golang.org>
Auto-Submit: Russ Cox <rsc@golang.org>
Reviewed-by: Cherry Mui <cherryyz@google.com>
src/text/template/parse/lex.go
src/text/template/parse/lex_test.go

index 3562e0abc9afb83ac2b0de9fa83201cc3639c63a..3e60a1ecef5f397fcae78c88b6b1a9531142bb00 100644 (file)
@@ -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()
index 947889a80bde9d4b5e649029fc3f72560176af00..d47f10f9d5e9b9b84b6c605ac8a28d0d1ab02d01 100644 (file)
@@ -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{