Switched to using eval expression on basically all expressions.

This commit is contained in:
Alan Johnson
2010-10-13 16:41:19 -04:00
parent 3517710578
commit 1d72f1bea4
2 changed files with 18 additions and 16 deletions
+16 -14
View File
@@ -5,6 +5,7 @@ var Handlebars = {
if (Handlebars.compilerCache[string] == null) {
var fnBody = Handlebars.compileFunctionBody(string);
var fn = new Function("context", "fallback", "Handlebars", fnBody);
console.log(fn);
Handlebars.compilerCache[string] =
function(context, fallback) { return fn(context, fallback, Handlebars); };
}
@@ -79,7 +80,9 @@ var Handlebars = {
return compiled;
},
evalExpression: function(path, context, stack) {
evalExpression: function(path, context, stack, fallback) {
fallback = fallback || {};
var parsedPath = Handlebars.parsePath(path);
var depth = parsedPath[0];
var parts = parsedPath[1];
@@ -89,10 +92,14 @@ var Handlebars = {
context = stack[stack.length - depth];
}
for (var i = 0; i < parts.length && context !== undefined; i++) {
for (var i = 0; i < parts.length && typeof context !== "undefined"; i++) {
context = context[parts[i]];
}
if (parts.length == 1 && typeof context === "undefined") {
return fallback[parts[0]];
}
return context;
},
@@ -350,17 +357,12 @@ Handlebars.Compiler.prototype = {
},
lookupFor: function(param) {
var parsed = Handlebars.parsePath(param);
var depth = parsed[0];
var parts = parsed[1];
if (depth > 0 || parts.length > 1) {
return "(Handlebars.evalExpression('" + param + "', context, stack))";
} else if (parts.length == 1) {
return "(!Handlebars.isEmpty(context['" + parts[0] + "']) ? context['" + parts[0] + "'] : fallback['" + parts[0] + "'])";
} else {
return "(!Handlebars.isEmpty(context) ? context : null)";
}
if (typeof param === "undefined") {
return "context";
}
else {
return "(Handlebars.evalExpression('" + param + "', context, stack, fallback))";
}
},
compileToEndOfBlock: function(mustache) {
+2 -2
View File
@@ -100,13 +100,13 @@ test("nested paths with empty string value", function() {
});
test("bad idea nested paths", function() {
var hash = {goodbyes: [{text: "goodbye"}, {text: "Goodbye"}, {text: "GOODBYE"}], world: "world"};
shouldThrow(function() {
Handlebars.compile("{{#goodbyes}}{{../name/../name}}{{/goodbyes}}");
Handlebars.compile("{{#goodbyes}}{{../name/../name}}{{/goodbyes}}")(hash);
}, Handlebars.Exception,
"Cannot jump (..) into previous context after moving into a context.");
var string = "{{#goodbyes}}{{.././world}} {{/goodbyes}}";
var hash = {goodbyes: [{text: "goodbye"}, {text: "Goodbye"}, {text: "GOODBYE"}], world: "world"};
shouldCompileTo(string, hash, "world world world ", "Same context (.) is ignored in paths");
});