Permalink
Browse files

Move namespace handling into tikkba.utils.dom/add-attrs

  • Loading branch information...
1 parent a4bac1d commit 4ae8cbbfaf367630d286c6f0a86ae44cbe628f01 @DanLipsitt DanLipsitt committed Jan 20, 2013
Showing with 15 additions and 15 deletions.
  1. +6 −1 src/tikkba/utils/dom.clj
  2. +9 −14 test/tikkba/utils/test/dom.clj
@@ -152,7 +152,12 @@
"Adds the attributes to the element elt."
[elt & attrs]
(doseq [[key value] (partition 2 attrs)]
- (set-attribute elt (name key) (str value))))
+ (let [name (name key)
+ value (str value)
+ [local-name ns-alias] (reverse (str/split name #":"))]
+ (if ns-alias
+ (set-attribute-ns elt (.lookupNamespaceURI elt ns-alias) local-name value)
+ (set-attribute elt name value)))))
(defn add-map-attrs
"Adds the attributes represented by a map to the element elt"
@@ -5,18 +5,13 @@
(def xlink-ns "http://www.w3.org/1999/xlink")
-(defn attrs
- "Return a map of the element's attributes as strings"
- [el]
- (let [attrs (.getAttributes el)]
- (apply merge (for [n (range (.getLength attrs))
- :let [attr (.item attrs n)]]
- {(.getName attr) (.getValue attr)}))))
-
-(deftest t-set-attribute-ns
+(deftest test-add-attrs
(let [doc (create-document (dom-implementation) svg-ns "svg" nil)
- el (create-element-ns doc svg-ns "image")]
- (set-attribute el "xlink:href" "foo.png")
- (.setAttributeNS el xlink-ns "bar" "baz")
- (is (= (get (attrs el) "xlink:href" "foo.png")))
- (is (= (.getAttributeNS el xlink-ns "href") "foo.png"))))
+ el (create-element-ns doc svg-ns "image")
+ atts ["xlink:href" "foo.png"
+ "key1" "val1"
+ :key2 "val2"]]
+ (apply add-attrs el atts)
+ (is (= "foo.png" (.getAttributeNS el xlink-ns "href")) "namespaced")
+ (is (= "val1" (.getAttribute el "key1")) "un-namespaced string")
+ (is (= "val2" (.getAttribute el "key2")) "un-namespaced keyword")))

0 comments on commit 4ae8cbb

Please sign in to comment.