Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

XML support #22

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion src/hickory/core.cljs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
(ns hickory.core
(:require [hickory.utils :as utils]
[clojure.zip :as zip]
[goog.string :as gstring]))
[goog.string :as gstring]
[goog.dom.xml :as gxml]))

;;
;; Protocols
Expand Down Expand Up @@ -94,6 +95,8 @@
(map as-hiccup (aget this "childNodes"))))))
Text (utils/html-escape (aget this "wholeText")))))



(extend-protocol HickoryRepresentable
object
(as-hickory [this] (condp = (aget this "nodeType")
Expand All @@ -116,6 +119,26 @@
(aget this "childNodes"))))}
Text (aget this "wholeText"))))

(defn as-hickory-xml [this] (condp = (aget this "nodeType")
Attribute [(keyword (aget this "name")) (aget this "value")]
Comment {:type :comment
:content [(aget this "data")]}
Document {:type :document
:content (not-empty
(into [] (map as-hickory-xml
(aget this "childNodes"))))}
DocumentType {:type :document-type
:attrs {:name (aget this "name")
:publicid (aget this "publicId")
:systemid (aget this "systemId")}}
Element {:type :element
:attrs (not-empty (into {} (map as-hickory-xml (aget this "attributes"))))
:tag (keyword (aget this "tagName"))
:content (not-empty
(into [] (map as-hickory-xml
(aget this "childNodes"))))}
Text (aget this "wholeText")))

(defn extract-doctype
[s]
;;Starting HTML5 doctype definition can be uppercase
Expand Down Expand Up @@ -158,3 +181,7 @@
each be passed as input to as-hiccup or as-hickory."
[s]
(aget (parse s) "body" "childNodes"))

(defn parse-xml
[s]
(gxml/loadXml s))
27 changes: 27 additions & 0 deletions src/hickory/render.cljx
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,33 @@
(ex-info (str "Not a valid node: " (pr-str dom)) {:dom dom})
e))))))

(defn hickory-to-xml
[dom]
(if (string? dom)
(utils/html-escape dom)
(try
(case (:type dom)
:document
(str "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
(apply str (map hickory-to-xml (:content dom))))
:element
(if-not (empty? (:content dom))
(str "<" (name (:tag dom))
(apply str (map render-hickory-attribute (:attrs dom)))
">"
(apply str (map hickory-to-xml (:content dom)))
"</" (name (:tag dom)) ">")
(str "<" (name (:tag dom))
(apply str (map render-hickory-attribute (:attrs dom)))
"/>"))
:comment
(str "<!--" (apply str (:content dom)) "-->"))
(catch #+clj IllegalArgumentException #+cljs js/Error e
(throw
(if (utils/starts-with #+clj (.getMessage e) #+cljs (aget e "message") "No matching clause: ")
(ex-info (str "Not a valid node: " (pr-str dom)) {:dom dom})
e))))))

;;
;; Hiccup to HTML
;;
Expand Down
18 changes: 18 additions & 0 deletions src/hickory/select_xml.cljx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
(ns hickory.select-xml
"Functions to query hickory-format XML data.

See clojure.zip for more information on zippers, locs, nodes, next, etc."
(:require [clojure.zip :as zip]))


(defn attr
"Selector which provides case sensitive version of (hickory.select/attr)"
([attr-name]
(attr attr-name (fn [_] true)))
([attr-name predicate]
(fn [hzip-loc]
(let [node (zip/node hzip-loc)
attr-key (keyword (name attr-name))]
(if (clojure.core/and (contains? (:attrs node) attr-key)
(predicate (get-in node [:attrs attr-key])))
hzip-loc)))))