mirror of
https://github.com/discourse/discourse.git
synced 2024-11-28 05:53:44 +08:00
FIX: Normalize links by converting them to lower case
The server side does this so the same link on the client side with any upper case letters does not warn as a duplicate.
This commit is contained in:
parent
ad4fda4722
commit
3b81c2d470
|
@ -1,6 +1,10 @@
|
|||
const _warned = {};
|
||||
let _warned = {};
|
||||
const NO_RESULT = [false, null];
|
||||
|
||||
export function reset() {
|
||||
_warned = {};
|
||||
}
|
||||
|
||||
export default class LinkLookup {
|
||||
constructor(links) {
|
||||
this._links = links;
|
||||
|
@ -11,7 +15,10 @@ export default class LinkLookup {
|
|||
return NO_RESULT;
|
||||
}
|
||||
|
||||
const normalized = href.replace(/^https?:\/\//, "").replace(/\/$/, "");
|
||||
const normalized = href
|
||||
.toLowerCase()
|
||||
.replace(/^https?:\/\//, "")
|
||||
.replace(/\/$/, "");
|
||||
if (_warned[normalized]) {
|
||||
return NO_RESULT;
|
||||
}
|
||||
|
|
|
@ -0,0 +1,48 @@
|
|||
import LinkLookup, { reset } from "discourse/lib/link-lookup";
|
||||
import { module, test } from "qunit";
|
||||
import Post from "discourse/models/post";
|
||||
|
||||
module("Unit | Utility | link-lookup", function (hooks) {
|
||||
hooks.afterEach(() => reset());
|
||||
hooks.beforeEach(function () {
|
||||
this.post = Post.create();
|
||||
this.linkLookup = new LinkLookup({
|
||||
"en.wikipedia.org/wiki/handheld_game_console": {
|
||||
post_number: 1,
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
test("works with https", function (assert) {
|
||||
assert.ok(
|
||||
this.linkLookup.check(
|
||||
this.post,
|
||||
"https://en.wikipedia.org/wiki/handheld_game_console"
|
||||
)[0]
|
||||
);
|
||||
});
|
||||
test("works with http", function (assert) {
|
||||
assert.ok(
|
||||
this.linkLookup.check(
|
||||
this.post,
|
||||
"http://en.wikipedia.org/wiki/handheld_game_console"
|
||||
)[0]
|
||||
);
|
||||
});
|
||||
test("works with trailing slash", function (assert) {
|
||||
assert.ok(
|
||||
this.linkLookup.check(
|
||||
this.post,
|
||||
"https://en.wikipedia.org/wiki/handheld_game_console/"
|
||||
)[0]
|
||||
);
|
||||
});
|
||||
test("works with uppercase characters", function (assert) {
|
||||
assert.ok(
|
||||
this.linkLookup.check(
|
||||
this.post,
|
||||
"https://en.wikipedia.org/wiki/Handheld_game_console"
|
||||
)[0]
|
||||
);
|
||||
});
|
||||
});
|
Loading…
Reference in New Issue
Block a user