diff options
Diffstat (limited to 'lib/handlebars/compiler/compiler.js')
-rw-r--r-- | lib/handlebars/compiler/compiler.js | 58 |
1 files changed, 26 insertions, 32 deletions
diff --git a/lib/handlebars/compiler/compiler.js b/lib/handlebars/compiler/compiler.js index 98e12b1..50195e3 100644 --- a/lib/handlebars/compiler/compiler.js +++ b/lib/handlebars/compiler/compiler.js @@ -1,13 +1,9 @@ -var compilerbase = require("./base"); +import Exception from "../exception"; +import { parse } from "./base"; +import JavaScriptCompiler from "./javascript-compiler"; +module AST from "./ast"; -exports.attach = function(Handlebars) { - -compilerbase.attach(Handlebars); - -// BEGIN(BROWSER) - -/*jshint eqnull:true*/ -var Compiler = Handlebars.Compiler = function() {}; +export function Compiler() {} // the foundHelper register will disambiguate helper lookup from finding a // function in a context. This is necessary for mustache compatibility, which @@ -40,6 +36,7 @@ Compiler.prototype = { return out.join("\n"); }, + equals: function(other) { var len = this.opcodes.length; if (other.opcodes.length !== len) { @@ -301,7 +298,7 @@ Compiler.prototype = { DATA: function(data) { this.options.data = true; if (data.id.isScoped || data.id.depth) { - throw new Handlebars.Exception('Scoped data references are not supported: ' + data.original); + throw new Exception('Scoped data references are not supported: ' + data.original); } this.opcode('lookupData'); @@ -415,49 +412,46 @@ Compiler.prototype = { } }; -Handlebars.precompile = function(input, options) { - if (input == null || (typeof input !== 'string' && input.constructor !== Handlebars.AST.ProgramNode)) { - throw new Handlebars.Exception("You must pass a string or Handlebars AST to Handlebars.precompile. You passed " + input); +export function precompile(input, options) { + if (input == null || (typeof input !== 'string' && input.constructor !== AST.ProgramNode)) { + throw new Exception("You must pass a string or Handlebars AST to Handlebars.precompile. You passed " + input); } options = options || {}; if (!('data' in options)) { options.data = true; } - var ast = Handlebars.parse(input); + + var ast = parse(input); var environment = new Compiler().compile(ast, options); - return new Handlebars.JavaScriptCompiler().compile(environment, options); -}; + return new JavaScriptCompiler().compile(environment, options); +} -Handlebars.compile = function(input, options) { - if (input == null || (typeof input !== 'string' && input.constructor !== Handlebars.AST.ProgramNode)) { - throw new Handlebars.Exception("You must pass a string or Handlebars AST to Handlebars.compile. You passed " + input); +export function compile(input, options, env) { + if (input == null || (typeof input !== 'string' && input.constructor !== AST.ProgramNode)) { + throw new Exception("You must pass a string or Handlebars AST to Handlebars.compile. You passed " + input); } options = options || {}; + if (!('data' in options)) { options.data = true; } + var compiled; - function compile() { - var ast = Handlebars.parse(input); + + function compileInput() { + var ast = parse(input); var environment = new Compiler().compile(ast, options); - var templateSpec = new Handlebars.JavaScriptCompiler().compile(environment, options, undefined, true); - return Handlebars.template(templateSpec); + var templateSpec = new JavaScriptCompiler().compile(environment, options, undefined, true); + return env.template(templateSpec); } // Template is only compiled on first use and cached after that point. return function(context, options) { if (!compiled) { - compiled = compile(); + compiled = compileInput(); } return compiled.call(this, context, options); }; -}; - - -// END(BROWSER) - -return Handlebars; - -}; +} |