diff --git a/common/src/app/common/files/migrations.cljc b/common/src/app/common/files/migrations.cljc index 8f2958f67d..aaaf3a6945 100644 --- a/common/src/app/common/files/migrations.cljc +++ b/common/src/app/common/files/migrations.cljc @@ -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"])) diff --git a/common/test/common_tests/files_migrations_0039_test.cljc b/common/test/common_tests/files_migrations_0039_test.cljc new file mode 100644 index 0000000000..4c84595107 --- /dev/null +++ b/common/test/common_tests/files_migrations_0039_test.cljc @@ -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'))))) diff --git a/common/test/common_tests/runner.cljc b/common/test/common_tests/runner.cljc index 401e9dec2b..cadec62289 100644 --- a/common/test/common_tests/runner.cljc +++ b/common/test/common_tests/runner.cljc @@ -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