Skip to content

Commit

Permalink
refactor: allow for in flowing text if for statement not found (#777)
Browse files Browse the repository at this point in the history
  • Loading branch information
a-h authored Aug 18, 2024
1 parent ef4dde6 commit 4c14ab0
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 7 deletions.
2 changes: 1 addition & 1 deletion parser/v2/elementparser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
23 changes: 23 additions & 0 deletions parser/v2/elementparser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1536,6 +1536,29 @@ func TestElementParser(t *testing.T) {
},
},
},
{
name: "element: can contain text that starts with for",
input: `<div>for which any
amount is charged</div>`,
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
Expand Down
4 changes: 2 additions & 2 deletions parser/v2/forexpressionparser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
11 changes: 7 additions & 4 deletions parser/v2/forexpressionparser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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`)
Expand All @@ -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")
}
})
}

0 comments on commit 4c14ab0

Please sign in to comment.