Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: allow for in flowing text if for statement not found #777

Merged
merged 1 commit into from
Aug 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -1506,6 +1506,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")
}
})
}
Loading