Remove "apply" Handlebars.js helper

This commit is contained in:
Dmitriy Simushev 2014-09-11 12:30:11 +00:00
parent 0b9fdeceb8
commit 6154992f4f
5 changed files with 3 additions and 107 deletions

View File

@ -17,39 +17,6 @@
*/
(function(Mibew, Handlebars){
/**
* Register 'apply' Handlebars helper.
*
* This helper provide an ability to apply several helpers to single
* Handlebars expression
*
* Example of helper usage:
* <code>
* {{apply text "emHelper, strongHelper"}}
* </code>
* In the example above helpers will apply to text one after another: first
* 'emHelper' and second 'strongHelper'.
*/
Handlebars.registerHelper('apply', function(text, helpers) {
var result = text;
var validHelperName = /^[0-9A-z_]+$/;
helpers = helpers.split(/\s*,\s*/);
// Apply helpers one after another
for (var prop in helpers) {
if (! helpers.hasOwnProperty(prop) ||
! validHelperName.test(helpers[prop])) {
continue;
}
if (typeof Handlebars.helpers[helpers[prop]] != 'function') {
throw new Error(
"Unregistered helper '" + helpers[prop] + "'!"
);
}
result = Handlebars.helpers[helpers[prop]](result).toString();
}
return new Handlebars.SafeString(result);
});
/**
* Register 'allowTags' Handlebars helper.
*

View File

@ -1,3 +1,3 @@
<span>{{formatTime created}}</span>
{{#if name}}<span class='n{{kindName}}'>{{name}}</span>: {{/if}}
<span class='m{{kindName}}'>{{#if allowFormatting}}{{apply message "urlReplace, nl2br, allowTags"}}{{else}}{{apply message "urlReplace, nl2br"}}{{/if}}</span><br/>
<span class='m{{kindName}}'>{{#if allowFormatting}}{{allowTags (nl2br (urlReplace message))}}{{else}}{{nl2br (urlReplace message)}}{{/if}}</span><br/>

View File

@ -1,3 +1,3 @@
<span>{{formatTime created}}</span>
{{#if name}}<span class='n{{kindName}}'>{{name}}</span>: {{/if}}
<span class='m{{kindName}}'>{{#if allowFormatting}}{{apply message "urlReplace, nl2br, allowTags"}}{{else}}{{apply message "urlReplace, nl2br"}}{{/if}}</span><br/>
<span class='m{{kindName}}'>{{#if allowFormatting}}{{allowTags (nl2br (urlReplace message))}}{{else}}{{nl2br (urlReplace message)}}{{/if}}</span><br/>

View File

@ -1,3 +1,3 @@
<span>{{formatTime created}}</span>
{{#if name}}<span class='n{{kindName}}'>{{name}}</span>: {{/if}}
<span class='m{{kindName}}'>{{#if allowFormatting}}{{apply message "urlReplace, nl2br, allowTags"}}{{else}}{{apply message "urlReplace, nl2br"}}{{/if}}</span><br/>
<span class='m{{kindName}}'>{{#if allowFormatting}}{{allowTags (nl2br (urlReplace message))}}{{else}}{{nl2br (urlReplace message)}}{{/if}}</span><br/>

View File

@ -1,77 +1,6 @@
// Testing Handlebars helpers
module('Handlebars helpers');
// Register test emphasis helper
Handlebars.registerHelper('emTestHelper', function(text) {
return new Handlebars.SafeString('<em>' + text + '</em>');
});
// Register test strong helper
Handlebars.registerHelper('strongTestHelper', function(text) {
return new Handlebars.SafeString('<strong>' + text + '</strong>');
});
// Test 'apply' Handlebars helper
test('apply', function() {
// Test application of two valid helpers to text.
// There are no spaces before or after comma.
var template = '{{apply text "emTestHelper,strongTestHelper"}}';
var compiledTemplate = Handlebars.compile(template);
var output = compiledTemplate({text: "some_text"});
equal(
output,
'<strong><em>some_text</em></strong>',
'Test two valid helpers'
);
// Test application of two valid helpers in reverse order to text.
// There are no spaces before or after comma.
template = '{{apply text "strongTestHelper,emTestHelper"}}';
compiledTemplate = Handlebars.compile(template);
output = compiledTemplate({text: "some_text"});
equal(
output,
'<em><strong>some_text</strong></em>',
'Test two valid helpers in reverse order'
);
// Test application of two valid helpers to text.
// There are some spaces before and after comma.
template = '{{apply text "emTestHelper , strongTestHelper"}}';
compiledTemplate = Handlebars.compile(template);
output = compiledTemplate({text: "some_text"});
equal(
output,
'<strong><em>some_text</em></strong>',
'Test two valid helpers with some spaces before and after comma'
);
// Test application of one valid helper and one with wrong name to text.
// There are no spaces before or after comma.
template = '{{apply text "emTestHelper,$strongTestHelper"}}';
compiledTemplate = Handlebars.compile(template);
output = compiledTemplate({text: "some_text"});
equal(
output,
'<em>some_text</em>',
'Test one valid helper and one with wrong name'
);
// Test application of one valid helper and one unregistered helper to text.
// There are no spaces before or after comma.
template = '{{apply text "emTestHelper,unregisteredTestHelper"}}';
compiledTemplate = Handlebars.compile(template);
try {
output = compiledTemplate({text: "some_text"});
} catch(e) {
equal(
e.message,
"Unregistered helper 'unregisteredTestHelper'!",
'Test one valid helper and one unregistered helper'
);
}
});
// Test "urlReplace" Handlebars helper
test('urlReplace', function() {
var template = '{{urlReplace foo}}';