mirror of
https://github.com/penpot/penpot.git
synced 2026-11-18 16:12:24 +00:00
🐛 Drop map-valued svg attrs when migrating legacy files (#13130)
Files written by old Penpot versions hold maps where an svg attribute value should be, such as a `:filter` that once read `url(#filter_2)`. Since #12006 the `:svg-attrs` schema is closed and admits scalars only, and migration 0035 kept those maps, so the file data check at the end of every migration rejected the file. Such a file can be neither imported nor opened. Two built-in templates, Avataaars and Tutorial for beginners, are among them. `attrs->kebab-props` now drops a value that is not a string, a number, or a boolean, at the top level and inside `:style`. Migration 0035 and every birth path go through it, so stored attrs match the schema. Of the 143 files in penpot/penpot-files, 4 fail to import at `:svg-attrs :filter` before this change, and all 4 import after it. AI-assisted-by: claude-opus-5-5
This commit is contained in:
parent
930f74218c
commit
3b3669b63b
@ -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
|
||||
|
||||
@ -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, ...)`.
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user