Skip to content

Commit

Permalink
Update html escaping and related tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
virvar committed May 8, 2015
1 parent 0781c84 commit 0df3878
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
13 changes: 5 additions & 8 deletions src/cljs/enfocus/core.cljs
Expand Up @@ -273,17 +273,14 @@
(domina/destroy-children! %1)
(domina/append! %1 %2))))

(defn- escape-html
[text]
(let [pre (.createElement js/document "pre")
text-node (.createTextNode js/document text)]
(.appendChild pre text-node)
(.-innerHTML pre)))

(defn text-content
"Replaces the content of the element escaping html."
[text]
(content (escape-html text)))
(content (string/escape text {\< "&lt;",
\> "&gt;",
\& "&amp;",
\' "&apos;",
\" "&quot;"})))

(defn html-content
"Replaces the content of the element with the dom structure represented by the html string passed"
Expand Down
14 changes: 11 additions & 3 deletions test/cljs/enfocus/core_test.cljs
Expand Up @@ -198,10 +198,18 @@


(deftest text-content-test
(testing "text-content"
(ef/at "#test-id" (ef/text-content "<b>testing</b>"))
(testing "text-content not matching html"
(ef/at "#test-id" (ef/text-content "<>&'\""))
(let [res (.-textContent (by-id "test-id"))]
(is (= "<b>testing</b>" res)))))
(is (= "<>&'\"" res))))
(testing "text-content with tags"
(ef/at "#test-id" (ef/text-content "<b>test</b>"))
(let [res (.-textContent (by-id "test-id"))]
(is (= "<b>test</b>" res))))
(testing "text-content with entities"
(ef/at "#test-id" (ef/text-content "&lt;&gt;"))
(let [res (.-textContent (by-id "test-id"))]
(is (= "&lt;&gt;" res)))))


(deftest html-content-test
Expand Down

0 comments on commit 0df3878

Please sign in to comment.