From ee03e971f7df380974a52d3ccc9ae6d40db05143 Mon Sep 17 00:00:00 2001 From: Alan Johnson Date: Wed, 22 Sep 2010 13:42:55 -0400 Subject: [PATCH] Fixed small context stack bug. --- lib/handlebars.js | 13 ++++++------- test/handlebars.js | 9 +++++++++ 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/lib/handlebars.js b/lib/handlebars.js index fd08b5db..e310eded 100644 --- a/lib/handlebars.js +++ b/lib/handlebars.js @@ -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; diff --git a/test/handlebars.js b/test/handlebars.js index 03c9491d..3a521813 100644 --- a/test/handlebars.js +++ b/test/handlebars.js @@ -191,6 +191,15 @@ test("helper with complex lookup", function() { shouldCompileTo(string, [hash, fallback], "Goodbye") }); +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 "" + fn(context) + ""; + }}; + shouldCompileTo(string, [hash, fallback], "Goodbye") +}); + 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" }] }] };