Skip to content

Commit 8321670

Browse files
committed
Convert indented partial results to a string before splitting
Rendering an indented partial that resolves through @partial-block crashes with "result.split is not a function" whenever a helper along the way hands back something other than a plain string, a SafeString being the common case. This happens with both a real partial-block and an inline partial, since the indentation code always assumes it's holding a string once options.indent is set. Fixes #1695. A prior attempt at this fix (#1916) went stale without ever getting reviewed; the change here is the same one-line idea, just reapplied against master with a couple of regression tests covering both the partial-block and inline-partial cases from the original report.
1 parent 13a7a67 commit 8321670

2 files changed

Lines changed: 23 additions & 0 deletions

File tree

lib/handlebars/runtime.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,10 @@ export function template(templateSpec, env) {
8686
}
8787
if (result != null) {
8888
if (options.indent) {
89+
// a helper invoked through the partial-block (e.g. one wrapping its
90+
// output in a SafeString) can hand back something other than a
91+
// plain string here, and that breaks the split() below
92+
result = '' + result;
8993
let lines = result.split('\n');
9094
for (let i = 0, l = lines.length; i < l; i++) {
9195
if (!lines[i] && i + 1 === l) {

spec/partials.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -604,6 +604,25 @@ describe('partials', function () {
604604
'Dudes:\n Yehuda\n http://yehuda!\n Alan\n http://alan!\n'
605605
);
606606
});
607+
608+
it('indented partial-block whose content is a SafeString (GH-1695)', function () {
609+
expectTemplate('{{#>myPartial}}{{#safe}}success{{/safe}}{{/myPartial}}')
610+
.withHelper('safe', function (options) {
611+
return new Handlebars.SafeString(options.fn(this));
612+
})
613+
.withPartial('myPartial', ' {{> @partial-block }}')
614+
.toCompileTo(' success');
615+
});
616+
617+
it('indented inline partial whose content is a SafeString (GH-1695)', function () {
618+
expectTemplate(
619+
'{{#*inline "myPartial"}}{{#safe}}success{{/safe}}{{/inline}}\n {{> myPartial}}'
620+
)
621+
.withHelper('safe', function (options) {
622+
return new Handlebars.SafeString(options.fn(this));
623+
})
624+
.toCompileTo('\n success');
625+
});
607626
});
608627

609628
describe('compat mode', function () {

0 commit comments

Comments
 (0)