Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion lib/handlebars/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,29 @@ import {
resultIsAllowed,
} from './internal/proto-access.js';

// Merges the hash of a partial invocation into the context passed to that
// partial. For plain objects, arrays, maps and other non-instance values
// this creates a flat copy, as it always has. For class instances the copy
// keeps the original prototype so that methods and getters still work when
// helpers access them through `this` inside the partial (GH-2043).
function mergePartialHash(context, hash) {
if (
typeof context === 'object' &&
context !== null &&
!Utils.isArray(context) &&
!Utils.isMap(context)
) {
const prototype = Object.getPrototypeOf(context);
if (prototype !== Object.prototype && prototype !== null) {
const instance = Object.create(prototype);
Utils.extend(instance, context);
Utils.extend(instance, hash);
Comment on lines +31 to +32
return instance;
}
}
return Utils.extend({}, context, hash);
}

export function checkRevision(compilerInfo) {
const compilerRevision = (compilerInfo && compilerInfo[0]) || 1,
currentRevision = COMPILER_REVISION;
Expand Down Expand Up @@ -67,7 +90,7 @@ export function template(templateSpec, env) {

function invokePartialWrapper(partial, context, options) {
if (options.hash) {
context = Utils.extend({}, context, options.hash);
context = mergePartialHash(context, options.hash);
}
partial = env.VM.resolvePartial.call(this, partial, context, options);

Expand Down
40 changes: 40 additions & 0 deletions spec/partials.js
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,46 @@ describe('partials', function () {
}
});

it('should keep class instances intact when a partial is invoked with a hash', function () {
if (Handlebars.compile) {
function Car(colour) {
this.colour = colour;
}
Car.prototype.hasFourWheels = function () {
return true;
};

var env = Handlebars.create();
env.registerHelper('hasFour', function () {
return this.hasFourWheels ? this.hasFourWheels() : 'no-method';
});
env.registerHelper('isCar', function () {
return this instanceof Car;
});
env.registerPartial('partial', '{{hasFour}} {{isCar}} {{colour}}');

expect(env.compile('{{> partial hashValue=1}}')(new Car('red'))).toBe(
'true true red'
);
}
});

it('should keep hash override of context values when a class instance is passed', function () {
if (Handlebars.compile) {
function Car(colour) {
this.colour = colour;
}

var env = Handlebars.create();
env.registerPartial('partial', '{{colour}}');

expect(env.compile('{{> partial colour="pink"}}')(new Car('red'))).toBe(
'pink'
);
expect(env.compile('{{> partial}}')(new Car('red'))).toBe('red');
}
});

describe('standalone partials', function () {
it('indented partials', function () {
expectTemplate('Dudes:\n{{#dudes}}\n {{>dude}}\n{{/dudes}}')
Expand Down