Array of elements passed to Typeset is ignored if no callback is given
Created by: jdleesmiller
The documentation for Hub.Typeset
suggests that you can pass an array of elements or a callback or both:
However, the argument handling in the elementCallback
helper method has some (IMHO) odd behaviour when element
is an array and callback
is not specified. The code in question is:
if (callback == null && (MathJax.Object.isArray(element) || typeof element === 'function'))
{try {MathJax.Callback(element); callback = element; element = null} catch(e) {}}
The result is that callback
ends up being set to the first element in the array, and element
is set to null
. The element
then defaults to the document's body
later on, which in most cases masks the bug (unless the intended elements aren't yet in thebody
, in which case much confusion ensues). However, if you use signal.Interest
to track the events, you can see that the element is not actually tracked.
It's also worth noting that this affects other hub functions, such as Update
.
Example
Run this in the dev console on the home page:
MathJax.Hub.signal.Interest(function (message) {console.log(message)});
MathJax.Hub.Queue(['Typeset', MathJax.Hub, $('.hero-math').toArray()]);
MathJax.Hub.Queue(['Typeset', MathJax.Hub, $('.hero-math').toArray(), function callbackHere() {}])
When the callback is not specified, the signal messages indicate that MathJax is searching the body
. When the callback is specified, the signal messages indicate that MathJax is searching the div.hero-math
element, which is what was intended.