Skip to content

Instantly share code, notes, and snippets.

@DeLaGuardo
Created September 4, 2021 20:25
Show Gist options
  • Save DeLaGuardo/65d4e58876c17adfeaa90b021da54719 to your computer and use it in GitHub Desktop.
Save DeLaGuardo/65d4e58876c17adfeaa90b021da54719 to your computer and use it in GitHub Desktop.
(ns omnitech.shims)
(defn- closest [selector]
(this-as node
(let [node-list (.querySelectorAll js/document selector)
all (mapv #(aget node-list %) (range 0 (.-length node-list)))]
(loop [node node]
(let [parent (.-parentNode node)]
(if (some #(= parent %) all)
parent
(if parent
(recur parent)
false)))))))
(defn- trim []
(this-as s
(-> s
(.replace (js/RegExp. "^\\s+") "")
(.replace (js/RegExp. "\\s+$") ""))))
(defn inject-shims []
(do
(let [proto (.. js/window -HTMLElement -prototype)]
(when-not (.-closest proto)
(set! (.-closest proto) closest)))
(when-not (.. js/String -prototype -trim)
(set! (.. js/String -prototype -trim) trim))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment