@@ -118,6 +118,11 @@ export interface EnrichedMathItem<N, T, D> extends MathItem<N, T, D> {
118118 * @param {MathDocument } document The document where enrichment is occurring
119119 */
120120 attachSpeech ( document : MathDocument < N , T , D > ) : void ;
121+
122+ /**
123+ * @param {MathDocument } document The MathDocument for the MathItem
124+ */
125+ unEnrich ( document : MathDocument < N , T , D > ) : void ;
121126}
122127
123128/**
@@ -201,6 +206,19 @@ export function EnrichedMathItemMixin<N, T, D, B extends Constructor<AbstractMat
201206 this . state ( STATE . ENRICHED ) ;
202207 }
203208
209+ /**
210+ * @param {MathDocument } document The MathDocument for the MathItem
211+ */
212+ public unEnrich ( document : MathDocument < N , T , D > ) {
213+ const mml = this . inputData . originalMml ;
214+ if ( ! mml ) return ;
215+ const math = new document . options . MathItem ( '' , MmlJax ) ;
216+ math . math = mml ;
217+ math . display = this . display ;
218+ math . compile ( document ) ;
219+ this . root = math . root ;
220+ }
221+
204222 /**
205223 * Correct the selection values for the maction items from the original MathML
206224 */
@@ -248,25 +266,28 @@ export function EnrichedMathItemMixin<N, T, D, B extends Constructor<AbstractMat
248266 if ( this . isEscaped || ! document . options . enableEnrichment ) return ;
249267 let [ speech , braille ] = this . existingSpeech ( ) ;
250268 let [ newSpeech , newBraille ] = [ '' , '' ] ;
251- if ( ( ! speech && document . options . enableSpeech ) ||
252- ( ! braille && document . options . enableBraille ) ) {
269+ const options = document . options ;
270+ if ( ( ! speech && options . enableSpeech ) ||
271+ ( ! braille && options . enableBraille ) ) {
253272 try {
254273 [ newSpeech , newBraille ] = this . generatorPool . computeSpeech (
255274 this . typesetRoot , this . toMathML ( this . root , this ) ) ;
256275 if ( newSpeech ) {
257276 newSpeech = buildSpeech ( newSpeech ) [ 0 ] ;
258277 }
259- } catch ( _e ) { }
278+ } catch ( err ) {
279+ document . options . speechError ( document , this , err ) ;
280+ }
260281 }
261282 speech = speech || newSpeech ;
262283 braille = braille || newBraille ;
263284 if ( ! speech && ! braille ) return ;
264285 const adaptor = document . adaptor ;
265286 const node = this . typesetRoot ;
266- if ( speech && document . options . enableSpeech ) {
287+ if ( speech && options . enableSpeech ) {
267288 adaptor . setAttribute ( node , 'aria-label' , speech as string ) ;
268289 }
269- if ( braille && document . options . enableBraille ) {
290+ if ( braille && options . enableBraille ) {
270291 adaptor . setAttribute ( node , 'aria-braillelabel' , braille as string ) ;
271292 }
272293 for ( const child of adaptor . childNodes ( node ) as N [ ] ) {
@@ -311,6 +332,13 @@ export interface EnrichedMathDocument<N, T, D> extends AbstractMathDocument<N, T
311332 * @param {Error } err The error being processed
312333 */
313334 enrichError ( doc : EnrichedMathDocument < N , T , D > , math : EnrichedMathItem < N , T , D > , err : Error ) : void ;
335+
336+ /**
337+ * @param {EnrichedMathDocument } doc The MathDocument for the error
338+ * @paarm {EnrichedMathItem} math The MathItem causing the error
339+ * @param {Error } err The error being processed
340+ */
341+ speechError ( doc : EnrichedMathDocument < N , T , D > , math : EnrichedMathItem < N , T , D > , err : Error ) : void ;
314342}
315343
316344/**
@@ -343,6 +371,9 @@ export function EnrichedMathDocumentMixin<N, T, D, B extends MathDocumentConstru
343371 enrichError : ( doc : EnrichedMathDocument < N , T , D > ,
344372 math : EnrichedMathItem < N , T , D > ,
345373 err : Error ) => doc . enrichError ( doc , math , err ) ,
374+ speechError : ( doc : EnrichedMathDocument < N , T , D > ,
375+ math : EnrichedMathItem < N , T , D > ,
376+ err : Error ) => doc . speechError ( doc , math , err ) ,
346377 renderActions : expandable ( {
347378 ...BaseDocument . OPTIONS . renderActions ,
348379 enrich : [ STATE . ENRICHED ] ,
@@ -416,13 +447,24 @@ export function EnrichedMathDocumentMixin<N, T, D, B extends MathDocumentConstru
416447 console . warn ( 'Enrichment error:' , err ) ;
417448 }
418449
450+ /**
451+ */
452+ public speechError ( _doc : EnrichedMathDocument < N , T , D > , _math : EnrichedMathItem < N , T , D > , err : Error ) {
453+ console . warn ( 'Speech generation error:' , err ) ;
454+ }
455+
419456 /**
420457 * @override
421458 */
422459 public state ( state : number , restore : boolean = false ) {
423460 super . state ( state , restore ) ;
424461 if ( state < STATE . ENRICHED ) {
425462 this . processed . clear ( 'enriched' ) ;
463+ if ( state >= STATE . COMPILED ) {
464+ for ( const item of this . math ) {
465+ ( item as EnrichedMathItem < N , T , D > ) . unEnrich ( this ) ;
466+ }
467+ }
426468 }
427469 if ( state < STATE . ATTACHSPEECH ) {
428470 this . processed . clear ( 'attach-speech' ) ;
0 commit comments