Skip to content

Commit 955e7ac

Browse files
authored
Fix 44987: Dynamic rendering of MathJax 3 fails (ILIAS-eLearning#11181)
https://mantis.ilias.de/view.php?id=44987 Merge of ILIAS-eLearning#9487 for ILIAS 10
1 parent 666c59f commit 955e7ac

File tree

4 files changed

+85
-33
lines changed

4 files changed

+85
-33
lines changed

components/ILIAS/Accordion/resources/accordion.js

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,18 @@
1-
/* */
1+
/**
2+
* This file is part of ILIAS, a powerful learning management system
3+
* published by ILIAS open source e-Learning e.V.
4+
*
5+
* ILIAS is licensed with the GPL-3.0,
6+
* see https://www.gnu.org/licenses/gpl-3.0.en.html
7+
* You should have received a copy of said license along with the
8+
* source code, too.
9+
*
10+
* If this is not the case or you just want to try ILIAS, you'll find
11+
* us at:
12+
* https://www.ilias.de
13+
* https://github.com/ILIAS-eLearning
14+
*
15+
******************************************************************** */
216

317
il.Accordion = {
418

@@ -474,10 +488,7 @@ il.Accordion = {
474488
rerenderContent: function(acc_el) {
475489

476490
// rerender mathjax
477-
if (typeof MathJax != "undefined" && typeof MathJax.Hub != "undefined") {
478-
MathJax.Hub.Queue(["Reprocess",MathJax.Hub, acc_el[0]]);
479-
}
480-
// see http://docs.mathjax.org/en/latest/typeset.html
491+
il.Util.renderMathJax([acc_el[0]], true);
481492

482493
// rerender google maps
483494
if (typeof ilMapRerender != "undefined") {

components/ILIAS/COPage/templates/default/tpl.question_export.html

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,8 @@
1919
jQuery('div#container{VAL_ID}').autoRender(questions[{VAL_ID}]);
2020
{HANDLE_IMAGES}
2121

22-
if (typeof MathJax != "undefined" && typeof MathJax.Hub != "undefined") {
23-
MathJax.Hub.Queue(["Typeset",MathJax.Hub, "container{VAL_ID}"]);
24-
}
22+
const el = document.getElementById("container{VAL_ID}");
23+
il.Util.renderMathJax([el]);
2524
}
2625
</script>
2726
<!-- END singlechoice -->
@@ -54,9 +53,8 @@
5453

5554
question.init();
5655

57-
if (typeof MathJax != "undefined" && typeof MathJax.Hub != "undefined") {
58-
MathJax.Hub.Queue(["Typeset", MathJax.Hub, "container{VAL_ID}"]);
59-
}
56+
const el = document.getElementById("container{VAL_ID}");
57+
il.Util.renderMathJax([el]);
6058
};
6159
</script>
6260
<!-- END multiplechoice -->
@@ -107,9 +105,8 @@
107105

108106
{HANDLE_IMAGES}
109107

110-
if (typeof MathJax != "undefined") {
111-
MathJax.Hub.Queue(["Typeset", MathJax.Hub, "container{VAL_ID}"]);
112-
}
108+
const el = document.getElementById("container{VAL_ID}");
109+
il.Util.renderMathJax([el]);
113110

114111
})(questions[{VAL_ID}]);
115112
};
@@ -134,10 +131,9 @@
134131
ilias.questions.shuffle(questions[{VAL_ID}]);
135132
jQuery('div#container{VAL_ID}').autoRender(questions[{VAL_ID}]);
136133
{HANDLE_IMAGES}
137-
if (typeof MathJax != "undefined") {
138-
MathJax.Hub.Queue(["Typeset", MathJax.Hub, "container{VAL_ID}"]);
139-
}
140-
il.test.orderingvertical.init(document.querySelector('#container{VAL_ID}'));
134+
const el = document.getElementById("#container{VAL_ID}");
135+
il.Util.renderMathJax([el]);
136+
il.test.orderingvertical.init(document.querySelector('#container{VAL_ID}'));
141137
}
142138
</script>
143139
<!-- END orderingquestion -->
@@ -287,10 +283,9 @@
287283

288284
$(document).ready(
289285
function () {
290-
if (typeof MathJax != "undefined" && typeof MathJax.Hub != "undefined") {
291-
MathJax.Hub.Queue(["Typeset", MathJax.Hub, "container{VAL_ID}"]);
292-
}
293-
il.test.matching.init(document.querySelector('#ilMatchingQuestionContainer_{VAL_ID}'), questionData.matching_mode);
286+
const el = document.getElementById("container{VAL_ID}");
287+
il.Util.renderMathJax([el]);
288+
il.test.matching.init(document.querySelector('#ilMatchingQuestionContainer_{VAL_ID}'), questionData.matching_mode);
294289
}
295290
);
296291

components/ILIAS/JavaScript/resources/Basic.js

Lines changed: 52 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -253,16 +253,63 @@ il.Util = {
253253
return false;
254254
},
255255

256+
/**
257+
* Render Mathjax
258+
* @param {array} elements Array of DOM elements
259+
* @see https://docs.mathjax.org/en/latest/web/typeset.html#typesetting-math-in-a-web-page
260+
*/
261+
renderMathJax(elements, reprocess = false) {
262+
if (typeof MathJax !== 'undefined') {
263+
if (typeof MathJax.Hub !== 'undefined') {
264+
// MathJax 2
265+
if (reprocess) {
266+
MathJax.Hub.Queue(['Reprocess', MathJax.Hub, elements]);
267+
} else {
268+
MathJax.Hub.Queue(['Typeset', MathJax.Hub, elements]);
269+
}
270+
} else {
271+
// MathJax 3
272+
const interval_id = setInterval((resolve, reject) => {
273+
if (typeof MathJax.startup.promise !== 'undefined') {
274+
clearInterval(interval_id);
275+
MathJax.startup.promise = MathJax.startup.promise
276+
.then(() => {
277+
if (reprocess) {
278+
MathJax.typesetClear(elements);
279+
}
280+
MathJax.typesetPromise()
281+
.catch((err) => console.log(`MathJax typesetting failed: ${err.message}`));
282+
});
283+
}
284+
});
285+
}
286+
}
287+
},
288+
256289
/**
257290
* print current window, thanks to anoack for the mathjax fix (see bug #)
258291
*/
259292
print() {
260293
if (typeof (window.print) !== 'undefined') {
261-
if (typeof MathJax !== 'undefined' && typeof MathJax.Hub !== 'undefined') {
262-
MathJax.Hub.Queue(
263-
['Delay', MathJax.Callback, 700],
264-
window.print,
265-
);
294+
if (typeof MathJax !== 'undefined') {
295+
if (typeof MathJax.Hub !== 'undefined') {
296+
// MathJax 2
297+
MathJax.Hub.Queue(
298+
['Delay', MathJax.Callback, 700],
299+
window.print,
300+
);
301+
} else {
302+
// MathJax 3
303+
const interval_id = setInterval((resolve, reject) => {
304+
if (typeof MathJax.startup.promise !== 'undefined') {
305+
clearInterval(interval_id);
306+
MathJax.startup.promise = MathJax.startup.promise
307+
.then(() => MathJax.typesetPromise()
308+
.then(window.print)
309+
.catch((err) => console.log(`MathJax typesetting failed: ${err.message}`)));
310+
}
311+
});
312+
}
266313
} else {
267314
window.print();
268315
}

components/ILIAS/TestQuestionPool/resources/js/dist/question_handling.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -296,9 +296,7 @@ ilias.questions.assTextQuestion = function(a_id) {
296296
const el = document.getElementById("feedback" + a_id);
297297
if (el) {
298298
el.style.display = '';
299-
if (typeof MathJax != "undefined") {
300-
MathJax.Hub.Queue(["Typeset",MathJax.Hub, el]);
301-
}
299+
il.Util.renderMathJax([el]);
302300
}
303301
answers[a_id].passed = true;
304302
ilias.questions.scormHandler(a_id,"neutral",jQuery('#textarea'+a_id).val());
@@ -679,6 +677,9 @@ ilias.questions.initClozeTest = function(a_id) {
679677
var parsed=jQuery("div#"+a_id).get(0).innerHTML.replace(/\[gap[\s\S\d]*?\](.*?)\[\/gap\]/g,
680678
() => {return _initClozeTestCallBack();});
681679
jQuery("div#"+a_id).html(parsed);
680+
681+
const el = document.getElementById("div#"+a_id);
682+
il.Util.renderMathJax([el]);
682683
};
683684

684685
ilias.questions.initLongMenu = function(a_id) {
@@ -957,9 +958,7 @@ ilias.questions.showFeedback = function(a_id) {
957958
const el = document.getElementById('feedback' + a_id);
958959
if (el) {
959960
el.style.display = '';
960-
if (typeof MathJax != "undefined" && typeof MathJax.Hub != "undefined") {
961-
MathJax.Hub.Queue(["Typeset",MathJax.Hub, el]);
962-
}
961+
il.Util.renderMathJax([el]);
963962
}
964963

965964
// update question overviews

0 commit comments

Comments
 (0)