Switched to using eval expression on basically all expressions.
This commit is contained in:
+16
-14
@@ -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
@@ -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");
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user