Gilles has uploaded a new change for review.
https://gerrit.wikimedia.org/r/192806
Change subject: Make media viewer use the same prefix as mobile
......................................................................
Make media viewer use the same prefix as mobile
Keep responding to the legacy prefix, since many links on the web
are referencing it.
Bug: T87769
Change-Id: I0936ada35141ddd85e0aa232b833d315e3246ce3
---
M resources/mmv/mmv.bootstrap.js
M resources/mmv/routing/mmv.routing.Router.js
M tests/browser/features/step_definitions/mmv_options_steps.rb
M tests/qunit/mmv/mmv.bootstrap.test.js
M tests/qunit/mmv/mmv.test.js
M tests/qunit/mmv/routing/mmv.routing.Router.test.js
6 files changed, 107 insertions(+), 25 deletions(-)
git pull
ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/MultimediaViewer
refs/changes/06/192806/1
diff --git a/resources/mmv/mmv.bootstrap.js b/resources/mmv/mmv.bootstrap.js
index f82b79e..53059f7 100644
--- a/resources/mmv/mmv.bootstrap.js
+++ b/resources/mmv/mmv.bootstrap.js
@@ -427,7 +427,9 @@
// There is no point loading the mmv if it isn't loaded yet for
hash changes unrelated to the mmv
// Such as anchor links on the page
- if ( !this.viewerInitialized && window.location.hash.indexOf(
'#mediaviewer/' ) !== 0 ) {
+ if ( !this.viewerInitialized
+ && window.location.hash.indexOf( '#mediaviewer/' ) !== 0
+ && window.location.hash.indexOf( '#/media/' ) !== 0 ) {
return;
}
diff --git a/resources/mmv/routing/mmv.routing.Router.js
b/resources/mmv/routing/mmv.routing.Router.js
index ef4b572..bea81f7 100644
--- a/resources/mmv/routing/mmv.routing.Router.js
+++ b/resources/mmv/routing/mmv.routing.Router.js
@@ -27,11 +27,19 @@
RP = Router.prototype;
/**
+ * The prefix originally used to namespace MediaViewer routing hashes.
Since there are many links
+ * out there pointing to those URLs, we should keep them working.
+ * @protected
+ * @property {string}
+ */
+ RP.legacyPrefix = 'mediaviewer';
+
+ /**
* The prefix used to namespace MediaViewer routing hashes
* @protected
* @property {string}
*/
- RP.applicationPrefix = 'mediaviewer';
+ RP.applicationPrefix = '/media';
/**
* Takes an URL hash and returns a route (or null if it could not be
parsed).
@@ -124,16 +132,27 @@
* @return {string[]}
*/
RP.tokenizeHash = function ( hash ) {
- var hashParts;
+ var prefix;
if ( hash[0] === '#' ) {
hash = hash.slice( 1 );
}
- hashParts = hash.split( '/' );
- if ( hashParts[0] !== this.applicationPrefix ) {
+
+ if ( hash.indexOf( this.legacyPrefix ) === 0 ) {
+ prefix = this.legacyPrefix;
+ }
+
+ if ( hash.indexOf( this.applicationPrefix ) === 0 ) {
+ prefix = this.applicationPrefix;
+ }
+
+ if ( prefix === undefined ) {
return [];
}
- return hashParts;
+
+ hash = hash.slice( prefix.length );
+
+ return hash.split( '/' );
};
/**
diff --git a/tests/browser/features/step_definitions/mmv_options_steps.rb
b/tests/browser/features/step_definitions/mmv_options_steps.rb
index ef8965c..7f97b85 100644
--- a/tests/browser/features/step_definitions/mmv_options_steps.rb
+++ b/tests/browser/features/step_definitions/mmv_options_steps.rb
@@ -64,6 +64,6 @@
Then /^I am taken to the file page$/ do
on(E2ETestPage) do |page|
page.current_url.should match /\/wiki\/File:/
- page.current_url.should_not match /#mediaviewer/
+ page.current_url.should_not match /#\/media/
end
end
\ No newline at end of file
diff --git a/tests/qunit/mmv/mmv.bootstrap.test.js
b/tests/qunit/mmv/mmv.bootstrap.test.js
index 16cf668..f5b3dbc 100644
--- a/tests/qunit/mmv/mmv.bootstrap.test.js
+++ b/tests/qunit/mmv/mmv.bootstrap.test.js
@@ -61,8 +61,8 @@
return bootstrap;
}
- function hashTest( bootstrap, assert ) {
- var hash = 'mediaviewer/foo';
+ function hashTest( prefix, bootstrap, assert ) {
+ var hash = prefix + '/foo';
bootstrap.setupEventHandlers();
@@ -325,7 +325,7 @@
bootstrap = createBootstrap();
- hashTest( bootstrap, assert );
+ hashTest( '/media', bootstrap, assert );
} );
QUnit.test( 'Only load the viewer on a valid hash (old browsers)', 1,
function ( assert ) {
@@ -336,11 +336,55 @@
bootstrap = createBootstrap();
bootstrap.browserHistory = undefined;
- hashTest( bootstrap, assert );
+ hashTest( '/media', bootstrap, assert );
} );
+ QUnit.test( 'Load the viewer on a legacy hash (modern browsers)', 1,
function ( assert ) {
+ var bootstrap;
+
+ window.location.hash = '';
+
+ bootstrap = createBootstrap();
+
+ hashTest( 'mediaviewer', bootstrap, assert );
+ } );
+
+ QUnit.test( 'Load the viewer on a legacy hash (old browsers)', 1,
function ( assert ) {
+ var bootstrap;
+
+ window.location.hash = '';
+
+ bootstrap = createBootstrap();
+ bootstrap.browserHistory = undefined;
+
+ hashTest( 'mediaviewer', bootstrap, assert );
+ } );
+
+
QUnit.test( 'internalHashChange', 1, function ( assert ) {
var bootstrap = createBootstrap(),
+ hash = '#/media/foo';
+
+ window.location.hash = '';
+
+ bootstrap.setupEventHandlers();
+
+ bootstrap.loadViewer = function () {
+ assert.ok( false, 'Viewer should not be loaded' );
+ return $.Deferred().reject();
+ };
+
+ bootstrap.internalHashChange( { hash: hash } );
+
+ assert.strictEqual( window.location.hash, hash, 'Window\'s hash
has been updated correctly' );
+
+ bootstrap.cleanupEventHandlers();
+
+ window.location.hash = '';
+ } );
+
+ QUnit.test( 'internalHashChange (legacy)', 1, function ( assert ) {
+ var bootstrap = createBootstrap(),
hash = '#mediaviewer/foo';
window.location.hash = '';
diff --git a/tests/qunit/mmv/mmv.test.js b/tests/qunit/mmv/mmv.test.js
index 431d6e9..53fb21d 100644
--- a/tests/qunit/mmv/mmv.test.js
+++ b/tests/qunit/mmv/mmv.test.js
@@ -29,7 +29,7 @@
} );
} );
- QUnit.test( 'Hash handling', 7, function ( assert ) {
+ QUnit.test( 'Hash handling', 8, function ( assert ) {
var oldUnattach,
viewer = new mw.mmv.MultimediaViewer( { get: $.noop } ),
ui = new mw.mmv.LightboxInterface(),
@@ -83,7 +83,7 @@
// Open a valid mmv hash link and check that the right image is
requested.
// imageSrc contains a space without any encoding on purpose
- window.location.hash = 'mediaviewer/File:' + imageSrc;
+ window.location.hash = '/media/File:' + imageSrc;
viewer.hash();
// Reset the hash, because for some browsers switching from the
non-URI-encoded to
@@ -92,7 +92,15 @@
viewer.hash();
// Try again with an URI-encoded imageSrc containing a space
- window.location.hash = 'mediaviewer/File:' +
encodeURIComponent( imageSrc );
+ window.location.hash = '/media/File:' + encodeURIComponent(
imageSrc );
+ viewer.hash();
+
+ // Reset the hash
+ window.location.hash = '';
+ viewer.hash();
+
+ // Try again with a legacy hash
+ window.location.hash = 'mediaviewer/File:' + imageSrc;
viewer.hash();
viewer.cleanupEventHandlers();
diff --git a/tests/qunit/mmv/routing/mmv.routing.Router.test.js
b/tests/qunit/mmv/routing/mmv.routing.Router.test.js
index f47dd6f..5d33835 100644
--- a/tests/qunit/mmv/routing/mmv.routing.Router.test.js
+++ b/tests/qunit/mmv/routing/mmv.routing.Router.test.js
@@ -25,14 +25,18 @@
assert.ok( router, 'Router created successfully' );
} );
- QUnit.test( 'isMediaViewerHash()', 6, function ( assert ) {
+ QUnit.test( 'isMediaViewerHash()', 10, function ( assert ) {
var router = new mw.mmv.routing.Router();
- assert.ok( router.isMediaViewerHash( 'mediaviewer/foo' ),
'Normal hash' );
- assert.ok( router.isMediaViewerHash( '#mediaviewer/foo' ),
'Normal hash with #' );
- assert.ok( router.isMediaViewerHash( 'mediaviewer' ), 'Bare
hash' );
- assert.ok( router.isMediaViewerHash( '#mediaviewer' ), 'Bare
hash with #' );
- assert.ok( !router.isMediaViewerHash( 'foo/mediaviewer' ),
'Foreign hash' );
+ assert.ok( router.isMediaViewerHash( 'mediaviewer/foo' ),
'Legacy hash' );
+ assert.ok( router.isMediaViewerHash( '#mediaviewer/foo' ),
'Legacy hash with #' );
+ assert.ok( router.isMediaViewerHash( 'mediaviewer' ), 'Bare
legacy hash' );
+ assert.ok( router.isMediaViewerHash( '#mediaviewer' ), 'Bare
legacy hash with #' );
+ assert.ok( router.isMediaViewerHash( '/media/foo' ), 'Normal
hash' );
+ assert.ok( router.isMediaViewerHash( '#/media/foo' ), 'Normal
hash with #' );
+ assert.ok( router.isMediaViewerHash( '/media' ), 'Bare hash' );
+ assert.ok( router.isMediaViewerHash( '#/media' ), 'Bare hash
with #' );
+ assert.ok( !router.isMediaViewerHash( 'foo/media' ), 'Foreign
hash' );
assert.ok( !router.isMediaViewerHash( '' ), 'Empty hash' );
} );
@@ -124,8 +128,8 @@
assert.ok( !router.parseHash( 'foo' ), 'Non-MMV hash is
rejected.' );
assert.ok( !router.parseHash( '#foo' ), 'Non-MMV hash is
rejected (with #).' );
- assert.ok( !router.parseHash( 'mediaviewer/foo/bar' ), 'Invalid
MMV hash is rejected.' );
- assert.ok( !router.parseHash( '#mediaviewer/foo/bar' ),
'Invalid MMV hash is rejected (with #).' );
+ assert.ok( !router.parseHash( '/media/foo/bar' ), 'Invalid MMV
hash is rejected.' );
+ assert.ok( !router.parseHash( '#/media/foo/bar' ), 'Invalid MMV
hash is rejected (with #).' );
} );
QUnit.test( 'parseHash() backwards compatibility', 2, function ( assert
) {
@@ -147,13 +151,13 @@
router = new mw.mmv.routing.Router();
url = router.createHashedUrl( route, 'http://example.com/' );
- assert.strictEqual( url, 'http://example.com/#mediaviewer',
'Url generation works' );
+ assert.strictEqual( url, 'http://example.com/#/media', 'Url
generation works' );
url = router.createHashedUrl( route, 'http://example.com/#foo'
);
- assert.strictEqual( url, 'http://example.com/#mediaviewer',
'Urls with fragments are handled' );
+ assert.strictEqual( url, 'http://example.com/#/media', 'Urls
with fragments are handled' );
} );
- QUnit.test( 'parseLocation()', 2, function ( assert ) {
+ QUnit.test( 'parseLocation()', 3, function ( assert ) {
var location, route,
router = new mw.mmv.routing.Router();
@@ -161,6 +165,11 @@
route = router.parseLocation( location );
assert.strictEqual( route.fileTitle.getPrefixedDb(),
'File:Foo.png', 'Reading location works' );
+ location = { href: 'http://example.com/foo#/media/File:Foo.png'
};
+ route = router.parseLocation( location );
+ assert.strictEqual( route.fileTitle.getPrefixedDb(),
'File:Foo.png', 'Reading location works' );
+
+
location = { href: 'http://example.com/foo' };
route = router.parseLocation( location );
assert.ok( !route, 'Reading location without fragment part
works' );
--
To view, visit https://gerrit.wikimedia.org/r/192806
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: I0936ada35141ddd85e0aa232b833d315e3246ce3
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/MultimediaViewer
Gerrit-Branch: master
Gerrit-Owner: Gilles <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits