@@ -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 }
0 commit comments