FIX: ensures insert hyperlink works with mailto (#10867)

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.

diff --git a/app/assets/javascripts/discourse/app/controllers/insert-hyperlink.js b/app/assets/javascripts/discourse/app/controllers/insert-hyperlink.js
index d3821b2..d57eec3 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 5acade5..c5ecda5 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 5c7c06a..cc23b88 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 a830192..51f13c7 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"
+  );
+});

GitHub sha: 85200960

This commit appears in #10867 which was approved by danielwaterworth. It was merged by jjaffeux.

This commit has been mentioned on Discourse Meta. There might be relevant details there:

https://meta.discourse.org/t/links-with-mailto-broken-by-prepended-http/166564/5