From 85200960435959e24f60fd4f3bc63b71f8663ec9 Mon Sep 17 00:00:00 2001 From: Joffrey JAFFEUX Date: Thu, 8 Oct 2020 13:16:07 +0200 Subject: [PATCH] FIX: ensures insert hyperlink works with mailto (#10867) The prefixing logic is moved into a `prefixProtocol` function in lib:url. This commit also renames an incorrectly named test and uses https as default instead of http, in 2020 it's reasonable to think we most likely want https and not http. User can still specify http if required. --- .../app/controllers/insert-hyperlink.js | 4 ++-- .../javascripts/discourse/app/lib/url.js | 6 ++++++ .../acceptance/composer-hyperlink-test.js | 8 ++++---- .../discourse/tests/unit/lib/url-test.js | 18 +++++++++++++++++- 4 files changed, 29 insertions(+), 7 deletions(-) diff --git a/app/assets/javascripts/discourse/app/controllers/insert-hyperlink.js b/app/assets/javascripts/discourse/app/controllers/insert-hyperlink.js index d3821b2aa38..d57eec38b47 100644 --- a/app/assets/javascripts/discourse/app/controllers/insert-hyperlink.js +++ b/app/assets/javascripts/discourse/app/controllers/insert-hyperlink.js @@ -4,6 +4,7 @@ import Controller from "@ember/controller"; import ModalFunctionality from "discourse/mixins/modal-functionality"; import { searchForTerm } from "discourse/lib/search"; import { bind } from "discourse-common/utils/decorators"; +import { prefixProtocol } from "discourse/lib/url"; export default Controller.extend(ModalFunctionality, { _debounced: null, @@ -144,8 +145,7 @@ export default Controller.extend(ModalFunctionality, { actions: { ok() { const origLink = this.linkUrl; - const linkUrl = - origLink.indexOf("://") === -1 ? `http://${origLink}` : origLink; + const linkUrl = prefixProtocol(origLink); const sel = this.toolbarEvent.selected; if (isEmpty(linkUrl)) { diff --git a/app/assets/javascripts/discourse/app/lib/url.js b/app/assets/javascripts/discourse/app/lib/url.js index 5acade559a7..c5ecda5d67e 100644 --- a/app/assets/javascripts/discourse/app/lib/url.js +++ b/app/assets/javascripts/discourse/app/lib/url.js @@ -493,4 +493,10 @@ export function setURLContainer(container) { setOwner(_urlInstance, container); } +export function prefixProtocol(url) { + return url.indexOf("://") === -1 && url.indexOf("mailto:") !== 0 + ? "https://" + url + : url; +} + export default _urlInstance; diff --git a/app/assets/javascripts/discourse/tests/acceptance/composer-hyperlink-test.js b/app/assets/javascripts/discourse/tests/acceptance/composer-hyperlink-test.js index 5c7c06af963..cc23b886710 100644 --- a/app/assets/javascripts/discourse/tests/acceptance/composer-hyperlink-test.js +++ b/app/assets/javascripts/discourse/tests/acceptance/composer-hyperlink-test.js @@ -24,8 +24,8 @@ test("add a hyperlink to a reply", async (assert) => { assert.equal( find(".d-editor-input").val(), - "This is a link to [Google](http://google.com)", - "adds link with url and text, prepends 'http://'" + "This is a link to [Google](https://google.com)", + "adds link with url and text, prepends 'https://'" ); assert.ok( @@ -43,7 +43,7 @@ test("add a hyperlink to a reply", async (assert) => { assert.equal( find(".d-editor-input").val(), "Reset textarea contents.", - "adds link with url and text, prepends 'http://'" + "doesn’t insert anything after cancelling" ); assert.ok( @@ -61,7 +61,7 @@ test("add a hyperlink to a reply", async (assert) => { assert.equal( find(".d-editor-input").val(), - "[Reset](http://somelink.com) textarea contents.", + "[Reset](https://somelink.com) textarea contents.", "adds link to a selected text" ); diff --git a/app/assets/javascripts/discourse/tests/unit/lib/url-test.js b/app/assets/javascripts/discourse/tests/unit/lib/url-test.js index a830192ae62..51f13c7b91b 100644 --- a/app/assets/javascripts/discourse/tests/unit/lib/url-test.js +++ b/app/assets/javascripts/discourse/tests/unit/lib/url-test.js @@ -1,5 +1,5 @@ import { test, module } from "qunit"; -import DiscourseURL, { userPath } from "discourse/lib/url"; +import DiscourseURL, { userPath, prefixProtocol } from "discourse/lib/url"; import { setPrefix } from "discourse-common/lib/get-url"; import { logIn } from "discourse/tests/helpers/qunit-helpers"; import User from "discourse/models/user"; @@ -80,3 +80,19 @@ test("routeTo with prefix", async (assert) => { "it should navigate to the messages page" ); }); + +test("prefixProtocol", async (assert) => { + assert.equal( + prefixProtocol("mailto:mr-beaver@aol.com"), + "mailto:mr-beaver@aol.com" + ); + assert.equal(prefixProtocol("discourse.org"), "https://discourse.org"); + assert.equal( + prefixProtocol("www.discourse.org"), + "https://www.discourse.org" + ); + assert.equal( + prefixProtocol("www.discourse.org/mailto:foo"), + "https://www.discourse.org/mailto:foo" + ); +});