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

Allow slashes properly within escape blocks #567

Merged
merged 8 commits into from
Apr 1, 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
5 changes: 4 additions & 1 deletion source/Handlebars.Test/PathInfoTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ public void DotPath()
[InlineData("a/[b.c].[b/c]/d", new [] {"a", "[b.c].[b/c]", "d"})]
[InlineData("a/[b/c]/d", new [] {"a", "[b/c]", "d"})]
[InlineData("a/[b.c/d]/e", new [] {"a", "[b.c/d]", "e"})]
[InlineData("a/[b.c/d/e/f]/e", new [] {"a", "[b.c/d/e/f]", "e"})]
[InlineData("a/[a//b/c.d/e]/e", new [] {"a", "[a//b/c.d/e]", "e"})]
[InlineData("a/[b/c/d].[e/f/g]/h", new [] {"a", "[b/c/d].[e/f/g]", "h"})]
public void SlashPath(string input, string[] expected)
{
var pathInfo = PathInfo.Parse(input);
Expand All @@ -62,4 +65,4 @@ public void SlashPath(string input, string[] expected)
}
}
}
}
}
9 changes: 6 additions & 3 deletions source/Handlebars/PathStructure/PathInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ public static PathInfo Parse(string path)
var extendedEnumerator = ExtendedEnumerator<Substring>.Create(pathParts);
using var container = StringBuilderPool.Shared.Use();
var buffer = container.Value;
var bufferHasOpenEscapeBlock = false;

while (extendedEnumerator.MoveNext())
{
Expand All @@ -159,6 +160,7 @@ public static PathInfo Parse(string path)
if(Substring.LastIndexOf(segment, ']', out var index)
&& !Substring.LastIndexOf(segment, '[', index, out _))
{
bufferHasOpenEscapeBlock = false;
var chainSegment = GetPathChain(buffer.ToString());
if (chainSegment.Length > 1) isValidHelperLiteral = false;

Expand All @@ -171,7 +173,8 @@ public static PathInfo Parse(string path)
if(Substring.LastIndexOf(segment, '[', out var startIndex)
&& !Substring.LastIndexOf(segment, ']', startIndex, out _))
{
buffer.Append(in segment);
if (!bufferHasOpenEscapeBlock) buffer.Append(in segment);
bufferHasOpenEscapeBlock = true;
continue;
}

Expand All @@ -192,7 +195,7 @@ public static PathInfo Parse(string path)

if (chainSegments.Length > 1 && pathType != PathType.BlockHelper) isValidHelperLiteral = false;

segments.Add(new PathSegment(segment, chainSegments));
if (!bufferHasOpenEscapeBlock) segments.Add(new PathSegment(segment, chainSegments));
}

if (isValidHelperLiteral && segments.Count > 1) isValidHelperLiteral = false;
Expand Down Expand Up @@ -252,4 +255,4 @@ private static PathType GetPathType(string path)
};
}
}
}
}
Loading