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

    textNodes = filterNode(origParent, isTextNode)

    # Calculate real offset as the combined length of all the
    # preceding textNode siblings. We include the length of the
    # node if it's the end node.
    nodes = textNodes.slice(0, textNodes.indexOf(node))
    offset = 0
    for n in nodes
      offset += n.nodeValue.length

    if isEnd then [path, offset + node.nodeValue.length] else [path, offset]

  start = serialization(range.startContainer)
  end   = serialization(range.endContainer, true)

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

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

    if not node?
