Skip to content

fix: keep values escaped in indented standalone partials - #701

Open
nadrummond wants to merge 1 commit into
Handlebars-Net:masterfrom
nadrummond:fix/indented-partial-escaping
Open

nadrummond wants to merge 1 commit into
Handlebars-Net:masterfrom
nadrummond:fix/indented-partial-escaping

Conversation

@nadrummond

Copy link
Copy Markdown

Fixes #699.

Since #635, a partial whose tag stands alone on an indented line renders into a temporary writer, so
that WriteWithIndent can indent each line of its output. That writer was created with
suppressEncoding: true, and WriteWithIndent writes the captured text with encode: false, so the
partial's values were never HTML-escaped. With p registered as [{{v}}] and v set to <b>&,
<div>\n {{> p}}\n</div> rendered <div>\n [<b>&]</div>, while the same call inline or at
column 0 was escaped.

The temporary writer now takes 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. Each of those branches, without indentation,
renders through the caller's writer, so an indented partial now encodes exactly as an unindented one
does. The indentation itself is unchanged, and so is the output with NoEscape = true.

Tests

Added to the partial-indentation tests in PartialTests:

  • PartialIndentationKeepsValuesEscaped, a theory over an indented registered partial, an indented
    {{> @partial-block}}, and an indented call to a missing partial that falls back to the partial
    block. Each expects <div>\n [&lt;b&gt;&amp;]</div>.
  • InlinePartialIndentationKeepsValuesEscaped, for an indented call to a {{#*inline}} partial.
  • PartialIndentationKeepsNoEscape, which checks that with NoEscape = true the indented partial
    stays unescaped and keeps its indentation.

Without the change in PartialBinder, the four escaping cases fail (Actual: "<div>\n [<b>&]</div>")
and the NoEscape case passes. With it, the whole suite passes on net10.0: 2,331 tests, 0 failed.

An indented standalone partial renders into a temporary writer so each
line can be indented. That writer was created with encoding suppressed,
and the captured text is written on without encoding, so the partial's
values were never escaped. The temporary writer now takes the caller's
SuppressEncoding, as the partial would without indentation.

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

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.

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

1 participant