Skip to content

Commit dfe1389

Browse files
authored
Merge pull request #1153 from mathjax/context
Add context.ts to hold hasWindow flag and browser's window and document objects.
2 parents b7d88a0 + 2a5a755 commit dfe1389

16 files changed

Lines changed: 105 additions & 51 deletions

File tree

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import './lib/explorer.js';
22

33
import {ExplorerHandler} from '#js/a11y/explorer.js';
4+
import {hasWindow} from '#js/util/context.js';
45

5-
if (MathJax.startup && typeof window !== 'undefined') {
6+
if (MathJax.startup && hasWindow) {
67
MathJax.startup.extendHandler(handler => ExplorerHandler(handler));
78
}

components/mjs/a11y/sre/sre.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import './lib/sre.js';
22
import './sre_config.js';
3+
import {context} from '#js/util/context.js';
34
import * as Sre from '#js/a11y/sre.js';
45

56
export {Sre};
67

78
if (MathJax.startup) {
8-
((typeof window !== 'undefined') ? window : global).
9-
SREfeature.custom = (loc) => Sre.preloadLocales(loc);
9+
(context.window || global).SREfeature.custom = (loc) => Sre.preloadLocales(loc);
1010
}
1111

components/mjs/a11y/sre/sre_config.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import {combineDefaults} from '#js/components/global.js';
22
import {Package} from '#js/components/package.js';
3+
import {context} from '#js/util/context.js';
34

45
// This sets up the correct link to the mathmaps files.
56
if (MathJax.startup) {
67

78
let path = Package.resolvePath('[sre]', false);
89

9-
if (typeof window !== 'undefined') {
10-
window.SREfeature = {json: path};
10+
if (context.window) {
11+
context.window.SREfeature = {json: path};
1112
} else {
1213
// In Node get the absolute path to the mathmaps directory.
1314
try {

components/mjs/input/tex/extension.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import {combineDefaults} from '#js/components/global.js';
2+
import {hasWindow} from '#js/util/context.js';
23

34
export function fontExtension(id, name, pkg = `@mathjax/${name}`) {
45
if (MathJax.loader) {
5-
const FONTPATH = (typeof document === 'undefined' ? pkg : `https://cdn.jsdelivr.net/npm/${name}`);
6+
const FONTPATH = hasWindow ? `https://cdn.jsdelivr.net/npm/${name}` : pkg;
67
const path = name.replace(/-font-extension$/, '-extension');
78
const extension = name.replace(/-font-extension$/, '');
89
const jax = (MathJax.config?.startup?.output || 'chtml');

components/mjs/output/util.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import {combineDefaults, combineWithMathJax} from '#js/components/global.js';
2-
import {Package} from "#js/components/package.js";
2+
import {Package} from '#js/components/package.js';
3+
import {hasWindow} from '#js/util/context.js';
34

4-
export const FONTPATH = (typeof document === 'undefined' ?
5-
'@mathjax/%%FONT%%-font' :
6-
'https://cdn.jsdelivr.net/npm/%%FONT%%-font');
5+
export const FONTPATH = hasWindow ?
6+
'https://cdn.jsdelivr.net/npm/%%FONT%%-font':
7+
'@mathjax/%%FONT%%-font';
78

89
export const OutputUtil = {
910
config(jax, jaxClass, defaultFont, fontClass) {

components/mjs/ui/menu/menu.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import './lib/menu.js';
22

33
import {combineDefaults} from '#js/components/global.js';
44
import {MenuHandler} from '#js/ui/menu/MenuHandler.js';
5+
import {hasWindow} from '#js/util/context.js';
56

6-
if (MathJax.startup && typeof window !== 'undefined') {
7+
if (MathJax.startup && hasWindow) {
78
MathJax.startup.extendHandler(handler => MenuHandler(handler), 20);
89
}

ts/a11y/explorer.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import {
3333
import { MathDocumentConstructor } from '../core/MathDocument.js';
3434
import { OptionList, expandable } from '../util/Options.js';
3535
import { SerializedMmlVisitor } from '../core/MmlTree/SerializedMmlVisitor.js';
36+
import { hasWindow } from '../util/context.js';
3637

3738
import { ExplorerPool, RegionPool } from './explorer/ExplorerPool.js';
3839

@@ -51,8 +52,6 @@ export type HTMLDOCUMENT = EnrichedMathDocument<HTMLElement, Text, Document>;
5152
export type HTMLMATHITEM = EnrichedMathItem<HTMLElement, Text, Document>;
5253
export type MATHML = MathML<HTMLElement, Text, Document>;
5354

54-
const hasWindow = typeof window !== 'undefined';
55-
5655
/*==========================================================================*/
5756

5857
/**

ts/adaptors/chooseAdaptor.ts

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,10 @@
2323

2424
import { liteAdaptor } from './liteAdaptor.js';
2525
import { browserAdaptor } from './browserAdaptor.js';
26-
27-
let choose;
28-
29-
try {
30-
// errors if not in browser
31-
document; // eslint-disable-line @typescript-eslint/no-unused-expressions
32-
choose = browserAdaptor;
33-
} catch (_e) {
34-
choose = liteAdaptor;
35-
}
26+
import { context } from '../util/context.js';
3627

3728
/**
38-
* Function to select which adaptor to use (depending on whether we are in a browser or node.js)
29+
* Select which adaptor to use (depending on whether we are in a browser or node.js)
3930
*/
40-
export const chooseAdaptor = choose;
31+
export const chooseAdaptor = context.document ? browserAdaptor : liteAdaptor;
32+

ts/components/loader.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,7 @@ import {
4040
} from './package.js';
4141
import { FunctionList } from '../util/FunctionList.js';
4242
import { mjxRoot } from '#root/root.js';
43-
44-
/*
45-
* The browser document (if any)
46-
*/
47-
declare const document: Document;
43+
import { context } from '../util/context.js';
4844

4945
/**
5046
* Function used to determine path to a given package.
@@ -272,11 +268,11 @@ export const Loader = {
272268
* @returns {string} The root location (directory for node.js, URL for browser)
273269
*/
274270
getRoot(): string {
275-
if (typeof document !== 'undefined') {
271+
if (context.document) {
276272
const script =
277-
document.currentScript || document.getElementById('MathJax-script');
278-
if (script) {
279-
return (script as HTMLScriptElement).src.replace(/\/[^/]*$/, '');
273+
context.document.currentScript || context.document.getElementById('MathJax-script');
274+
if (script && script instanceof HTMLScriptElement) {
275+
return script.src.replace(/\/[^/]*$/, '');
280276
}
281277
}
282278
return mjxRoot();

ts/components/package.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,7 @@
2323
*/
2424

2525
import { CONFIG, Loader } from './loader.js';
26-
27-
/*
28-
* The browser document (for creating scripts to load components)
29-
*/
30-
declare const document: Document;
26+
import { context } from '../util/context.js';
3127

3228
/**
3329
* A map of package names to Package instances
@@ -337,13 +333,13 @@ export class Package {
337333
* @param {string} url The URL to load from
338334
*/
339335
protected loadScript(url: string) {
340-
const script = document.createElement('script');
336+
const script = context.document.createElement('script');
341337
script.src = url;
342338
script.charset = 'UTF-8';
343339
script.onload = (_event) => this.checkLoad();
344340
script.onerror = (_event) => this.failed('Can\'t load "' + url + '"');
345341
// FIXME: Should there be a timeout failure as well?
346-
document.head.appendChild(script);
342+
context.document.head.appendChild(script);
347343
}
348344

349345
/**

0 commit comments

Comments
 (0)