From 83636a02efa0f9680dec6efbcea20177e4f1b8ad Mon Sep 17 00:00:00 2001 From: Dmitriy Simushev Date: Fri, 5 Oct 2012 11:53:04 +0000 Subject: [PATCH] Add several Handlebars helpers Add 'apply', 'formatTime' and 'nl2br' Handlebars helpers. Also add QUnit tests for 'apply' helper. --- .../tests/client_side/qunit/index.html | 6 ++ .../test_cases/handlebars_helpers_tests.js | 73 ++++++++++++++++++ .../webim/js/164/handlebars_helpers.js | 9 +++ .../webim/js/source/handlebars_helpers.js | 75 +++++++++++++++++++ 4 files changed, 163 insertions(+) create mode 100644 src/messenger/tests/client_side/qunit/test_cases/handlebars_helpers_tests.js create mode 100644 src/messenger/webim/js/164/handlebars_helpers.js create mode 100644 src/messenger/webim/js/source/handlebars_helpers.js diff --git a/src/messenger/tests/client_side/qunit/index.html b/src/messenger/tests/client_side/qunit/index.html index 6b7ee313..52bc74b8 100644 --- a/src/messenger/tests/client_side/qunit/index.html +++ b/src/messenger/tests/client_side/qunit/index.html @@ -20,5 +20,11 @@ + + + + + + diff --git a/src/messenger/tests/client_side/qunit/test_cases/handlebars_helpers_tests.js b/src/messenger/tests/client_side/qunit/test_cases/handlebars_helpers_tests.js new file mode 100644 index 00000000..6b9231e5 --- /dev/null +++ b/src/messenger/tests/client_side/qunit/test_cases/handlebars_helpers_tests.js @@ -0,0 +1,73 @@ +// 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' + ); + } +}); \ No newline at end of file diff --git a/src/messenger/webim/js/164/handlebars_helpers.js b/src/messenger/webim/js/164/handlebars_helpers.js new file mode 100644 index 00000000..367a8a8e --- /dev/null +++ b/src/messenger/webim/js/164/handlebars_helpers.js @@ -0,0 +1,9 @@ +/* + This file is part of Mibew Messenger project. + http://mibew.org + + Copyright (c) 2005-2011 Mibew Messenger Community + License: http://mibew.org/license.php +*/ +Handlebars.registerHelper("apply",function(b,a){var c=b,e=/^[0-9A-z_]+$/,a=a.split(/\s*,\s*/),d;for(d in a)if(a.hasOwnProperty(d)&&e.test(a[d])){if("function"!=typeof Handlebars.helpers[a[d]])throw Error("Unregistered helper '"+a[d]+"'!");c=Handlebars.helpers[a[d]](c)}return new Handlebars.SafeString(c)}); +Handlebars.registerHelper("formatTime",function(b){var a=new Date(1E3*b),b=a.getHours().toString(),c=a.getMinutes().toString(),a=a.getSeconds().toString();return(10$1'))});Handlebars.registerHelper("nl2br",function(b){return new Handlebars.SafeString(b.replace(/\n/g,"
"))}); \ No newline at end of file diff --git a/src/messenger/webim/js/source/handlebars_helpers.js b/src/messenger/webim/js/source/handlebars_helpers.js new file mode 100644 index 00000000..39a9753c --- /dev/null +++ b/src/messenger/webim/js/source/handlebars_helpers.js @@ -0,0 +1,75 @@ +/** + * @preserve This file is part of Mibew Messenger project. + * http://mibew.org + * + * Copyright (c) 2005-2011 Mibew Messenger Community + * License: http://mibew.org/license.php + */ + +/** + * Register 'apply' Handlebars helper. + * + * This helper provide an ability to apply several helpers to single Handlebars + * expression + */ +Handlebars.registerHelper('apply', function(text, helpers) { + var result = text; + var validHelperName = /^[0-9A-z_]+$/; + helpers = helpers.split(/\s*,\s*/); + 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); + } + return new Handlebars.SafeString(result); +}); + +/** + * Register 'formatTime' Handlebars helper. + * + * This helper takes unix timestamp as argument and return time in "HH:MM:SS" + * format + */ +Handlebars.registerHelper('formatTime', function(unixTimestamp){ + var d = new Date(unixTimestamp * 1000); + // Get time parts + var hours = d.getHours().toString(); + var minutes = d.getMinutes().toString(); + var seconds = d.getSeconds().toString(); + // Add leading zero if needed + hours = hours > 10 ? hours : '0' + hours; + minutes = minutes > 10 ? minutes : '0' + minutes; + seconds = seconds > 10 ? seconds : '0' + seconds; + // Build result string + return hours + ':' + minutes + ':' + seconds; +}); + +/** + * Register 'urlReplace' Handlebars helper. + * + * This helper serch URLs and replace them by 'a' tag + */ +Handlebars.registerHelper('urlReplace', function(text) { + return new Handlebars.SafeString( + text.replace( + /((?:https?|ftp):\/\/\S*)/g, + '$1' + ) + ); +}); + +/** + * Register 'nl2br' Handlebars helper. + * + * This helper replace all new line characters (\n) by 'br' tags + */ +Handlebars.registerHelper('nl2br', function(text) { + return new Handlebars.SafeString(text.replace(/\n/g, "
")); +}); \ No newline at end of file