[BUGFIX] Check if client supports localStorage 29/56729/3
authorAndreas Fernandez <a.fernandez@scripting-base.de>
Wed, 18 Apr 2018 15:45:12 +0000 (17:45 +0200)
committerBenjamin Kott <benjamin.kott@outlook.com>
Thu, 19 Apr 2018 16:57:35 +0000 (18:57 +0200)
Resolves: #84788
Releases: master, 8.7
Change-Id: If7a1c3e1b04c4de391d0bdb943d989971def6e8a
Reviewed-on: https://review.typo3.org/56729
Tested-by: TYPO3com <no-reply@typo3.com>
Reviewed-by: Frank Naegler <frank.naegler@typo3.org>
Tested-by: Frank Naegler <frank.naegler@typo3.org>
Reviewed-by: Benjamin Kott <benjamin.kott@outlook.com>
Tested-by: Benjamin Kott <benjamin.kott@outlook.com>
typo3/sysext/backend/Resources/Private/TypeScript/Storage/Client.ts
typo3/sysext/backend/Resources/Public/JavaScript/Storage/Client.js

index 9bf3d94..c233081 100644 (file)
@@ -20,12 +20,19 @@ class Client {
   private keyPrefix: string = 't3-';
 
   /**
+   * @returns {boolean}
+   */
+  private static isCapable(): boolean {
+    return localStorage !== null;
+  }
+
+  /**
    * Simple localStorage wrapper, to get value from localStorage
    * @param {string} key
    * @returns {string}
    */
   public get = (key: string): string => {
-    return localStorage.getItem(this.keyPrefix + key);
+    return Client.isCapable() ? localStorage.getItem(this.keyPrefix + key) : null;
   }
 
   /**
@@ -36,7 +43,9 @@ class Client {
    * @returns {string}
    */
   public set = (key: string, value: string): void => {
-    localStorage.setItem(this.keyPrefix + key, value);
+    if (Client.isCapable()) {
+      localStorage.setItem(this.keyPrefix + key, value);
+    }
   }
 
   /**
@@ -45,7 +54,9 @@ class Client {
    * @param {string} key
    */
   public unset = (key: string): void => {
-    localStorage.removeItem(this.keyPrefix + key);
+    if (Client.isCapable()) {
+      localStorage.removeItem(this.keyPrefix + key);
+    }
   }
 
   /**
@@ -54,6 +65,10 @@ class Client {
    * @param {string} prefix
    */
   public unsetByPrefix = (prefix: string): void => {
+    if (!Client.isCapable()) {
+      return;
+    }
+
     prefix = this.keyPrefix + prefix;
 
     const keysToDelete: Array<string> = [];
@@ -76,7 +91,9 @@ class Client {
    * Simple localStorage wrapper, to clear localStorage
    */
   public clear = (): void => {
-    localStorage.clear();
+    if (Client.isCapable()) {
+      localStorage.clear();
+    }
   }
 
   /**
@@ -86,8 +103,12 @@ class Client {
    * @returns {boolean}
    */
   public isset = (key: string): boolean => {
-    const value = this.get(key);
-    return (typeof value !== 'undefined' && value !== null);
+    if (Client.isCapable()) {
+      const value = this.get(key);
+      return (typeof value !== 'undefined' && value !== null);
+    }
+
+    return false;
   }
 }
 
index a0d7e98..a3ee416 100644 (file)
@@ -10,4 +10,4 @@
  *
  * The TYPO3 project - inspiring people to share!
  */
-var __values=this&&this.__values||function(e){var t="function"==typeof Symbol&&e[Symbol.iterator],r=0;return t?t.call(e):{next:function(){return e&&r>=e.length&&(e=void 0),{value:e&&e[r++],done:!e}}}};define(["require","exports"],function(e,t){"use strict";return new function(){var e=this;this.keyPrefix="t3-",this.get=function(t){return localStorage.getItem(e.keyPrefix+t)},this.set=function(t,r){localStorage.setItem(e.keyPrefix+t,r)},this.unset=function(t){localStorage.removeItem(e.keyPrefix+t)},this.unsetByPrefix=function(t){t=e.keyPrefix+t;for(var r,n,o=[],i=0;i<localStorage.length;++i)if(localStorage.key(i).substring(0,t.length)===t){var l=localStorage.key(i).substr(e.keyPrefix.length);o.push(l)}try{for(var a=__values(o),u=a.next();!u.done;u=a.next())l=u.value,e.unset(l)}catch(e){r={error:e}}finally{try{u&&!u.done&&(n=a.return)&&n.call(a)}finally{if(r)throw r.error}}},this.clear=function(){localStorage.clear()},this.isset=function(t){var r=e.get(t);return null!=r}}});
\ No newline at end of file
+var __values=this&&this.__values||function(e){var t="function"==typeof Symbol&&e[Symbol.iterator],r=0;return t?t.call(e):{next:function(){return e&&r>=e.length&&(e=void 0),{value:e&&e[r++],done:!e}}}};define(["require","exports"],function(e,t){"use strict";return new(function(){function e(){var t=this;this.keyPrefix="t3-",this.get=function(r){return e.isCapable()?localStorage.getItem(t.keyPrefix+r):null},this.set=function(r,n){e.isCapable()&&localStorage.setItem(t.keyPrefix+r,n)},this.unset=function(r){e.isCapable()&&localStorage.removeItem(t.keyPrefix+r)},this.unsetByPrefix=function(r){if(e.isCapable()){r=t.keyPrefix+r;for(var n,a,i=[],l=0;l<localStorage.length;++l)if(localStorage.key(l).substring(0,r.length)===r){var o=localStorage.key(l).substr(t.keyPrefix.length);i.push(o)}try{for(var u=__values(i),s=u.next();!s.done;s=u.next()){o=s.value;t.unset(o)}}catch(e){n={error:e}}finally{try{s&&!s.done&&(a=u.return)&&a.call(u)}finally{if(n)throw n.error}}}},this.clear=function(){e.isCapable()&&localStorage.clear()},this.isset=function(r){if(e.isCapable()){var n=t.get(r);return null!=n}return!1}}return e.isCapable=function(){return null!==localStorage},e}())});
\ No newline at end of file