mathjax / MathJax-node

MathJax for Node
Apache License 2.0
615 stars 97 forks source link

[speech] alttext on MathML input leads to duplication in output #236

Closed pkra closed 8 years ago

pkra commented 8 years ago

[Splitting this from https://github.com/mathjax/MathJax-node/issues/121#issuecomment-190666132]

If MathML input has alttext and mathjax-node is set to generate output with speechtext, then we get a duplication.

For MathML, the source alttext is changed to the speechtext but the attribute is duplicated in the output. This is due to a check missing at https://github.com/mathjax/MathJax-node/blob/master/lib/mj-single.js#L544

For HTML, the speechtext is added as an aria-label on the outer span; the source alttext is added to its first child (but that's in an aria-hidden element). This is due to https://github.com/mathjax/MathJax-node/blob/develop/lib/mj-single.js#L570-L574

For SVG output, the source alttext ends up on an aria-label (because of https://github.com/mathjax/MathJax/blob/develop/unpacked/jax/output/SVG/jax.js#L2111) and the speechtext ends up in a title tag pointed to by an aria-labeledby (due to https://github.com/mathjax/MathJax-node/blob/master/lib/mj-single.js#L575-L585).

Examples;

MML

$ node bin/mml2mml --speech '<math alttext="hello world"> <msup> <mi>&#x03B1;<!-- α --></mi> 

produces

<mn>2</mn> </msup> </math>'
<math xmlns="http://www.w3.org/1998/Math/MathML" alttext="alpha squared" alttext="alpha squared">
  <msup>
    <mi>&#x3B1;</mi>
    <mn>2</mn>
  </msup>
</math>

HTML

$ node bin/mml2html --speech '<math alttext="hello world"> <msup> <mi>&#x03B1;<!-- α --></mi> <mn>2</mn> </msup> </math>'

produces

<span class="mjx-chtml" aria-label="alpha squared"><span class="mjx-math" aria-label="hello world" role="math" aria-hidden="true"><span class="mjx-mrow"><span class="mjx-msup"><span class="mjx-base"><span class="mjx-mi"><span class="mjx-char MJXc-TeX-math-I" style="padding-top: 0.225em; padding-bottom: 0.298em;">α</span></span></span><span class="mjx-sup" style="font-size: 70.7%; vertical-align: 0.513em; padding-left: 0px; padding-right: 0.071em;"><span class="mjx-mn" style=""><span class="mjx-char MJXc-TeX-main-R" style="padding-top: 0.372em; padding-bottom: 0.372em;">2</span></span></span></span></span></span></span>

SVG

$ node bin/mml2svg --speech '<math alttext="hello world"> <msup> <mi>&#x03B1;<!-- α --></mi><mn>2</mn> </msup> </math>'

will produce

<svg xmlns:xlink="http://www.w3.org/1999/xlink" width="2.542ex" height="2.509ex" style="vertical-align: -0.338ex;" viewBox="0 -934.9 1094.4 1080.4" aria-label="hello world" role="math" focusable="false" xmlns="http://www.w3.org/2000/svg" aria-labelledby="MathJax-SVG-1-Title MathJax-SVG-1-Desc">
<title id="MathJax-SVG-1-Title">Equation</title>
<desc id="MathJax-SVG-1-Desc">alpha squared</desc>
<defs aria-hidden="true">
<path stroke-width="1" id="E1-MJMATHI-3B1" d="M34 156Q34 270 120 356T309 442Q379 442 421 402T478 304Q484 275 485 237V208Q534 282 560 374Q564 388 566 390T582 393Q603 393 603 385Q603 376 594 346T558 261T497 161L486 147L487 123Q489 67 495 47T514 26Q528 28 540 37T557 60Q559 67 562 68T577 70Q597 70 597 62Q597 56 591 43Q579 19 556 5T512 -10H505Q438 -10 414 62L411 69L400 61Q390 53 370 41T325 18T267 -2T203 -11Q124 -11 79 39T34 156ZM208 26Q257 26 306 47T379 90L403 112Q401 255 396 290Q382 405 304 405Q235 405 183 332Q156 292 139 224T121 120Q121 71 146 49T208 26Z"></path>
<path stroke-width="1" id="E1-MJMAIN-32" d="M109 429Q82 429 66 447T50 491Q50 562 103 614T235 666Q326 666 387 610T449 465Q449 422 429 383T381 315T301 241Q265 210 201 149L142 93L218 92Q375 92 385 97Q392 99 409 186V189H449V186Q448 183 436 95T421 3V0H50V19V31Q50 38 56 46T86 81Q115 113 136 137Q145 147 170 174T204 211T233 244T261 278T284 308T305 340T320 369T333 401T340 431T343 464Q343 527 309 573T212 619Q179 619 154 602T119 569T109 550Q109 549 114 549Q132 549 151 535T170 489Q170 464 154 447T109 429Z"></path>
</defs>
<g stroke="currentColor" fill="currentColor" stroke-width="0" transform="matrix(1 0 0 -1 0 0)" aria-hidden="true">
 <use xlink:href="#E1-MJMATHI-3B1" x="0" y="0"></use>
 <use transform="scale(0.707)" xlink:href="#E1-MJMAIN-32" x="905" y="513"></use>
</g>
</svg>
pkra commented 8 years ago

I'm wondering if this isn't mostly of an upstream issue.

The check for pushing to the alttext attribute is obviously something to fix.

But once that's done, should MathJax take care of the correct markup? I.e., use title and labeled-by for SVG and for CommonHTML, set aria-hidden to the first child (that's technically not necessary but worthwhile to be sure AT won't do anything funky).

pkra commented 8 years ago

But once that's done, should MathJax take care of the correct markup?

I realized that this wouldn't help since the timing isn't right. The changes to root.alttext won't affect SVG and HTML output. Not sure if we want to change that. I'll fix this locally in GetSVG and GetHTML.

dpvc commented 8 years ago

I suppose there is also the question of which should take precedence, the alttext from the math node, or the speech text. If you think of adding speech as the default action, an element with alttext could be considered to be an element with data that overrides the default.

If the application wants to replace existing alttext attributes with speech text, then it should make sure that alttext attributes are removed before passing the math to MathJax for processing. That seems to be more in line with the current approach of pushing more responsibility onto the calling program.

pkra commented 8 years ago

Fair point. I don't really mind either way around. There's also #206 which makes it somewhat moot if we go ahead with that.

@zorokow any thoughts?

dpvc commented 8 years ago

I'm not sure I understand your point about 206. The changes you made here are for mj-single, but 206 is about mj-page, right?

pkra commented 8 years ago

Sorry, typo. I mean #207.

dpvc commented 8 years ago

OK, that makes more sense. Thanks.

dpvc commented 8 years ago

==> Merged.