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 27, 2026
We need to confirm behavioral parity with the JS library.
stencil-harness commentedon Nov 27, 2026
I'm unable to proceed further on this and am awaiting follow-up from a human maintainer.
nadrummond commentedon Nov 30, 2026
Handlebars.js escapes values inside an indented partial the same way it escapes them everywhere else. Handlebars.Net 2.5.0 doesn't, and #701 brings it back in line.
By an indented partial I mean a partial tag that sits alone on its line with spaces in front of it, like
{{> p}}. Both libraries copy those spaces onto every line the partial outputs. Since 2.2.0, Handlebars.Net also stops escaping anything inside such a partial, so a value like<b>&lands in the page as real HTML instead of text. Without the leading spaces, the same partial is escaped as normal.Here is the same template and the same value (
v = "<b>&") in each library:{{> @partial-block}}{{{v}}}in an indented partialnoEscapeon{{> missing}}inside a partial blockWith #701, the output of the first seven cases is identical to Handlebars.js character for character, indentation and line breaks included.
The last case is the only difference, and it isn't about escaping. When a partial called inside a partial block can't be found, Handlebars.Net renders the block's content in its place, where Handlebars.js throws. Handlebars.Net has done this since #237. #701 doesn't change that, it only escapes the content it falls back to, like everything else.
Technical detail
Versions: Handlebars.js 4.7.10 (latest on npm), Handlebars.Net 2.5.0 from NuGet, and #701 at 4b59000. Every case renders
{ v: "<b>&" }with a fresh instance, andpis registered as a partial where a case has one.Handlebars.js renders the partial with escaping as usual, then
invokePartialWrapper(lib/handlebars/runtime.js) splits the finished output on\nand puts the indent in front of each line. The indent is added after escaping, so it can't affect it.Since #635, Handlebars.Net renders an indented standalone partial into a temporary
EncodedTextWriterso thatWriteWithIndentcan indent each line. That writer is created withsuppressEncoding: true, andWriteWithIndentwrites the captured text withencode: false, so nothing the partial outputs is encoded.#701 creates the temporary writer with the caller's
writer.SuppressEncodingin all four indentation branches ofPartialBinder:InvokePartialWithFallback, and the@partial-block, inline-partial and registered-template branches ofInvokePartial. Values are encoded while the partial renders and the indent is added afterwards, the same order as Handlebars.js.WriteWithIndentis unchanged, which is why the indentation and line breaks above match exactly.The fallback in the last case is
InvokePartialWithFallback: when the partial isn't registered and the context has aPartialBlockTemplate, it renders that template instead of throwing. Handlebars.js only falls back for a block-form call,{{#> missing}}…{{/missing}}, using that call's own block, so a plain{{> missing}}throws.stencil-harness commentedon Nov 30, 2026
Parity with handlebars.js is confirmed for #700. For every escaping case it now produces the same output as handlebars.js 4.7.10, character for character, indentation included.
invokePartialWrapperrenders the partial with normal escaping, then adds the indent to each line of the finished string. That is the same order the fix gives Handlebars.Net, so the JS outputs @nadrummond reported above are what that code produces.masterand on fix: HTML-escape values in indented standalone partials #700. My outputs match the 2.5.0 and fix: keep values escaped in indented standalone partials #701 columns exactly.{{> missing}}inside{{#> p}}…{{/p}}throws in handlebars.js. Handlebars.Net renders the block instead, with or without indentation. fix: HTML-escape values in indented standalone partials #700 only makes that fallback output escaped. Matching JS there would turn a render into an exception, which is a breaking change. That's a separate decision for a maintainer; I haven't touched it.#700 vs #701: both change the same four lines of
PartialBinder.cs. #700's tests also cover a nested partial,{{{v}}}, indented vs. non-indented output, and a multi-line partial (added this session, from the parity table). Either PR fixes this issue; only one should be merged. I recommend #700 for the wider coverage. #701 also needs an update frommaster.Changed (Handlebars-Net/Handlebars.Net, branch
robot/issue-699, not merged or released): merged currentmasterand added one test.dotnet test: 2358 passed, 0 failed.pr.viewreports #700 asMERGEABLE; CI checks are still running. Recommend a patch release (2.5.1) after merging.