Fix number arguments in "allowTags" Handlebars.js helper

This commit is contained in:
Dmitriy Simushev 2014-09-11 12:15:19 +00:00
parent 34017d54db
commit 0b9fdeceb8
2 changed files with 38 additions and 1 deletions

View File

@ -56,7 +56,7 @@
* This helper unescape HTML entities for allowed (span and strong) tags.
*/
Handlebars.registerHelper('allowTags', function(text) {
var result = text;
var result = text.toString();
result = result.replace(
/<(span|strong)>(.*?)<\/\1>/g,
'<$1>$2</$1>'

View File

@ -108,6 +108,43 @@ test('urlReplace', function() {
);
});
// Test "allowTags" Handlebars helper
test('allowTags', function() {
var template = '{{allowTags foo}}';
var compiledTemplate = Handlebars.compile(template);
var escape = Handlebars.Utils.escapeExpression;
equal(
compiledTemplate({foo: escape('<span>The content</span>')}),
'<span>The content</span>',
'Test a tag without attributes'
);
equal(
compiledTemplate({foo: escape('<span class="red">The content</span>')}),
'<span class="red">The content</span>',
'Test a tag with class attribute'
);
equal(
compiledTemplate({foo: escape('<span data-foo="bar">The content</span>')}),
escape('<span data-foo="bar">The content</span>'),
'Test a tag with arbitrary attributes'
);
equal(
compiledTemplate({foo: 'content'}),
'content',
'Test not a tag'
);
equal(
compiledTemplate({foo: 456}),
'456',
'Test not a string argument'
);
});
// Test "nl2br" Handlebars helper
test('nl2br', function() {
var template = '{{nl2br foo}}';