Skip to content

Commit 539e3e0

Browse files
committed
cljs: share node namespace with clj
1 parent 78e36db commit 539e3e0

13 files changed

Lines changed: 393 additions & 131 deletions

File tree

pom.xml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,15 @@
3535
<version>0.1.2</version>
3636
</parent>
3737

38+
<dependencies>
39+
<dependency>
40+
<groupId>org.clojure</groupId>
41+
<artifactId>clojurescript</artifactId>
42+
<version>1.9.293</version>
43+
<scope>test</scope>
44+
</dependency>
45+
</dependencies>
46+
3847
<build>
3948
<plugins>
4049
<plugin>
@@ -72,7 +81,7 @@
7281
</build>
7382

7483
<properties>
75-
<clojure.version>1.5.0</clojure.version>
84+
<clojure.version>1.9.0-alpha14</clojure.version>
7685
</properties>
7786

7887
<repositories>

project.clj

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,7 @@
66
[org.clojure/clojurescript "1.9.293"]
77
[com.cemerick/piggieback "0.2.1"]
88
[org.clojure/tools.nrepl "0.2.12"]
9-
[figwheel-sidecar "0.5.8"]]
9+
[org.clojure/test.check "0.9.0"]
10+
[figwheel-sidecar "0.5.8"]
11+
[binaryage/devtools "0.8.3"]]
1012
:repl-options {:nrepl-middleware [cemerick.piggieback/wrap-cljs-repl]})

src/main/clojure/clojure/data/xml.clj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
(export-api node/element* node/element node/cdata node/xml-comment
3535
prxml/sexp-as-element prxml/sexps-as-fragment event/element-nss
3636
name/alias-uri name/parse-qname name/qname-uri
37-
name/qname-local name/qname name/to-qname name/uri-symbol
37+
name/qname-local name/qname name/to-qname name/uri-symbol name/symbol-uri
3838
process/find-xmlns process/aggregate-xmlns)
3939

4040
(defn canonical-name

src/main/clojure/clojure/data/xml.cljs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@
44
(:require
55
[clojure.data.xml.name :as name]
66
[clojure.data.xml.node :as node]
7+
[clojure.data.xml.js.dom :as dom]
78
[clojure.data.xml.protocols :refer [AsQName]]))
89

910
(export-api
10-
name/parse-qname name/qname-uri name/qname-local name/qname name/to-qname name/uri-symbol
11-
node/element* node/element node/cdata node/xml-comment node/text-node
12-
node/extend-dom-as-data! node/element-node node/element-data)
11+
name/parse-qname name/qname-uri name/qname-local name/qname name/to-qname name/uri-symbol name/symbol-uri
12+
node/element* node/element node/cdata node/xml-comment
13+
dom/extend-dom-as-data! dom/element-node dom/element-data)
1314

1415
(defn canonical-name
1516
"Put (q)name into canonical form as per ns-env"
@@ -46,5 +47,5 @@
4647
[e & {:keys []}]
4748
(. (js/XMLSerializer.)
4849
(serializeToString
49-
(node/element-node e))))
50+
(element-node e))))
5051

src/main/clojure/clojure/data/xml/node.cljs renamed to src/main/clojure/clojure/data/xml/js/dom.cljs

Lines changed: 115 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
(ns clojure.data.xml.node
1+
(ns clojure.data.xml.js.dom
22
(:require
3-
[clojure.data.xml.name :refer [qname-uri qname-local canonical-name]]))
3+
[clojure.data.xml.name :refer [qname-uri qname-local canonical-name xmlns-uri]]
4+
[clojure.data.xml.node :as node]))
45

56
(def doc
67
(.. (js/DOMParser.)
@@ -77,6 +78,7 @@
7778
(def Element (type (element :e)))
7879
(def NamedNodeMap (type (.-attributes (element :e))))
7980
(def NodeList (type (node-list [])))
81+
(def Attr (type (aget (.-attributes (element :e {:a "1"})) 0)))
8082

8183
;; ## Coercions
8284

@@ -104,14 +106,23 @@
104106
(.-localName el)
105107
""))
106108

109+
(defn- xmlns-attr? [a]
110+
(identical? xmlns-uri (.-namespaceURI a)))
111+
(def remove-xmlns-attrs-xf (remove xmlns-attr?))
112+
(def remove-xmlns-attrs (partial into {} remove-xmlns-attrs-xf))
113+
(def filter-xmlns-attrs (partial into {} (filter xmlns-attr?)))
114+
107115
(defn dom-element-attrs [el]
108-
(persistent!
109-
(reduce (fn [ta attr-node]
110-
(assoc! ta
111-
(dom-element-tag attr-node)
112-
(.-value attr-node)))
113-
(transient {})
114-
(array-seq el))))
116+
(transduce
117+
remove-xmlns-attrs-xf
118+
(completing
119+
(fn [ta attr-node]
120+
(assoc! ta
121+
(dom-element-tag attr-node)
122+
(.-value attr-node)))
123+
persistent!)
124+
(transient {})
125+
(array-seq el)))
115126

116127
(declare element-data)
117128

@@ -130,9 +141,14 @@
130141
(instance? Text el)
131142
(.-nodeValue el)
132143
(instance? Element el)
133-
{:tag (dom-element-tag el)
134-
:attrs (dom-element-attrs (.-attributes el))
135-
:content (node-list-vec (.-childNodes el))}
144+
(node/element* (dom-element-tag el)
145+
(dom-element-attrs (.-attributes el))
146+
(node-list-vec (.-childNodes el))
147+
(do
148+
(prn "META" {:clojure.data.xml/nss (filter-xmlns-attrs
149+
(.-attributes el))})
150+
{:clojure.data.xml/nss (filter-xmlns-attrs
151+
(.-attributes el))}))
136152
;;(instance? NamedNodeMap el)
137153
(.-getNamedItemNS el)
138154
(dom-element-attrs el)
@@ -144,51 +160,87 @@
144160

145161
(defn extend-dom-as-data! []
146162
(extend-type Element
163+
IMap
164+
IMeta
165+
(-meta [el]
166+
{:clojure.data.xml/nss (filter-xmlns-attrs
167+
(.-attributes el))})
147168
ILookup
148-
(-lookup [el k]
149-
(case k
150-
:tag (dom-element-tag el)
151-
:attrs (.-attributes el)
152-
:content (.-childNodes el)
153-
(throw "XML tag has no key" {:key k :el el})))
169+
(-lookup
170+
([el k]
171+
(case k
172+
:tag (dom-element-tag el)
173+
:attrs (.-attributes el)
174+
:content (.-childNodes el)
175+
(throw "XML tag has no key" {:key k :el el})))
176+
([el k nf]
177+
(println "Element" k "=>" (case k
178+
:tag (dom-element-tag el)
179+
:attrs (.-attributes el)
180+
:content (.-childNodes el)
181+
nf))
182+
(case k
183+
:tag (dom-element-tag el)
184+
:attrs (remove-xmlns-attrs (.-attributes el))
185+
:content (.-childNodes el)
186+
nf)))
187+
ICounted
188+
(-count [nm] 3)
154189
IEquiv
155190
(-equiv [el0 el1]
156-
(.isEqualNode el0 el1)))
191+
(if false #_(instance? Element el1)
192+
(do
193+
;; we can't use .isEqualNode, since that has bugs with namespaces
194+
(.log js/console el0 el1)
195+
(println 'isEqualNode (.isEqualNode el0 el1))
196+
(.isEqualNode el0 el1))
197+
(and (= (:tag el0) (:tag el1))
198+
(= (:attrs el0) (:attrs el1))
199+
(= (:content el0) (:content el1))))))
157200
(extend-type NamedNodeMap
201+
IMap
202+
ISeqable
203+
(-seq [nm] (array-seq nm))
158204
ILookup
159205
(-lookup
160206
([attrs attr]
161207
(if-let [i (.getNamedItemNS attrs (qname-uri attr) (qname-local attr))]
162208
(.-value i)
163209
nil))
164210
([attrs attr not-found]
211+
(println "Attrs" attr "=>" (if-let [i (.getNamedItemNS attrs (qname-uri attr) (qname-local attr))]
212+
(.-value i)
213+
not-found))
165214
(if-let [i (.getNamedItemNS attrs (qname-uri attr) (qname-local attr))]
166215
(.-value i)
167216
not-found)))
168217
ICounted
169-
(-count [nm] (alength nm))
218+
(-count [nm] (reduce (fn [acc attr]
219+
(if (xmlns-attr? attr)
220+
acc
221+
(inc acc)))
222+
0 nm))
170223
IKVReduce
171224
(-kv-reduce [nm f init]
172225
(reduce (fn [acc attr]
173-
(f acc (dom-element-tag attr) (.-value attr)))
226+
(if (xmlns-attr? attr)
227+
acc
228+
(f acc (dom-element-tag attr) (.-value attr))))
174229
init nm))
175-
IIndexed
176-
(-nth
177-
([nm n] (.-value (aget nm n)))
178-
([nm n nf] (if (and (<= 0 n) (< n (alength nm)))
179-
(.-value (aget nm n))
180-
nf)))
181230
IEquiv
182231
(-equiv [nm0 nm1]
183-
(and (identical? (count nm0) (count nm1))
184-
(reduce-kv (fn [_ qn v]
185-
(or (identical? v (get nm1 qn ""))
186-
(reduced false)))
187-
true nm0))))
232+
(println "NamedNodeMap.-equiv" (identical? nm0 nm1) (count nm0) (count nm1))
233+
(or (identical? nm0 nm1)
234+
(and (identical? (count nm0) (count nm1))
235+
(reduce-kv (fn [_ qn v]
236+
(println "=" v 'qn qn '(get nm1 qn "") (get nm1 qn ""))
237+
(or (identical? v (get nm1 qn ""))
238+
(reduced false)))
239+
true nm0)))))
188240
(extend-type NodeList
189241
;specify! (.. (node-list []) -constructor -prototype)
190242
ISeqable
191-
(-seq [nl] (map as-node (array-seq nl)))
243+
(-seq [nl] (seq (map as-node (array-seq nl))))
192244
ISequential
193245
ICounted
194246
(-count [nl] (alength nl))
@@ -202,15 +254,39 @@
202254
nf)))
203255
IEquiv
204256
(-equiv [nl0 nl1]
205-
(and (identical? (count nl0) (count nl1))
206-
(reduce (fn [_ n]
207-
(or (= (nth nl0 n) (nth nl1 n))
208-
(reduced false)))
209-
true (range (count nl0))))))
257+
(println "NodeList.-equiv")
258+
(or (identical? nl0 nl1)
259+
(and (identical? (count nl0) (count nl1))
260+
(reduce (fn [_ n]
261+
(or (= (nth nl0 n) (nth nl1 n))
262+
(reduced false)))
263+
true (range (count nl0)))))))
210264
(extend-type Text
211265
IEquiv
212266
(-equiv [t0 t1]
213-
(identical? (.-nodeValue t0) (.-nodeValue t1))))
267+
(identical? (.-nodeValue t0)
268+
(if (instance? Text t1)
269+
(.-nodeValue t1)
270+
t1))))
271+
(extend-type Attr
272+
ISeqable
273+
(-seq [attr] (array-seq #js[(key attr) (key attr)]))
274+
IMapEntry
275+
(-key [attr] (dom-element-tag attr))
276+
(-val [attr] (.-value attr))
277+
ISequential
278+
ICounted
279+
(-count [_] 2)
280+
IIndexed
281+
(-nth
282+
([attr n] (case n
283+
0 (key attr)
284+
1 (val attr)))
285+
([attr n nf]
286+
(case n
287+
0 (dom-element-tag attr)
288+
1 (.-value attr)
289+
nf))))
214290
{'Text Text
215291
'Element Element
216292
'NamedNodeMap NamedNodeMap

src/main/clojure/clojure/data/xml/name.cljc

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@
3030
(defn uri-symbol [uri]
3131
(symbol (encode-uri (str "xmlns." uri))))
3232

33+
(defn symbol-uri [ss]
34+
(let [du (decode-uri (str ss))]
35+
(if (.startsWith du "xmlns.")
36+
(subs du 6)
37+
(throw (ex-info "Uri symbol not valid" {:sym ss})))))
38+
3339
(defn qname-uri
3440
"Get the namespace uri for this qname"
3541
[v]
@@ -75,10 +81,13 @@
7581
(qname-local [s] (qname-local (parse-qname s)))
7682
(qname-uri [s] (qname-uri (parse-qname s))))
7783

78-
(defn canonical-name [uri local prefix]
79-
(keyword (when-not (str/blank? uri)
80-
(encode-uri (str "xmlns." uri)))
81-
local))
84+
(defn canonical-name
85+
([local] (canonical-name "" local ""))
86+
([uri local] (canonical-name uri local ""))
87+
([uri local prefix]
88+
(keyword (when-not (str/blank? uri)
89+
(encode-uri (str "xmlns." uri)))
90+
local)))
8291

8392
(defn to-qname [n]
8493
(make-qname (or (qname-uri n) "") (qname-local n) ""))

src/main/clojure/clojure/data/xml/node.clj renamed to src/main/clojure/clojure/data/xml/node.cljc

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,33 +27,40 @@
2727
(defrecord CData [content])
2828
(defrecord Comment [content])
2929

30+
#?(:cljs ;; http://dev.clojure.org/jira/browse/CLJS-1859
31+
(extend-type Element
32+
IEquiv
33+
(-equiv [el o]
34+
(js/cljs.core.equiv_map el o))))
35+
3036
(defn element*
3137
"Create an xml element from a content collection and optional metadata"
3238
([tag attrs content meta]
33-
(Element. tag (or attrs {}) (remove nil? content) meta nil))
39+
(Element. tag (or attrs {}) (remove nil? content) meta nil))
3440
([tag attrs content]
35-
(Element. tag (or attrs {}) (remove nil? content))))
41+
(Element. tag (or attrs {}) (remove nil? content))))
3642

37-
;; Compiler macro for inlining the two constructors
38-
(alter-meta! #'element* assoc :inline
39-
(fn
40-
([tag attrs content meta]
41-
`(Element. ~tag (or ~attrs {}) (remove nil? ~content) ~meta nil))
42-
([tag attrs content]
43-
`(Element. ~tag (or ~attrs {}) (remove nil? ~content)))))
43+
#?(:clj
44+
;; Compiler macro for inlining the two constructors
45+
(alter-meta! #'element* assoc :inline
46+
(fn
47+
([tag attrs content meta]
48+
`(Element. ~tag (or ~attrs {}) (remove nil? ~content) ~meta nil))
49+
([tag attrs content]
50+
`(Element. ~tag (or ~attrs {}) (remove nil? ~content))))))
4451

4552
(defn element
4653
"Create an xml Element from content varargs"
4754
([tag] (element* tag nil nil))
4855
([tag attrs] (element* tag attrs nil))
4956
([tag attrs & content] (element* tag attrs content)))
5057

51-
(definline cdata
58+
(defn cdata
5259
"Create a CData node"
5360
[content]
54-
`(CData. ~content))
61+
(CData. content))
5562

56-
(definline xml-comment
63+
(defn xml-comment
5764
"Create a Comment node"
5865
[content]
59-
`(Comment. ~content))
66+
(Comment. content))

0 commit comments

Comments
 (0)