Skip to content

Commit 4c14ab0

Browse files
authoredAug 18, 2024··
refactor: allow for in flowing text if for statement not found (#777)
1 parent ef4dde6 commit 4c14ab0

File tree

4 files changed

+33
-7
lines changed

4 files changed

+33
-7
lines changed
 

‎parser/v2/elementparser.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ func (elementParser) Parse(pi *parse.Input) (n Node, ok bool, err error) {
407407
// Void elements _might_ have children, even though it's invalid.
408408
// We want to allow this to be parsed.
409409
closer := StripType(parse.All(parse.String("</"), parse.String(ot.Name), parse.Rune('>')))
410-
tnp := newTemplateNodeParser[any](closer, fmt.Sprintf("<%s>: close tag", ot.Name))
410+
tnp := newTemplateNodeParser(closer, fmt.Sprintf("<%s>: close tag", ot.Name))
411411
nodes, _, err := tnp.Parse(pi)
412412
if err != nil {
413413
notFoundErr, isNotFoundError := err.(UntilNotFoundError)

‎parser/v2/elementparser_test.go

+23
Original file line numberDiff line numberDiff line change
@@ -1536,6 +1536,29 @@ func TestElementParser(t *testing.T) {
15361536
},
15371537
},
15381538
},
1539+
{
1540+
name: "element: can contain text that starts with for",
1541+
input: `<div>for which any
1542+
amount is charged</div>`,
1543+
expected: Element{
1544+
Name: "div",
1545+
IndentChildren: true,
1546+
NameRange: Range{
1547+
From: Position{Index: 1, Line: 0, Col: 1},
1548+
To: Position{Index: 4, Line: 0, Col: 4},
1549+
},
1550+
Children: []Node{
1551+
Text{
1552+
Value: "for which any ",
1553+
TrailingSpace: SpaceVertical,
1554+
},
1555+
Text{
1556+
Value: "amount is charged",
1557+
TrailingSpace: SpaceNone,
1558+
},
1559+
},
1560+
},
1561+
},
15391562
}
15401563
for _, tt := range tests {
15411564
tt := tt

‎parser/v2/forexpressionparser.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ func (forExpressionParser) Parse(pi *parse.Input) (n Node, ok bool, err error) {
2929

3030
// Eat " {\n".
3131
if _, ok, err = parse.All(openBraceWithOptionalPadding, parse.NewLine).Parse(pi); err != nil || !ok {
32-
err = parse.Error("for: "+unterminatedMissingCurly, pi.PositionAt(start))
33-
return
32+
pi.Seek(start)
33+
return r, false, err
3434
}
3535

3636
// Node contents.

‎parser/v2/forexpressionparser_test.go

+7-4
Original file line numberDiff line numberDiff line change
@@ -140,11 +140,14 @@ func TestForExpressionParser(t *testing.T) {
140140

141141
func TestIncompleteFor(t *testing.T) {
142142
t.Run("no opening brace", func(t *testing.T) {
143-
input := parse.NewInput(`for with no brace`)
144-
_, _, err := forExpression.Parse(input)
145-
if err.Error() != "for: unterminated (missing closing '{\\n') - https://templ.guide/syntax-and-usage/statements#incomplete-statements: line 0, col 0" {
143+
input := parse.NewInput(`for with no brace is ignored`)
144+
_, ok, err := forExpression.Parse(input)
145+
if err != nil {
146146
t.Fatalf("unexpected error: %v", err)
147147
}
148+
if ok {
149+
t.Fatal("expected a non match, but got a match")
150+
}
148151
})
149152
t.Run("capitalised For", func(t *testing.T) {
150153
input := parse.NewInput(`For with no brace`)
@@ -153,7 +156,7 @@ func TestIncompleteFor(t *testing.T) {
153156
t.Fatalf("unexpected error: %v", err)
154157
}
155158
if ok {
156-
t.Fatal("expected a non match")
159+
t.Fatal("expected a non match, but got a match")
157160
}
158161
})
159162
}

0 commit comments

Comments
 (0)
Please sign in to comment.