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 empty statements parsing #593

Merged
merged 1 commit into from
Sep 26, 2023
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
20 changes: 20 additions & 0 deletions Fluid.Tests/ParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,26 @@ public void ShouldParseRawWithBlocks()
Assert.Equal(" {%if true%} {%endif%} ", (statements.ElementAt(0) as RawStatement).Text.ToString());
}

[Fact]
public void ShouldParseEmptyRawTags()
{
var statements = Parse(@"{% raw %}{% endraw %}");

Assert.Single(statements);
Assert.IsType<RawStatement>(statements.ElementAt(0));
Assert.Equal("", (statements.ElementAt(0) as RawStatement).Text.ToString());
}

[Fact]
public void ShouldParseEmptyCommentTags()
{
var statements = Parse(@"{% comment %}{% endcomment %}");

Assert.Single(statements);
Assert.IsType<CommentStatement>(statements.ElementAt(0));
Assert.Equal("", (statements.ElementAt(0) as CommentStatement).Text.ToString());
}

[Fact]
public void ShouldParseComment()
{
Expand Down
10 changes: 5 additions & 5 deletions Fluid/FluidParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -250,20 +250,20 @@ public FluidParser(FluidParserOptions parserOptions)
var BreakTag = TagEnd.Then<Statement>(x => new BreakStatement()).ElseError("Invalid 'break' tag");
var ContinueTag = TagEnd.Then<Statement>(x => new ContinueStatement()).ElseError("Invalid 'continue' tag");
var CommentTag = TagEnd
.SkipAnd(AnyCharBefore(CreateTag("endcomment")))
.SkipAnd(AnyCharBefore(CreateTag("endcomment"), canBeEmpty: true))
.AndSkip(CreateTag("endcomment").ElseError($"'{{% endcomment %}}' was expected"))
.Then<Statement>(x => new CommentStatement(x))
.ElseError("Invalid 'comment' tag")
;
var CaptureTag = Identifier
var CaptureTag = Identifier.ElseError(string.Format(ErrorMessages.IdentifierAfterTag, "capture"))
.AndSkip(TagEnd)
.And(AnyTagsList)
.AndSkip(CreateTag("endcapture").ElseError($"'{{% endcapture %}}' was expected"))
.Then<Statement>(x => new CaptureStatement(x.Item1, x.Item2))
.ElseError("Invalid 'capture' tag")
;
var MacroTag = Identifier.ElseError(ErrorMessages.IdentifierAfterMacro)
.AndSkip(LParen).ElseError(ErrorMessages.IdentifierAfterMacro)
var MacroTag = Identifier.ElseError(string.Format(ErrorMessages.IdentifierAfterTag, "macro"))
.AndSkip(LParen).ElseError(string.Format(ErrorMessages.IdentifierAfterTag, "macro"))
.And(FunctionDefinitionArgumentsList)
.AndSkip(RParen)
.AndSkip(TagEnd)
Expand Down Expand Up @@ -309,7 +309,7 @@ public FluidParser(FluidParserOptions parserOptions)
.ElseError("Invalid 'render' tag")
;

var RawTag = TagEnd.SkipAnd(AnyCharBefore(CreateTag("endraw"), consumeDelimiter: true, failOnEof: true).Then<Statement>(x => new RawStatement(x))).ElseError("Not end tag found for {% raw %}");
var RawTag = TagEnd.SkipAnd(AnyCharBefore(CreateTag("endraw"), canBeEmpty: true, consumeDelimiter: true, failOnEof: true).Then<Statement>(x => new RawStatement(x))).ElseError("Not end tag found for {% raw %}");
var AssignTag = Identifier.Then(x => x).ElseError(ErrorMessages.IdentifierAfterAssign).AndSkip(Equal.ElseError(ErrorMessages.EqualAfterAssignIdentifier)).And(FilterExpression).AndSkip(TagEnd.ElseError(ErrorMessages.ExpectedTagEnd)).Then<Statement>(x => new AssignStatement(x.Item1, x.Item2));
var IfTag = LogicalExpression
.AndSkip(TagEnd)
Expand Down
7 changes: 5 additions & 2 deletions Fluid/Parser/ErrorMessages.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace Fluid.Parser
using System;

namespace Fluid.Parser
{
public static class ErrorMessages
{
Expand All @@ -11,7 +13,8 @@ public static class ErrorMessages
public const string ExpectedOutputEnd = "End of tag '}}' was expected";
public const string ExpectedStringRender = "A quoted string value is required for the render tag";
public const string FunctionsNotAllowed = "Functions are not allowed";
public const string IdentifierAfterMacro = "An identifier was expected after the 'macro' tag";
[Obsolete] public const string IdentifierAfterMacro = "An identifier was expected after the 'macro' tag";
public const string IdentifierAfterTag = "An identifier was expected after the '{0}' tag";
public const string ParentesesAfterFunctionName = "Start of arguments '(' is expected after a function name";
}
}