Skip to content

Commit

Permalink
Fix Template#evaluate "eating" previous character if null was ret…
Browse files Browse the repository at this point in the history
…urned from `toTemplateReplacements` function.
  • Loading branch information
Juriy Zaytsev committed May 26, 2009
1 parent c2f4508 commit ae40c03
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG
@@ -1,3 +1,5 @@
* Fix `Template#evaluate` "eating" previous character if `null` was returned from `toTemplateReplacements` function. (Nir, Jürgen Hörmann, kangax)

* Do not use relatively slow `new RegExp` in `String#extractScripts`. (MIYAMUKO Katsuyuki, kangax)

* Element#readAttribute should always return string values or `null`. Fix IE's broken attributes model by converting to string any non-string value returned by `getAttribute` which is identical to the same-named property. (Jason B, kangax)
Expand Down
4 changes: 2 additions & 2 deletions src/lang/template.js
Expand Up @@ -122,11 +122,11 @@ var Template = Class.create({
* with symbols replaced by corresponding object’s 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];
Expand Down
8 changes: 8 additions & 0 deletions test/unit/string_test.js
@@ -1,5 +1,6 @@
new Test.Unit.Runner({
testInterpret: function(){
console.profile(1);
this.assertIdentical('true', String.interpret(true));
this.assertIdentical('123', String.interpret(123));
this.assertIdentical('foo bar', String.interpret('foo bar'));
Expand Down Expand Up @@ -348,6 +349,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() {
Expand Down Expand Up @@ -551,5 +558,6 @@ new Test.Unit.Runner({
this.assertIdentical(true, 'true'.evalJSON());
this.assertIdentical(false, 'false'.evalJSON());
this.assertEqual('"', '"\\""'.evalJSON());
console.profileEnd(1);
}
});

2 comments on commit ae40c03

@savetheclocktower
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Committed. Took out the console.profile calls, though — won't those raise errors in IE?

@kangax
Copy link
Owner

@kangax kangax commented on ae40c03 Jun 5, 2009

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Of course they will : )
That was an oversight on my part; I took them out in the following commit.

Please sign in to comment.