     latexNodes.push(node)
 
   unless _.isEmpty(latexNodes)
-    window.MathJax.Hub.Typeset(latexNodes)
+    windowImpl.MathJax.Hub.Typeset(latexNodes)
 
-  mathMLNodes = _.toArray(document.querySelectorAll(MATH_ML_SELECTOR))
+  mathMLNodes = _.toArray(windowImpl.document.querySelectorAll(MATH_ML_SELECTOR))
   unless _.isEmpty(mathMLNodes)
     # style the entire document because mathjax is unable to style individual math elements
-    window.MathJax.Hub.Typeset( window.document )
+    windowImpl.MathJax.Hub.Typeset( windowImpl.document )
 
-  window.MathJax.Hub.Queue ->
+  windowImpl.MathJax.Hub.Queue ->
     # Queue a call to mark the found nodes as rendered so are ignored if typesetting is called repeatedly
     for node in latexNodes.concat(mathMLNodes)
       node.classList.add(MATH_RENDERED_CLASS)

COMBINED_MATH_SELECTOR = "#{MATH_DATA_SELECTOR}, #{MATH_ML_SELECTOR}"

# Search document for math and [data-math] elements and then typeset them
typesetDocument = ->
  latexNodes = []

  for node in document.querySelectorAll(MATH_DATA_SELECTOR)
    formula = node.getAttribute('data-math')
    # divs should be rendered as a block, others inline
    if node.tagName.toLowerCase() is 'div'

# every Xms even if called multiple times in that period
typesetDocument = _.debounce( typesetDocument, 100)

# typesetMath is the main exported function.
# It's called by components like HTML after they're rendered
typesetMath = (root, windowImpl = window) ->
  # schedule a Mathjax pass if there is at least one [data-math] or <math> element present
  if windowImpl.MathJax?.Hub?.Queue? and root.querySelector(COMBINED_MATH_SELECTOR)
    typesetDocument(windowImpl)


# The following should be called once and configures MathJax.
