From 8df97369419f0c377a94478aec76464f90bbba09 Mon Sep 17 00:00:00 2001 From: Arnaud Bos Date: Fri, 17 Jan 2014 01:21:55 +0100 Subject: [PATCH 1/2] Make template/->node-like able to handle HTML comments --- src/dommy/template.cljs | 3 +++ test/dommy/template_test.cljs | 10 +++++++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/dommy/template.cljs b/src/dommy/template.cljs index 15fa5f4..f276a75 100644 --- a/src/dommy/template.cljs +++ b/src/dommy/template.cljs @@ -102,6 +102,9 @@ js/Text (-elem [this] this) + js/Comment + (-elem [this] this) + js/Document (-elem [this] this) diff --git a/test/dommy/template_test.cljs b/test/dommy/template_test.cljs index abd6d6c..60a07db 100644 --- a/test/dommy/template_test.cljs +++ b/test/dommy/template_test.cljs @@ -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= + "
" + (.-outerHTML + (.querySelector + (template/->node-like + (template/html->nodes + "
")) + "div")))) (deftemplate span-wrapper [content] [:span content]) From 3e1b3749cc03bba3c94dae9cf60ede434486f406 Mon Sep 17 00:00:00 2001 From: Arnaud Bos Date: Fri, 17 Jan 2014 02:05:36 +0100 Subject: [PATCH 2/2] Make sel and sel1 able to deal with js/DocumentFragment for class and tagname selectors --- src/dommy/macros.clj | 16 ++++++++++++---- test/dommy/core_test.cljs | 39 ++++++++++++++++++++++++++++++++++++++- 2 files changed, 50 insertions(+), 5 deletions(-) diff --git a/src/dommy/macros.clj b/src/dommy/macros.clj index 0037dfb..74424e1 100644 --- a/src/dommy/macros.clj +++ b/src/dommy/macros.clj @@ -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] @@ -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] diff --git a/test/dommy/core_test.cljs b/test/dommy/core_test.cljs index 72d1b9e..ebb6bf4 100644 --- a/test/dommy/core_test.cljs +++ b/test/dommy/core_test.cljs @@ -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) @@ -121,6 +122,42 @@ ;; Simple (dommy/append! body (node [:div#foo])) (is= (.-tagName (sel1 :#foo)) "DIV") + ;; Simple id selector on DocumentFragment + (is= + "
" + (.-outerHTML + (sel1 + (template/->node-like + (template/html->nodes + "
")) + :#id1))) + ;; Simple class unique-selector on DocumentFragment + (is= + "
" + (.-outerHTML + (sel1 + (template/->node-like + (template/html->nodes + "
")) + :.class1))) + ;; Simple tagname unique-selector on DocumentFragment + (is= + "
" + (.-outerHTML + (sel1 + (template/->node-like + (template/html->nodes + "
")) + :div))) + ;; Simple class selector on DocumentFragment + (is= + 2 + (.-length + (sel + (template/->node-like + (template/html->nodes + "
")) + :.class1))) ;; Evaluated (let [bar :#bar bar-el (node bar)]