From f13bbea1243d35467601e82096b3db956774b2a5 Mon Sep 17 00:00:00 2001 From: Ophir LOJKINE Date: Wed, 10 Feb 2021 14:53:22 +0100 Subject: [PATCH 1/2] Catch errors on localStorage setting failure MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes https://github.com/rust-lang/rust/issues/81928 “Ask forgiveness not permission” : this makes the code both simpler and more robust --- src/librustdoc/html/static/storage.js | 31 +++++++-------------------- 1 file changed, 8 insertions(+), 23 deletions(-) diff --git a/src/librustdoc/html/static/storage.js b/src/librustdoc/html/static/storage.js index 9c5ac1625afea..11265ae0ee339 100644 --- a/src/librustdoc/html/static/storage.js +++ b/src/librustdoc/html/static/storage.js @@ -89,35 +89,20 @@ function hasOwnProperty(obj, property) { return Object.prototype.hasOwnProperty.call(obj, property); } -function usableLocalStorage() { - // Check if the browser supports localStorage at all: - if (typeof Storage === "undefined") { - return false; - } - // Check if we can access it; this access will fail if the browser - // preferences deny access to localStorage, e.g., to prevent storage of - // "cookies" (or cookie-likes, as is the case here). - try { - return window.localStorage !== null && window.localStorage !== undefined; - } catch(err) { - // Storage is supported, but browser preferences deny access to it. - return false; - } -} - function updateLocalStorage(name, value) { - if (usableLocalStorage()) { - localStorage[name] = value; - } else { - // No Web Storage support so we do nothing + try { + window.localStorage.setItem(name, value); + } catch(e) { + // localStorage is not accessible, do nothing } } function getCurrentValue(name) { - if (usableLocalStorage() && localStorage[name] !== undefined) { - return localStorage[name]; + try { + window.localStorage.getItem(name); + } catch(e) { + return null; } - return null; } function switchTheme(styleElem, mainStyleElem, newTheme, saveTheme) { From 16f0ccda86b7cce1b2cfb7b6bcdef42e3e76dfbc Mon Sep 17 00:00:00 2001 From: Ophir LOJKINE Date: Wed, 10 Feb 2021 14:55:53 +0100 Subject: [PATCH 2/2] Fix getCurrentValue --- src/librustdoc/html/static/storage.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/librustdoc/html/static/storage.js b/src/librustdoc/html/static/storage.js index 11265ae0ee339..a50ed5b662bf6 100644 --- a/src/librustdoc/html/static/storage.js +++ b/src/librustdoc/html/static/storage.js @@ -99,7 +99,7 @@ function updateLocalStorage(name, value) { function getCurrentValue(name) { try { - window.localStorage.getItem(name); + return window.localStorage.getItem(name); } catch(e) { return null; }