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

Followup to f70a65ea02

1. Update a second regex in `routeTo` to avoid stripping domain/protocol from middle of string

2. Update `URL.handleURL` to strip double-slashes in paths, before calling the ember router. This mimics what Ember does on initial page-load

Additional tests are added for both
This commit is contained in:
David Taylor 2024-08-02 18:11:11 +01:00 committed by GitHub
parent 18ac600352
commit 1b2e0e86f8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 32 additions and 3 deletions

View File

@ -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;

View File

@ -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();
});
});

View File

@ -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) {