From c3c8de77fcce4fbae4e45d009e7a45341a43acea Mon Sep 17 00:00:00 2001 From: Keegan Date: Tue, 14 Jun 2016 22:57:36 -0700 Subject: [PATCH] Allow response in webviewclosed event to optionally be URI encoded --- .gitignore | 1 + index.js | 3 ++- test/spec/index.js | 18 +++++++++++++++++- 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index d301e89..b578e89 100755 --- a/.gitignore +++ b/.gitignore @@ -8,6 +8,7 @@ # Dependency directory # https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git /node_modules +/npm-debug.log /tmp/ /src/js/index.js diff --git a/index.js b/index.js index 08006f7..7838ae2 100755 --- a/index.js +++ b/index.js @@ -162,9 +162,10 @@ Clay.prototype.generateUrl = function() { Clay.prototype.getSettings = function(response, convert) { // Decode and parse config data as JSON var settings = {}; + response = response.match(/^\{/) ? response : decodeURIComponent(response); try { - settings = JSON.parse(decodeURIComponent(response)); + settings = JSON.parse(response); } catch (e) { throw new Error('The provided response was not valid JSON'); } diff --git a/test/spec/index.js b/test/spec/index.js index ec56aba..0ef30e3 100644 --- a/test/spec/index.js +++ b/test/spec/index.js @@ -239,7 +239,7 @@ describe('Clay', function() { }); describe('.getSettings', function() { - it('stores the response to localStorage and returns the decoded data', + it('it writes to localStorage and returns the data when input is encoded', function() { var clay = fixture.clay([]); var settings = encodeURIComponent(JSON.stringify({ @@ -255,6 +255,22 @@ describe('Clay', function() { assert.deepEqual(result, expected); }); + it('it writes to localStorage and returns the data when input is not encoded', + function() { + var clay = fixture.clay([]); + var settings = JSON.stringify({ + key1: 'value1', + key2: {value: 'value2%7Dbreaks'} + }); + var expected = { + key1: 'value1', + key2: 'value2%7Dbreaks' + }; + var result = clay.getSettings(settings); + assert.equal(localStorage.getItem('clay-settings'), JSON.stringify(expected)); + assert.deepEqual(result, expected); + }); + it('does not store the response if it is invalid JSON and logs an error', function() { var clay = fixture.clay([]);