From ad98bfddb09c6be641ff6755bb8caefc50f1636d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Asko=20N=C3=B5mm?= Date: Sun, 19 Oct 2025 18:12:40 +0300 Subject: [PATCH] #3: Fixes an issue where if the attribute value contained forward slashes, it would break the attribute parsing. --- src/dompa/coordinates.cljc | 31 +++++++++++++++++-------------- test/dompa/coordinates_test.cljc | 10 ++++++++++ 2 files changed, 27 insertions(+), 14 deletions(-) diff --git a/src/dompa/coordinates.cljc b/src/dompa/coordinates.cljc index ebe9b65..e5ca5e8 100644 --- a/src/dompa/coordinates.cljc +++ b/src/dompa/coordinates.cljc @@ -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: @@ -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 @@ -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 diff --git a/test/dompa/coordinates_test.cljc b/test/dompa/coordinates_test.cljc index 79248e3..1a69114 100644 --- a/test/dompa/coordinates_test.cljc +++ b/test/dompa/coordinates_test.cljc @@ -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}}] + (-> "" + coordinates/compose + coordinates/unify + coordinates/->nodes)))) (testing "Create nodes with attributes" (is (= [{:node/name :div