Skip to content

Indented standalone partial writes its values unescaped (regression in 2.2.0) #699

In review

Description

@nadrummond

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[&lt;b&gt;&amp;]</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 [&lt;b&gt;&amp;] 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>
    [&lt;b&gt;&amp;]</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    [&lt;b&gt;&amp;]</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

  1. stencil-harness commented on Nov 25, 2026

    @stencil-harness
    Contributor

    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.

  2. stencil-harness commented on Nov 25, 2026

    @stencil-harness
    Contributor

    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}}" where p is <b>{{value}}</b> and value is <i> renders <b><i></b>. The correct output is <b>&lt;i&gt;</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 the NoEscape setting still produce unescaped output, as before.

    Testing: new tests in PartialTests cover each partial type. They failed before the fix and pass with it, and the full test suite passes (2350 tests). GitHub reports #700 as MERGEABLE; 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.

  3. stencil-harness commented on Nov 25, 2026

    @stencil-harness
    Contributor

    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.SuppressEncoding instead of true, 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:

    • your PartialIndentationKeepsValuesEscaped repro
    • the missing-partial fallback to {{> @partial-block}} with an actual indent (my first version of this case didn't reach the indented branch)
    • an indented partial that calls another partial

    7 of the 8 new tests fail against current master and pass with the fix; the eighth is the NoEscape check, which passes on both, as expected. The full suite passes (2352 tests). pr.view reports #700 as MERGEABLE; its CI checks are still running.

    Release: I recommend a patch release (2.5.1) once a maintainer merges #700.

  4. rexm commented on Nov 25, 2026

    @rexm
    Member

    We need to confirm behavioral parity with the JS library.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions