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
This commit is contained in:
m4tx 2014-12-30 02:26:17 +01:00
parent ed5d49f1b6
commit eb7d6ef537
2 changed files with 25 additions and 2 deletions

View file

@ -326,7 +326,14 @@
var $thumbCaption; var $thumbCaption;
if ( $thumbContain.length !== 0 && $thumbContain.is( '.thumb' ) ) { 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(); $thumbCaption.find( '.magnify' ).remove();
if ( !$thumbCaption.length ) { // gallery, maybe if ( !$thumbCaption.length ) { // gallery, maybe
$thumbCaption = $thumbContain $thumbCaption = $thumbContain

View file

@ -36,6 +36,19 @@
return link; return link;
} }
function createMultipleImage( images ) {
var contain = $( '<div>' ).addClass( 'thumb' ),
thumbinner = $( '<div>' ).addClass( 'thumbinner' ).appendTo( contain );
for ( var i = 0; i < images.length; ++i ) {
var div = $( '<div>' ).appendTo( thumbinner );
var thumbimage = $( '<div>' ).addClass( 'thumbimage' ).appendTo( div );
var link = $( '<a>' ).addClass( 'image' ).appendTo( thumbimage );
$( '<img>' ).prop( 'src', images[i][0] ).appendTo( link );
$( '<div>' ).addClass( 'thumbcaption' ).text( images[i][1] ).appendTo( div );
}
return contain;
}
function createBootstrap( viewer ) { function createBootstrap( viewer ) {
var bootstrap = new mw.mmv.MultimediaViewerBootstrap(); var bootstrap = new mw.mmv.MultimediaViewerBootstrap();
@ -450,14 +463,17 @@
assert.strictEqual( bootstrap.isAllowedThumb( $thumb ), false, 'Image with a noviewer class is disallowed.' ); 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' ), var gallery = createGallery( 'foo.jpg', 'Baz' ),
thumb = createThumb( 'foo.jpg', 'Quuuuux' ), thumb = createThumb( 'foo.jpg', 'Quuuuux' ),
link = createNormal( 'foo.jpg', 'Foobar' ), link = createNormal( 'foo.jpg', 'Foobar' ),
multiple = createMultipleImage( [ [ 'foo.jpg', 'Image #1' ], [ 'bar.jpg', 'Image #2' ],
[ 'foobar.jpg', 'Image #3' ] ] ),
bootstrap = createBootstrap(); bootstrap = createBootstrap();
assert.strictEqual( bootstrap.findCaption( gallery.find( '.thumb' ), gallery.find( 'a.image' ) ), 'Baz', 'A gallery caption is found.' ); 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( 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( $(), 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 ) ); }( mediaWiki, jQuery ) );