Fix number arguments in "urlReplace" Handlebars.js helper

This commit is contained in:
Dmitriy Simushev 2014-09-11 11:24:33 +00:00
parent aa12eceeea
commit 34017d54db
2 changed files with 37 additions and 1 deletions

View File

@ -96,7 +96,7 @@
*/
Handlebars.registerHelper('urlReplace', function(text) {
return new Handlebars.SafeString(
text.replace(
text.toString().replace(
/((?:https?|ftp):\/\/\S*)/g,
'<a href="$1" target="_blank">$1</a>'
)

View File

@ -72,6 +72,42 @@ test('apply', function() {
}
});
// Test "urlReplace" Handlebars helper
test('urlReplace', function() {
var template = '{{urlReplace foo}}';
var compiledTemplate = Handlebars.compile(template);
equal(
compiledTemplate({foo: 'http://example.com'}),
'<a href="http://example.com" target="_blank">http://example.com</a>',
'Test HTTP URL'
);
equal(
compiledTemplate({foo: 'https://example.com'}),
'<a href="https://example.com" target="_blank">https://example.com</a>',
'Test HTTPS URL'
);
equal(
compiledTemplate({foo: 'ftp://example.com'}),
'<a href="ftp://example.com" target="_blank">ftp://example.com</a>',
'Test FTP URL'
);
equal(
compiledTemplate({foo: 'plain text'}),
'plain text',
'Test not a URL'
);
equal(
compiledTemplate({foo: 456}),
'456',
'Test number argument'
);
});
// Test "nl2br" Handlebars helper
test('nl2br', function() {
var template = '{{nl2br foo}}';