vendor/assets/javascripts/handlebars.js in handlebars_assets-0.8.2 vs vendor/assets/javascripts/handlebars.js in handlebars_assets-0.9.0
- old
+ new
@@ -1,13 +1,37 @@
+/*
+
+Copyright (C) 2011 by Yehuda Katz
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
+
+*/
+
// lib/handlebars/base.js
/*jshint eqnull:true*/
this.Handlebars = {};
(function(Handlebars) {
-Handlebars.VERSION = "1.0.rc.1";
+Handlebars.VERSION = "1.0.rc.2";
Handlebars.helpers = {};
Handlebars.partials = {};
Handlebars.registerHelper = function(name, fn, inverse) {
@@ -700,22 +724,27 @@
var dig = [], depth = 0;
for(var i=0,l=parts.length; i<l; i++) {
var part = parts[i];
- if(part === "..") { depth++; }
- else if(part === "." || part === "this") { this.isScoped = true; }
+ if (part === ".." || part === "." || part === "this") {
+ if (dig.length > 0) { throw new Handlebars.Exception("Invalid path: " + this.original); }
+ else if (part === "..") { depth++; }
+ else { this.isScoped = true; }
+ }
else { dig.push(part); }
}
this.parts = dig;
this.string = dig.join('.');
this.depth = depth;
// an ID is simple if it only has one part, and that part is not
// `..` or `this`.
this.isSimple = parts.length === 1 && !this.isScoped && depth === 0;
+
+ this.stringModeValue = this.string;
};
Handlebars.AST.PartialNameNode = function(name) {
this.type = "PARTIAL_NAME";
this.name = name;
@@ -727,20 +756,23 @@
};
Handlebars.AST.StringNode = function(string) {
this.type = "STRING";
this.string = string;
+ this.stringModeValue = string;
};
Handlebars.AST.IntegerNode = function(integer) {
this.type = "INTEGER";
this.integer = integer;
+ this.stringModeValue = Number(integer);
};
Handlebars.AST.BooleanNode = function(bool) {
this.type = "BOOLEAN";
this.bool = bool;
+ this.stringModeValue = bool === "true";
};
Handlebars.AST.CommentNode = function(comment) {
this.type = "comment";
this.comment = comment;
@@ -846,11 +878,31 @@
}
}
return out.join("\n");
},
+ equals: function(other) {
+ var len = this.opcodes.length;
+ if (other.opcodes.length !== len) {
+ return false;
+ }
+ for (var i = 0; i < len; i++) {
+ var opcode = this.opcodes[i],
+ otherOpcode = other.opcodes[i];
+ if (opcode.opcode !== otherOpcode.opcode || opcode.args.length !== otherOpcode.args.length) {
+ return false;
+ }
+ for (var j = 0; j < opcode.args.length; j++) {
+ if (opcode.args[j] !== otherOpcode.args[j]) {
+ return false;
+ }
+ }
+ }
+ return true;
+ },
+
guid: 0,
compile: function(program, options) {
this.children = [];
this.depths = {list: []};
@@ -937,38 +989,44 @@
// now that the simple mustache is resolved, we need to
// evaluate it by executing `blockHelperMissing`
this.opcode('pushProgram', program);
this.opcode('pushProgram', inverse);
- this.opcode('pushLiteral', '{}');
+ this.opcode('emptyHash');
this.opcode('blockValue');
} else {
this.ambiguousMustache(mustache, program, inverse);
// now that the simple mustache is resolved, we need to
// evaluate it by executing `blockHelperMissing`
this.opcode('pushProgram', program);
this.opcode('pushProgram', inverse);
- this.opcode('pushLiteral', '{}');
+ this.opcode('emptyHash');
this.opcode('ambiguousBlockValue');
}
this.opcode('append');
},
hash: function(hash) {
var pairs = hash.pairs, pair, val;
- this.opcode('push', '{}');
+ this.opcode('pushHash');
for(var i=0, l=pairs.length; i<l; i++) {
pair = pairs[i];
val = pair[1];
- this.accept(val);
+ if (this.options.stringParams) {
+ this.opcode('pushStringParam', val.stringModeValue, val.type);
+ } else {
+ this.accept(val);
+ }
+
this.opcode('assignToHash', pair[0]);
}
+ this.opcode('popHash');
},
partial: function(partial) {
var partialName = partial.partialName;
this.usePartial = true;
@@ -1005,21 +1063,23 @@
this.opcode('append');
}
},
ambiguousMustache: function(mustache, program, inverse) {
- var id = mustache.id, name = id.parts[0];
+ var id = mustache.id,
+ name = id.parts[0],
+ isBlock = program != null || inverse != null;
this.opcode('getContext', id.depth);
this.opcode('pushProgram', program);
this.opcode('pushProgram', inverse);
- this.opcode('invokeAmbiguous', name);
+ this.opcode('invokeAmbiguous', name, isBlock);
},
- simpleMustache: function(mustache, program, inverse) {
+ simpleMustache: function(mustache) {
var id = mustache.id;
if (id.type === 'DATA') {
this.DATA(id);
} else if (id.parts.length) {
@@ -1132,11 +1192,11 @@
if(param.depth) {
this.addDepth(param.depth);
}
this.opcode('getContext', param.depth || 0);
- this.opcode('pushStringParam', param.string);
+ this.opcode('pushStringParam', param.stringModeValue, param.type);
} else {
this[param.type](param);
}
}
},
@@ -1146,11 +1206,11 @@
this.pushParams(params);
if(mustache.hash) {
this.hash(mustache.hash);
} else {
- this.opcode('pushLiteral', '{}');
+ this.opcode('emptyHash');
}
return params;
},
@@ -1163,11 +1223,11 @@
this.opcode('pushProgram', inverse);
if(mustache.hash) {
this.hash(mustache.hash);
} else {
- this.opcode('pushLiteral', '{}');
+ this.opcode('emptyHash');
}
return params;
}
};
@@ -1177,11 +1237,11 @@
};
JavaScriptCompiler.prototype = {
// PUBLIC API: You can override these methods in a subclass to provide
// alternative compiled forms for name lookup and buffering semantics
- nameLookup: function(parent, name, type) {
+ nameLookup: function(parent, name /* , type*/) {
if (/^[0-9]+$/.test(name)) {
return parent + "[" + name + "]";
} else if (JavaScriptCompiler.isValidJavaScriptVariableName(name)) {
return parent + "." + name;
}
@@ -1192,11 +1252,15 @@
appendToBuffer: function(string) {
if (this.environment.isSimple) {
return "return " + string + ";";
} else {
- return "buffer += " + string + ";";
+ return {
+ appendToBuffer: true,
+ content: string,
+ toString: function() { return "buffer += " + string + ";"; }
+ };
}
},
initializeBuffer: function() {
return this.quotedString("");
@@ -1213,19 +1277,21 @@
this.name = this.environment.name;
this.isChild = !!context;
this.context = context || {
programs: [],
+ environments: [],
aliases: { }
};
this.preamble();
this.stackSlot = 0;
this.stackVars = [];
this.registers = { list: [] };
this.compileStack = [];
+ this.inlineStack = [];
this.compileChildren(environment, options);
var opcodes = environment.opcodes, opcode;
@@ -1243,15 +1309,15 @@
return this.createFunctionContext(asObject);
},
nextOpcode: function() {
- var opcodes = this.environment.opcodes, opcode = opcodes[this.i + 1];
+ var opcodes = this.environment.opcodes;
return opcodes[this.i + 1];
},
- eat: function(opcode) {
+ eat: function() {
this.i = this.i + 1;
},
preamble: function() {
var out = [];
@@ -1285,11 +1351,10 @@
this.source[1] = this.source[1] + ", " + locals.join(", ");
}
// Generate minimizer alias mappings
if (!this.isChild) {
- var aliases = [];
for (var alias in this.context.aliases) {
this.source[1] = this.source[1] + ', ' + alias + '=' + this.context.aliases[alias];
}
}
@@ -1310,20 +1375,46 @@
for(var i=0, l=this.environment.depths.list.length; i<l; i++) {
params.push("depth" + this.environment.depths.list[i]);
}
+ // Perform a second pass over the output to merge content when possible
+ var source = this.mergeSource();
+
if (asObject) {
- params.push(this.source.join("\n "));
+ params.push(source);
return Function.apply(this, params);
} else {
- var functionSource = 'function ' + (this.name || '') + '(' + params.join(',') + ') {\n ' + this.source.join("\n ") + '}';
+ var functionSource = 'function ' + (this.name || '') + '(' + params.join(',') + ') {\n ' + source + '}';
Handlebars.log(Handlebars.logger.DEBUG, functionSource + "\n\n");
return functionSource;
}
},
+ mergeSource: function() {
+ // WARN: We are not handling the case where buffer is still populated as the source should
+ // not have buffer append operations as their final action.
+ var source = '',
+ buffer;
+ for (var i = 0, len = this.source.length; i < len; i++) {
+ var line = this.source[i];
+ if (line.appendToBuffer) {
+ if (buffer) {
+ buffer = buffer + '\n + ' + line.content;
+ } else {
+ buffer = line.content;
+ }
+ } else {
+ if (buffer) {
+ source += 'buffer += ' + buffer + ';\n ';
+ buffer = undefined;
+ }
+ source += line + '\n ';
+ }
+ }
+ return source;
+ },
// [blockValue]
//
// On stack, before: hash, inverse, program, value
// On stack, after: return value of blockHelperMissing
@@ -1357,10 +1448,13 @@
this.setupParams(0, params);
var current = this.topStack();
params.splice(1, 0, current);
+ // Use the options value generated from the invocation
+ params[params.length-1] = 'options';
+
this.source.push("if (!" + this.lastHelper + ") { " + current + " = blockHelperMissing.call(" + params.join(", ") + "); }");
},
// [appendContent]
//
@@ -1380,10 +1474,13 @@
// Coerces `value` to a String and appends it to the current buffer.
//
// If `value` is truthy, or 0, it is coerced into a string and appended
// Otherwise, the empty string is appended
append: function() {
+ // Force anything that is inlined onto the stack so we don't have duplication
+ // when we examine local
+ this.flushInline();
var local = this.popStack();
this.source.push("if(" + local + " || " + local + " === 0) { " + this.appendToBuffer(local) + " }");
if (this.environment.isSimple) {
this.source.push("else { " + this.appendToBuffer("''") + " }");
}
@@ -1394,19 +1491,13 @@
// On stack, before: value, ...
// On stack, after: ...
//
// Escape `value` and append it to the buffer
appendEscaped: function() {
- var opcode = this.nextOpcode(), extra = "";
this.context.aliases.escapeExpression = 'this.escapeExpression';
- if(opcode && opcode.opcode === 'appendContent') {
- extra = " + " + this.quotedString(opcode.args[0]);
- this.eat(opcode);
- }
-
- this.source.push(this.appendToBuffer("escapeExpression(" + this.popStack() + ")" + extra));
+ this.source.push(this.appendToBuffer("escapeExpression(" + this.popStack() + ")"));
},
// [getContext]
//
// On stack, before: ...
@@ -1426,11 +1517,11 @@
// On stack, after: currentContext[name], ...
//
// Looks up the value of `name` on the current context and pushes
// it onto the stack.
lookupOnContext: function(name) {
- this.pushStack(this.nameLookup('depth' + this.lastContext, name, 'context'));
+ this.push(this.nameLookup('depth' + this.lastContext, name, 'context'));
},
// [pushContext]
//
// On stack, before: ...
@@ -1474,26 +1565,53 @@
// On stack, before: ...
// On stack, after: data[id], ...
//
// Push the result of looking up `id` on the current data
lookupData: function(id) {
- this.pushStack(this.nameLookup('data', id, 'data'));
+ this.push(this.nameLookup('data', id, 'data'));
},
// [pushStringParam]
//
// On stack, before: ...
// On stack, after: string, currentContext, ...
//
// This opcode is designed for use in string mode, which
// provides the string value of a parameter along with its
// depth rather than resolving it immediately.
- pushStringParam: function(string) {
+ pushStringParam: function(string, type) {
this.pushStackLiteral('depth' + this.lastContext);
- this.pushString(string);
+
+ this.pushString(type);
+
+ if (typeof string === 'string') {
+ this.pushString(string);
+ } else {
+ this.pushStackLiteral(string);
+ }
},
+ emptyHash: function() {
+ this.pushStackLiteral('{}');
+
+ if (this.options.stringParams) {
+ this.register('hashTypes', '{}');
+ }
+ },
+ pushHash: function() {
+ this.hash = {values: [], types: []};
+ },
+ popHash: function() {
+ var hash = this.hash;
+ this.hash = undefined;
+
+ if (this.options.stringParams) {
+ this.register('hashTypes', '{' + hash.types.join(',') + '}');
+ }
+ this.push('{\n ' + hash.values.join(',\n ') + '\n }');
+ },
+
// [pushString]
//
// On stack, before: ...
// On stack, after: quotedString(string), ...
//
@@ -1507,11 +1625,12 @@
// On stack, before: ...
// On stack, after: expr, ...
//
// Push an expression onto the stack
push: function(expr) {
- this.pushStack(expr);
+ this.inlineStack.push(expr);
+ return expr;
},
// [pushLiteral]
//
// On stack, before: ...
@@ -1550,16 +1669,18 @@
//
// If the helper is not found, `helperMissing` is called.
invokeHelper: function(paramSize, name) {
this.context.aliases.helperMissing = 'helpers.helperMissing';
- var helper = this.lastHelper = this.setupHelper(paramSize, name);
- this.register('foundHelper', helper.name);
+ var helper = this.lastHelper = this.setupHelper(paramSize, name, true);
- this.pushStack("foundHelper ? foundHelper.call(" +
- helper.callParams + ") " + ": helperMissing.call(" +
- helper.helperMissingParams + ")");
+ this.push(helper.name);
+ this.replaceStack(function(name) {
+ return name + ' ? ' + name + '.call(' +
+ helper.callParams + ") " + ": helperMissing.call(" +
+ helper.helperMissingParams + ")";
+ });
},
// [invokeKnownHelper]
//
// On stack, before: hash, inverse, program, params..., ...
@@ -1567,11 +1688,11 @@
//
// This operation is used when the helper is known to exist,
// so a `helperMissing` fallback is not required.
invokeKnownHelper: function(paramSize, name) {
var helper = this.setupHelper(paramSize, name);
- this.pushStack(helper.name + ".call(" + helper.callParams + ")");
+ this.push(helper.name + ".call(" + helper.callParams + ")");
},
// [invokeAmbiguous]
//
// On stack, before: hash, inverse, program, params..., ...
@@ -1582,23 +1703,22 @@
// is a helper or a path.
//
// This operation emits more code than the other options,
// and can be avoided by passing the `knownHelpers` and
// `knownHelpersOnly` flags at compile-time.
- invokeAmbiguous: function(name) {
+ invokeAmbiguous: function(name, helperCall) {
this.context.aliases.functionType = '"function"';
- this.pushStackLiteral('{}');
- var helper = this.setupHelper(0, name);
+ this.pushStackLiteral('{}'); // Hash value
+ var helper = this.setupHelper(0, name, helperCall);
var helperName = this.lastHelper = this.nameLookup('helpers', name, 'helper');
- this.register('foundHelper', helperName);
var nonHelper = this.nameLookup('depth' + this.lastContext, name, 'context');
var nextStack = this.nextStack();
- this.source.push('if (foundHelper) { ' + nextStack + ' = foundHelper.call(' + helper.callParams + '); }');
+ this.source.push('if (' + nextStack + ' = ' + helperName + ') { ' + nextStack + ' = ' + nextStack + '.call(' + helper.callParams + '); }');
this.source.push('else { ' + nextStack + ' = ' + nonHelper + '; ' + nextStack + ' = typeof ' + nextStack + ' === functionType ? ' + nextStack + '.apply(depth0) : ' + nextStack + '; }');
},
// [invokePartial]
//
@@ -1613,25 +1733,34 @@
if (this.options.data) {
params.push("data");
}
this.context.aliases.self = "this";
- this.pushStack("self.invokePartial(" + params.join(", ") + ")");
+ this.push("self.invokePartial(" + params.join(", ") + ")");
},
// [assignToHash]
//
// On stack, before: value, hash, ...
// On stack, after: hash, ...
//
// Pops a value and hash off the stack, assigns `hash[key] = value`
// and pushes the hash back onto the stack.
assignToHash: function(key) {
- var value = this.popStack();
- var hash = this.topStack();
+ var value = this.popStack(),
+ type;
- this.source.push(hash + "['" + key + "'] = " + value + ";");
+ if (this.options.stringParams) {
+ type = this.popStack();
+ this.popStack();
+ }
+
+ var hash = this.hash;
+ if (type) {
+ hash.types.push("'" + key + "': " + type);
+ }
+ hash.values.push("'" + key + "': (" + value + ")");
},
// HELPERS
compiler: JavaScriptCompiler,
@@ -1641,17 +1770,33 @@
for(var i=0, l=children.length; i<l; i++) {
child = children[i];
compiler = new this.compiler();
- this.context.programs.push(''); // Placeholder to prevent name conflicts for nested children
- var index = this.context.programs.length;
- child.index = index;
- child.name = 'program' + index;
- this.context.programs[index] = compiler.compile(child, options, this.context);
+ var index = this.matchExistingProgram(child);
+
+ if (index == null) {
+ this.context.programs.push(''); // Placeholder to prevent name conflicts for nested children
+ index = this.context.programs.length;
+ child.index = index;
+ child.name = 'program' + index;
+ this.context.programs[index] = compiler.compile(child, options, this.context);
+ this.context.environments[index] = child;
+ } else {
+ child.index = index;
+ child.name = 'program' + index;
+ }
}
},
+ matchExistingProgram: function(child) {
+ for (var i = 0, len = this.context.environments.length; i < len; i++) {
+ var environment = this.context.environments[i];
+ if (environment && environment.equals(child)) {
+ return i;
+ }
+ }
+ },
programExpression: function(guid) {
this.context.aliases.self = "this";
if(guid == null) {
@@ -1689,61 +1834,115 @@
this.registers.list.push(name);
}
},
pushStackLiteral: function(item) {
- this.compileStack.push(new Literal(item));
- return item;
+ return this.push(new Literal(item));
},
pushStack: function(item) {
+ this.flushInline();
+
var stack = this.incrStack();
- this.source.push(stack + " = " + item + ";");
+ if (item) {
+ this.source.push(stack + " = " + item + ";");
+ }
this.compileStack.push(stack);
return stack;
},
replaceStack: function(callback) {
- var stack = this.topStack(),
- item = callback.call(this, stack);
+ var prefix = '',
+ inline = this.isInline(),
+ stack;
- // Prevent modification of the context depth variable. Through replaceStack
- if (/^depth/.test(stack)) {
- stack = this.nextStack();
+ // If we are currently inline then we want to merge the inline statement into the
+ // replacement statement via ','
+ if (inline) {
+ var top = this.popStack(true);
+
+ if (top instanceof Literal) {
+ // Literals do not need to be inlined
+ stack = top.value;
+ } else {
+ // Get or create the current stack name for use by the inline
+ var name = this.stackSlot ? this.topStackName() : this.incrStack();
+
+ prefix = '(' + this.push(name) + ' = ' + top + '),';
+ stack = this.topStack();
+ }
+ } else {
+ stack = this.topStack();
}
- this.source.push(stack + " = " + item + ";");
+ var item = callback.call(this, stack);
+
+ if (inline) {
+ if (this.inlineStack.length || this.compileStack.length) {
+ this.popStack();
+ }
+ this.push('(' + prefix + item + ')');
+ } else {
+ // Prevent modification of the context depth variable. Through replaceStack
+ if (!/^stack/.test(stack)) {
+ stack = this.nextStack();
+ }
+
+ this.source.push(stack + " = (" + prefix + item + ");");
+ }
return stack;
},
- nextStack: function(skipCompileStack) {
- var name = this.incrStack();
- this.compileStack.push(name);
- return name;
+ nextStack: function() {
+ return this.pushStack();
},
incrStack: function() {
this.stackSlot++;
if(this.stackSlot > this.stackVars.length) { this.stackVars.push("stack" + this.stackSlot); }
+ return this.topStackName();
+ },
+ topStackName: function() {
return "stack" + this.stackSlot;
},
+ flushInline: function() {
+ var inlineStack = this.inlineStack;
+ if (inlineStack.length) {
+ this.inlineStack = [];
+ for (var i = 0, len = inlineStack.length; i < len; i++) {
+ var entry = inlineStack[i];
+ if (entry instanceof Literal) {
+ this.compileStack.push(entry);
+ } else {
+ this.pushStack(entry);
+ }
+ }
+ }
+ },
+ isInline: function() {
+ return this.inlineStack.length;
+ },
- popStack: function() {
- var item = this.compileStack.pop();
+ popStack: function(wrapped) {
+ var inline = this.isInline(),
+ item = (inline ? this.inlineStack : this.compileStack).pop();
- if (item instanceof Literal) {
+ if (!wrapped && (item instanceof Literal)) {
return item.value;
} else {
- this.stackSlot--;
+ if (!inline) {
+ this.stackSlot--;
+ }
return item;
}
},
- topStack: function() {
- var item = this.compileStack[this.compileStack.length - 1];
+ topStack: function(wrapped) {
+ var stack = (this.isInline() ? this.inlineStack : this.compileStack),
+ item = stack[stack.length - 1];
- if (item instanceof Literal) {
+ if (!wrapped && (item instanceof Literal)) {
return item.value;
} else {
return item;
}
},
@@ -1754,27 +1953,27 @@
.replace(/"/g, '\\"')
.replace(/\n/g, '\\n')
.replace(/\r/g, '\\r') + '"';
},
- setupHelper: function(paramSize, name) {
+ setupHelper: function(paramSize, name, missingParams) {
var params = [];
- this.setupParams(paramSize, params);
+ this.setupParams(paramSize, params, missingParams);
var foundHelper = this.nameLookup('helpers', name, 'helper');
return {
params: params,
name: foundHelper,
callParams: ["depth0"].concat(params).join(", "),
- helperMissingParams: ["depth0", this.quotedString(name)].concat(params).join(", ")
+ helperMissingParams: missingParams && ["depth0", this.quotedString(name)].concat(params).join(", ")
};
},
// the params and contexts arguments are passed in arrays
// to fill in
- setupParams: function(paramSize, params) {
- var options = [], contexts = [], param, inverse, program;
+ setupParams: function(paramSize, params, useRegister) {
+ var options = [], contexts = [], types = [], param, inverse, program;
options.push("hash:" + this.popStack());
inverse = this.popStack();
program = this.popStack();
@@ -1799,22 +1998,31 @@
for(var i=0; i<paramSize; i++) {
param = this.popStack();
params.push(param);
if(this.options.stringParams) {
+ types.push(this.popStack());
contexts.push(this.popStack());
}
}
if (this.options.stringParams) {
options.push("contexts:[" + contexts.join(",") + "]");
+ options.push("types:[" + types.join(",") + "]");
+ options.push("hashTypes:hashTypes");
}
if(this.options.data) {
options.push("data:data");
}
- params.push("{" + options.join(",") + "}");
+ options = "{" + options.join(",") + "}";
+ if (useRegister) {
+ this.register('options', options);
+ params.push('options');
+ } else {
+ params.push(options);
+ }
return params.join(", ");
}
};
var reservedWords = (