diff --git a/common/src/app/common/svg.cljc b/common/src/app/common/svg.cljc index 15a3bff12d..7349e7e1d8 100644 --- a/common/src/app/common/svg.cljc +++ b/common/src/app/common/svg.cljc @@ -673,15 +673,32 @@ {} attrs))) +(defn- drop-non-scalar-values + "Removes the entries whose value is not a string, a number or a + boolean: the stored schema admits nothing else." + [attrs] + (reduce-kv (fn [res k v] + (if (or (string? v) (number? v) (boolean? v)) + res + (dissoc res k))) + attrs + attrs)) + (defn attrs->kebab-props "Like `attrs->props` (same whitelist, trim and `:style` parsing) but with kebab-case keys, the stored convention for `:svg-attrs` and - `:svg-defs` node attrs. Kebab is a fixed point: running it twice - returns the same map." + `:svg-defs` node attrs. Values that are not scalars, at the top level + or inside `:style`, are dropped: files written by old versions hold + maps there, which no renderer can use and the closed schema rejects. + Kebab is a fixed point: running it twice returns the same map." [attrs] - (-> attrs - (attrs->props) - (d/kebab-keys))) + (let [props (attrs->props attrs) + style (some-> (:style props) (drop-non-scalar-values) (not-empty))] + (-> props + (dissoc :style) + (drop-non-scalar-values) + (cond-> (some? style) (assoc :style style)) + (d/kebab-keys)))) (defn- normalize-node-tree "Walks a svg node tree applying `attrs-fn` to every `:attrs` map. Tags diff --git a/common/test/common_tests/files_migrations_0035_test.cljc b/common/test/common_tests/files_migrations_0035_test.cljc index 350b669fed..078bc895e4 100644 --- a/common/test/common_tests/files_migrations_0035_test.cljc +++ b/common/test/common_tests/files_migrations_0035_test.cljc @@ -367,6 +367,31 @@ (t/is (= :mask (get-in file' [:data :pages-index page-id :objects rect-id :svg-defs :a :tag]))) (t/is (= :g (get-in file' [:data :pages-index page-id :objects raw-id :content :tag]))))) +(t/deftest migration-0035-drops-map-valued-svg-attrs + ;; Files exported by old Penpot versions hold maps where an svg attr + ;; value should be, such as a `:filter` that once read + ;; `url(#filter_2)`. The closed `:svg-attrs` schema admits scalars + ;; only, so `migrate-file` must drop them or the file cannot be + ;; imported or opened. The values below are copied from the + ;; "Avataaars" and "Tutorial for beginners" templates. + (let [shape-id (uuid/next) + file (ctf/make-file {:name "Map-valued svg attrs"}) + page-id (first (get-in file [:data :pages])) + shape (-> (cts/setup-shape {:id shape-id :type :rect :name "rect"}) + (assoc :svg-attrs {:fillRule "evenodd" + :filter {(keyword "{") "{ \"url(#filter_2) nil}\""} + :mask {(keyword "url(-filter0-d-16837-160)") nil} + :style {:fill "red" + :filter {:a "b"}}})) + file (-> file + (assoc :migrations (disj cfm/available-migrations migration-id)) + (update-in [:data :pages-index page-id :objects] assoc shape-id shape) + (update-in [:data :pages-index page-id :objects uuid/zero :shapes] conj shape-id)) + file' (cfm/migrate-file file {}) + attrs (get-in file' [:data :pages-index page-id :objects shape-id :svg-attrs])] + (t/is (= {:fill-rule "evenodd" :style {:fill "red"}} attrs) + "map values are dropped, scalars are kept"))) + (t/deftest migration-0035-turns-plain-map-svg-transform-into-matrix ;; The svg defs renderer multiplies `:svg-transform` as a matrix; a ;; plain map there renders as `matrix(NaN, ...)`.