diff --git a/CHANGELOG b/CHANGELOG index a8df558..fee3d3b 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,5 @@ +* Fix `Template#evaluate` "eating" previous character if `null` was returned from `toTemplateReplacements` function. (Nir, Jürgen Hörmann, kangax) + * Make sure (deficient) APPLET, OBJECT and EMBED elements are extended with simulated methods in IE8. Return early if `_extendedByPrototype` is present on an element. (Tobie Langel, kangax) * Replace array creation and `Array#include` with a more efficient `RegExp#test`. (kangax) diff --git a/src/lang/template.js b/src/lang/template.js index 9cb227d..292dd79 100644 --- a/src/lang/template.js +++ b/src/lang/template.js @@ -124,11 +124,11 @@ var Template = Class.create({ * with symbols replaced by `object`’s corresponding properties. **/ evaluate: function(object) { - if (Object.isFunction(object.toTemplateReplacements)) + if (object && Object.isFunction(object.toTemplateReplacements)) object = object.toTemplateReplacements(); return this.template.gsub(this.pattern, function(match) { - if (object == null) return ''; + if (object == null) return (match[1] + ''); var before = match[1] || ''; if (before == '\\') return match[2]; diff --git a/test/unit/string_test.js b/test/unit/string_test.js index 93308a4..6b910d9 100644 --- a/test/unit/string_test.js +++ b/test/unit/string_test.js @@ -342,6 +342,12 @@ new Test.Unit.Runner({ toTemplateReplacements: function() { return { name: this.name, job: this.getJob() } } }; this.assertEqual('My name is Stephan, my job is Web developer', new Template(source).evaluate(subject)); + + var strActual = new Template('foo #{bar} baz').evaluate({ + toTemplateReplacements: function(){ return null; } + }); + this.assertIdentical('foo baz', strActual); + this.assertIdentical('foo', new Template('foo#{bar}').evaluate(null)); }, testTemplateEvaluationCombined: function() {