Skip to content

Commit 42fd15d

Browse files
authored
Merge pull request #1040 from mathjax/refactor/speechgenerator-pool
Introduces a speechgenerator pool
2 parents f5ddfc7 + 17c4e8d commit 42fd15d

File tree

12 files changed

+660
-283
lines changed

12 files changed

+660
-283
lines changed

ts/a11y/complexity/collapse.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,9 @@ export class Collapse {
505505

506506
const attributes = node.attributes.getAllAttributes();
507507
for (const name of Object.keys(attributes)) {
508-
if (name.substring(0, 14) === 'data-semantic-') {
508+
if (name.substring(0, 14) === 'data-semantic-' ||
509+
name.substring(0, 5) === 'aria-' ||
510+
name === 'role') {
509511
mrow.attributes.set(name, attributes[name]);
510512
delete attributes[name];
511513
}

ts/a11y/explorer.ts

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -96,14 +96,9 @@ export function ExplorerMathItemMixin<B extends Constructor<HTMLMATHITEM>>(
9696
public explorers: ExplorerPool;
9797

9898
/**
99-
* True when a rerendered element should regain the focus
99+
* Semantic id of the rerendered element that should regain the focus.
100100
*/
101-
protected refocus: boolean = false;
102-
103-
/**
104-
* Save explorer id during rerendering.
105-
*/
106-
protected savedId: string = null;
101+
protected refocus: number = null;
107102

108103
/**
109104
* Add the explorer to the output for this math item
@@ -116,10 +111,6 @@ export function ExplorerMathItemMixin<B extends Constructor<HTMLMATHITEM>>(
116111
if (!this.isEscaped && (document.options.enableExplorer || force)) {
117112
const node = this.typesetRoot;
118113
const mml = toMathML(this.root);
119-
if (this.savedId) {
120-
this.typesetRoot.setAttribute('sre-explorer-id', this.savedId);
121-
this.savedId = null;
122-
}
123114
if (!this.explorers) {
124115
this.explorers = new ExplorerPool();
125116
}
@@ -132,9 +123,12 @@ export function ExplorerMathItemMixin<B extends Constructor<HTMLMATHITEM>>(
132123
* @override
133124
*/
134125
public rerender(document: ExplorerMathDocument, start: number = STATE.RERENDER) {
135-
this.savedId = this.typesetRoot.getAttribute('sre-explorer-id');
136-
this.refocus = (hasWindow ? window.document.activeElement === this.typesetRoot : false);
137126
if (this.explorers) {
127+
let speech = this.explorers.speech;
128+
if (speech && speech.attached && speech.active) {
129+
const focus = speech.semanticFocus();
130+
this.refocus = focus ? focus.id : null;
131+
}
138132
this.explorers.reattach();
139133
}
140134
super.rerender(document, start);
@@ -145,11 +139,13 @@ export function ExplorerMathItemMixin<B extends Constructor<HTMLMATHITEM>>(
145139
*/
146140
public updateDocument(document: ExplorerMathDocument) {
147141
super.updateDocument(document);
148-
this.refocus && this.typesetRoot.focus();
142+
if (this.explorers?.speech) {
143+
this.explorers.speech.restarted = this.refocus;
144+
}
145+
this.refocus = null;
149146
if (this.explorers) {
150147
this.explorers.restart();
151148
}
152-
this.refocus = false;
153149
}
154150

155151
};

ts/a11y/explorer/ExplorerPool.ts

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import {LiveRegion, SpeechRegion, ToolTip, HoverRegion} from './Region.js';
2626
import type { ExplorerMathDocument, ExplorerMathItem } from '../explorer.js';
2727

2828
import {Explorer} from './Explorer.js';
29-
import * as ke from './KeyExplorer.js';
29+
import {SpeechExplorer} from './KeyExplorer.js';
3030
import * as me from './MouseExplorer.js';
3131
import {TreeColorer, FlameColorer} from './TreeExplorer.js';
3232

@@ -87,9 +87,9 @@ type ExplorerInit = (doc: ExplorerMathDocument, pool: ExplorerPool,
8787
*/
8888
let allExplorers: {[options: string]: ExplorerInit} = {
8989
speech: (doc: ExplorerMathDocument, pool: ExplorerPool, node: HTMLElement, ...rest: any[]) => {
90-
let explorer = ke.SpeechExplorer.create(
90+
let explorer = SpeechExplorer.create(
9191
doc, pool, doc.explorerRegions.speechRegion, node,
92-
doc.explorerRegions.brailleRegion, doc.explorerRegions.magnifier, rest[0], rest[1]) as ke.SpeechExplorer;
92+
doc.explorerRegions.brailleRegion, doc.explorerRegions.magnifier, rest[0], rest[1]) as SpeechExplorer;
9393
explorer.sound = true;
9494
return explorer;
9595
},
@@ -213,7 +213,7 @@ export class ExplorerPool {
213213
let keyExplorers = [];
214214
for (let key of Object.keys(this.explorers)) {
215215
let explorer = this.explorers[key];
216-
if (explorer instanceof ke.SpeechExplorer) {
216+
if (explorer instanceof SpeechExplorer) {
217217
explorer.AddEvents();
218218
explorer.stoppable = false;
219219
keyExplorers.unshift(explorer);
@@ -253,7 +253,9 @@ export class ExplorerPool {
253253
* Restarts explorers after a MathItem is rerendered.
254254
*/
255255
public restart() {
256-
this._restart.forEach(x => this.explorers[x].Start());
256+
this._restart.forEach(x => {
257+
this.explorers[x].Start();
258+
});
257259
this._restart = [];
258260
}
259261

@@ -275,7 +277,7 @@ export class ExplorerPool {
275277
{color: 'red'}, {color: 'black'},
276278
{renderer: this.document.outputJax.name, browser: 'v3'}
277279
);
278-
((this.explorers['speech'] as ke.SpeechExplorer).region as SpeechRegion).highlighter =
280+
(this.speech.region as SpeechRegion).highlighter =
279281
this.secondaryHighlighter;
280282
}
281283

@@ -295,6 +297,16 @@ export class ExplorerPool {
295297
this.highlighter.unhighlight();
296298
}
297299

300+
/**
301+
* Convenience method to return the speech explorer of the pool with the
302+
* correct type.
303+
*
304+
* @return {SpeechExplorer}
305+
*/
306+
public get speech(): SpeechExplorer {
307+
return this.explorers['speech'] as SpeechExplorer;
308+
}
309+
298310
/**
299311
* Retrieves color assignment for the document options.
300312
*

0 commit comments

Comments
 (0)