Skip to content

Commit

Permalink
(set-html!) takes any DomContent for both args, not just the first
Browse files Browse the repository at this point in the history
  • Loading branch information
Bobby Calderwood authored and levand committed Apr 27, 2012
1 parent b931ed7 commit 68d6e70
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 9 deletions.
27 changes: 18 additions & 9 deletions src/cljs/domina.cljs
Expand Up @@ -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?
Expand All @@ -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
Expand Down
12 changes: 12 additions & 0 deletions test/cljs/domina/test.cljs
Expand Up @@ -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>")
Expand Down

0 comments on commit 68d6e70

Please sign in to comment.