Skip to content

Commit

Permalink
FixedeEmpty string returned as null when using IE<11
Browse files Browse the repository at this point in the history
closes #1
  • Loading branch information
Voltace committed Apr 16, 2015
1 parent 832024c commit 53404f8
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 5 deletions.
7 changes: 5 additions & 2 deletions browser-cookies.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,11 @@ exports.get = function(name) {
// Determine separator index ("name=value")
var separatorIndex = cookie.indexOf('=');

// If a separator index is found, Decode the cookie name and compare to the requested cookie name
if (separatorIndex != -1 && decodeURIComponent(cookie.substring(0, separatorIndex).replace(/^\s+|\s+$/g,'')) == name) {
// IE<11 emits the equal sign when the cookie value is empty
separatorIndex = separatorIndex < 0 ? cookie.length : separatorIndex;

// Decode the cookie name and remove any leading/trailing spaces, then compare to the requested cookie name
if (decodeURIComponent(cookie.substring(0, separatorIndex).replace(/^\s+|\s+$/g,'')) == name) {
return decodeURIComponent(cookie.substring(separatorIndex + 1, cookie.length));
}
}
Expand Down
2 changes: 1 addition & 1 deletion dist/browser-cookies.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion dist/browser-cookies.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,10 @@ exports.get = function(name) {

// Determine separator index ("name=value")
var separatorIndex = cookie.indexOf('=');
separatorIndex = separatorIndex < 0 ? cookie.length : separatorIndex;

// If a separator index is found, Decode the cookie name and compare to the requested cookie name
if (separatorIndex != -1 && decodeURIComponent(cookie.substring(0, separatorIndex).replace(/^\s+|\s+$/g,'')) == name) {
if (decodeURIComponent(cookie.substring(0, separatorIndex).replace(/^\s+|\s+$/g,'')) == name) {
return decodeURIComponent(cookie.substring(separatorIndex + 1, cookie.length));
}
}
Expand Down
11 changes: 10 additions & 1 deletion test/spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,11 @@ describe("Stubbed Test Suite", function() {
});
});

it("Set empty cookie", function() {
this.browsercookies.set('banana', '');
expect(this.docStub.cookie).toBe('banana=;path=/');
});

it("Set cookie using using no global defaults at all", function() {
this.browsercookies.defaults = {};
this.browsercookies.set('banana', 'yellow');
Expand Down Expand Up @@ -436,6 +441,11 @@ describe("Browser-based Test Suite", function() {
expect(this.browsercookies.get('banana')).toBe(null);
});

it("Set empty cookie", function() {
this.browsercookies.set('banana', '');
expect(this.browsercookies.get('banana')).toBe('');
});

it("Erase non-existing cookie", function() {
// Shouldn't raise any error
expect(this.browsercookies.erase('orange')).toBe(undefined);
Expand All @@ -446,7 +456,6 @@ describe("Browser-based Test Suite", function() {
expect(this.browsercookies.get('báñâñâ')).toBe('yellow');
});


it("Verify cookie value encoding and decoding", function() {
// Should apply URI encoding
this.browsercookies.set('banana', '¿yéllów?');
Expand Down

0 comments on commit 53404f8

Please sign in to comment.