diff --git a/parser/v2/elementparser.go b/parser/v2/elementparser.go
index 478d17621..63b1e680e 100644
--- a/parser/v2/elementparser.go
+++ b/parser/v2/elementparser.go
@@ -407,7 +407,7 @@ func (elementParser) Parse(pi *parse.Input) (n Node, ok bool, err error) {
// Void elements _might_ have children, even though it's invalid.
// We want to allow this to be parsed.
closer := StripType(parse.All(parse.String(""), parse.String(ot.Name), parse.Rune('>')))
- tnp := newTemplateNodeParser[any](closer, fmt.Sprintf("<%s>: close tag", ot.Name))
+ tnp := newTemplateNodeParser(closer, fmt.Sprintf("<%s>: close tag", ot.Name))
nodes, _, err := tnp.Parse(pi)
if err != nil {
notFoundErr, isNotFoundError := err.(UntilNotFoundError)
diff --git a/parser/v2/elementparser_test.go b/parser/v2/elementparser_test.go
index 2ca10c1d4..37c8d5c74 100644
--- a/parser/v2/elementparser_test.go
+++ b/parser/v2/elementparser_test.go
@@ -1506,6 +1506,29 @@ func TestElementParser(t *testing.T) {
},
},
},
+ {
+ name: "element: can contain text that starts with for",
+ input: `
for which any
+amount is charged
`,
+ expected: Element{
+ Name: "div",
+ IndentChildren: true,
+ NameRange: Range{
+ From: Position{Index: 1, Line: 0, Col: 1},
+ To: Position{Index: 4, Line: 0, Col: 4},
+ },
+ Children: []Node{
+ Text{
+ Value: "for which any ",
+ TrailingSpace: SpaceVertical,
+ },
+ Text{
+ Value: "amount is charged",
+ TrailingSpace: SpaceNone,
+ },
+ },
+ },
+ },
}
for _, tt := range tests {
tt := tt
diff --git a/parser/v2/forexpressionparser.go b/parser/v2/forexpressionparser.go
index c8f0236fb..3f8bc2ec1 100644
--- a/parser/v2/forexpressionparser.go
+++ b/parser/v2/forexpressionparser.go
@@ -29,8 +29,8 @@ func (forExpressionParser) Parse(pi *parse.Input) (n Node, ok bool, err error) {
// Eat " {\n".
if _, ok, err = parse.All(openBraceWithOptionalPadding, parse.NewLine).Parse(pi); err != nil || !ok {
- err = parse.Error("for: "+unterminatedMissingCurly, pi.PositionAt(start))
- return
+ pi.Seek(start)
+ return r, false, err
}
// Node contents.
diff --git a/parser/v2/forexpressionparser_test.go b/parser/v2/forexpressionparser_test.go
index 793bb6f3f..4a012e7b2 100644
--- a/parser/v2/forexpressionparser_test.go
+++ b/parser/v2/forexpressionparser_test.go
@@ -140,11 +140,14 @@ func TestForExpressionParser(t *testing.T) {
func TestIncompleteFor(t *testing.T) {
t.Run("no opening brace", func(t *testing.T) {
- input := parse.NewInput(`for with no brace`)
- _, _, err := forExpression.Parse(input)
- if err.Error() != "for: unterminated (missing closing '{\\n') - https://templ.guide/syntax-and-usage/statements#incomplete-statements: line 0, col 0" {
+ input := parse.NewInput(`for with no brace is ignored`)
+ _, ok, err := forExpression.Parse(input)
+ if err != nil {
t.Fatalf("unexpected error: %v", err)
}
+ if ok {
+ t.Fatal("expected a non match, but got a match")
+ }
})
t.Run("capitalised For", func(t *testing.T) {
input := parse.NewInput(`For with no brace`)
@@ -153,7 +156,7 @@ func TestIncompleteFor(t *testing.T) {
t.Fatalf("unexpected error: %v", err)
}
if ok {
- t.Fatal("expected a non match")
+ t.Fatal("expected a non match, but got a match")
}
})
}