 #
 # Returns String
 fromNode = (node, root = document) ->
-  path = ''
-  while node != root
+  path = '/'
+  while node isnt root
     unless node?
       throw new Error("Given node is not a descendant of the root node.")
-    path = '/' + nodeStep(node) + path
+    path = "/#{nodeName(node)}[#{nodePosition(node)}]#{path}"
     node = node.parentNode
-  return path
+  return path.replace(/\/$/, '')
 
-findChild = (node, type, index) ->
-  unless node.hasChildNodes()
-    throw new Error("XPath error: node has no children!")
-  children = node.childNodes
-  found = 0
-  for child in children
-    name = name(child)
-    if name is type
-      found += 1
-      if found is index
-        return child
-  throw new Error("XPath error: wanted child not found.")
-
-# Get the XPath node name.
-nodeName = (node) ->
-  name = node.nodeName.toLowerCase()
-  switch name
-    when "#text" then return "text()"
-    when "#comment" then return "comment()"
-    when "#cdata-section" then return "cdata-section()"
-    else return name
-
-# Get the position of this node among its siblings of the same name.
-nodePosition = (node) ->
-  position = 1
-  name = node.nodeName
-  while node = node.previousSibling
-    if node.nodeName is name
-      position += 1
-  return position
-
-# Make an XPath location step for the node by name and position.
-nodeStep = (node) ->
-  return "#{nodeName(node)}[#{nodePosition(node)}]"
 
 # Public: Finds an Element Node using an XPath relative to the document root.
 #

#     # Do something with the node.
#
# Returns the Node if found otherwise null.
toNode = (path, root = document, resolver = null) ->
  if document.lookupNamespaceURI(null)? and not resolver?
    documentElement = document.documentElement

    if document.ownerDocument?
      documentElement = document.ownerDocument.documentElement

    resolver = document.createNSResolver(documentElement)

  return evaluateXPath(path, root, resolver)

module.exports =
  fromNode: fromNode

FIRST_ORDERED_NODE_TYPE = 9
HTML_NAMESPACE = 'http://www.w3.org/1999/xhtml'

# Public: Compute an XPath expression for the given node.
#
