A few more lingering bugs:

* add helperMissing.not to the specs
* add Handlebars.Utils.isEmpty
* add runtime handling for inverse sections
* fix __get__ to pass an IdNode to evaluate
* handle case in wrapProgram where context is undefined
This commit is contained in:
wycats
2010-12-03 01:39:00 -05:00
parent c89ecf80a5
commit 9a6f77af56
3 changed files with 53 additions and 1 deletions
+36 -1
View File
@@ -140,6 +140,38 @@ Handlebars.Runtime.prototype = {
this.buffer = this.buffer + data.apply(this.wrapContext(), params);
},
// TODO: Block and Inverse can share code
inverse: function(block) {
var mustache = block.mustache,
id = mustache.id;
var idObj = this.accept(id),
data = idObj.data,
isInverse = Handlebars.Utils.isEmpty(data);
if(toString.call(data) !== "[object Function]") {
params = [data];
data = this.context.evaluate({depth: 0, parts: ["helperMissing"]}, this.stack).data;
id = "helperMissing";
} else {
params = this.evaluateParams(mustache.params);
id = id.parts.join("/");
}
if(isInverse) {
var not = data.not;
if(not) {
var context = this.wrapContext();
params.push(this.wrapProgram(block.program));
this.buffer = this.buffer + not.apply(this.wrapContext(), params);
} else {
throw new Handlebars.Exception("Not .not property found on " + id);
}
}
},
content: function(content) {
this.buffer += content.string;
},
@@ -150,6 +182,8 @@ Handlebars.Runtime.prototype = {
for(var i=0, l=params.length; i<l; i++) {
params[i] = this.accept(params[i]).data;
}
if(params.length === 0) { params = [undefined]; }
return params;
},
@@ -160,6 +194,7 @@ Handlebars.Runtime.prototype = {
var stack = proxy.__stack__ = this.stack.slice(0);
proxy.__get__ = function(path) {
path = new Handlebars.AST.IdNode(path.split("/"));
return context.evaluate(path, stack).data
};
@@ -175,7 +210,7 @@ Handlebars.Runtime.prototype = {
var fallback = this.context.fallback;
return function(context) {
if(context.isWrappedContext) { context = context.__data__; }
if(context && context.isWrappedContext) { context = context.__data__; }
var runtime = new Handlebars.Runtime(context, fallback, stack);
runtime.accept(program);
return runtime.buffer;
+13
View File
@@ -44,6 +44,19 @@ Handlebars.Utils = {
return str;
}
});
},
isEmpty: function(value) {
if (typeof value === "undefined") {
return true;
} else if (value === null) {
return true;
} else if (value === false) {
return true;
} else if(Object.prototype.toString.call(value) === "[object Array]" && value.length == 0) {
return true;
} else {
return false;
}
}
}
+4
View File
@@ -15,6 +15,10 @@ var helperMissing = function(context, fn) {
} else {
return fn(context);
}
};
helperMissing.not = function(context, fn) {
return fn(context);
}
var shouldCompileTo = function(string, hash, expected, message) {