Fixed small context stack bug.

This commit is contained in:
Alan Johnson
2010-09-22 13:42:55 -04:00
parent d374744223
commit ee03e971f7
2 changed files with 15 additions and 7 deletions
+6 -7
View File
@@ -83,7 +83,7 @@ var Handlebars = {
},
buildContext: function(context, stack) {
var contextWrapper = function(stack) {
var ContextWrapper = function(stack) {
this.__stack__ = stack;
this.__get__ = function(path) {
var context = this;
@@ -91,7 +91,7 @@ var Handlebars = {
var depth = parsedPath[0];
var parts = parsedPath[1];
if (depth > 0) {
context = this.__stack__[stack.length - depth];
context = this.__stack__[this.__stack__.length - depth];
}
for (var i = 0; i < parts.length; i++) {
@@ -102,8 +102,8 @@ var Handlebars = {
};
};
contextWrapper.prototype = context;
return new contextWrapper(stack);
ContextWrapper.prototype = context;
return new ContextWrapper(stack);
},
// Returns a two element array containing the numbers of contexts to back up the stack and
@@ -374,8 +374,7 @@ Handlebars.Compiler.prototype = {
// each function made internally needs a unique IDs. These are locals, so they
// don't need to be globally unique, just per compiler
var fnId = "fn" + this.pointer.toString();
this.fn += "var proxy = Handlebars.buildContext(" + param + ", stack);";
this.fn += "var wrappedContext = Handlebars.buildContext(context);";
this.fn += "var wrappedContext = Handlebars.buildContext(context, stack);";
this.fn += "var " + fnId + " = function(context) {" + result + "}; ";
this.fn += "lookup = " + this.lookupFor(mustache) + "; ";
@@ -386,7 +385,7 @@ Handlebars.Compiler.prototype = {
else {
this.fn += " var " + fnId + "Not = null;";
}
this.fn += "out = out + Handlebars.handleBlock(lookup, wrappedContext, proxy, " + fnId + ", " + fnId + "Not);"
this.fn += "out = out + Handlebars.handleBlock(lookup, wrappedContext, " + param + ", " + fnId + ", " + fnId + "Not);"
this.fn += "stack.pop();";
this.openBlock = false;
+9
View File
@@ -191,6 +191,15 @@ test("helper with complex lookup", function() {
shouldCompileTo(string, [hash, fallback], "<a href='/root/goodbye'>Goodbye</a>")
});
test("helper with complex lookup and nested template", function() {
var string = "{{#goodbyes}}{{#link}}{{text}}{{/link}}{{/goodbyes}}";
var hash = {prefix: '/root', goodbyes: [{text: "Goodbye", url: "goodbye"}]};
var fallback = {link: function (context, fn) {
return "<a href='" + this.__get__("../../prefix") + "/" + context.url + "'>" + fn(context) + "</a>";
}};
shouldCompileTo(string, [hash, fallback], "<a href='/root/goodbye'>Goodbye</a>")
});
test("block with deep nested complex lookup", function() {
var string = "{{#outer}}Goodbye {{#inner}}cruel {{../../omg}}{{/inner}}{{/outer}}";
var hash = {omg: "OMG!", outer: [{ inner: [{ text: "goodbye" }] }] };