A platform for community discussion. Free, open, simple.
FIX: Preserve pasted text inside tilde code fences (#43711)
Alan Guo Xiang Tan258f39ff5d42
diff --git a/frontend/discourse/app/lib/textarea-text-manipulation.ts b/frontend/discourse/app/lib/textarea-text-manipulation.tsindex 44fa00b52a9..b68576af97b 100644--- a/frontend/discourse/app/lib/textarea-text-manipulation.ts+++ b/frontend/discourse/app/lib/textarea-text-manipulation.ts@@ -484,7 +484,7 @@ export default class TextareaTextManipulation implements TextManipulation {if (canPasteHtml && plainText) {if (isInlinePasting) {canPasteHtml = !(- lineVal.match(/^```/) ||+ isCodeBlock ||this.isInside(pre, /`/g) ||lineVal.match(/^ /));@@ -1089,8 +1089,22 @@ export default class TextareaTextManipulation implements TextManipulation {};}- #isAfterStartedCodeFence(beforeText: string): number | null {- return this.isInside(beforeText, /(^|\n)```/g);+ #isAfterStartedCodeFence(beforeText: string): boolean {+ let openingFence: string | undefined;++ for (const [, fence, trailingText] of beforeText.matchAll(+ /^ {0,3}(`{3,}|~{3,})(.*)$/gm+ )) {+ if (openingFence) {+ if (fence.startsWith(openingFence) && !trailingText.trim()) {+ openingFence = undefined;+ }+ } else if (fence[0] === "~" || !trailingText.includes("`")) {+ openingFence = fence;+ }+ }++ return Boolean(openingFence);}// perform the same operation over many lines of textdiff --git a/spec/system/composer/prosemirror_pasting_spec.rb b/spec/system/composer/prosemirror_pasting_spec.rbindex acaf591e9af..77a260183eb 100644--- a/spec/system/composer/prosemirror_pasting_spec.rb+++ b/spec/system/composer/prosemirror_pasting_spec.rb@@ -67,6 +67,21 @@ describe "Composer - ProseMirror - Pasting content" doexpect(composer).to have_value("not selected **[bold](www.example.com)** not selected")end+ it "pastes plain text inside tilde-fenced code blocks and formatted text outside them" do+ open_composer+ composer.toggle_rich_editor+ composer.fill_content("~~~console\nprefix \n~~~")+ composer.move_cursor_after("prefix ")+ html = "<strong>bold</strong>"+ plain_text = "bold"+ cdp.copy_paste(html, html: true, plain_text: plain_text)+ expect(composer).to have_value("~~~console\nprefix bold\n~~~")++ composer.fill_content("```\n~~~\n```\n")+ cdp.copy_paste(html, html: true, plain_text: plain_text)+ expect(composer).to have_value("```\n~~~\n```\n**bold**")+ end+it "removes newlines from alt/title in pasted image" docdp.allow_clipboardopen_composerdiff --git a/spec/system/page_objects/cdp.rb b/spec/system/page_objects/cdp.rbindex 1f3ea903bc0..27008e4e6be 100644--- a/spec/system/page_objects/cdp.rb+++ b/spec/system/page_objects/cdp.rb@@ -45,16 +45,17 @@ module PageObjectspage.evaluate_async_script("navigator.clipboard.readText().then(arguments[0])")end- def write_clipboard(content, html: false)+ def write_clipboard(content, html: false, plain_text: content)if htmlpage.evaluate_async_script("navigator.clipboard.write([new ClipboardItem({'text/html': new Blob([arguments[0]], { type: 'text/html' }),- 'text/plain': new Blob([arguments[0]], { type: 'text/plain' })+ 'text/plain': new Blob([arguments[1]], { type: 'text/plain' })})- ]).then(arguments[1])",+ ]).then(arguments[2])",content,+ plain_text,)elsepage.evaluate_async_script(@@ -83,9 +84,9 @@ module PageObjectsexpect(clipboard_text).to strict ? eq(text) : include(text)end- def copy_paste(text, html: false, css_selector: nil)+ def copy_paste(text, html: false, plain_text: text, css_selector: nil)allow_clipboard- write_clipboard(text, html: html)+ write_clipboard(text, html: html, plain_text: plain_text)paste(css_selector:)end