summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorkpdecker <kpdecker@gmail.com>2013-09-02 18:19:18 -0500
committerkpdecker <kpdecker@gmail.com>2013-09-02 18:19:18 -0500
commitcb0c45b29fa9df0b912d843e3a695293f1d10cad (patch)
treecf50b624e59f3beee7d9e7c89be67d3dae7c6b8b /lib
parent192887cedce6e6155bb1a079ab2802ff28fbd2bf (diff)
parent0fe78f379ab85e586381e167aecd7d5527984697 (diff)
downloadhandlebars.js-cb0c45b29fa9df0b912d843e3a695293f1d10cad.zip
handlebars.js-cb0c45b29fa9df0b912d843e3a695293f1d10cad.tar.gz
handlebars.js-cb0c45b29fa9df0b912d843e3a695293f1d10cad.tar.bz2
Merge branch 'master' into es6-modules
Conflicts: Gruntfile.js Rakefile dist/handlebars.js dist/handlebars.runtime.js lib/handlebars.js lib/handlebars/base.js lib/handlebars/runtime.js lib/handlebars/utils.js package.json
Diffstat (limited to 'lib')
-rw-r--r--lib/handlebars/base.js41
-rw-r--r--lib/handlebars/runtime.js57
-rw-r--r--lib/handlebars/utils.js7
3 files changed, 67 insertions, 38 deletions
diff --git a/lib/handlebars/base.js b/lib/handlebars/base.js
index a6cd7ff..b375554 100644
--- a/lib/handlebars/base.js
+++ b/lib/handlebars/base.js
@@ -15,9 +15,24 @@ export var REVISION_CHANGES = {
};
var toString = Object.prototype.toString,
- functionType = '[object Function]',
objectType = '[object Object]';
+// Sourced from lodash
+// https://github.com/bestiejs/lodash/blob/master/LICENSE.txt
+function isFunction(value) {
+ return typeof value === 'function';
+}
+// fallback for older versions of Chrome and Safari
+if (isFunction(/x/)) {
+ isFunction = function(value) {
+ return typeof value === 'function' && toString.call(value) === '[object Function]';
+ };
+}
+
+function isArray(value) {
+ return (value && typeof value === 'object') ? toString.call(value) === '[object Array]' : false;
+};
+
export function HandlebarsEnvironment(helpers, partials) {
this.helpers = helpers || {};
this.partials = partials || {};
@@ -59,15 +74,13 @@ function registerDefaultHelpers(instance) {
instance.registerHelper('blockHelperMissing', function(context, options) {
var inverse = options.inverse || function() {}, fn = options.fn;
- var type = toString.call(context);
-
- if(type === functionType) { context = context.call(this); }
+ if (isFunction(context)) { context = context.call(this); }
if(context === true) {
return fn(this);
} else if(context === false || context == null) {
return inverse(this);
- } else if(type === "[object Array]") {
+ } else if (isArray(context)) {
if(context.length > 0) {
return instance.helpers.each(context, options);
} else {
@@ -82,15 +95,14 @@ function registerDefaultHelpers(instance) {
var fn = options.fn, inverse = options.inverse;
var i = 0, ret = "", data;
- var type = toString.call(context);
- if(type === functionType) { context = context.call(this); }
+ if (isFunction(context)) { context = context.call(this); }
if (options.data) {
data = createFrame(options.data);
}
if(context && typeof context === 'object') {
- if(context instanceof Array){
+ if (isArray(context)) {
for(var j = context.length; i<j; i++) {
if (data) { data.index = i; }
ret = ret + fn(context[i], { data: data });
@@ -114,8 +126,7 @@ function registerDefaultHelpers(instance) {
});
instance.registerHelper('if', function(conditional, options) {
- var type = toString.call(conditional);
- if(type === functionType) { conditional = conditional.call(this); }
+ if (isFunction(conditional)) { conditional = conditional.call(this); }
if (isEmpty(conditional)) {
return options.inverse(this);
@@ -129,8 +140,7 @@ function registerDefaultHelpers(instance) {
});
instance.registerHelper('with', function(context, options) {
- var type = toString.call(context);
- if(type === functionType) { context = context.call(this); }
+ if (isFunction(context)) { context = context.call(this); }
if (!isEmpty(context)) return options.fn(context);
});
@@ -161,9 +171,8 @@ export var logger = {
export function log(level, obj) { logger.log(level, obj); };
-export var createFrame = Object.create || function(object) {
- K.prototype = object;
- var obj = new K();
- K.prototype = null;
+export var createFrame = function(object) {
+ var obj = {};
+ Handlebars.Utils.extend(obj, object);
return obj;
};
diff --git a/lib/handlebars/runtime.js b/lib/handlebars/runtime.js
index 74361e8..eeef182 100644
--- a/lib/handlebars/runtime.js
+++ b/lib/handlebars/runtime.js
@@ -1,11 +1,31 @@
import { escapeExpression, extend, Exception } from "./utils";
import { COMPILER_REVISION, REVISION_CHANGES } from "./base";
+function checkRevision(compilerInfo) {
+ var compilerRevision = compilerInfo && compilerInfo[0] || 1,
+ currentRevision = Handlebars.COMPILER_REVISION;
+
+ if (compilerRevision !== currentRevision) {
+ if (compilerRevision < currentRevision) {
+ var runtimeVersions = Handlebars.REVISION_CHANGES[currentRevision],
+ compilerVersions = Handlebars.REVISION_CHANGES[compilerRevision];
+ throw "Template was precompiled with an older version of Handlebars than the current runtime. "+
+ "Please update your precompiler to a newer version ("+runtimeVersions+") or downgrade your runtime to an older version ("+compilerVersions+").";
+ } else {
+ // Use the embedded version info since the runtime doesn't know about this revision yet
+ throw "Template was precompiled with a newer version of Handlebars than the current runtime. "+
+ "Please update your runtime to a newer version ("+compilerInfo[1]+").";
+ }
+ }
+}
+
// TODO: Remove this line and break up compilePartial
export function template(templateSpec, Hbars, compile) {
if (compile) {
var invokePartialWrapper = function(partial, name, context, helpers, partials, data) {
+ // TODO : Check this for all inputs and the options handling (partial flag, etc). This feels
+ // like there should be a common exec path
var result = invokePartial.apply(this, arguments);
if (result) { return result; }
@@ -42,7 +62,7 @@ export function template(templateSpec, Hbars, compile) {
merge: function(param, common) {
var ret = param || common;
- if (param && common) {
+ if (param && common && (param !== common)) {
ret = {};
extend(ret, common);
extend(ret, param);
@@ -56,24 +76,23 @@ export function template(templateSpec, Hbars, compile) {
return function(context, options) {
options = options || {};
+ var namespace = options.partial ? options : Handlebars,
+ helpers,
+ partials;
- var result = templateSpec.call(container, Hbars, context, options.helpers, options.partials, options.data);
-
- var compilerInfo = container.compilerInfo || [],
- compilerRevision = compilerInfo[0] || 1,
- currentRevision = COMPILER_REVISION;
-
- if (compilerRevision !== currentRevision) {
- if (compilerRevision < currentRevision) {
- var runtimeVersions = REVISION_CHANGES[currentRevision],
- compilerVersions = REVISION_CHANGES[compilerRevision];
- throw "Template was precompiled with an older version of Handlebars than the current runtime. "+
- "Please update your precompiler to a newer version ("+runtimeVersions+") or downgrade your runtime to an older version ("+compilerVersions+").";
- } else {
- // Use the embedded version info since the runtime doesn't know about this revision yet
- throw "Template was precompiled with a newer version of Handlebars than the current runtime. "+
- "Please update your runtime to a newer version ("+compilerInfo[1]+").";
- }
+ if (!options.partial) {
+ helpers = options.helpers;
+ partials = options.partials;
+ }
+ var result = templateSpec.call(
+ container,
+ namespace, context,
+ helpers,
+ partials,
+ options.data);
+
+ if (!options.partial) {
+ checkRevision(container.compilerInfo);
}
return result;
@@ -105,7 +124,7 @@ export function program(i, fn, data) {
}
export function invokePartial(partial, name, context, helpers, partials, data) {
- var options = { helpers: helpers, partials: partials, data: data };
+ var options = { partial: true, helpers: helpers, partials: partials, data: data };
if(partial === undefined) {
throw new Exception("The partial " + name + " could not be found");
diff --git a/lib/handlebars/utils.js b/lib/handlebars/utils.js
index 26d2b54..458c46d 100644
--- a/lib/handlebars/utils.js
+++ b/lib/handlebars/utils.js
@@ -1,4 +1,5 @@
-var toString = Object.prototype.toString;
+var toString = Object.prototype.toString,
+ isArray = Array.isArray;
var errorProps = ['description', 'fileName', 'lineNumber', 'message', 'name', 'number', 'stack'];
@@ -50,7 +51,7 @@ export function escapeExpression(string) {
// don't escape SafeStrings, since they're already safe
if (string instanceof SafeString) {
return string.toString();
- } else if (string == null || string === false) {
+ } else if (!string && string !== 0) {
return "";
}
@@ -66,7 +67,7 @@ export function escapeExpression(string) {
export function isEmpty(value) {
if (!value && value !== 0) {
return true;
- } else if(toString.call(value) === "[object Array]" && value.length === 0) {
+ } else if (isArray(value) && value.length === 0) {
return true;
} else {
return false;