Merge branch 'master' into inverted_sections

This commit is contained in:
Alan Johnson
2010-08-15 16:23:00 -04:00
2 changed files with 20 additions and 6 deletions
+6 -5
View File
@@ -2,7 +2,7 @@ Handlebars = {
compile: function(string) {
var compiler = new Handlebars.Compiler(string);
var result = compiler.compile();
return new Function("context", "fallback", "fallback = fallback || {}; var stack = [];var partials = {};" + result);
return new Function("context", "fallback", "fallback = fallback || {}; var stack = [];" + result);
},
isFunction: function(fn) {
@@ -90,7 +90,7 @@ Handlebars = {
switch(parts[i]) {
case "..":
if (readDepth) {
throw new Handlebars.ParseError("Cannot jump out of context after moving into a context.");
throw new Handlebars.Exception("Cannot jump out of context after moving into a context.");
} else {
depth += 1;
}
@@ -122,7 +122,7 @@ Handlebars.Compiler = function(string) {
this.partial = false;
};
Handlebars.ParseError = function(message) {
Handlebars.Exception = function(message) {
this.message = message;
};
@@ -252,8 +252,9 @@ Handlebars.Compiler.prototype = {
addPartial: function(mustache, param) {
// either used a cached copy of the partial or compile a new one
this.fn += "if (typeof partials['" + mustache + "'] === 'undefined') partials['" + mustache + "'] = Handlebars.compile(fallback['partials']['" + mustache + "']);";
this.fn += "out = out + partials['" + mustache + "'](" + param + ", fallback);";
this.fn += "if (typeof fallback['partials'] === 'undefined' || typeof fallback['partials']['" + mustache + "'] === 'undefined') throw new Handlebars.Exception('Attempted to render undefined partial: " + mustache + "');";
this.fn += "if (!Handlebars.isFunction(fallback['partials']['" + mustache + "'])) fallback['partials']['" + mustache + "'] = Handlebars.compile(fallback['partials']['" + mustache + "']);";
this.fn += "out = out + fallback['partials']['" + mustache + "'](" + param + ", fallback);";
},
parseMustache: function() {
+14 -1
View File
@@ -64,7 +64,7 @@ test("bad idea nested paths", function() {
try {
Handlebars.compile("{{#goodbyes}}{{../name/../name}}{{/goodbyes}}");
} catch (e) {
if (e instanceof Handlebars.ParseError) {
if (e instanceof Handlebars.Exception) {
caught = true;
}
}
@@ -237,6 +237,19 @@ test("partial in a partial", function() {
shouldCompileTo(string, [hash, {partials: {dude: dude, url: url}}], "Dudes: Yehuda <a href='http://yehuda'>http://yehuda</a> Alan <a href='http://alan'>http://alan</a> ", "Partials are rendered inside of other partials");
});
test("rendering undefined partial throws an exception", function() {
var caught = false;
try {
var template = Handlebars.compile("{{> whatever}}");
template();
} catch (e) {
if (e instanceof Handlebars.Exception) {
caught = true;
}
}
equals(caught, true);
});
module("safestring");
test("constructing a safestring from a string and checking its type", function() {