The hickory.zip namespace provides zippers for both Hiccup and Hickory formats, allowing you to navigate, edit, and reconstruct trees. This is useful for complex transformations.
hiccup-zip: Creates a zipper for Hiccup vector trees.hickory-zip: Creates a zipper for Hickory DOM maps.
Once a zipper is created, you can use standard clojure.zip functions (like zip/next, zip/root, zip/replace) to navigate the tree and perform modifications. After modifications, use zip/root to retrieve the entire updated tree.
(use 'hickory.zip)
(require '[clojure.zip :as zip])
(require '[hickory.render :refer [hickory-to-html]])
;; Example: Navigating and replacing a node in a Hiccup tree
(-> (hiccup-zip (as-hiccup (parse "<a href=foo>bar<br></a>")))
zip/next zip/next
(zip/replace [:head {:id "a"}])
zip/root)
;; Example: Navigating and replacing a node in a Hickory tree, then rendering to HTML
(-> (hickory-zip (as-hickory (parse "<a href=foo>bar<br></a>")))
zip/next zip/next
(zip/replace {:type :element :tag :head :attrs {:id "a"} :content nil})
zip/root
hickory-to-html)
;; => "<html><head id=\"a\"></head>..."