       if node is endContainer and endOffset == 0 then return false
       return true
 
-    # Find the start and end TextNode nodes.
-    start = findNode(startContainer, last, checkNode, preFirst)
-    end = findNode(endContainer, first, checkNode, preLast)
+    # Find the start and end TextNode.
+    checkStart = (node) ->
+      if checkNode(node) then start = node
+      return not (start or node is last)
+
+    checkEnd = (node) ->
+      if checkNode(node) then end = node
+      return not (end or node is start)
+
+    someNode(startContainer, checkStart)
+    someNode(endContainer, checkEnd, preLast)
 
     return new NormalizedRange({commonAncestor, start, end})
 

        origParent = node.parentNode

      path = xpath.fromNode(origParent, root)
      textNodes = getNodes(origParent, isTextNode)

      # Calculate real offset as the combined length of all the
      # preceding textNode siblings. We include the length of the

    if fn(node) then result.push(node)
    return node isnt last

  someNode(root, collect)

  return result

