diff options
author | kpdecker <kpdecker@gmail.com> | 2013-01-20 21:13:53 -0600 |
---|---|---|
committer | kpdecker <kpdecker@gmail.com> | 2013-01-20 21:18:49 -0600 |
commit | 231543feaf165605bb369026ab244bb4a8058657 (patch) | |
tree | aba3a5f84f11489ae4182ade5d95c3ec5e4a3611 /lib/handlebars/compiler/compiler.js | |
parent | b3a39ccefd2d00428ace05b172f9faf8fd081052 (diff) | |
download | handlebars.js-231543feaf165605bb369026ab244bb4a8058657.zip handlebars.js-231543feaf165605bb369026ab244bb4a8058657.tar.gz handlebars.js-231543feaf165605bb369026ab244bb4a8058657.tar.bz2 |
Merge consecutive buffer appends
Diffstat (limited to 'lib/handlebars/compiler/compiler.js')
-rw-r--r-- | lib/handlebars/compiler/compiler.js | 40 |
1 files changed, 30 insertions, 10 deletions
diff --git a/lib/handlebars/compiler/compiler.js b/lib/handlebars/compiler/compiler.js index 4742fa8..caba954 100644 --- a/lib/handlebars/compiler/compiler.js +++ b/lib/handlebars/compiler/compiler.js @@ -392,7 +392,11 @@ Handlebars.JavaScriptCompiler = function() {}; if (this.environment.isSimple) { return "return " + string + ";"; } else { - return "buffer += " + string + ";"; + return { + appendToBuffer: true, + content: string, + toString: function() { return "buffer += " + string + ";"; } + }; } }, @@ -510,12 +514,34 @@ Handlebars.JavaScriptCompiler = function() {}; params.push("depth" + this.environment.depths.list[i]); } + // Perform a second pass over the output to merge content when possible + // 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 '; + } + } + 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; } @@ -600,15 +626,9 @@ Handlebars.JavaScriptCompiler = function() {}; // // 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] |