@@ -60,6 +60,7 @@ var defaults = {
6060 svg : false , // return svg output?
6161 svgNode : false , // DOM element for SVG output?
6262 timeout : 10 * 1000 , // 10 second timeout before restarting MathJax
63+ speakText : true , // add spoken annotations to svg output
6364} ;
6465
6566//
@@ -513,6 +514,10 @@ function AddError(message,nopush) {
513514function GetMML ( result ) {
514515 if ( ! data . mml && ! data . mmlNode ) return ;
515516 var jax = MathJax . Hub . getAllJax ( ) [ 0 ] ;
517+ if ( data . speakText && ! jax . root . alttext ) {
518+ jax . root . alttext = result . speakText ;
519+ jax . root . attrNames . push ( "alttext" ) ;
520+ }
516521 try {
517522 if ( data . mml ) result . mml = jax . root . toMathML ( '' , jax ) ;
518523 if ( data . mmlNode ) result . mmlNode = jsdom ( result . mml ) . body . firstChild ;
@@ -523,20 +528,16 @@ function GetMML(result) {
523528}
524529
525530//
526- // Creates dummy speech string and updates the MathML to include it, if needed
531+ // Creates speech string and updates the MathML to include it, if needed
527532//
528533function GetSpeech ( result ) {
534+ if ( ! data . speakText ) return ;
529535 result . speakText = "Equation" ;
530536 if ( data . format !== "MathML" ) result . speakText = data . math ;
531- if ( ! data . mml ) return ;
532- var jax = MathJax . Hub . getAllJax ( ) [ 0 ] ;
533- if ( jax . root . alttext ) result . speakText = jax . root . alttext ;
534- else jax . root . alttext = result . speakText ;
535- var attrNames = jax . root . attrNames ;
536- if ( attrNames && attrNames . indexOf ( "alttext" ) === - 1 ) {
537- attrNames . push ( "alttext" ) ;
537+ else {
538+ var jax = MathJax . Hub . getAllJax ( ) [ 0 ] ;
539+ if ( jax . root . alttext ) result . speakText = jax . root . alttext ;
538540 }
539- result . mml = jax . root . toMathML ( '' , jax ) ;
540541}
541542
542543//
@@ -547,11 +548,11 @@ function GetHTML(result) {
547548 var jax = MathJax . Hub . getAllJax ( ) [ 0 ] ; if ( ! jax ) return ;
548549 var script = jax . SourceElement ( ) , html = script . previousSibling ;
549550
550- // add dummy speech text if there isn't one
551- for ( var i = 0 , m = html . childNodes . length ; i < m ; i ++ )
552- html . childNodes [ i ] . setAttribute ( "aria-hidden" , true ) ;
553- if ( ! html . getAttribute ( "aria-label" ) ) {
554- html . setAttribute ( "aria-label" , result . speakText ) ;
551+ // add speech text if there isn't one
552+ if ( data . speakText ) {
553+ for ( var i = 0 , m = html . childNodes . length ; i < m ; i ++ )
554+ html . childNodes [ i ] . setAttribute ( "aria-hidden" , true ) ;
555+ if ( ! html . getAttribute ( "aria-label" ) ) html . setAttribute ( "aria-label" , result . speakText ) ;
555556 }
556557 // remove automatically generated IDs
557558 var ids = html . querySelectorAll ( '[id^="MJXc-Node-"]' ) ;
@@ -587,17 +588,18 @@ function GetSVG(result) {
587588 //
588589 // Add the speech text and mark the SVG appropriately
589590 //
590- for ( var i = 0 , m = svg . childNodes . length ; i < m ; i ++ )
591- svg . childNodes [ i ] . setAttribute ( "aria-hidden" , true ) ;
592- // Note: if aria-label exists, getSpeech preserved it in speakText
593- // remove aria-label since labelled-by desc is preferred
594- svg . removeAttribute ( "aria-label" ) ;
595- ID ++ ; var id = "MathJax-SVG-" + ID ;
596- svg . setAttribute ( "aria-labelledby" , id + "-Desc" ) ;
597- var node = MathJax . HTML . Element ( "desc" , { id :id + "-Desc" } , [ result . speakText ] ) ;
591+ if ( data . speakText ) {
592+ for ( var i = 0 , m = svg . childNodes . length ; i < m ; i ++ )
593+ svg . childNodes [ i ] . setAttribute ( "aria-hidden" , true ) ;
594+ // Note: if aria-label exists, getSpeech preserved it in speakText
595+ // remove aria-label since labelled-by desc is preferred
596+ svg . removeAttribute ( "aria-label" ) ;
597+ }
598+ ID ++ ; var id = "MathJax-SVG-" + ID + "-Desc" ;
599+ svg . setAttribute ( "aria-labelledby" , id ) ;
600+ var node = MathJax . HTML . Element ( "desc" , { id :id } , [ result . speakText ] ) ;
598601 svg . insertBefore ( node , svg . firstChild ) ;
599602
600-
601603 if ( data . svg ) {
602604 //
603605 // SVG data is modified to add linebreaks for readability,
@@ -675,8 +677,8 @@ function StartQueue() {
675677 $$ ( SetRenderer , renderer ) ,
676678 $$ ( "Typeset" , HUB ) ,
677679 $$ ( TypesetDone , result ) ,
678- $$ ( GetMML , result ) ,
679680 $$ ( GetSpeech , result ) ,
681+ $$ ( GetMML , result ) ,
680682 $$ ( GetHTML , result ) ,
681683 $$ ( RerenderSVG , result ) ,
682684 $$ ( GetSVG , result ) ,
@@ -737,9 +739,6 @@ function ReturnResult(result) {
737739 if ( errors . length ) {
738740 result . errors = errors ;
739741 }
740- if ( ! data . mml ) {
741- delete result . mml ;
742- }
743742 var state = data . state ;
744743 if ( state ) {
745744 var AMS = MathJax . Extension [ "TeX/AMSmath" ] ;
0 commit comments