From 68345654dac724d0f14d031dda190f0aed384e93 Mon Sep 17 00:00:00 2001 From: FrederikHennecke1 Date: Fri, 29 Nov 2024 02:34:57 +0100 Subject: [PATCH] Fix Preview not working with MathML rendering The Extension:Popup preview was only working in SVG mode. Added a new selector for the preview in MathML mode while keeping the old one working. The selector for the preview in MathML works via the href attribute. Bug: T381046 Change-Id: I81b5c3f2eabaf738de231868089ed80f0f19db6b --- modules/ext.math.popup.js | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/modules/ext.math.popup.js b/modules/ext.math.popup.js index f8ad2f015..ddd04779c 100644 --- a/modules/ext.math.popup.js +++ b/modules/ext.math.popup.js @@ -1,6 +1,7 @@ ( function () { 'use strict'; const previewType = 'math'; + const selector = '.mwe-math-element[href*="qid"], .mwe-math-element[data-qid] img'; const api = new mw.Rest(); const isValidId = function ( qid ) { return qid.match( /Q\d+/g ) === null; @@ -11,10 +12,24 @@ 'Accept-Language': mw.config.language } ); }; + const getQidStr = function ( parent ) { + if ( parent.getAttribute( 'href' ) ) { + const href = parent.getAttribute( 'href' ); + const match = href.match( /qid=(Q\d+)/ ); + if ( match ) { + return match[ 1 ]; + } + } + return null; + }; const fetchPreviewForTitle = function ( title, el ) { const deferred = $.Deferred(); - let qidstr = el.parentNode.parentNode.dataset.qid; - if ( isValidId( qidstr ) ) { + const parent = el.closest( '.mwe-math-element' ); + let qidstr = getQidStr( parent ); + if ( parent.dataset.qid ) { + qidstr = parent.dataset.qid; + } + if ( !qidstr || isValidId( qidstr ) ) { return deferred.reject(); } qidstr = qidstr.slice( 1 ); @@ -35,16 +50,16 @@ }; // popups require title attributes [].forEach.call( - document.querySelectorAll( '.mwe-math-element[data-qid] img' ), + document.querySelectorAll( selector ), ( node ) => { - if ( isValidId( node.parentNode.parentNode.dataset.qid ) ) { + const qidstr = getQidStr( node ); + if ( qidstr && isValidId( qidstr ) ) { node.dataset.title = 'math-unique-identifier'; } } ); const mathDisabledByUser = mw.user.isNamed() && mw.user.options.get( 'math-popups' ) !== '1'; - const selector = '.mwe-math-element[data-qid] img'; const mathAppliesToThisPage = document.querySelectorAll( selector ).length > 0; module.exports = !mathAppliesToThisPage || mathDisabledByUser ? null : {