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

Fix end tag detection in liquid tag #733

Merged
merged 1 commit into from
Dec 13, 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
21 changes: 21 additions & 0 deletions Fluid.Tests/ParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1078,5 +1078,26 @@ public void ShouldContinueForLoop()
var template = _parser.Parse(source);
Assert.Equal("12345", template.Render());
}

[Theory]
[InlineData("")]
[InlineData(" ")]
[InlineData(" \n")]
[InlineData(" \n ")]
[InlineData("\n")]
public void ShouldParseLiquidTagWithDifferentSpaces(string spaces)
{
var source = """
{% liquid
for c in (1..3)
echo c
endforSPACE%}SPACE{{chars}}SPACE
""".Replace("SPACE", spaces);

var _parser = new FluidParser();
Assert.True(_parser.TryParse(source, out var template, out var errors), errors);
var rendered = template.Render();
Assert.Contains("123", rendered);
}
}
}
10 changes: 3 additions & 7 deletions Fluid/Parser/TagParsers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -198,28 +198,24 @@ public override bool Parse(ParseContext context, ref ParseResult<TagResult> resu
}
}

private sealed class OutputTagStartParser : Parser<TagResult>, ISeekable
private sealed class OutputTagStartParser : Parser<TagResult>
{
public OutputTagStartParser(bool skipWhiteSpace = false)
{
SkipWhitespace = skipWhiteSpace;
}

public bool CanSeek => true;

public char[] ExpectedChars { get; set; } = ['{'];

public bool SkipWhitespace { get; }

public override bool Parse(ParseContext context, ref ParseResult<TagResult> result)
{
var start = context.Scanner.Cursor.Position;

if (SkipWhitespace)
{
context.SkipWhiteSpace();
}

var start = context.Scanner.Cursor.Position;

if (context.Scanner.ReadChar('{') && context.Scanner.ReadChar('{'))
{
var trim = context.Scanner.ReadChar('-');
Expand Down
Loading