     name = "IndexSizeError"
     throw new DOMException(message, name)
 
-  start = findBoundary(startPath, startOffset)
-  end = findBoundary(endPath, endOffset)
+  start = findBoundary(startPath, startOffset, false)
+  end = findBoundary(endPath, endOffset, true)
 
   range.setStart(start.container, start.offset)
   range.setEnd(end.container, end.offset)

  document = root.ownerDocument
  range = document.createRange()

  findBoundary = (path, offset) ->
    node = xpath.toNode(path, root)

    if not node?

    # process each TextNode until their combined length exceeds or matches the
    # value of the offset.
    for tn in filterNode(node, isTextNode)
      if (isEnd and tn.length == offset) or (offset < tn.length)
        return {container: tn, offset: offset}
      else
        offset -= tn.length
