Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions src/dompa/nodes.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
(let [value (nodes->html-fn (-> node :node/children))]
(str html "<" node-name node-attrs ">" value "</" node-name ">"))))

:else "")))
:else html)))

(defn traverse
"Recursively traverses given tree of `nodes` with a `traverser-fn`
Expand Down Expand Up @@ -149,15 +149,18 @@

(defn- nodes-from-opt
[opt]
(cond (map? opt)
(cond (nil? opt)
nil

(map? opt)
opt

(empty-seq? opt)
nil

(list-of-many? opt)
{:node/name :<>
:node/children opt}
:node/children (remove nil? opt)}

(list-of-one? opt)
(first opt)
Expand All @@ -176,7 +179,9 @@
children-opts (if attrs? (drop 2 opts) (rest opts))
children-nodes (->> children-opts
(map nodes-from-opt)
flatten)]
flatten
(remove nil?)
vec)]
(cond-> {:node/name first-opt}
attrs? (assoc :node/attrs attrs)
(seq children-nodes) (assoc :node/children children-nodes))))
Expand Down
38 changes: 38 additions & 0 deletions test/dompa/nodes_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,44 @@
($ x))
["l" "d"])])]))))))

(deftest nil-in-hierarchy-does-not-remove-siblings-test
(testing "nil in a fragment should not remove sibling elements"
(is (= "<div>one</div><div>two</div>"
(nodes/->html
[($ :<>
($ :div "one")
($ :div "two")
nil)]))))

(testing "nil between siblings should not affect them"
(is (= "<div>one</div><div>two</div>"
(nodes/->html
[($ :<>
($ :div "one")
nil
($ :div "two"))]))))

(testing "multiple nils should not affect siblings"
(is (= "<div>one</div><div>two</div><div>three</div>"
(nodes/->html
[($ :<>
nil
($ :div "one")
nil
($ :div "two")
nil
($ :div "three")
nil)]))))

(testing "nil inside nested element should not affect parent siblings"
(is (= "<div><span>hello</span></div><div>world</div>"
(nodes/->html
[($ :<>
($ :div
($ :span "hello")
nil)
($ :div "world"))])))))

(deftest traverse-test
(let [traverser-fn (fn [node]
(if (= :dompa/text (:node/name node))
Expand Down