Skip to content

Commit 5fc726c

Browse files
committed
Merge pull request #160 from ahelmel/feature/cookie-is-supported
made the function browserSupportsCookies available for public access as cookie.isSupported
2 parents dbd1913 + 8223e64 commit 5fc726c

File tree

4 files changed

+47
-14
lines changed

4 files changed

+47
-14
lines changed

dist/angular-local-storage.js

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* An Angular module that gives you access to the browsers local storage
3-
* @version v0.1.3 - 2014-10-14
3+
* @version v0.1.3 - 2014-10-30
44
* @link https://github.com/grevory/angular-local-storage
55
* @author grevory <[email protected]>
66
* @license MIT License, http://www.opensource.org/licenses/MIT
@@ -286,16 +286,16 @@ angularLocalStorage.provider('localStorageService', function() {
286286
};
287287

288288
// Checks the browser to see if cookies are supported
289-
var browserSupportsCookies = function() {
289+
var browserSupportsCookies = (function() {
290290
try {
291-
return navigator.cookieEnabled ||
291+
return $window.navigator.cookieEnabled ||
292292
("cookie" in $document && ($document.cookie.length > 0 ||
293293
($document.cookie = "test").indexOf.call($document.cookie, "test") > -1));
294294
} catch (e) {
295295
$rootScope.$broadcast('LocalStorageModule.notification.error', e.message);
296296
return false;
297297
}
298-
};
298+
}());
299299

300300
// Directly adds a value to cookies
301301
// Typically used as a fallback is local storage is not available in the browser
@@ -308,7 +308,7 @@ angularLocalStorage.provider('localStorageService', function() {
308308
value = toJson(value);
309309
}
310310

311-
if (!browserSupportsCookies()) {
311+
if (!browserSupportsCookies) {
312312
$rootScope.$broadcast('LocalStorageModule.notification.error', 'COOKIES_NOT_SUPPORTED');
313313
return false;
314314
}
@@ -344,7 +344,7 @@ angularLocalStorage.provider('localStorageService', function() {
344344
// Directly get a value from a cookie
345345
// Example use: localStorageService.cookie.get('library'); // returns 'angular'
346346
var getFromCookies = function (key) {
347-
if (!browserSupportsCookies()) {
347+
if (!browserSupportsCookies) {
348348
$rootScope.$broadcast('LocalStorageModule.notification.error', 'COOKIES_NOT_SUPPORTED');
349349
return false;
350350
}
@@ -437,6 +437,7 @@ angularLocalStorage.provider('localStorageService', function() {
437437
deriveKey: deriveQualifiedKey,
438438
length: lengthOfLocalStorage,
439439
cookie: {
440+
isSupported: browserSupportsCookies,
440441
set: addToCookies,
441442
add: addToCookies, //DEPRECATED
442443
get: getFromCookies,

dist/angular-local-storage.min.js

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/angular-local-storage.js

+6-5
Original file line numberDiff line numberDiff line change
@@ -259,16 +259,16 @@ angularLocalStorage.provider('localStorageService', function() {
259259
};
260260

261261
// Checks the browser to see if cookies are supported
262-
var browserSupportsCookies = function() {
262+
var browserSupportsCookies = (function() {
263263
try {
264-
return navigator.cookieEnabled ||
264+
return $window.navigator.cookieEnabled ||
265265
("cookie" in $document && ($document.cookie.length > 0 ||
266266
($document.cookie = "test").indexOf.call($document.cookie, "test") > -1));
267267
} catch (e) {
268268
$rootScope.$broadcast('LocalStorageModule.notification.error', e.message);
269269
return false;
270270
}
271-
};
271+
}());
272272

273273
// Directly adds a value to cookies
274274
// Typically used as a fallback is local storage is not available in the browser
@@ -281,7 +281,7 @@ angularLocalStorage.provider('localStorageService', function() {
281281
value = toJson(value);
282282
}
283283

284-
if (!browserSupportsCookies()) {
284+
if (!browserSupportsCookies) {
285285
$rootScope.$broadcast('LocalStorageModule.notification.error', 'COOKIES_NOT_SUPPORTED');
286286
return false;
287287
}
@@ -317,7 +317,7 @@ angularLocalStorage.provider('localStorageService', function() {
317317
// Directly get a value from a cookie
318318
// Example use: localStorageService.cookie.get('library'); // returns 'angular'
319319
var getFromCookies = function (key) {
320-
if (!browserSupportsCookies()) {
320+
if (!browserSupportsCookies) {
321321
$rootScope.$broadcast('LocalStorageModule.notification.error', 'COOKIES_NOT_SUPPORTED');
322322
return false;
323323
}
@@ -410,6 +410,7 @@ angularLocalStorage.provider('localStorageService', function() {
410410
deriveKey: deriveQualifiedKey,
411411
length: lengthOfLocalStorage,
412412
cookie: {
413+
isSupported: browserSupportsCookies,
413414
set: addToCookies,
414415
add: addToCookies, //DEPRECATED
415416
get: getFromCookies,

test/spec/localStorageSpec.js

+32-1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,12 @@ describe('localStorageService', function() {
6262
};
6363
}
6464

65+
function expectCookieSupporting(expected) {
66+
return function(localStorageService) {
67+
expect(localStorageService.cookie.isSupported).toEqual(expected);
68+
};
69+
}
70+
6571
function expectDomain(domain) {
6672
return function($document, localStorageService) {
6773
localStorageService.set('foo','bar'); //Should trigger first time
@@ -417,7 +423,10 @@ describe('localStorageService', function() {
417423
beforeEach(module('LocalStorageModule', function($provide) {
418424
$provide.value('$window', {
419425
localStorage: false,
420-
sessionStorage: false
426+
sessionStorage: false,
427+
navigator: {
428+
cookieEnabled: true
429+
}
421430
});
422431
$provide.value('$document', {
423432
cookie: ''
@@ -428,6 +437,10 @@ describe('localStorageService', function() {
428437
expectSupporting(false)
429438
));
430439

440+
it('cookie.isSupported should be true if cookies are enabled', inject(
441+
expectCookieSupporting(true)
442+
));
443+
431444
it('fallback storage type should be cookie', inject(
432445
expectStorageTyping('cookie')
433446
));
@@ -504,4 +517,22 @@ describe('localStorageService', function() {
504517
};
505518
});
506519

520+
//cookie disabled
521+
describe('No Cookie', function() {
522+
523+
beforeEach(module('LocalStorageModule', function($provide) {
524+
$provide.value('$window', {
525+
navigator: {
526+
cookieEnabled: false
527+
}
528+
});
529+
$provide.value('$document', {
530+
531+
});
532+
}));
533+
534+
it('cookie.isSupported should be false if cookies are disabled', inject(
535+
expectCookieSupporting(false)
536+
));
537+
});
507538
});

0 commit comments

Comments
 (0)