Skip to content

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

FIX: Further improvements to `//` handling (#28211)

David Taylor1b2e0e86f8f0
diff --git a/app/assets/javascripts/discourse/app/lib/url.js b/app/assets/javascripts/discourse/app/lib/url.js
index 7a942426c0c..c2dcfe26bbc 100644
--- a/app/assets/javascripts/discourse/app/lib/url.js
+++ b/app/assets/javascripts/discourse/app/lib/url.js
@@ -235,7 +235,7 @@ class DiscourseURL extends EmberObject {
const oldPath = this.routerService.currentURL;
- path = path.replace(/(https?\:)?\/\/[^\/]+/, "");
+ path = path.replace(/^(https?\:)?\/\/[^\/]+/, "");
// handle prefixes
if (path.startsWith("/")) {
@@ -437,6 +437,10 @@ class DiscourseURL extends EmberObject {
elementId = split[1];
}
+ // Remove multiple consecutive slashes from path. Same as Ember does on initial page load:
+ // https://github.com/emberjs/ember.js/blob/8abcd000ee/packages/%40ember/routing/history-location.ts#L146
+ path = path.replaceAll(/\/\/+/g, "/");
+
const transition = this.routerService.transitionTo(path);
transition._discourse_intercepted = true;
diff --git a/app/assets/javascripts/discourse/tests/acceptance/url-test.js b/app/assets/javascripts/discourse/tests/acceptance/url-test.js
new file mode 100644
index 00000000000..9c6b6a15b51
--- /dev/null
+++ b/app/assets/javascripts/discourse/tests/acceptance/url-test.js
@@ -0,0 +1,19 @@
+import { currentURL, settled, visit } from "@ember/test-helpers";
+import { test } from "qunit";
+import DiscourseURL from "discourse/lib/url";
+import { acceptance } from "discourse/tests/helpers/qunit-helpers";
+
+acceptance("DiscourseURL", function () {
+ test("handleURL strips multiple slashes", async function (assert) {
+ await visit("/");
+
+ DiscourseURL.handleURL("/t//280");
+ await settled();
+
+ assert.strictEqual(
+ currentURL(),
+ "/t/internationalization-localization/280"
+ );
+ assert.dom("#topic-title").exists();
+ });
+});
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 8efc03846bf..7c169f29d28 100644
--- a/app/assets/javascripts/discourse/tests/unit/lib/url-test.js
+++ b/app/assets/javascripts/discourse/tests/unit/lib/url-test.js
@@ -141,11 +141,17 @@ module("Unit | Utility | url", function (hooks) {
"it strips the protocol and domain when protocol-less"
);
- DiscourseURL.routeTo("https://example.com//foo4");
+ DiscourseURL.routeTo("https://example.com/t//1");
assert.ok(
- DiscourseURL.handleURL.calledWith(`//foo4`),
+ DiscourseURL.handleURL.calledWith(`/t//1`),
"it does not strip double-slash in the middle of urls"
);
+
+ DiscourseURL.routeTo("/t//2");
+ assert.ok(
+ DiscourseURL.handleURL.calledWith(`/t//2`),
+ "it does not strip double-slash in the middle of urls, even without a domain"
+ );
});
test("routeTo does not rewrite routes started with /my", async function (assert) {