 # Return an Array of the result of applying a function to each Node in a tree.
 # An optional third argument specifies a traversal and should be a function
 # that returns the successor of its argument. The default is document order.
-mapNode = (node, fn, next = preFirst) ->
+mapNode = (node, fn, next = documentForward) ->
   return reduceNode(node, ((acc, n) -> acc.push(fn(n))), [], next)
 
 
-reduceNode = (node, fn, initialValue = undefined, next = preFirst) ->
+reduceNode = (node, fn, initialValue = undefined, next = documentForward) ->
   acc = initialValue
   last = lastLeaf(node)
   if arguments.length is 2
     if node is last then return node
     acc = node
-    node = preFirst(node)
+    node = documentForward(node)
   else
     acc = fn(acc, node)
 
-  while node = preFirst(node)
+  while node = documentForward(node)
     acc = fn(acc, node)
     if node is last then break
 

# Return true if the given predicate is true for any Node of the tree.
# An optional third argument specifies a traversal and should be a function
# that returns the successor of its argument. The default is document order.
someNode = (node, fn, next = preFirst) ->
  return !!findNode(node, fn, next)


# Return an Array of each Node in the tree for which the predicate is true.
# An optional third argument specifies a traversal and should be a function
# that returns the successor of its argument. The default is document order.
filterNode = (node, fn, next = preFirst) ->
  collect = (acc, n) -> if fn(n) then acc.concat([n]) else acc
  return reduceNode(node, collect, [], next)



    if not contains(bounds, @start)
      node = bounds
      next = (node) -> node isnt last and documentForward(node) or null
      last = lastLeaf(bounds)
      node = next(node) while not isTextNode(node)
      @start = node

    if not contains(bounds, @end)
      node = bounds
      next = (node) -> node isnt last and documentReverse(node) or null
      last = firstLeaf(bounds)
      node = next(node) while not isTextNode(node)
      @end = node
