Skip to content

fix: match Handlebars.js for quoted-string paths, numeric partial names and {{#.}} sections - #703

Open
stencil-harness[bot] wants to merge 4 commits into
masterfrom
robot/issue-702
Open

stencil-harness[bot] wants to merge 4 commits into
masterfrom
robot/issue-702

Conversation

@stencil-harness

Copy link
Copy Markdown
Contributor

Refs #702. This fixes three of the six Handlebars.js 4.7.10 spec differences reported there. The other three are written up as known gaps and need a maintainer decision, so the issue stays open.

Fixed

Template Data Before After (= Handlebars.js)
{{'a\b'}} { "a\b": "c" } "" c
{{'foo bar'}} / {{"foo bar"}} { "foo bar": "baz" } "" baz
Dudes: {{> 404}} partial 404 = {{name}}, name = Jeepers InvalidCastException Dudes: Jeepers
{{#.}}{{name}}{{/.}} ×3 [{name:"John Doe"},{name:"Jane Doe"}] HandlebarsCompilerException John DoeJane Doe ×3

The before and after values come from running these templates against master (2939312) and against this branch.

  • Quoted-string paths (LoneStringLiteralConverter, new): PathBinder evaluated a mustache whose only content is a string literal and then threw the value away, so {{'…'}} always rendered empty. Handlebars.js treats {{"foo bar"}} as the one-segment path [foo bar]. The new converter rewrites the literal to exactly that: it becomes a helper call when a helper with that name is registered, and a property lookup otherwise. {{'a.b'}} looks up the key a.b and does not walk the nested path. The converter runs after RawHelperAccumulator, so {{{{raw}}}}{{'x'}}{{{{/raw}}}} still outputs the text verbatim. Two kinds of literal are left alone, empty ones and ones containing ], because neither can be written as a segment literal.
  • Numeric and boolean partial names (PartialConverter): undelimited names such as 404 or true had been converted to int/bool constants and then cast to string. Their invariant-culture text is now used as the partial name.
  • {{#.}} / {{^.}} (HelperConverter): these are accepted as block openers in the same way as {{#this}} / {{^this}}.

Backward compatibility

  • The partial and {{#.}} fixes only affect templates that currently throw.
  • The quoted-string fix does change output. A template containing a lone {{'x'}} used to render nothing. It now renders the value of x from the model, or calls a registered helper x. Because the old output was always empty, any template using this form intentionally was already broken. I think a patch release is reasonable, but this is the one item to check.

Not changed (documented as gaps in HandlebarsSpec.md, Gaps 5 and 6)

The current behavior is locked in by regression tests Section_ZeroValueSkipsBlock_SpecGap and HelperMissing_ExplicitCallRendersEmpty_SpecGap.

  1. {{#foo}} with foo = 0. Handlebars.js renders the block with 0 as the context (This is ~). Handlebars.Net skips it (""). Changing this would break templates like {{#unreadCount}}You have {{.}} new messages{{/unreadCount}}, which currently render nothing at 0 and would start rendering You have 0 new messages. Recommendation: leave it as is, or add an opt-in compatibility flag. Changing the default would need a major version.
  2. Explicit {{helperMissing}} / {{#helperMissing}}{{/helperMissing}}. Handlebars.js throws unless allowExplicitCallOfHelperMissing is set. Handlebars.Net renders "". Throwing by default would break templates that call a custom missing-helper hook directly. With the default hooks a direct call renders nothing, so the security reason behind the JS restriction has little force here. Recommendation: document it as an intentional difference. If parity is wanted, add an opt-in option (minor version).

Verification

  • dotnet test source/Handlebars.Test/Handlebars.Test.csproj: 2384 passed, 0 failed. Each new test runs against all five HandlebarsEnvGenerator configurations.
  • dotnet build source/Handlebars/Handlebars.csproj succeeds for netstandard2.0, netstandard2.1, net8.0 and net10.0.

No existing open PR or branch was linked to #702.

🤖 Generated with Claude Code

Closes #702

stencil-harness Bot and others added 4 commits November 25, 2026 07:33
Undelimited partial names were typed as int/bool literals and failed with
InvalidCastException at render time; use their text as the partial name,
as Handlebars.js does.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
{{#.}} failed to compile; treat it like {{#this}}, as Handlebars.js does.
Also exposes the registered-helper check for reuse.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A mustache containing only a quoted string always rendered empty.
Handlebars.js resolves it as a single literal path segment; do the same
by rewriting it to the equivalent segment literal. Raw blocks are
unaffected because the rewrite runs after raw content is collected.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…emaining gaps

Adds coverage for quoted-string paths, numeric partial names and {{#.}}
sections, and records the zero-valued section and explicit helperMissing
call differences as known gaps with regression tests.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@sonarqubecloud

Copy link
Copy Markdown

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Six Handlebars.js spec examples render differently in Handlebars.Net 2.5.0

0 participants