Add several Handlebars helpers

Add 'apply', 'formatTime' and 'nl2br' Handlebars helpers.
Also add QUnit tests for 'apply' helper.
This commit is contained in:
Dmitriy Simushev 2012-10-05 11:53:04 +00:00
parent 36c8e6c042
commit 83636a02ef
4 changed files with 163 additions and 0 deletions

View File

@ -20,5 +20,11 @@
<script type="text/javascript" src="/webim/js/164/pluginmanager.js"></script>
<script type="text/javascript" src="test_cases/pluginmanager_tests.js"></script>
<!-- End of the mibew_api.js class tests -->
<!-- Start of Handlebars' helpers tests -->
<script type="text/javascript" src="/webim/js/164/handlebars.js"></script>
<script type="text/javascript" src="/webim/js/164/handlebars_helpers.js"></script>
<script type="text/javascript" src="test_cases/handlebars_helpers_tests.js"></script>
<!-- Start of Handlebars' helpers tests -->
</body>
</html>

View File

@ -0,0 +1,73 @@
// 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'
);
}
});

View File

@ -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<b?b:"0"+b)+":"+(10<c?c:"0"+c)+":"+(10<a?a:"0"+a)});Handlebars.registerHelper("urlReplace",function(b){return new Handlebars.SafeString(b.replace(/((?:https?|ftp):\/\/\S*)/g,'<a href="$1" target="_blank">$1</a>'))});Handlebars.registerHelper("nl2br",function(b){return new Handlebars.SafeString(b.replace(/\n/g,"<br/>"))});

View File

@ -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,
'<a href="$1" target="_blank">$1</a>'
)
);
});
/**
* 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, "<br/>"));
});