Skip to content

Commit

Permalink
Merge pull request #454 from leshill/fix_string_mode_contexts
Browse files Browse the repository at this point in the history
Add contexts for string mode hash values
  • Loading branch information
kpdecker committed Apr 8, 2013
2 parents 4429ffa + 53de759 commit 075fdb8
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 4 deletions.
15 changes: 13 additions & 2 deletions dist/handlebars.js
Expand Up @@ -1050,6 +1050,10 @@ Compiler.prototype = {
val = pair[1];

if (this.options.stringParams) {
if(val.depth) {
this.addDepth(val.depth);
}
this.opcode('getContext', val.depth || 0);
this.opcode('pushStringParam', val.stringModeValue, val.type);
} else {
this.accept(val);
Expand Down Expand Up @@ -1634,16 +1638,18 @@ JavaScriptCompiler.prototype = {

if (this.options.stringParams) {
this.register('hashTypes', '{}');
this.register('hashContexts', '{}');
}
},
pushHash: function() {
this.hash = {values: [], types: []};
this.hash = {values: [], types: [], contexts: []};
},
popHash: function() {
var hash = this.hash;
this.hash = undefined;

if (this.options.stringParams) {
this.register('hashContexts', '{' + hash.contexts.join(',') + '}');
this.register('hashTypes', '{' + hash.types.join(',') + '}');
}
this.push('{\n ' + hash.values.join(',\n ') + '\n }');
Expand Down Expand Up @@ -1786,14 +1792,18 @@ JavaScriptCompiler.prototype = {
// and pushes the hash back onto the stack.
assignToHash: function(key) {
var value = this.popStack(),
context,
type;

if (this.options.stringParams) {
type = this.popStack();
this.popStack();
context = this.popStack();
}

var hash = this.hash;
if (context) {
hash.contexts.push("'" + key + "': " + context);
}
if (type) {
hash.types.push("'" + key + "': " + type);
}
Expand Down Expand Up @@ -2044,6 +2054,7 @@ JavaScriptCompiler.prototype = {
if (this.options.stringParams) {
options.push("contexts:[" + contexts.join(",") + "]");
options.push("types:[" + types.join(",") + "]");
options.push("hashContexts:hashContexts");
options.push("hashTypes:hashTypes");
}

Expand Down
15 changes: 13 additions & 2 deletions lib/handlebars/compiler/compiler.js
Expand Up @@ -189,6 +189,10 @@ Compiler.prototype = {
val = pair[1];

if (this.options.stringParams) {
if(val.depth) {
this.addDepth(val.depth);
}
this.opcode('getContext', val.depth || 0);
this.opcode('pushStringParam', val.stringModeValue, val.type);
} else {
this.accept(val);
Expand Down Expand Up @@ -773,16 +777,18 @@ JavaScriptCompiler.prototype = {

if (this.options.stringParams) {
this.register('hashTypes', '{}');
this.register('hashContexts', '{}');
}
},
pushHash: function() {
this.hash = {values: [], types: []};
this.hash = {values: [], types: [], contexts: []};
},
popHash: function() {
var hash = this.hash;
this.hash = undefined;

if (this.options.stringParams) {
this.register('hashContexts', '{' + hash.contexts.join(',') + '}');
this.register('hashTypes', '{' + hash.types.join(',') + '}');
}
this.push('{\n ' + hash.values.join(',\n ') + '\n }');
Expand Down Expand Up @@ -925,14 +931,18 @@ JavaScriptCompiler.prototype = {
// and pushes the hash back onto the stack.
assignToHash: function(key) {
var value = this.popStack(),
context,
type;

if (this.options.stringParams) {
type = this.popStack();
this.popStack();
context = this.popStack();
}

var hash = this.hash;
if (context) {
hash.contexts.push("'" + key + "': " + context);
}
if (type) {
hash.types.push("'" + key + "': " + type);
}
Expand Down Expand Up @@ -1183,6 +1193,7 @@ JavaScriptCompiler.prototype = {
if (this.options.stringParams) {
options.push("contexts:[" + contexts.join(",") + "]");
options.push("types:[" + types.join(",") + "]");
options.push("hashContexts:hashContexts");
options.push("hashTypes:hashTypes");
}

Expand Down
26 changes: 26 additions & 0 deletions spec/qunit_spec.js
Expand Up @@ -1329,6 +1329,32 @@ test("in string mode, hash parameters get type information", function() {
equal(result, "Helper called");
});

test("in string mode, hash parameters get context information", function() {
var template = CompilerContext.compile('{{#with dale}}{{tomdale he.says desire="need" noun=../dad/joke bool=true}}{{/with}}', { stringParams: true });

var context = {dale: {}};

var helpers = {
tomdale: function(exclamation, options) {
equal(exclamation, "he.says");
equal(options.types[0], "ID");

equal(options.contexts.length, 1);
equal(options.hashContexts.noun, context);
equal(options.hash.desire, "need");
equal(options.hash.noun, "dad.joke");
equal(options.hash.bool, true);
return "Helper called";
},
"with": function(context, options) {
return options.fn(options.contexts[0][context]);
}
};

var result = template(context, { helpers: helpers });
equal(result, "Helper called");
});

test("when inside a block in String mode, .. passes the appropriate context in the options hash to a block helper", function() {
var template = CompilerContext.compile('{{#with dale}}{{#tomdale ../need dad.joke}}wot{{/tomdale}}{{/with}}', {stringParams: true});

Expand Down

0 comments on commit 075fdb8

Please sign in to comment.