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
31 changes: 17 additions & 14 deletions src/dompa/coordinates.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -265,13 +265,16 @@
class=\"test\"
```"
[html]
(->> (subs html 1)
(take-while #(not (contains? #{\> \/} %)))
(partition-by #(= % \space))
(drop 1)
flatten
(apply str)
str/trim))
(let [attrs-html (->> (subs html 1)
(take-while #(not (contains? #{\>} %)))
(partition-by #(= % \space))
(drop 1)
flatten
(apply str)
str/trim)]
(if (= \/ (last attrs-html))
(subs attrs-html 0 (dec (count attrs-html)))
attrs-html)))

(defn- html-str->node-attrs
"Turns a given `html` string into an attribute map, e.g:
Expand All @@ -294,10 +297,10 @@
:has-attrs? false
:attrs []}]
(as-> (html->str->node-attrs-reducer-fn attrs-html) $
(reduce $ default-reducer-state indexed-attrs-html)
(remove str/blank? (:attrs $))
(map parse-html-attr-str $)
(into {} $)))))
(reduce $ default-reducer-state indexed-attrs-html)
(remove str/blank? (:attrs $))
(map parse-html-attr-str $)
(into {} $)))))

(defn- construct-node
"Constructs a node map from `node-html` string and
Expand All @@ -306,9 +309,9 @@
(let [node-name (html-str->node-name node-html)
node-attrs (html-str->node-attrs node-html)]
(cond-> {:node/name node-name}
(= node-name :dompa/text) (assoc :node/value node-html)
(not (nil? node-attrs)) (assoc :node/attrs node-attrs)
(not (nil? node-children)) (assoc :node/children node-children))))
(= node-name :dompa/text) (assoc :node/value node-html)
(not (nil? node-attrs)) (assoc :node/attrs node-attrs)
(not (nil? node-children)) (assoc :node/children node-children))))

(defn ->nodes
"Transform given `html` according to given `coordinates` into
Expand Down
10 changes: 10 additions & 0 deletions test/dompa/coordinates_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,16 @@
coordinates/compose
coordinates/unify
coordinates/->nodes))))

(testing "Parse attributes with forward slashes in them"
(is (= [{:node/name :meta,
:node/attrs {:name "route-pattern",
:content "/:user_id/:repository",
:data-turbo-transient true}}]
(-> "<meta name=\"route-pattern\" content=\"/:user_id/:repository\" data-turbo-transient>"
coordinates/compose
coordinates/unify
coordinates/->nodes))))

(testing "Create nodes with attributes"
(is (= [{:node/name :div
Expand Down