From 6154992f4fb5ef9988530b74837316c31dfc4337 Mon Sep 17 00:00:00 2001 From: Dmitriy Simushev Date: Thu, 11 Sep 2014 12:30:11 +0000 Subject: [PATCH] Remove "apply" Handlebars.js helper --- .../js/source/default/handlebars_helpers.js | 33 --------- .../client_side/chat/message.handlebars | 2 +- .../client_side/message.handlebars | 2 +- .../client_side/default/message.handlebars | 2 +- .../test_cases/handlebars_helpers_tests.js | 71 ------------------- 5 files changed, 3 insertions(+), 107 deletions(-) diff --git a/src/mibew/js/source/default/handlebars_helpers.js b/src/mibew/js/source/default/handlebars_helpers.js index bbfff4d9..c4308df6 100644 --- a/src/mibew/js/source/default/handlebars_helpers.js +++ b/src/mibew/js/source/default/handlebars_helpers.js @@ -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: - * - * {{apply text "emHelper, strongHelper"}} - * - * 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. * diff --git a/src/mibew/styles/dialogs/default/templates_src/client_side/chat/message.handlebars b/src/mibew/styles/dialogs/default/templates_src/client_side/chat/message.handlebars index 5ceeeac6..3137ac76 100644 --- a/src/mibew/styles/dialogs/default/templates_src/client_side/chat/message.handlebars +++ b/src/mibew/styles/dialogs/default/templates_src/client_side/chat/message.handlebars @@ -1,3 +1,3 @@ {{formatTime created}} {{#if name}}{{name}}: {{/if}} -{{#if allowFormatting}}{{apply message "urlReplace, nl2br, allowTags"}}{{else}}{{apply message "urlReplace, nl2br"}}{{/if}}
\ No newline at end of file +{{#if allowFormatting}}{{allowTags (nl2br (urlReplace message))}}{{else}}{{nl2br (urlReplace message)}}{{/if}}
\ No newline at end of file diff --git a/src/mibew/styles/dialogs/default/templates_src/client_side/message.handlebars b/src/mibew/styles/dialogs/default/templates_src/client_side/message.handlebars index 472ee744..6f751bd7 100644 --- a/src/mibew/styles/dialogs/default/templates_src/client_side/message.handlebars +++ b/src/mibew/styles/dialogs/default/templates_src/client_side/message.handlebars @@ -1,3 +1,3 @@ {{formatTime created}} {{#if name}}{{name}}: {{/if}} -{{#if allowFormatting}}{{apply message "urlReplace, nl2br, allowTags"}}{{else}}{{apply message "urlReplace, nl2br"}}{{/if}}
\ No newline at end of file +{{#if allowFormatting}}{{allowTags (nl2br (urlReplace message))}}{{else}}{{nl2br (urlReplace message)}}{{/if}}
\ No newline at end of file diff --git a/src/mibew/styles/pages/default/templates_src/client_side/default/message.handlebars b/src/mibew/styles/pages/default/templates_src/client_side/default/message.handlebars index 472ee744..6f751bd7 100644 --- a/src/mibew/styles/pages/default/templates_src/client_side/default/message.handlebars +++ b/src/mibew/styles/pages/default/templates_src/client_side/default/message.handlebars @@ -1,3 +1,3 @@ {{formatTime created}} {{#if name}}{{name}}: {{/if}} -{{#if allowFormatting}}{{apply message "urlReplace, nl2br, allowTags"}}{{else}}{{apply message "urlReplace, nl2br"}}{{/if}}
\ No newline at end of file +{{#if allowFormatting}}{{allowTags (nl2br (urlReplace message))}}{{else}}{{nl2br (urlReplace message)}}{{/if}}
\ No newline at end of file diff --git a/src/tests/client_side/qunit/test_cases/handlebars_helpers_tests.js b/src/tests/client_side/qunit/test_cases/handlebars_helpers_tests.js index c8d88e35..521ffecb 100644 --- a/src/tests/client_side/qunit/test_cases/handlebars_helpers_tests.js +++ b/src/tests/client_side/qunit/test_cases/handlebars_helpers_tests.js @@ -1,77 +1,6 @@ // Testing Handlebars helpers module('Handlebars helpers'); -// Register test emphasis helper -Handlebars.registerHelper('emTestHelper', function(text) { - return new Handlebars.SafeString('' + text + ''); -}); - -// Register test strong helper -Handlebars.registerHelper('strongTestHelper', function(text) { - return new Handlebars.SafeString('' + text + ''); -}); - -// 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, - 'some_text', - '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, - 'some_text', - '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, - 'some_text', - '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, - 'some_text', - '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}}';