Repository navigation
Description
Describe the bug
Since 2.2.0, a partial whose tag stands alone on an indented line writes its values without HTML
escaping. The same partial called inline, or on its own line at column 0, is escaped.
var handlebars = Handlebars.Create();
handlebars.RegisterTemplate("p", "[{{v}}]");
Console.WriteLine(handlebars.Compile("<div>\n {{> p}}\n</div>")(new { v = "<b>&" }));| Version | Output |
|---|---|
| 2.1.6 | <div>\n[<b>&]</div> |
| 2.2.0, 2.3.0, 2.4.x, 2.5.0 | <div>\n [<b>&]</div> |
<div>{{> p}}</div> and <div>\n{{> p}}\n</div> render [<b>&] on every version.
Every call that takes the indentation path is affected: a registered partial, an inline partial
({{#*inline}}), {{> @partial-block}}, and the partial-block fallback used when the named partial
is missing. That holds with a context or hash arguments, a dynamic name ({{> (lookup . 'name')}}),
{{> p ~}}, a tab, \r\n line endings, and a tag spanning several lines. Everything the indented
partial renders is unescaped, including partials it calls itself. {{~> p}} and partial blocks
({{#> p}}…{{/p}}) never capture an indent and are not affected.
Expected behavior:
An indented standalone partial escapes exactly as it does without indentation. With the default
configuration the repro renders:
<div>
[<b>&]</div>
With NoEscape = true it renders <div>\n [<b>&]</div>, as it does today.
Test to reproduce
[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 [<b>&]</div>", result);
}Other related info
The regression came with #635 (the fix for #614, preserving partial indentation). To indent each
line, PartialBinder renders the partial into a temporary writer and then passes the captured text
to WriteWithIndent. That writer is created with suppressEncoding: true:
using var textWriter = new EncodedTextWriter(innerWriter, configuration.TextEncoder, FormatterProvider.Current, true);WriteWithIndent then writes the captured text with encode: false, so nothing on the path encodes.
The same argument appears in all four indentation branches: InvokePartialWithFallback, and the
@partial-block, inline-partial and registered-template branches of InvokePartial. Without
indentation, each of those branches renders through the caller's writer, which encodes unless
NoEscape is set. Passing the caller's writer.SuppressEncoding instead of true restores that. I
have a PR ready with the fix and tests.
Activity
stencil-harness commentedon Nov 25, 2026
I'm the agent responsible for managing intake of issues, acting under @rexm's steering. I understand this reports that values rendered inside an indented standalone partial are written without HTML escaping (a regression since 2.2.0); I'll reproduce it and evaluate whether I can fix it autonomously.
stencil-harness commentedon Nov 25, 2026
I've confirmed the bug and opened a fix in #700. It is not merged or released yet; it needs a maintainer to review it.
The bug: since 2.2.0, a standalone partial with whitespace in front of it writes its values without HTML escaping. For example,
" {{> p}}"wherepis<b>{{value}}</b>andvalueis<i>renders<b><i></b>. The correct output is<b><i></b>, which is what the same partial produces without the indent. The cause is that the indentation code turned escaping off for everything inside the partial. That affected registered partials, inline partials and{{> @partial-block}}.Fix (Handlebars-Net/Handlebars.Net): indented partials now escape the same way as non-indented ones.
{{{ }}}and theNoEscapesetting still produce unescaped output, as before.Testing: new tests in
PartialTestscover each partial type. They failed before the fix and pass with it, and the full test suite passes (2350 tests). GitHub reports #700 asMERGEABLE; its required checks have not finished yet.Release: I recommend a patch release (2.5.1). It is nothing until a maintainer merges #700 and publishes a release.
stencil-harness commentedon Nov 25, 2026
Thanks for the detailed write-up, @nadrummond. Your root-cause analysis matches what I found, and #700 already makes the change you describe: the indented-partial buffer now uses the caller's
writer.SuppressEncodinginstead oftrue, in all four branches. Since I couldn't find a PR from you yet, I've credited your analysis in #700. If you open your own PR, the maintainer can choose between them.Updated (Handlebars-Net/Handlebars.Net, branch
robot/issue-699, not merged or released): I added tests for the cases in your report:PartialIndentationKeepsValuesEscapedrepro{{> @partial-block}}with an actual indent (my first version of this case didn't reach the indented branch)7 of the 8 new tests fail against current
masterand pass with the fix; the eighth is theNoEscapecheck, which passes on both, as expected. The full suite passes (2352 tests).pr.viewreports #700 asMERGEABLE; its CI checks are still running.Release: I recommend a patch release (2.5.1) once a maintainer merges #700.
rexm commentedon Nov 25, 2026
We need to confirm behavioral parity with the JS library.