Skip to content

Commit 2ff6ae6

Browse files
committed
PIE-423
1 parent 090cd56 commit 2ff6ae6

3 files changed

Lines changed: 80 additions & 1 deletion

File tree

packages/elements-react/multiple-choice/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@pie-element/multiple-choice",
3-
"version": "13.1.1-next.1",
3+
"version": "13.1.0",
44
"description": "React implementation of multiple-choice element synced from pie-elements",
55
"dependencies": {
66
"@emotion/react": "^11.14.0",

packages/shared/math-rendering-mathjax/src/adapter.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,30 +16,43 @@ interface MathJaxTexConfig {
1616
}
1717

1818
interface MathJaxConfig {
19+
loader?: {
20+
load?: string[];
21+
};
1922
tex?: MathJaxTexConfig;
2023
options?: {
2124
enableMenu?: boolean;
2225
enableExplorer?: boolean;
26+
enableAssistiveMml?: boolean;
2327
};
2428
startup?: {
2529
ready?: () => void;
2630
defaultReady?: () => void;
31+
document?: MathJaxDocument;
2732
};
2833
chtml?: {
2934
fontURL?: string;
3035
};
3136
}
3237

38+
interface MathJaxDocument {
39+
assistiveMml?: () => {
40+
updateDocument?: () => unknown;
41+
};
42+
}
43+
3344
interface MathJaxInstance {
3445
version?: string;
3546
tex?: MathJaxTexConfig;
3647
options?: {
3748
enableMenu?: boolean;
3849
enableExplorer?: boolean;
50+
enableAssistiveMml?: boolean;
3951
};
4052
startup: {
4153
ready?: () => void;
4254
defaultReady: () => void;
55+
document?: MathJaxDocument;
4356
};
4457
typesetPromise?: (elements?: Element[]) => Promise<void>;
4558
}
@@ -71,6 +84,9 @@ function ensureMathjaxLoaded(options: MathjaxOptions): Promise<void> {
7184
const { useSingleDollar = false, accessibility = true, loadFonts = true, srcUrl } = options;
7285

7386
const config: MathJaxConfig = {
87+
loader: {
88+
load: accessibility ? ['a11y/assistive-mml'] : [],
89+
},
7490
tex: {
7591
packages: ['base', 'ams', 'autoload'],
7692
macros: {
@@ -83,6 +99,7 @@ function ensureMathjaxLoaded(options: MathjaxOptions): Promise<void> {
8399
options: {
84100
enableMenu: accessibility,
85101
enableExplorer: accessibility,
102+
enableAssistiveMml: accessibility,
86103
},
87104
startup: {
88105
ready: () => {
@@ -124,6 +141,20 @@ function ensureMathjaxLoaded(options: MathjaxOptions): Promise<void> {
124141
return mathjaxLoading;
125142
}
126143

144+
function attachAssistiveMml(): void {
145+
const mathDocument = window.MathJax?.startup?.document;
146+
147+
if (typeof mathDocument?.assistiveMml !== 'function') {
148+
return;
149+
}
150+
151+
try {
152+
mathDocument.assistiveMml().updateDocument?.();
153+
} catch (error) {
154+
console.warn('[mathjax-renderer] Failed to attach assistive MathML:', error);
155+
}
156+
}
157+
127158
/**
128159
* Create a MathJax-based math renderer
129160
*
@@ -155,6 +186,7 @@ export function createMathjaxRenderer(
155186
}
156187

157188
await window.MathJax.typesetPromise([element]);
189+
attachAssistiveMml();
158190
};
159191
}
160192

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { afterEach, describe, expect, it, vi } from 'vitest';
2+
import { createMathjaxRenderer } from '../src/adapter.js';
3+
4+
describe('createMathjaxRenderer', () => {
5+
afterEach(() => {
6+
vi.restoreAllMocks();
7+
document.body.innerHTML = '';
8+
delete window.MathJax;
9+
});
10+
11+
it('attaches assistive MathML for screen readers after typesetting', async () => {
12+
const target = document.createElement('div');
13+
target.innerHTML = '\\(x^2 + 1\\)';
14+
document.body.append(target);
15+
16+
const updatedDocument = {
17+
assistiveMml: vi.fn(() => {
18+
const container = target.querySelector('mjx-container');
19+
const assistiveMath = document.createElement('mjx-assistive-mml');
20+
assistiveMath.innerHTML =
21+
'<math><msup><mi>x</mi><mn>2</mn></msup><mo>+</mo><mn>1</mn></math>';
22+
container?.append(assistiveMath);
23+
return updatedDocument;
24+
}),
25+
updateDocument: vi.fn(() => updatedDocument),
26+
};
27+
const mathDocument = {
28+
assistiveMml: vi.fn(() => updatedDocument.assistiveMml()),
29+
};
30+
31+
window.MathJax = {
32+
version: '4.0.0',
33+
startup: {
34+
defaultReady: vi.fn(),
35+
document: mathDocument,
36+
},
37+
typesetPromise: vi.fn(async () => {
38+
target.innerHTML = '<mjx-container>x squared plus 1</mjx-container>';
39+
}),
40+
};
41+
42+
await createMathjaxRenderer()(target);
43+
44+
expect(mathDocument.assistiveMml).toHaveBeenCalled();
45+
expect(target.querySelector('mjx-assistive-mml math')).not.toBeNull();
46+
});
47+
});

0 commit comments

Comments
 (0)