Skip to content

Commit

Permalink
cljs: conversion fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
bendlas committed Dec 5, 2016
1 parent fc3b701 commit 60b1022
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
17 changes: 10 additions & 7 deletions src/main/clojure/clojure/data/xml.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@
"Use XMLSerializer to render an xml string"
[e & {:keys []}]
(. (js/XMLSerializer.)
(serializeToString e)))
(serializeToString
(node/coerce-to-dom e))))

(defn extend-nodes-as-qname! []
(extend-protocol AsQName
Expand All @@ -50,6 +51,11 @@
(qname-local [e] (.-localName e))
(qname-uri [e] (.-namespaceURI e))))

(defn- as-node [n]
(if (instance? js/Text n)
(.-wholeText n) ;; .-data
n))

(defn extend-dom-as-data! []
(extend-type js/Element
ILookup
Expand All @@ -74,20 +80,17 @@
not-found))))
(extend-type js/NodeList
ISeqable
(-seq [nl] (array-seq nl))
(-seq [nl] (map as-node (array-seq nl)))
ISequential
ICounted
(-count [nl] (.-length nl))
IIndexed
(-nth
([nl n]
(aget nl n))
(as-node (aget nl n)))
([nl n nf]
(if (and (<= 0 n) (< n (.-length nl)))
(let [res (aget nl n)]
(if (instance? js/Text res)
(.-data res)
res))
(as-node (aget nl n))
nf)))))

(comment
Expand Down
9 changes: 9 additions & 0 deletions src/main/clojure/clojure/data/xml/node.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,12 @@
"Create a Comment node"
[content]
(.createComment doc content))

(defn coerce-to-dom [node]
(cond
(string? node) node
(instance? js/Element node) node
(satisfies? ILookup node) (element* (:tag node)
(:attrs node)
(map coerce-to-dom (:content node)))
:else (throw (ex-info "Cannot coerce" {:form node}))))

0 comments on commit 60b1022

Please sign in to comment.