From 4b59000fcda733f8f2ff842394ba2733b754bddb Mon Sep 17 00:00:00 2001 From: Noor Drummond Date: Wed, 25 Nov 2026 06:39:09 +0000 Subject: [PATCH] fix: keep values escaped in indented standalone partials An indented standalone partial renders into a temporary writer so each line can be indented. That writer was created with encoding suppressed, and the captured text is written on without encoding, so the partial's values were never escaped. The temporary writer now takes the caller's SuppressEncoding, as the partial would without indentation. Co-Authored-By: Claude Opus 5.5 --- source/Handlebars.Test/PartialTests.cs | 46 +++++++++++++++++++ .../Translation/Expression/PartialBinder.cs | 8 ++-- 2 files changed, 50 insertions(+), 4 deletions(-) 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);