-
Notifications
You must be signed in to change notification settings - Fork 27
/
cljss_compiler.clj
59 lines (49 loc) · 1.91 KB
/
cljss_compiler.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
(ns sablono.cljss-compiler
(:require [sablono.compiler :as s]
[sablono.util :as sutil]
[cljss.builder :as builder]))
(defn gc [gen-class class]
(if (seq class)
`(str ~gen-class " " ~@(->> (mapcat identity class)
(interpose " ")))
gen-class))
(defn- compile-class [class styles]
(let [cls (str "css-" (hash styles))
css (builder/build-styles cls styles)]
(if cljss.core/*exclude-static?*
(let [[cls static vals] css]
(swap! cljss.ssr/*ssr-ctx* assoc-in [:static cls] static)
(gc `(cljss.core/css ~cls "" ~vals) class))
(gc `(cljss.core/css ~@css) class))))
(defn- normalize-attr [tag name type]
(if (and (or (= name :on-change) (= name :onChange))
(or (= tag "textarea")
(and (= tag "input")
(or (nil? type)
(->> type (re-matches #"^(fil|che|rad).*") nil?)))))
:on-input
name))
(defn- compile-prum-attrs [tag attrs]
(->> attrs
(map (fn [[name value]]
[(normalize-attr tag name (:type attrs)) value]))))
(defn- compile-rum-attrs [class attrs]
(->> attrs
(reduce (fn [attrs [name value]]
(if (= name :css)
(assoc attrs :class (compile-class class value))
(assoc attrs name (s/compile-attr name value))))
nil)))
(defn- compile-attrs
([attrs]
(compile-attrs nil attrs))
([tag {:keys [class className class-name] :as attrs}]
(let [class (filter identity [class className class-name])]
(cond->> (seq attrs)
tag (compile-prum-attrs tag)
true (compile-rum-attrs class)
true (sutil/html-to-dom-attrs)
true (s/to-js)))))
;; WARNING: NEVER EVER DO THIS AT HOME!
;; swap `sablono.compiler/compile-attrs` fn with own, adjusted, implementation
(alter-var-root #'s/compile-attrs (fn [_] compile-attrs))