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 27, 2026

    @rexm
    Member

    We need to confirm behavioral parity with the JS library.

  5. stencil-harness commented on Nov 27, 2026

    @stencil-harness
    Contributor

    I'm unable to proceed further on this and am awaiting follow-up from a human maintainer.

  6. nadrummond commented on Nov 30, 2026

    @nadrummond
    Author

    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:

    Case Handlebars.js 4.7.10 Handlebars.Net 2.5.0 Handlebars.Net with #701
    Partial on an indented line escaped not escaped escaped
    Indented {{> @partial-block}} escaped not escaped escaped
    Indented inline partial escaped not escaped escaped
    Two-line partial on an indented line escaped not escaped escaped
    {{{v}}} in an indented partial raw, as intended raw, as intended raw, as intended
    Indented partial with noEscape on raw, as intended raw, as intended raw, as intended
    Same partial, not indented escaped escaped escaped
    Indented {{> missing}} inside a partial block throws "partial not found" renders the block, not escaped renders the block, escaped

    With #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, and p is registered as a partial where a case has one.

    Partial on an indented line
      template       "<div>\n    {{> p}}\n</div>"
      partial p      "[{{v}}]"
      Handlebars.js  "<div>\n    [&lt;b&gt;&amp;]</div>"
      2.5.0          "<div>\n    [<b>&]</div>"
      #701           "<div>\n    [&lt;b&gt;&amp;]</div>"
    
    Indented {{> @partial-block}}
      template       "{{#> p}}[{{v}}]{{/p}}"
      partial p      "<div>\n    {{> @partial-block}}\n</div>"
      Handlebars.js  "<div>\n    [&lt;b&gt;&amp;]</div>"
      2.5.0          "<div>\n    [<b>&]</div>"
      #701           "<div>\n    [&lt;b&gt;&amp;]</div>"
    
    Indented inline partial
      template       "{{#*inline \"p\"}}[{{v}}]{{/inline}}<div>\n    {{> p}}\n</div>"
      Handlebars.js  "<div>\n    [&lt;b&gt;&amp;]</div>"
      2.5.0          "<div>\n    [<b>&]</div>"
      #701           "<div>\n    [&lt;b&gt;&amp;]</div>"
    
    Two-line partial on an indented line
      template       "<div>\n    {{> p}}\n</div>"
      partial p      "[{{v}}]\n({{v}})"
      Handlebars.js  "<div>\n    [&lt;b&gt;&amp;]\n    (&lt;b&gt;&amp;)</div>"
      2.5.0          "<div>\n    [<b>&]\n    (<b>&)</div>"
      #701           "<div>\n    [&lt;b&gt;&amp;]\n    (&lt;b&gt;&amp;)</div>"
    
    {{{v}}} in an indented partial
      template       "<div>\n    {{> p}}\n</div>"
      partial p      "[{{{v}}}]"
      Handlebars.js  "<div>\n    [<b>&]</div>"
      2.5.0          "<div>\n    [<b>&]</div>"
      #701           "<div>\n    [<b>&]</div>"
    
    Indented partial with noEscape on (noEscape: true / NoEscape = true)
      template       "<div>\n    {{> p}}\n</div>"
      partial p      "[{{v}}]"
      Handlebars.js  "<div>\n    [<b>&]</div>"
      2.5.0          "<div>\n    [<b>&]</div>"
      #701           "<div>\n    [<b>&]</div>"
    
    Same partial, not indented
      template       "<div>\n{{> p}}\n</div>"
      partial p      "[{{v}}]"
      Handlebars.js  "<div>\n[&lt;b&gt;&amp;]</div>"
      2.5.0          "<div>\n[&lt;b&gt;&amp;]</div>"
      #701           "<div>\n[&lt;b&gt;&amp;]</div>"
    
    Indented {{> missing}} inside a partial block
      template       "{{#> p}}[{{v}}]{{/p}}"
      partial p      "<div>\n    {{> missing}}\n</div>"
      Handlebars.js  throws: The partial missing could not be found
      2.5.0          "<div>\n    [<b>&]</div>"
      #701           "<div>\n    [&lt;b&gt;&amp;]</div>"
    

    Handlebars.js renders the partial with escaping as usual, then invokePartialWrapper (lib/handlebars/runtime.js) splits the finished output on \n and 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 EncodedTextWriter so that WriteWithIndent can indent each line. That writer is created with suppressEncoding: true, and WriteWithIndent writes the captured text with encode: false, so nothing the partial outputs is encoded.

    #701 creates the temporary writer with the caller's writer.SuppressEncoding in all four indentation branches of PartialBinder: InvokePartialWithFallback, and the @partial-block, inline-partial and registered-template branches of InvokePartial. Values are encoded while the partial renders and the indent is added afterwards, the same order as Handlebars.js. WriteWithIndent is 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 a PartialBlockTemplate, 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.

  7. stencil-harness commented on Nov 30, 2026

    @stencil-harness
    Contributor

    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.

    #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 from master.

    Changed (Handlebars-Net/Handlebars.Net, branch robot/issue-699, not merged or released): merged current master and added one test. dotnet test: 2358 passed, 0 failed. pr.view reports #700 as MERGEABLE; CI checks are still running. Recommend a patch release (2.5.1) after merging.

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