From 11fb97ca29792d62f9896f3afa9795a7df8f9f3b Mon Sep 17 00:00:00 2001 From: "stencil-harness[bot]" <353851134+stencil-harness[bot]@users.noreply.github.com> Date: Wed, 25 Nov 2026 01:47:30 -0500 Subject: [PATCH 1/2] fix: HTML-escape values in indented standalone partials The indented-partial rendering paths (registered, inline, @partial-block and partial-block fallback) buffered output through an EncodedTextWriter created with suppressEncoding: true, so {{value}} inside an indented partial was written raw. The buffer now inherits the outer writer's SuppressEncoding, matching the non-indented path (and still honoring NoEscape / triple-stash). Fixes #699 Co-Authored-By: Claude Opus 5.5 --- source/Handlebars.Test/PartialTests.cs | 43 +++++++++++++++++++ .../Translation/Expression/PartialBinder.cs | 8 ++-- 2 files changed, 47 insertions(+), 4 deletions(-) diff --git a/source/Handlebars.Test/PartialTests.cs b/source/Handlebars.Test/PartialTests.cs index 41848ae6..c5337666 100644 --- a/source/Handlebars.Test/PartialTests.cs +++ b/source/Handlebars.Test/PartialTests.cs @@ -994,6 +994,49 @@ 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", " {{#> missing}}\n{{value}}\n {{/missing}}\n")] + public void IndentedPartialEscapesValues(string kind, string source) + { + var handlebars = Handlebars.Create(); + handlebars.RegisterTemplate("p", "{{value}}"); + handlebars.RegisterTemplate("outer", " {{> @partial-block}}\n"); + + 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 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); From f94d944ac4faea337cf5957addbfc294f3dc1e2a Mon Sep 17 00:00:00 2001 From: "stencil-harness[bot]" <353851134+stencil-harness[bot]@users.noreply.github.com> Date: Wed, 25 Nov 2026 01:49:54 -0500 Subject: [PATCH 2/2] test: cover indented fallback and nested partial escaping Exercise the missing-partial fallback branch with an actual indent, add a nested-partial case and the reproduction from the issue report. Co-Authored-By: Claude Opus 5.5 --- source/Handlebars.Test/PartialTests.cs | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/source/Handlebars.Test/PartialTests.cs b/source/Handlebars.Test/PartialTests.cs index c5337666..86e00a52 100644 --- a/source/Handlebars.Test/PartialTests.cs +++ b/source/Handlebars.Test/PartialTests.cs @@ -1001,12 +1001,16 @@ public void MultiLinePartialIndentationInsideBlock() [InlineData("registered", " {{> p}}\n")] [InlineData("inline", "{{#*inline \"p\"}}{{value}}{{/inline}}\n {{> p}}\n")] [InlineData("partial-block", "{{#> outer}}{{value}}{{/outer}}")] - [InlineData("missing-partial-fallback", " {{#> missing}}\n{{value}}\n {{/missing}}\n")] + [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 = "&\"" }); @@ -1027,6 +1031,17 @@ public void IndentedPartialMatchesUnindentedEscaping() 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() {