   formattedText = formattedText.replace(/&nbsp;/g, ' ')           # &nbsp; -> ' '
   return formattedText
 
-saveCursorLocation = () ->
-
-restoreCursorLocation = () ->
+cursorLocationFromNodeAndOffset = (rootNode, anchorNode, anchorOffset) ->
+  location = 0
+  if rootNode == anchorNode
+    if rootNode.nodeType == Node.TEXT_NODE
+      location += anchorOffset
+    else if rootNode.tagName == "SPAN"
+      for i in [0..anchorOffset] by 1
+        location += cursorLocationFromNodeAndOffset(rootNode.childNodes[i], anchorNode, anchorOffset)
+  else
+    if rootNode.nodeType == Node.TEXT_NODE
+      location += rootNode.nodeValue.length
+    else if rootNode.tagName == "BR"
+      location += 1
+    else
+      for childNode in rootNode.childNodes
+        if childNode == anchorNode
+          location += cursorLocationFromNodeAndOffset(childNode, anchorNode, anchorOffset)
+          return location
+        else
+          location += cursorLocationFromNodeAndOffset(childNode, anchorNode, anchorOffset)
+  return location
+
+nodeAndOffsetFromCursorLocation = (cursorLocation, rootNode) ->
+  if rootNode.nodeType == Node.TEXT_NODE
+    if rootNode.nodeValue.length >= cursorLocation
+      return [rootNode, cursorLocation]
+    else
+      return [null, rootNode.nodeValue.length]
+  else if rootNode.tagName == "BR"
+    if cursorLocation == 1
+      return [rootNode, 0]
+    return [null, 1]
+#  else if rootNode.tagName == "SPAN"
+  else
+    toMinus = 0
+    for childNode in rootNode.childNodes
+      [retNod, retOff] = nodeAndOffsetFromCursorLocation(cursorLocation, childNode)
+      if retOff and !retNod
+        cursorLocation -= retOff
+        toMinus += retOff
+      else if retOff and retNod
+        return [retNod, retOff]
+  [null, toMinus]
+
+saveCursorLocation = (jObject) ->
+  plainObject = jObject[0]
+  return if document.activeElement != plainObject
+  sel = window.getSelection()
+  return unless sel.isCollapsed
+  anchorNode = sel.anchorNode
+  anchorOffset = sel.anchorOffset
+  return unless plainObject.contains(anchorNode)
+  cursorLo = cursorLocationFromNodeAndOffset(plainObject, anchorNode, anchorOffset)
+  console.log("CursorLo: #{cursorLo}")
+  jObject.data("cursorlocation", cursorLo)
+
+
+restoreCursorLocation = (jObject) ->
+  plainObject = jObject[0]
+  return if document.activeElement != plainObject
+  sel = window.getSelection()
+  return unless sel.isCollapsed
+  # return unless plainObject.contains(sel.anchorNode)
+  [anchorNode, anchorOffset] = nodeAndOffsetFromCursorLocation(jObject.data("cursorlocation"), plainObject)
+  console.log(anchorNode)
+  console.log(anchorOffset)
+  if anchorNode && anchorOffset >= 0
+    sel.collapse(anchorNode, anchorOffset)
 
 $.fn.colorfy = (plainTextProcessor) ->
 

    area.val(div.data("content"))

  div.on "keyup paste", ->
    div.data("content", formattedTextToDataText(div.html())).trigger("send-content").trigger("receive-content")



    else
      div.css("display", "inline-block")
    div.html(dataTextToFormattedText(div.data("content")))
    restoreCursorLocation(div)
    return

  div.on "send-content", ->
    # Fix big cursor issue
    if div.text().length == 0
