A platform for community discussion. Free, open, simple.
FIX: Preserve quote structure when pasting quotes starting with lists (#36038)
Martin Brennane122d8a8acaf
diff --git a/.github/labeler.yml b/.github/labeler.ymlindex 0745b891272..b3f6ff7ee6a 100644--- a/.github/labeler.yml+++ b/.github/labeler.yml@@ -175,3 +175,9 @@ i18n:- config/locales/*en.yml- plugins/**/config/locales/*en.yml- "**/*i18n*"++rte:+ - changed-files:+ - any-glob-to-any-file:+ - frontend/discourse/app/static/prosemirror/**/*+ - spec/system/composer/prosemirror_editor_spec.rbdiff --git a/frontend/discourse/app/static/prosemirror/extensions/quote.js b/frontend/discourse/app/static/prosemirror/extensions/quote.jsindex 285d4b48fd5..fa79db0188b 100644--- a/frontend/discourse/app/static/prosemirror/extensions/quote.js+++ b/frontend/discourse/app/static/prosemirror/extensions/quote.js@@ -124,6 +124,24 @@ const extension = {return new Plugin({props: {transformPasted(slice, view) {+ // Prevent quotes that start with a list from being unwrapped during paste.+ // When a quote's first child is a list, Slice.maxOpen opens it too deeply+ // (openStart: 4), causing ProseMirror to unwrap the quote during paste.+ // We detect this case and limit the opening depth.+ let wasFixed = false;+ const firstChild = slice.content.firstChild;+ if (+ firstChild?.type.name === "quote" &&+ slice.openStart > 2 &&+ (firstChild.firstChild?.type.name === "bullet_list" ||+ firstChild.firstChild?.type.name === "ordered_list")+ ) {+ // Limit opening to preserve quote structure while still allowing+ // normal paste merging for quotes with mixed content+ slice = new Slice(slice.content, 1, 1);+ wasFixed = true;+ }+if (view.endOfTextblock("forward") &&slice.content.childCount === 1 &&@@ -132,7 +150,16 @@ const extension = {const quote = slice.content.firstChild;const paragraph = view.state.schema.nodes.paragraph.create();- return Slice.maxOpen(Fragment.from([quote, paragraph]), false);+ // If we fixed the slice above, preserve those open values.+ // Otherwise use the original behavior (maxOpen with openIsolating=false)+ const result = wasFixed+ ? new Slice(+ Fragment.from([quote, paragraph]),+ slice.openStart,+ slice.openEnd+ )+ : Slice.maxOpen(Fragment.from([quote, paragraph]), false);+ return result;}return slice;diff --git a/spec/system/composer/prosemirror_editor_spec.rb b/spec/system/composer/prosemirror_editor_spec.rbindex 46368415f08..32288556dd5 100644--- a/spec/system/composer/prosemirror_editor_spec.rb+++ b/spec/system/composer/prosemirror_editor_spec.rb@@ -2151,5 +2151,67 @@ describe "Composer - ProseMirror editor", type: :system doexpect(rich).to have_css("aside.quote blockquote p", text: "Inside")expect(rich).to have_css("aside.quote + p", text: "Outside")end++ it "converts quotes with mixed content into the correct HTML" do+ cdp.allow_clipboard+ open_composer++ cdp.copy_paste(<<~QUOTE)+ [quote="john, post:1, topic:1"]+ This is a quote with a link: https://example.com++ And also a list:++ * Item 1+ * Item 2+ [/quote]+ QUOTE++ expect(rich).to have_css("aside.quote blockquote p", text: "This is a quote with a link: ")+ expect(rich).to have_css(+ "aside.quote blockquote p a[href='https://example.com']",+ text: "https://example.com",+ )+ expect(rich).to have_css("aside.quote blockquote ul li", text: "Item 1")+ expect(rich).to have_css("aside.quote blockquote ul li", text: "Item 2")+ end++ it "converts quotes with only a list into the correct HTML" do+ cdp.allow_clipboard+ open_composer++ cdp.copy_paste(<<~QUOTE)+ [quote="john, post:1, topic:1"]+ * Item 1+ * Item 2+ [/quote]+ QUOTE++ expect(rich).to have_css("aside.quote blockquote ul li", text: "Item 1")+ expect(rich).to have_css("aside.quote blockquote ul li", text: "Item 2")+ end++ it "converts quotes that start with a list into the correct HTML" do+ cdp.allow_clipboard+ open_composer++ cdp.copy_paste(<<~QUOTE)+ [quote="john, post:1, topic:1"]+ * Item 1+ * Item 2++ This is some post-list text.++ ```ruby+ puts "and some code"+ ```+ [/quote]+ QUOTE++ expect(rich).to have_css("aside.quote blockquote ul li", text: "Item 1")+ expect(rich).to have_css("aside.quote blockquote ul li", text: "Item 2")+ expect(rich).to have_css("aside.quote blockquote p", text: "This is some post-list text.")+ expect(rich).to have_css("aside.quote blockquote pre code", text: 'puts "and some code"')+ endendend