   # Scroll element to position top, left.  This is used by edit mode to ensure that the caret remains visible
   # in text inputs (not contentEditable).
   scrollToPosition: (element, top, left) ->
-    activatedElement ||= document.body and firstScrollableElement()
+    activatedElement ||= document.documentElement and firstScrollableElement()
 
     # Scroll down, "y".
     amount = top + 20 - (element.clientHeight + element.scrollTop)

  scrollBy: (element, direction, amount, continuous = false) ->
    factor = 1
    # if this is called before domReady, just use the window scroll function
    if (!document.body and amount instanceof Number)
      if (direction == "x")
        window.scrollBy(amount, 0)
      else
        window.scrollBy(0, amount)
      return

    activatedElement = element ? document.body
    return unless activatedElement

    # Avoid the expensive scroll calculation if it will not be used.  This reduces costs during smooth,


# From element and its parents, find the first which we should scroll and which does scroll.
findScrollableElement = (element, direction, amount, factor) ->
  while element != document.documentElement and
    not (doesScroll(element, direction, amount, factor) and shouldScroll(element, direction))
      element = element.parentElement || document.documentElement
  element

# On some pages, document.documentElement is not scrollable.  Here, we search the document for the largest visible
# element which does scroll vertically. This is used to initialize activatedElement. See #1358.
firstScrollableElement = (element=document.documentElement) ->
  if doesScroll(element, "y", 1, 1) or doesScroll(element, "y", -1, 1)
    element
  else
