Skip to content

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

UX: only apply link formats on paste to selections that do not contain bbcode-like tags (#15204)

Kerry Liuc8af3e7bc1d7
diff --git a/app/assets/javascripts/discourse/app/mixins/textarea-text-manipulation.js b/app/assets/javascripts/discourse/app/mixins/textarea-text-manipulation.js
index f8ec19f1ff1..2e2de5c5ef6 100644
--- a/app/assets/javascripts/discourse/app/mixins/textarea-text-manipulation.js
+++ b/app/assets/javascripts/discourse/app/mixins/textarea-text-manipulation.js
@@ -286,7 +286,10 @@ export default Mixin.create({
plainText &&
!handled &&
selected.end > selected.start &&
- !this._cachedLinkify.test(selectedValue)
+ // text selection does not contain url
+ !this._cachedLinkify.test(selectedValue) &&
+ // text selection does not contain a bbcode-like tag
+ !selectedValue.match(/\[\/?[a-z =]+?\]/g)
) {
if (this._cachedLinkify.test(plainText)) {
const match = this._cachedLinkify.match(plainText)[0];
diff --git a/app/assets/javascripts/discourse/tests/integration/components/d-editor-test.js b/app/assets/javascripts/discourse/tests/integration/components/d-editor-test.js
index 623739695d6..da9cf70f094 100644
--- a/app/assets/javascripts/discourse/tests/integration/components/d-editor-test.js
+++ b/app/assets/javascripts/discourse/tests/integration/components/d-editor-test.js
@@ -846,6 +846,19 @@ third line`
}
);
+ testCase(
+ `pasting a url onto a selection that contains bbcode-like tags will use default paste behavior`,
+ async function (assert, textarea) {
+ this.set("value", "hello [url=foobar]foobar[/url]");
+ setTextareaSelection(textarea, 0, 30);
+ const element = query(".d-editor");
+ const event = await paste(element, "https://www.discourse.com/");
+ // Synthetic paste events do not manipulate document content.
+ assert.strictEqual(this.value, "hello [url=foobar]foobar[/url]");
+ assert.strictEqual(event.defaultPrevented, false);
+ }
+ );
+
(() => {
// Tests to check cursor/selection after replace-text event.
const BEFORE = "red green blue";