diff options
Diffstat (limited to 'lib/handlebars/compiler/compiler.js')
-rw-r--r-- | lib/handlebars/compiler/compiler.js | 73 |
1 files changed, 32 insertions, 41 deletions
diff --git a/lib/handlebars/compiler/compiler.js b/lib/handlebars/compiler/compiler.js index 3e83a62..a4bd09a 100644 --- a/lib/handlebars/compiler/compiler.js +++ b/lib/handlebars/compiler/compiler.js @@ -1,4 +1,5 @@ import Exception from "../exception"; +import {isArray} from "../utils"; export function Compiler() {} @@ -10,30 +11,6 @@ export function Compiler() {} Compiler.prototype = { compiler: Compiler, - disassemble: function() { - var opcodes = this.opcodes, opcode, out = [], params, param; - - for (var i=0, l=opcodes.length; i<l; i++) { - opcode = opcodes[i]; - - if (opcode.opcode === 'DECLARE') { - out.push("DECLARE " + opcode.name + "=" + opcode.value); - } else { - params = []; - for (var j=0; j<opcode.args.length; j++) { - param = opcode.args[j]; - if (typeof param === "string") { - param = "\"" + param.replace("\n", "\\n") + "\""; - } - params.push(param); - } - out.push(opcode.opcode + " " + params.join(" ")); - } - } - - return out.join("\n"); - }, - equals: function(other) { var len = this.opcodes.length; if (other.opcodes.length !== len) { @@ -43,20 +20,14 @@ Compiler.prototype = { 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) { + if (opcode.opcode !== otherOpcode.opcode || !argEquals(opcode.args, otherOpcode.args)) { return false; } - for (var j = 0; j < opcode.args.length; j++) { - if (opcode.args[j] !== otherOpcode.args[j]) { - return false; - } - } } + // We know that length is the same between the two arrays because they are directly tied + // to the opcode behavior above. len = this.children.length; - if (other.children.length !== len) { - return false; - } for (i = 0; i < len; i++) { if (!this.children[i].equals(other.children[i])) { return false; @@ -214,15 +185,18 @@ Compiler.prototype = { if (partial.context) { this.accept(partial.context); } else { - this.opcode('push', 'depth0'); + this.opcode('getContext', 0); + this.opcode('pushContext'); } - this.opcode('invokePartial', partialName.name); + this.opcode('invokePartial', partialName.name, partial.indent || ''); this.opcode('append'); }, content: function(content) { - this.opcode('appendContent', content.string); + if (!content.omit) { + this.opcode('appendContent', content.string); + } }, mustache: function(mustache) { @@ -305,7 +279,7 @@ Compiler.prototype = { // Context reference, i.e. `{{foo .}}` or `{{foo ..}}` this.opcode('pushContext'); } else { - this.opcode('lookupOnContext', id.parts, id.falsy); + this.opcode('lookupOnContext', id.parts, id.falsy, id.isScoped); } }, @@ -333,10 +307,6 @@ Compiler.prototype = { this.opcodes.push({ opcode: name, args: [].slice.call(arguments, 1) }); }, - declare: function(name, value) { - this.opcodes.push({ opcode: 'DECLARE', name: name, value: value }); - }, - addDepth: function(depth) { if(depth === 0) { return; } @@ -421,6 +391,9 @@ export function precompile(input, options, env) { if (!('data' in options)) { options.data = true; } + if (options.compat) { + options.useDepths = true; + } var ast = env.parse(input); var environment = new env.Compiler().compile(ast, options); @@ -437,6 +410,9 @@ export function compile(input, options, env) { if (!('data' in options)) { options.data = true; } + if (options.compat) { + options.useDepths = true; + } var compiled; @@ -468,3 +444,18 @@ export function compile(input, options, env) { }; return ret; } + +function argEquals(a, b) { + if (a === b) { + return true; + } + + if (isArray(a) && isArray(b) && a.length === b.length) { + for (var i = 0; i < a.length; i++) { + if (!argEquals(a[i], b[i])) { + return false; + } + } + return true; + } +} |