diff --git a/source/Handlebars.Test/PartialTests.cs b/source/Handlebars.Test/PartialTests.cs index 41848ae6..86e00a52 100644 --- a/source/Handlebars.Test/PartialTests.cs +++ b/source/Handlebars.Test/PartialTests.cs @@ -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\"}}{{value}}{{/inline}}\n {{> p}}\n")] + [InlineData("partial-block", "{{#> outer}}{{value}}{{/outer}}")] + [InlineData("missing-partial-fallback", "{{#> outerMissing}}{{value}}{{/outerMissing}}")] + [InlineData("nested", " {{> wrapper}}\n")] + public void IndentedPartialEscapesValues(string kind, string source) + { + var handlebars = Handlebars.Create(); + handlebars.RegisterTemplate("p", "{{value}}"); + 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 = "&\"" }); + + Assert.True(result.Contains("<i>&""), $"{kind}: {result}"); + } + + [Fact] + public void IndentedPartialMatchesUnindentedEscaping() + { + var handlebars = Handlebars.Create(); + handlebars.RegisterTemplate("p", "{{value}} {{{value}}}"); + var data = new { value = "" }; + + var indented = handlebars.Compile(" {{> p}}\n")(data); + var unindented = handlebars.Compile("{{> p}}\n")(data); + + Assert.Equal(" <i> ", indented); + Assert.Equal("<i> ", unindented); + } + + [Fact] + public void PartialIndentationKeepsValuesEscaped() + { + var handlebars = Handlebars.Create(); + handlebars.RegisterTemplate("p", "[{{v}}]"); + + var result = handlebars.Compile("
\n {{> p}}\n
")(new { v = "&" }); + + Assert.Equal("
\n [<b>&]
", result); + } + + [Fact] + public void IndentedPartialHonorsNoEscape() + { + var handlebars = Handlebars.Create(new HandlebarsConfiguration { NoEscape = true }); + handlebars.RegisterTemplate("p", "{{value}}"); + + var result = handlebars.Compile(" {{> p}}\n")(new { value = "" }); + + Assert.Equal(" ", result); + } } } diff --git a/source/Handlebars/Compiler/Translation/Expression/PartialBinder.cs b/source/Handlebars/Compiler/Translation/Expression/PartialBinder.cs index 399e9c96..81c70fb8 100644 --- a/source/Handlebars/Compiler/Translation/Expression/PartialBinder.cs +++ b/source/Handlebars/Compiler/Translation/Expression/PartialBinder.cs @@ -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); } @@ -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); } @@ -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); } @@ -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);