From eb7d6ef537f801f42d626f2b3945b3cb5ab20af4 Mon Sep 17 00:00:00 2001 From: m4tx Date: Tue, 30 Dec 2014 02:26:17 +0100 Subject: [PATCH] More accurate looking for image caption MediaViewer now handles Template:Multiple_image. Instead of looking for caption in whole thumbnail container, it tries to find the closest one to the image. Bug: T85354 Change-Id: I18d982a4bf245c4925213d83a3410274d499845e --- resources/mmv/mmv.bootstrap.js | 9 ++++++++- tests/qunit/mmv/mmv.bootstrap.test.js | 18 +++++++++++++++++- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/resources/mmv/mmv.bootstrap.js b/resources/mmv/mmv.bootstrap.js index 4efd48d8f..d18b4ba81 100644 --- a/resources/mmv/mmv.bootstrap.js +++ b/resources/mmv/mmv.bootstrap.js @@ -326,7 +326,14 @@ var $thumbCaption; if ( $thumbContain.length !== 0 && $thumbContain.is( '.thumb' ) ) { - $thumbCaption = $thumbContain.find( '.thumbcaption' ).clone(); + // try to find closest caption to the image + $thumbCaption = $link.closest( ':has(> .thumbcaption)', $thumbContain ) + .find( '> .thumbcaption' ) + .clone(); + if ( !$thumbCaption.length ) { + // if nothing is found, look for the caption in whole container + $thumbCaption = $thumbContain.find( '.thumbcaption' ).clone(); + } $thumbCaption.find( '.magnify' ).remove(); if ( !$thumbCaption.length ) { // gallery, maybe $thumbCaption = $thumbContain diff --git a/tests/qunit/mmv/mmv.bootstrap.test.js b/tests/qunit/mmv/mmv.bootstrap.test.js index 2be14f9d4..df2b76666 100644 --- a/tests/qunit/mmv/mmv.bootstrap.test.js +++ b/tests/qunit/mmv/mmv.bootstrap.test.js @@ -36,6 +36,19 @@ return link; } + function createMultipleImage( images ) { + var contain = $( '
' ).addClass( 'thumb' ), + thumbinner = $( '
' ).addClass( 'thumbinner' ).appendTo( contain ); + for ( var i = 0; i < images.length; ++i ) { + var div = $( '
' ).appendTo( thumbinner ); + var thumbimage = $( '
' ).addClass( 'thumbimage' ).appendTo( div ); + var link = $( '' ).addClass( 'image' ).appendTo( thumbimage ); + $( '' ).prop( 'src', images[i][0] ).appendTo( link ); + $( '
' ).addClass( 'thumbcaption' ).text( images[i][1] ).appendTo( div ); + } + return contain; + } + function createBootstrap( viewer ) { var bootstrap = new mw.mmv.MultimediaViewerBootstrap(); @@ -450,14 +463,17 @@ assert.strictEqual( bootstrap.isAllowedThumb( $thumb ), false, 'Image with a noviewer class is disallowed.' ); } ); - QUnit.test( 'findCaption', 3, function ( assert ) { + QUnit.test( 'findCaption', 4, function ( assert ) { var gallery = createGallery( 'foo.jpg', 'Baz' ), thumb = createThumb( 'foo.jpg', 'Quuuuux' ), link = createNormal( 'foo.jpg', 'Foobar' ), + multiple = createMultipleImage( [ [ 'foo.jpg', 'Image #1' ], [ 'bar.jpg', 'Image #2' ], + [ 'foobar.jpg', 'Image #3' ] ] ), bootstrap = createBootstrap(); assert.strictEqual( bootstrap.findCaption( gallery.find( '.thumb' ), gallery.find( 'a.image' ) ), 'Baz', 'A gallery caption is found.' ); assert.strictEqual( bootstrap.findCaption( thumb, thumb.find( 'a.image' ) ), 'Quuuuux', 'A thumbnail caption is found.' ); assert.strictEqual( bootstrap.findCaption( $(), link ), 'Foobar', 'The caption is found even if the image is not a thumbnail.' ); + assert.strictEqual( bootstrap.findCaption( multiple, multiple.find( 'img[src="bar.jpg"]' ).closest( 'a' ) ), 'Image #2', 'The caption is found in {{Multiple image}}.' ); } ); }( mediaWiki, jQuery ) );