From 95e44c9eee581a35b79f9dccbcb7db280eacd70e Mon Sep 17 00:00:00 2001 From: jhe Date: Sun, 2 Sep 2018 22:18:00 +0200 Subject: [PATCH] make element properties optional Signed-off-by: Julien Eluard --- src/pluto/reader/hooks.cljc | 10 ++++++---- src/pluto/reader/views.cljc | 18 ++++++++++++++---- test/pluto/reader/views_test.cljc | 3 +-- 3 files changed, 21 insertions(+), 10 deletions(-) diff --git a/src/pluto/reader/hooks.cljc b/src/pluto/reader/hooks.cljc index 0f4011d..186046a 100644 --- a/src/pluto/reader/hooks.cljc +++ b/src/pluto/reader/hooks.cljc @@ -27,10 +27,12 @@ {:data m})) (defn hiccup-with-properties [h properties] - (if (vector? h) - (let [[tag props & children] h - {:keys [data]} (inject-properties props properties)] - (apply conj [tag data] + (if (vector? h) + (let [[tag & properties-children] h + [props children] (views/resolve-properties-children properties-children) + {:keys [data]} (when properties + (inject-properties props properties))] + (apply conj (if data [tag data] [tag]) (map #(hiccup-with-properties % properties) children))) h)) diff --git a/src/pluto/reader/views.cljc b/src/pluto/reader/views.cljc index ffa9956..b1e9a31 100644 --- a/src/pluto/reader/views.cljc +++ b/src/pluto/reader/views.cljc @@ -70,6 +70,12 @@ :errors []} properties)) +(defn resolve-properties-children [[properties? & children]] + [(and (map? properties?) properties?) + (if (map? properties?) + children + (cons properties? children))]) + (defn parse-hiccup-element [{:keys [capacities] :as opts} o] (let [explain (spec/explain-data ::form o)] (cond @@ -80,13 +86,17 @@ (or (symbol? o) (utils/primitive? o)) {:data o} (vector? o) - (let [[element properties & children] o - component (resolve-element capacities element) - {:keys [data errors]} (resolve-element-properties capacities properties)] + (let [[element & properties-children] o + [properties children] (resolve-properties-children properties-children) + component (resolve-element capacities element) + {:keys [data errors]} (when properties + (resolve-element-properties capacities properties))] (cond-> (let [m (parse-hiccup-children opts children)] ;; Reduce parsed children to a single map and wrap them in a hiccup element ;; whose component has been translated to the local platform - (update m :data #(apply conj [(or component element) data] %))) + (update m :data #(apply conj (if data [(or component element) data] + [(or component element)]) + %))) (nil? component) (errors/accumulate-errors [(errors/error ::errors/unknown-component element)]) (seq errors) (errors/accumulate-errors errors)))))) diff --git a/test/pluto/reader/views_test.cljc b/test/pluto/reader/views_test.cljc index ab4efd6..6b22aac 100644 --- a/test/pluto/reader/views_test.cljc +++ b/test/pluto/reader/views_test.cljc @@ -28,7 +28,6 @@ (is (= {:data [:text {} "Hello"]} (views/parse {:capacities {:components {'text :text}}} ['text {} "Hello"]))) (is (= {:data [:view - {} [:text {} "Hello"] [blocks/let-block {:env {'cond? '@queries/random-boolean}} @@ -38,7 +37,7 @@ [:text {:style {:color "red"}} "World?"]]]]} (views/parse {:capacities {:components {'text :text 'view :view}}} - '[view {} + '[view [text {} "Hello"] (let [cond? @queries/random-boolean] (if cond?