Make mmv handle urlencoded hashes

Change-Id: Id05806be4586a3d6f04b92ace2c8195b8de016bc
Mingle: https://wikimedia.mingle.thoughtworks.com/projects/multimedia/cards/153
This commit is contained in:
Gilles Dubuc 2014-01-31 10:29:39 +01:00
parent 4667c9a896
commit 67fa053687
2 changed files with 52 additions and 5 deletions

View file

@ -982,17 +982,23 @@
return date.format( 'LL' );
};
function handleHash( hash ) {
var statedIndex, $foundElement, linkState = hash.split( '/' );
function handleHash() {
var statedIndex,
$foundElement,
hash = decodeURIComponent( document.location.hash ),
linkState = hash.split( '/' );
comingFromPopstate = true;
if ( linkState[0] === '#mediaviewer' ) {
statedIndex = mw.mediaViewer.lightbox.images[linkState[2]];
if ( statedIndex.filePageTitle.getPrefixedText() === linkState[1] ) {
$foundElement = $( imgsSelector ).eq( linkState[2] );
mw.mediaViewer.loadImage( statedIndex, $foundElement.prop( 'src' ) );
}
} else {
if ( mw.mediaViewer.lightbox ) {
// If the hash is invalid (not a mmv hash) we check if there's any mmv lightbox open and we close it
if ( mw.mediaViewer && mw.mediaViewer.lightbox && mw.mediaViewer.lightbox.iface ) {
mw.mediaViewer.lightbox.iface.unattach();
}
}
@ -1006,8 +1012,8 @@
mw.mediaViewer = viewer;
handleHash( document.location.hash );
window.addEventListener( 'popstate', function () { handleHash( document.location.hash ); } );
handleHash();
window.addEventListener( 'popstate', handleHash );
} );
mw.MultimediaViewer = MultimediaViewer;

View file

@ -529,4 +529,45 @@
profileEvent = 'image-load';
viewer.loadAndSetImage( ui, imageData, targetWidth, requestedWidth, profileEvent );
} );
QUnit.test( 'Hash handling', 5, function ( assert ) {
var oldLoadImage = mw.mediaViewer.loadImage,
oldLightbox = mw.mediaViewer.lightbox,
imageSrc = 'Foo bar.jpg',
image = { filePageTitle: new mw.Title( 'File:' + imageSrc ) };
document.location.hash = '';
mw.mediaViewer.lightbox = { iface: { unattach: function() {
assert.ok( true, 'Interface unattached' );
} } };
// Verify that passing an invalid mmv hash triggers unattach()
document.location.hash = 'Foo';
mw.mediaViewer.lightbox = { images: [ image ] };
$( '#qunit-fixture' ).append( '<a class="image"><img src="' + imageSrc + '"></a>' );
mw.mediaViewer.loadImage = function( img, src ) {
assert.strictEqual( img, image, 'The image object matches' );
assert.ok( src.match( encodeURIComponent( imageSrc ) ), 'The image url matches' );
};
// Open a valid mmv hash link and check that the right image is requested.
// imageSrc contains a space without any encoding on purpose
document.location.hash = 'mediaviewer/File:' + imageSrc + '/0';
// Reset the hash, because for some browsers switching from the non-URI-encoded to
// the non-URI-encoded version of the same text with a space will not trigger a hash change
document.location.hash = '';
// Try again with an URI-encoded imageSrc containing a space
document.location.hash = 'mediaviewer/File:' + encodeURIComponent( imageSrc ) + '/0';
mw.mediaViewer.lightbox = oldLightbox;
mw.mediaViewer.loadImage = oldLoadImage;
document.location.hash = '';
} );
}( mediaWiki, jQuery ) );