Skip to content

Commit 87940ce

Browse files
authored
Merge pull request #4475 from dpalou/MOBILE-4828
Mobile 4828
2 parents 119c609 + 3880aaf commit 87940ce

File tree

2 files changed

+27
-24
lines changed

2 files changed

+27
-24
lines changed

scripts/copy-assets.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ const ASSETS = {
2323
'/node_modules/mathjax/es5/input/mml/extensions': '/lib/mathjax/input/mml/extensions',
2424
'/node_modules/mathjax/es5/input/tex/extensions': '/lib/mathjax/input/tex/extensions',
2525
'/node_modules/mathjax/es5/output/chtml/fonts/woff-v2': '/lib/mathjax/output/chtml/fonts/woff-v2',
26+
'/node_modules/mathjax/es5/ui/safe.js': '/lib/mathjax/ui/safe.js',
2627
'/node_modules/mp3-mediarecorder/dist/vmsg.wasm': '/lib/vmsg/vmsg.wasm',
2728
'/src/core/features/h5p/assets': '/lib/h5p',
2829
'/node_modules/ogv/dist': '/lib/ogv',

src/addons/filter/mathjaxloader/services/handlers/mathjaxloader.ts

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import { CoreText } from '@singletons/text';
2121
import { CorePromiseUtils } from '@singletons/promise-utils';
2222
import { CoreSite } from '@classes/sites/site';
2323
import { makeSingleton } from '@singletons';
24-
import { CoreWait } from '@singletons/wait';
2524
import { CoreDom } from '@singletons/dom';
2625
import { CoreLogger } from '@singletons/logger';
2726

@@ -40,32 +39,52 @@ export class AddonFilterMathJaxLoaderHandlerService extends CoreFilterDefaultHan
4039
/**
4140
* Initialize MathJax.
4241
*/
43-
initialize(): void {
42+
async initialize(): Promise<void> {
4443
if (document.head.querySelector('#core-filter-mathjax-script')) {
4544
// Script already added, don't add it again.
45+
await this.window.MathJax?.startup.promise;
46+
4647
return;
4748
}
4849

4950
// The MathJax configuration needs to be created before loading the MathJax script. Changing the options
5051
// after MathJax is initialized doesn't work (e.g. chaning window.MathJax.options or window.MathJax.config.options).
52+
// @todo: Obtain mathjaxconfig from the site.
5153
this.window.MathJax = {
5254
options: {
5355
enableMenu: false, // Disable right-click menu on equations.
5456
},
5557
startup: {
5658
typeset: false, // Don't run typeset automatically on the whole page when MathJax is loaded.
5759
},
60+
loader: {
61+
load: ['ui/safe'], // Prevent XSS.
62+
},
5863
};
5964

6065
// Add the script to the header.
61-
const script = document.createElement('script');
62-
script.id = 'core-filter-mathjax-script';
63-
script.src = 'assets/lib/mathjax/tex-mml-chtml.js';
64-
document.head.appendChild(script);
66+
await this.loadMathJax();
67+
68+
await this.window.MathJax?.startup.promise;
6569

6670
// @todo: Once MathJax supports locale, set current language and listen for CoreEvents.LANGUAGE_CHANGED events.
6771
}
6872

73+
/**
74+
* Load the MathJax script.
75+
*/
76+
protected loadMathJax(): Promise<void> {
77+
return new Promise((resolve, reject) => {
78+
const script = document.createElement('script');
79+
script.onload = () => resolve();
80+
script.onerror = (error) => reject(error);
81+
script.id = 'core-filter-mathjax-script';
82+
script.src = 'assets/lib/mathjax/tex-mml-chtml.js';
83+
script.type = 'text/javascript';
84+
document.head.appendChild(script);
85+
});
86+
}
87+
6988
/**
7089
* @inheritdoc
7190
*/
@@ -152,8 +171,7 @@ export class AddonFilterMathJaxLoaderHandlerService extends CoreFilterDefaultHan
152171
return;
153172
}
154173

155-
this.initialize();
156-
await this.waitForReady();
174+
await this.initialize();
157175

158176
await Promise.all(equations.map((node) => this.typesetNode(node)));
159177
}
@@ -183,22 +201,6 @@ export class AddonFilterMathJaxLoaderHandlerService extends CoreFilterDefaultHan
183201
}
184202
}
185203

186-
/**
187-
* Wait for the MathJax library and our JS object to be loaded.
188-
*
189-
* @param retries Number of times this has been retried.
190-
* @returns Promise resolved when ready or if it took too long to load.
191-
*/
192-
protected async waitForReady(retries: number = 0): Promise<void> {
193-
if (this.window.MathJax?.typesetPromise || retries >= 25) {
194-
// Loaded or too many retries, stop.
195-
return;
196-
}
197-
198-
await CoreWait.wait(20 + 10 * retries);
199-
await CorePromiseUtils.ignoreErrors(this.waitForReady(retries + 1));
200-
}
201-
202204
/**
203205
* Find math environments in the $text and wrap them in no link spans
204206
* (<span class="nolink"></span>). If math environments are nested, only

0 commit comments

Comments
 (0)