mirror of
https://github.com/penpot/penpot.git
synced 2026-11-21 01:22:01 +00:00
🐛 Repair svg attributes an old reader split into maps
Two dashboard templates, Avataaars and Tutorial for beginners, hold the
filter attribute of some shapes as a map an old reader split out of its
text, as `{:url(#filter_2) nil}` for `url(#filter_2)`. The svg attribute
schema takes only scalars outside `:style`, so their import failed the
file validation after migrating.
Migration 0039 turns such a map back into the reference to one of the
shape's defs it names, matched to the defs id as stored though 0035 may
have kebab-cased the map, and drops any other map value. Both templates
then import valid, with their filters.
The benchmark is not run: the change is a file migration.
AI-assisted-by: claude-opus-5-5
This commit is contained in:
parent
08069b8793
commit
65da6b1404
@ -2394,6 +2394,44 @@
|
||||
{})]
|
||||
(cfcp/convert-text-overrides data libraries)))
|
||||
|
||||
(defmethod migrate-data "0039-repair-svg-attr-maps"
|
||||
;; Some imported files hold an svg attribute other than `:style` as a map
|
||||
;; an old reader split out of its text, as `{:url(#filter_2) nil}` for
|
||||
;; `filter: url(#filter_2)`, which the attribute schema, taking only
|
||||
;; scalars there, rejects. A reference to one of the shape's defs becomes
|
||||
;; that reference again, to the defs id as stored, which 0035 may have
|
||||
;; kebab-cased in the map; any other map value drops.
|
||||
[data _]
|
||||
(letfn [(id-key [id]
|
||||
(-> (str id) (str/replace #"^[#-]+" "") (str/replace "_" "-") (str/lower)))
|
||||
|
||||
(def-reference [shape value]
|
||||
(when-let [[_ id] (re-find #"url\(#?([^)\s\"]+)\)" (pr-str value))]
|
||||
(when-let [def-id (some #(when (= (id-key %) (id-key id)) %) (keys (:svg-defs shape)))]
|
||||
(str "url(#" def-id ")"))))
|
||||
|
||||
(repair-attrs [shape attrs]
|
||||
(reduce-kv (fn [attrs k v]
|
||||
(if (and (map? v) (not= k :style))
|
||||
(if-let [reference (def-reference shape v)]
|
||||
(assoc attrs k reference)
|
||||
(dissoc attrs k))
|
||||
attrs))
|
||||
attrs
|
||||
attrs))
|
||||
|
||||
(migrate-shape [shape]
|
||||
(if (map? (:svg-attrs shape))
|
||||
(update shape :svg-attrs #(repair-attrs shape %))
|
||||
shape))
|
||||
|
||||
(migrate-container [container]
|
||||
(d/update-when container :objects d/update-vals migrate-shape))]
|
||||
|
||||
(-> data
|
||||
(update :pages-index d/update-vals migrate-container)
|
||||
(d/update-when :components d/update-vals migrate-container))))
|
||||
|
||||
(def available-migrations
|
||||
(into (d/ordered-set)
|
||||
["legacy-2"
|
||||
@ -2488,4 +2526,5 @@
|
||||
"0035-fix-shape-svg-attrs-and-defs"
|
||||
"0036-convert-swap-slots-to-swap-groups"
|
||||
"0037-recompute-text-overrides"
|
||||
"0038-recompute-content-group-alone"]))
|
||||
"0038-recompute-content-group-alone"
|
||||
"0039-repair-svg-attr-maps"]))
|
||||
|
||||
54
common/test/common_tests/files_migrations_0039_test.cljc
Normal file
54
common/test/common_tests/files_migrations_0039_test.cljc
Normal file
@ -0,0 +1,54 @@
|
||||
;; This Source Code Form is subject to the terms of the Mozilla Public
|
||||
;; License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
;; file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
;;
|
||||
;; Copyright (c) KALEIDOS SUBSIDIARY SL
|
||||
|
||||
(ns common-tests.files-migrations-0039-test
|
||||
(:require
|
||||
[app.common.files.migrations :as cfm]
|
||||
[app.common.uuid :as uuid]
|
||||
[clojure.test :as t]))
|
||||
|
||||
;; 0039-repair-svg-attr-maps
|
||||
;; Some imported files hold an svg attribute other than `:style` as a map an
|
||||
;; old reader split out of its text. A reference to one of the shape's defs
|
||||
;; becomes that reference again; any other map value drops.
|
||||
|
||||
(def ^:private migration-id
|
||||
"0039-repair-svg-attr-maps")
|
||||
|
||||
(defn- migrate-attrs
|
||||
"The `:svg-attrs` the migration leaves on a page shape with `attrs` and
|
||||
`defs`, and on a deleted component's shape alike."
|
||||
[attrs defs]
|
||||
(let [shape-id (uuid/next)
|
||||
page-id (uuid/next)
|
||||
component-id (uuid/next)
|
||||
shape {:id shape-id :type :rect :svg-attrs attrs :svg-defs defs}
|
||||
data {:pages-index {page-id {:objects {shape-id shape}}}
|
||||
:components {component-id {:objects {shape-id shape}}}}
|
||||
data' (cfm/migrate-data data migration-id)]
|
||||
[(get-in data' [:pages-index page-id :objects shape-id :svg-attrs])
|
||||
(get-in data' [:components component-id :objects shape-id :svg-attrs])]))
|
||||
|
||||
(t/deftest migration-0039-restores-a-split-defs-reference
|
||||
(let [defs {"filter0_d_16837_160" {:tag :filter :attrs {:id "filter0_d_16837_160"}}}]
|
||||
(doseq [value [{(keyword "url(#filter0_d_16837_160)") nil}
|
||||
;; as 0035 left it, kebab-cased
|
||||
{(keyword "url(-filter0-d-16837-160)") nil}]
|
||||
attrs (migrate-attrs {:filter value :fill "none"} defs)]
|
||||
(t/is (= {:filter "url(#filter0_d_16837_160)" :fill "none"} attrs)))))
|
||||
|
||||
(t/deftest migration-0039-drops-other-map-values-and-keeps-style
|
||||
(let [style {:fill "red"}]
|
||||
(doseq [attrs (migrate-attrs {:filter {(keyword "url(#missing)") nil}
|
||||
:mask {:a 1}
|
||||
:style style}
|
||||
{})]
|
||||
(t/is (= {:style style} attrs)))))
|
||||
|
||||
(t/deftest migration-0039-leaves-scalar-attrs-alone
|
||||
(let [attrs {:filter "url(#f)" :fill "none"}]
|
||||
(doseq [attrs' (migrate-attrs attrs {"f" {:tag :filter :attrs {:id "f"}}})]
|
||||
(t/is (= attrs attrs')))))
|
||||
@ -25,6 +25,7 @@
|
||||
[common-tests.files-migrations-0026-test]
|
||||
[common-tests.files-migrations-0034-test]
|
||||
[common-tests.files-migrations-0035-test]
|
||||
[common-tests.files-migrations-0039-test]
|
||||
[common-tests.files-migrations-test]
|
||||
[common-tests.files.comp-processors-test]
|
||||
[common-tests.files.convert-swap-slots-test]
|
||||
@ -165,6 +166,7 @@
|
||||
'common-tests.files-migrations-0026-test
|
||||
'common-tests.files-migrations-0034-test
|
||||
'common-tests.files-migrations-0035-test
|
||||
'common-tests.files-migrations-0039-test
|
||||
'common-tests.files-migrations-test
|
||||
'common-tests.files.comp-processors-test
|
||||
'common-tests.files.convert-swap-slots-test
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user