   getDOMRange = ->
     getDOMSelection()?.getRangeAt(0)
 
+  setDOMRange = (domRange) ->
+    selection = window.getSelection()
+    selection.removeAllRanges()
+    selection.addRange(domRange)
+
   getClientRects = ->
     rects = getDOMRange()?.getClientRects()
     rects if rects?.length

      @currentLocationRange = locationRange
      @delegate?.locationRangeDidChange?(@currentLocationRange)

  setDOMRange: benchmark "SelectionManager#setDOMRange", (locationRange) ->
    rangeStart = @findContainerAndOffsetForLocation(locationRange.start)
    rangeEnd =
      if locationRange.isCollapsed()
        rangeStart
      else
        @findContainerAndOffsetForLocation(locationRange.end)

    return unless rangeStart? and rangeEnd?

    range = document.createRange()
    range.setStart(rangeStart...)
    range.setEnd(rangeEnd...)

    applyRange = benchmark "  #setDOMRange -> applyRange", =>
      if document.activeElement is @element
        selection = window.getSelection()
        selection.removeAllRanges()
        selection.addRange(range)

    # Selection#addRange is unreasonably slow in WebKit when performed in the
    # same call stack as a mouse or key event so defer calling it.
    # https://code.google.com/p/chromium/issues/detail?id=423170
    # https://code.google.com/p/chromium/issues/detail?id=138439
    defer(applyRange)

    locationRange

  createLocationRangeFromDOMRange: (range) ->
    return unless range? and @rangeWithinElement(range)

    else
      new Trix.LocationRange start, end

    if domRange = @createDOMRangeFromLocationRange(locationRange)
      # Selection#addRange is unreasonably slow in WebKit when performed in the
      # same call stack as a mouse or key event so defer calling it.
      # https://code.google.com/p/chromium/issues/detail?id=423170
      # https://code.google.com/p/chromium/issues/detail?id=138439
      defer => setDOMRange(domRange) if document.activeElement is @element
      @updateCurrentLocationRange(locationRange)

  setLocationRangeFromPoint: (point) ->
    if locationRange = @getLocationRangeAtPoint(point)
