Skip to content
Open
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
58 changes: 58 additions & 0 deletions source/Handlebars.Test/PartialTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -994,6 +994,64 @@ public void MultiLinePartialIndentationInsideBlock()
// Each 2-line partial gets " " prepended to both lines.
Assert.Equal(" - A\n (alpha) - B\n (beta)", result);
}

// Indentation must not change escaping: an indented standalone partial escapes {{value}}
// exactly like the same partial written without indentation (regression in 2.2.0, see #699).
[Theory]
[InlineData("registered", " {{> p}}\n")]
[InlineData("inline", "{{#*inline \"p\"}}<b>{{value}}</b>{{/inline}}\n {{> p}}\n")]
[InlineData("partial-block", "{{#> outer}}<b>{{value}}</b>{{/outer}}")]
[InlineData("missing-partial-fallback", "{{#> outerMissing}}<b>{{value}}</b>{{/outerMissing}}")]
[InlineData("nested", " {{> wrapper}}\n")]
public void IndentedPartialEscapesValues(string kind, string source)
{
var handlebars = Handlebars.Create();
handlebars.RegisterTemplate("p", "<b>{{value}}</b>");
handlebars.RegisterTemplate("outer", " {{> @partial-block}}\n");
// "missing" is not registered, so the indented call falls back to the partial block.
handlebars.RegisterTemplate("outerMissing", " {{> missing}}\n");
handlebars.RegisterTemplate("wrapper", "{{> p}}");

var result = handlebars.Compile(source)(new { value = "<i>&\"" });

Assert.True(result.Contains("<b>&lt;i&gt;&amp;&quot;</b>"), $"{kind}: {result}");
}

[Fact]
public void IndentedPartialMatchesUnindentedEscaping()
{
var handlebars = Handlebars.Create();
handlebars.RegisterTemplate("p", "<b>{{value}}</b> {{{value}}}");
var data = new { value = "<i>" };

var indented = handlebars.Compile(" {{> p}}\n")(data);
var unindented = handlebars.Compile("{{> p}}\n")(data);

Assert.Equal(" <b>&lt;i&gt;</b> <i>", indented);
Assert.Equal("<b>&lt;i&gt;</b> <i>", unindented);
}

[Fact]
public void PartialIndentationKeepsValuesEscaped()
{
var handlebars = Handlebars.Create();
handlebars.RegisterTemplate("p", "[{{v}}]");

var result = handlebars.Compile("<div>\n {{> p}}\n</div>")(new { v = "<b>&" });

Assert.Equal("<div>\n [&lt;b&gt;&amp;]</div>", result);
}

[Fact]
public void IndentedPartialHonorsNoEscape()
{
var handlebars = Handlebars.Create(new HandlebarsConfiguration { NoEscape = true });
handlebars.RegisterTemplate("p", "<b>{{value}}</b>");

var result = handlebars.Compile(" {{> p}}\n")(new { value = "<i>" });

Assert.Equal(" <b><i></b>", result);
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ private static void InvokePartialWithFallback(
if (!string.IsNullOrEmpty(indent))
{
using var innerWriter = ReusableStringWriter.Get(writer.UnderlyingWriter.FormatProvider);
using var textWriter = new EncodedTextWriter(innerWriter, configuration.TextEncoder, FormatterProvider.Current, true);
using var textWriter = new EncodedTextWriter(innerWriter, configuration.TextEncoder, FormatterProvider.Current, writer.SuppressEncoding);
context.PartialBlockTemplate(textWriter, context);
WriteWithIndent(writer, innerWriter.ToString(), indent);
}
Expand Down Expand Up @@ -196,7 +196,7 @@ private static bool InvokePartial(
if (!string.IsNullOrEmpty(indent))
{
using var innerWriter = ReusableStringWriter.Get(writer.UnderlyingWriter.FormatProvider);
using var textWriter = new EncodedTextWriter(innerWriter, configuration.TextEncoder, FormatterProvider.Current, true);
using var textWriter = new EncodedTextWriter(innerWriter, configuration.TextEncoder, FormatterProvider.Current, writer.SuppressEncoding);
partialBlockTemplate(textWriter, context);
WriteWithIndent(writer, innerWriter.ToString(), indent);
}
Expand Down Expand Up @@ -227,7 +227,7 @@ void IncreaseDepth()
if (!string.IsNullOrEmpty(indent))
{
using var innerWriter = ReusableStringWriter.Get(writer.UnderlyingWriter.FormatProvider);
using var textWriter = new EncodedTextWriter(innerWriter, configuration.TextEncoder, FormatterProvider.Current, true);
using var textWriter = new EncodedTextWriter(innerWriter, configuration.TextEncoder, FormatterProvider.Current, writer.SuppressEncoding);
partial(textWriter, context);
WriteWithIndent(writer, innerWriter.ToString(), indent);
}
Expand Down Expand Up @@ -261,7 +261,7 @@ void IncreaseDepth()
if (!string.IsNullOrEmpty(indent))
{
using var innerWriter = ReusableStringWriter.Get(writer.UnderlyingWriter.FormatProvider);
using (var encodedInner = new EncodedTextWriter(innerWriter, configuration.TextEncoder, FormatterProvider.Current, true))
using (var encodedInner = new EncodedTextWriter(innerWriter, configuration.TextEncoder, FormatterProvider.Current, writer.SuppressEncoding))
{
using var textWriter = encodedInner.CreateWrapper();
configuration.RegisteredTemplates[partialName]!(textWriter, context);
Expand Down
Loading