 # https://developer.mozilla.org/en-US/docs/Web/API/Node/nodeType
 ELEMENT_NODE = 1
 TEXT_NODE = 3
+
+
+insertAfter = (node, referenceNode) ->
+  parent = referenceNode.parentNode
+  next = referenceNode.nextSibling
+  if next then return parent.insertBefore(node, next)
+  return parent.appendChild(node)
+
+
+# https://github.com/Raynos/DOM-shim/issues/11
+splitText = (node, offset) ->
+  tail = node.cloneNode(false)
+  tail.deleteData(0, offset)
+  node.deleteData(offset, node.length - offset)
+  return insertAfter(tail, node)
 
 
 # Public: Creates a wrapper around a range object obtained from a DOMSelection.


          @startOffset = 0
        else
          @startContainer = @startContainer.splitText(@startOffset)
          @startOffset = 0

  # Public: Normalize the start and end to TextNode boundaries.

      if 0 < @endOffset < @endContainer.length
        if @startContainer is @endContainer
          @commonAncestorContainer = @endContainer.parentNode
          @endContainer = splitText(@endContainer, @endOffset)

          if @endOffset <= @startOffset
            @startContainer = @endContainer
