diff --git a/source/Handlebars.Test/PartialTests.cs b/source/Handlebars.Test/PartialTests.cs index 41848ae6..f098691b 100644 --- a/source/Handlebars.Test/PartialTests.cs +++ b/source/Handlebars.Test/PartialTests.cs @@ -994,6 +994,52 @@ public void MultiLinePartialIndentationInsideBlock() // Each 2-line partial gets " " prepended to both lines. Assert.Equal(" - A\n (alpha) - B\n (beta)", result); } + + [Theory] + [InlineData("
\n {{> p}}\n
", "[{{v}}]")] + [InlineData("{{#> p}}[{{v}}]{{/p}}", "
\n {{> @partial-block}}\n
")] + [InlineData("{{#> p}}[{{v}}]{{/p}}", "
\n {{> missing}}\n
")] + public void PartialIndentationKeepsValuesEscaped(string source, string partialSource) + { + var handlebars = Handlebars.Create(); + + using (var reader = new StringReader(partialSource)) + { + handlebars.RegisterTemplate("p", handlebars.Compile(reader)); + } + + var result = handlebars.Compile(source)(new { v = "&" }); + + Assert.Equal("
\n [<b>&]
", result); + } + + [Fact] + public void InlinePartialIndentationKeepsValuesEscaped() + { + var handlebars = Handlebars.Create(); + var source = "{{#*inline \"p\"}}[{{v}}]{{/inline}}
\n {{> p}}\n
"; + + var result = handlebars.Compile(source)(new { v = "&" }); + + Assert.Equal("
\n [<b>&]
", result); + } + + [Fact] + public void PartialIndentationKeepsNoEscape() + { + var handlebars = Handlebars.Create(new HandlebarsConfiguration { NoEscape = true }); + var source = "
\n {{> p}}\n
"; + var partialSource = "[{{v}}]"; + + using (var reader = new StringReader(partialSource)) + { + handlebars.RegisterTemplate("p", handlebars.Compile(reader)); + } + + var result = handlebars.Compile(source)(new { v = "&" }); + + Assert.Equal("
\n [&]
", 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);