From edd487cddd2dc52d4b135f969147ca3ee1922d43 Mon Sep 17 00:00:00 2001 From: Eva Marco Date: Wed, 25 Nov 2026 13:58:36 +0100 Subject: [PATCH] :bug: Normalize legacy svg attrs of pasted shapes (#13334) * :bug: Normalize legacy svg attrs of pasted shapes Shapes copied from a version before 2.20 can hold camelCase svg attrs, for example :strokeLinejoin. Files get them normalized by migration 0037 when they load, but clipboard data skips the migrations, so the closed SvgAttrs schema rejected the pasted shape and the whole paste was cancelled. Move the shape normalization of migration 0037 to csvg/normalize-shape and run it on every pasted shape in paste-shapes. Closes #13327 AI-assisted-by: claude-opus-5-5 * :books: Name the shared svg repair in the data model checklist From the review: the checklist still named the withdrawn migration 0035 as the svg repair site. Point it to csvg/normalize-shape, used by migration 0037 and by paste-shapes. AI-assisted-by: claude-opus-5-5 --- .../common/data-model-change-checklist.md | 2 +- common/src/app/common/files/migrations.cljc | 22 +---------- common/src/app/common/svg.cljc | 25 ++++++++++++ common/test/common_tests/svg_test.cljc | 12 ++++++ .../app/main/data/workspace/clipboard.cljs | 7 +++- .../logic/copying_and_duplicating_test.cljs | 38 +++++++++++++++++++ 6 files changed, 84 insertions(+), 22 deletions(-) diff --git a/.serena/memories/common/data-model-change-checklist.md b/.serena/memories/common/data-model-change-checklist.md index 980f406673..462d88f7dd 100644 --- a/.serena/memories/common/data-model-change-checklist.md +++ b/.serena/memories/common/data-model-change-checklist.md @@ -6,7 +6,7 @@ - Do not treat nil as a distinct persisted state from absence. Import/export and cleanup paths may filter nil attrs away. - Avoid Clojure-special naming in exported object attrs, especially boolean names ending in `?`; exported/imported data must survive JSON/SVG/Transit and external tooling. - Any new shape attr that participates in component sync must be listed in `app.common.types.component/sync-attrs` with the correct touched group. Attrs absent from `sync-attrs` are ignored by component synchronization. -- Shape `:svg-attrs` keys are kebab-case (`fill-rule`, not `fillRule`): birth (`app.common.svg/attrs->kebab-props`, applied everywhere data lands on a shape) normalizes any outside spelling through the `svg-props` whitelist, and render boundaries (`get-svg-props`, `render-xml*`, `svg-node*`) translate back to camelCase for React. `:svg-defs` node attrs follow the same rule recursively; svg-raw `:content` attrs are kebab-ized spelling-only, never filtered — the sidebar reads `:stroke-style`, which is outside the whitelist. Non-spec attrs Penpot keeps live in `penpot-extra-attrs` (seeded with `:stroke-style`) and travel in source spelling. Excluded, each with its reason: `:tag` values (React and tag sets need them verbatim; the schema decoder restores keywords from stored strings), outer `:svg-defs` keys (id strings at birth, keywords or strings after JSON decode — accept both), all values (the kebab reader never touches values). Repair sites: birth plus migration `0035-fix-shape-svg-attrs-and-defs` (replaces the withdrawn `0021`); the binfile cleaner carries no svg repair. +- Shape `:svg-attrs` keys are kebab-case (`fill-rule`, not `fillRule`): birth (`app.common.svg/attrs->kebab-props`, applied everywhere data lands on a shape) normalizes any outside spelling through the `svg-props` whitelist, and render boundaries (`get-svg-props`, `render-xml*`, `svg-node*`) translate back to camelCase for React. `:svg-defs` node attrs follow the same rule recursively; svg-raw `:content` attrs are kebab-ized spelling-only, never filtered — the sidebar reads `:stroke-style`, which is outside the whitelist. Non-spec attrs Penpot keeps live in `penpot-extra-attrs` (seeded with `:stroke-style`) and travel in source spelling. Excluded, each with its reason: `:tag` values (React and tag sets need them verbatim; the schema decoder restores keywords from stored strings), outer `:svg-defs` keys (id strings at birth, keywords or strings after JSON decode — accept both), all values (the kebab reader never touches values). Repair sites: birth plus `app.common.svg/normalize-shape`, shared by migration `0037-fix-shape-svg-attrs-and-defs` (replaces the withdrawn `0021` and `0035`) and `paste-shapes` (clipboard data skips migrations, so shapes copied from older versions are normalized on paste); the binfile cleaner carries no svg repair. ## Cross-module update checklist diff --git a/common/src/app/common/files/migrations.cljc b/common/src/app/common/files/migrations.cljc index 1c7b8d82bb..51520c888d 100644 --- a/common/src/app/common/files/migrations.cljc +++ b/common/src/app/common/files/migrations.cljc @@ -2401,26 +2401,8 @@ ;; anything runs. [data _] (some-> cfeat/*new* (swap! conj "fdata/shape-data-type")) - (letfn [(migrate-defs [defs] - (if (map? defs) - (d/mapm (fn [_ node] (csvg/normalize-def-node node)) defs) - defs)) - - (migrate-shape [shape] - (-> shape - (d/update-when :svg-attrs - (fn [attrs] - (if (map? attrs) - (csvg/attrs->kebab-props attrs) - attrs))) - (d/update-when :svg-defs migrate-defs) - (d/update-when :svg-viewbox grc/make-rect) - (d/update-when :svg-transform gmt/coerce) - (cond-> (= :svg-raw (:type shape)) - (d/update-when :content csvg/kebabize-content-node)))) - - (migrate-container [container] - (d/update-when container :objects d/update-vals migrate-shape))] + (letfn [(migrate-container [container] + (d/update-when container :objects d/update-vals csvg/normalize-shape))] (-> data (update :pages-index d/update-vals migrate-container) diff --git a/common/src/app/common/svg.cljc b/common/src/app/common/svg.cljc index 7349e7e1d8..839d914dd2 100644 --- a/common/src/app/common/svg.cljc +++ b/common/src/app/common/svg.cljc @@ -10,6 +10,7 @@ [app.common.data.macros :as dm] [app.common.geom.matrix :as gmt] [app.common.geom.point :as gpt] + [app.common.geom.rect :as grc] [app.common.geom.shapes :as gsh] [app.common.math :as mth] [app.common.uuid :as uuid] @@ -728,6 +729,30 @@ [node] (normalize-node-tree d/kebab-keys node)) +(defn normalize-shape + "Brings the svg data of `shape` to the stored convention: `:svg-attrs` + and `:svg-defs` through the whitelist + kebab transform, `:svg-viewbox` + as a rect, `:svg-transform` as a matrix and svg-raw `:content` with + kebab keys. Shapes written by versions before 2.20 hold camelCase keys + that the closed schema rejects. Already normalized shapes pass through + unchanged." + [shape] + (-> shape + (d/update-when :svg-attrs + (fn [attrs] + (if (map? attrs) + (attrs->kebab-props attrs) + attrs))) + (d/update-when :svg-defs + (fn [defs] + (if (map? defs) + (d/mapm (fn [_ node] (normalize-def-node node)) defs) + defs))) + (d/update-when :svg-viewbox grc/make-rect) + (d/update-when :svg-transform gmt/coerce) + (cond-> (= :svg-raw (:type shape)) + (d/update-when :content kebabize-content-node)))) + (defn update-attr-ids "Replaces the ids inside a property" [attrs replace-fn] diff --git a/common/test/common_tests/svg_test.cljc b/common/test/common_tests/svg_test.cljc index c528e37f98..7e44cffe6e 100644 --- a/common/test/common_tests/svg_test.cljc +++ b/common/test/common_tests/svg_test.cljc @@ -97,3 +97,15 @@ (t/is (= {} (:attrs (svg/normalize-def-node {:tag :g :attrs nil :content []})))) (t/is (not (contains? (svg/kebabize-content-node {:tag :g :content []}) :attrs)) "absent attrs stay absent")) + +(t/deftest normalize-shape-kebabizes-legacy-svg-data + (let [shape {:type :svg-raw + :svg-attrs {:fill "none" :strokeLinejoin "round"} + :svg-defs {"a" {:tag "linearGradient" :attrs {:gradientUnits "userSpaceOnUse"} :content []}} + :content {:tag :path :attrs {:strokeWidth "2"} :content []}} + result (svg/normalize-shape shape)] + (t/is (= {:fill "none" :stroke-linejoin "round"} (:svg-attrs result))) + (t/is (= {:gradient-units "userSpaceOnUse"} (get-in result [:svg-defs "a" :attrs]))) + (t/is (= :linearGradient (get-in result [:svg-defs "a" :tag]))) + (t/is (= {:stroke-width "2"} (get-in result [:content :attrs]))) + (t/is (= result (svg/normalize-shape result)) "normalized shapes pass through unchanged"))) diff --git a/frontend/src/app/main/data/workspace/clipboard.cljs b/frontend/src/app/main/data/workspace/clipboard.cljs index 55cb32fb36..787b6b1bc2 100644 --- a/frontend/src/app/main/data/workspace/clipboard.cljs +++ b/frontend/src/app/main/data/workspace/clipboard.cljs @@ -20,6 +20,7 @@ [app.common.logic.libraries :as cll] [app.common.logic.shapes :as cls] [app.common.schema :as sm] + [app.common.svg :as csvg] [app.common.transit :as t] [app.common.types.component :as ctk] [app.common.types.container :as ctn] @@ -912,7 +913,11 @@ (ptk/reify ::paste-shapes ptk/WatchEvent (watch [it state _] - (let [file-id (:current-file-id state) + (let [;; Clipboard data skips the file migrations: shapes copied + ;; from an older version can hold camelCase svg attrs that + ;; the closed schema rejects, which would cancel the paste. + pdata (update pdata :objects d/update-vals csvg/normalize-shape) + file-id (:current-file-id state) page-id (:current-page-id state) page (dsh/lookup-page state file-id page-id) page-objects (:objects page)] diff --git a/frontend/test/frontend_tests/logic/copying_and_duplicating_test.cljs b/frontend/test/frontend_tests/logic/copying_and_duplicating_test.cljs index b096a96417..d7e2bfa136 100644 --- a/frontend/test/frontend_tests/logic/copying_and_duplicating_test.cljs +++ b/frontend/test/frontend_tests/logic/copying_and_duplicating_test.cljs @@ -657,6 +657,44 @@ done))) +;; Shapes copied from a version before 2.20 hold camelCase svg attrs. +;; Clipboard data skips the file migrations, so the paste must normalize +;; them or the closed schema cancels it (issue #13327). +(t/deftest paste-normalizes-legacy-svg-attrs-in-a-nested-copy + (t/async done + (mock/with-mocks {uuid/next cthi/next-uuid} + (fn [done'] + (let [file (-> (cthf/sample-file :file1 :page-label :page-1) + (ctho/add-simple-component :icon :frame-icon :rect-icon) + (ctho/add-frame :frame-card :name "card") + (cthc/instantiate-component :icon :copy-icon :parent-label :frame-card :children-labels [:copy-rect-icon]) + (cthc/make-component :card :frame-card) + (cthc/instantiate-component :card :copy-card :children-labels [:copy-card-icon])) + page (cthf/current-page file) + shape (cths/get-shape file :copy-card) + pdata (-> (thp/simulate-copy-shape #{(:id shape)} (:objects page) {(:id file) file} page file #{"components/v2"} 46) + (update :objects update-vals + (fn [obj] + (cond-> obj + (= :rect (:type obj)) + (assoc :svg-attrs {:fill "none" :strokeLinejoin "round"}))))) + store (ths/setup-store file) + events [(dws/select-shape uuid/zero) + (dw/paste-shapes pdata)]] + + (ths/run-store + store done' events + (fn [new-state] + (let [file' (ths/get-file-from-state new-state) + page' (cthf/current-page file') + pasted (->> (vals (:objects page')) + (filter #(= :rect (:type %))) + (remove #(contains? (:objects page) (:id %))))] + (t/is (= 1 (count pasted))) + (t/is (= {:fill "none" :stroke-linejoin "round"} + (:svg-attrs (first pasted))))))))) + done))) + ;; Grid tracks in component copies (defn- add-grid-with-child