[go] text/template/parse: fix confusion about markers near right delims

已查看 1 次
跳至第一个未读帖子

Gopher Robot (Gerrit)

未读,
2022年9月23日 11:03:502022/9/23
收件人 Russ Cox、goph...@pubsubhelper.golang.org、golang-...@googlegroups.com、Cherry Mui、Rob Pike、golang-co...@googlegroups.com

Gopher Robot submitted this change.

View Change


Approvals: Cherry Mui: Looks good to me, but someone else must approve Russ Cox: Run TryBots; Automatically submit change Rob Pike: Looks good to me, approved Gopher Robot: TryBots succeeded
text/template/parse: fix confusion about markers near right delims

Fixes #52527.
Fixes #55336.

Change-Id: I8f5c521c693e74451a558788909e7e4ad1cc797a
Reviewed-on: https://go-review.googlesource.com/c/go/+/433036
Run-TryBot: Russ Cox <r...@golang.org>
TryBot-Result: Gopher Robot <go...@golang.org>
Reviewed-by: Rob Pike <r...@golang.org>
Auto-Submit: Russ Cox <r...@golang.org>
Reviewed-by: Cherry Mui <cher...@google.com>
---
M src/text/template/parse/lex.go
M src/text/template/parse/lex_test.go
2 files changed, 36 insertions(+), 1 deletion(-)

diff --git a/src/text/template/parse/lex.go b/src/text/template/parse/lex.go
index 3562e0a..3e60a1e 100644
--- a/src/text/template/parse/lex.go
+++ b/src/text/template/parse/lex.go
@@ -369,7 +369,7 @@

// 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()
diff --git a/src/text/template/parse/lex_test.go b/src/text/template/parse/lex_test.go
index 947889a..d47f10f 100644
--- a/src/text/template/parse/lex_test.go
+++ b/src/text/template/parse/lex_test.go
@@ -491,6 +491,23 @@
}
}

+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{

To view, visit change 433036. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: I8f5c521c693e74451a558788909e7e4ad1cc797a
Gerrit-Change-Number: 433036
Gerrit-PatchSet: 2
Gerrit-Owner: Russ Cox <r...@golang.org>
Gerrit-Reviewer: Cherry Mui <cher...@google.com>
Gerrit-Reviewer: Gopher Robot <go...@golang.org>
Gerrit-Reviewer: Rob Pike <r...@golang.org>
Gerrit-Reviewer: Russ Cox <r...@golang.org>
Gerrit-MessageType: merged
回复全部
回复作者
转发
0 个新帖子