From b0f83b39fb8a3ee49030f83fe609b4150617c36d Mon Sep 17 00:00:00 2001 From: Arlo Breault Date: Wed, 26 Oct 2022 19:53:42 -0400 Subject: [PATCH] Use Parsoid's regularity to find caption Follow up to Ic652576f84e07c9fc8ef8be216a6fc94466198a6 where this was hinted at. The comment about active formatting elements is removed due to I7b4e9df8490357f44d31d6a869fa9b7a15f029ea. Bug: T321440 Change-Id: Ifb12dac64bd7a7fb8444ac2ada557868502cf213 --- resources/mmv.bootstrap/mmv.bootstrap.js | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/resources/mmv.bootstrap/mmv.bootstrap.js b/resources/mmv.bootstrap/mmv.bootstrap.js index 823e80eaa..49008f9eb 100644 --- a/resources/mmv.bootstrap/mmv.bootstrap.js +++ b/resources/mmv.bootstrap/mmv.bootstrap.js @@ -292,9 +292,6 @@ * Processes a Parsoid thumb, making use of the specified structure, * https://www.mediawiki.org/wiki/Specs/HTML#Media * - * Active formatting elements are sometimes re-opened inside - * the structure and accounted for using .closest() - * * @param {Object} thumb */ MMVB.processParsoidThumb = function ( thumb ) { @@ -309,7 +306,9 @@ ), link = $link.prop( 'href' ), alt = $thumb.attr( 'alt' ), - title = mw.Title.newFromImg( $thumb ); + title = mw.Title.newFromImg( $thumb ), + caption, + $thumbCaption; if ( !bs.isValidExtension( title ) ) { // Short-circuit event handler and interface setup, because @@ -325,6 +324,13 @@ bs.preloadAssets( $thumbContainer ); } + if ( ( $thumbContainer.prop( 'tagName' ) || '' ).toLowerCase() === 'figure' ) { + $thumbCaption = $thumbContainer.find( 'figcaption' ); + caption = this.htmlUtils.htmlToTextWithTags( $thumbCaption.html() || '' ); + } else { + caption = $link.prop( 'title' ) || undefined; + } + // This is the data that will be passed onto the mmv this.thumbs.push( { thumb: thumb, @@ -332,8 +338,7 @@ title: title, link: link, alt: alt, - // FIXME: findCaption can further make use of Parsoid's regularity - caption: this.findCaption( $thumbContainer, $link ) + caption: caption } ); $link.on( 'click', function ( e ) { @@ -469,7 +474,7 @@ return $link.prop( 'title' ) || undefined; } - $potentialCaptions = $thumbContainer.find( '.thumbcaption, figcaption' ); + $potentialCaptions = $thumbContainer.find( '.thumbcaption' ); if ( $potentialCaptions.length < 2 ) { $thumbCaption = $potentialCaptions.eq( 0 ); } else {