Skip to content

Commit

Permalink
Fix end tag detection in liquid tag (#733)
Browse files Browse the repository at this point in the history
Fixes #731
  • Loading branch information
sebastienros authored Dec 13, 2024
1 parent bd80b7b commit 457e25c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
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

0 comments on commit 457e25c

Please sign in to comment.