Skip to content

Instantly share code, notes, and snippets.

@noprompt
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save noprompt/9541611 to your computer and use it in GitHub Desktop.
Save noprompt/9541611 to your computer and use it in GitHub Desktop.
(defn- do-selector-fn []
(let [fn-sym (gensym "f")
arg (gensym "x")
invoke-specs (clojure.core/map
(fn [i syms]
(let [argslist (vec (take i syms))
body `(~fn-sym (str (p/selector ~arg)
~@(clojure.core/map
(fn [sym]
`(p/selector ~sym))
(rest argslist))))]
`(~'invoke ~argslist ~body)))
(range 1 22)
(repeat '[_ a b c d e f g h i j k l m n o p q r s t]))
impl `(reify
p/ICssSelector
(~'selector [~'_]
(p/selector ~arg))
IFn
~@invoke-specs)]
`(fn ~fn-sym [~arg] ~impl)))
(defmacro deftypeselector [sym]
`(def ~sym (~(do-selector-fn) ~(name sym))))
(defmacro defpseudoclass [sym]
`(def ~sym (~(do-selector-fn) ~(str \: (name sym)))))
;; Example
(deftypeselector a)
(defpseudoclass hover)
(p/selector a)
;; => "a"
(p/selector hover)
;; => ":hover"
(a hover)
;; => #<selectors$f16521$reify__16523 garden.selectors$f16521$reify__16523@2917eb65>
(p/selector (a hover))
;; => "a:hover"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment