            (.getValue))
        (catch Exception e nil)))
 
+(defn child-position
+  [parent a-w3c-node]
+  (let [child-node-list (.getChildNodes parent)
+        child-nodes-cnt (.getLength child-node-list)]
+    (.indexOf
+     (map
+      #(.isSameNode
+        a-w3c-node
+        (.item child-node-list %))
+      (range child-nodes-cnt))
+     true)))
+
+(defn is-first-child?
+  [a-w3c-node]
+  (let [parent (.getParentNode a-w3c-node)]
+    (and parent
+         (= 0 (child-position
+               parent a-w3c-node)))))
+
+(defn is-last-child?
+  [a-w3c-node]
+  (let [parent (.getParentNode a-w3c-node)]
+    (and parent
+         (=
+          (-
+           (.getLength
+            (.getChildNodes parent))
+           1)
+          (child-position
+           parent a-w3c-node)))))
+
 (defn tag-id-class-node
   "Returns a list containing a tag's name, its id
 - formatted slightly - and its classes - formatted slightly"

        silent-fail-attr  (fn [attr key] (try (.getValue
                                              (.getNamedItem attr key))
                                             (catch Exception e nil)))
        
        attributes        (.getAttributes a-w3c-node)]

    (list (.getNodeName a-w3c-node)
          (-> attributes
              (silent-fail-attr "id")
              format-attr)
          (map
           format-attr
           (-> attributes
               (silent-fail-attr "class")
               (silent-fail-split #"\s+"))))))

(defn tag-id-class->xpath
  "Args:
    a-tag-id-class : a tag, its id and a list of classes
   Returns:
    a component that fits in an xpath"
  [[tag id class-list]]
  (let [formatted-id      (format "contains(@id,'%s')" id)
        formatted-classes (map
                           #(format "contains(@class,'%s')" %)

                                              (.getNamedItem "href")
                                              (.getValue))]
                                 {:node x
                                  :href (try
                                          (uri/fragment
                                           (uri/resolve-uri url link)
                                           nil)
                                          (catch Exception e nil))
                                  :text (.getTextContent x)}))
                             a-tags-w-hrefs)))]
       
       (reduce
        (fn [acc [an-xpath node]]
          (merge-with
           concat acc {an-xpath [node]})) {} nodes-xpaths))))
