Skip to content

A platform for community discussion. Free, open, simple.

FIX: Keep pasted text that contains a URL among other words (#44337)

Sérgio Saquetimfafc26b2a142
diff --git a/frontend/discourse/app/static/prosemirror/extensions/link.ts b/frontend/discourse/app/static/prosemirror/extensions/link.ts
index 8b75b384ec6..7ed0636b921 100644
--- a/frontend/discourse/app/static/prosemirror/extensions/link.ts
+++ b/frontend/discourse/app/static/prosemirror/extensions/link.ts
@@ -184,7 +184,9 @@ const extension: RichEditorExtension = {
return slice;
}
- return addLinkMark(view, node.text, utils);
+ // Every transformPasted hook receives the previous one's result, so
+ // hand the slice on unchanged when the text is not a lone link.
+ return addLinkMark(view, node.text, utils) ?? slice;
},
},
diff --git a/frontend/discourse/tests/integration/components/prosemirror-editor/link-test.js b/frontend/discourse/tests/integration/components/prosemirror-editor/link-test.js
index f19810955cd..458238a9ae8 100644
--- a/frontend/discourse/tests/integration/components/prosemirror-editor/link-test.js
+++ b/frontend/discourse/tests/integration/components/prosemirror-editor/link-test.js
@@ -1,6 +1,11 @@
+import { settled } from "@ember/test-helpers";
+import { TextSelection } from "prosemirror-state";
import { module, test } from "qunit";
import { setupRenderingTest } from "discourse/tests/helpers/component-test";
-import { testMarkdown } from "discourse/tests/helpers/rich-editor-helper";
+import {
+ setupRichEditor,
+ testMarkdown,
+} from "discourse/tests/helpers/rich-editor-helper";
module(
"Integration | Component | prosemirror-editor - link extension",
@@ -48,5 +53,41 @@ module(
await testMarkdown(assert, markdown, html, expectedMarkdown);
});
});
+
+ module("pasting rich content over a selection", function () {
+ async function pasteOverWorld(assert, html) {
+ const [editor] = await setupRichEditor(assert, "Hello world");
+ const { view } = editor;
+ view.dispatch(
+ view.state.tr.setSelection(
+ TextSelection.create(view.state.doc, 7, 12)
+ )
+ );
+ view.pasteHTML(html);
+ await settled();
+ return editor;
+ }
+
+ test("a lone URL links the selection", async function (assert) {
+ const editor = await pasteOverWorld(
+ assert,
+ "<span>https://discourse.org</span>"
+ );
+
+ assert.strictEqual(
+ editor.value,
+ "Hello [world](https://discourse.org)"
+ );
+ });
+
+ test("text with a URL among other words replaces the selection", async function (assert) {
+ const editor = await pasteOverWorld(
+ assert,
+ "<span>see https://discourse.org</span>"
+ );
+
+ assert.strictEqual(editor.value, "Hello see https://discourse.org");
+ });
+ });
}
);