summaryrefslogtreecommitdiffstats
path: root/lib/handlebars/compiler
diff options
context:
space:
mode:
authormachty <machty@gmail.com>2013-02-06 17:51:47 -0500
committermachty <machty@gmail.com>2013-02-07 02:06:17 -0500
commita48e32bbf6e1c4cf1b22dc61aca7ee6e7448d46c (patch)
treeef9e5ff18efc96dd0209837cbf72901893d9533f /lib/handlebars/compiler
parent448e43c41eaec473f3c6a27ad3a833a41f83f996 (diff)
downloadhandlebars.js-a48e32bbf6e1c4cf1b22dc61aca7ee6e7448d46c.zip
handlebars.js-a48e32bbf6e1c4cf1b22dc61aca7ee6e7448d46c.tar.gz
handlebars.js-a48e32bbf6e1c4cf1b22dc61aca7ee6e7448d46c.tar.bz2
Handlebars.parse/precompile/compile can now be passed an already-compiled Handlebars AST.
Diffstat (limited to 'lib/handlebars/compiler')
-rw-r--r--lib/handlebars/compiler/base.js8
-rw-r--r--lib/handlebars/compiler/compiler.js16
2 files changed, 14 insertions, 10 deletions
diff --git a/lib/handlebars/compiler/base.js b/lib/handlebars/compiler/base.js
index 6919d38..df8ced0 100644
--- a/lib/handlebars/compiler/base.js
+++ b/lib/handlebars/compiler/base.js
@@ -4,9 +4,13 @@ var Handlebars = require("../base");
// BEGIN(BROWSER)
Handlebars.Parser = handlebars;
-Handlebars.parse = function(string) {
+Handlebars.parse = function(input) {
+
+ // Just return if an already-compile AST was passed in.
+ if(input.constructor === Handlebars.AST.ProgramNode) { return input; }
+
Handlebars.Parser.yy = Handlebars.AST;
- return Handlebars.Parser.parse(string);
+ return Handlebars.Parser.parse(input);
};
Handlebars.print = function(ast) {
diff --git a/lib/handlebars/compiler/compiler.js b/lib/handlebars/compiler/compiler.js
index e38f157..dd62b2c 100644
--- a/lib/handlebars/compiler/compiler.js
+++ b/lib/handlebars/compiler/compiler.js
@@ -1221,23 +1221,23 @@ Handlebars.JavaScriptCompiler = function() {};
})(Handlebars.Compiler, Handlebars.JavaScriptCompiler);
-Handlebars.precompile = function(string, options) {
- if (typeof string !== 'string') {
- throw new Handlebars.Exception("You must pass a string to Handlebars.compile. You passed " + string);
+Handlebars.precompile = function(input, options) {
+ if (!input || (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);
}
options = options || {};
if (!('data' in options)) {
options.data = true;
}
- var ast = Handlebars.parse(string);
+ var ast = Handlebars.parse(input);
var environment = new Handlebars.Compiler().compile(ast, options);
return new Handlebars.JavaScriptCompiler().compile(environment, options);
};
-Handlebars.compile = function(string, options) {
- if (typeof string !== 'string') {
- throw new Handlebars.Exception("You must pass a string to Handlebars.compile. You passed " + string);
+Handlebars.compile = function(input, options) {
+ if (!input || (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);
}
options = options || {};
@@ -1246,7 +1246,7 @@ Handlebars.compile = function(string, options) {
}
var compiled;
function compile() {
- var ast = Handlebars.parse(string);
+ var ast = Handlebars.parse(input);
var environment = new Handlebars.Compiler().compile(ast, options);
var templateSpec = new Handlebars.JavaScriptCompiler().compile(environment, options, undefined, true);
return Handlebars.template(templateSpec);