From 7cb8f8fb4498aa5d8b04310cd4580997e65a7e86 Mon Sep 17 00:00:00 2001 From: Fredrik Bonander Date: Wed, 6 Feb 2013 10:30:40 +0100 Subject: [PATCH] fix($cookies): set cookies on Safari&IE when base[href] is undefined Safari and IE don't like being told to store cookies with path set to undefined. This change ensures that if base[href] (from which cookie path is derived) is undefined then the cookie path defaults to ''. The test verifies that the cookie is set instead of checking that cookie has correct path, this is due to that cookie meta information is not avabile once the cookie is set. Closes #1190, #1191 --- src/ng/browser.js | 2 +- test/ng/browserSpecs.js | 16 ++++++++++++++-- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/ng/browser.js b/src/ng/browser.js index 9682cd28a0c8..fa050d54511f 100644 --- a/src/ng/browser.js +++ b/src/ng/browser.js @@ -237,7 +237,7 @@ function Browser(window, document, $log, $sniffer) { */ self.baseHref = function() { var href = baseElement.attr('href'); - return href ? href.replace(/^https?\:\/\/[^\/]*/, '') : href; + return href ? href.replace(/^https?\:\/\/[^\/]*/, '') : ''; }; ////////////////////////////////////////////////////////////// diff --git a/test/ng/browserSpecs.js b/test/ng/browserSpecs.js index 53ca26429493..e989330bbee8 100644 --- a/test/ng/browserSpecs.js +++ b/test/ng/browserSpecs.js @@ -279,6 +279,18 @@ describe('browser', function() { }); }); + describe('put via cookies(cookieName, string), if no ', function () { + beforeEach(function () { + fakeDocument.basePath = undefined; + }); + + it('should default path in cookie to "" (empty string)', function () { + browser.cookies('cookie', 'bender'); + // This only fails in Safari and IE when cookiePath returns undefined + // Where it now succeeds since baseHref return '' instead of undefined + expect(document.cookie).toEqual('cookie=bender'); + }); + }); describe('get via cookies()[cookieName]', function() { @@ -555,9 +567,9 @@ describe('browser', function() { expect(browser.baseHref()).toEqual('/base/path/'); }); - it('should return undefined if no ', function() { + it('should return \'\' (empty string) if no ', function() { fakeDocument.basePath = undefined; - expect(browser.baseHref()).toBeUndefined(); + expect(browser.baseHref()).toEqual(''); }); it('should remove domain from ', function() {