From 92ea8edbc50aae3104f35215c4ce9e87c0b4a0d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gerg=C5=91=20Tisza?= Date: Wed, 11 Jun 2014 18:42:29 +0000 Subject: [PATCH] Make MediaViewer ignore images which are in 'noviewer' class This includes images where the element has that class (achievable with [[File:Foo.png|class=noviewer]]) and also those where some parent element has it. Change-Id: I666be026828ea9ecb6e8c93d3f5ad1e3c190f81e Mingle: https://wikimedia.mingle.thoughtworks.com/projects/multimedia/cards/511 --- resources/mmv/mmv.bootstrap.js | 23 +++++++++++++++-------- tests/qunit/mmv/mmv.bootstrap.test.js | 24 ++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 8 deletions(-) diff --git a/resources/mmv/mmv.bootstrap.js b/resources/mmv/mmv.bootstrap.js index 7d11b02b1..c3f301f8b 100755 --- a/resources/mmv/mmv.bootstrap.js +++ b/resources/mmv/mmv.bootstrap.js @@ -148,6 +148,20 @@ } }; + /** + * Check if this thumbnail should be handled by MediaViewer + * @param {jQuery} $thumb the thumbnail (an `` element) in question + * @return {boolean} + */ + MMVB.isAllowedThumb = function ( $thumb ) { + // .metadata means this is inside an informational template like {{refimprove}} on enwiki. + // .noviewer means MediaViewer has been specifically disabled for this image + // .noarticletext means we are on an error page for a non-existing article, the image is part of some + // template // FIXME this should be handled by .metadata + return $thumb.closest( '.metadata, .noviewer, .noarticletext' ).length === 0; + + }; + /** * Processes a thumb * @param {Object} thumb @@ -168,14 +182,7 @@ return; } - if ( - // This is almost certainly an icon for an informational template like - // {{refimprove}} on enwiki. - $thumb.closest( '.metadata' ).length > 0 || - - // This is an article with no text. - $thumb.closest( '.noarticletext' ).length > 0 - ) { + if ( !bs.isAllowedThumb( $thumb ) ) { return; } diff --git a/tests/qunit/mmv/mmv.bootstrap.test.js b/tests/qunit/mmv/mmv.bootstrap.test.js index d0f41772f..796b8a77b 100644 --- a/tests/qunit/mmv/mmv.bootstrap.test.js +++ b/tests/qunit/mmv/mmv.bootstrap.test.js @@ -407,4 +407,28 @@ assert.ok( mw.loader.load.called, 'Dependencies should be preloaded if the thumb is hovered long enough' ); } ); + + QUnit.test( 'isAllowedThumb', 5, function ( assert ) { + var $container = $( '
' ), + $thumb = $( '' ).appendTo( $container ), + bootstrap = createBootstrap(); + + + assert.ok( bootstrap.isAllowedThumb( $thumb ), 'Normal image in a div is allowed.' ); + + $container.addClass( 'metadata' ); + assert.strictEqual( bootstrap.isAllowedThumb( $thumb ), false, 'Image in a metadata container is disallowed.' ); + + $container.prop( 'class', '' ); + $container.addClass( 'noviewer' ); + assert.strictEqual( bootstrap.isAllowedThumb( $thumb ), false, 'Image in a noviewer container is disallowed.' ); + + $container.prop( 'class', '' ); + $container.addClass( 'noarticletext' ); + assert.strictEqual( bootstrap.isAllowedThumb( $thumb ), false, 'Image in an empty article is disallowed.' ); + + $container.prop( 'class', '' ); + $thumb.addClass( 'noviewer' ); + assert.strictEqual( bootstrap.isAllowedThumb( $thumb ), false, 'Image with a noviewer class is disallowed.' ); + } ); }( mediaWiki, jQuery ) );