A platform for community discussion. Free, open, simple.
FIX: stop double encoding URLs when pasting via link UI (#34834)
Samf20b71be158c
diff --git a/app/assets/javascripts/discourse/app/components/modal/upsert-hyperlink.gjs b/app/assets/javascripts/discourse/app/components/modal/upsert-hyperlink.gjsindex 56569086e97..5f534f5f1d2 100644--- a/app/assets/javascripts/discourse/app/components/modal/upsert-hyperlink.gjs+++ b/app/assets/javascripts/discourse/app/components/modal/upsert-hyperlink.gjs@@ -145,13 +145,30 @@ export default class UpsertHyperlink extends Component {@actiononFormSubmit(data) {const origLink = data.linkUrl;- const linkUrl = encodeURI(prefixProtocol(origLink));- const sel = this.args.model.toolbarEvent.selected;+ let linkUrl = prefixProtocol(origLink);++ if (typeof linkUrl === "string") {+ try {+ linkUrl = encodeURI(decodeURI(linkUrl));+ } catch {+ try {+ let withPercentEncoded = linkUrl.replace(+ /%(?![0-9A-Fa-f]{2})/g,+ "%25"+ );+ linkUrl = encodeURI(decodeURI(withPercentEncoded));+ } catch {+ // give up and do something+ linkUrl = encodeURI(linkUrl);+ }+ }+ }if (isEmpty(linkUrl)) {return;}+ const sel = this.args.model.toolbarEvent.selected;const linkText = data.linkText || sel.value || origLink || "";this.args.model.toolbarEvent.addText(`[${linkText.trim()}](${linkUrl})`);diff --git a/app/assets/javascripts/discourse/app/static/prosemirror/extensions/link-toolbar.js b/app/assets/javascripts/discourse/app/static/prosemirror/extensions/link-toolbar.jsindex d9fcd2a1158..16e4af84bc6 100644--- a/app/assets/javascripts/discourse/app/static/prosemirror/extensions/link-toolbar.js+++ b/app/assets/javascripts/discourse/app/static/prosemirror/extensions/link-toolbar.js@@ -195,7 +195,7 @@ class LinkToolbarPluginView {model: {editing: true,linkText: currentLinkText,- linkUrl: decodeURI(this.#linkState.href),+ linkUrl: this.#linkState.href,toolbarEvent: {addText: (text) => this.#replaceText(text),selected: { value: this.#linkState.href },diff --git a/spec/system/composer/prosemirror_editor_spec.rb b/spec/system/composer/prosemirror_editor_spec.rbindex 373b51b8691..9c23324c240 100644--- a/spec/system/composer/prosemirror_editor_spec.rb+++ b/spec/system/composer/prosemirror_editor_spec.rb@@ -1251,6 +1251,57 @@ describe "Composer - ProseMirror editor", type: :system do)end+ it "preserves existing percent escapes when inserting a link" do+ open_composer++ composer.click_toolbar_button("link")++ expect(upsert_hyperlink_modal).to be_open++ upsert_hyperlink_modal.fill_in_link_text("Encoded URL")+ upsert_hyperlink_modal.fill_in_link_url("https://example.com/%20test")+ upsert_hyperlink_modal.click_primary_button++ expect(rich).to have_css("a[href='https://example.com/%20test']", text: "Encoded URL")++ composer.toggle_rich_editor++ expect(composer).to have_value("[Encoded URL](https://example.com/%20test)")+ end++ it "handles malformed links gracefully" do+ cdp.allow_clipboard+ open_composer++ composer.click_toolbar_button("link")++ expect(upsert_hyperlink_modal).to be_open++ upsert_hyperlink_modal.fill_in_link_text("Encoded URL")+ upsert_hyperlink_modal.fill_in_link_url("https://example.com/100%/working 1")+ upsert_hyperlink_modal.click_primary_button++ expect(rich).to have_css(+ "a[href='https://example.com/100%25/working%201']",+ text: "Encoded URL",+ )++ composer.send_keys(:left, :left, :left)+ find("button.composer-link-toolbar__edit").click++ expect(upsert_hyperlink_modal).to be_open+ expect(upsert_hyperlink_modal.link_text_value).to eq("Encoded URL")+ # this ensures we keeps the corrected encoding and do not decode prior to edit+ # if we decode prior to edit user may end up being confused about why the url has spaces etc...+ expect(upsert_hyperlink_modal.link_url_value).to eq("https://example.com/100%25/working%201")++ upsert_hyperlink_modal.close++ composer.toggle_rich_editor++ expect(composer).to have_value("[Encoded URL](https://example.com/100%25/working%201)")+ end+it "allows copying a link URL via toolbar" docdp.allow_clipboardopen_composer