Fix number arguments in "nl2br" Handlebars.js helper

This commit is contained in:
Dmitriy Simushev 2014-09-11 11:14:10 +00:00
parent 9805b4bb18
commit aa12eceeea
2 changed files with 21 additions and 1 deletions

View File

@ -109,7 +109,9 @@
* This helper replace all new line characters (\n) by 'br' tags
*/
Handlebars.registerHelper('nl2br', function(text) {
return new Handlebars.SafeString(text.replace(/\n/g, "<br/>"));
return new Handlebars.SafeString(
text.toString().replace(/\n/g, "<br/>")
);
});
/**

View File

@ -72,6 +72,24 @@ test('apply', function() {
}
});
// Test "nl2br" Handlebars helper
test('nl2br', function() {
var template = '{{nl2br foo}}';
var compiledTemplate = Handlebars.compile(template);
equal(
compiledTemplate({foo: 'Hello\ncruel\nworld!\n'}),
'Hello<br/>cruel<br/>world!<br/>',
'Test simple replacements'
);
equal(
compiledTemplate({foo: 456}),
'456',
'Test number argument'
);
});
// Test "ifEven" Handlebars helper
test('ifEven', function() {
var template = '{{#ifEven foo}}true{{else}}false{{/ifEven}}';