From a24414eb47a3695a98c510efe8cb2daba6f24b8e Mon Sep 17 00:00:00 2001 From: Lucy Wang Date: Wed, 29 Apr 2020 15:30:54 +0800 Subject: [PATCH 1/7] normalize the class prop --- src/helix/impl/props.cljc | 20 +++++++++++++++++++- test/helix/impl/props_test.cljc | 17 +++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/src/helix/impl/props.cljc b/src/helix/impl/props.cljc index f9335e0..1c6f1ec 100644 --- a/src/helix/impl/props.cljc +++ b/src/helix/impl/props.cljc @@ -72,6 +72,24 @@ (defn merge-obj [o1 o2] #?(:cljs (js/Object.assign o1 o2))) +(defn seq-to-class [class] + (if (sequential? class) + (->> class + (map str) + (string/join " ")) + class)) + +(defn clean-class [class] + (if (string? class) + (-> class + (string/replace #"[ \n]+" " ") + (string/trim)) + class)) + +(defn normalize-class [class] + (-> class + seq-to-class + clean-class)) (defn -native-props ([m] #?(:clj (if-let [spread-sym (cond @@ -88,7 +106,7 @@ k (key entry) v (val entry)] (case k - :class (set-obj o "className" v) + :class (set-obj o "className" (normalize-class v)) :for (set-obj o "htmlFor" v) :style (set-obj o "style" (if (vector? v) diff --git a/test/helix/impl/props_test.cljc b/test/helix/impl/props_test.cljc index 89405c5..ce87131 100644 --- a/test/helix/impl/props_test.cljc +++ b/test/helix/impl/props_test.cljc @@ -112,3 +112,20 @@ :b :extra-b}] (impl/props {:foo-bar :a :b :b :c :c :d :d & extra-props})) #js {:foo-bar :extra-foo-bar :b :extra-b :c :c :d :d})))) + + +(t/deftest test-normalize-class + (t/testing "support seqs as :class value" + (t/is (= (impl/normalize-class '[foo bar]) + "foo bar")) + (t/is (= (impl/normalize-class '(foo bar)) + "foo bar")) + (t/is (= (impl/normalize-class ["foo" "bar"]) + "foo bar"))) + (t/testing "normalize string values" + (t/is (= (impl/normalize-class "foo bar ") + "foo bar")) + (t/is (= (impl/normalize-class "foo +bar + barz") + "foo bar barz")))) From aa0bee84866e265de9ae2b0756911aee23853208 Mon Sep 17 00:00:00 2001 From: Lucy Wang Date: Wed, 29 Apr 2020 21:54:32 +0800 Subject: [PATCH 2/7] separate macro expansion logic and runtime logic --- src/helix/impl/props.cljc | 28 ++++++++++++++++++++++++---- test/helix/impl/props_test.cljc | 32 ++++++++++++++++++-------------- 2 files changed, 42 insertions(+), 18 deletions(-) diff --git a/src/helix/impl/props.cljc b/src/helix/impl/props.cljc index 1c6f1ec..b579cf7 100644 --- a/src/helix/impl/props.cljc +++ b/src/helix/impl/props.cljc @@ -86,10 +86,30 @@ (string/trim)) class)) -(defn normalize-class [class] - (-> class - seq-to-class - clean-class)) +#?(:clj + (defn unquote-class + "Handle the case of (quote '[foo bar])" + [class] + (if (and (list? class) + (= (first class) 'quote)) + (-> class + second + seq-to-class + str) + class))) + +#?(:clj + (defn normalize-class [class] + #p (-> class + unquote-class + clean-class))) + +#?(:cljs + (defn normalize-class [class] + (-> class + seq-to-class + str + clean-class))) (defn -native-props ([m] #?(:clj (if-let [spread-sym (cond diff --git a/test/helix/impl/props_test.cljc b/test/helix/impl/props_test.cljc index ce87131..4ba80ab 100644 --- a/test/helix/impl/props_test.cljc +++ b/test/helix/impl/props_test.cljc @@ -115,17 +115,21 @@ (t/deftest test-normalize-class - (t/testing "support seqs as :class value" - (t/is (= (impl/normalize-class '[foo bar]) - "foo bar")) - (t/is (= (impl/normalize-class '(foo bar)) - "foo bar")) - (t/is (= (impl/normalize-class ["foo" "bar"]) - "foo bar"))) - (t/testing "normalize string values" - (t/is (= (impl/normalize-class "foo bar ") - "foo bar")) - (t/is (= (impl/normalize-class "foo -bar - barz") - "foo bar barz")))) + #?(:clj + (do + (t/testing "macro expansion - normal value shall be kept as-is" + (t/is (= (impl/normalize-class 'foo) + 'foo)) + (t/is (= (impl/normalize-class '[foo bar]) + '[foo bar]))) + (t/testing "macro expansion - quoted forms shall be converted to string" + (t/is (= (impl/normalize-class (quote '[foo bar])) + "foo bar")) + (t/is (= (impl/normalize-class (quote 'bar)) + "bar"))))) + #?(:cljs + (t/testing "runtime - all shall be converted to string" + (t/is (= (impl/normalize-class 'foo) + "foo")) + (t/is (= (impl/normalize-class '[foo bar]) + "foo bar"))))) From a99fa2c36ba4ac5d74312ebb22df13767ac4bf59 Mon Sep 17 00:00:00 2001 From: Lucy Wang Date: Wed, 29 Apr 2020 21:56:30 +0800 Subject: [PATCH 3/7] remove debug code --- src/helix/impl/props.cljc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/helix/impl/props.cljc b/src/helix/impl/props.cljc index b579cf7..974eb3e 100644 --- a/src/helix/impl/props.cljc +++ b/src/helix/impl/props.cljc @@ -100,7 +100,7 @@ #?(:clj (defn normalize-class [class] - #p (-> class + (-> class unquote-class clean-class))) From b6a11fa8fbf11ba1bf957baf2b2df058d8912680 Mon Sep 17 00:00:00 2001 From: Lucy Wang Date: Wed, 29 Apr 2020 22:07:00 +0800 Subject: [PATCH 4/7] improve cljs version a bit --- src/helix/impl/props.cljc | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/helix/impl/props.cljc b/src/helix/impl/props.cljc index 974eb3e..9c2af90 100644 --- a/src/helix/impl/props.cljc +++ b/src/helix/impl/props.cljc @@ -106,10 +106,13 @@ #?(:cljs (defn normalize-class [class] - (-> class - seq-to-class - str - clean-class))) + (if (string? class) + ;; quick path + class + (-> class + seq-to-class + str + clean-class)))) (defn -native-props ([m] #?(:clj (if-let [spread-sym (cond From be385a6ea101eead979236db3d83fa50c08addc7 Mon Sep 17 00:00:00 2001 From: Lucy Wang Date: Thu, 30 Apr 2020 11:17:18 +0800 Subject: [PATCH 5/7] remove multiline string normalize --- src/helix/impl/props.cljc | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/src/helix/impl/props.cljc b/src/helix/impl/props.cljc index 9c2af90..e939f2e 100644 --- a/src/helix/impl/props.cljc +++ b/src/helix/impl/props.cljc @@ -79,13 +79,6 @@ (string/join " ")) class)) -(defn clean-class [class] - (if (string? class) - (-> class - (string/replace #"[ \n]+" " ") - (string/trim)) - class)) - #?(:clj (defn unquote-class "Handle the case of (quote '[foo bar])" @@ -101,8 +94,7 @@ #?(:clj (defn normalize-class [class] (-> class - unquote-class - clean-class))) + unquote-class))) #?(:cljs (defn normalize-class [class] @@ -111,8 +103,7 @@ class (-> class seq-to-class - str - clean-class)))) + str)))) (defn -native-props ([m] #?(:clj (if-let [spread-sym (cond From 9c3a746cda5048c40fb8177de0c38158afc06116 Mon Sep 17 00:00:00 2001 From: Lucy Wang Date: Thu, 30 Apr 2020 11:25:19 +0800 Subject: [PATCH 6/7] filter nil at runtime --- src/helix/impl/props.cljc | 1 + test/helix/impl/props_test.cljc | 5 ++++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/helix/impl/props.cljc b/src/helix/impl/props.cljc index e939f2e..93f8c41 100644 --- a/src/helix/impl/props.cljc +++ b/src/helix/impl/props.cljc @@ -75,6 +75,7 @@ (defn seq-to-class [class] (if (sequential? class) (->> class + (remove nil?) (map str) (string/join " ")) class)) diff --git a/test/helix/impl/props_test.cljc b/test/helix/impl/props_test.cljc index 4ba80ab..9c50e8c 100644 --- a/test/helix/impl/props_test.cljc +++ b/test/helix/impl/props_test.cljc @@ -132,4 +132,7 @@ (t/is (= (impl/normalize-class 'foo) "foo")) (t/is (= (impl/normalize-class '[foo bar]) - "foo bar"))))) + "foo bar"))) + (t/testing "runtime - nil shall be filtered out" + (t/is (= (impl/normalize-class ["foo" nil]) + "foo"))))) From e272be79ca11baf93f03058674d2f777241ca016 Mon Sep 17 00:00:00 2001 From: Lucy Wang Date: Fri, 1 May 2020 10:21:49 +0800 Subject: [PATCH 7/7] address cr --- src/helix/impl/props.cljc | 12 +++++++++--- test/helix/impl/props_test.cljc | 33 +++++++++++++++++++-------------- 2 files changed, 28 insertions(+), 17 deletions(-) diff --git a/src/helix/impl/props.cljc b/src/helix/impl/props.cljc index 93f8c41..c7d1919 100644 --- a/src/helix/impl/props.cljc +++ b/src/helix/impl/props.cljc @@ -84,13 +84,19 @@ (defn unquote-class "Handle the case of (quote '[foo bar])" [class] - (if (and (list? class) - (= (first class) 'quote)) + (cond + (string? class) + class + + (and (list? class) + (= (first class) 'quote)) (-> class second seq-to-class str) - class))) + + :default + `(normalize-class ~class)))) #?(:clj (defn normalize-class [class] diff --git a/test/helix/impl/props_test.cljc b/test/helix/impl/props_test.cljc index 9c50e8c..4430e49 100644 --- a/test/helix/impl/props_test.cljc +++ b/test/helix/impl/props_test.cljc @@ -117,22 +117,27 @@ (t/deftest test-normalize-class #?(:clj (do - (t/testing "macro expansion - normal value shall be kept as-is" - (t/is (= (impl/normalize-class 'foo) - 'foo)) - (t/is (= (impl/normalize-class '[foo bar]) - '[foo bar]))) + (t/testing "macro expansion - string value shall be kept as is" + (t/is (= (impl/normalize-class "foo") + "foo"))) (t/testing "macro expansion - quoted forms shall be converted to string" (t/is (= (impl/normalize-class (quote '[foo bar])) "foo bar")) (t/is (= (impl/normalize-class (quote 'bar)) - "bar"))))) + "bar"))) + (t/testing "macro expansion - other value shall be passed to runtime check" + (t/is (= (impl/normalize-class 'foo) + '(helix.impl.props/normalize-class foo))) + (t/is (= (impl/normalize-class '[foo bar]) + '(helix.impl.props/normalize-class [foo bar]))) + (t/is (= (impl/normalize-class '(vector foo bar)) + '(helix.impl.props/normalize-class (vector foo bar))))))) #?(:cljs - (t/testing "runtime - all shall be converted to string" - (t/is (= (impl/normalize-class 'foo) - "foo")) - (t/is (= (impl/normalize-class '[foo bar]) - "foo bar"))) - (t/testing "runtime - nil shall be filtered out" - (t/is (= (impl/normalize-class ["foo" nil]) - "foo"))))) + (do (t/testing "runtime - all shall be converted to string" + (t/is (= (impl/normalize-class 'foo) + "foo")) + (t/is (= (impl/normalize-class '[foo bar]) + "foo bar"))) + (t/testing "runtime - nil shall be filtered out" + (t/is (= (impl/normalize-class ["foo" nil]) + "foo"))))))