Skip to content

Commit

Permalink
Merge pull request metosin#258 from fraxu/shadow-dom-fix
Browse files Browse the repository at this point in the history
Support html5 links inside Shadow DOM
  • Loading branch information
Deraen authored Apr 15, 2019
2 parents d2d6253 + c9076e5 commit 9241de9
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
10 changes: 9 additions & 1 deletion modules/reitit-frontend/src/reitit/frontend/history.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -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 (.getBrowserEvent 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]
Expand All @@ -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)))
Expand Down
38 changes: 38 additions & 0 deletions test/cljs/reitit/frontend/history_test.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -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)))))

0 comments on commit 9241de9

Please sign in to comment.