diff options
author | Kevin Decker <kpdecker@gmail.com> | 2013-10-27 09:54:25 -0700 |
---|---|---|
committer | Kevin Decker <kpdecker@gmail.com> | 2013-10-27 09:54:25 -0700 |
commit | 320c0a6fb54489606bec0d607658c0d0be0f03f6 (patch) | |
tree | 1f8e672b06c7d9be52f4237cec3f0b6df455cca4 /lib/handlebars/compiler/javascript-compiler.js | |
parent | 06d94fed56b43bdf0c824bdce966596e551d3324 (diff) | |
parent | 31f7c25a8faac32e4996d95ccec509ead41f3f3c (diff) | |
download | handlebars.js-320c0a6fb54489606bec0d607658c0d0be0f03f6.zip handlebars.js-320c0a6fb54489606bec0d607658c0d0be0f03f6.tar.gz handlebars.js-320c0a6fb54489606bec0d607658c0d0be0f03f6.tar.bz2 |
Merge pull request #336 from wycats/whitespace-control
Unecessary Whitespace
Diffstat (limited to 'lib/handlebars/compiler/javascript-compiler.js')
-rw-r--r-- | lib/handlebars/compiler/javascript-compiler.js | 71 |
1 files changed, 51 insertions, 20 deletions
diff --git a/lib/handlebars/compiler/javascript-compiler.js b/lib/handlebars/compiler/javascript-compiler.js index 283c20c..b04ef1a 100644 --- a/lib/handlebars/compiler/javascript-compiler.js +++ b/lib/handlebars/compiler/javascript-compiler.js @@ -75,18 +75,17 @@ JavaScriptCompiler.prototype = { } else { this[opcode.opcode].apply(this, opcode.args); } - } - return this.createFunctionContext(asObject); - }, + // Reset the stripNext flag if it was not set by this operation. + if (opcode.opcode !== this.stripNext) { + this.stripNext = false; + } + } - nextOpcode: function() { - var opcodes = this.environment.opcodes; - return opcodes[this.i + 1]; - }, + // Flush any trailing content that might be pending. + this.pushSource(''); - eat: function() { - this.i = this.i + 1; + return this.createFunctionContext(asObject); }, preamble: function() { @@ -141,7 +140,7 @@ JavaScriptCompiler.prototype = { } if (!this.environment.isSimple) { - this.source.push("return buffer;"); + this.pushSource("return buffer;"); } var params = this.isChild ? ["depth0", "data"] : ["Handlebars", "depth0", "helpers", "partials", "data"]; @@ -232,7 +231,7 @@ JavaScriptCompiler.prototype = { // Use the options value generated from the invocation params[params.length-1] = 'options'; - this.source.push("if (!" + this.lastHelper + ") { " + current + " = blockHelperMissing.call(" + params.join(", ") + "); }"); + this.pushSource("if (!" + this.lastHelper + ") { " + current + " = blockHelperMissing.call(" + params.join(", ") + "); }"); }, // [appendContent] @@ -242,7 +241,28 @@ JavaScriptCompiler.prototype = { // // Appends the string value of `content` to the current buffer appendContent: function(content) { - this.source.push(this.appendToBuffer(this.quotedString(content))); + if (this.pendingContent) { + content = this.pendingContent + content; + } + if (this.stripNext) { + content = content.replace(/^\s+/, ''); + } + + this.pendingContent = content; + }, + + // [strip] + // + // On stack, before: ... + // On stack, after: ... + // + // Removes any trailing whitespace from the prior content node and flags + // the next operation for stripping if it is a content node. + strip: function() { + if (this.pendingContent) { + this.pendingContent = this.pendingContent.replace(/\s+$/, ''); + } + this.stripNext = 'strip'; }, // [append] @@ -259,9 +279,9 @@ JavaScriptCompiler.prototype = { // when we examine local this.flushInline(); var local = this.popStack(); - this.source.push("if(" + local + " || " + local + " === 0) { " + this.appendToBuffer(local) + " }"); + this.pushSource("if(" + local + " || " + local + " === 0) { " + this.appendToBuffer(local) + " }"); if (this.environment.isSimple) { - this.source.push("else { " + this.appendToBuffer("''") + " }"); + this.pushSource("else { " + this.appendToBuffer("''") + " }"); } }, @@ -274,7 +294,7 @@ JavaScriptCompiler.prototype = { appendEscaped: function() { this.context.aliases.escapeExpression = 'this.escapeExpression'; - this.source.push(this.appendToBuffer("escapeExpression(" + this.popStack() + ")")); + this.pushSource(this.appendToBuffer("escapeExpression(" + this.popStack() + ")")); }, // [getContext] @@ -498,8 +518,8 @@ JavaScriptCompiler.prototype = { var nonHelper = this.nameLookup('depth' + this.lastContext, name, 'context'); var nextStack = this.nextStack(); - this.source.push('if (' + nextStack + ' = ' + helperName + ') { ' + nextStack + ' = ' + nextStack + '.call(' + helper.callParams + '); }'); - this.source.push('else { ' + nextStack + ' = ' + nonHelper + '; ' + nextStack + ' = typeof ' + nextStack + ' === functionType ? ' + nextStack + '.call(' + helper.callParams + ') : ' + nextStack + '; }'); + this.pushSource('if (' + nextStack + ' = ' + helperName + ') { ' + nextStack + ' = ' + nextStack + '.call(' + helper.callParams + '); }'); + this.pushSource('else { ' + nextStack + ' = ' + nonHelper + '; ' + nextStack + ' = typeof ' + nextStack + ' === functionType ? ' + nextStack + '.call(' + helper.callParams + ') : ' + nextStack + '; }'); }, // [invokePartial] @@ -606,7 +626,7 @@ JavaScriptCompiler.prototype = { register: function(name, val) { this.useRegister(name); - this.source.push(name + " = " + val + ";"); + this.pushSource(name + " = " + val + ";"); }, useRegister: function(name) { @@ -620,12 +640,23 @@ JavaScriptCompiler.prototype = { return this.push(new Literal(item)); }, + pushSource: function(source) { + if (this.pendingContent) { + this.source.push(this.appendToBuffer(this.quotedString(this.pendingContent))); + this.pendingContent = undefined; + } + + if (source) { + this.source.push(source); + } + }, + pushStack: function(item) { this.flushInline(); var stack = this.incrStack(); if (item) { - this.source.push(stack + " = " + item + ";"); + this.pushSource(stack + " = " + item + ";"); } this.compileStack.push(stack); return stack; @@ -668,7 +699,7 @@ JavaScriptCompiler.prototype = { stack = this.nextStack(); } - this.source.push(stack + " = (" + prefix + item + ");"); + this.pushSource(stack + " = (" + prefix + item + ");"); } return stack; }, |