     range.collapse(true)
     range
 
+  # in webkit, when the caret is between a contenteditable=false div and a the
+  # end, it disappears or flashes to the end of the div. This gives the user a
+  # helpful hint that the caret is still where they think it is
+  maybeFixSquishedCaret: ->
+    @input.children().removeClass('squish-selected')
+    lastChild = @input.children().last()
+    if @input.text().substr(@input.text().length - lastChild.text().length) == lastChild.text() && @isCaretAtEnd()
+      lastChild.addClass('squish-selected')
+
+  isCaretAtEnd: ->
+    atEnd = false
+    if window.getSelection
+      sel = window.getSelection()
+      if sel.rangeCount
+        range = sel.getRangeAt(0)
+        testRange = range.cloneRange()
+        testRange.selectNodeContents @input[0]
+        testRange.setStart range.endContainer, range.endOffset
+        atEnd = testRange.toString() == ''
+    else if document.selection and document.selection.type != 'Control'
+      range = document.selection.createRange()
+      testRange.moveToElementText @input[0]
+      testRange.setEndPoint 'StartToEnd', range
+      atEnd = testRange.text == ''
+    return atEnd
+
   getWord: ->
     # Determine the length of text in the input.
     range = @getRange()

  registerEvents: ->
    @input.on "keyup", =>

      clearTimeout(@typingTimer) unless @typingTimer is null
      @typingTimer = setTimeout( =>
        # Store selected range (used to recover position when autocomplete menu is clicked).

    @input.on "keydown", (event) =>
      clearTimeout(@typingTimer) unless @typingTimer is null

      @maybeFixSquishedCaret()
      switch event.which
        when 13 # Enter
          @kill(event)
