   # Scroll the top, bottom, left and right of element into view.  The is used by visual mode to ensure the
   # focus remains visible.
   scrollIntoView: (element) ->
-    activatedElement ||= document.body and firstScrollableElement()
+    activatedElement ||= document.documentElement and firstScrollableElement()
     rect = element. getClientRects()?[0]
     if rect?
       # Scroll y axis.


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

# On some pages, document.body 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.body) ->
  if doesScroll(element, "y", 1, 1) or doesScroll(element, "y", -1, 1)
    element
  else

  # 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.documentElement and firstScrollableElement()

    # Scroll down, "y".
    amount = top + 20 - (element.clientHeight + element.scrollTop)
