0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-01-08 07:50:43 -05:00

Split path/selector for disallowing creating tokens at path segments

This commit is contained in:
Florian Schroedl 2024-07-01 10:16:15 +02:00
parent a98f59469e
commit 4a85ef3608
2 changed files with 32 additions and 15 deletions

View file

@ -9,6 +9,18 @@
[token-name] [token-name]
(str/split token-name #"\.+")) (str/split token-name #"\.+"))
(defn token-name->path-selector
"Splits token-name into map with `:path` and `:selector` using `token-name->path`.
`:selector` is the last item of the names path
`:path` is everything leading up the the `:selector`."
[token-name]
(let [path-segments (token-name->path token-name)
last-idx (dec (count path-segments))
[path [selector]] (split-at last-idx path-segments)]
{:path (seq path)
:selector selector}))
(defn token-names-tree (defn token-names-tree
"Convert tokens into a nested tree with their `:name` as the path." "Convert tokens into a nested tree with their `:name` as the path."
[tokens] [tokens]
@ -31,10 +43,11 @@
{\"foo\" {:name \"other\"}}" {\"foo\" {:name \"other\"}}"
[token-name token-names-tree] [token-name token-names-tree]
(let [name-path (token-name->path token-name) (let [{:keys [path selector]} (token-name->path-selector token-name)
result (reduce path-target (reduce
(fn [acc cur] (fn [acc cur]
(let [target (get acc cur)] (let [target (get acc cur)]
(prn target cur)
(cond (cond
;; Path segment doesn't exist yet ;; Path segment doesn't exist yet
(nil? target) (reduced false) (nil? target) (reduced false)
@ -42,7 +55,10 @@
(:name target) (reduced true) (:name target) (reduced true)
;; Continue traversing the true ;; Continue traversing the true
:else target))) :else target)))
token-names-tree name-path)] token-names-tree path)]
(if (map? result) (cond
(some? (:name result)) (boolean? path-target) path-target
result))) (get path-target :name) true
:else (-> (get path-target selector)
(seq)
(boolean)))))

View file

@ -28,6 +28,7 @@
:value "{foo.bar.baz}"}})))) :value "{foo.bar.baz}"}}))))
(t/deftest token-name-path-exists?-test (t/deftest token-name-path-exists?-test
(t/is (true? (wtt/token-name-path-exists? "border-radius" {"border-radius" {"sm" {:name "sm"}}})))
(t/is (true? (wtt/token-name-path-exists? "border-radius" {"border-radius" {:name "sm"}}))) (t/is (true? (wtt/token-name-path-exists? "border-radius" {"border-radius" {:name "sm"}})))
(t/is (true? (wtt/token-name-path-exists? "border-radius.sm" {"border-radius" {:name "sm"}}))) (t/is (true? (wtt/token-name-path-exists? "border-radius.sm" {"border-radius" {:name "sm"}})))
(t/is (true? (wtt/token-name-path-exists? "border-radius.sm.x" {"border-radius" {:name "sm"}}))) (t/is (true? (wtt/token-name-path-exists? "border-radius.sm.x" {"border-radius" {:name "sm"}})))