     # Move the range to the start of the element
     range.collapse true
     # Move the positions to the place specified in characters
-    range.moveEnd 'character', lineHandler(elem, end)
-    range.moveStart 'character', lineHandler(elem, start)
+    range.moveEnd 'character', end - lineHandler(elem, end)
+    range.moveStart 'character', start - lineHandler(elem, start)
     # Select the range so that it is visible
     range.select()
 

    else
      # moveStart and moveEnd returns how many characters it moved
      start = - elemRange.moveStart('character', - elem.value.length)
      # Check if the caret ends at the end of the text
      if elemRange.compareEndPoints('EndToEnd', endRange) >= 0
        end = elem.value.length
      else
        # Finally we calculate the end position
        end = - elemRange.moveEnd('character', - elem.value.length)
    # Return the selection as an object
    start: start,
    end: end

# selix - github.com/bilde - MIT License

lineHandler = (elem, position) ->
  elem.value.slice(0, position).split('\r\n').length - 1

getCaret = (elem) ->
  if typeof elem.selectionStart is 'number' and typeof elem.selectionEnd is 'number'
