Fix Template#evaluate "eating" previous character if null was returned from toTemplateReplacements function.

This commit is contained in:
Juriy Zaytsev 2009-05-26 16:26:33 -04:00 committed by Andrew Dupont
parent d655665fb6
commit b2492aeec4
3 changed files with 10 additions and 2 deletions

View File

@ -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)

View File

@ -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];

View File

@ -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() {