Krinkle has uploaded a new change for review. https://gerrit.wikimedia.org/r/323344
Change subject: mediawiki.storage: provide a wrapper for sessionStorage ...................................................................... mediawiki.storage: provide a wrapper for sessionStorage T119146 provides a use-case for using sessionStorage. But the wrapper that we use to make web storage operations safe to call on all browsers is localStorage-specific. With a small modification, we can allow the storage engine to be specified on object creation, which allows us to create a wrapper around sessionStorage (mw.storage.local) with minimal code duplication. Change-Id: I73bc82d9fa2359148fe1e50b6535bfa0dbe8bd3e --- M maintenance/jsduck/categories.json M resources/src/mediawiki/mediawiki.storage.js M tests/qunit/suites/resources/mediawiki/mediawiki.storage.test.js 3 files changed, 34 insertions(+), 34 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core refs/changes/44/323344/1 diff --git a/maintenance/jsduck/categories.json b/maintenance/jsduck/categories.json index aad85da..278d932 100644 --- a/maintenance/jsduck/categories.json +++ b/maintenance/jsduck/categories.json @@ -26,7 +26,7 @@ "mw.messagePoster.*", "mw.notification", "mw.Notification_", - "mw.storage", + "SafeStorage", "mw.user", "mw.util", "mw.plugin.*", diff --git a/resources/src/mediawiki/mediawiki.storage.js b/resources/src/mediawiki/mediawiki.storage.js index a9d17ff..47655f0 100644 --- a/resources/src/mediawiki/mediawiki.storage.js +++ b/resources/src/mediawiki/mediawiki.storage.js @@ -2,22 +2,19 @@ 'use strict'; /** - * Library for storing device specific information. It should be used for storing simple - * strings and is not suitable for storing large chunks of data. + * A wrapper for an HTML5 Storage interface (`localStorage` or `sessionStorage`) + * that is safe to call on all browsers. * - * @class mw.storage - * @singleton + * @private + * @class SafeStorage + * + * @constructor + * @param {Object} [store] The Storage instance to wrap around */ - mw.storage = { + function SafeStorage( store ) { + var self = this; - localStorage: ( function () { - // Catch exceptions to avoid fatal in Chrome's "Block data storage" mode - // which throws when accessing the localStorage property itself, as opposed - // to the standard behaviour of throwing on getItem/setItem. (T148998) - try { - return window.localStorage; - } catch ( e ) {} - }() ), + self.store = store; /** * Retrieve value from device storage. @@ -25,12 +22,12 @@ * @param {string} key Key of item to retrieve * @return {string|boolean} False when localStorage not available, otherwise string */ - get: function ( key ) { + self.get = function ( key ) { try { - return mw.storage.localStorage.getItem( key ); + return self.store.getItem( key ); } catch ( e ) {} return false; - }, + }; /** * Set a value in device storage. @@ -39,13 +36,13 @@ * @param {string} value Value to be stored * @return {boolean} Whether the save succeeded or not */ - set: function ( key, value ) { + self.set = function ( key, value ) { try { - mw.storage.localStorage.setItem( key, value ); + self.store.setItem( key, value ); return true; } catch ( e ) {} return false; - }, + }; /** * Remove a value from device storage. @@ -53,13 +50,16 @@ * @param {string} key Key of item to remove * @return {boolean} Whether the save succeeded or not */ - remove: function ( key ) { + self.remove = function ( key ) { try { - mw.storage.localStorage.removeItem( key ); + self.store.removeItem( key ); return true; } catch ( e ) {} return false; - } - }; + }; + } + + mw.storage = new SafeStorage( window.localStorage ); + mw.storage.session = new SafeStorage( window.sessionStorage ); }( mediaWiki ) ); diff --git a/tests/qunit/suites/resources/mediawiki/mediawiki.storage.test.js b/tests/qunit/suites/resources/mediawiki/mediawiki.storage.test.js index 6cef4a7..a87457a 100644 --- a/tests/qunit/suites/resources/mediawiki/mediawiki.storage.test.js +++ b/tests/qunit/suites/resources/mediawiki/mediawiki.storage.test.js @@ -1,35 +1,35 @@ ( function ( mw ) { QUnit.module( 'mediawiki.storage' ); - QUnit.test( 'set/get with localStorage', 3, function ( assert ) { - this.sandbox.stub( mw.storage, 'localStorage', { + QUnit.test( 'set/get with browser support', 3, function ( assert ) { + this.sandbox.stub( mw.storage, 'store', { setItem: this.sandbox.spy(), getItem: this.sandbox.stub() } ); mw.storage.set( 'foo', 'test' ); - assert.ok( mw.storage.localStorage.setItem.calledOnce ); + assert.ok( mw.storage.store.setItem.calledOnce ); - mw.storage.localStorage.getItem.withArgs( 'foo' ).returns( 'test' ); - mw.storage.localStorage.getItem.returns( null ); + mw.storage.store.getItem.withArgs( 'foo' ).returns( 'test' ); + mw.storage.store.getItem.returns( null ); assert.strictEqual( mw.storage.get( 'foo' ), 'test', 'Check value gets stored.' ); assert.strictEqual( mw.storage.get( 'bar' ), null, 'Unset values are null.' ); } ); - QUnit.test( 'set/get without localStorage', 3, function ( assert ) { - this.sandbox.stub( mw.storage, 'localStorage', { + QUnit.test( 'set/get without browser support', 3, function ( assert ) { + this.sandbox.stub( mw.storage, 'store', { getItem: this.sandbox.stub(), removeItem: this.sandbox.stub(), setItem: this.sandbox.stub() } ); - mw.storage.localStorage.getItem.throws(); + mw.storage.store.getItem.throws(); assert.strictEqual( mw.storage.get( 'foo' ), false ); - mw.storage.localStorage.setItem.throws(); + mw.storage.store.setItem.throws(); assert.strictEqual( mw.storage.set( 'foo', 'test' ), false ); - mw.storage.localStorage.removeItem.throws(); + mw.storage.store.removeItem.throws(); assert.strictEqual( mw.storage.remove( 'foo', 'test' ), false ); } ); -- To view, visit https://gerrit.wikimedia.org/r/323344 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I73bc82d9fa2359148fe1e50b6535bfa0dbe8bd3e Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/core Gerrit-Branch: master Gerrit-Owner: Krinkle <krinklem...@gmail.com> Gerrit-Reviewer: Ori.livneh <o...@wikimedia.org> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits