From 30acd12f9808f74eede43df160c330bdea320656 Mon Sep 17 00:00:00 2001 From: "alonso.torres" Date: Sat, 14 Nov 2026 08:37:57 +0100 Subject: [PATCH] :bug: Index an empty paragraph's run before adding it to the runs The letters of a text keep, for a paragraph with no letters, the index of its run among the runs. It was counted after the run was added to the transient list, so it pointed one past the run: at the next paragraph's run, or past the end when the empty paragraph came last. Comparing the styles of such a copy with its main failed, and importing the Penpot Design System template stopped in the migration that recomputes old text overrides. Benchmark (lower quantile, ms): | world | baseline | now | mean | ratio | |------------------------------------+----------+---------+---------+-------| | deepest-edit-through-three-levels | 35.215 | 21.783 | 24.854 | 0.62 | | edit-through-a-chain-of-dependents | 68.931 | 47.467 | 53.332 | 0.69 | | library-update-into-copies | 83.083 | 70.859 | 102.070 | 0.85 | | main-edit-among-many-shapes | 4.837 | 3.322 | 3.654 | 0.69 | | main-edit-into-10-copies | 1.79 | 1.200 | 1.572 | 0.67 | | main-edit-into-100-copies | 15.415 | 11.457 | 18.418 | 0.74 | | main-edit-into-1000-copies | 153.086 | 109.634 | 125.910 | 0.72 | | variant-switch-into-copies | 32.052 | 30.110 | 35.890 | 0.94 | AI-assisted-by: claude-opus-5-5 --- common/src/app/common/types/text.cljc | 6 ++++-- common/test/common_tests/types/text_letters_test.cljc | 11 ++++++++++- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/common/src/app/common/types/text.cljc b/common/src/app/common/types/text.cljc index a7b42372bf..3353729023 100644 --- a/common/src/app/common/types/text.cljc +++ b/common/src/app/common/types/text.cljc @@ -690,11 +690,13 @@ no-letters? (not (some #(seq (:text % "")) (:children paragraph))) node (dissoc paragraph :children)] (if no-letters? - (let [run (-> paragraph :children first (dissoc :text))] + (let [run (-> paragraph :children first (dissoc :text)) + ;; its index in `runs`, taken before the run is added to them + ri (count runs)] (recur (inc i) (conj! runs {:paragraph i :node run :empty? true}) letters - (conj! paragraphs' (assoc node :empty-run run :empty-ri (count runs))))) + (conj! paragraphs' (assoc node :empty-run run :empty-ri ri)))) (recur (inc i) runs letters (conj! paragraphs' node)))))))) (defn letters->content diff --git a/common/test/common_tests/types/text_letters_test.cljc b/common/test/common_tests/types/text_letters_test.cljc index cd4c067670..3f56cc4368 100644 --- a/common/test/common_tests/types/text_letters_test.cljc +++ b/common/test/common_tests/types/text_letters_test.cljc @@ -63,7 +63,16 @@ (map #(get-in % [:run :font-weight]) (:letters letters)))) (t/testing "a paragraph with no letters keeps its run" (t/is (= [false true false] (map #(contains? % :empty-run) (:paragraphs letters)))) - (t/is (nil? (some :children (:paragraphs letters))))))) + (t/is (nil? (some :children (:paragraphs letters))))) + (t/testing "and its index among the runs" + (t/is (= 2 (get-in letters [:paragraphs 1 :empty-ri]))) + (t/is (:empty? (get-in letters [:runs 2])))))) + +(t/deftest a-copy-ending-in-an-empty-paragraph-is-compared-with-its-main + ;; the empty paragraph's run is the last of the copy's runs + (let [main (thtx/content [[["Label"]]]) + copy (thtx/content [[["Copy"]] [[""]]])] + (t/is (contains? (cttx/override-groups main copy) :text-content-text)))) (t/deftest letters-build-the-content-back (doseq [text [[[["Hello world"]]]