From 788987d04ac4d84ad8a670b1116ac55c5ea725dd Mon Sep 17 00:00:00 2001 From: Fabian Becker Date: Tue, 15 Oct 2013 18:14:10 +0000 Subject: [PATCH] Added new helper to escape URIs called 'encode' fixes #1089 --- core/server/helpers/index.js | 13 +++++++++++++ core/test/unit/server_helpers_index_spec.js | 18 +++++++++++++++++- 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/core/server/helpers/index.js b/core/server/helpers/index.js index 6d96222f12..f4b8967e0f 100644 --- a/core/server/helpers/index.js +++ b/core/server/helpers/index.js @@ -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:* diff --git a/core/test/unit/server_helpers_index_spec.js b/core/test/unit/server_helpers_index_spec.js index 5499f30c4a..ac8571a21c 100644 --- a/core/test/unit/server_helpers_index_spec.js +++ b/core/test/unit/server_helpers_index_spec.js @@ -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); }); }); -}); \ No newline at end of file +});