Added new helper to escape URIs called 'encode'

fixes #1089
This commit is contained in:
Fabian Becker 2013-10-15 18:14:10 +00:00
parent 35347795a8
commit 788987d04a
2 changed files with 30 additions and 1 deletions

View File

@ -47,6 +47,19 @@ coreHelpers = function (ghost) {
return date;
});
//
// ### URI Encoding helper
//
// *Usage example:*
// `{{encode uri}}`
//
// Returns URI encoded string
//
ghost.registerThemeHelper('encode', function (context, str) {
var uri = context || str;
return new hbs.handlebars.SafeString(encodeURIComponent(uri));
});
// ### Page URL Helper
//
// *Usage example:*

View File

@ -86,6 +86,22 @@ describe('Core Helpers', function () {
});
describe('encode Helper', function () {
it('has loaded encode helper', function() {
should.exist(handlebars.helpers.encode);
});
it('can escape URI', function() {
var uri = "$pecial!Charact3r(De[iver]y)Foo #Bar",
expected = "%24pecial!Charact3r(De%5Biver%5Dy)Foo%20%23Bar",
escaped = handlebars.helpers.encode(uri);
should.exist(escaped);
String(escaped).should.equal(expected);
});
});
describe('Excerpt Helper', function () {
it('has loaded excerpt helper', function () {
@ -367,4 +383,4 @@ describe('Core Helpers', function () {
}).then(null, done);
});
});
});
});