Skip to content

Commit

Permalink
Merge pull request ScottHamper#48 from ScottHamper/Issue-47
Browse files Browse the repository at this point in the history
Resolved issue ScottHamper#47
  • Loading branch information
ScottHamper committed Sep 10, 2015
2 parents 060f8ae + 0bc2c8f commit f0bb95d
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 13 deletions.
1 change: 0 additions & 1 deletion dist/cookies.min.js

This file was deleted.

18 changes: 15 additions & 3 deletions src/cookies.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@
if (Cookies._cachedDocumentCookie !== Cookies._document.cookie) {
Cookies._renewCache();
}

var value = Cookies._cache[Cookies._cacheKeyPrefix + key];

return Cookies._cache[Cookies._cacheKeyPrefix + key];
return value === undefined ? undefined : decodeURIComponent(value);
};

Cookies.set = function (key, value, options) {
Expand Down Expand Up @@ -119,9 +121,19 @@
// IE omits the "=" when the cookie value is an empty string
separatorIndex = separatorIndex < 0 ? cookieString.length : separatorIndex;

var key = cookieString.substr(0, separatorIndex);
var decodedKey;
try {
decodedKey = decodeURIComponent(key);
} catch (e) {
if (console && typeof console.error === 'function') {
console.error('Could not decode cookie with key "' + key + '"', e);
}
}

return {
key: decodeURIComponent(cookieString.substr(0, separatorIndex)),
value: decodeURIComponent(cookieString.substr(separatorIndex + 1))
key: decodedKey,
value: cookieString.substr(separatorIndex + 1) // Defer decoding value until accessed
};
};

Expand Down
2 changes: 1 addition & 1 deletion tests/spec-runner.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<script src="lib/jasmine-1.3.1/jasmine-html.js"></script>

<!-- include source files here... -->
<script src="../dist/cookies.min.js"></script>
<script src="../src/cookies.js"></script>

<!-- include spec files here... -->
<script src="spec/cookies-spec.js"></script>
Expand Down
20 changes: 12 additions & 8 deletions tests/spec/cookies-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,18 @@ describe('UNIT TESTS', function () {

expect(Cookies.get(key)).toEqual('value');
});

it('returns the value of a properly encoded cookie when another cookie with a malformed value exists', function () {
mockDocument.cookie = 'key=value; malformed=%D0%EE%F1%F1%E8%FF';

expect(Cookies.get('key')).toEqual('value');
});

it('returns the value of a properly encoded cookie when another cookie with a malformed key exists', function () {
mockDocument.cookie = 'key=value; %D0%EE%F1%F1%E8%FF=malformed';

expect(Cookies.get('key')).toEqual('value');
});
});

describe('Cookies.set(key, value[, options])', function () {
Expand Down Expand Up @@ -451,14 +463,6 @@ describe('UNIT TESTS', function () {
});
});

it('URI decodes cookie values', function () {
var cookieString = 'key=%5C%22%2C%3B%20%C3%B1%C3%A2%C3%A9';
expect(Cookies._getKeyValuePairFromCookieString(cookieString)).toEqual({
key: 'key',
value: '\\",; ñâé'
});
});

it('parses cookie values containing an "=" character', function () {
var cookieString = 'key=value=value';
expect(Cookies._getKeyValuePairFromCookieString(cookieString)).toEqual({
Expand Down

0 comments on commit f0bb95d

Please sign in to comment.