Skip to content

Commit f98da55

Browse files
committed
Fix path handling to work with Windows and add tests for it
1 parent afaf1ea commit f98da55

21 files changed

Lines changed: 136 additions & 27 deletions

components/mjs/a11y/speech/speech.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,20 @@ import './lib/speech.js';
22

33
import {combineDefaults} from '#js/components/global.js';
44
import {Package} from '#js/components/package.js';
5-
import {hasWindow} from '#js/util/context.js';
5+
import {context} from '#js/util/context.js';
66
import {SpeechHandler} from '#js/a11y/speech.js';
77

88
if (MathJax.loader) {
99
let path = Package.resolvePath('[sre]', false);
1010
let maps = Package.resolvePath('[mathmaps]', false);
11-
if (hasWindow) {
11+
if (context.window) {
1212
path = new URL(path, location).href;
1313
maps = new URL(maps, location).href;
1414
} else {
1515
const REQUIRE = typeof require !== 'undefined' ? require : MathJax.config.loader.require;
1616
if (REQUIRE?.resolve) {
17-
path = REQUIRE.resolve(`${path}/require.mjs`).replace(/\/[^\/]*$/, '');
18-
maps = REQUIRE.resolve(`${maps}/base.json`).replace(/\/[^\/]*$/, '');
17+
path = context.path(REQUIRE.resolve(`${path}/require.mjs`)).replace(/\/[^\/]*$/, '');
18+
maps = context.path(REQUIRE.resolve(`${maps}/base.json`)).replace(/\/[^\/]*$/, '');
1919
} else {
2020
path = maps = '';
2121
}

components/mjs/node-main/node-main-setup.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ global.require = createRequire(import.meta.url);
44
const path = require("path");
55

66
if (!global.MathJax) global.MathJax = {};
7-
global.MathJax.__dirname = path.dirname(new URL(import.meta.url).pathname);
7+
global.MathJax.__dirname = path.dirname(new URL(import.meta.url).pathname);

components/mjs/node-main/node-main.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,15 @@ import '../startup/init.js';
2323
import {Loader, CONFIG} from '#js/components/loader.js';
2424
import {Package} from '#js/components/package.js';
2525
import {combineDefaults, combineConfig} from '#js/components/global.js';
26+
import {context} from '#js/util/context.js';
2627
import '../core/core.js';
2728
import '../adaptors/liteDOM/liteDOM.js';
2829
import {source} from '../source.js';
2930

3031
const MathJax = global.MathJax;
3132

3233
const path = eval('require("path")'); // get path from node, not webpack
33-
const dir = MathJax.config.__dirname; // set up by node-main.mjs or node-main.cjs
34+
const dir = context.path(MathJax.config.__dirname); // set up by node-main.mjs or node-main.cjs
3435

3536
/*
3637
* Set up the initial configuration

components/mjs/source-lab.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,4 @@
1515
* limitations under the License.
1616
*/
1717

18-
export const src = String(new URL('.', import.meta.url)).replace(/\/$/, '');
18+
export const dirname = String(new URL('.', import.meta.url)).replace(/\/$/, '');

components/mjs/source.cjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,4 @@
1515
* limitations under the License.
1616
*/
1717

18-
module.exports.src = __dirname;
18+
module.exports.dirname = __dirname;

components/mjs/source.d.cts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export declare const src: string;
1+
export declare const dirname: string;

components/mjs/source.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
* limitations under the License.
1616
*/
1717

18-
import {src} from '#source/source.cjs';
18+
import {dirname} from '#source/source.cjs';
19+
import {context} from '#js/util/context.js';
20+
const src = context.path(dirname);
1921

2022
export const source = {
2123
'core': `${src}/core/core.js`,

testsuite/tests/util/Context-android.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ describe('context object', () => {
77

88
test('context', async () => {
99
let {context, hasWindow} = await import("#js/util/context.js");
10+
expect(context.path('C:\\test.js')).toBe('C:\\test.js');
11+
delete context.path;
1012
expect(context).toEqual({window: window, document: window.document, os: 'Unix'});
1113
expect(hasWindow).toBe(true);
1214
});

testsuite/tests/util/Context-browser.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ describe('context object', () => {
77

88
test('context', async () => {
99
let {context, hasWindow} = await import("#js/util/context.js");
10+
expect(context.path('C:\\test.js')).toBe('C:\\test.js');
11+
delete context.path;
1012
expect(context).toEqual({window: window, document: window.document, os: 'Unix'});
1113
expect(hasWindow).toBe(true);
1214
});
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { describe, test, expect } from '@jest/globals';
2+
3+
global.process = {...process, platform: 'test'} as any;
4+
5+
describe('context object', () => {
6+
7+
test('context', async () => {
8+
let {context, hasWindow} = await import("#js/util/context.js");
9+
expect(context.path('C:\\test.js')).toBe('C:\\test.js');
10+
delete context.path;
11+
expect(context).toEqual({window: null, document: null, os: 'test'});
12+
expect(hasWindow).toBe(false);
13+
});
14+
15+
});

0 commit comments

Comments
 (0)