FIX: relative links in the insert hyperlink modal (#30842)

When trying to insert schemaless or relative links using the "insert
hyperlink modal" in the composer, the resulting link would be wrongly
prefixed with "https://"
This commit is contained in:
Régis Hanol 2025-01-17 11:26:28 +01:00 committed by GitHub
parent 560c1875a5
commit 359bbbe617
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 24 additions and 3 deletions

View File

@ -483,9 +483,21 @@ export function setURLContainer(container) {
}
export function prefixProtocol(url) {
return !url.includes("://") && !url.startsWith("mailto:")
? "https://" + url
: url;
if (!url || typeof url !== "string") {
return url;
}
url = url.trim();
if (url.startsWith("//")) {
return `https:${url}`;
}
if (url.startsWith("/") || url.includes("://") || url.startsWith("mailto:")) {
return url;
}
return `https://${url}`;
}
export function getCategoryAndTagUrl(category, subcategories, tag) {

View File

@ -184,6 +184,15 @@ module("Unit | Utility | url", function (hooks) {
prefixProtocol("www.discourse.org/mailto:foo"),
"https://www.discourse.org/mailto:foo"
);
assert.strictEqual(
prefixProtocol("http://www.discourse.org"),
"http://www.discourse.org"
);
assert.strictEqual(
prefixProtocol("ftp://www.discourse.org"),
"ftp://www.discourse.org"
);
assert.strictEqual(prefixProtocol("/my/preferences"), "/my/preferences");
});
test("getCategoryAndTagUrl", function (assert) {