Permalink
Browse files

(set-html!) takes any DomContent for both args, not just the first

  • Loading branch information...
1 parent b931ed7 commit 68d6e7033ecbfe8edf1b82a97c068d34484f97d3 Bobby Calderwood committed with Apr 18, 2012
Showing with 30 additions and 9 deletions.
  1. +18 −9 src/cljs/domina.cljs
  2. +12 −0 test/cljs/domina/test.cljs
View
27 src/cljs/domina.cljs
@@ -331,16 +331,18 @@
[content]
(. (single-node content) -innerHTML))
-(defn set-html!
- "Sets the innerHTML value for all the nodes in the given content."
+(defn- replace-children!
+ [content inner-content]
+ (-> content
+ destroy-children!
+ (append! inner-content)))
+
+(defn- set-inner-html!
[content html-string]
(let [allows-inner-html? (not (re-find re-no-inner-html html-string))
leading-whitespace? (re-find re-leading-whitespace html-string)
tag-name (. (str (second (re-find re-tag-name html-string))) (toLowerCase))
- special-tag? (contains? wrap-map tag-name)
- fallback #(-> content
- destroy-children!
- (append! %))]
+ special-tag? (contains? wrap-map tag-name)]
(if (and allows-inner-html?
(or
support/leading-whitespace?
@@ -352,9 +354,16 @@
(events/removeAll node)
(set! (. node -innerHTML) value))
(catch Exception e
- (fallback value))))
- (fallback html-string)))
- content)
+ (replace-children! content value))))
+ (replace-children! content html-string))
+ content))
+
+(defn set-html!
+ "Sets the innerHTML value for all the nodes in the given content."
+ [content inner-content]
+ (if (string? inner-content)
+ (set-inner-html! content inner-content)
+ (replace-children! content inner-content)))
(defn get-data
"Returns data associated with a node for a given key. Assumes
View
12 test/cljs/domina/test.cljs
@@ -553,12 +553,24 @@
(set-html! (xpath "//div") "<p class='foobar'>some text</p>")
(assert (= 1 (count (nodes (xpath "//body/div/p[@class='foobar']")))))))
+(add-test "can set a node's innerHTML to non-string content"
+ #(do (reset)
+ (append! (xpath "//body") "<div></div>")
+ (set-html! (xpath "//div") (nodes "<p class='foobar'>some text</p>"))
+ (assert (= 1 (count (nodes (xpath "//body/div/p[@class='foobar']")))))))
+
(add-test "can set multiple nodes' innerHTML"
#(do (reset)
(append! (xpath "//body") "<div></div><div></div>")
(set-html! (xpath "//div") "<p class='foobar'>some text</p>")
(assert (= 2 (count (nodes (xpath "//body/div/p[@class='foobar']")))))))
+(add-test "can set multiple nodes' innerHTML to non-string content"
+ #(do (reset)
+ (append! (xpath "//body") "<div></div><div></div>")
+ (set-html! (xpath "//div") (nodes "<p class='foobar'>some text</p>"))
+ (assert (= 2 (count (nodes (xpath "//body/div/p[@class='foobar']")))))))
+
(add-test "can set a table's innerHTML"
#(do (reset)
(append! (xpath "//body") "<table></table>")

0 comments on commit 68d6e70

Please sign in to comment.