From b2492aeec4f2cab86851a97e3cc0ef0d7db3187a Mon Sep 17 00:00:00 2001 From: Juriy Zaytsev Date: Tue, 26 May 2009 16:26:33 -0400 Subject: [PATCH] Fix `Template#evaluate` "eating" previous character if `null` was returned from `toTemplateReplacements` function. --- CHANGELOG | 2 ++ src/lang/template.js | 4 ++-- test/unit/string_test.js | 6 ++++++ 3 files changed, 10 insertions(+), 2 deletions(-) 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() {