diff --git a/lib/handlebars.js b/lib/handlebars.js index 78244969..7d85c261 100644 --- a/lib/handlebars.js +++ b/lib/handlebars.js @@ -2,18 +2,18 @@ var handlebars = require("./handlebars/base"), // Each of these augment the Handlebars object. No need to setup here. // (This is done to easily share code between commonjs and browse envs) - utils = require("./handlebars/utils"), - compiler = require("./handlebars/compiler"), - runtime = require("./handlebars/runtime"); + utils = require("./handlebars/utils"), + compiler = require("./handlebars/compiler"), + runtime = require("./handlebars/runtime"); var create = function() { - var hb = handlebars.create(); + var hb = handlebars.create(); - utils.attach(hb); - compiler.attach(hb); - runtime.attach(hb); + utils.attach(hb); + compiler.attach(hb); + runtime.attach(hb); - return hb; + return hb; }; var Handlebars = create(); @@ -29,4 +29,4 @@ module.exports = Handlebars; // instantiate an instance // var handlebars = require('handlebars'); // var singleton = handlebars.Handlebars, -// local = handlebars.create(); +// local = handlebars.create(); diff --git a/lib/handlebars/base.js b/lib/handlebars/base.js index 656f0fb9..8640cb85 100644 --- a/lib/handlebars/base.js +++ b/lib/handlebars/base.js @@ -1,116 +1,116 @@ module.exports.create = function() { - // BEGIN(BROWSER) +// BEGIN(BROWSER) - var Handlebars = {}; +var Handlebars = {}; - Handlebars.VERSION = "1.0.rc.1"; +Handlebars.VERSION = "1.0.rc.1"; - Handlebars.helpers = {}; - Handlebars.partials = {}; +Handlebars.helpers = {}; +Handlebars.partials = {}; - Handlebars.registerHelper = function(name, fn, inverse) { - if(inverse) { fn.not = inverse; } - this.helpers[name] = fn; - }; +Handlebars.registerHelper = function(name, fn, inverse) { + if(inverse) { fn.not = inverse; } + this.helpers[name] = fn; +}; - Handlebars.registerPartial = function(name, str) { - this.partials[name] = str; - }; +Handlebars.registerPartial = function(name, str) { + this.partials[name] = str; +}; - Handlebars.registerHelper('helperMissing', function(arg) { - if(arguments.length === 2) { - return undefined; - } else { - throw new Error("Could not find property '" + arg + "'"); - } - }); +Handlebars.registerHelper('helperMissing', function(arg) { + if(arguments.length === 2) { + return undefined; + } else { + throw new Error("Could not find property '" + arg + "'"); + } +}); - var toString = Object.prototype.toString, functionType = "[object Function]"; +var toString = Object.prototype.toString, functionType = "[object Function]"; - Handlebars.registerHelper('blockHelperMissing', function(context, options) { - var inverse = options.inverse || function() {}, fn = options.fn; +Handlebars.registerHelper('blockHelperMissing', function(context, options) { + var inverse = options.inverse || function() {}, fn = options.fn; - var ret = ""; - var type = toString.call(context); + var ret = ""; + var type = toString.call(context); - if(type === functionType) { context = context.call(this); } + if(type === functionType) { context = context.call(this); } - if(context === true) { - return fn(this); - } else if(context === false || context == null) { - return inverse(this); - } else if(type === "[object Array]") { - if(context.length > 0) { - for(var i=0, j=context.length; i 0) { + if(context === true) { + return fn(this); + } else if(context === false || context == null) { + return inverse(this); + } else if(type === "[object Array]") { + if(context.length > 0) { for(var i=0, j=context.length; i 0) { + for(var i=0, j=context.length; i 0) { - this.source[1] = this.source[1] + ", " + locals.join(", "); - } - - // Generate minimizer alias mappings - if (!this.isChild) { - var aliases = []; - for (var alias in this.context.aliases) { - this.source[1] = this.source[1] + ', ' + alias + '=' + this.context.aliases[alias]; - } - } - - if (this.source[1]) { - this.source[1] = "var " + this.source[1].substring(2) + ";"; - } - - // Merge children - if (!this.isChild) { - this.source[1] += '\n' + this.context.programs.join('\n') + '\n'; - } - - if (!this.environment.isSimple) { - this.source.push("return buffer;"); - } - - var params = this.isChild ? ["depth0", "data"] : ["Handlebars", "depth0", "helpers", "partials", "data"]; - - for(var i=0, l=this.environment.depths.list.length; i this.stackVars.length) { this.stackVars.push("stack" + this.stackSlot); } - return "stack" + this.stackSlot; - }, - - popStack: function() { - var item = this.compileStack.pop(); - - if (item instanceof Literal) { - return item.value; - } else { - this.stackSlot--; - return item; - } - }, - - topStack: function() { - var item = this.compileStack[this.compileStack.length - 1]; - - if (item instanceof Literal) { - return item.value; - } else { - return item; - } - }, - - quotedString: function(str) { - return '"' + str - .replace(/\\/g, '\\\\') - .replace(/"/g, '\\"') - .replace(/\n/g, '\\n') - .replace(/\r/g, '\\r') + '"'; - }, - - setupHelper: function(paramSize, name) { - var params = []; - this.setupParams(paramSize, params); - var foundHelper = this.nameLookup('helpers', name, 'helper'); - - return { - params: params, - name: foundHelper, - callParams: ["depth0"].concat(params).join(", "), - helperMissingParams: ["depth0", this.quotedString(name)].concat(params).join(", ") - }; - }, - - // the params and contexts arguments are passed in arrays - // to fill in - setupParams: function(paramSize, params) { - var options = [], contexts = [], param, inverse, program; - - options.push("hash:" + this.popStack()); - - inverse = this.popStack(); - program = this.popStack(); - - // Avoid setting fn and inverse if neither are set. This allows - // helpers to do a check for `if (options.fn)` - if (program || inverse) { - if (!program) { - this.context.aliases.self = "this"; - program = "self.noop"; - } - - if (!inverse) { - this.context.aliases.self = "this"; - inverse = "self.noop"; - } - - options.push("inverse:" + inverse); - options.push("fn:" + program); - } - - for(var i=0; i 0) { + this.source[1] = this.source[1] + ", " + locals.join(", "); + } + + // Generate minimizer alias mappings + if (!this.isChild) { + var aliases = []; + for (var alias in this.context.aliases) { + this.source[1] = this.source[1] + ', ' + alias + '=' + this.context.aliases[alias]; + } + } + + if (this.source[1]) { + this.source[1] = "var " + this.source[1].substring(2) + ";"; + } + + // Merge children + if (!this.isChild) { + this.source[1] += '\n' + this.context.programs.join('\n') + '\n'; + } + + if (!this.environment.isSimple) { + this.source.push("return buffer;"); + } + + var params = this.isChild ? ["depth0", "data"] : ["Handlebars", "depth0", "helpers", "partials", "data"]; + + for(var i=0, l=this.environment.depths.list.length; i this.stackVars.length) { this.stackVars.push("stack" + this.stackSlot); } + return "stack" + this.stackSlot; + }, + + popStack: function() { + var item = this.compileStack.pop(); + + if (item instanceof Literal) { + return item.value; + } else { + this.stackSlot--; + return item; + } + }, + + topStack: function() { + var item = this.compileStack[this.compileStack.length - 1]; + + if (item instanceof Literal) { + return item.value; + } else { + return item; + } + }, + + quotedString: function(str) { + return '"' + str + .replace(/\\/g, '\\\\') + .replace(/"/g, '\\"') + .replace(/\n/g, '\\n') + .replace(/\r/g, '\\r') + '"'; + }, + + setupHelper: function(paramSize, name) { + var params = []; + this.setupParams(paramSize, params); + var foundHelper = this.nameLookup('helpers', name, 'helper'); + + return { + params: params, + name: foundHelper, + callParams: ["depth0"].concat(params).join(", "), + helperMissingParams: ["depth0", this.quotedString(name)].concat(params).join(", ") + }; + }, + + // the params and contexts arguments are passed in arrays + // to fill in + setupParams: function(paramSize, params) { + var options = [], contexts = [], param, inverse, program; + + options.push("hash:" + this.popStack()); + + inverse = this.popStack(); + program = this.popStack(); + + // Avoid setting fn and inverse if neither are set. This allows + // helpers to do a check for `if (options.fn)` + if (program || inverse) { + if (!program) { + this.context.aliases.self = "this"; + program = "self.noop"; + } + + if (!inverse) { + this.context.aliases.self = "this"; + inverse = "self.noop"; + } + + options.push("inverse:" + inverse); + options.push("fn:" + program); + } + + for(var i=0; i " + content + " }}"); - }; - - Handlebars.PrintVisitor.prototype.hash = function(hash) { - var pairs = hash.pairs; - var joinedPairs = [], left, right; - - for(var i=0, l=pairs.length; i 1) { - return "PATH:" + path; - } else { - return "ID:" + path; - } - }; - - Handlebars.PrintVisitor.prototype.DATA = function(data) { - return "@" + data.id; - }; - - Handlebars.PrintVisitor.prototype.content = function(content) { - return this.pad("CONTENT[ '" + content.string + "' ]"); - }; - - Handlebars.PrintVisitor.prototype.comment = function(comment) { - return this.pad("{{! '" + comment.comment + "' }}"); - }; - // END(BROWSER) - - return Handlebars; + if(newline !== false) { out = out + "\n"; } + return out; +}; + +Handlebars.PrintVisitor.prototype.program = function(program) { + var out = "", + statements = program.statements, + inverse = program.inverse, + i, l; + + for(i=0, l=statements.length; i " + content + " }}"); +}; + +Handlebars.PrintVisitor.prototype.hash = function(hash) { + var pairs = hash.pairs; + var joinedPairs = [], left, right; + + for(var i=0, l=pairs.length; i 1) { + return "PATH:" + path; + } else { + return "ID:" + path; + } +}; + +Handlebars.PrintVisitor.prototype.DATA = function(data) { + return "@" + data.id; +}; + +Handlebars.PrintVisitor.prototype.content = function(content) { + return this.pad("CONTENT[ '" + content.string + "' ]"); +}; + +Handlebars.PrintVisitor.prototype.comment = function(comment) { + return this.pad("{{! '" + comment.comment + "' }}"); +}; +// END(BROWSER) + +return Handlebars; }; diff --git a/lib/handlebars/compiler/visitor.js b/lib/handlebars/compiler/visitor.js index 56f262ab..5d073140 100644 --- a/lib/handlebars/compiler/visitor.js +++ b/lib/handlebars/compiler/visitor.js @@ -1,20 +1,18 @@ - exports.attach = function(Handlebars) { - // BEGIN(BROWSER) +// BEGIN(BROWSER) - Handlebars.Visitor = function() {}; +Handlebars.Visitor = function() {}; - Handlebars.Visitor.prototype = { - accept: function(object) { - return this[object.type](object); - } - }; +Handlebars.Visitor.prototype = { + accept: function(object) { + return this[object.type](object); + } +}; - // END(BROWSER) - - return Handlebars; +// END(BROWSER) +return Handlebars; }; diff --git a/lib/handlebars/runtime.js b/lib/handlebars/runtime.js index edf10257..6fd38fd9 100644 --- a/lib/handlebars/runtime.js +++ b/lib/handlebars/runtime.js @@ -1,72 +1,72 @@ exports.attach = function(Handlebars) { - // BEGIN(BROWSER) - - Handlebars.VM = { - template: function(templateSpec) { - // Just add water - var container = { - escapeExpression: Handlebars.Utils.escapeExpression, - invokePartial: Handlebars.VM.invokePartial, - programs: [], - program: function(i, fn, data) { - var programWrapper = this.programs[i]; - if(data) { - return Handlebars.VM.program(fn, data); - } else if(programWrapper) { - return programWrapper; - } else { - programWrapper = this.programs[i] = Handlebars.VM.program(fn); - return programWrapper; - } - }, - programWithDepth: Handlebars.VM.programWithDepth, - noop: Handlebars.VM.noop - }; +// BEGIN(BROWSER) - return function(context, options) { - options = options || {}; - return templateSpec.call(container, Handlebars, context, options.helpers, options.partials, options.data); - }; - }, +Handlebars.VM = { + template: function(templateSpec) { + // Just add water + var container = { + escapeExpression: Handlebars.Utils.escapeExpression, + invokePartial: Handlebars.VM.invokePartial, + programs: [], + program: function(i, fn, data) { + var programWrapper = this.programs[i]; + if(data) { + return Handlebars.VM.program(fn, data); + } else if(programWrapper) { + return programWrapper; + } else { + programWrapper = this.programs[i] = Handlebars.VM.program(fn); + return programWrapper; + } + }, + programWithDepth: Handlebars.VM.programWithDepth, + noop: Handlebars.VM.noop + }; - programWithDepth: function(fn, data, $depth) { - var args = Array.prototype.slice.call(arguments, 2); + return function(context, options) { + options = options || {}; + return templateSpec.call(container, Handlebars, context, options.helpers, options.partials, options.data); + }; + }, - return function(context, options) { - options = options || {}; + programWithDepth: function(fn, data, $depth) { + var args = Array.prototype.slice.call(arguments, 2); - return fn.apply(this, [context, options.data || data].concat(args)); - }; - }, - program: function(fn, data) { - return function(context, options) { - options = options || {}; + return function(context, options) { + options = options || {}; - return fn(context, options.data || data); - }; - }, - noop: function() { return ""; }, - invokePartial: function(partial, name, context, helpers, partials, data) { - var options = { helpers: helpers, partials: partials, data: data }; + return fn.apply(this, [context, options.data || data].concat(args)); + }; + }, + program: function(fn, data) { + return function(context, options) { + options = options || {}; - if(partial === undefined) { - throw new Handlebars.Exception("The partial " + name + " could not be found"); - } else if(partial instanceof Function) { - return partial(context, options); - } else if (!Handlebars.compile) { - throw new Handlebars.Exception("The partial " + name + " could not be compiled when running in runtime-only mode"); - } else { - partials[name] = Handlebars.compile(partial); - return partials[name](context, options); - } + return fn(context, options.data || data); + }; + }, + noop: function() { return ""; }, + invokePartial: function(partial, name, context, helpers, partials, data) { + var options = { helpers: helpers, partials: partials, data: data }; + + if(partial === undefined) { + throw new Handlebars.Exception("The partial " + name + " could not be found"); + } else if(partial instanceof Function) { + return partial(context, options); + } else if (!Handlebars.compile) { + throw new Handlebars.Exception("The partial " + name + " could not be compiled when running in runtime-only mode"); + } else { + partials[name] = Handlebars.compile(partial); + return partials[name](context, options); } - }; + } +}; - Handlebars.template = Handlebars.VM.template; +Handlebars.template = Handlebars.VM.template; - // END(BROWSER) +// END(BROWSER) - return Handlebars; +return Handlebars; }; \ No newline at end of file diff --git a/lib/handlebars/utils.js b/lib/handlebars/utils.js index 0aa89b84..d467205a 100644 --- a/lib/handlebars/utils.js +++ b/lib/handlebars/utils.js @@ -1,72 +1,72 @@ exports.attach = function(Handlebars) { - // BEGIN(BROWSER) +// BEGIN(BROWSER) - Handlebars.Exception = function(message) { - var tmp = Error.prototype.constructor.apply(this, arguments); +Handlebars.Exception = function(message) { + var tmp = Error.prototype.constructor.apply(this, arguments); - for (var p in tmp) { - if (tmp.hasOwnProperty(p)) { this[p] = tmp[p]; } - } + for (var p in tmp) { + if (tmp.hasOwnProperty(p)) { this[p] = tmp[p]; } + } - this.message = tmp.message; - }; - Handlebars.Exception.prototype = new Error(); - - // Build out our basic SafeString type - Handlebars.SafeString = function(string) { - this.string = string; - }; - Handlebars.SafeString.prototype.toString = function() { - return this.string.toString(); - }; - - (function() { - var escape = { - "<": "<", - ">": ">", - '"': """, - "'": "'", - "`": "`" - }; - - var badChars = /&(?!\w+;)|[<>"'`]/g; - var possible = /[&<>"'`]/; - - var escapeChar = function(chr) { - return escape[chr] || "&"; - }; - - Handlebars.Utils = { - escapeExpression: function(string) { - // don't escape SafeStrings, since they're already safe - if (string instanceof Handlebars.SafeString) { - return string.toString(); - } else if (string == null || string === false) { - return ""; - } - - if(!possible.test(string)) { return string; } - return string.replace(badChars, escapeChar); - }, - - isEmpty: function(value) { - if (typeof value === "undefined") { - return true; - } else if (value === null) { - return true; - } else if (value === false) { - return true; - } else if(Object.prototype.toString.call(value) === "[object Array]" && value.length === 0) { - return true; - } else { - return false; - } - } - }; - })(); - - // END(BROWSER) - - return Handlebars; + this.message = tmp.message; +}; +Handlebars.Exception.prototype = new Error(); + +// Build out our basic SafeString type +Handlebars.SafeString = function(string) { + this.string = string; +}; +Handlebars.SafeString.prototype.toString = function() { + return this.string.toString(); +}; + +(function() { + var escape = { + "<": "<", + ">": ">", + '"': """, + "'": "'", + "`": "`" + }; + + var badChars = /&(?!\w+;)|[<>"'`]/g; + var possible = /[&<>"'`]/; + + var escapeChar = function(chr) { + return escape[chr] || "&"; + }; + + Handlebars.Utils = { + escapeExpression: function(string) { + // don't escape SafeStrings, since they're already safe + if (string instanceof Handlebars.SafeString) { + return string.toString(); + } else if (string == null || string === false) { + return ""; + } + + if(!possible.test(string)) { return string; } + return string.replace(badChars, escapeChar); + }, + + isEmpty: function(value) { + if (typeof value === "undefined") { + return true; + } else if (value === null) { + return true; + } else if (value === false) { + return true; + } else if(Object.prototype.toString.call(value) === "[object Array]" && value.length === 0) { + return true; + } else { + return false; + } + } + }; +})(); + +// END(BROWSER) + +return Handlebars; };