11require ( 'mathjax-full/js/util/asyncLoad/node.js' ) ;
2- // This is loaded to import `STATE.ENRICHED` is set.
2+ // This is loaded to ensure `STATE.ENRICHED` is set.
33require ( 'mathjax-full/js/a11y/semantic-enrich.js' ) ;
4- const { STATE } = require ( 'mathjax-full/js/core/MathItem.js' ) ;
4+ const { newState, STATE } = require ( 'mathjax-full/js/core/MathItem.js' ) ;
5+
56
67//
78// Remove the data-semantic-* attributes other than data-semantic-speech
@@ -21,16 +22,16 @@ function removeSemanticData(math) {
2122
2223//
2324// Moves all speech elements into aria-labels for SVG output. This allows for
24- // elements without containers some limited exploration.
25+ // elements without containers to have some limited exploration.
2526//
26- // Note, that here we cannot walk the source tree, as we alterations are to be
27+ // Note, that here we cannot walk the source tree, as alterations are to be
2728// done on the typeset element, if it is a SVG node.
2829//
2930function speechToRoles ( math ) {
3031 const root = math . typesetRoot ;
3132 const adaptor = math . adaptor ;
32- const svg = adaptor . childNodes ( root ) [ 0 ] ;
33- if ( ! svg || adaptor . kind ( svg ) !== 'svg' ) return ;
33+ const svg = adaptor . tags ( root , 'svg' ) [ 0 ] ;
34+ if ( ! svg ) return ;
3435 adaptor . removeAttribute ( svg , 'aria-hidden' ) ;
3536 adaptor . removeAttribute ( svg , 'role' ) ;
3637 const children = [ svg ] ;
@@ -85,16 +86,37 @@ exports.sreconfig = function(data) {
8586 Object . assign ( MathJax . config . options . sre , config ) ;
8687} ;
8788
89+ //
90+ // Let's define some new states for enlisting new renderActions into the queue.
91+ //
92+ // STATE.ENRICHED is the priority of the enrichment. Enrichment happens before
93+ // elements are rendered (i.e., typeset) and therefore all attributes added during
94+ // enrichment will be part of the MathItem.
95+ //
96+ // STATE 1000 is so hight that any other render action should be done by
97+ // then. That is, the MathItem is fully typeset in the document.
98+ //
99+ newState ( 'SIMPLIFY' , STATE . ENRICHED + 1 ) ;
100+ newState ( 'ROLE' , 1000 ) ;
101+ newState ( 'DESCRIBE' , STATE . ROLE + 1 ) ;
88102
89103//
90- // The renderActions needed to remove the data-semantic-attributes .
91- // STATE.ENRICHED is the priority of the enrichment, so this will run just after enrichment.
104+ // The renderActions needed for manipulating MathItems with speech entries .
105+ // We define three render actions, each with two functions:
92106// The first function is the one for when the document's render() method is called.
93107// The second is for when a MathItem's render(), rerender() or convert() method is called.
94108//
109+ // simplify: Removes the data-semantic-attributes except speech directly after enrichment.
110+ //
111+ // role: Adds an aria role to the container element so aria-labels are spoken on custom elements.
112+ // This happens after typesetting.
113+ //
114+ // describe: Rewrites speech attributes into aria-labels with img roles in SVGs.
115+ // This happens after container elements are rewritten.
116+ //
95117exports . speechAction = {
96118 simplify : [
97- STATE . ENRICHED + 1 ,
119+ STATE . SIMPLIFY ,
98120 ( doc ) => {
99121 for ( const math of doc . math ) {
100122 removeSemanticData ( math ) ;
@@ -105,7 +127,7 @@ exports.speechAction = {
105127 }
106128 ] ,
107129 role : [
108- 1000 ,
130+ STATE . ROLE ,
109131 ( doc ) => {
110132 for ( const math of doc . math ) {
111133 roleImg ( math ) ;
@@ -116,7 +138,7 @@ exports.speechAction = {
116138 }
117139 ] ,
118140 describe : [
119- 1001 ,
141+ STATE . DESCRIBE ,
120142 ( doc ) => {
121143 for ( const math of doc . math ) {
122144 speechToRoles ( math ) ;
0 commit comments