diff options
author | kpdecker <kpdecker@gmail.com> | 2014-12-27 14:57:09 -0600 |
---|---|---|
committer | kpdecker <kpdecker@gmail.com> | 2014-12-27 14:59:36 -0600 |
commit | 6650957f790f0ca42cd67c6bf72282e2cfc991f8 (patch) | |
tree | cd7403a2fa67e0afbf2c13435e9d7486074573dc /lib/handlebars/compiler/javascript-compiler.js | |
parent | b764fb1ded3c2bb3c56796e6d7264981e63ba0d7 (diff) | |
download | handlebars.js-6650957f790f0ca42cd67c6bf72282e2cfc991f8.zip handlebars.js-6650957f790f0ca42cd67c6bf72282e2cfc991f8.tar.gz handlebars.js-6650957f790f0ca42cd67c6bf72282e2cfc991f8.tar.bz2 |
Implement strict and assumeObject modes
Causes templates to throw when lookup fields are not defined within the context. Strict mode will throw when any field is omitted. assumeObjects mode maintains the existing behavior of outputting an empty response when fields are not defined but allows for performance/size optimizations by not doing safety checks for intermediate objects in the lookup chain.
Strict mode effectively disables the helperMissing and inverse handling for blockHelperMissing as templates will throw rather than call those features for missing data fields.
Fixes #651
Fixes #805
Diffstat (limited to 'lib/handlebars/compiler/javascript-compiler.js')
-rw-r--r-- | lib/handlebars/compiler/javascript-compiler.js | 49 |
1 files changed, 42 insertions, 7 deletions
diff --git a/lib/handlebars/compiler/javascript-compiler.js b/lib/handlebars/compiler/javascript-compiler.js index 247f5e0..641daec 100644 --- a/lib/handlebars/compiler/javascript-compiler.js +++ b/lib/handlebars/compiler/javascript-compiler.js @@ -439,6 +439,11 @@ JavaScriptCompiler.prototype = { resolvePath: function(type, parts, i, falsy) { /*jshint -W083 */ + if (this.options.strict || this.options.assumeObjects) { + this.push(strictLookup(this.options.strict, this, parts, type)); + return; + } + var len = parts.length; for (; i < len; i++) { this.replaceStack(function(current) { @@ -572,11 +577,13 @@ JavaScriptCompiler.prototype = { var helper = this.setupHelper(paramSize, name); var simple = isSimple ? [helper.name, ' || '] : ''; - this.push( - this.source.functionCall( - ['('].concat(simple, nonHelper, ' || ', this.aliasable('helpers.helperMissing'), ')'), - 'call', - helper.callParams)); + var lookup = ['('].concat(simple, nonHelper); + if (!this.options.strict) { + lookup.push(' || ', this.aliasable('helpers.helperMissing')); + } + lookup.push(')'); + + this.push(this.source.functionCall(lookup, 'call', helper.callParams)); }, // [invokeKnownHelper] @@ -613,9 +620,17 @@ JavaScriptCompiler.prototype = { var helperName = this.lastHelper = this.nameLookup('helpers', name, 'helper'); + var lookup = ['(', '(helper = ', helperName, ' || ', nonHelper, ')']; + if (!this.options.strict) { + lookup[0] = '(helper = '; + lookup.push( + ' != null ? helper : ', + this.aliasable('helpers.helperMissing') + ); + } + this.push([ - '((helper = (helper = ', helperName, ' || ', nonHelper, ') != null ? helper : ', - this.aliasable('helpers.helperMissing'), + '(', lookup, (helper.paramsInit ? ['),(', helper.paramsInit] : []), '),', '(typeof helper === ', this.aliasable('"function"'), ' ? ', this.source.functionCall('helper','call', helper.callParams), ' : helper))' @@ -1023,4 +1038,24 @@ JavaScriptCompiler.isValidJavaScriptVariableName = function(name) { return !JavaScriptCompiler.RESERVED_WORDS[name] && /^[a-zA-Z_$][0-9a-zA-Z_$]*$/.test(name); }; +function strictLookup(requireTerminal, compiler, parts, type) { + var stack = compiler.popStack(); + + var i = 0, + len = parts.length; + if (requireTerminal) { + len--; + } + + for (; i < len; i++) { + stack = compiler.nameLookup(stack, parts[i], type); + } + + if (requireTerminal) { + return [compiler.aliasable('this.strict'), '(', stack, ', ', compiler.quotedString(parts[i]), ')']; + } else { + return stack; + } +} + export default JavaScriptCompiler; |