Skip to content

Commit 611d09f

Browse files
authored
Merge pull request #1053 from mathjax/fix/enrichment_switch
Fix and catch speech computation errors, add missing braille option
2 parents 67c2871 + 9f793a4 commit 611d09f

File tree

5 files changed

+36
-46
lines changed

5 files changed

+36
-46
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,6 @@
141141
"mathjax-modern-font": "^4.0.0-beta.4",
142142
"mhchemparser": "^4.2.1",
143143
"mj-context-menu": "^0.9.1",
144-
"speech-rule-engine": "^4.1.0-beta.7"
144+
"speech-rule-engine": "^4.1.0-beta.8"
145145
}
146146
}

pnpm-lock.yaml

Lines changed: 10 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ts/a11y/explorer/KeyExplorer.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,13 @@ export class SpeechExplorer extends AbstractExplorer<string> implements KeyExplo
309309
return parent && this.highlighter.isMactionNode(parent) ? parent : null;
310310
}
311311

312+
/**
313+
* Computes the nesting depth announcement for the currently focused sub
314+
* expression.
315+
*
316+
* @param {HTMLElement} node The current node.
317+
* @return {HTMLElement} The refocused node.
318+
*/
312319
public depth(node: HTMLElement): HTMLElement {
313320
this.generators.depth(node, !!this.actionable(node));
314321
this.refocus(node);

ts/a11y/semantic-enrich.ts

Lines changed: 17 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -244,19 +244,23 @@ export function EnrichedMathItemMixin<N, T, D, B extends Constructor<AbstractMat
244244
*/
245245
public attachSpeech(document: MathDocument<N, T, D>) {
246246
if (this.state() >= STATE.ATTACHSPEECH) return;
247+
this.state(STATE.ATTACHSPEECH);
248+
if (this.isEscaped || !document.options.enableEnrichment) return;
247249
let [speech, braille] = this.existingSpeech();
248250
let [newSpeech, newBraille] = ['', ''];
249-
if (!speech || !braille ||
250-
document.options.enableSpeech || document.options.enableBraille) {
251-
[newSpeech, newBraille] = this.generatorPool.computeSpeech(
252-
this.typesetRoot, this.toMathML(this.root, this));
251+
if ((!speech && document.options.enableSpeech) ||
252+
(!braille && document.options.enableBraille)) {
253+
try {
254+
[newSpeech, newBraille] = this.generatorPool.computeSpeech(
255+
this.typesetRoot, this.toMathML(this.root, this));
256+
if (newSpeech) {
257+
newSpeech = buildSpeech(newSpeech)[0];
258+
}
259+
} catch (_e) { }
253260
}
254261
speech = speech || newSpeech;
255262
braille = braille || newBraille;
256-
if (!speech && !braille) {
257-
this.state(STATE.ATTACHSPEECH);
258-
return;
259-
}
263+
if (!speech && !braille) return;
260264
const adaptor = document.adaptor;
261265
const node = this.typesetRoot;
262266
if (speech) {
@@ -270,30 +274,6 @@ export function EnrichedMathItemMixin<N, T, D, B extends Constructor<AbstractMat
270274
}
271275
this.outputData.speech = speech;
272276
this.outputData.braille = braille;
273-
this.state(STATE.ATTACHSPEECH);
274-
}
275-
276-
/**
277-
* Retrieves the actual speech element that should be used as aria label.
278-
* @param {MmlNode} node The root node to search from.
279-
* @return {string} The speech content.
280-
*/
281-
protected getSpeech(node: MmlNode): string {
282-
const attributes = node.attributes;
283-
if (!attributes) return '';
284-
const speech = attributes.getExplicit('data-semantic-speech') as string;
285-
// TODO (explorer) For tree role move all speech etc. to container
286-
// element.
287-
if (!attributes.hasExplicit('data-semantic-parent') && speech) {
288-
return speech;
289-
}
290-
for (let child of node.childNodes) {
291-
let value = this.getSpeech(child);
292-
if (value) {
293-
return value;
294-
}
295-
}
296-
return '';
297277
}
298278

299279
};
@@ -359,6 +339,7 @@ export function EnrichedMathDocumentMixin<N, T, D, B extends MathDocumentConstru
359339
...BaseDocument.OPTIONS,
360340
enableEnrichment: true,
361341
enableSpeech: true,
342+
enableBraille: true,
362343
enrichError: (doc: EnrichedMathDocument<N, T, D>,
363344
math: EnrichedMathItem<N, T, D>,
364345
err: Error) => doc.enrichError(doc, math, err),
@@ -404,8 +385,10 @@ export function EnrichedMathDocumentMixin<N, T, D, B extends MathDocumentConstru
404385
*/
405386
public attachSpeech() {
406387
if (!this.processed.isSet('attach-speech')) {
407-
for (const math of this.math) {
408-
(math as EnrichedMathItem<N, T, D>).attachSpeech(this);
388+
if (this.options.enableSpeech || this.options.enableBraille) {
389+
for (const math of this.math) {
390+
(math as EnrichedMathItem<N, T, D>).attachSpeech(this);
391+
}
409392
}
410393
this.processed.set('attach-speech');
411394
}

ts/a11y/speech/GeneratorPool.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ export class GeneratorPool<N, T, D> {
394394
this.CleanUp(node);
395395
return this.lastSpeech;
396396
}
397-
let postfix = this.summaryGenerator.getExpandable(
397+
let postfix = this.summaryGenerator.getActionable(
398398
actionable ?
399399
(this.adaptor.childNodes(node).length === 0 ? -1 : 1)
400400
: 0);

0 commit comments

Comments
 (0)