diff --git a/common/src/app/common/files/builder.cljc b/common/src/app/common/files/builder.cljc index 2e02166a93..c5b769121f 100644 --- a/common/src/app/common/files/builder.cljc +++ b/common/src/app/common/files/builder.cljc @@ -86,7 +86,7 @@ (defn- unique-name [name state] (let [container-id (::current-page-id state) - unames (dm/get-in state [:unames container-id])] + unames (dm/get-in state [::unames container-id])] (d/unique-name name (or unames #{})))) (defn- clear-names [file] @@ -462,12 +462,24 @@ frame-id (or frame-id (get state ::current-frame-id)) + name + (or name "anonymous") + + path + (d/nilv path "") + + ;; The main instance root name must match the component's path+name, so + ;; the frame is renamed if needed. This keeps the builder output valid + ;; under the :invalid-main-instance-name integrity check. + normalized-name + (cpn/merge-path-item path name) + change1 (d/without-nils {:type :add-component :id component-id - :name (or name "anonmous") - :path (d/nilv path "") + :name name + :path path :main-instance-id frame-id :main-instance-page page-id :variant-id variant-id @@ -481,11 +493,13 @@ [{:type :set :attr :component-root :val true} {:type :set :attr :main-instance :val true} {:type :set :attr :component-id :val component-id} - {:type :set :attr :component-file :val file-id}]}] + {:type :set :attr :component-file :val file-id} + {:type :set :attr :name :val normalized-name}]}] (-> state (commit-change change1) - (commit-change change2)))) + (commit-change change2) + (track-used-name normalized-name)))) (defn add-tokens-lib [state tokens-lib] diff --git a/common/src/app/common/files/shapes_builder.cljc b/common/src/app/common/files/shapes_builder.cljc index 9577fb528d..61826488b7 100644 --- a/common/src/app/common/files/shapes_builder.cljc +++ b/common/src/app/common/files/shapes_builder.cljc @@ -18,6 +18,7 @@ [app.common.geom.shapes.common :as gsc] [app.common.geom.shapes.transforms :as gst] [app.common.math :as mth] + [app.common.path-names :as cpn] [app.common.schema :as sm :refer [max-safe-int min-safe-int]] [app.common.svg :as csvg] [app.common.types.color :as clr] @@ -196,7 +197,7 @@ (let [[vb-x vb-y vb-width vb-height] (svg-dimensions svg-data) unames (cfh/get-used-names objects) - svg-name (str/replace (:name svg-data) ".svg" "") + svg-name (cpn/remove-extension (:name svg-data)) svg-data (-> svg-data (assoc :x (mth/round diff --git a/common/src/app/common/logic/libraries.cljc b/common/src/app/common/logic/libraries.cljc index 16bf550dcf..3a0c2949ff 100644 --- a/common/src/app/common/logic/libraries.cljc +++ b/common/src/app/common/logic/libraries.cljc @@ -1146,26 +1146,69 @@ initial-root? (:component-root shape-inst) + ;; The component name is normalized (path separators with one space at + ;; each side), so the name synced to the main instance root must be + ;; normalized too. Otherwise the root would not match the component's + ;; path+name and the file would fail :invalid-main-instance-name. + rename? (and (some? component) + (contains? (:touched shape-inst) :name-group)) + original-name (:name shape-inst) + clean-name (when rename? + (cpn/clean-path original-name)) + shape-inst (cond-> shape-inst + (some? clean-name) + (assoc :name clean-name)) + root-inst shape-inst root-main (ctf/get-component-root library component) - changes (cond-> changes - (and component (contains? (:touched shape-inst) :name-group)) - (generate-rename-component (:component-id shape-inst) (:name shape-inst) library))] + ;; When the synced shape is itself the component's main instance root, + ;; `find-remote-shape` returns nil and the recursive sync never reaches + ;; it, so it must be renamed explicitly to keep it matching the + ;; component's path+name. + main-root? (and (some? component) + (= (:id shape-inst) (:main-instance-id component))) - (if component - (generate-sync-shape-inverse-recursive changes - container - shape-inst - component - library - file - libraries - shape-main - root-inst - root-main - initial-root?) - changes))) + changes (cond-> changes + (some? clean-name) + (generate-rename-component (:component-id shape-inst) clean-name library)) + + changes (if component + (generate-sync-shape-inverse-recursive changes + container + shape-inst + component + library + file + libraries + shape-main + root-inst + root-main + initial-root?) + changes) + + changes (cond-> changes + (and (some? clean-name) + main-root? + (not= clean-name original-name)) + (-> (update :redo-changes conj + (make-change container + {:type :mod-obj + :id (:id shape-inst) + :local-change? true + :operations [{:type :set + :attr :name + :val clean-name}]})) + (update :undo-changes conj + (make-change container + {:type :mod-obj + :id (:id shape-inst) + :local-change? true + :operations [{:type :set + :attr :name + :val original-name}]}))))] + + changes)) (defn- generate-sync-shape-inverse-recursive [changes container shape-inst component library file libraries shape-main root-inst root-main initial-root?] @@ -2694,10 +2737,20 @@ [path name] (cpn/split-group-name name) + ;; The name of the main instance root must match the path+name of the + ;; component. The component name is normalized here (via split-group-name), + ;; so the root shape name must be normalized too. Otherwise creating a + ;; component from a shape with an unnormalized name (e.g. "Group/Subgroup") + ;; would leave a main instance failing :invalid-main-instance-name. + normalized-name (cpn/merge-path-item path name) + [root-shape updated-shapes] - (ctn/convert-shape-in-component root objects file-id) + (ctn/convert-shape-in-component (assoc root :name normalized-name) objects file-id) changes (-> changes + (cond-> (not= normalized-name (:name root)) + (pcb/update-shapes [(:id root)] #(assoc % :name normalized-name) + {:ignore-touched true})) (pcb/add-component (:id root-shape) path name diff --git a/common/src/app/common/path_names.cljc b/common/src/app/common/path_names.cljc index 3f5fd28603..a81b6cfaee 100644 --- a/common/src/app/common/path_names.cljc +++ b/common/src/app/common/path_names.cljc @@ -147,6 +147,20 @@ Some naming conventions: [(subs s 0 (inc last-period)) (subs s (inc last-period))] [s ""])) +(defn remove-extension + "Removes the extension (the text after the last period, including the + period itself) from a name: + 'photo.svg' -> 'photo' + 'icon.SVG' -> 'icon' + 'my.svg.icon.svg' -> 'my.svg.icon' + 'archive.tar.gz' -> 'archive.tar' + 'design' -> 'design'" + [name] + (let [[base ext] (split-by-last-period name)] + (if (str/empty? ext) + name + (subs base 0 (dec (count base)))))) + ;; Tree building functions -------------------------------------------------- (defn- attach-remaining-path diff --git a/common/test/common_tests/files/shapes_builder_test.cljc b/common/test/common_tests/files/shapes_builder_test.cljc index 614ff7cf77..44e6560f36 100644 --- a/common/test/common_tests/files/shapes_builder_test.cljc +++ b/common/test/common_tests/files/shapes_builder_test.cljc @@ -91,3 +91,20 @@ ;; stroke-style is not set at import time (nil means default solid rendering) (t/is (nil? (:stroke-style stroke))))))) + +;; Regression for https://github.com/penpot/penpot/issues/12773: the SVG +;; root shape name must have any extension stripped (not only ".svg"), +;; so the component name and the main instance root name match. +(t/deftest create-svg-shapes-strips-name-extension + (let [svg-data {:name "my.svg.icon.SVG" + :tag :svg + :attrs {:xmlns "http://www.w3.org/2000/svg" + :width "24" :height "24" + :viewBox "0 0 24 24"} + :content [{:tag :rect + :attrs {:x "0" :y "0" :width "24" :height "24"} + :content []}]} + [root _children] (sb/create-svg-shapes svg-data {:x 0 :y 0} {} nil nil #{} false)] + + (t/is (= "my.svg.icon" (:name root))))) + diff --git a/common/test/common_tests/files_builder_test.cljc b/common/test/common_tests/files_builder_test.cljc index 23891a583b..5a35829d56 100644 --- a/common/test/common_tests/files_builder_test.cljc +++ b/common/test/common_tests/files_builder_test.cljc @@ -7,6 +7,8 @@ (ns common-tests.files-builder-test (:require [app.common.files.builder :as fb] + [app.common.files.validate :as cfv] + [app.common.path-names :as cpn] [app.common.uuid :as uuid] [clojure.test :as t])) @@ -87,3 +89,30 @@ :height 10})) shape (fb/get-shape state shape-id)] (t/is (= "Group / Subgroup / Name" (:name shape))))) + +(t/deftest add-component-renames-frame-to-match-component + (t/testing "the main instance frame name is aligned with the component's path+name" + (let [file-id (uuid/next) + page-id (uuid/next) + frame-id (uuid/next) + component-id (uuid/next) + state (-> (fb/create-state) + (fb/add-file {:id file-id :name "Test file"}) + (fb/add-page {:id page-id :name "Page 1"}) + (fb/add-board {:id frame-id + :name "Wrong / Frame / Name" + :x 0 + :y 0 + :width 10 + :height 10}) + (fb/add-component {:component-id component-id + :name "Component" + :path "Group / Subgroup"})) + shape (fb/get-shape state frame-id) + file-data (get-in state [::fb/files file-id :data]) + component (get-in file-data [:components component-id]) + errors (cfv/validate-file {:id file-id :data file-data} {})] + + (t/is (= "Group / Subgroup / Component" (:name shape))) + (t/is (= (:name shape) (cpn/merge-path-item (:path component) (:name component)))) + (t/is (nil? errors))))) diff --git a/common/test/common_tests/logic/comp_creation_test.cljc b/common/test/common_tests/logic/comp_creation_test.cljc index 95f158e2a3..eb0fb36d01 100644 --- a/common/test/common_tests/logic/comp_creation_test.cljc +++ b/common/test/common_tests/logic/comp_creation_test.cljc @@ -9,6 +9,7 @@ [app.common.data :as d] [app.common.files.changes-builder :as pcb] [app.common.files.shapes-helpers :as cfsh] + [app.common.files.validate :as cfv] [app.common.geom.point :as gpt] [app.common.logic.libraries :as cll] [app.common.logic.shapes :as cls] @@ -91,6 +92,37 @@ (t/is (ctk/main-instance? root)) (t/is (ctk/main-instance-of? (:id root) (:id page) component)))) +(t/deftest test-add-component-normalizes-root-name + (t/testing "main instance root name matches the component even if the source name is not normalized" + (let [;; ==== Setup + file (-> (thf/sample-file :file1) + (ths/add-sample-shape :frame1 :type :frame :name "Group/Subgroup / Name")) + + page (thf/current-page file) + frame1 (ths/get-shape file :frame1) + + ;; ==== Action + [_ component-id changes] + (cll/generate-add-component (pcb/empty-changes) + [frame1] + (:objects page) + (:id page) + (:id file) + nil) + + file' (thf/apply-changes file changes) + + ;; ==== Get + component (thc/get-component-by-id file' component-id) + root (ths/get-shape-by-id file' (:main-instance-id component)) + errors (cfv/validate-file file' {})] + + ;; ==== Check + (t/is (= "Group / Subgroup" (:path component))) + (t/is (= "Name" (:name component))) + (t/is (= "Group / Subgroup / Name" (:name root))) + (t/is (nil? errors))))) + (t/deftest test-add-component-from-several-shapes (let [;; ==== Setup file (-> (thf/sample-file :file1) diff --git a/common/test/common_tests/logic/comp_sync_test.cljc b/common/test/common_tests/logic/comp_sync_test.cljc index a929c7a616..f44d526b33 100644 --- a/common/test/common_tests/logic/comp_sync_test.cljc +++ b/common/test/common_tests/logic/comp_sync_test.cljc @@ -8,16 +8,20 @@ (:require [app.common.data :as d] [app.common.files.changes-builder :as pcb] + [app.common.files.validate :as cfv] [app.common.geom.point :as gpt] [app.common.geom.shapes :as gsh] [app.common.logic.libraries :as cll] [app.common.logic.shapes :as cls] + [app.common.path-names :as cpn] [app.common.test-helpers.components :as thc] [app.common.test-helpers.compositions :as tho] [app.common.test-helpers.files :as thf] [app.common.test-helpers.ids-map :as thi] [app.common.test-helpers.shapes :as ths] [app.common.types.component :as ctk] + [app.common.types.components-list :as ctkl] + [app.common.types.container :as ctn] [app.common.types.shape-tree :as ctst] [clojure.test :as t])) @@ -539,3 +543,64 @@ ;; A position-only change in the main component must not propagate to copies ;; and therefore must produce no redo-changes. (t/is (empty? (:redo-changes sync-changes))))) + +(defn- run-sync-inverse + "Run the inverse sync on `shape-tag`, which has been renamed to an + unnormalized name, and return the resulting main root and component + names, and the updated file." + [shape-tag] + (let [file (-> (thf/sample-file :file1) + (tho/add-simple-component-with-copy :component1 + :main-root + :main-child + :copy-root) + (tho/change-shape shape-tag #(update % :touched (fnil conj #{}) :name-group)) + (ths/update-shape shape-tag :name "Group/Subgroup")) + page (thf/current-page file) + container (ctn/make-container page :page) + changes (-> (pcb/empty-changes) + (pcb/with-container container) + (cll/generate-sync-shape-inverse (:data file) + {(:id file) file} + container + (thi/id shape-tag))) + file' (thf/apply-changes file changes) + main-root' (ths/get-shape file' :main-root) + component' (ctkl/get-component (:data file') (thi/id :component1))] + [(:name main-root') + (cpn/merge-path-item (:path component') (:name component')) + file'])) + +(t/deftest test-sync-inverse-normalizes-copy-name + (t/testing "syncing an unnormalized copy name keeps the component and its main root matching" + (let [file (-> (thf/sample-file :file1) + (tho/add-simple-component-with-copy :component1 + :main-root + :main-child + :copy-root) + (tho/change-shape :copy-root #(update % :touched (fnil conj #{}) :name-group)) + (ths/update-shape :copy-root :name "Group/Subgroup")) + page (thf/current-page file) + container (ctn/make-container page :page) + changes (-> (pcb/empty-changes) + (pcb/with-container container) + (cll/generate-sync-shape-inverse (:data file) + {(:id file) file} + container + (thi/id :copy-root))) + file' (thf/apply-changes file changes) + main-root' (ths/get-shape file' :main-root) + component' (ctkl/get-component (:data file') (thi/id :component1))] + + ;; ==== Check + (t/is (= "Group / Subgroup" (:name main-root'))) + (t/is (= "Group / Subgroup" (cpn/merge-path-item (:path component') (:name component')))) + (t/is (nil? (cfv/validate-file file' {})))))) + +(t/deftest test-sync-inverse-normalizes-main-name + (t/testing "syncing an unnormalized main name keeps the component and the main root matching" + (let [[main-root-name component-name file'] (run-sync-inverse :main-root)] + ;; ==== Check + (t/is (= "Group / Subgroup" main-root-name)) + (t/is (= "Group / Subgroup" component-name)) + (t/is (nil? (cfv/validate-file file' {})))))) diff --git a/common/test/common_tests/path_names_test.cljc b/common/test/common_tests/path_names_test.cljc index 04cf0650b8..b6dde08be0 100644 --- a/common/test/common_tests/path_names_test.cljc +++ b/common/test/common_tests/path_names_test.cljc @@ -9,6 +9,14 @@ [app.common.path-names :as cpn] [clojure.test :as t])) +(t/deftest remove-extension + (t/is (= "photo" (cpn/remove-extension "photo.svg"))) + (t/is (= "icon" (cpn/remove-extension "icon.SVG"))) + (t/is (= "my.svg.icon" (cpn/remove-extension "my.svg.icon.svg"))) + (t/is (= "archive.tar" (cpn/remove-extension "archive.tar.gz"))) + (t/is (= "design" (cpn/remove-extension "design"))) + (t/is (= "" (cpn/remove-extension ".svg")))) + (t/deftest split-group-name (t/is (= ["foo" "bar"] (cpn/split-group-name "foo/bar"))) (t/is (= ["" "foo"] (cpn/split-group-name "foo"))) diff --git a/frontend/src/app/main/data/workspace/media.cljs b/frontend/src/app/main/data/workspace/media.cljs index f3a395a504..2ef99f761f 100644 --- a/frontend/src/app/main/data/workspace/media.cljs +++ b/frontend/src/app/main/data/workspace/media.cljs @@ -14,6 +14,7 @@ [app.common.files.shapes-builder :as sb] [app.common.math :as mth] [app.common.media :as media] + [app.common.path-names :as cpn] [app.common.schema :as sm] [app.common.types.container :as ctn] [app.common.types.fills :as types.fills] @@ -435,10 +436,12 @@ fdata (dsh/lookup-file-data state file-id) page (dsh/get-page fdata page-id) - pos (dsh/get-viewport-center state)] + pos (dsh/get-viewport-center state) + + name (cpn/remove-extension (:name svg-data))] (->> (create-shapes-svg file-id (:objects page) pos svg-data) - (rx/map (partial add-shapes-and-component it fdata page (:name svg-data)))))))) + (rx/map (partial add-shapes-and-component it fdata page name))))))) (defn upload-media-components [params] diff --git a/frontend/test/frontend_tests/data/workspace_media_test.cljs b/frontend/test/frontend_tests/data/workspace_media_test.cljs index cabdd54abb..03e80547c3 100644 --- a/frontend/test/frontend_tests/data/workspace_media_test.cljs +++ b/frontend/test/frontend_tests/data/workspace_media_test.cljs @@ -8,12 +8,24 @@ "Integration tests for the chunked-upload logic in app.main.data.workspace.media." (:require + [app.common.path-names :as cpn] + [app.common.test-helpers.files :as cthf] + [app.common.test-helpers.ids-map :as cthi] [app.common.uuid :as uuid] [app.config :as cf] [app.main.data.workspace.media :as media] + [app.main.store :as st] [beicon.v2.core :as rx] [cljs.test :as t :include-macros true] - [frontend-tests.helpers.http :as http])) + [frontend-tests.helpers.async :as async] + [frontend-tests.helpers.http :as http] + [frontend-tests.helpers.mock :as mock] + [frontend-tests.helpers.state :as ths] + [potok.v2.core :as ptk] + [tubax.core :as tubax])) + +(t/use-fixtures :each + {:before cthi/reset-idmap!}) ;; --------------------------------------------------------------------------- ;; Local helpers @@ -187,3 +199,62 @@ (http/restore-fetch! orig) (t/is (= 3 @chunk-calls)) (done)))))))) + +;; --------------------------------------------------------------------------- +;; SVG component creation: any file extension must be stripped from both +;; the component name and the main instance root shape name. +;; --------------------------------------------------------------------------- + +(def ^:private svg-content + (str "" + "" + "")) + +;; The `sax` XML parser used by `tubax/xml->clj` is not available in the +;; ESM test build, so the parse step is mocked with an equivalent tree. +;; Everything else (name handling included) runs for real. +(def ^:private parsed-svg + {:tag :svg + :attrs {:xmlns "http://www.w3.org/2000/svg" + :width "100" :height "100" + :viewBox "0 0 100 100"} + :content [{:tag :rect + :attrs {:x "0" :y "0" :width "100" :height "100"} + :content []}]}) + +(t/deftest ^:async upload-svg-component-strips-svg-extension + (t/testing "creating a component from an uploaded SVG removes the extension from both names" + ;; Any extension must be stripped (not only ".svg"), including + ;; uppercase ones, extensions in the middle of the name, and + ;; filenames with no extension at all. + (doseq [[filename expected] + [["my-component.svg" "my-component"] + ["icon.SVG" "icon"] + ["my.svg.icon.svg" "my.svg.icon"] + ["design" "design"]]] + (let [file (cthf/sample-file :file1) + store (ths/setup-store file) + blob (js/File. #js [svg-content] + filename + #js {:type "image/svg+xml"})] + (await + (mock/with-mocks* + {st/state store + st/emit! (mock/stub (fn [& events] (apply ptk/emit! store events))) + tubax/xml->clj (mock/stub (fn [_source] parsed-svg))} + ;; The SVG is placed at the viewport center; without a vbox the + ;; geometry setup would receive nil coordinates. + (ptk/emit! store #(assoc % :workspace-local {:vbox {:x 0 :y 0 :width 800 :height 600}})) + (ptk/emit! store (media/upload-media-components {:file-id (:id file) + :blobs [blob]})) + (await (async/wait-for #(seq (get-in @store [:files (:id file) :data :components])) + "component created")) + (let [data (get-in @store [:files (:id file) :data]) + component (first (vals (:components data))) + root (get-in data [:pages-index (:main-instance-page component) + :objects (:main-instance-id component)])] + (t/is (some? component) (str "component not created for " filename)) + (t/is (some? root) (str "root not created for " filename)) + (t/is (= expected (:name component))) + (t/is (= expected (:name root))) + (t/is (= expected (cpn/merge-path-item (:path component) (:name component)))))))))))