diff options
| author | Asko Nõmm <asko@nmm.ee> | 2025-09-22 17:41:54 +0300 |
|---|---|---|
| committer | Asko Nõmm <asko@nmm.ee> | 2025-09-22 17:41:54 +0300 |
| commit | 1cde4cbae25e4cd1a79e882f0858f44b664a453b (patch) | |
| tree | 46ab0102f787b71c9f6dac737ee4c2156cbc536f /src/dompa/nodes.cljc | |
| parent | 2362996d9788638e139c3102117dcd2f43e9466b (diff) | |
Improvements to turning nodes into a HTML string, namely added support for attributes, and improvements to the $ macro.
Diffstat (limited to 'src/dompa/nodes.cljc')
| -rw-r--r-- | src/dompa/nodes.cljc | 24 |
1 files changed, 16 insertions, 8 deletions
diff --git a/src/dompa/nodes.cljc b/src/dompa/nodes.cljc index d035116..cf79e21 100644 --- a/src/dompa/nodes.cljc +++ b/src/dompa/nodes.cljc @@ -4,19 +4,27 @@ #{:!doctype :area :base :br :col :embed :hr :img :input :link :meta :source :track :wbr}) +(defn- node-attrs-reducer [attrs k v] + (let [attr-name (-> k name)] + (if (true? v) + (str attrs " " attr-name) + (str attrs " " attr-name "=\"" v "\"")))) + (defn- node->html-reducer-fn [void-nodes nodes->html-fn] (fn [html node] - (cond - (= (-> node :name) :dompa/text) - (str html (-> node :value)) + (let [node-name (-> node :name name) + node-attrs (reduce-kv node-attrs-reducer "" (-> node :attrs))] + (cond + (= (-> node :name) :dompa/text) + (str html (-> node :value)) - (contains? void-nodes (-> node :name)) - (str "<" (-> node :name) ">") + (contains? void-nodes (-> node :name)) + (str html "<" node-name node-attrs">") - :else - (let [value (nodes->html-fn (-> node :children))] - (str "<" (-> node :name) ">" value "</" (-> node :name) ">"))))) + :else + (let [value (nodes->html-fn (-> node :children))] + (str html "<" node-name node-attrs ">" value "</" node-name ">")))))) (defn traverse "Recursively traverses given tree of `nodes` with a `traverser-fn` |
