Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance selectors and templating when dealing with js/DocumentFragment #71

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions src/dommy/macros.clj
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,12 @@
#(= "body" %) `js/document.body
#(= "head" %) `js/document.head
#(and (= 'js/document base) (id-selector? %)) `(by-id ~data)
class-selector? `(aget (by-class ~base ~data) 0)
tag-selector? `(aget (by-tag ~base ~data) 0)
class-selector? `(if (= js/DocumentFragment (type ~base))
(.querySelector (node ~base) ~(selector-form data))
(aget (by-class ~base ~data) 0))
tag-selector? `(if (= js/DocumentFragment (type ~base))
(.querySelector (node ~base) ~(selector-form data))
(aget (by-tag ~base ~data) 0))
(query-selector base data))
(query-selector base data)))
([data]
Expand All @@ -81,8 +85,12 @@
([base data]
(if (constant? data)
(condp #(%1 %2) (name data)
class-selector? `(by-class ~base ~data)
tag-selector? `(by-tag ~base ~data)
class-selector? `(if (= js/DocumentFragment (type ~base))
(.querySelectorAll (node ~base) ~(selector-form data))
(by-class ~base ~data))
tag-selector? `(if (= js/DocumentFragment (type ~base))
(.querySelectorAll (node ~base) ~(selector-form data))
(by-tag ~base ~data))
(query-selector-all base data))
(query-selector-all base data)))
([data]
Expand Down
3 changes: 3 additions & 0 deletions src/dommy/template.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@
js/Text
(-elem [this] this)

js/Comment
(-elem [this] this)

js/Document
(-elem [this] this)

Expand Down
39 changes: 38 additions & 1 deletion test/dommy/core_test.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
(:require
[cljs-test.core :as test]
[dommy.utils :as utils]
[dommy.core :as dommy]))
[dommy.core :as dommy]
[dommy.template :as template]))

(def body js/document.body)

Expand Down Expand Up @@ -121,6 +122,42 @@
;; Simple
(dommy/append! body (node [:div#foo]))
(is= (.-tagName (sel1 :#foo)) "DIV")
;; Simple id selector on DocumentFragment
(is=
"<div id=\"id1\"></div>"
(.-outerHTML
(sel1
(template/->node-like
(template/html->nodes
"<div id=\"id1\"></div>"))
:#id1)))
;; Simple class unique-selector on DocumentFragment
(is=
"<div class=\"class1\"></div>"
(.-outerHTML
(sel1
(template/->node-like
(template/html->nodes
"<div class=\"class1\"></div>"))
:.class1)))
;; Simple tagname unique-selector on DocumentFragment
(is=
"<div class=\"class1\"></div>"
(.-outerHTML
(sel1
(template/->node-like
(template/html->nodes
"<div class=\"class1\"></div>"))
:div)))
;; Simple class selector on DocumentFragment
(is=
2
(.-length
(sel
(template/->node-like
(template/html->nodes
"<div class=\"class1\"></div><div class=\"class1\"></div>"))
:.class1)))
;; Evaluated
(let [bar :#bar
bar-el (node bar)]
Expand Down
10 changes: 9 additions & 1 deletion test/dommy/template_test.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,15 @@
(.-outerHTML
(doto (template/node [:div])
(.appendChild
(template/->node-like (list :.class1 :.class2 :.class3)))))))
(template/->node-like (list :.class1 :.class2 :.class3))))))
(is=
"<div><!-- commented out <div class=\"class3\"></div>--></div>"
(.-outerHTML
(.querySelector
(template/->node-like
(template/html->nodes
"<div><!-- commented out <div class=\"class3\"></div>--></div>"))
"div"))))

(deftemplate span-wrapper [content]
[:span content])
Expand Down