summaryrefslogtreecommitdiffstats
path: root/lib/handlebars/compiler/javascript-compiler.js
diff options
context:
space:
mode:
authorkpdecker <kpdecker@gmail.com>2014-01-17 19:14:36 -0600
committerkpdecker <kpdecker@gmail.com>2014-01-17 23:15:17 -0600
commitace2896ec876104f5bd220744124e7370fe6b9a2 (patch)
tree93fd30d6e64324e6914fb212310cfdedc3a6d260 /lib/handlebars/compiler/javascript-compiler.js
parent9df919083d4e25512fb6dc6fe1a050e63ad12c80 (diff)
downloadhandlebars.js-ace2896ec876104f5bd220744124e7370fe6b9a2.zip
handlebars.js-ace2896ec876104f5bd220744124e7370fe6b9a2.tar.gz
handlebars.js-ace2896ec876104f5bd220744124e7370fe6b9a2.tar.bz2
Add trackIds compiler flag
Allows helpers that care about where a particular field came from derive this data while maintaining backward compatibility with existing helpers.
Diffstat (limited to 'lib/handlebars/compiler/javascript-compiler.js')
-rw-r--r--lib/handlebars/compiler/javascript-compiler.js39
1 files changed, 36 insertions, 3 deletions
diff --git a/lib/handlebars/compiler/javascript-compiler.js b/lib/handlebars/compiler/javascript-compiler.js
index 96a8a0e..ff6bc47 100644
--- a/lib/handlebars/compiler/javascript-compiler.js
+++ b/lib/handlebars/compiler/javascript-compiler.js
@@ -61,6 +61,7 @@ JavaScriptCompiler.prototype = {
this.environment = environment;
this.options = options || {};
this.stringParams = this.options.stringParams;
+ this.trackIds = this.options.trackIds;
log('debug', this.environment.disassemble() + "\n\n");
@@ -418,6 +419,9 @@ JavaScriptCompiler.prototype = {
emptyHash: function() {
this.pushStackLiteral('{}');
+ if (this.trackIds) {
+ this.push('{}'); // hashIds
+ }
if (this.stringParams) {
this.push('{}'); // hashContexts
this.push('{}'); // hashTypes
@@ -427,12 +431,15 @@ JavaScriptCompiler.prototype = {
if (this.hash) {
this.hashes.push(this.hash);
}
- this.hash = {values: [], types: [], contexts: []};
+ this.hash = {values: [], types: [], contexts: [], ids: []};
},
popHash: function() {
var hash = this.hash;
this.hash = this.hashes.pop();
+ if (this.trackIds) {
+ this.push('{' + hash.ids.join(',') + '}');
+ }
if (this.stringParams) {
this.push('{' + hash.contexts.join(',') + '}');
this.push('{' + hash.types.join(',') + '}');
@@ -589,8 +596,12 @@ JavaScriptCompiler.prototype = {
assignToHash: function(key) {
var value = this.popStack(),
context,
- type;
+ type,
+ id;
+ if (this.trackIds) {
+ id = this.popStack();
+ }
if (this.stringParams) {
type = this.popStack();
context = this.popStack();
@@ -603,9 +614,22 @@ JavaScriptCompiler.prototype = {
if (type) {
hash.types.push("'" + key + "': " + type);
}
+ if (id) {
+ hash.ids.push("'" + key + "': " + id);
+ }
hash.values.push("'" + key + "': (" + value + ")");
},
+ pushId: function(type, name) {
+ if (type === 'ID' || type === 'DATA') {
+ this.pushString(name);
+ } else if (type === 'sexpr') {
+ this.pushStackLiteral('true');
+ } else {
+ this.pushStackLiteral('null');
+ }
+ },
+
// HELPERS
compiler: JavaScriptCompiler,
@@ -844,11 +868,14 @@ JavaScriptCompiler.prototype = {
},
setupOptions: function(helper, paramSize, params) {
- var options = {}, contexts = [], types = [], param, inverse, program;
+ var options = {}, contexts = [], types = [], ids = [], param, inverse, program;
options.name = this.quotedString(helper);
options.hash = this.popStack();
+ if (this.trackIds) {
+ options.hashIds = this.popStack();
+ }
if (this.stringParams) {
options.hashTypes = this.popStack();
options.hashContexts = this.popStack();
@@ -878,12 +905,18 @@ JavaScriptCompiler.prototype = {
param = this.popStack();
params.push(param);
+ if (this.trackIds) {
+ ids.push(this.popStack());
+ }
if (this.stringParams) {
types.push(this.popStack());
contexts.push(this.popStack());
}
}
+ if (this.trackIds) {
+ options.ids = "[" + ids.join(",") + "]";
+ }
if (this.stringParams) {
options.types = "[" + types.join(",") + "]";
options.contexts = "[" + contexts.join(",") + "]";