Speed up caption collection

* don't do the slow lookup for closest caption unless there are
  multiple captions
* don't clone when there is no need to change the caption

Might give wrong result when selecting a thumbnail which is in a
multi-image template and has no caption but some other image
in the same template has; that seems hard to avoid.

Bug: T141714
Change-Id: Iacebd44ab9dd469d0c95b94e90faa90d579caaa3
This commit is contained in:
Gergő Tisza 2016-08-01 14:00:59 -07:00
parent 10a09b6600
commit 4518f8f27d

View file

@ -329,43 +329,48 @@
* @return {string|undefined} Unsafe HTML may be present - caution
*/
MMVB.findCaption = function ( $thumbContain, $link ) {
var $thumbCaption;
var $thumbCaption, $potentialCaptions;
if ( $thumbContain.length ) {
// 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, figcaption' ).clone();
}
$thumbCaption.find( '.magnify' ).remove();
if ( !$thumbCaption.length ) { // gallery, maybe
$thumbCaption = $thumbContain
.closest( '.gallerybox' )
.not( function () {
// do not treat categories as galleries - the autogenerated caption they have is not helpful
return $thumbContain.closest( '#mw-category-media' ).length;
} )
.not( function () {
// do not treat special file related pages as galleries
var $specialFileRelatedPages = $(
'.page-Special_NewFiles, ' +
'.page-Special_MostLinkedFiles,' +
'.page-Special_MostGloballyLinkedFiles, ' +
'.page-Special_UncategorizedFiles, ' +
'.page-Special_UnusedFiles'
);
return $thumbContain.closest( $specialFileRelatedPages ).length;
} )
.find( '.gallerytext' )
.clone();
}
return this.htmlUtils.htmlToTextWithTags( $thumbCaption.html() || '' );
} else if ( $link.prop( 'title' ) ) {
return $link.prop( 'title' );
if ( !$thumbContain.length ) {
return $link.prop( 'title' ) || undefined;
}
$potentialCaptions = $thumbContain.find( '.thumbcaption, figcaption' );
if ( $potentialCaptions.length < 2 ) {
$thumbCaption = $potentialCaptions.eq( 0 );
} else {
// Template:Multiple_image or some such; try to find closest caption to the image
$thumbCaption = $link.closest( ':has(> .thumbcaption)', $thumbContain )
.find( '> .thumbcaption' );
}
if ( !$thumbCaption.length ) { // gallery, maybe
$thumbCaption = $thumbContain
.closest( '.gallerybox' )
.not( function () {
// do not treat categories as galleries - the autogenerated caption they have is not helpful
return $thumbContain.closest( '#mw-category-media' ).length;
} )
.not( function () {
// do not treat special file related pages as galleries
var $specialFileRelatedPages = $(
'.page-Special_NewFiles, ' +
'.page-Special_MostLinkedFiles,' +
'.page-Special_MostGloballyLinkedFiles, ' +
'.page-Special_UncategorizedFiles, ' +
'.page-Special_UnusedFiles'
);
return $thumbContain.closest( $specialFileRelatedPages ).length;
} )
.find( '.gallerytext' );
}
if ( $thumbCaption.find( '.magnify' ).length ) {
$thumbCaption = $thumbCaption.clone();
$thumbCaption.find( '.magnify' ).remove();
}
return this.htmlUtils.htmlToTextWithTags( $thumbCaption.html() || '' );
};
/**