Skip to content

Commit

Permalink
Merge pull request #567 from Hoeksema/patch-1
Browse files Browse the repository at this point in the history
Allow slashes properly within escape blocks
  • Loading branch information
oformaniuk authored Apr 1, 2024
2 parents dd9c16f + 435296b commit 9fc63f8
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
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)
};
}
}
}
}

0 comments on commit 9fc63f8

Please sign in to comment.