Skip to content

Commit

Permalink
Merge pull request #355 from mathjax/a11y_aria_labels_computation
Browse files Browse the repository at this point in the history
A11y aria labels computation
  • Loading branch information
zorkow committed Sep 29, 2019
2 parents 4ce214f + fbd046e commit f1d2abb
Showing 1 changed file with 24 additions and 3 deletions.
27 changes: 24 additions & 3 deletions ts/a11y/semantic-enrich.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,11 @@ export function EnrichedMathItemMixin<N, T, D, B extends Constructor<AbstractMat
/**
* @param {MathDocument} document The MathDocument for the MathItem
*/
attachSpeech(document: MathDocument<N, T, D>) {
public attachSpeech(document: MathDocument<N, T, D>) {
if (this.state() >= STATE.ATTACHSPEECH) return;
const attributes =this.root.attributes;
const speech = (attributes.get('aria-label') || attributes.get('data-semantic-speech')) as string;
const attributes = this.root.attributes;
const speech = (attributes.get('aria-label') ||
this.getSpeech(this.root)) as string;
if (speech) {
const adaptor = document.adaptor;
const node = this.typesetRoot;
Expand All @@ -146,6 +147,26 @@ export function EnrichedMathItemMixin<N, T, D, B extends Constructor<AbstractMat
this.state(STATE.ATTACHSPEECH);
}

/**
* Retrieves the actual speech element that should be used as aria label.
* @param {MmlNode} node The root node to search from.
* @return {string} The speech content.
*/
private getSpeech(node: MmlNode): string {
const attributes = node.attributes;
if (!attributes) return;
const speech = attributes.getExplicit('data-semantic-speech') as string;
if (!attributes.getExplicit('data-semantic-parent') && speech) {
return speech;
}
for (let child of node.childNodes) {
let value = this.getSpeech(child as MmlNode);
if (value != null) {
return value;
}
}
}

};

}
Expand Down

0 comments on commit f1d2abb

Please sign in to comment.