mathjax / MathJax

Beautiful and accessible math in all browsers
http://www.mathjax.org/
Apache License 2.0
10.21k stars 1.16k forks source link

HTML-CSS output: if the page already contains an element with id "MathJax-Element-N-Frame", predictably bad things result #1633

Open christianp opened 8 years ago

christianp commented 8 years ago

An author of some text didn't know how to get a π symbol, so he got MathJax to render it, then copied and pasted the output back into the WYSIWYG editor. What he didn't notice (and it can reasonably be argued he shouldn't have to worry about this) was that he had pasted in the entire HTML element produced by MathJax, which had id attribute MathJax-Element-3-Frame. To compound his bad luck, he pasted all this before the third occurence of LaTeX in the page.

When MathJax processes a bit of LaTeX, it inserts an element with an ID of the form MathJax-Element-N-Frame in place, to store the rendered output. Later on, when it comes to filling that element up, it uses document.getElementById to retrieve it. In this case, that returns the pasted element instead of the one MathJax just created.

I've made a minimal example at http://codepen.io/christianp/pen/VKymrd. Here's how it looks on Chrome: image (The person who reported this was running Firefox)

Not only has the third bit of maths gone in the wrong place, the metrics seem to be messed up for every other bit of maths on the page, even stuff that appeared before our pasted element.

The obvious solution is not to have such badly-named elements in your page to start with, but I think MathJax could be more careful here.

I can think of two solutions

pkra commented 8 years ago

Marking this a feature request.

dpvc commented 8 years ago

Version 2.7 already handles this better, and the HTML-CSS and SVG output will work properly in your example with that (change cdn.mathjax.org to beta.mathjax.org and see). The CommonHTML will drop the third expression (it probably could be made to do better like the other two). In any case, I don't think there is anything more that needs to be done once v2.7 is out.

rather than using document.getElementById(jax.inputID+'-Frame'), keep a reference to the element itself. I assume there's a good reason we don't do this.

The HTML-CSS output no longer uses getElementById() for this. Yes, there were reasons for this originally, but they have to do with older browsers, and so are no longer so pertinent.

pkra commented 8 years ago

@dpvc change of label then?