Skip to content
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
29 changes: 29 additions & 0 deletions source/Handlebars.Test/IssueTests.cs
Original file line numberDiff line numberDiff line change
Expand Up@@ -734,6 +734,35 @@ public void UnrecognisedExpressionThrowsOutOfMemoryException()
Assert.Throws<HandlebarsCompilerException>(()=> Handlebars.Compile(source));
}

// Issue: https://github.com/Handlebars-Net/Handlebars.Net/issues/584
[Fact]
public void Issue584_EscapedDoubleQuoteInHelperStringArgument()
{
var handlebars = Handlebars.Create();
handlebars.RegisterHelper("myHelper", (writer, context, args) =>
{
writer.WriteSafeString(args[0]?.ToString() + "|" + args[1]?.ToString());
});

// Double-quoted string arg with escaped double-quote inside
var template = handlebars.Compile("{{myHelper name \"hello \\\"world\\\"\"}}");
var data = new { name = "test" };
var result = template(data);
Assert.Equal("test|hello \"world\"", result);
}

// Issue: https://github.com/Handlebars-Net/Handlebars.Net/issues/584
[Fact]
public void Issue584_SingleQuoteStringArgStillWorks()
{
var handlebars = Handlebars.Create();
handlebars.RegisterHelper("myHelper", (context, args) => args[0]?.ToString());

var template = handlebars.Compile("{{myHelper 'hello world'}}");
var result = template(new { });
Assert.Equal("hello world", result);
}

// Issue: https://github.com/Handlebars-Net/Handlebars.Net/issues/466
// Dictionary keys were inaccessible in nested #each when data is a mix of Dictionary and List
[Fact]
Expand Down
24 changes: 23 additions & 1 deletion source/Handlebars/Compiler/Lexer/Parsers/LiteralParser.cs
Original file line numberDiff line numberDiff line change
Expand Up@@ -36,7 +36,7 @@
return char.IsDigit(peek) || peek == '-';
}

private static string AccumulateLiteral(ExtendedStringReader reader, bool captureDelimiter, params char[] delimiters)

Check failure on line 39 in source/Handlebars/Compiler/Lexer/Parsers/LiteralParser.cs

View check run for this annotation

SonarQubeCloud/ SonarCloud Code Analysis

Refactor this method to reduce its Cognitive Complexity from 20 to the 15 allowed.

See more on https://sonarcloud.io/project/issues?id=Handlebars-Net_Handlebars.Net&issues=AZ7iiUhQkYFgzP1s47AU&open=AZ7iiUhQkYFgzP1s47AU&pullRequest=621
{
using(var container = StringBuilderPool.Shared.Use())
{
Expand All@@ -49,6 +49,28 @@
throw new HandlebarsParserException("Reached end of template before the expression was closed.", reader.GetContext());
}

// Handle escape sequences inside delimited literals (e.g. \" inside "...")
if (captureDelimiter && (char)node == '\\')
{
reader.Read(); // consume the backslash
var next = reader.Peek();
if (next == -1)
{
throw new HandlebarsParserException("Reached end of template before the expression was closed.", reader.GetContext());
}
var nextChar = (char)next;
// If the escaped character is one of the delimiters, emit the raw char
if (delimiters.Contains(nextChar))
{
reader.Read();
buffer.Append(nextChar);
continue;
}
// Otherwise keep the backslash and let the next iteration handle the char
buffer.Append('\\');
continue;
}

if (delimiters.Contains((char)node))
{
if (captureDelimiter)
Expand All@@ -65,7 +87,7 @@

buffer.Append((char)reader.Read());
}

return buffer.ToString();
}
}
Expand Down
Loading