From b258e0abbaa7cecc181355437f6d3274d25a7c42 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antti=20Lepp=C3=A4nen?= Date: Wed, 10 Apr 2019 08:42:09 +0300 Subject: [PATCH 1/3] Support html5 links inside Shadow DOM --- .../src/reitit/frontend/history.cljs | 10 ++++- test/cljs/reitit/frontend/history_test.cljs | 38 +++++++++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/modules/reitit-frontend/src/reitit/frontend/history.cljs b/modules/reitit-frontend/src/reitit/frontend/history.cljs index 090b489f..30952f4b 100644 --- a/modules/reitit-frontend/src/reitit/frontend/history.cljs +++ b/modules/reitit-frontend/src/reitit/frontend/history.cljs @@ -59,6 +59,14 @@ el (recur (.-parentNode el))))))) +(defn- event-target [event] + ;; Read event's target from composed path to get shadow dom working, + ;; fallback to target property if not available + (let [original-event (.-event_ event)] + (if (exists? (.-composedPath original-event)) + (first (.composedPath original-event)) + (.-target event)))) + (defrecord Html5History [on-navigate router listen-key click-listen-key] History (-init [this] @@ -76,7 +84,7 @@ (fn ignore-anchor-click [e] ;; Returns the next matching anchestor of event target - (when-let [el (closest-by-tag (.-target e) "a")] + (when-let [el (closest-by-tag (event-target e) "a")] (let [uri (.parse Uri (.-href el))] (when (and (or (and (not (.hasScheme uri)) (not (.hasDomain uri))) (= current-domain (.getDomain uri))) diff --git a/test/cljs/reitit/frontend/history_test.cljs b/test/cljs/reitit/frontend/history_test.cljs index 81878d45..2873d730 100644 --- a/test/cljs/reitit/frontend/history_test.cljs +++ b/test/cljs/reitit/frontend/history_test.cljs @@ -127,3 +127,41 @@ (done)) (do (is false "extra event"))))) {:use-fragment false})])))) + +(deftest html5-history-link-click-test + (when browser + (gevents/removeAll js/window goog.events.EventType.POPSTATE) + (gevents/removeAll js/window goog.events.EventType.HASHCHANGE) + (gevents/removeAll js/document goog.events.EventType.CLICK) + + ;; Will fail with "Some of your tests did a full page reload!" + ;; if link events are not handled + + (async done + (let [clicks (atom nil) + click-next (fn [] + (let [target (first @clicks)] + (swap! clicks rest) + (.click target))) + shadow-element (js/document.createElement "DIV") + shadow-root (.attachShadow shadow-element #js {:mode "open"}) + history (rfh/start! router (fn [_ history] + (when @clicks + (if (seq @clicks) + (click-next) + (do + (rfh/stop! history) + (done))))) + {:use-fragment false}) + create-link #(doto + (js/document.createElement "A") + (.setAttribute "href" (rfh/href history ::foo))) + document-link (create-link) + shadow-link (create-link)] + (.appendChild js/document.body document-link) + + (.appendChild js/document.body shadow-element) + (.appendChild shadow-root shadow-link) + + (reset! clicks [document-link shadow-link]) + (click-next))))) From 733958404e942507b2c0b3f765e1c9c0d526f6a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antti=20Lepp=C3=A4nen?= Date: Wed, 10 Apr 2019 12:14:21 +0300 Subject: [PATCH 2/3] Use doc-string. --- modules/reitit-frontend/src/reitit/frontend/history.cljs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/reitit-frontend/src/reitit/frontend/history.cljs b/modules/reitit-frontend/src/reitit/frontend/history.cljs index 30952f4b..b759e29d 100644 --- a/modules/reitit-frontend/src/reitit/frontend/history.cljs +++ b/modules/reitit-frontend/src/reitit/frontend/history.cljs @@ -60,8 +60,8 @@ (recur (.-parentNode el))))))) (defn- event-target [event] - ;; Read event's target from composed path to get shadow dom working, - ;; fallback to target property if not available + "Read event's target from composed path to get shadow dom working, + fallback to target property if not available" (let [original-event (.-event_ event)] (if (exists? (.-composedPath original-event)) (first (.composedPath original-event)) From c9076e57ad2925dc9702c205d96515c60c2b7a4c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antti=20Lepp=C3=A4nen?= Date: Fri, 12 Apr 2019 07:12:46 +0300 Subject: [PATCH 3/3] Use getBrowserEvent method instead of _event property --- modules/reitit-frontend/src/reitit/frontend/history.cljs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/reitit-frontend/src/reitit/frontend/history.cljs b/modules/reitit-frontend/src/reitit/frontend/history.cljs index b759e29d..35cbf52d 100644 --- a/modules/reitit-frontend/src/reitit/frontend/history.cljs +++ b/modules/reitit-frontend/src/reitit/frontend/history.cljs @@ -62,7 +62,7 @@ (defn- event-target [event] "Read event's target from composed path to get shadow dom working, fallback to target property if not available" - (let [original-event (.-event_ event)] + (let [original-event (.getBrowserEvent event)] (if (exists? (.-composedPath original-event)) (first (.composedPath original-event)) (.-target event))))